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"
26 #include "misc/environment.h"
28 #include "tflite/Diff.h"
29 #include "tflite/TensorShapeUtils.h"
30 #include "tflite/interp/FunctionBuilder.h"
38 using namespace tflite;
39 using namespace nnfw::tflite;
41 TEST(NNAPI_Quickcheck_add_3, simple_test)
44 int SEED = std::chrono::system_clock::now().time_since_epoch().count();
46 nnfw::misc::env::IntAccessor("SEED").access(SEED);
48 // Initialize random number generator
49 std::minstd_rand random(SEED);
51 #define STR_VALUE(NAME, VALUE) StrVar NAME##_Value(#NAME, VALUE);
55 const auto LHS_SHAPE = nnfw::misc::tensor::Shape::from(LHS_SHAPE_Value());
56 const auto RHS_SHAPE = nnfw::misc::tensor::Shape::from(RHS_SHAPE_Value());
57 const auto OUT_SHAPE = nnfw::tflite::broadcast(LHS_SHAPE, RHS_SHAPE);
59 std::cout << "Configurations:" << std::endl;
60 #define PRINT_NEWLINE() \
62 std::cout << std::endl; \
64 #define PRINT_VALUE(value) \
66 std::cout << " " << #value << ": " << (value) << std::endl; \
71 PRINT_VALUE(LHS_SHAPE);
72 PRINT_VALUE(RHS_SHAPE);
73 PRINT_VALUE(OUT_SHAPE);
77 auto setup = [&](Interpreter &interp) {
78 using nnfw::tflite::as_dims;
80 // Comment from 'context.h'
82 // Parameters for asymmetric quantization. Quantized values can be converted
83 // back to float using:
84 // real_value = scale * (quantized_value - zero_point);
86 // Q: Is this necessary?
87 TfLiteQuantizationParams quantization;
89 quantization.scale = 1;
90 quantization.zero_point = 0;
92 // On AddTensors(N) call, T/F Lite interpreter creates N tensors whose index is [0 ~ N)
96 interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "output" /* name */,
97 as_dims(OUT_SHAPE), quantization);
100 interp.SetTensorParametersReadWrite(1, kTfLiteFloat32 /* type */, "left" /* name */,
101 as_dims(LHS_SHAPE), quantization);
103 interp.SetTensorParametersReadWrite(2, kTfLiteFloat32 /* type */, "right" /* name */,
104 as_dims(RHS_SHAPE), quantization);
106 // Add Convolution Node
108 // NOTE AddNodeWithParameters take the ownership of param, and deallocate it with free
109 // So, param should be allocated with malloc
110 auto param = make_alloc<TfLiteAddParams>();
112 param->activation = kTfLiteActNone;
114 // Run Add and store the result into Tensor #0
115 // - Read Left from Tensor #1
116 // - Read Left from Tensor #2,
117 interp.AddNodeWithParameters({1, 2}, {0}, nullptr, 0, reinterpret_cast<void *>(param),
118 BuiltinOpResolver().FindOp(BuiltinOperator_ADD, 1));
120 interp.SetInputs({1, 2});
121 interp.SetOutputs({0});
124 const nnfw::tflite::FunctionBuilder builder(setup);
126 RandomTestParam param;
131 nnfw::misc::env::IntAccessor("VERBOSE").access(param.verbose);
132 nnfw::misc::env::IntAccessor("TOLERANCE").access(param.tolerance);
134 int res = RandomTestRunner{SEED, param}.run(builder);