tensorflow/lite/tools/serialization/writer_lib_test.cc
// Tests if SetCustomInputOutput handles variable tensors correctly.
Tests if SetCustomInputOutput handles variable tensors correctly.
Tests if SetCustomInputOutput handles variable tensors correctly .
TEST_P(SingleSubgraphTest, CustomInputOutputVariableTensorTest) {
  Interpreter interpreter;
  tflite::ops::builtin::BuiltinOpResolverWithoutDefaultDelegates resolver;
  interpreter.AddTensors(3);
  interpreter.SetTensorParametersReadWrite(0, kTfLiteFloat32, "a", {3},
                                           TfLiteQuantization());
  interpreter.SetTensorParametersReadWrite(1, kTfLiteFloat32, "b", {3},
                                           TfLiteQuantization(),
                                           true);
  interpreter.SetTensorParametersReadWrite(2, kTfLiteFloat32, "c", {3},
                                           TfLiteQuantization());
  interpreter.SetInputs({0});
  interpreter.SetOutputs({2});
  interpreter.SetVariables({1});
  TfLiteAddParams* builtin_data =
      reinterpret_cast<TfLiteAddParams*>(malloc(sizeof(TfLiteAddParams)));
  builtin_data->activation = kTfLiteActNone;
  builtin_data->pot_scale_int16 = false;
  interpreter.AddNodeWithParameters({0, 1}, {2}, nullptr, 0,
                                    reinterpret_cast<void*>(builtin_data),
                                    resolver.FindOp(BuiltinOperator_ADD, 1));
  const std::string test_file = CreateFilePath("test_variables.tflite");
  SubgraphWriter writer(&interpreter.primary_subgraph());
  EXPECT_EQ(writer.SetCustomInputOutput({0}, {2},
                                        {0}),
            kTfLiteOk);
  writer.Write(test_file);
  std::unique_ptr<FlatBufferModel> model =
      FlatBufferModel::BuildFromFile(test_file.c_str());
  InterpreterBuilder builder(*model, resolver);
  std::unique_ptr<Interpreter> new_interpreter;
  builder(&new_interpreter);
  CHECK_EQ(new_interpreter->AllocateTensors(), kTfLiteOk);
}
TEST_P ( SingleSubgraphTest , CustomInputOutputVariableTensorTest ) { Interpreter interpreter ; tflite :: ops :: builtin :: BuiltinOpResolverWithoutDefaultDelegates resolver ; interpreter . AddTensors ( 3 ) ; interpreter . SetTensorParametersReadWrite ( 0 , kTfLiteFloat32 , " " , { 3 } , TfLiteQuantization ( ) ) ; interpreter . SetTensorParametersReadWrite ( 1 , kTfLiteFloat32 , " " , { 3 } , TfLiteQuantization ( ) , true ) ; interpreter . SetTensorParametersReadWrite ( 2 , kTfLiteFloat32 , " " , { 3 } , TfLiteQuantization ( ) ) ; interpreter . SetInputs ( { 0 } ) ; interpreter . SetOutputs ( { 2 } ) ; interpreter . SetVariables ( { 1 } ) ; TfLiteAddParams * builtin_data = reinterpret_cast < TfLiteAddParams * > ( malloc ( sizeof ( TfLiteAddParams ) ) ) ; builtin_data -> activation = kTfLiteActNone ; builtin_data -> pot_scale_int16 = false ; interpreter . AddNodeWithParameters ( { 0 , 1 } , { 2 } , nullptr , 0 , reinterpret_cast < void * > ( builtin_data ) , resolver . FindOp ( BuiltinOperator_ADD , 1 ) ) ; const std :: string test_file = CreateFilePath ( " " ) ; SubgraphWriter writer ( & interpreter . primary_subgraph ( ) ) ; EXPECT_EQ ( writer . SetCustomInputOutput ( { 0 } , { 2 } , { 0 } ) , kTfLiteOk ) ; writer . Write ( test_file ) ; std :: unique_ptr < FlatBufferModel > model = FlatBufferModel :: BuildFromFile ( test_file . c_str ( ) ) ; InterpreterBuilder builder ( * model , resolver ) ; std :: unique_ptr < Interpreter > new_interpreter ; builder ( & new_interpreter ) ; CHECK_EQ ( new_interpreter -> AllocateTensors ( ) , kTfLiteOk ) ; }
Tests if SetCustomInputOutput handles variable tensors correctly.
Tests if SetCustomInputOutput handles variable tensors correctly .
// Create tensors. /*is_variable=*/ // Create an Add node. // Write model to file. /*inputs=*/ /*outputs=*/ /*execution_plan=*/ // Read model and test.