Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci-interpreter / src / kernels / PadV2.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/PadV2.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 PadV2::PadV2(const Tensor *input, const Tensor *paddings, const Tensor *constant_values,
29              Tensor *output)
30   : Kernel({input, paddings, constant_values}, {output})
31 {
32 }
33
34 void PadV2::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   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);
51
52   Shape output_shape(num_dims);
53   const auto *paddings_data = getTensorData<int32_t>(paddings());
54   for (int i = 0; i < num_dims; ++i)
55   {
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;
60   }
61
62   output()->resize(output_shape);
63 }
64
65 void PadV2::execute() const
66 {
67   const int num_dims = input()->shape().num_dims();
68
69   tflite::PadParams params{};
70   params.left_padding_count = num_dims;
71   params.right_padding_count = num_dims;
72
73   const auto *paddings_data = getTensorData<int32_t>(paddings());
74   for (int i = num_dims - 1; i >= 0; --i)
75   {
76     params.left_padding[i] = paddings_data[i * 2];
77     params.right_padding[i] = paddings_data[i * 2 + 1];
78   }
79
80   switch (input()->element_type())
81   {
82     case DataType::FLOAT32:
83     {
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()));
88       break;
89     }
90     case DataType::U8:
91     {
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()));
98       break;
99     }
100     default:
101       throw std::runtime_error("Unsupported type.");
102   }
103 }
104
105 } // namespace kernels
106 } // namespace luci_interpreter