2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "kernels/Pow.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
22 namespace luci_interpreter
29 using namespace testing;
31 class PowTest : public ::testing::Test
34 void SetUp() override { _memory_manager = std::make_unique<TestMemoryManager>(); }
36 std::unique_ptr<IMemoryManager> _memory_manager;
39 TEST_F(PowTest, SimplePow)
41 std::initializer_list<int32_t> base_shape = {1, 1, 3, 2};
43 std::vector<float> input1_data{0.3f, 2.3f, 0.9f, 0.5f, 0.8f, 1.1f};
44 std::vector<float> input2_data{0.2f, 0.3f, -0.4f, 0.5f, 1.0f, 0.9f};
45 std::vector<float> test_outputs{0.786f, 1.2838f, 1.043f, 0.7071f, 0.8f, 1.08956f};
47 Tensor input1_tensor =
48 makeInputTensor<DataType::FLOAT32>(base_shape, input1_data, _memory_manager.get());
49 Tensor input2_tensor =
50 makeInputTensor<DataType::FLOAT32>(base_shape, input2_data, _memory_manager.get());
51 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
53 Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
55 _memory_manager->allocate_memory(output_tensor);
58 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(test_outputs, 0.0001f));
59 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(base_shape));
62 TEST_F(PowTest, FloatBroadcastPow)
64 std::initializer_list<int32_t> input1_shape = {1, 3};
65 std::initializer_list<int32_t> input2_shape = {3, 1};
67 std::vector<float> input1_data{0.3f, 2.3f, 0.9f};
68 std::vector<float> input2_data{0.2f, 0.3f, 0.4f};
69 std::vector<float> test_outputs{0.786f, 1.18126f, 0.9791f, 0.6968f, 1.28386f,
70 0.96888f, 0.6178f, 1.3953f, 0.9587f};
72 Tensor input1_tensor =
73 makeInputTensor<DataType::FLOAT32>(input1_shape, input1_data, _memory_manager.get());
74 Tensor input2_tensor =
75 makeInputTensor<DataType::FLOAT32>(input2_shape, input2_data, _memory_manager.get());
76 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
78 Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
80 _memory_manager->allocate_memory(output_tensor);
83 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(test_outputs, 0.0001f));
86 TEST_F(PowTest, IntPow)
88 std::initializer_list<int32_t> base_shape = {1, 3};
90 std::vector<int32_t> input_data{2, 3, 4};
91 std::vector<int32_t> test_outputs{4, 27, 256};
93 Tensor input1_tensor =
94 makeInputTensor<DataType::S32>(base_shape, input_data, _memory_manager.get());
95 Tensor input2_tensor =
96 makeInputTensor<DataType::S32>(base_shape, input_data, _memory_manager.get());
97 Tensor output_tensor = makeOutputTensor(DataType::S32);
99 Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
101 _memory_manager->allocate_memory(output_tensor);
104 EXPECT_THAT(extractTensorData<int32_t>(output_tensor), ::testing::ElementsAreArray(test_outputs));
105 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(base_shape));
108 TEST_F(PowTest, Input_Output_Type_NEG)
110 Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.0f}, _memory_manager.get());
111 Tensor input2_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.0f}, _memory_manager.get());
112 Tensor output_tensor = makeOutputTensor(DataType::BOOL);
114 Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
115 EXPECT_ANY_THROW(kernel.configure());
118 TEST_F(PowTest, Input_Type_Mismatch_NEG)
120 Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.0f}, _memory_manager.get());
121 Tensor input2_tensor = makeInputTensor<DataType::S32>({1}, {4}, _memory_manager.get());
122 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
124 Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
125 EXPECT_ANY_THROW(kernel.configure());
128 TEST_F(PowTest, Invalid_Input_Type_NEG)
130 Tensor input1_tensor = makeInputTensor<DataType::S64>({1}, {1}, _memory_manager.get());
131 Tensor input2_tensor = makeInputTensor<DataType::S64>({1}, {1}, _memory_manager.get());
132 Tensor output_tensor = makeOutputTensor(DataType::S64);
134 Pow kernel(&input1_tensor, &input2_tensor, &output_tensor);
136 EXPECT_ANY_THROW(kernel.execute());
140 } // namespace kernels
141 } // namespace luci_interpreter