ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Div.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 "PALDiv.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 Add, Sub
40OMStatus onert_micro::execute::execute_kernel_CircleDiv(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 const circle::DivOptions *options;
55 // Read kernel
56 {
57 execute::OMRuntimeKernel runtime_kernel;
58 runtime_kernel.readKernel(op_index, runtime_context);
59
60 input1 = runtime_kernel.inputs[input1TensorIdx];
61 input2 = runtime_kernel.inputs[input2TensorIdx];
62 output = runtime_kernel.outputs[outputTensorIdx];
63 assert(input1 != nullptr);
64 assert(input2 != nullptr);
65 assert(output != nullptr);
66
67 runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
68
69 input1_data = runtime_kernel.inputs_data[input1TensorIdx];
70 input2_data = runtime_kernel.inputs_data[input2TensorIdx];
72 assert(input1_data != nullptr);
73 assert(input2_data != nullptr);
74 assert(output_data != nullptr);
75
76 options = runtime_kernel.first_operator->builtin_options_as_DivOptions();
77 }
78
79 OMStatus status;
80
81 core::OMRuntimeShape input1_shape(input1);
82 core::OMRuntimeShape input2_shape(input2);
84
86 const bool need_broadcast = pal::processBroadcastShapes(input1_shape, input2_shape, &params);
87
88 switch (input1->type())
89 {
90#ifndef DIS_FLOAT
91 case circle::TensorType_FLOAT32:
92 {
93 status = execute::calculateActivationRange(options->fused_activation_function(),
94 &params.float_activation_min,
95 &params.float_activation_max);
96
97 if (need_broadcast)
98 {
100 params, input1_shape, core::utils::castInputData<float>(input1_data), input2_shape,
101 core::utils::castInputData<float>(input2_data), output_shape,
102 core::utils::castOutputData<float>(output_data));
103 }
104 else
105 {
106 status =
107 pal::Div(params, input1_shape.flatSize(), core::utils::castInputData<float>(input1_data),
108 core::utils::castInputData<float>(input2_data),
109 core::utils::castOutputData<float>(output_data));
110 }
111 }
112 break;
113#endif // DIS_FLOAT
114 case circle::TensorType_INT64:
115 {
116 status = execute::calculateActivationRange(options->fused_activation_function(),
117 &params.int64_activation_min,
118 &params.int64_activation_max);
119
120 if (need_broadcast)
121 {
123 params, input1_shape, core::utils::castInputData<int64_t>(input1_data), input2_shape,
124 core::utils::castInputData<int64_t>(input2_data), output_shape,
125 core::utils::castOutputData<int64_t>(output_data));
126 }
127 else
128 {
129 status = pal::Div(params, input1_shape.flatSize(),
130 core::utils::castInputData<int64_t>(input1_data),
131 core::utils::castInputData<int64_t>(input2_data),
132 core::utils::castOutputData<int64_t>(output_data));
133 }
134 }
135 break;
136 case circle::TensorType_INT32:
137 {
138 status = execute::calculateActivationRange(options->fused_activation_function(),
139 &params.int32_activation_min,
140 &params.int32_activation_max);
141
142 if (need_broadcast)
143 {
145 params, input1_shape, core::utils::castInputData<int32_t>(input1_data), input2_shape,
146 core::utils::castInputData<int32_t>(input2_data), output_shape,
147 core::utils::castOutputData<int32_t>(output_data));
148 }
149 else
150 {
151 status = pal::Div(params, input1_shape.flatSize(),
152 core::utils::castInputData<int32_t>(input1_data),
153 core::utils::castInputData<int32_t>(input2_data),
154 core::utils::castOutputData<int32_t>(output_data));
155 }
156 }
157 break;
158 default:
159 {
160 status = UnsupportedType;
161 assert(false && "Unsupported type.");
162 }
163 }
164
165 return status;
166}
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 BroadcastDiv4DSlow(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 Div(const core::BinaryArithmeticBroadcastParams &params, const int flat_size, const T *input1_data, const T *input2_data, T *output_data)
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