4ab164ea16c4191535d12f2f283cb108126556a9
[platform/core/ml/nnfw.git] / tools / nnapi_quickcheck / tests / gather_1.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "gtest/gtest.h"
18
19 #include "tflite/ext/kernels/register.h"
20 #include "tensorflow/lite/model.h"
21 #include "tensorflow/lite/builtin_op_data.h"
22
23 #include "env.h"
24 #include "memory.h"
25 #include "misc/environment.h"
26
27 #include "tflite/Diff.h"
28 #include "tflite/interp/FunctionBuilder.h"
29
30 #include <chrono>
31 #include <iostream>
32
33 using namespace tflite;
34 using namespace nnfw::tflite;
35
36 TEST(NNAPI_Quickcheck_gather_1, simple_test)
37 {
38   // Set random seed
39   int SEED = std::chrono::system_clock::now().time_since_epoch().count();
40
41   nnfw::misc::env::IntAccessor("SEED").access(SEED);
42
43   // Set random test parameters
44   int verbose = 0;
45   int tolerance = 1;
46
47   nnfw::misc::env::IntAccessor("VERBOSE").access(verbose);
48   nnfw::misc::env::IntAccessor("TOLERANCE").access(tolerance);
49
50 #define INT_VALUE(NAME, VALUE) IntVar NAME##_Value(#NAME, VALUE);
51 #include "gather_1.lst"
52 #undef INT_VALUE
53
54   const int32_t INPUT_DATA = INPUT_DATA_Value();
55   const int32_t INDEX_DATA = INDEX_DATA_Value();
56
57   const int32_t OUTPUT_DATA = INDEX_DATA;
58
59   std::cout << "Configurations:" << std::endl;
60 #define PRINT_NEWLINE()     \
61   {                         \
62     std::cout << std::endl; \
63   }
64 #define PRINT_VALUE(value)                                       \
65   {                                                              \
66     std::cout << "  " << #value << ": " << (value) << std::endl; \
67   }
68   PRINT_VALUE(SEED);
69   PRINT_NEWLINE();
70
71   PRINT_VALUE(INPUT_DATA);
72   PRINT_VALUE(INDEX_DATA);
73   PRINT_NEWLINE();
74
75   PRINT_VALUE(OUTPUT_DATA);
76 #undef PRINT_VALUE
77 #undef PRINT_NEWLINE
78
79   auto setup = [&](Interpreter &interp) {
80     // Comment from 'context.h'
81     //
82     // Parameters for asymmetric quantization. Quantized values can be converted
83     // back to float using:
84     //    real_value = scale * (quantized_value - zero_point);
85     //
86     // Q: Is this necessary?
87     TfLiteQuantizationParams quantization;
88
89     quantization.scale = 1;
90     quantization.zero_point = 0;
91
92     // On AddTensors(N) call, T/F Lite interpreter creates N tensors whose index is [0 ~ N)
93     interp.AddTensors(3);
94
95     // Configure INPUT_DATA
96     interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "input" /* name */,
97                                         {INPUT_DATA} /* dims */, quantization);
98
99     // Configure INDEX_DATA
100     interp.SetTensorParametersReadWrite(1, kTfLiteInt32 /* type */, "index" /* name */,
101                                         {INDEX_DATA} /* dims */, quantization);
102
103     // Configure OUTPUT_VALUES
104     interp.SetTensorParametersReadWrite(2, kTfLiteFloat32 /* type */, "output_data" /* name */,
105                                         {OUTPUT_DATA} /* dims */, quantization);
106
107     auto *param = reinterpret_cast<TfLiteGatherParams *>(malloc(sizeof(TfLiteGatherParams)));
108
109     param->axis = 0;
110
111     // Add GATHER Node
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));
116
117     // Set Tensor #0 and #1 as Input, and Tensor #2 as Output
118     interp.SetInputs({0, 1});
119     interp.SetOutputs({2});
120   };
121
122   const nnfw::tflite::FunctionBuilder builder(setup);
123
124   RandomTestParam param;
125
126   param.verbose = verbose;
127   param.tolerance = tolerance;
128
129   int res = RandomTestRunner{SEED, param}.run(builder);
130
131   EXPECT_EQ(res, 0);
132 }