Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[onert-micro] proprocessing phase for cmsisnn Softmax #14116

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions onert-micro/onert-micro/include/execute/OMUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ OMStatus TISOHeader(const OMExecuteArgs &execute_args, const circle::Tensor **in
const circle::Tensor **input2, const circle::Tensor **output,
OMRuntimeKernel *runtime_kernel);

inline int calculateInputRadius(int input_integer_bits, int input_left_shift, int total_signed_bits)
{
const double max_input_rescaled = 1.0 * ((1 << input_integer_bits) - 1) *
(1LL << (total_signed_bits - input_integer_bits)) /
(1LL << input_left_shift);
// Tighten bound using floor. Suppose that we could use the exact value.
// After scaling the difference, the result would be at the maximum. Thus we
// must ensure that our value has lower magnitude.
return static_cast<int>(std::floor(max_input_rescaled));
}

} // namespace execute
} // namespace onert_micro

Expand Down
22 changes: 22 additions & 0 deletions onert-micro/onert-micro/src/execute/kernels/Softmax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include "PALSoftmax.h"

#include "execute/OMUtils.h"

using namespace onert_micro;
using namespace onert_micro::execute;

Expand All @@ -32,6 +34,18 @@ namespace
constexpr uint32_t inputTensorIdx = 0;
constexpr uint32_t outputTensorIdx = 0;

static const int kScaledDiffIntegerBits = 5;
void preprocessSoftmaxScaling(double beta, double input_scale, int input_integer_bits,
int32_t *quantized_multiplier, int *left_shift)
{
const double max_real_multiplier = (1LL << 31) - 1.0;
const double input_beta_real_multiplier =
std::min<double>(beta * input_scale * (1 << (31 - input_integer_bits)), max_real_multiplier);

onert_micro::execute::quantizeMultiplier(input_beta_real_multiplier, quantized_multiplier,
left_shift);
}

} // namespace

// NOTE: doesnt currently support dynamic shapes
Expand Down Expand Up @@ -126,6 +140,14 @@ OMStatus onert_micro::execute::execute_kernel_CircleSoftmax(const OMExecuteArgs
params.output_zp = output->quantization()->zero_point()->operator[](0);
params.input_zp = input->quantization()->zero_point()->operator[](0);

int left_shift = 0;
preprocessSoftmaxScaling(static_cast<double>(params.beta),
static_cast<double>(params.input_scale), kScaledDiffIntegerBits,
&params.input_multiplier, &left_shift);
params.input_left_shift = left_shift;
params.diff_min = -1.0 * onert_micro::execute::calculateInputRadius(
kScaledDiffIntegerBits, params.input_left_shift, 31);

status = pal::Softmax(params, core::utils::castInputData<int8_t>(input_data),
core::utils::castOutputData<int8_t>(output_data));
}
Expand Down