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