tensorflow/lite/toco/graph_transformations/resolve_constant_reshape.cc
// Resolves a constant reshape operation by copying the buffer.
Resolves a constant reshape operation by copying the buffer.
Resolves a constant reshape operation by copying the buffer .
::tensorflow::Status ResolveConstantReshape::Run(Model* model,
                                                 std::size_t op_index,
                                                 bool* modified) {
  *modified = false;
  auto it = model->operators.begin() + op_index;
  const auto* base_op = it->get();
  if (base_op->type != OperatorType::kReshape) {
    return ::tensorflow::Status::OK();
  }
  const auto* op = static_cast<const TensorFlowReshapeOperator*>(base_op);
  CHECK_EQ(op->inputs.size(), 2);
  CHECK_EQ(op->outputs.size(), 1);
  if (!IsConstantParameterArray(*model, op->inputs[0]) ||
      !IsConstantParameterArray(*model, op->inputs[1])) {
    return ::tensorflow::Status::OK();
  }
  auto& output_array = model->GetArray(op->outputs[0]);
  if (output_array.data_type == ArrayDataType::kNone) {
    return ::tensorflow::Status::OK();
  }
  if (!output_array.has_shape()) {
    return ::tensorflow::Status::OK();
  }
  const Array& input_array = model->GetArray(op->inputs[0]);
  if (!ShapesAgreeUpToExtending(input_array.shape(), output_array.shape())) {
    AddMessageF("Constant reshape is non-trivial (%s -> %s)",
                ShapeToString(input_array.shape()),
                ShapeToString(output_array.shape()));
    return ::tensorflow::Status::OK();
  }
  CHECK(!output_array.buffer);
  switch (input_array.data_type) {
    case ArrayDataType::kBool:
      CopyArrayBuffer<ArrayDataType::kBool>(input_array, &output_array);
      break;
    case ArrayDataType::kFloat:
      CopyArrayBuffer<ArrayDataType::kFloat>(input_array, &output_array);
      break;
    case ArrayDataType::kInt8:
      CopyArrayBuffer<ArrayDataType::kInt8>(input_array, &output_array);
      break;
    case ArrayDataType::kUint8:
      CopyArrayBuffer<ArrayDataType::kUint8>(input_array, &output_array);
      break;
    case ArrayDataType::kInt16:
      CopyArrayBuffer<ArrayDataType::kInt16>(input_array, &output_array);
      break;
    case ArrayDataType::kUint16:
      CopyArrayBuffer<ArrayDataType::kUint16>(input_array, &output_array);
      break;
    case ArrayDataType::kInt32:
      CopyArrayBuffer<ArrayDataType::kInt32>(input_array, &output_array);
      break;
    case ArrayDataType::kUint32:
      CopyArrayBuffer<ArrayDataType::kUint32>(input_array, &output_array);
      break;
    case ArrayDataType::kInt64:
      CopyArrayBuffer<ArrayDataType::kInt64>(input_array, &output_array);
      break;
    case ArrayDataType::kUint64:
      CopyArrayBuffer<ArrayDataType::kUint64>(input_array, &output_array);
      break;
    case ArrayDataType::kString:
      CopyArrayBuffer<ArrayDataType::kString>(input_array, &output_array);
      break;
    case ArrayDataType::kComplex64:
      CopyArrayBuffer<ArrayDataType::kComplex64>(input_array, &output_array);
      break;
    default:
      LOG(FATAL) << "Unsupported data type: "
                 << ArrayDataTypeName(input_array.data_type);
      return ::tensorflow::Status::OK();
  }
  AddMessageF("Resolving constant reshape of %s", LogName(*op));
  CopyMinMaxAndQuantizationRelatedFields(input_array, &output_array);
  DeleteOpAndArrays(model, op);
  *modified = true;
  return ::tensorflow::Status::OK();
}
:: tensorflow :: Status ResolveConstantReshape :: Run ( Model * model , std :: size_t op_index , bool * modified ) { * modified = false ; auto it = model -> operators . begin ( ) + op_index ; const auto * base_op = it -> get ( ) ; if ( base_op -> type != OperatorType :: kReshape ) { return :: tensorflow :: Status :: OK ( ) ; } const auto * op = static_cast < const TensorFlowReshapeOperator * > ( base_op ) ; CHECK_EQ ( op -> inputs . size ( ) , 2 ) ; CHECK_EQ ( op -> outputs . size ( ) , 1 ) ; if ( ! IsConstantParameterArray ( * model , op -> inputs [ 0 ] ) || ! IsConstantParameterArray ( * model , op -> inputs [ 1 ] ) ) { return :: tensorflow :: Status :: OK ( ) ; } auto & output_array = model -> GetArray ( op -> outputs [ 0 ] ) ; if ( output_array . data_type == ArrayDataType :: kNone ) { return :: tensorflow :: Status :: OK ( ) ; } if ( ! output_array . has_shape ( ) ) { return :: tensorflow :: Status :: OK ( ) ; } const Array & input_array = model -> GetArray ( op -> inputs [ 0 ] ) ; if ( ! ShapesAgreeUpToExtending ( input_array . shape ( ) , output_array . shape ( ) ) ) { AddMessageF ( " " , ShapeToString ( input_array . shape ( ) ) , ShapeToString ( output_array . shape ( ) ) ) ; return :: tensorflow :: Status :: OK ( ) ; } CHECK ( ! output_array . buffer ) ; switch ( input_array . data_type ) { case ArrayDataType :: kBool : CopyArrayBuffer < ArrayDataType :: kBool > ( input_array , & output_array ) ; break ; case ArrayDataType :: kFloat : CopyArrayBuffer < ArrayDataType :: kFloat > ( input_array , & output_array ) ; break ; case ArrayDataType :: kInt8 : CopyArrayBuffer < ArrayDataType :: kInt8 > ( input_array , & output_array ) ; break ; case ArrayDataType :: kUint8 : CopyArrayBuffer < ArrayDataType :: kUint8 > ( input_array , & output_array ) ; break ; case ArrayDataType :: kInt16 : CopyArrayBuffer < ArrayDataType :: kInt16 > ( input_array , & output_array ) ; break ; case ArrayDataType :: kUint16 : CopyArrayBuffer < ArrayDataType :: kUint16 > ( input_array , & output_array ) ; break ; case ArrayDataType :: kInt32 : CopyArrayBuffer < ArrayDataType :: kInt32 > ( input_array , & output_array ) ; break ; case ArrayDataType :: kUint32 : CopyArrayBuffer < ArrayDataType :: kUint32 > ( input_array , & output_array ) ; break ; case ArrayDataType :: kInt64 : CopyArrayBuffer < ArrayDataType :: kInt64 > ( input_array , & output_array ) ; break ; case ArrayDataType :: kUint64 : CopyArrayBuffer < ArrayDataType :: kUint64 > ( input_array , & output_array ) ; break ; case ArrayDataType :: kString : CopyArrayBuffer < ArrayDataType :: kString > ( input_array , & output_array ) ; break ; case ArrayDataType :: kComplex64 : CopyArrayBuffer < ArrayDataType :: kComplex64 > ( input_array , & output_array ) ; break ; default : LOG ( FATAL ) << " " << ArrayDataTypeName ( input_array . data_type ) ; return :: tensorflow :: Status :: OK ( ) ; } AddMessageF ( " " , LogName ( * op ) ) ; CopyMinMaxAndQuantizationRelatedFields ( input_array , & output_array ) ; DeleteOpAndArrays ( model , op ) ; * modified = true ; return :: tensorflow :: Status :: OK ( ) ; }
Resolves a constant reshape operation by copying the buffer.
Resolves a constant reshape operation by copying the buffer .
// We require constant inputs. // Yield until the output type has been set by PropagateArrayDataTypes. // Yield until the output shape has been set by PropagateFixedShapes.