Skip to content

Commit

Permalink
[onert-micro] proprocessing phase for cmsisnn Softmax (#14116)
Browse files Browse the repository at this point in the history
- Add missing part for cmsisnn softmax

ONE-DCO-1.0-Signed-off-by: Chunseok Lee <[email protected]>
  • Loading branch information
chunseoklee authored Sep 30, 2024
1 parent c74c988 commit 8893023
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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

0 comments on commit 8893023

Please sign in to comment.