From a0e3b6394d51b1efafb4f44903c4a9961d4f1338 Mon Sep 17 00:00:00 2001 From: Chunseok Lee Date: Thu, 29 Aug 2024 14:18:13 +0900 Subject: [PATCH] [onert-micro] Fix DC for Add, Sub Kernel - remove duplicated code of calculateQuantParams ONE-DCO-1.0-Signed-off-by: Chunseok Lee --- .../onert-micro/include/execute/OMUtils.h | 5 ++ .../onert-micro/src/execute/OMUtils.cpp | 46 +++++++++++++++++++ .../onert-micro/src/execute/kernels/Add.cpp | 44 ------------------ .../onert-micro/src/execute/kernels/Mul.cpp | 11 +++-- .../onert-micro/src/execute/kernels/Sub.cpp | 44 ------------------ 5 files changed, 57 insertions(+), 93 deletions(-) diff --git a/onert-micro/onert-micro/include/execute/OMUtils.h b/onert-micro/onert-micro/include/execute/OMUtils.h index abb7a8006f1..177c61b2535 100644 --- a/onert-micro/onert-micro/include/execute/OMUtils.h +++ b/onert-micro/onert-micro/include/execute/OMUtils.h @@ -21,6 +21,7 @@ #include "OMStatus.h" #include "core/reader/OMCircleReader.h" #include "core/OMRuntimeShape.h" +#include "core/OMKernelData.h" namespace onert_micro { @@ -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 ¶ms, const circle::Tensor *input1, + const circle::Tensor *input2, const circle::Tensor *output, + circle::ActivationFunctionType act); + } // namespace execute } // namespace onert_micro diff --git a/onert-micro/onert-micro/src/execute/OMUtils.cpp b/onert-micro/onert-micro/src/execute/OMUtils.cpp index a207083b8c0..23e463707b8 100644 --- a/onert-micro/onert-micro/src/execute/OMUtils.cpp +++ b/onert-micro/onert-micro/src/execute/OMUtils.cpp @@ -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 ¶ms, + 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(input1_zp); + params.input2_offset = -static_cast(input2_zp); + params.output_offset = static_cast(output_zp); + params.left_shift = (output->type() == circle::TensorType_INT16) ? 15 : 20; + const double twice_max_input_scale = + 2 * static_cast(std::max(input1_scale, input2_scale)); + const double real_input1_multiplier = static_cast(input1_scale) / twice_max_input_scale; + const double real_input2_multiplier = static_cast(input2_scale) / twice_max_input_scale; + const double real_output_multiplier = + twice_max_input_scale / ((1 << params.left_shift) * static_cast(output_scale)); + + quantizeMultiplierSmallerThanOneExp(real_input1_multiplier, ¶ms.input1_multiplier, + ¶ms.input1_shift); + + quantizeMultiplierSmallerThanOneExp(real_input2_multiplier, ¶ms.input2_multiplier, + ¶ms.input2_shift); + + quantizeMultiplierSmallerThanOneExp(real_output_multiplier, ¶ms.output_multiplier, + ¶ms.output_shift); + + calculateActivationRangeQuantized(act, output_zp, output_scale, output->type(), + ¶ms.quantized_activation_min, + ¶ms.quantized_activation_max); +} diff --git a/onert-micro/onert-micro/src/execute/kernels/Add.cpp b/onert-micro/onert-micro/src/execute/kernels/Add.cpp index c083eb151ed..b8ee5502afa 100644 --- a/onert-micro/onert-micro/src/execute/kernels/Add.cpp +++ b/onert-micro/onert-micro/src/execute/kernels/Add.cpp @@ -33,50 +33,6 @@ constexpr uint32_t input1TensorIdx = 0; constexpr uint32_t input2TensorIdx = 1; constexpr uint32_t outputTensorIdx = 0; -void calculateQuantParams(core::ArithmeticQuantParams ¶ms, 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(input1_zp); - params.input2_offset = -static_cast(input2_zp); - params.output_offset = static_cast(output_zp); - params.left_shift = (output->type() == circle::TensorType_INT16) ? 15 : 20; - const double twice_max_input_scale = - 2 * static_cast(std::max(input1_scale, input2_scale)); - const double real_input1_multiplier = static_cast(input1_scale) / twice_max_input_scale; - const double real_input2_multiplier = static_cast(input2_scale) / twice_max_input_scale; - const double real_output_multiplier = - twice_max_input_scale / ((1 << params.left_shift) * static_cast(output_scale)); - - quantizeMultiplierSmallerThanOneExp(real_input1_multiplier, ¶ms.input1_multiplier, - ¶ms.input1_shift); - - quantizeMultiplierSmallerThanOneExp(real_input2_multiplier, ¶ms.input2_multiplier, - ¶ms.input2_shift); - - quantizeMultiplierSmallerThanOneExp(real_output_multiplier, ¶ms.output_multiplier, - ¶ms.output_shift); - - calculateActivationRangeQuantized(act, output_zp, output_scale, output->type(), - ¶ms.quantized_activation_min, - ¶ms.quantized_activation_max); -} - } // namespace // NOTE: doesnt currently support dynamic shapes diff --git a/onert-micro/onert-micro/src/execute/kernels/Mul.cpp b/onert-micro/onert-micro/src/execute/kernels/Mul.cpp index 2f7fbec1d53..089c7b5ee1e 100644 --- a/onert-micro/onert-micro/src/execute/kernels/Mul.cpp +++ b/onert-micro/onert-micro/src/execute/kernels/Mul.cpp @@ -34,9 +34,10 @@ constexpr uint32_t input1TensorIdx = 0; constexpr uint32_t input2TensorIdx = 1; constexpr uint32_t outputTensorIdx = 0; -void calculateQuantParams(core::ArithmeticQuantParams ¶ms, 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 ¶ms, const circle::Tensor *input1, + const circle::Tensor *input2, const circle::Tensor *output, + circle::ActivationFunctionType act) { long input1_zp; long input2_zp; @@ -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) { diff --git a/onert-micro/onert-micro/src/execute/kernels/Sub.cpp b/onert-micro/onert-micro/src/execute/kernels/Sub.cpp index 788eb6813e0..10cfc5a43ce 100644 --- a/onert-micro/onert-micro/src/execute/kernels/Sub.cpp +++ b/onert-micro/onert-micro/src/execute/kernels/Sub.cpp @@ -38,50 +38,6 @@ constexpr uint32_t input1TensorIdx = 0; constexpr uint32_t input2TensorIdx = 1; constexpr uint32_t outputTensorIdx = 0; -void calculateQuantParams(core::ArithmeticQuantParams ¶ms, 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(input1_zp); - params.input2_offset = -static_cast(input2_zp); - params.output_offset = static_cast(output_zp); - params.left_shift = (output->type() == circle::TensorType_INT16) ? 15 : 20; - const double twice_max_input_scale = - 2 * static_cast(std::max(input1_scale, input2_scale)); - const double real_input1_multiplier = static_cast(input1_scale) / twice_max_input_scale; - const double real_input2_multiplier = static_cast(input2_scale) / twice_max_input_scale; - const double real_output_multiplier = - twice_max_input_scale / ((1 << params.left_shift) * static_cast(output_scale)); - - quantizeMultiplierSmallerThanOneExp(real_input1_multiplier, ¶ms.input1_multiplier, - ¶ms.input1_shift); - - quantizeMultiplierSmallerThanOneExp(real_input2_multiplier, ¶ms.input2_multiplier, - ¶ms.input2_shift); - - quantizeMultiplierSmallerThanOneExp(real_output_multiplier, ¶ms.output_multiplier, - ¶ms.output_shift); - - calculateActivationRangeQuantized(act, output_zp, output_scale, output->type(), - ¶ms.quantized_activation_min, - ¶ms.quantized_activation_max); -} - } // namespace // NOTE: doesnt currently support dynamic shapes