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/MirrorPad.h"
19 #include "kernels/Utils.h"
21 #include <tensorflow/lite/kernels/internal/reference/pad.h>
23 namespace luci_interpreter
28 MirrorPad::MirrorPad(const Tensor *input, const Tensor *paddings, Tensor *output,
29 const MirrorPadParams ¶ms)
30 : KernelWithParams<MirrorPadParams>({input, paddings}, {output}, params)
34 void MirrorPad::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 // Paddings shape should be [N, 2].
45 assert(paddings()->shape().num_dims() == 2);
46 assert(paddings()->shape().dim(0) == num_dims);
47 assert(paddings()->shape().dim(1) == 2);
49 Shape output_shape(num_dims);
50 const auto *paddings_data = getTensorData<int32_t>(paddings());
51 for (int i = 0; i < num_dims; ++i)
53 const int32_t padding_before = paddings_data[i * 2];
54 const int32_t padding_after = paddings_data[i * 2 + 1];
55 assert(padding_before >= 0 && padding_after >= 0);
56 output_shape.dim(i) = input_shape.dim(i) + padding_before + padding_after;
59 output()->resize(output_shape);
62 void MirrorPad::execute() const
64 const int num_dims = input()->shape().num_dims();
66 tflite::PadParams params{};
67 params.left_padding_count = num_dims;
68 params.right_padding_count = num_dims;
70 const auto *paddings_data = getTensorData<int32_t>(paddings());
71 for (int i = num_dims - 1; i >= 0; --i)
73 params.left_padding[i] = paddings_data[i * 2];
74 params.right_padding[i] = paddings_data[i * 2 + 1];
77 switch (input()->element_type())
79 case DataType::FLOAT32:
81 const float pad_value = 0;
83 // NOTE: this implementation only obtains min-max values for quantization
84 // TODO: calculate proper inference values
85 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<float>(input()),
86 &pad_value, getTensorShape(output()),
87 getTensorData<float>(output()));
92 // NOTE: this implementation only obtains min-max values for quantization
93 // TODO: calculate proper inference values
94 assert(output()->zero_point() >= std::numeric_limits<uint8_t>::min());
95 assert(output()->zero_point() <= std::numeric_limits<uint8_t>::max());
96 const auto pad_value = static_cast<uint8_t>(output()->zero_point());
97 tflite::reference_ops::Pad(params, getTensorShape(input()), getTensorData<uint8_t>(input()),
98 &pad_value, getTensorShape(output()),
99 getTensorData<uint8_t>(output()));
103 throw std::runtime_error("Unsupported type.");
107 } // namespace kernels
108 } // namespace luci_interpreter