6063fa5f25af0d32e7e4f5c026dc0e10b69ad95a
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / LeakyRelu.test.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "kernels/LeakyRelu.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>
32 void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
33            std::initializer_list<float> input_data, std::initializer_list<float> output_data,
34            float alpha)
35 {
36   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
37   constexpr DataType element_type = getElementType<T>();
38   Tensor input_tensor =
39     makeInputTensor<element_type>(input_shape, input_data, memory_manager.get());
40   Tensor output_tensor = makeOutputTensor(element_type);
41
42   LeakyReluParams params{};
43   params.alpha = alpha;
44
45   LeakyRelu kernel(&input_tensor, &output_tensor, params);
46
47   kernel.configure();
48   memory_manager->allocate_memory(output_tensor);
49   kernel.execute();
50
51   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
52   EXPECT_THAT(extractTensorData<T>(output_tensor), ::testing::ElementsAreArray(output_data));
53 }
54
55 template <>
56 void Check<uint8_t>(std::initializer_list<int32_t> input_shape,
57                     std::initializer_list<int32_t> output_shape,
58                     std::initializer_list<float> input_data,
59                     std::initializer_list<float> output_data, float alpha)
60 {
61   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
62   const float quantized_tolerance = getTolerance(-8, 127.f / 16.f, 255);
63   std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(-8, 127.f / 16.f);
64   Tensor input_tensor = makeInputTensor<DataType::U8>(
65     input_shape, quant_param.first, quant_param.second, input_data, memory_manager.get());
66   Tensor output_tensor = makeOutputTensor(DataType::U8, quant_param.first, quant_param.second);
67
68   LeakyReluParams params{};
69   params.alpha = alpha;
70
71   LeakyRelu kernel(&input_tensor, &output_tensor, params);
72
73   kernel.configure();
74   memory_manager->allocate_memory(output_tensor);
75   kernel.execute();
76
77   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
78   EXPECT_THAT(dequantizeTensorData(output_tensor),
79               FloatArrayNear(output_data, quantized_tolerance));
80 }
81
82 template <typename T> class LeakReluTest : public ::testing::Test
83 {
84 };
85
86 using DataTypes = ::testing::Types<float, uint8_t>;
87 TYPED_TEST_SUITE(LeakReluTest, DataTypes);
88
89 TYPED_TEST(LeakReluTest, Simple)
90 {
91   Check<TypeParam>(/*input_shape=*/{2, 3}, /*output_shape=*/{2, 3},
92                    /*input_data=*/
93                    {
94                      0.0f, 1.0f, 3.0f,   // Row 1
95                      1.0f, -1.0f, -2.0f, // Row 2
96                    },
97                    /*output_data=*/
98                    {
99                      0.0f, 1.0f, 3.0f,   // Row 1
100                      1.0f, -0.5f, -1.0f, // Row 2
101                    },
102                    /*alpha=*/0.5f);
103
104   SUCCEED();
105 }
106
107 TEST(LeakReluTest, IvalidInputOutputType_NEG)
108 {
109   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
110   Tensor input_tensor = makeInputTensor<DataType::FLOAT32>({2, 3},
111                                                            {
112                                                              0.0f, 1.0f, 3.0f,   // Row 1
113                                                              1.0f, -1.0f, -2.0f, // Row 2
114                                                            },
115                                                            memory_manager.get());
116   Tensor output_tensor = makeOutputTensor(DataType::U8);
117
118   LeakyReluParams params{};
119   params.alpha = 0.5f;
120
121   LeakyRelu kernel(&input_tensor, &output_tensor, params);
122
123   EXPECT_ANY_THROW(kernel.configure());
124 }
125
126 } // namespace
127 } // namespace kernels
128 } // namespace luci_interpreter