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] Fix DC for Add, Sub Kernel #13844

Merged
merged 1 commit into from
Aug 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
5 changes: 5 additions & 0 deletions onert-micro/onert-micro/include/execute/OMUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "OMStatus.h"
#include "core/reader/OMCircleReader.h"
#include "core/OMRuntimeShape.h"
#include "core/OMKernelData.h"

namespace onert_micro
{
Expand Down Expand Up @@ -152,6 +153,10 @@ inline void computePaddingHeightWidth(int32_t stride_height, int32_t stride_widt
*padding_w = computePadding(stride_width, dilation_rate_width, in_width, filter_width, out_width);
}

void calculateQuantParams(core::ArithmeticQuantParams &params, const circle::Tensor *input1,
const circle::Tensor *input2, const circle::Tensor *output,
circle::ActivationFunctionType act);

} // namespace execute
} // namespace onert_micro

Expand Down
46 changes: 46 additions & 0 deletions onert-micro/onert-micro/src/execute/OMUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,49 @@ void onert_micro::execute::readQuantParams(const circle::Tensor *tensor, long &z
// read scale
scale = tensor->quantization()->scale()->operator[](0);
}

void onert_micro::execute::calculateQuantParams(core::ArithmeticQuantParams &params,
const circle::Tensor *input1,
const circle::Tensor *input2,
const circle::Tensor *output,
circle::ActivationFunctionType act)
{
long input1_zp;
long input2_zp;
long output_zp;

float input1_scale;
float input2_scale;
float output_scale;

// Read input1 quant params
readQuantParams(input1, input1_zp, input1_scale);
// Read input2 quant params
readQuantParams(input2, input2_zp, input2_scale);
// Read output quant params
readQuantParams(output, output_zp, output_scale);

params.input1_offset = -static_cast<int32_t>(input1_zp);
params.input2_offset = -static_cast<int32_t>(input2_zp);
params.output_offset = static_cast<int32_t>(output_zp);
params.left_shift = (output->type() == circle::TensorType_INT16) ? 15 : 20;
const double twice_max_input_scale =
2 * static_cast<double>(std::max(input1_scale, input2_scale));
const double real_input1_multiplier = static_cast<double>(input1_scale) / twice_max_input_scale;
const double real_input2_multiplier = static_cast<double>(input2_scale) / twice_max_input_scale;
const double real_output_multiplier =
twice_max_input_scale / ((1 << params.left_shift) * static_cast<double>(output_scale));

quantizeMultiplierSmallerThanOneExp(real_input1_multiplier, &params.input1_multiplier,
&params.input1_shift);

quantizeMultiplierSmallerThanOneExp(real_input2_multiplier, &params.input2_multiplier,
&params.input2_shift);

quantizeMultiplierSmallerThanOneExp(real_output_multiplier, &params.output_multiplier,
&params.output_shift);

calculateActivationRangeQuantized(act, output_zp, output_scale, output->type(),
&params.quantized_activation_min,
&params.quantized_activation_max);
}
44 changes: 0 additions & 44 deletions onert-micro/onert-micro/src/execute/kernels/Add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,6 @@ constexpr uint32_t input1TensorIdx = 0;
constexpr uint32_t input2TensorIdx = 1;
constexpr uint32_t outputTensorIdx = 0;

void calculateQuantParams(core::ArithmeticQuantParams &params, const circle::Tensor *input1,
const circle::Tensor *input2, const circle::Tensor *output,
circle::ActivationFunctionType act)
{
long input1_zp;
long input2_zp;
long output_zp;

float input1_scale;
float input2_scale;
float output_scale;

// Read input1 quant params
readQuantParams(input1, input1_zp, input1_scale);
// Read input2 quant params
readQuantParams(input2, input2_zp, input2_scale);
// Read output quant params
readQuantParams(output, output_zp, output_scale);

params.input1_offset = -static_cast<int32_t>(input1_zp);
params.input2_offset = -static_cast<int32_t>(input2_zp);
params.output_offset = static_cast<int32_t>(output_zp);
params.left_shift = (output->type() == circle::TensorType_INT16) ? 15 : 20;
const double twice_max_input_scale =
2 * static_cast<double>(std::max(input1_scale, input2_scale));
const double real_input1_multiplier = static_cast<double>(input1_scale) / twice_max_input_scale;
const double real_input2_multiplier = static_cast<double>(input2_scale) / twice_max_input_scale;
const double real_output_multiplier =
twice_max_input_scale / ((1 << params.left_shift) * static_cast<double>(output_scale));

quantizeMultiplierSmallerThanOneExp(real_input1_multiplier, &params.input1_multiplier,
&params.input1_shift);

quantizeMultiplierSmallerThanOneExp(real_input2_multiplier, &params.input2_multiplier,
&params.input2_shift);

quantizeMultiplierSmallerThanOneExp(real_output_multiplier, &params.output_multiplier,
&params.output_shift);

calculateActivationRangeQuantized(act, output_zp, output_scale, output->type(),
&params.quantized_activation_min,
&params.quantized_activation_max);
}

} // namespace

// NOTE: doesnt currently support dynamic shapes
Expand Down
11 changes: 6 additions & 5 deletions onert-micro/onert-micro/src/execute/kernels/Mul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ constexpr uint32_t input1TensorIdx = 0;
constexpr uint32_t input2TensorIdx = 1;
constexpr uint32_t outputTensorIdx = 0;

void calculateQuantParams(core::ArithmeticQuantParams &params, const circle::Tensor *input1,
const circle::Tensor *input2, const circle::Tensor *output,
circle::ActivationFunctionType act)
// TODO: Remove duplicated code with Sub,Add
void calculateQuantParamsForMul(core::ArithmeticQuantParams &params, const circle::Tensor *input1,
const circle::Tensor *input2, const circle::Tensor *output,
circle::ActivationFunctionType act)
{
long input1_zp;
long input2_zp;
Expand Down Expand Up @@ -194,8 +195,8 @@ OMStatus onert_micro::execute::execute_kernel_CircleMul(const OMExecuteArgs &exe
{
core::ArithmeticQuantParams add_params{};

calculateQuantParams(add_params, input1, input2, output,
options->fused_activation_function());
calculateQuantParamsForMul(add_params, input1, input2, output,
options->fused_activation_function());

if (need_broadcast)
{
Expand Down
44 changes: 0 additions & 44 deletions onert-micro/onert-micro/src/execute/kernels/Sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,6 @@ constexpr uint32_t input1TensorIdx = 0;
constexpr uint32_t input2TensorIdx = 1;
constexpr uint32_t outputTensorIdx = 0;

void calculateQuantParams(core::ArithmeticQuantParams &params, const circle::Tensor *input1,
const circle::Tensor *input2, const circle::Tensor *output,
circle::ActivationFunctionType act)
{
long input1_zp;
long input2_zp;
long output_zp;

float input1_scale;
float input2_scale;
float output_scale;

// Read input1 quant params
readQuantParams(input1, input1_zp, input1_scale);
// Read input2 quant params
readQuantParams(input2, input2_zp, input2_scale);
// Read output quant params
readQuantParams(output, output_zp, output_scale);

params.input1_offset = -static_cast<int32_t>(input1_zp);
params.input2_offset = -static_cast<int32_t>(input2_zp);
params.output_offset = static_cast<int32_t>(output_zp);
params.left_shift = (output->type() == circle::TensorType_INT16) ? 15 : 20;
const double twice_max_input_scale =
2 * static_cast<double>(std::max(input1_scale, input2_scale));
const double real_input1_multiplier = static_cast<double>(input1_scale) / twice_max_input_scale;
const double real_input2_multiplier = static_cast<double>(input2_scale) / twice_max_input_scale;
const double real_output_multiplier =
twice_max_input_scale / ((1 << params.left_shift) * static_cast<double>(output_scale));

quantizeMultiplierSmallerThanOneExp(real_input1_multiplier, &params.input1_multiplier,
&params.input1_shift);

quantizeMultiplierSmallerThanOneExp(real_input2_multiplier, &params.input2_multiplier,
&params.input2_shift);

quantizeMultiplierSmallerThanOneExp(real_output_multiplier, &params.output_multiplier,
&params.output_shift);

calculateActivationRangeQuantized(act, output_zp, output_scale, output->type(),
&params.quantized_activation_min,
&params.quantized_activation_max);
}

} // namespace

// NOTE: doesnt currently support dynamic shapes
Expand Down