2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "tflite/ext/kernels/register.h"
18 #include "tensorflow/lite/model.h"
19 #include "tensorflow/lite/builtin_op_data.h"
23 using namespace tflite;
24 using namespace nnfw::tflite;
29 template <typename T> struct View
31 virtual ~View() = default;
33 virtual int32_t size(void) const = 0;
34 virtual T at(uint32_t off) const = 0;
48 template <typename T> struct View
50 virtual ~View() = default;
52 virtual const Shape &shape(void) const = 0;
53 virtual T at(uint32_t ch, uint32_t row, uint32_t col) const = 0;
55 } // namespace feature
68 template <typename T> struct View
70 virtual ~View() = default;
72 virtual const Shape &shape(void) const = 0;
73 virtual T at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const = 0;
80 class SampleBiasObject final : public vector::View<float>
83 SampleBiasObject() : _size(N)
89 int32_t size(void) const override { return _size; }
91 float at(uint32_t off) const override { return 0.0f; }
97 class SampleFeatureObject final : public feature::View<float>
100 SampleFeatureObject()
106 const uint32_t size = _shape.C * _shape.H * _shape.W;
108 for (uint32_t off = 0; off < size; ++off)
110 _value.emplace_back(off);
113 assert(_value.size() == size);
117 const feature::Shape &shape(void) const override { return _shape; };
119 float at(uint32_t ch, uint32_t row, uint32_t col) const override
121 return _value.at(ch * _shape.H * _shape.W + row * _shape.W + col);
125 float &at(uint32_t ch, uint32_t row, uint32_t col)
127 return _value.at(ch * _shape.H * _shape.W + row * _shape.W + col);
131 feature::Shape _shape;
132 std::vector<float> _value;
135 class SampleKernelObject final : public kernel::View<float>
145 const uint32_t size = _shape.N * _shape.C * _shape.H * _shape.W;
147 for (uint32_t off = 0; off < size; ++off)
149 _value.emplace_back(off);
152 assert(_value.size() == size);
156 const kernel::Shape &shape(void) const override { return _shape; };
158 float at(uint32_t nth, uint32_t ch, uint32_t row, uint32_t col) const override
160 return _value.at(nth * _shape.C * _shape.H * _shape.W + ch * _shape.H * _shape.W +
161 row * _shape.W + col);
165 kernel::Shape _shape;
166 std::vector<float> _value;
169 int main(int argc, char **argv)
171 const SampleFeatureObject ifm;
172 const SampleKernelObject kernel;
173 const SampleBiasObject bias;
175 const int32_t IFM_C = ifm.shape().C;
176 const int32_t IFM_H = ifm.shape().H;
177 const int32_t IFM_W = ifm.shape().W;
179 const int32_t KER_N = kernel.shape().N;
180 const int32_t KER_C = kernel.shape().C;
181 const int32_t KER_H = kernel.shape().H;
182 const int32_t KER_W = kernel.shape().W;
184 const int32_t OFM_C = kernel.shape().N;
185 const int32_t OFM_H = (IFM_H - KER_H) + 1;
186 const int32_t OFM_W = (IFM_W - KER_W) + 1;
188 // Assumption on this example
189 assert(IFM_C == KER_C);
190 assert(KER_N == bias.size());
192 // Comment from 'context.h'
194 // Parameters for asymmetric quantization. Quantized values can be converted
195 // back to float using:
196 // real_value = scale * (quantized_value - zero_point);
198 // Q: Is this necessary?
199 TfLiteQuantizationParams quantization;
201 quantization.scale = 1;
202 quantization.zero_point = 0;
206 // On AddTensors(N) call, T/F Lite interpreter creates N tensors whose index is [0 ~ N)
207 interp.AddTensors(5);
210 interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "output" /* name */,
211 {1 /*N*/, OFM_H, OFM_W, OFM_C} /* dims */, quantization);
214 interp.SetTensorParametersReadWrite(1, kTfLiteFloat32 /* type */, "input" /* name */,
215 {1 /*N*/, IFM_H, IFM_W, IFM_C} /* dims */, quantization);
218 const uint32_t kernel_size = KER_N * KER_C * KER_H * KER_W;
219 float kernel_data[kernel_size] = {
223 // Fill kernel data in NHWC order
227 for (uint32_t nth = 0; nth < KER_N; ++nth)
229 for (uint32_t row = 0; row < KER_H; ++row)
231 for (uint32_t col = 0; col < KER_W; ++col)
233 for (uint32_t ch = 0; ch < KER_C; ++ch)
235 const auto value = kernel.at(nth, ch, row, col);
236 kernel_data[off++] = value;
242 assert(kernel_size == off);
245 interp.SetTensorParametersReadOnly(
246 2, kTfLiteFloat32 /* type */, "filter" /* name */, {KER_N, KER_H, KER_W, KER_C} /* dims */,
247 quantization, reinterpret_cast<const char *>(kernel_data), sizeof(kernel_data));
250 const uint32_t bias_size = bias.size();
251 float bias_data[bias_size] = {
256 for (uint32_t off = 0; off < bias.size(); ++off)
258 bias_data[off] = bias.at(off);
261 interp.SetTensorParametersReadOnly(3, kTfLiteFloat32 /* type */, "bias" /* name */,
262 {bias.size()} /* dims */, quantization,
263 reinterpret_cast<const char *>(bias_data), sizeof(bias_data));
265 // Add Convolution Node
267 // NOTE AddNodeWithParameters take the ownership of param, and deallocate it with free
268 // So, param should be allocated with malloc
269 TfLiteConvParams *param = reinterpret_cast<TfLiteConvParams *>(malloc(sizeof(TfLiteConvParams)));
271 param->padding = kTfLitePaddingValid;
272 param->stride_width = 1;
273 param->stride_height = 1;
274 param->activation = kTfLiteActRelu;
276 // Run Convolution and store its result into Tensor #0
277 // - Read IFM from Tensor #1
278 // - Read Filter from Tensor #2,
279 // - Read Bias from Tensor #3
280 interp.AddNodeWithParameters({1, 2, 3}, {0}, nullptr, 0, reinterpret_cast<void *>(param),
281 BuiltinOpResolver().FindOp(BuiltinOperator_CONV_2D, 1));
283 // Set Tensor #1 as Input #0, and Tensor #0 as Output #0
284 interp.SetInputs({1});
285 interp.SetOutputs({0});
287 // Let's use NNAPI (if possible)
288 interp.UseNNAPI(true);
291 interp.AllocateTensors();
293 // Fill IFM data in HWC order
297 for (uint32_t row = 0; row < ifm.shape().H; ++row)
299 for (uint32_t col = 0; col < ifm.shape().W; ++col)
301 for (uint32_t ch = 0; ch < ifm.shape().C; ++ch)
303 const auto value = ifm.at(ch, row, col);
304 interp.typed_input_tensor<float>(0)[off++] = value;
310 // Let's Rock-n-Roll!
317 for (uint32_t row = 0; row < OFM_H; ++row)
319 for (uint32_t col = 0; col < OFM_W; ++col)
321 for (uint32_t ch = 0; ch < kernel.shape().N; ++ch)
323 std::cout << interp.typed_output_tensor<float>(0)[off++] << std::endl;