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 "gtest/gtest.h"
19 #include "tflite/ext/kernels/register.h"
20 #include "tensorflow/lite/model.h"
21 #include "tensorflow/lite/builtin_op_data.h"
25 #include "misc/environment.h"
27 #include "tflite/Diff.h"
28 #include "tflite/interp/FunctionBuilder.h"
36 using namespace tflite;
37 using namespace nnfw::tflite;
39 TEST(NNAPI_Quickcheck_mul_1, simple_test)
44 nnfw::misc::env::IntAccessor("VERBOSE").access(verbose);
45 nnfw::misc::env::IntAccessor("TOLERANCE").access(tolerance);
48 int SEED = std::chrono::system_clock::now().time_since_epoch().count();
50 nnfw::misc::env::IntAccessor("SEED").access(SEED);
52 #define INT_VALUE(NAME, VALUE) IntVar NAME##_Value(#NAME, VALUE);
56 const int32_t LEFT_1D = LEFT_1D_Value();
57 const int32_t LEFT_2D = LEFT_2D_Value();
58 const int32_t LEFT_3D = LEFT_3D_Value();
60 const int32_t RIGHT_W = RIGHT_W_Value();
62 const int32_t OFM_1D = LEFT_1D_Value();
63 const int32_t OFM_2D = LEFT_2D_Value();
64 const int32_t OFM_3D = LEFT_3D_Value();
66 // Initialize random number generator
67 std::minstd_rand random(SEED);
69 std::cout << "Configurations:" << std::endl;
70 #define PRINT_NEWLINE() \
72 std::cout << std::endl; \
74 #define PRINT_VALUE(value) \
76 std::cout << " " << #value << ": " << (value) << std::endl; \
95 auto setup = [&](Interpreter &interp) {
96 // Comment from 'context.h'
98 // Parameters for asymmetric quantization. Quantized values can be converted
99 // back to float using:
100 // real_value = scale * (quantized_value - zero_point);
102 // Q: Is this necessary?
103 TfLiteQuantizationParams quantization;
105 quantization.scale = 1;
106 quantization.zero_point = 0;
108 // On AddTensors(N) call, T/F Lite interpreter creates N tensors whose index is [0 ~ N)
109 interp.AddTensors(3);
112 interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "output" /* name */,
113 {OFM_1D, OFM_2D, OFM_3D} /* dims */, quantization);
115 // Configure input(s)
116 interp.SetTensorParametersReadWrite(1, kTfLiteFloat32 /* type */, "left" /* name */,
117 {LEFT_1D, LEFT_2D, LEFT_3D} /* dims */, quantization);
119 interp.SetTensorParametersReadWrite(2, kTfLiteFloat32 /* type */, "right" /* name */,
120 {RIGHT_W} /* dims */, quantization);
124 // NOTE AddNodeWithParameters take the ownership of param, and deallocate it with free
125 // So, param should be allocated with malloc
126 auto param = make_alloc<TfLiteAddParams>();
128 param->activation = kTfLiteActNone;
130 // Run MUL and store the result into Tensor #0
131 // - Read Left from Tensor #1
132 // - Read Right from Tensor #2,
133 interp.AddNodeWithParameters({1, 2}, {0}, nullptr, 0, reinterpret_cast<void *>(param),
134 BuiltinOpResolver().FindOp(BuiltinOperator_MUL, 1));
136 interp.SetInputs({1, 2});
137 interp.SetOutputs({0});
140 const nnfw::tflite::FunctionBuilder builder(setup);
142 RandomTestParam param;
144 param.verbose = verbose;
145 param.tolerance = tolerance;
146 param.tensor_logging = 1;
147 param.log_path = "report/tensor_mul_1.log";
149 int res = RandomTestRunner{SEED, param}.run(builder);