Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / Gelu.test.cpp
1 /*
2  * Copyright (c) 2023 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/Gelu.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 void Check(std::initializer_list<int32_t> input_shape, std::initializer_list<int32_t> output_shape,
32            std::initializer_list<float> input_data, std::initializer_list<float> output_data,
33            bool approximate)
34 {
35   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
36   constexpr DataType element_type = getElementType<float>();
37   Tensor input_tensor =
38     makeInputTensor<element_type>(input_shape, input_data, memory_manager.get());
39   Tensor output_tensor = makeOutputTensor(element_type);
40
41   GeluParams params{};
42   params.approximate = approximate;
43
44   Gelu kernel(&input_tensor, &output_tensor, params);
45
46   kernel.configure();
47   memory_manager->allocate_memory(output_tensor);
48   kernel.execute();
49
50   EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
51   EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(output_data));
52 }
53
54 class GeluTest : public ::testing::Test
55 {
56 };
57
58 TEST_F(GeluTest, Simple)
59 {
60   Check(/*input_shape=*/{2, 3}, /*output_shape=*/{2, 3},
61         /*input_data=*/
62         {
63           0.0f, 1.0f, 3.0f,   // Row 1
64           1.0f, -1.0f, -2.0f, // Row 2
65         },
66         /*output_data=*/
67         {
68           0.0f, 0.841345f, 2.99595f,          // Row 1
69           0.841345f, -0.158655f, -0.0455003f, // Row 2
70         },
71         /*approximate=*/false);
72
73   SUCCEED();
74 }
75
76 TEST_F(GeluTest, Approximate)
77 {
78   Check(/*input_shape=*/{2, 3}, /*output_shape=*/{2, 3},
79         /*input_data=*/
80         {
81           0.0f, 1.0f, 3.0f,   // Row 1
82           1.0f, -1.0f, -2.0f, // Row 2
83         },
84         /*output_data=*/
85         {
86           0.0f, 0.841192f, 2.99636f,          // Row 1
87           0.841192f, -0.158808f, -0.0454023f, // Row 2
88         },
89         /*approximate=*/true);
90
91   SUCCEED();
92 }
93
94 TEST_F(GeluTest, DifferentInOutType_NEG)
95 {
96   std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
97   Tensor input_tensor = makeInputTensor<DataType::FLOAT32>({2, 3},
98                                                            {
99                                                              0.0f, 1.0f, 3.0f,   // Row 1
100                                                              1.0f, -1.0f, -2.0f, // Row 2
101                                                            },
102                                                            memory_manager.get());
103   Tensor output_tensor = makeOutputTensor(DataType::U8);
104
105   GeluParams params{};
106   params.approximate = false;
107
108   Gelu kernel(&input_tensor, &output_tensor, params);
109
110   EXPECT_ANY_THROW(kernel.configure());
111 }
112
113 } // namespace
114 } // namespace kernels
115 } // namespace luci_interpreter