2 * Copyright (c) 2021 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/PadV2.h"
19 #include "kernels/TestUtils.h"
20 #include "luci_interpreter/TestMemoryManager.h"
22 namespace luci_interpreter
29 using namespace testing;
31 float GetTolerance(float min, float max) { return (max - min) / 255.0; }
35 std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
36 float kQuantizedTolerance = GetTolerance(-1.0, 1.0);
37 std::pair<float, int32_t> quant_param = quantizationParams<uint8_t>(-1.0f, 1.0f);
38 std::vector<float> input_data{-0.8, 0.2, 0.9, 0.7, 0.1, -0.3};
39 std::vector<int32_t> paddings_data{0, 0, 0, 2, 1, 3, 0, 0};
40 std::vector<float> constant_values_data{0.5};
41 Tensor input_tensor = makeInputTensor<DataType::U8>(
42 {1, 2, 3, 1}, quant_param.first, quant_param.second, input_data, memory_manager.get());
43 Tensor paddings_tensor =
44 makeInputTensor<DataType::S32>({4, 2}, paddings_data, memory_manager.get());
45 Tensor constant_values = makeInputTensor<DataType::U8>(
46 {1}, quant_param.first, quant_param.second, constant_values_data, memory_manager.get());
47 Tensor output_tensor = makeOutputTensor(DataType::U8, quant_param.first, quant_param.second);
49 PadV2 kernel(&input_tensor, &paddings_tensor, &constant_values, &output_tensor);
51 memory_manager->allocate_memory(output_tensor);
54 std::vector<float> ref_output_data = {
55 0.5, -0.8, 0.2, 0.9, 0.5, 0.5, 0.5, 0.5, 0.7, 0.1, -0.3, 0.5, 0.5, 0.5, //
56 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5}; //
57 EXPECT_THAT(dequantizeTensorData(output_tensor),
58 FloatArrayNear(ref_output_data, kQuantizedTolerance));
59 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray({1, 4, 7, 1}));
64 std::unique_ptr<IMemoryManager> memory_manager = std::make_unique<TestMemoryManager>();
65 std::vector<float> input_data{1, 2, 3, 4, 5, 6};
66 std::vector<int32_t> paddings_data{1, 0, 0, 2, 0, 3, 0, 0};
67 std::vector<float> constant_values_data{7};
69 makeInputTensor<DataType::FLOAT32>({1, 2, 3, 1}, input_data, memory_manager.get());
70 Tensor paddings_tensor =
71 makeInputTensor<DataType::S32>({4, 2}, paddings_data, memory_manager.get());
72 Tensor constant_values =
73 makeInputTensor<DataType::FLOAT32>({1}, constant_values_data, memory_manager.get());
74 Tensor output_tensor = makeOutputTensor(DataType::FLOAT32);
76 PadV2 kernel(&input_tensor, &paddings_tensor, &constant_values, &output_tensor);
78 memory_manager->allocate_memory(output_tensor);
81 std::vector<float> ref_output_data{7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
82 7, 7, 7, 7, 7, 7, 7, 7, 1, 2, 3, 7, 7, 7, 4, 5,
83 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
84 std::initializer_list<int32_t> ref_output_shape{2, 4, 6, 1};
85 EXPECT_THAT(extractTensorData<float>(output_tensor), FloatArrayNear(ref_output_data));
86 EXPECT_THAT(extractTensorShape(output_tensor), ::testing::ElementsAreArray(ref_output_shape));
90 } // namespace kernels
91 } // namespace luci_interpreter