Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Softmax.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/Softmax.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> constexpr loco::DataType toLocoDataType();
31
32 template <> constexpr loco::DataType toLocoDataType<float>() { return loco::DataType::FLOAT32; }
33
34 template <> constexpr loco::DataType toLocoDataType<uint8_t>() { return loco::DataType::U8; }
35
36 template <> constexpr loco::DataType toLocoDataType<int8_t>() { return loco::DataType::S8; }
37
38 template <typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
39 void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
40            std::initializer_list<float> input_data, std::initializer_list<float> output_data)
41 {
42   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
43
44   Tensor input_tensor =
45     makeInputTensor<toLocoDataType<T>()>(input_shape, input_data, memory_manager.get());
46   Tensor output_tensor = makeOutputTensor(toLocoDataType<T>());
47
48   SoftmaxParams params{};
49   params.beta = 0.1;
50
51   Softmax kernel(&input_tensor, &output_tensor, params);
52   kernel.configure();
53   memory_manager->allocate_memory(output_tensor);
54   kernel.execute();
55
56   EXPECT_THAT(extractTensorData<T>(output_tensor), FloatArrayNear(output_data));
57   EXPECT_THAT(extractTensorShape(output_tensor), output_shape);
58 }
59
60 template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
61 void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
62            std::initializer_list<float> input_data, std::initializer_list<float> output_data)
63 {
64   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
65
66   std::pair<float, int32_t> input_quant_param =
67     quantizationParams<T>(std::min<float>(std::min<float>(input_data), 0.f),
68                           std::max<float>(std::max<float>(input_data), 0.f));
69   std::pair<float, int32_t> output_quant_param =
70     quantizationParams<T>(std::min<float>(std::min<float>(output_data), 0.f),
71                           std::max<float>(std::max<float>(output_data), 0.f));
72   Tensor input_tensor = makeInputTensor<toLocoDataType<T>()>(input_shape, input_quant_param.first,
73                                                              input_quant_param.second, input_data,
74                                                              memory_manager.get());
75   Tensor output_tensor =
76     makeOutputTensor(toLocoDataType<T>(), output_quant_param.first, output_quant_param.second);
77
78   SoftmaxParams params{};
79   params.beta = 0.1;
80
81   Softmax kernel(&input_tensor, &output_tensor, params);
82   kernel.configure();
83   memory_manager->allocate_memory(output_tensor);
84   kernel.execute();
85
86   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
87   EXPECT_THAT(dequantizeTensorData(output_tensor),
88               FloatArrayNear(output_data, output_tensor.scale()));
89 }
90
91 template <typename T> class SoftmaxTest : public ::testing::Test
92 {
93 };
94
95 using DataTypes = ::testing::Types<float, uint8_t, int8_t>;
96 TYPED_TEST_CASE(SoftmaxTest, DataTypes);
97
98 TYPED_TEST(SoftmaxTest, Simple)
99 {
100   Check<TypeParam>({2, 1, 2, 3}, {2, 1, 2, 3},
101                    {
102                      5, -9, 8,  //
103                      -7, 2, -4, //
104                      1, -2, 9,  //
105                      3, -6, -1, //
106                    },
107                    {
108                      0.38514, 0.09497, 0.51989, //
109                      0.20792, 0.51141, 0.28067, //
110                      0.25212, 0.18678, 0.56110, //
111                      0.48149, 0.19576, 0.32275, //
112                    });
113 }
114
115 } // namespace
116 } // namespace kernels
117 } // namespace luci_interpreter