• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_TESTING_KERNEL_TEST_UTIL_H_
16 #define TENSORFLOW_LITE_TESTING_KERNEL_TEST_UTIL_H_
17 
18 #include <fstream>
19 
20 #include "tensorflow/core/util/command_line_flags.h"
21 #include "tensorflow/lite/c/common.h"
22 #include "tensorflow/lite/testing/kernel_test/input_generator.h"
23 #include "tensorflow/lite/testing/split.h"
24 #include "tensorflow/lite/testing/tflite_driver.h"
25 
26 namespace tflite {
27 namespace testing {
28 namespace kernel_test {
29 
30 struct TestOptions {
31   // Path of tensorflow lite model.
32   string tflite_model;
33   // Path of the input file. If empty, generate at runtime.
34   string read_input_from_file;
35   // Path to dump the input file.
36   string dump_input_to_file;
37   // Path to dump the output.
38   string dump_output_to_file;
39   // Input distribution.
40   string input_distribution;
41   // Kernel type.
42   string kernel_type;
43 };
44 
ParseTfliteKernelTestFlags(int * argc,char ** argv)45 TestOptions ParseTfliteKernelTestFlags(int* argc, char** argv) {
46   TestOptions options;
47   std::vector<tensorflow::Flag> flags = {
48       tensorflow::Flag("tflite_model", &options.tflite_model,
49                        "Path of tensorflow lite model."),
50       tensorflow::Flag("read_input_from_file", &options.read_input_from_file,
51                        "File to read input data from. If empty, generates "
52                        "input at runtime."),
53       tensorflow::Flag("dump_input_to_file", &options.dump_input_to_file,
54                        "File to dump randomly generated input."),
55       tensorflow::Flag("dump_output_to_file", &options.dump_output_to_file,
56                        "File to dump output."),
57       tensorflow::Flag("input_distribution", &options.input_distribution,
58                        "Input distribution. Default: Gaussian."),
59       tensorflow::Flag("kernel_type", &options.kernel_type, "Kernel type."),
60   };
61 
62   tensorflow::Flags::Parse(argc, argv, flags);
63 
64   return options;
65 }
66 
RunKernelTest(const kernel_test::TestOptions & options,TestRunner * runner)67 TfLiteStatus RunKernelTest(const kernel_test::TestOptions& options,
68                            TestRunner* runner) {
69   InputGenerator input_generator;
70 
71   if (options.read_input_from_file.empty()) {
72     TF_LITE_ENSURE_STATUS(input_generator.LoadModel(options.tflite_model));
73     TF_LITE_ENSURE_STATUS(
74         input_generator.GenerateInput(options.input_distribution));
75   } else {
76     TF_LITE_ENSURE_STATUS(
77         input_generator.ReadInputsFromFile(options.read_input_from_file));
78   }
79 
80   runner->LoadModel(options.tflite_model);
81   runner->AllocateTensors();
82   if (!runner->IsValid()) return kTfLiteError;
83   auto input_tensor_ids = runner->GetInputs();
84   auto inputs = input_generator.GetInputs();
85   if (inputs.size() != input_tensor_ids.size()) {
86     fprintf(stderr,
87             "Number of input tensors generated doesn't match what the model "
88             "asks for.");
89   }
90   for (int i = 0; i < inputs.size(); i++) {
91     runner->SetInput(input_tensor_ids[i], inputs[i]);
92   }
93 
94   runner->Invoke();
95 
96   if (!options.dump_input_to_file.empty()) {
97     TF_LITE_ENSURE_STATUS(
98         input_generator.WriteInputsToFile(options.dump_input_to_file));
99   }
100 
101   if (!options.dump_output_to_file.empty()) {
102     std::ofstream output_file;
103     output_file.open(options.dump_output_to_file,
104                      std::fstream::out | std::fstream::trunc);
105     if (!output_file) {
106       return kTfLiteError;
107     }
108 
109     for (auto id : runner->GetOutputs()) {
110       output_file << runner->ReadOutput(id) << "\n";
111     }
112     output_file.close();
113   }
114 
115   return kTfLiteOk;
116 }
117 
118 }  // namespace kernel_test
119 }  // namespace testing
120 }  // namespace tflite
121 
122 #endif  // TENSORFLOW_LITE_TESTING_KERNEL_TEST_UTIL_H_
123