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"
33 using namespace tflite;
34 using namespace nnfw::tflite;
36 TEST(NNAPI_Quickcheck_gather_2, simple_test)
39 int SEED = std::chrono::system_clock::now().time_since_epoch().count();
41 nnfw::misc::env::IntAccessor("SEED").access(SEED);
43 // Set random test parameters
47 nnfw::misc::env::IntAccessor("VERBOSE").access(verbose);
48 nnfw::misc::env::IntAccessor("TOLERANCE").access(tolerance);
50 #define INT_VALUE(NAME, VALUE) IntVar NAME##_Value(#NAME, VALUE);
51 #include "gather_2.lst"
54 const int32_t INPUT_DATA_H = INPUT_DATA_H_Value();
55 const int32_t INPUT_DATA_W = INPUT_DATA_W_Value();
56 const int32_t INDEX_DATA = INDEX_DATA_Value();
58 const int32_t OUTPUT_DATA_H = INPUT_DATA_H;
59 const int32_t OUTPUT_DATA_W = INDEX_DATA;
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; \
73 PRINT_VALUE(INPUT_DATA_H);
74 PRINT_VALUE(INPUT_DATA_W);
75 PRINT_VALUE(INDEX_DATA);
78 PRINT_VALUE(OUTPUT_DATA_H);
79 PRINT_VALUE(OUTPUT_DATA_W);
83 auto setup = [&](Interpreter &interp) {
84 // Comment from 'context.h'
86 // Parameters for asymmetric quantization. Quantized values can be converted
87 // back to float using:
88 // real_value = scale * (quantized_value - zero_point);
90 // Q: Is this necessary?
91 TfLiteQuantizationParams quantization;
93 quantization.scale = 1;
94 quantization.zero_point = 0;
96 // On AddTensors(N) call, T/F Lite interpreter creates N tensors whose index is [0 ~ N)
99 // Configure INPUT_DATA
100 interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "input" /* name */,
101 {INPUT_DATA_H, INPUT_DATA_W} /* dims */, quantization);
103 // Configure INDEX_DATA
104 interp.SetTensorParametersReadWrite(1, kTfLiteInt32 /* type */, "index" /* name */,
105 {INDEX_DATA} /* dims */, quantization);
107 // Configure OUTPUT_VALUES
108 interp.SetTensorParametersReadWrite(2, kTfLiteFloat32 /* type */, "output_data" /* name */,
109 {OUTPUT_DATA_H, OUTPUT_DATA_W} /* dims */, quantization);
111 auto *param = reinterpret_cast<TfLiteGatherParams *>(malloc(sizeof(TfLiteGatherParams)));
116 // Run GATHER and store its result into Tensor #2
117 // - Read input data and index_data from Tensor #0 and #1, respectively
118 interp.AddNodeWithParameters({0, 1}, {2}, nullptr, 0, reinterpret_cast<void *>(param),
119 BuiltinOpResolver().FindOp(BuiltinOperator_GATHER, 1));
121 // Set Tensor #0 and #1 as Input, and Tensor #2 as Output
122 interp.SetInputs({0, 1});
123 interp.SetOutputs({2});
126 const nnfw::tflite::FunctionBuilder builder(setup);
128 RandomTestParam param;
130 param.verbose = verbose;
131 param.tolerance = tolerance;
133 int res = RandomTestRunner{SEED, param}.run(builder);