ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Add.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 "execute/OMUtils.h"
19#include "OMStatus.h"
21#include "core/OMUtils.h"
22
23#include "core/OMRuntimeShape.h"
24#include "PALAdd.h"
25
26using namespace onert_micro;
27using namespace onert_micro::execute;
28
29namespace
30{
31
32constexpr uint32_t input1TensorIdx = 0;
33constexpr uint32_t input2TensorIdx = 1;
34constexpr uint32_t outputTensorIdx = 0;
35
36} // namespace
37
38// NOTE: doesnt currently support dynamic shapes
39// TODO: reduce code duplication with Mul, Sub
40OMStatus onert_micro::execute::execute_kernel_CircleAdd(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 *output;
49
50 uint8_t *input1_data;
51 uint8_t *input2_data;
52 uint8_t *output_data;
53
54 uint16_t input1_index = 0;
55 uint16_t input2_index = 0;
56
57 const circle::AddOptions *options;
58 // Read kernel
59 {
60 execute::OMRuntimeKernel runtime_kernel;
61 runtime_kernel.readKernel(op_index, runtime_context);
62
63 input1 = runtime_kernel.inputs[input1TensorIdx];
64 input2 = runtime_kernel.inputs[input2TensorIdx];
65 output = runtime_kernel.outputs[outputTensorIdx];
66 assert(input1 != nullptr);
67 assert(input2 != nullptr);
68 assert(output != nullptr);
69
70 runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
71
72 input1_data = runtime_kernel.inputs_data[input1TensorIdx];
73 input2_data = runtime_kernel.inputs_data[input2TensorIdx];
75 assert(input1_data != nullptr);
76 assert(input2_data != nullptr);
77 assert(output_data != nullptr);
78
79 options = runtime_kernel.first_operator->builtin_options_as_AddOptions();
80
81 input1_index = runtime_kernel.inputs_index[input1TensorIdx];
82 input2_index = runtime_kernel.inputs_index[input2TensorIdx];
83 }
84
85 OMStatus status;
86
87 core::OMRuntimeShape input1_shape(input1);
88 core::OMRuntimeShape input2_shape(input2);
90
91#ifndef DIS_DYN_SHAPES
92 // Check dynamic shapes
93 {
94 auto input_1_dynamic_shape = runtime_storage.getDynamicRuntimeShape(input1_index);
95 if (input_1_dynamic_shape.flatSize() != 0)
96 input1_shape = input_1_dynamic_shape;
97
98 auto input_2_dynamic_shape = runtime_storage.getDynamicRuntimeShape(input2_index);
99 if (input_2_dynamic_shape.flatSize() != 0)
100 input2_shape = input_2_dynamic_shape;
101 }
102#endif // DIS_DYN_SHAPES
103
104 // Check broadcast property
106 const bool need_broadcast = pal::processBroadcastShapes(input1_shape, input2_shape, &params);
107 switch (input1->type())
108 {
109#ifndef DIS_FLOAT
110 case circle::TensorType_FLOAT32:
111 {
112 execute::calculateActivationRange(options->fused_activation_function(),
113 &params.float_activation_min, &params.float_activation_max);
114 if (need_broadcast)
115 {
117 params, input1_shape, core::utils::castInputData<float>(input1_data), input2_shape,
118 core::utils::castInputData<float>(input2_data), output_shape,
119 core::utils::castOutputData<float>(output_data));
120 }
121 else
122 {
123 status =
124 pal::Add(params, output_shape.flatSize(), core::utils::castInputData<float>(input1_data),
125 core::utils::castInputData<float>(input2_data),
126 core::utils::castOutputData<float>(output_data));
127 }
128 }
129 break;
130#endif // DIS_FLOAT
131 case circle::TensorType_INT64:
132 {
133 execute::calculateActivationRange(options->fused_activation_function(),
134 &params.int64_activation_min, &params.int64_activation_max);
135
136 if (need_broadcast)
137 {
139 params, input1_shape, core::utils::castInputData<int64_t>(input1_data), input2_shape,
140 core::utils::castInputData<int64_t>(input2_data), output_shape,
141 core::utils::castOutputData<int64_t>(output_data));
142 }
143 else
144 {
145 status = pal::Add(params, input1_shape.flatSize(),
146 core::utils::castInputData<int64_t>(input1_data),
147 core::utils::castInputData<int64_t>(input2_data),
148 core::utils::castOutputData<int64_t>(output_data));
149 }
150 }
151 break;
152 case circle::TensorType_INT32:
153 {
154 execute::calculateActivationRange(options->fused_activation_function(),
155 &params.int32_activation_min, &params.int32_activation_max);
156
157 if (need_broadcast)
158 {
160 params, input1_shape, core::utils::castInputData<int32_t>(input1_data), input2_shape,
161 core::utils::castInputData<int32_t>(input2_data), output_shape,
162 core::utils::castOutputData<int32_t>(output_data));
163 }
164 else
165 {
166 status = pal::Add(params, input1_shape.flatSize(),
167 core::utils::castInputData<int32_t>(input1_data),
168 core::utils::castInputData<int32_t>(input2_data),
169 core::utils::castOutputData<int32_t>(output_data));
170 }
171 }
172 break;
173#ifndef DIS_QUANT
174 case circle::TensorType_INT8:
175 {
176 core::ArithmeticQuantParams add_params{};
177
178 calculateQuantParams(add_params, input1, input2, output,
179 options->fused_activation_function());
180
181 if (need_broadcast)
182 {
184 add_params, input1_shape, core::utils::castInputData<int8_t>(input1_data), input2_shape,
185 core::utils::castInputData<int8_t>(input2_data), output_shape,
186 core::utils::castOutputData<int8_t>(output_data));
187 }
188 else
189 {
190 status = pal::Add(add_params, input1_shape.flatSize(),
191 core::utils::castInputData<int8_t>(input1_data),
192 core::utils::castInputData<int8_t>(input2_data),
193 core::utils::castOutputData<int8_t>(output_data));
194 }
195 }
196 break;
197#endif // DIF_QUANT
198 default:
199 {
200 status = UnsupportedType;
201 assert(false && "Unsupported type.");
202 }
203 }
204
205 return status;
206}
OMRuntimeShape getDynamicRuntimeShape(uint16_t tensor_index)
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 BroadcastAdd4DSlow(const core::BinaryArithmeticBroadcastParams &params, const core::OMRuntimeShape &input1_shape, const T *input1_data, const core::OMRuntimeShape &input2_shape, const T *input2_data, const core::OMRuntimeShape &output_shape, T *output_data)
bool processBroadcastShapes(const core::OMRuntimeShape &shape0, const core::OMRuntimeShape &shape1, core::BinaryArithmeticBroadcastParams *params)
OMStatus Add(const core::ArithmeticQuantParams &params, const uint32_t flat_size, const int8_t *input1_data, const int8_t *input2_data, int8_t *output_data)
Definition PALAdd.h:33
void calculateQuantParams(core::ArithmeticQuantParams &params, const circle::Tensor *input1, const circle::Tensor *input2, const circle::Tensor *output, circle::ActivationFunctionType act)
Definition OMUtils.cpp:194
OMStatus calculateActivationRange(circle::ActivationFunctionType activation, T *activation_min, T *activation_max)
Definition OMUtils.h:36
@ UnsupportedType
Definition OMStatus.h:26
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage