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 inline 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 inline 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 inputs = input_generator.GetInputs();
84
85 runner->Invoke(inputs);
86
87 if (!options.dump_input_to_file.empty()) {
88 TF_LITE_ENSURE_STATUS(
89 input_generator.WriteInputsToFile(options.dump_input_to_file));
90 }
91
92 if (!options.dump_output_to_file.empty()) {
93 std::ofstream output_file;
94 output_file.open(options.dump_output_to_file,
95 std::fstream::out | std::fstream::trunc);
96 if (!output_file) {
97 return kTfLiteError;
98 }
99
100 for (const auto& name : runner->GetOutputNames()) {
101 output_file << name << ":" << runner->ReadOutput(name) << "\n";
102 }
103 output_file.close();
104 }
105
106 return kTfLiteOk;
107 }
108
109 } // namespace kernel_test
110 } // namespace testing
111 } // namespace tflite
112
113 #endif // TENSORFLOW_LITE_TESTING_KERNEL_TEST_UTIL_H_
114