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.
18 #include "kernels/Softmax.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
22 namespace luci_interpreter
29 using namespace testing;
31 template <typename T> constexpr loco::DataType toLocoDataType();
33 template <> constexpr loco::DataType toLocoDataType<float>() { return loco::DataType::FLOAT32; }
35 template <> constexpr loco::DataType toLocoDataType<uint8_t>() { return loco::DataType::U8; }
37 template <> constexpr loco::DataType toLocoDataType<int8_t>() { return loco::DataType::S8; }
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)
43 std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
46 makeInputTensor<toLocoDataType<T>()>(input_shape, input_data, memory_manager.get());
47 Tensor output_tensor = makeOutputTensor(toLocoDataType<T>());
49 SoftmaxParams params{};
52 Softmax kernel(&input_tensor, &output_tensor, params);
54 memory_manager->allocate_memory(output_tensor);
57 EXPECT_THAT(extractTensorData<T>(output_tensor), FloatArrayNear(output_data));
58 EXPECT_THAT(extractTensorShape(output_tensor), output_shape);
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)
65 std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
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);
79 SoftmaxParams params{};
82 Softmax kernel(&input_tensor, &output_tensor, params);
84 memory_manager->allocate_memory(output_tensor);
87 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
88 EXPECT_THAT(dequantizeTensorData(output_tensor),
89 FloatArrayNear(output_data, output_tensor.scale()));
92 template <typename T> class SoftmaxTest : public ::testing::Test
96 using DataTypes = ::testing::Types<float, uint8_t, int8_t>;
97 TYPED_TEST_SUITE(SoftmaxTest, DataTypes);
99 TYPED_TEST(SoftmaxTest, Simple)
101 Check<TypeParam>({2, 1, 2, 3}, {2, 1, 2, 3},
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, //
117 } // namespace kernels
118 } // namespace luci_interpreter