1 /* Copyright 2017 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 #include "tensorflow/lite/kernels/test_util.h"
16
17 #include "tensorflow/lite/version.h"
18 #include "tensorflow/core/platform/logging.h"
19
20 namespace tflite {
21
22 using ::testing::FloatNear;
23 using ::testing::Matcher;
24
ArrayFloatNear(const std::vector<float> & values,float max_abs_error)25 std::vector<Matcher<float>> ArrayFloatNear(const std::vector<float>& values,
26 float max_abs_error) {
27 std::vector<Matcher<float>> matchers;
28 matchers.reserve(values.size());
29 for (const float& v : values) {
30 matchers.emplace_back(FloatNear(v, max_abs_error));
31 }
32 return matchers;
33 }
34
ArrayComplex64Near(const std::vector<std::complex<float>> & values,float max_abs_error)35 std::vector<Matcher<std::complex<float>>> ArrayComplex64Near(
36 const std::vector<std::complex<float>>& values, float max_abs_error) {
37 std::vector<Matcher<std::complex<float>>> matchers;
38 matchers.reserve(values.size());
39 for (const std::complex<float>& v : values) {
40 matchers.emplace_back(
41 AllOf(::testing::Property(&std::complex<float>::real,
42 FloatNear(v.real(), max_abs_error)),
43 ::testing::Property(&std::complex<float>::imag,
44 FloatNear(v.imag(), max_abs_error))));
45 }
46 return matchers;
47 }
48
AddInput(const TensorData & t,bool is_variable)49 int SingleOpModel::AddInput(const TensorData& t, bool is_variable) {
50 int id = 0;
51 if (t.per_channel_quantization) {
52 id = AddTensorPerChannelQuant(t);
53 } else {
54 id = AddTensor<float>(t, {}, is_variable);
55 }
56 inputs_.push_back(id);
57 return id;
58 }
59
AddNullInput()60 int SingleOpModel::AddNullInput() {
61 int id = kOptionalTensor;
62 inputs_.push_back(id);
63 return id;
64 }
65
AddOutput(const TensorData & t)66 int SingleOpModel::AddOutput(const TensorData& t) {
67 int id = AddTensor<float>(t, {});
68 outputs_.push_back(id);
69 return id;
70 }
71
SetBuiltinOp(BuiltinOperator type,BuiltinOptions builtin_options_type,flatbuffers::Offset<void> builtin_options)72 void SingleOpModel::SetBuiltinOp(BuiltinOperator type,
73 BuiltinOptions builtin_options_type,
74 flatbuffers::Offset<void> builtin_options) {
75 opcodes_.push_back(CreateOperatorCode(builder_, type, 0));
76 operators_.push_back(CreateOperator(
77 builder_, /*opcode_index=*/0, builder_.CreateVector<int32_t>(inputs_),
78 builder_.CreateVector<int32_t>(outputs_), builtin_options_type,
79 builtin_options,
80 /*custom_options=*/0, CustomOptionsFormat_FLEXBUFFERS));
81 }
82
SetCustomOp(const string & name,const std::vector<uint8_t> & custom_option,const std::function<TfLiteRegistration * ()> & registration)83 void SingleOpModel::SetCustomOp(
84 const string& name, const std::vector<uint8_t>& custom_option,
85 const std::function<TfLiteRegistration*()>& registration) {
86 custom_registrations_[name] = registration;
87 opcodes_.push_back(
88 CreateOperatorCodeDirect(builder_, BuiltinOperator_CUSTOM, name.data()));
89 operators_.push_back(CreateOperator(
90 builder_, /*opcode_index=*/0, builder_.CreateVector<int32_t>(inputs_),
91 builder_.CreateVector<int32_t>(outputs_), BuiltinOptions_NONE, 0,
92 builder_.CreateVector<uint8_t>(custom_option),
93 CustomOptionsFormat_FLEXBUFFERS));
94 }
95
BuildInterpreter(std::vector<std::vector<int>> input_shapes,bool allow_fp32_relax_to_fp16)96 void SingleOpModel::BuildInterpreter(std::vector<std::vector<int>> input_shapes,
97 bool allow_fp32_relax_to_fp16) {
98 auto opcodes = builder_.CreateVector(opcodes_);
99 auto operators = builder_.CreateVector(operators_);
100 auto tensors = builder_.CreateVector(tensors_);
101 auto inputs = builder_.CreateVector<int32_t>(inputs_);
102 auto outputs = builder_.CreateVector<int32_t>(outputs_);
103 // Create a single subgraph
104 std::vector<flatbuffers::Offset<SubGraph>> subgraphs;
105 auto subgraph = CreateSubGraph(builder_, tensors, inputs, outputs, operators);
106 subgraphs.push_back(subgraph);
107 auto subgraphs_flatbuffer = builder_.CreateVector(subgraphs);
108
109 auto buffers = builder_.CreateVector(buffers_);
110 auto description = builder_.CreateString("programmatic model");
111 builder_.Finish(CreateModel(builder_, TFLITE_SCHEMA_VERSION, opcodes,
112 subgraphs_flatbuffer, description, buffers));
113
114 auto* model = GetModel(builder_.GetBufferPointer());
115
116 if (!resolver_) {
117 auto resolver = new ops::builtin::BuiltinOpResolver();
118 for (const auto& reg : custom_registrations_) {
119 resolver->AddCustom(reg.first.data(), reg.second());
120 }
121 resolver_ = std::unique_ptr<OpResolver>(resolver);
122 }
123 CHECK(InterpreterBuilder(model, *resolver_)(&interpreter_) == kTfLiteOk);
124
125 CHECK(interpreter_ != nullptr);
126
127 for (size_t i = 0; i < input_shapes.size(); ++i) {
128 const int input_idx = interpreter_->inputs()[i];
129 if (input_idx == kOptionalTensor) continue;
130 const auto& shape = input_shapes[i];
131 if (shape.empty()) continue;
132 CHECK(interpreter_->ResizeInputTensor(input_idx, shape) == kTfLiteOk);
133 }
134
135 interpreter_->SetAllowFp16PrecisionForFp32(allow_fp32_relax_to_fp16);
136
137 CHECK(interpreter_->AllocateTensors() == kTfLiteOk)
138 << "Cannot allocate tensors";
139 interpreter_->ResetVariableTensors();
140
141 // Modify delegate with function.
142 if (apply_delegate_fn_) {
143 apply_delegate_fn_(interpreter_.get());
144 }
145 }
146
Invoke()147 void SingleOpModel::Invoke() { CHECK(interpreter_->Invoke() == kTfLiteOk); }
148
GetTensorSize(int index) const149 int32_t SingleOpModel::GetTensorSize(int index) const {
150 TfLiteTensor* t = interpreter_->tensor(index);
151 CHECK(t);
152 int total_size = 1;
153 for (int i = 0; i < t->dims->size; ++i) {
154 total_size *= t->dims->data[i];
155 }
156 return total_size;
157 }
158
159 template <>
ExtractVector(int index)160 std::vector<string> SingleOpModel::ExtractVector(int index) {
161 TfLiteTensor* tensor_ptr = interpreter_->tensor(index);
162 CHECK(tensor_ptr != nullptr);
163 const int num_strings = GetStringCount(tensor_ptr);
164 std::vector<string> result;
165 result.reserve(num_strings);
166 for (int i = 0; i < num_strings; ++i) {
167 const auto str = GetString(tensor_ptr, i);
168 result.emplace_back(str.str, str.len);
169 }
170 return result;
171 }
172 } // namespace tflite
173