Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / onert-micro / luci-interpreter / pal / common / PALPad.h
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  * Copyright 2019 The TensorFlow Authors. All Rights Reserved.
4  *
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
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #ifndef LUCI_INTERPRETER_PAL_PAD_H
19 #define LUCI_INTERPRETER_PAL_PAD_H
20
21 #include "PALUtils.h"
22
23 namespace luci_interpreter_pal
24 {
25
26 constexpr int PadKernelMaxDimensionCount() { return 5; }
27
28 void Pad(const PadParams &op_params, const luci_interpreter::RuntimeShape &input_shape,
29          const float *input_data, const float *pad_value_ptr,
30          const luci_interpreter::RuntimeShape &output_shape, float *output_data)
31 {
32   // Runtime calls are currently fixed at 5 dimensions. Copy inputs so we can
33   // pad them to 5 dims (yes, we are "padding the padding").
34   int left_padding_copy[PadKernelMaxDimensionCount()];
35   for (int i = 0; i < PadKernelMaxDimensionCount(); i++)
36   {
37     left_padding_copy[i] = 0;
38   }
39   for (int i = 0; i < op_params.left_padding_count; ++i)
40   {
41     left_padding_copy[i + PadKernelMaxDimensionCount() - op_params.left_padding_count] =
42       op_params.left_padding[i];
43   }
44   int right_padding_copy[PadKernelMaxDimensionCount()];
45   for (int i = 0; i < PadKernelMaxDimensionCount(); i++)
46   {
47     right_padding_copy[i] = 0;
48   }
49   for (int i = 0; i < op_params.right_padding_count; ++i)
50   {
51     right_padding_copy[i + PadKernelMaxDimensionCount() - op_params.right_padding_count] =
52       op_params.right_padding[i];
53   }
54   const auto extended_output =
55     luci_interpreter::RuntimeShape::extendedShape(PadKernelMaxDimensionCount(), output_shape);
56   const int output_batch = extended_output.dims(0);
57   const int output_plane = extended_output.dims(1);
58   const int output_height = extended_output.dims(2);
59   const int output_width = extended_output.dims(3);
60   const int output_depth = extended_output.dims(4);
61
62   const int left_b_padding = left_padding_copy[0];
63   const int left_p_padding = left_padding_copy[1];
64   const int left_h_padding = left_padding_copy[2];
65   const int left_w_padding = left_padding_copy[3];
66   const int left_d_padding = left_padding_copy[4];
67
68   const int right_b_padding = right_padding_copy[0];
69   const int right_p_padding = right_padding_copy[1];
70   const int right_h_padding = right_padding_copy[2];
71   const int right_w_padding = right_padding_copy[3];
72   const int right_d_padding = right_padding_copy[4];
73
74   const float pad_value = *pad_value_ptr;
75
76   const float *in_ptr = input_data;
77   float *out_ptr = output_data;
78   for (int out_b = 0; out_b < output_batch; ++out_b)
79   {
80     for (int out_p = 0; out_p < output_plane; ++out_p)
81     {
82       for (int out_h = 0; out_h < output_height; ++out_h)
83       {
84         for (int out_w = 0; out_w < output_width; ++out_w)
85         {
86           for (int out_d = 0; out_d < output_depth; ++out_d)
87           {
88             if (out_b < left_b_padding || out_b >= output_batch - right_b_padding ||
89                 out_p < left_p_padding || out_p >= output_plane - right_p_padding ||
90                 out_h < left_h_padding || out_h >= output_height - right_h_padding ||
91                 out_w < left_w_padding || out_w >= output_width - right_w_padding ||
92                 out_d < left_d_padding || out_d >= output_depth - right_d_padding)
93             {
94               *out_ptr++ = pad_value;
95             }
96             else
97             {
98               *out_ptr++ = *in_ptr++;
99             }
100           }
101         }
102       }
103     }
104   }
105 }
106
107 } // namespace luci_interpreter_pal
108
109 #endif // LUCI_INTERPRETER_PAL_PAD_H