ONE - On-device Neural Engine
Loading...
Searching...
No Matches
PALPad.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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#ifndef ONERT_MICRO_EXECUTE_PAL_PAD_COMMON_H
18#define ONERT_MICRO_EXECUTE_PAL_PAD_COMMON_H
19
20#include "core/OMRuntimeShape.h"
21#include "core/OMKernelData.h"
22#include "OMStatus.h"
23
24namespace onert_micro
25{
26namespace execute
27{
28namespace pal
29{
30namespace
31{
32constexpr int padKernelMaxDimensionCount = 5;
33} // namespace
34
35OMStatus Pad(const core::PadParams &op_params, const core::OMRuntimeShape &input_shape,
36 const float *input_data, const float pad_value,
37 const core::OMRuntimeShape &output_shape, float *output_data)
38{
39 // Runtime calls are currently fixed at 5 dimensions. Copy inputs so we can
40 // pad them to 5 dims (yes, we are "padding the padding").
41 int left_padding_copy[padKernelMaxDimensionCount];
42 for (int &i : left_padding_copy)
43 {
44 i = 0;
45 }
46 for (int i = 0; i < op_params.left_padding_count; ++i)
47 {
48 left_padding_copy[i + padKernelMaxDimensionCount - op_params.left_padding_count] =
49 op_params.left_padding[i];
50 }
51 int right_padding_copy[padKernelMaxDimensionCount];
52 for (int &i : right_padding_copy)
53 {
54 i = 0;
55 }
56 for (int i = 0; i < op_params.right_padding_count; ++i)
57 {
58 right_padding_copy[i + padKernelMaxDimensionCount - op_params.right_padding_count] =
59 op_params.right_padding[i];
60 }
61 const auto extended_output =
62 core::OMRuntimeShape::extendedShape(padKernelMaxDimensionCount, output_shape);
63 const int output_batch = extended_output.dims(0);
64 const int output_plane = extended_output.dims(1);
65 const int output_height = extended_output.dims(2);
66 const int output_width = extended_output.dims(3);
67 const int output_depth = extended_output.dims(4);
68
69 const int left_b_padding = left_padding_copy[0];
70 const int left_p_padding = left_padding_copy[1];
71 const int left_h_padding = left_padding_copy[2];
72 const int left_w_padding = left_padding_copy[3];
73 const int left_d_padding = left_padding_copy[4];
74
75 const int right_b_padding = right_padding_copy[0];
76 const int right_p_padding = right_padding_copy[1];
77 const int right_h_padding = right_padding_copy[2];
78 const int right_w_padding = right_padding_copy[3];
79 const int right_d_padding = right_padding_copy[4];
80
81 const float *in_ptr = input_data;
82 float *out_ptr = output_data;
83 for (int out_b = 0; out_b < output_batch; ++out_b)
84 {
85 for (int out_p = 0; out_p < output_plane; ++out_p)
86 {
87 for (int out_h = 0; out_h < output_height; ++out_h)
88 {
89 for (int out_w = 0; out_w < output_width; ++out_w)
90 {
91 for (int out_d = 0; out_d < output_depth; ++out_d)
92 {
93 if (out_b < left_b_padding || out_b >= output_batch - right_b_padding ||
94 out_p < left_p_padding || out_p >= output_plane - right_p_padding ||
95 out_h < left_h_padding || out_h >= output_height - right_h_padding ||
96 out_w < left_w_padding || out_w >= output_width - right_w_padding ||
97 out_d < left_d_padding || out_d >= output_depth - right_d_padding)
98 {
99 *out_ptr++ = pad_value;
100 }
101 else
102 {
103 *out_ptr++ = *in_ptr++;
104 }
105 }
106 }
107 }
108 }
109 }
110
111 return Ok;
112}
113
114} // namespace pal
115} // namespace execute
116} // namespace onert_micro
117
118#endif // ONERT_MICRO_EXECUTE_PAL_NEG_COMMON_H
static OMRuntimeShape extendedShape(int new_shape_size, const OMRuntimeShape &shape)
const luci_interpreter::RuntimeShape output_shape
OMStatus Pad(const core::PadParams &op_params, const core::OMRuntimeShape &input_shape, const float *input_data, const float pad_value, const core::OMRuntimeShape &output_shape, float *output_data)
Definition PALPad.h:35