ce409ccbcf1c00e75bd876f11df1689130fdb6ee
[platform/core/ml/nnfw.git] / tools / nnapi_quickcheck / tests / add_3.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
26 #include "misc/environment.h"
27
28 #include "tflite/Diff.h"
29 #include "tflite/TensorShapeUtils.h"
30 #include "tflite/interp/FunctionBuilder.h"
31
32 #include <iostream>
33 #include <cassert>
34
35 #include <chrono>
36 #include <random>
37
38 using namespace tflite;
39 using namespace nnfw::tflite;
40
41 TEST(NNAPI_Quickcheck_add_3, simple_test)
42 {
43   // Set random seed
44   int SEED = std::chrono::system_clock::now().time_since_epoch().count();
45
46   nnfw::misc::env::IntAccessor("SEED").access(SEED);
47
48   // Initialize random number generator
49   std::minstd_rand random(SEED);
50
51 #define STR_VALUE(NAME, VALUE) StrVar NAME##_Value(#NAME, VALUE);
52 #include "add_3.lst"
53 #undef STR_VALUE
54
55   const auto LHS_SHAPE = nnfw::misc::tensor::Shape::from(LHS_SHAPE_Value());
56   const auto RHS_SHAPE = nnfw::misc::tensor::Shape::from(RHS_SHAPE_Value());
57   const auto OUT_SHAPE = nnfw::tflite::broadcast(LHS_SHAPE, RHS_SHAPE);
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(LHS_SHAPE);
72   PRINT_VALUE(RHS_SHAPE);
73   PRINT_VALUE(OUT_SHAPE);
74 #undef PRINT_VALUE
75 #undef PRINT_NEWLINE
76
77   auto setup = [&](Interpreter &interp) {
78     using nnfw::tflite::as_dims;
79
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 output
96     interp.SetTensorParametersReadWrite(0, kTfLiteFloat32 /* type */, "output" /* name */,
97                                         as_dims(OUT_SHAPE), quantization);
98
99     // Configure input(s)
100     interp.SetTensorParametersReadWrite(1, kTfLiteFloat32 /* type */, "left" /* name */,
101                                         as_dims(LHS_SHAPE), quantization);
102
103     interp.SetTensorParametersReadWrite(2, kTfLiteFloat32 /* type */, "right" /* name */,
104                                         as_dims(RHS_SHAPE), quantization);
105
106     // Add Convolution Node
107     //
108     // NOTE AddNodeWithParameters take the ownership of param, and deallocate it with free
109     //      So, param should be allocated with malloc
110     auto param = make_alloc<TfLiteAddParams>();
111
112     param->activation = kTfLiteActNone;
113
114     // Run Add and store the result into Tensor #0
115     //  - Read Left from Tensor #1
116     //  - Read Left from Tensor #2,
117     interp.AddNodeWithParameters({1, 2}, {0}, nullptr, 0, reinterpret_cast<void *>(param),
118                                  BuiltinOpResolver().FindOp(BuiltinOperator_ADD, 1));
119
120     interp.SetInputs({1, 2});
121     interp.SetOutputs({0});
122   };
123
124   const nnfw::tflite::FunctionBuilder builder(setup);
125
126   RandomTestParam param;
127
128   param.verbose = 0;
129   param.tolerance = 1;
130
131   nnfw::misc::env::IntAccessor("VERBOSE").access(param.verbose);
132   nnfw::misc::env::IntAccessor("TOLERANCE").access(param.tolerance);
133
134   int res = RandomTestRunner{SEED, param}.run(builder);
135
136   EXPECT_EQ(res, 0);
137 }