2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 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/Div.h"
19 #include "kernels/TestUtils.h"
21 namespace luci_interpreter
28 using namespace testing;
30 float GetTolerance(float min, float max)
32 const float kQuantizedStep = (max - min) / 255.0f;
33 const float kQuantizedTolerance = 2.0f * kQuantizedStep + kQuantizedStep * kQuantizedStep;
34 return kQuantizedTolerance;
39 Shape base_shape = {2, 3, 1, 1};
41 std::vector<int32_t> output_shape = {2, 3, 1, 1};
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, 1.6f, 0.5f, 0.4f, 1.6f, 0.4f};
45 std::vector<float> test_outputs{1.5f, 1.4375f, 1.8f, 1.25f, 0.5f, 2.75f};
47 Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>(base_shape, input1_data);
48 Tensor input2_tensor = makeInputTensor<DataType::FLOAT32>(base_shape, input2_data);
50 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
53 params.activation = Activation::RELU;
55 Div kernel(&input1_tensor, &input2_tensor, &output_tensor, params);
59 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(test_outputs, 0.0001f));
60 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
63 TEST(DivTest, FloatBroadcast)
65 Shape input1_shape = {1, 3};
66 Shape input2_shape = {3, 1};
68 std::vector<float> input1_data{-0.3f, 2.3f, 0.9f};
69 std::vector<float> input2_data{0.2f, 1.6f, 0.5f};
70 std::vector<float> test_outputs{0.f, 11.5f, 4.5f, 0.f, 1.4375f, 0.5625f, 0.f, 4.6f, 1.8f};
72 Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>(input1_shape, input1_data);
73 Tensor input2_tensor = makeInputTensor<DataType::FLOAT32>(input2_shape, input2_data);
75 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
78 params.activation = Activation::RELU;
80 Div kernel(&input1_tensor, &input2_tensor, &output_tensor, params);
84 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(test_outputs, 0.0001f));
89 Shape base_shape = {1, 2, 2, 1};
91 std::vector<int32_t> output_shape = {1, 2, 2, 1};
93 std::vector<float> input1_data = {-0.8f, -0.2f, 0.3f, 0.7f};
94 std::vector<float> input2_data = {-0.8f, 0.4f, 0.8f, 1.0f};
95 std::vector<float> test_outputs{1.0f, 0.f, 0.375f, 0.7f};
97 const float kQuantizedTolerance = GetTolerance(-1.0, 1.0);
99 std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(-1.f, 1.f);
101 Tensor input1_tensor =
102 makeInputTensor<DataType::U8>(base_shape, quant_param.first, quant_param.second, input1_data);
103 Tensor input2_tensor =
104 makeInputTensor<DataType::U8>(base_shape, quant_param.first, quant_param.second, input2_data);
106 Tensor output_tensor =
107 makeOutputTensor(getElementType<uint8_t>(), quant_param.first, quant_param.second);
110 params.activation = Activation::RELU;
112 Div kernel(&input1_tensor, &input2_tensor, &output_tensor, params);
116 EXPECT_THAT(dequantizeTensorData(output_tensor),
117 FloatArrayNear(test_outputs, kQuantizedTolerance));
118 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(output_shape));
121 TEST(DivTest, Input_Output_Type_NEG)
123 Tensor input1_tensor = makeInputTensor<DataType::FLOAT32>({1}, {1.f});
124 Tensor input2_tensor = makeInputTensor<DataType::S32>({1}, {2});
125 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
128 params.activation = Activation::RELU;
130 Div kernel(&input1_tensor, &input2_tensor, &output_tensor, params);
131 EXPECT_ANY_THROW(kernel.configure());
134 TEST(DivTest, Invalid_Input_Type_NEG)
136 Tensor input1_tensor = makeInputTensor<DataType::S64>({1}, {1});
137 Tensor input2_tensor = makeInputTensor<DataType::S64>({1}, {2});
138 Tensor output_tensor = makeOutputTensor(DataType::S64);
141 params.activation = Activation::RELU;
143 Div kernel(&input1_tensor, &input2_tensor, &output_tensor, params);
145 EXPECT_ANY_THROW(kernel.execute());
149 } // namespace kernels
150 } // namespace luci_interpreter