Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Logistic.test.cpp
1 /*
2  * Copyright (c) 2020 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 "kernels/Logistic.h"
18 #include "kernels/TestUtils.h"
19 #include "luci_interpreter/TestMemoryManager.h"
20
21 namespace luci_interpreter
22 {
23 namespace kernels
24 {
25 namespace
26 {
27
28 using namespace testing;
29
30 template <typename T>
31 void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
32            std::initializer_list<float> input_data, std::initializer_list<float> output_data)
33 {
34   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
35
36   Tensor input_tensor =
37     makeInputTensor<getElementType<T>()>(input_shape, input_data, memory_manager.get());
38   Tensor output_tensor = makeOutputTensor(getElementType<T>());
39
40   Logistic kernel(&input_tensor, &output_tensor);
41   kernel.configure();
42   memory_manager->allocate_memory(output_tensor);
43   kernel.execute();
44
45   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(output_data));
46   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
47 }
48
49 template <>
50 void Check<uint8_t>(std::initializer_list<int32_t> input_shape,
51                     std::initializer_list<int32_t> output_shape,
52                     std::initializer_list<float> input_data,
53                     std::initializer_list<float> output_data)
54 {
55   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
56
57   std::pair<float, int32_t> input_quant_param =
58     quantizationParams<uint8_t>(std::min(input_data), std::max(input_data));
59   Tensor input_tensor =
60     makeInputTensor<DataType::U8>(input_shape, input_quant_param.first, input_quant_param.second,
61                                   input_data, memory_manager.get());
62   Tensor output_tensor = makeOutputTensor(DataType::U8, 1. / 256, 0);
63
64   Logistic kernel(&input_tensor, &output_tensor);
65   kernel.configure();
66   memory_manager->allocate_memory(output_tensor);
67   kernel.execute();
68
69   EXPECT_THAT(dequantizeTensorData(output_tensor),
70               FloatArrayNear(output_data, output_tensor.scale() * 2));
71   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
72 }
73
74 template <typename T> class LogisticTest : public ::testing::Test
75 {
76 };
77
78 using DataTypes = ::testing::Types<float, uint8_t>;
79 TYPED_TEST_CASE(LogisticTest, DataTypes);
80
81 TYPED_TEST(LogisticTest, Simple)
82 {
83   Check<TypeParam>(
84     {89}, {89},
85     {-10.0000000000, -9.7727272727, -9.5454545455, -9.3181818182, -9.0909090909, -8.8636363636,
86      -8.6363636364,  -8.4090909091, -8.1818181818, -7.9545454545, -7.7272727273, -7.5000000000,
87      -7.2727272727,  -7.0454545455, -6.8181818182, -6.5909090909, -6.3636363636, -6.1363636364,
88      -5.9090909091,  -5.6818181818, -5.4545454545, -5.2272727273, -5.0000000000, -4.7727272727,
89      -4.5454545455,  -4.3181818182, -4.0909090909, -3.8636363636, -3.6363636364, -3.4090909091,
90      -3.1818181818,  -2.9545454545, -2.7272727273, -2.5000000000, -2.2727272727, -2.0454545455,
91      -1.8181818182,  -1.5909090909, -1.3636363636, -1.1363636364, -0.9090909091, -0.6818181818,
92      -0.4545454545,  -0.2272727273, 0.0000000000,  0.2272727273,  0.4545454545,  0.6818181818,
93      0.9090909091,   1.1363636364,  1.3636363636,  1.5909090909,  1.8181818182,  2.0454545455,
94      2.2727272727,   2.5000000000,  2.7272727273,  2.9545454545,  3.1818181818,  3.4090909091,
95      3.6363636364,   3.8636363636,  4.0909090909,  4.3181818182,  4.5454545455,  4.7727272727,
96      5.0000000000,   5.2272727273,  5.4545454545,  5.6818181818,  5.9090909091,  6.1363636364,
97      6.3636363636,   6.5909090909,  6.8181818182,  7.0454545455,  7.2727272727,  7.5000000000,
98      7.7272727273,   7.9545454545,  8.1818181818,  8.4090909091,  8.6363636364,  8.8636363636,
99      9.0909090909,   9.3181818182,  9.5454545455,  9.7727272727,  10.0000000000},
100     {0.0000453979, 0.0000569815, 0.0000715205, 0.0000897689, 0.0001126729, 0.0001414198,
101      0.0001774998, 0.0002227827, 0.0002796147, 0.0003509396, 0.0004404502, 0.0005527786,
102      0.0006937345, 0.0008706021, 0.0010925128, 0.0013709094, 0.0017201256, 0.0021581065,
103      0.0027073042, 0.0033957870, 0.0042586071, 0.0053394826, 0.0066928509, 0.0083863576,
104      0.0105038445, 0.0131488902, 0.0164489307, 0.0205599431, 0.0256715863, 0.0320125562,
105      0.0398556989, 0.0495221198, 0.0613831074, 0.0758581800, 0.0934070047, 0.1145124805,
106      0.1396521834, 0.1692560327, 0.2036499335, 0.2429886272, 0.2871859014, 0.3358556241,
107      0.3882805886, 0.4434251301, 0.5000000000, 0.5565748699, 0.6117194114, 0.6641443759,
108      0.7128140986, 0.7570113728, 0.7963500665, 0.8307439673, 0.8603478166, 0.8854875195,
109      0.9065929953, 0.9241418200, 0.9386168926, 0.9504778802, 0.9601443011, 0.9679874438,
110      0.9743284137, 0.9794400569, 0.9835510693, 0.9868511098, 0.9894961555, 0.9916136424,
111      0.9933071491, 0.9946605174, 0.9957413929, 0.9966042130, 0.9972926958, 0.9978418935,
112      0.9982798744, 0.9986290906, 0.9989074872, 0.9991293979, 0.9993062655, 0.9994472214,
113      0.9995595498, 0.9996490604, 0.9997203853, 0.9997772173, 0.9998225002, 0.9998585802,
114      0.9998873271, 0.9999102311, 0.9999284795, 0.9999430185, 0.9999546021});
115 }
116
117 TEST(LogisticTest, IvalidInputOutputType_NEG)
118 {
119   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
120
121   Shape input_shape = {1};
122   std::vector<float> input_data{10};
123   Tensor input_tensor =
124     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, memory_manager.get());
125   Tensor output_tensor = makeOutputTensor(DataType::U8, 1. / 256, 0);
126
127   Logistic kernel(&input_tensor, &output_tensor);
128   EXPECT_ANY_THROW(kernel.configure());
129 }
130
131 TEST(LogisticTest, IvalidQuantParam_NEG)
132 {
133   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
134   Shape input_shape = {2};
135   std::vector<float> input_data{-10, 10};
136   std::pair<float, int32_t> input_quant_param = quantizationParams<uint8_t>(-10, 10);
137   Tensor input_tensor =
138     makeInputTensor<DataType::U8>(input_shape, input_quant_param.first, input_quant_param.second,
139                                   input_data, memory_manager.get());
140   Tensor output_tensor = makeOutputTensor(DataType::U8, 1. / 255, 0);
141
142   Logistic kernel(&input_tensor, &output_tensor);
143   EXPECT_ANY_THROW(kernel.configure());
144 }
145
146 } // namespace
147 } // namespace kernels
148 } // namespace luci_interpreter