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