2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "kernels/Softmax.h"
18 #include "kernels/TestUtils.h"
19 #include "luci_interpreter/TestMemoryManager.h"
21 namespace luci_interpreter
28 using namespace testing;
30 template <typename T> constexpr loco::DataType toLocoDataType();
32 template <> constexpr loco::DataType toLocoDataType<float>() { return loco::DataType::FLOAT32; }
34 template <> constexpr loco::DataType toLocoDataType<uint8_t>() { return loco::DataType::U8; }
36 template <> constexpr loco::DataType toLocoDataType<int8_t>() { return loco::DataType::S8; }
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)
42 std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
45 makeInputTensor<toLocoDataType<T>()>(input_shape, input_data, memory_manager.get());
46 Tensor output_tensor = makeOutputTensor(toLocoDataType<T>());
48 SoftmaxParams params{};
51 Softmax kernel(&input_tensor, &output_tensor, params);
53 memory_manager->allocate_memory(output_tensor);
56 EXPECT_THAT(extractTensorData<T>(output_tensor), FloatArrayNear(output_data));
57 EXPECT_THAT(extractTensorShape(output_tensor), output_shape);
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)
64 std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
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);
78 SoftmaxParams params{};
81 Softmax kernel(&input_tensor, &output_tensor, params);
83 memory_manager->allocate_memory(output_tensor);
86 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
87 EXPECT_THAT(dequantizeTensorData(output_tensor),
88 FloatArrayNear(output_data, output_tensor.scale()));
91 template <typename T> class SoftmaxTest : public ::testing::Test
95 using DataTypes = ::testing::Types<float, uint8_t, int8_t>;
96 TYPED_TEST_CASE(SoftmaxTest, DataTypes);
98 TYPED_TEST(SoftmaxTest, Simple)
100 Check<TypeParam>({2, 1, 2, 3}, {2, 1, 2, 3},
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, //
116 } // namespace kernels
117 } // namespace luci_interpreter