Imported Upstream version 1.11.1
[platform/core/ml/nnfw.git] / tools / nnapi_quickcheck / tests / gather_2.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_2, 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_2.lst"
52 #undef INT_VALUE
53
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();
57
58   const int32_t OUTPUT_DATA_H = INPUT_DATA_H;
59   const int32_t OUTPUT_DATA_W = INDEX_DATA;
60
61   std::cout << "Configurations:" << std::endl;
62 #define PRINT_NEWLINE()     \
63   {                         \
64     std::cout << std::endl; \
65   }
66 #define PRINT_VALUE(value)                                       \
67   {                                                              \
68     std::cout << "  " << #value << ": " << (value) << std::endl; \
69   }
70   PRINT_VALUE(SEED);
71   PRINT_NEWLINE();
72
73   PRINT_VALUE(INPUT_DATA_H);
74   PRINT_VALUE(INPUT_DATA_W);
75   PRINT_VALUE(INDEX_DATA);
76   PRINT_NEWLINE();
77
78   PRINT_VALUE(OUTPUT_DATA_H);
79   PRINT_VALUE(OUTPUT_DATA_W);
80 #undef PRINT_VALUE
81 #undef PRINT_NEWLINE
82
83   auto setup = [&](Interpreter &interp) {
84     // Comment from 'context.h'
85     //
86     // Parameters for asymmetric quantization. Quantized values can be converted
87     // back to float using:
88     //    real_value = scale * (quantized_value - zero_point);
89     //
90     // Q: Is this necessary?
91     TfLiteQuantizationParams quantization;
92
93     quantization.scale = 1;
94     quantization.zero_point = 0;
95
96     // On AddTensors(N) call, T/F Lite interpreter creates N tensors whose index is [0 ~ N)
97     interp.AddTensors(3);
98
99     // Configure INPUT_DATA
100     interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "input" /* name */,
101                                         {INPUT_DATA_H, INPUT_DATA_W} /* dims */, quantization);
102
103     // Configure INDEX_DATA
104     interp.SetTensorParametersReadWrite(1, kTfLiteInt32 /* type */, "index" /* name */,
105                                         {INDEX_DATA} /* dims */, quantization);
106
107     // Configure OUTPUT_VALUES
108     interp.SetTensorParametersReadWrite(2, kTfLiteFloat32 /* type */, "output_data" /* name */,
109                                         {OUTPUT_DATA_H, OUTPUT_DATA_W} /* dims */, quantization);
110
111     auto *param = reinterpret_cast<TfLiteGatherParams *>(malloc(sizeof(TfLiteGatherParams)));
112
113     param->axis = 0;
114
115     // Add GATHER Node
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));
120
121     // Set Tensor #0 and #1 as Input, and Tensor #2 as Output
122     interp.SetInputs({0, 1});
123     interp.SetOutputs({2});
124   };
125
126   const nnfw::tflite::FunctionBuilder builder(setup);
127
128   RandomTestParam param;
129
130   param.verbose = verbose;
131   param.tolerance = tolerance;
132
133   int res = RandomTestRunner{SEED, param}.run(builder);
134
135   EXPECT_EQ(res, 0);
136 }