bfae479a9c1f2802fa7405de551b99ec53d88cd0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / src / kernels / Tanh.test.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2017 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/Tanh.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 class TanhTest : public ::testing::Test
32 {
33 protected:
34   void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
35
36   std::unique_ptr<IMemoryManager> _memory_manager;
37 };
38
39 TEST_F(TanhTest, Float)
40 {
41   Shape input_shape{1, 2, 4, 1};
42   std::vector<float> input_data{
43     0, -6, 2,  4, //
44     3, -2, 10, 1, //
45   };
46   Tensor input_tensor =
47     makeInputTensor<DataType::FLOAT32>(input_shape, input_data, _memory_manager.get());
48   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
49
50   Tanh kernel(&input_tensor, &output_tensor);
51   kernel.configure();
52   _memory_manager->allocate_memory(output_tensor);
53   kernel.execute();
54
55   std::vector<float> ref_output_data{
56     0,          -0.9999877, 0.9640275, 0.999329,  //
57     0.99505475, -0.9640275, 1,         0.7615941, //
58   };
59   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
60 }
61
62 TEST_F(TanhTest, Uint8)
63 {
64   float kMin = -1;
65   float kMax = 127.f / 128.f;
66   float kTanhTolerance = 2 * (1. / 256);
67   std::pair<float, int32_t> input_quant_param = quantizationParams<uint8_t>(8 * kMin, 8 * kMax);
68   std::pair<float, int32_t> output_quant_param = quantizationParams<uint8_t>(kMin, kMax);
69   std::vector<float> input_data{
70     0,  -6, 2, 4, //
71     -4, -2, 8, 1, //
72     0,  -6, 2, 4, //
73     -4, -2, 8, 1, //
74     0,  -6, 2, 4, //
75     -4, -2, 8, 1, //
76     0,  -6, 2, 4, //
77     -4, -2, 8, 1, //
78     0,  -6, 2, 4, //
79     -4, -2, 8, 1, //
80     0,  -6, 2, 4, //
81     -4, -2, 8, 1, //
82   };
83   Tensor input_tensor =
84     makeInputTensor<DataType::U8>({2, 6, 4, 1}, input_quant_param.first, input_quant_param.second,
85                                   input_data, _memory_manager.get());
86   Tensor output_tensor =
87     makeOutputTensor(DataType::U8, output_quant_param.first, output_quant_param.second);
88
89   Tanh kernel(&input_tensor, &output_tensor);
90   kernel.configure();
91   _memory_manager->allocate_memory(output_tensor);
92   kernel.execute();
93
94   std::vector<float> ref_output_data{
95     0.0,       -0.999987, 0.964027, 0.999329, //
96     -0.999329, -0.96402,  0.99999,  0.76159,  //
97     0.0,       -0.999987, 0.964027, 0.999329, //
98     -0.999329, -0.96402,  0.99999,  0.76159,  //
99     0.0,       -0.999987, 0.964027, 0.999329, //
100     -0.999329, -0.96402,  0.99999,  0.76159,  //
101     0.0,       -0.999987, 0.964027, 0.999329, //
102     -0.999329, -0.96402,  0.99999,  0.76159,  //
103     0.0,       -0.999987, 0.964027, 0.999329, //
104     -0.999329, -0.96402,  0.99999,  0.76159,  //
105     0.0,       -0.999987, 0.964027, 0.999329, //
106     -0.999329, -0.96402,  0.99999,  0.76159,  //
107   };
108   std::vector<int32_t> ref_output_shape{2, 6, 4, 1};
109   EXPECT_THAT(dequantizeTensorData(output_tensor), FloatArrayNear(ref_output_data, kTanhTolerance));
110   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
111 }
112
113 TEST_F(TanhTest, InputTypeInvalid_NEG)
114 {
115   std::vector<int64_t> input_data{
116     0,  -6, 2, 4, //
117     -4, -2, 8, 1, //
118     0,  -6, 2, 4, //
119     -4, -2, 8, 1, //
120     0,  -6, 2, 4, //
121     -4, -2, 8, 1, //
122     0,  -6, 2, 4, //
123     -4, -2, 8, 1, //
124     0,  -6, 2, 4, //
125     -4, -2, 8, 1, //
126     0,  -6, 2, 4, //
127     -4, -2, 8, 1, //
128   };
129   Tensor input_tensor =
130     makeInputTensor<DataType::S64>({2, 6, 4, 1}, input_data, _memory_manager.get());
131   Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
132
133   Tanh kernel(&input_tensor, &output_tensor);
134   _memory_manager->allocate_memory(output_tensor);
135   EXPECT_ANY_THROW(kernel.execute());
136 }
137
138 TEST_F(TanhTest, InputOutputMismatch_NEG)
139 {
140   std::vector<float> input_data{
141     0,  -6, 2, 4, //
142     -4, -2, 8, 1, //
143     0,  -6, 2, 4, //
144     -4, -2, 8, 1, //
145     0,  -6, 2, 4, //
146     -4, -2, 8, 1, //
147     0,  -6, 2, 4, //
148     -4, -2, 8, 1, //
149     0,  -6, 2, 4, //
150     -4, -2, 8, 1, //
151     0,  -6, 2, 4, //
152     -4, -2, 8, 1, //
153   };
154   Tensor input_tensor =
155     makeInputTensor<DataType::FLOAT32>({2, 6, 4, 1}, input_data, _memory_manager.get());
156   Tensor output_tensor = makeOutputTensor(DataType::U8);
157
158   Tanh kernel(&input_tensor, &output_tensor);
159   EXPECT_ANY_THROW(kernel.configure());
160 }
161
162 } // namespace
163 } // namespace kernels
164 } // namespace luci_interpreter