43cd0a4707bc04ab0129f8859209bd6264f61ba8
[platform/core/ml/nnfw.git] / tools / nnapi_quickcheck / tests / fully_connected_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 "misc/environment.h"
25
26 #include "tflite/Diff.h"
27 #include "tflite/Quantization.h"
28 #include "tflite/interp/FunctionBuilder.h"
29
30 #include <iostream>
31 #include <cassert>
32
33 #include <chrono>
34 #include <random>
35
36 using namespace tflite;
37 using namespace nnfw::tflite;
38
39 template <typename T> T *make_malloc(void) { return reinterpret_cast<T *>(malloc(sizeof(T))); }
40
41 TEST(NNAPI_Quickcheck_fully_connected_1, simple_test)
42 {
43   int verbose = 0;
44   int tolerance = 1;
45
46   nnfw::misc::env::IntAccessor("VERBOSE").access(verbose);
47   nnfw::misc::env::IntAccessor("TOLERANCE").access(tolerance);
48
49   // Set random seed
50   int SEED = std::chrono::system_clock::now().time_since_epoch().count();
51
52   nnfw::misc::env::IntAccessor("SEED").access(SEED);
53
54 #define INT_VALUE(NAME, VALUE) IntVar NAME##_Value(#NAME, VALUE);
55 #include "conv_1.lst"
56 #undef INT_VALUE
57
58   const int32_t IFM_C = IFM_C_Value();
59   const int32_t IFM_H = IFM_H_Value();
60   const int32_t IFM_W = IFM_W_Value();
61
62   const int32_t KER_H = KER_N_Value();
63   const int32_t KER_W = IFM_C_Value() * IFM_H_Value() * IFM_W_Value();
64
65   const int32_t OUT_LEN = KER_H;
66
67   // Initialize random number generator
68   std::minstd_rand random(SEED);
69
70   std::cout << "Configurations:" << std::endl;
71 #define PRINT_NEWLINE()     \
72   {                         \
73     std::cout << std::endl; \
74   }
75 #define PRINT_VALUE(value)                                       \
76   {                                                              \
77     std::cout << "  " << #value << ": " << (value) << std::endl; \
78   }
79   PRINT_VALUE(SEED);
80   PRINT_NEWLINE();
81
82   PRINT_VALUE(IFM_C);
83   PRINT_VALUE(IFM_H);
84   PRINT_VALUE(IFM_W);
85   PRINT_NEWLINE();
86
87   PRINT_VALUE(KER_H);
88   PRINT_VALUE(KER_W);
89   PRINT_NEWLINE();
90
91   PRINT_VALUE(OUT_LEN);
92 #undef PRINT_VALUE
93 #undef PRINT_NEWLINE
94
95   // Configure Kernel Data
96   const uint32_t kernel_size = KER_H * KER_W;
97   float kernel_data[kernel_size] = {
98       0.0f,
99   };
100
101   // Fill kernel data with random data
102   {
103     std::normal_distribution<float> kernel_dist(-1.0f, +1.0f);
104
105     for (uint32_t off = 0; off < kernel_size; ++off)
106     {
107       kernel_data[off++] = kernel_dist(random);
108     }
109   }
110
111   // Configure Bias Data
112   const auto bias_size = KER_H;
113   float bias_data[bias_size] = {
114       0.0f,
115   };
116
117   // Fill bias data with random data
118   {
119     std::normal_distribution<float> bias_dist(-1.0f, +1.0f);
120
121     for (uint32_t off = 0; off < bias_size; ++off)
122     {
123       bias_data[off] = bias_dist(random);
124     }
125   }
126
127   auto setup = [&](Interpreter &interp) {
128     // Comment from 'context.h'
129     //
130     // Parameters for asymmetric quantization. Quantized values can be converted
131     // back to float using:
132     //    real_value = scale * (quantized_value - zero_point);
133     //
134     // Q: Is this necessary?
135     TfLiteQuantizationParams quantization = make_default_quantization();
136
137     // On AddTensors(N) call, T/F Lite interpreter creates N tensors whose index is [0 ~ N)
138     interp.AddTensors(4);
139
140     // Configure OFM
141     interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "output" /* name */,
142                                         {1 /*N*/, KER_H} /* dims */, quantization);
143
144     // Configure IFM
145     interp.SetTensorParametersReadWrite(1, kTfLiteFloat32 /* type */, "input" /* name */,
146                                         {1 /*N*/, IFM_H, IFM_W, IFM_C} /* dims */, quantization);
147
148     // NOTE kernel_data & bias_data should live longer than interpreter!
149     interp.SetTensorParametersReadOnly(
150         2, kTfLiteFloat32 /* type */, "filter" /* name */, {KER_H, KER_W} /* dims */, quantization,
151         reinterpret_cast<const char *>(kernel_data), kernel_size * sizeof(float));
152
153     interp.SetTensorParametersReadOnly(
154         3, kTfLiteFloat32 /* type */, "bias" /* name */, {bias_size} /* dims */, quantization,
155         reinterpret_cast<const char *>(bias_data), bias_size * sizeof(float));
156
157     // Add Fully Connected Node
158     //
159     // NOTE AddNodeWithParameters take the ownership of param, and deallocate it with free
160     //      So, param should be allocated with malloc
161     auto param = make_malloc<TfLiteFullyConnectedParams>();
162
163     param->activation = kTfLiteActRelu;
164
165     // Run Convolution and store its result into Tensor #0
166     //  - Read IFM from Tensor #1
167     //  - Read Filter from Tensor #2,
168     //  - Read Bias from Tensor #3
169     interp.AddNodeWithParameters({1, 2, 3}, {0}, nullptr, 0, reinterpret_cast<void *>(param),
170                                  BuiltinOpResolver().FindOp(BuiltinOperator_FULLY_CONNECTED, 1));
171
172     // Set Tensor #1 as Input #0, and Tensor #0 as Output #0
173     interp.SetInputs({1});
174     interp.SetOutputs({0});
175   };
176
177   const nnfw::tflite::FunctionBuilder builder(setup);
178
179   RandomTestParam param;
180
181   param.verbose = verbose;
182   param.tolerance = tolerance;
183
184   int res = RandomTestRunner{SEED, param}.run(builder);
185
186   EXPECT_EQ(res, 0);
187 }