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_1, 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_1.lst"
54 const int32_t INPUT_DATA = INPUT_DATA_Value();
55 const int32_t INDEX_DATA = INDEX_DATA_Value();
57 const int32_t OUTPUT_DATA = INDEX_DATA;
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(INPUT_DATA);
72 PRINT_VALUE(INDEX_DATA);
75 PRINT_VALUE(OUTPUT_DATA);
79 auto setup = [&](Interpreter &interp) {
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)
95 // Configure INPUT_DATA
96 interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "input" /* name */,
97 {INPUT_DATA} /* dims */, quantization);
99 // Configure INDEX_DATA
100 interp.SetTensorParametersReadWrite(1, kTfLiteInt32 /* type */, "index" /* name */,
101 {INDEX_DATA} /* dims */, quantization);
103 // Configure OUTPUT_VALUES
104 interp.SetTensorParametersReadWrite(2, kTfLiteFloat32 /* type */, "output_data" /* name */,
105 {OUTPUT_DATA} /* dims */, quantization);
107 auto *param = reinterpret_cast<TfLiteGatherParams *>(malloc(sizeof(TfLiteGatherParams)));
112 // Run GATHER and store its result into Tensor #2
113 // - Read input data and index_data from Tensor #0 and #1, respectively
114 interp.AddNodeWithParameters({0, 1}, {2}, nullptr, 0, reinterpret_cast<void *>(param),
115 BuiltinOpResolver().FindOp(BuiltinOperator_GATHER, 1));
117 // Set Tensor #0 and #1 as Input, and Tensor #2 as Output
118 interp.SetInputs({0, 1});
119 interp.SetOutputs({2});
122 const nnfw::tflite::FunctionBuilder builder(setup);
124 RandomTestParam param;
126 param.verbose = verbose;
127 param.tolerance = tolerance;
129 int res = RandomTestRunner{SEED, param}.run(builder);