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 #include "misc/environment.h"
24 #include "misc/feature/Shape.h"
26 #include "tflite/Diff.h"
27 #include "tflite/Quantization.h"
28 #include "tflite/interp/FunctionBuilder.h"
35 using namespace tflite;
36 using namespace nnfw::tflite;
38 int main(int argc, char **argv)
43 nnfw::misc::env::IntAccessor("VERBOSE").access(verbose);
44 nnfw::misc::env::IntAccessor("TOLERANCE").access(tolerance);
46 #define INT_VALUE(NAME, VALUE) IntVar NAME##_Value(#NAME, VALUE);
47 #include "relu1_1.lst"
50 const int32_t IFM_H = IFM_H_Value();
51 const int32_t IFM_W = IFM_W_Value();
54 int SEED = std::chrono::system_clock::now().time_since_epoch().count();
56 nnfw::misc::env::IntAccessor("SEED").access(SEED);
58 // Initialize random number generator
59 std::minstd_rand random(SEED);
61 std::cout << "Configurations:" << std::endl;
62 #define PRINT_NEWLINE() \
64 std::cout << std::endl; \
66 #define PRINT_VALUE(value) \
68 std::cout << " " << #value << ": " << (value) << std::endl; \
78 const int32_t OFM_H = IFM_H;
79 const int32_t OFM_W = IFM_W;
81 auto setup = [&](Interpreter &interp) {
82 // Comment from 'context.h'
84 // Parameters for asymmetric quantization. Quantized values can be converted
85 // back to float using:
86 // real_value = scale * (quantized_value - zero_point);
88 // Q: Is this necessary?
89 TfLiteQuantizationParams quantization = make_default_quantization();
91 // On AddTensors(N) call, T/F Lite interpreter creates N tensors whose index is [0 ~ N)
94 // Configure Output Tensor
95 interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "output" /* name */,
96 {OFM_H, OFM_W} /* dims */, quantization);
98 // Configure Input Tensor
99 interp.SetTensorParametersReadWrite(1, kTfLiteFloat32 /* type */, "input" /* name */,
100 {IFM_H, IFM_W} /* dims */, quantization);
103 // Run ReLU and store its result into Tensor #0
104 // - Read IFM from Tensor #1
105 interp.AddNodeWithParameters({1}, {0}, nullptr, 0, nullptr,
106 BuiltinOpResolver().FindOp(BuiltinOperator_RELU_N1_TO_1, 1));
108 // Set Tensor #1 as Input #0, and Tensor #0 as Output #0
109 interp.SetInputs({1});
110 interp.SetOutputs({0});
113 const nnfw::tflite::FunctionBuilder builder(setup);
115 RandomTestParam param;
117 param.verbose = verbose;
118 param.tolerance = tolerance;
120 return RandomTestRunner{SEED, param}.run(builder);