ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Softmax.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
23
24#include "PALSoftmax.h"
25
26#include "execute/OMUtils.h"
27
28using namespace onert_micro;
29using namespace onert_micro::execute;
30
31namespace
32{
33
34constexpr uint32_t inputTensorIdx = 0;
35constexpr uint32_t outputTensorIdx = 0;
36
37static const int kScaledDiffIntegerBits = 5;
38void preprocessSoftmaxScaling(double beta, double input_scale, int input_integer_bits,
39 int32_t *quantized_multiplier, int *left_shift)
40{
41 const double max_real_multiplier = (1LL << 31) - 1.0;
42 const double input_beta_real_multiplier =
43 std::min<double>(beta * input_scale * (1 << (31 - input_integer_bits)), max_real_multiplier);
44
45 onert_micro::execute::quantizeMultiplier(input_beta_real_multiplier, quantized_multiplier,
46 left_shift);
47}
48
49} // namespace
50
51// NOTE: doesnt currently support dynamic shapes
52namespace onert_micro
53{
54namespace execute
55{
56
58{
59 core::OMRuntimeContext &runtime_context = execute_args.runtime_context;
60 core::OMRuntimeStorage &runtime_storage = execute_args.runtime_storage;
61 uint16_t op_index = execute_args.kernel_index;
62
63 const circle::Tensor *input = nullptr;
64 const circle::Tensor *output = nullptr;
65
66 uint8_t *input_data = nullptr;
67 uint8_t *output_data = nullptr;
68
69 OMStatus status = Ok;
70
71 const circle::SoftmaxOptions *options;
72 {
73 OMRuntimeKernel runtime_kernel;
74 runtime_kernel.readKernel(op_index, runtime_context);
75
76 input = runtime_kernel.inputs[inputTensorIdx];
77 output = runtime_kernel.outputs[outputTensorIdx];
78
79 assert(input != nullptr);
80 assert(output != nullptr);
81
82 status = runtime_kernel.getDataFromStorage(op_index, runtime_storage, runtime_context);
83 if (status != Ok)
84 return status;
85
86 input_data = runtime_kernel.inputs_data[inputTensorIdx];
87 output_data = runtime_kernel.outputs_data[outputTensorIdx];
88
89 options = runtime_kernel.first_operator->builtin_options_as_SoftmaxOptions();
90 }
91
92 assert(input_data != nullptr);
93 assert(output_data != nullptr);
94
95 const float beta = options->beta();
96
97 core::OMRuntimeShape inputs_shape(input);
98 core::OMRuntimeShape outputs_shape(output);
99
100 const auto dim_count = inputs_shape.dimensionsCount();
101
102 const auto trailing_dim = dim_count - 1;
103
104 int flat_size = 1;
105 for (int i = 0; i < inputs_shape.dimensionsCount(); ++i)
106 {
107 flat_size *= (i == trailing_dim) ? 1 : inputs_shape.dims(i);
108 }
109
110 core::SoftmaxParams params{};
111 params.beta = beta;
112 params.num_rows = flat_size;
113 params.row_size = std::min(inputs_shape.dims(trailing_dim), outputs_shape.dims(trailing_dim));
114
115 switch (input->type())
116 {
117#ifndef DIS_FLOAT
118 case circle::TensorType_FLOAT32:
119 {
120
121 status = pal::Softmax(params, core::utils::castInputData<float>(input_data),
122 core::utils::castOutputData<float>(output_data));
123 }
124 break;
125#endif // DIS_FLOAT
126#ifndef DIS_QUANT
127 case circle::TensorType_INT8:
128 {
129 assert(output->type() == circle::TensorType_INT8);
130 if (output->type() != circle::TensorType_INT8)
131 return UnsupportedType;
132
133 assert(input->quantization() != nullptr and output->quantization() != nullptr);
134 assert(input->quantization()->scale() != nullptr and
135 output->quantization()->scale() != nullptr);
136 assert(input->quantization()->zero_point() != nullptr and
137 output->quantization()->zero_point() != nullptr);
138 assert(input->quantization()->scale()->size() == 1 and
139 output->quantization()->scale()->size() == 1);
140 assert(input->quantization()->zero_point()->size() == 1 and
141 output->quantization()->zero_point()->size() == 1);
142
143 params.output_scale = output->quantization()->scale()->operator[](0);
144 params.input_scale = input->quantization()->scale()->operator[](0);
145 params.output_zp = output->quantization()->zero_point()->operator[](0);
146 params.input_zp = input->quantization()->zero_point()->operator[](0);
147
148 int left_shift = 0;
149 preprocessSoftmaxScaling(static_cast<double>(params.beta),
150 static_cast<double>(params.input_scale), kScaledDiffIntegerBits,
151 &params.input_multiplier, &left_shift);
152 params.input_left_shift = left_shift;
153 params.diff_min = -1.0 * onert_micro::execute::calculateInputRadius(
154 kScaledDiffIntegerBits, params.input_left_shift, 31);
155
156 status = pal::Softmax(params, core::utils::castInputData<int8_t>(input_data),
157 core::utils::castOutputData<int8_t>(output_data));
158 }
159 break;
160#endif // DIS_QUANT
161 default:
162 {
163 status = UnsupportedType;
164 assert(false && "Unsupported type.");
165 }
166 }
167
168 return status;
169}
170
171} // namespace execute
172} // namespace onert_micro
size_t dimensionsCount() const noexcept
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]
constexpr uint32_t outputTensorIdx
OMStatus Softmax(const core::SoftmaxParams &params, const T *input_data, U *output_data)
void quantizeMultiplier(double double_multiplier, int32_t *quantized_multiplier, int *shift)
Definition OMUtils.cpp:23
OMStatus execute_kernel_CircleSoftmax(const OMExecuteArgs &execute_args)
Definition Softmax.cpp:57
int calculateInputRadius(int input_integer_bits, int input_left_shift, int total_signed_bits)
Definition OMUtils.h:170
@ UnsupportedType
Definition OMStatus.h:26
core::OMRuntimeContext & runtime_context
core::OMRuntimeStorage & runtime_storage