2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "kernels/PadV2.h"
19 #include "kernels/Utils.h"
21 #include <tensorflow/lite/kernels/internal/reference/pad.h>
23 namespace luci_interpreter
28 PadV2::PadV2(const Tensor *input, const Tensor *paddings, const Tensor *constant_values,
30 : Kernel({input, paddings, constant_values}, {output})
34 void PadV2::configure()
36 const Shape &input_shape = input()->shape();
37 const int num_dims = input_shape.num_dims();
40 throw std::runtime_error("Unsupported number of dimensions.");
42 assert(output()->element_type() == input()->element_type());
43 assert(paddings()->element_type() == DataType::S32);
44 assert(constant_values()->element_type() == output()->element_type());
45 // Paddings shape should be [N, 2].
46 assert(paddings()->shape().num_dims() == 2);
47 assert(paddings()->shape().dim(0) == num_dims);
48 assert(paddings()->shape().dim(1) == 2);
49 // Constant values elements number should be 1.
50 assert(constant_values()->shape().num_elements() == 1);
52 Shape output_shape(num_dims);
53 const auto *paddings_data = getTensorData<int32_t>(paddings());
54 for (int i = 0; i < num_dims; ++i)
56 const int32_t padding_before = paddings_data[i * 2];
57 const int32_t padding_after = paddings_data[i * 2 + 1];
58 assert(padding_before >= 0 && padding_after >= 0);
59 output_shape.dim(i) = input_shape.dim(i) + padding_before + padding_after;
62 output()->resize(output_shape);
65 void PadV2::execute() const
67 const int num_dims = input()->shape().num_dims();
69 tflite::PadParams params{};
70 params.left_padding_count = num_dims;
71 params.right_padding_count = num_dims;
73 const auto *paddings_data = getTensorData<int32_t>(paddings());
74 for (int i = num_dims - 1; i >= 0; --i)
76 params.left_padding[i] = paddings_data[i * 2];
77 params.right_padding[i] = paddings_data[i * 2 + 1];
80 switch (input()->element_type())
82 case DataType::FLOAT32:
84 const auto pad_value = getTensorData<float>(constant_values())[0];
85 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<float>(input()),
86 &pad_value, getTensorShape(output()),
87 getTensorData<float>(output()));
92 assert(output()->zero_point() >= std::numeric_limits<uint8_t>::min());
93 assert(output()->zero_point() <= std::numeric_limits<uint8_t>::max());
94 const auto pad_value = getTensorData<uint8_t>(constant_values())[0];
95 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
96 &pad_value, getTensorShape(output()),
97 getTensorData<uint8_t>(output()));
101 throw std::runtime_error("Unsupported type.");
105 } // namespace kernels
106 } // namespace luci_interpreter