Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / MirrorPad.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "kernels/MirrorPad.h"
18
19 #include "kernels/Utils.h"
20
21 #include <tensorflow/lite/kernels/internal/reference/pad.h>
22
23 namespace luci_interpreter
24 {
25 namespace kernels
26 {
27
28 MirrorPad::MirrorPad(const Tensor *input, const Tensor *paddings, Tensor *output,
29                      const MirrorPadParams &params)
30   : KernelWithParams<MirrorPadParams>({input, paddings}, {output}, params)
31 {
32 }
33
34 void MirrorPad::configure()
35 {
36   const Shape &input_shape = input()->shape();
37   const int num_dims = input_shape.num_dims();
38
39   if (num_dims > 4)
40     throw std::runtime_error("Unsupported number of dimensions.");
41
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);
48
49   Shape output_shape(num_dims);
50   const auto *paddings_data = getTensorData<int32_t>(paddings());
51   for (int i = 0; i < num_dims; ++i)
52   {
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;
57   }
58
59   output()->resize(output_shape);
60 }
61
62 void MirrorPad::execute() const
63 {
64   const int num_dims = input()->shape().num_dims();
65
66   tflite::PadParams params{};
67   params.left_padding_count = num_dims;
68   params.right_padding_count = num_dims;
69
70   const auto *paddings_data = getTensorData<int32_t>(paddings());
71   for (int i = num_dims - 1; i >= 0; --i)
72   {
73     params.left_padding[i] = paddings_data[i * 2];
74     params.right_padding[i] = paddings_data[i * 2 + 1];
75   }
76
77   switch (input()->element_type())
78   {
79     case DataType::FLOAT32:
80     {
81       const float pad_value = 0;
82
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()));
88       break;
89     }
90     case DataType::U8:
91     {
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()));
100       break;
101     }
102     default:
103       throw std::runtime_error("Unsupported type.");
104   }
105 }
106
107 } // namespace kernels
108 } // namespace luci_interpreter