ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Pad.cpp
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#include "OMStatus.h"
18
19#include "core/OMUtils.h"
20#include "core/OMRuntimeShape.h"
21
24
25#include "PALPad.h"
26
27using namespace onert_micro;
28using namespace onert_micro::execute;
29
30namespace
31{
32
33constexpr uint32_t input1TensorIdx = 0;
34constexpr uint32_t input2TensorIdx = 1;
35constexpr uint32_t input3TensorIdx = 2;
36constexpr uint32_t outputTensorIdx = 0;
37
38} // namespace
39
40OMStatus onert_micro::execute::execute_kernel_CirclePad(const OMExecuteArgs &execute_args)
41{
42 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
43 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
44 uint16_t op_index = execute_args.kernel_index;
45
46 const circle::Tensor *input1;
47 const circle::Tensor *input2;
48 const circle::Tensor *input3;
49 const circle::Tensor *output;
50
51 uint8_t *input1_data;
52 uint8_t *input2_data;
53 uint8_t *input3_data;
54 uint8_t *output_data;
55
56 const circle::PadOptions *options;
57 // Read kernel
58 {
59 execute::OMRuntimeKernel runtime_kernel;
60 runtime_kernel.readKernel(op_index, runtime_context);
61
62 input1 = runtime_kernel.inputs[input1TensorIdx];
63 input2 = runtime_kernel.inputs[input2TensorIdx];
64 input3 = runtime_kernel.inputs[input3TensorIdx];
65 output = runtime_kernel.outputs[outputTensorIdx];
66 assert(input1 != nullptr);
67 assert(input2 != nullptr);
68 // input3 - can be nullptr
69 assert(output != nullptr);
70
71 runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
72
73 input1_data = runtime_kernel.inputs_data[input1TensorIdx];
74 input2_data = runtime_kernel.inputs_data[input2TensorIdx];
75 input3_data = runtime_kernel.inputs_data[input3TensorIdx];
77 assert(input1_data != nullptr);
78 assert(input2_data != nullptr);
79 // input3_data can be nullptr
80 assert(output_data != nullptr);
81
82 options = runtime_kernel.first_operator->builtin_options_as_PadOptions();
83 }
84
85 OMStatus status = Ok;
86
87 core::OMRuntimeShape input1_shape(input1);
88 core::OMRuntimeShape input2_shape(input2);
90
91 // Create PadParams
92 core::PadParams pad_params{};
93 const auto num_input_dimensions = input1_shape.dimensionsCount();
94 assert(num_input_dimensions <= 5);
95
96 if (num_input_dimensions > 5)
97 return UnsupportedType;
98
99 pad_params.left_padding_count = num_input_dimensions;
100 pad_params.right_padding_count = num_input_dimensions;
101
102 auto *paddings_data = reinterpret_cast<int32_t *>(input2_data);
103 for (int idx = num_input_dimensions - 1; idx >= 0; --idx)
104 {
105 pad_params.left_padding[idx] = paddings_data[idx * 2];
106 pad_params.right_padding[idx] = paddings_data[idx * 2 + 1];
107 }
108
109 switch (input1->type())
110 {
111#ifndef DIS_FLOAT
112 case circle::TensorType_FLOAT32:
113 {
114 float pad_value = input3_data == nullptr ? 0.f : *reinterpret_cast<float *>(input3_data[0]);
115 status = pal::Pad(pad_params, input1_shape, core::utils::castInputData<float>(input1_data),
116 pad_value, output_shape, core::utils::castOutputData<float>(output_data));
117 }
118 break;
119#endif // DIS_FLOAT
120 default:
121 {
122 status = UnsupportedType;
123 assert(false && "Unsupported type");
124 }
125 }
126
127 return status;
128}
uint8_t * outputs_data[maxOutputSize]
const circle::Operator * first_operator
OMStatus getDataFromStorage(uint16_t op_index, core::OMRuntimeStorage &storage, core::OMRuntimeContext &context)
OMStatus readKernel(uint16_t op_index, core::OMRuntimeContext &runtime_context)
const circle::Tensor * outputs[maxOutputSize]
const circle::Tensor * inputs[maxInputSize]
const luci_interpreter::RuntimeShape output_shape
constexpr uint32_t input1TensorIdx
constexpr uint32_t outputTensorIdx
constexpr uint32_t input2TensorIdx
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
@ UnsupportedType
Definition OMStatus.h:26
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage