From 7d75ac047f5b6ff3e3d57de3829a9e4e5984ef70 Mon Sep 17 00:00:00 2001 From: wang_shaocong Date: Tue, 20 Oct 2020 15:48:46 +0800 Subject: [PATCH] Fix bug of several operators. --- mindspore/lite/nnacl/int8/scale_int8.c | 22 ++++++------- mindspore/lite/nnacl/int8/scale_int8.h | 4 +-- .../src/runtime/kernel/arm/fp32/arithmetic.cc | 13 ++++++-- .../src/runtime/kernel/arm/int8/scale_int8.cc | 31 +++++++++++++++++-- mindspore/lite/test/models_onnx.cfg | 1 + .../converter/parser/onnx/onnx_pool_parser.cc | 7 ++++- .../converter/parser/onnx/onnx_relu_parser.cc | 9 +++++- .../parser/onnx/onnx_upsample_parser.cc | 3 +- 8 files changed, 69 insertions(+), 21 deletions(-) diff --git a/mindspore/lite/nnacl/int8/scale_int8.c b/mindspore/lite/nnacl/int8/scale_int8.c index b70e34cc6e2..bfe1dbc3f8c 100644 --- a/mindspore/lite/nnacl/int8/scale_int8.c +++ b/mindspore/lite/nnacl/int8/scale_int8.c @@ -18,7 +18,7 @@ #include "nnacl/quantization/fixed_point.h" void ScaleInnerInt8(const int8_t *in_data, int8_t *out_data, const int8_t *scale, int outer_start, int outer_end, - int axis_size, int inner_size, const ScaleParameter *scale_param) { + int axis_size, int inner_size, const ScaleParameter *scale_param, int max, int min) { for (int out = outer_start; out < outer_end; out++) { int out_offset = out * axis_size * inner_size; for (int i = 0; i < axis_size; i++) { @@ -34,8 +34,8 @@ void ScaleInnerInt8(const int8_t *in_data, int8_t *out_data, const int8_t *scale scale_param->scale_mul_arg_.multiplier_), scale_param->scale_mul_arg_.right_shift_); int tmp = input_mul_scale + scale_param->output_zp_; - tmp = tmp > INT8_MAX ? INT8_MAX : tmp; - tmp = tmp < INT8_MIN ? INT8_MIN : tmp; + tmp = tmp > max ? max : tmp; + tmp = tmp < min ? min : tmp; out_data[in_offset] = tmp; } } @@ -44,7 +44,7 @@ void ScaleInnerInt8(const int8_t *in_data, int8_t *out_data, const int8_t *scale void ScaleInnerWithBiasInt8(const int8_t *in_data, int8_t *out_data, const int8_t *scale, const int8_t *offset, int outer_start, int outer_end, int axis_size, int inner_size, - const ScaleParameter *scale_param) { + const ScaleParameter *scale_param, int max, int min) { for (int out = outer_start; out < outer_end; out++) { int out_offset = out * axis_size * inner_size; for (int i = 0; i < axis_size; i++) { @@ -63,10 +63,10 @@ void ScaleInnerWithBiasInt8(const int8_t *in_data, int8_t *out_data, const int8_ int bias = RoundingDivideByPOT( SaturatingRoundingDoublingHighMul(tmp_bias * (1 << (unsigned int)scale_param->offset_mul_arg_.left_shift_), scale_param->offset_mul_arg_.multiplier_), - scale_param->scale_mul_arg_.right_shift_); + scale_param->offset_mul_arg_.right_shift_); int tmp = input_mul_scale + bias + scale_param->output_zp_; - tmp = tmp > INT8_MAX ? INT8_MAX : tmp; - tmp = tmp < INT8_MIN ? INT8_MIN : tmp; + tmp = tmp > max ? max : tmp; + tmp = tmp < min ? min : tmp; out_data[in_offset] = tmp; } } @@ -74,21 +74,21 @@ void ScaleInnerWithBiasInt8(const int8_t *in_data, int8_t *out_data, const int8_ } void DoScaleInt8(const int8_t *in_data, int8_t *out_data, const int8_t *scale, int task_id, - const ScaleParameter *scale_param) { + const ScaleParameter *scale_param, int max, int min) { int outer_step = UP_DIV(scale_param->outer_size_, scale_param->op_parameter_.thread_num_); int outer_start = task_id * outer_step; int outer_end = MSMIN(outer_start + outer_step, scale_param->outer_size_); ScaleInnerInt8(in_data, out_data, scale, outer_start, outer_end, scale_param->axis_size_, scale_param->inner_size_, - scale_param); + scale_param, max, min); } void DoScaleWithBiasInt8(const int8_t *in_data, int8_t *out_data, const int8_t *scale, const int8_t *offset, - int task_id, const ScaleParameter *scale_param) { + int task_id, const ScaleParameter *scale_param, int max, int min) { int outer_step = UP_DIV(scale_param->outer_size_, scale_param->op_parameter_.thread_num_); int outer_start = task_id * outer_step; int outer_end = MSMIN(outer_start + outer_step, scale_param->outer_size_); ScaleInnerWithBiasInt8(in_data, out_data, scale, offset, outer_start, outer_end, scale_param->axis_size_, - scale_param->inner_size_, scale_param); + scale_param->inner_size_, scale_param, max, min); } diff --git a/mindspore/lite/nnacl/int8/scale_int8.h b/mindspore/lite/nnacl/int8/scale_int8.h index a9225a23708..7a4fcb855b2 100644 --- a/mindspore/lite/nnacl/int8/scale_int8.h +++ b/mindspore/lite/nnacl/int8/scale_int8.h @@ -23,9 +23,9 @@ extern "C" { #endif void DoScaleInt8(const int8_t *in_data, int8_t *out_data, const int8_t *scale, int task_id, - const ScaleParameter *scale_param); + const ScaleParameter *scale_param, int max, int min); void DoScaleWithBiasInt8(const int8_t *in_data, int8_t *out_data, const int8_t *scale, const int8_t *offset, - int task_id, const ScaleParameter *scale_param); + int task_id, const ScaleParameter *scale_param, int max, int min); #ifdef __cplusplus } #endif diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic.cc index 042b2e39e36..f3c007746b5 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic.cc @@ -15,12 +15,13 @@ */ #include "src/runtime/kernel/arm/fp32/arithmetic.h" -#include "src/runtime/kernel/arm/int8/add_int8.h" -#include "src/runtime/kernel/arm/int8/mul_int8.h" + +#include "include/errorcode.h" #include "schema/model_generated.h" #include "src/kernel_registry.h" +#include "src/runtime/kernel/arm/int8/add_int8.h" +#include "src/runtime/kernel/arm/int8/mul_int8.h" #include "src/runtime/runtime_api.h" -#include "include/errorcode.h" using mindspore::kernel::KERNEL_ARCH::kCPU; using mindspore::lite::KernelRegistrar; @@ -48,6 +49,12 @@ int ArithmeticCPUKernel::ReSize() { arithmeticParameter_->in_elements_num0_ = in_tensors_[0]->ElementsNum(); arithmeticParameter_->in_elements_num1_ = in_tensors_[1]->ElementsNum(); arithmeticParameter_->out_elements_num_ = out_tensors_[0]->ElementsNum(); + memcpy(arithmeticParameter_->in_shape0_, static_cast(in_tensors_[0]->shape().data()), + in_tensors_[0]->shape().size() * sizeof(int)); + memcpy(arithmeticParameter_->in_shape1_, static_cast(in_tensors_[1]->shape().data()), + in_tensors_[1]->shape().size() * sizeof(int)); + memcpy(arithmeticParameter_->out_shape_, static_cast(out_tensors_[0]->shape().data()), + out_tensors_[0]->shape().size() * sizeof(int)); if (arithmeticParameter_->in_elements_num0_ == 1 || arithmeticParameter_->in_elements_num1_ == 1) { switch (arithmeticParameter_->op_parameter_.type_) { diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/scale_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/scale_int8.cc index 11add9dad04..c5980d68f97 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/scale_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/scale_int8.cc @@ -15,6 +15,7 @@ */ #include "src/runtime/kernel/arm/int8/scale_int8.h" + #include #include #include "nnacl/int8/scale_int8.h" @@ -195,9 +196,35 @@ int ScaleInt8CPUKernel::ReSize() { int ScaleInt8CPUKernel::Scale(int task_id) { if (has_bias_) { - DoScaleWithBiasInt8(input_ptr_, output_ptr_, scale_, offset_, task_id, scale_param_); + switch (scale_param_->activation_type_) { + case schema::ActivationType_RELU: + DoScaleWithBiasInt8(input_ptr_, output_ptr_, scale_, offset_, task_id, scale_param_, INT8_MAX, 0); + break; + case schema::ActivationType_RELU6: + DoScaleWithBiasInt8(input_ptr_, output_ptr_, scale_, offset_, task_id, scale_param_, 6, 0); + break; + case schema::ActivationType_NO_ACTIVATION: + DoScaleWithBiasInt8(input_ptr_, output_ptr_, scale_, offset_, task_id, scale_param_, INT8_MAX, INT8_MIN); + break; + default: + MS_LOG(ERROR) << "Scale does not support activation type " << scale_param_->activation_type_; + return RET_ERROR; + } } else { - DoScaleInt8(input_ptr_, output_ptr_, scale_, task_id, scale_param_); + switch (scale_param_->activation_type_) { + case schema::ActivationType_RELU: + DoScaleInt8(input_ptr_, output_ptr_, scale_, task_id, scale_param_, INT8_MAX, 0); + break; + case schema::ActivationType_RELU6: + DoScaleInt8(input_ptr_, output_ptr_, scale_, task_id, scale_param_, 6, 0); + break; + case schema::ActivationType_NO_ACTIVATION: + DoScaleInt8(input_ptr_, output_ptr_, scale_, task_id, scale_param_, INT8_MAX, INT8_MIN); + break; + default: + MS_LOG(ERROR) << "Scale does not support activation type " << scale_param_->activation_type_; + return RET_ERROR; + } } return RET_OK; diff --git a/mindspore/lite/test/models_onnx.cfg b/mindspore/lite/test/models_onnx.cfg index 6f44c13931b..b3796d968e9 100644 --- a/mindspore/lite/test/models_onnx.cfg +++ b/mindspore/lite/test/models_onnx.cfg @@ -5,3 +5,4 @@ ml_face_3d.onnx gts_version-RFB-320_simplified.onnx mnist-8.onnx crnn_lite_lstm_v2.onnx:32,32,32,1 +psenet_lite_mbv2.onnx:1,32,32,3 diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_pool_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_pool_parser.cc index 105f6ca3740..ec47d14d8e6 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_pool_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_pool_parser.cc @@ -15,6 +15,7 @@ */ #include "tools/converter/parser/onnx/onnx_pool_parser.h" + #include namespace mindspore { @@ -77,7 +78,11 @@ STATUS OnnxPoolParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::Nod } } if (attribute_name == "auto_pad") { - MS_ASSERT(false); + if (onnx_node_attr.s() == "SAME_UPPER") { + attr->padMode = schema::PadMode_SAME_UPPER; + } else if (onnx_node_attr.s() == "SAME_LOWER") { + attr->padMode = schema::PadMode_SAME_LOWER; + } } if (attribute_name == "pads") { if (onnx_node_attr.ints_size() == 4) { diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_relu_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_relu_parser.cc index 3fff5ff08a8..fb0f449b483 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_relu_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_relu_parser.cc @@ -15,8 +15,9 @@ */ #include "tools/converter/parser/onnx/onnx_relu_parser.h" -#include + #include +#include #include "securec/include/securec.h" namespace mindspore { @@ -47,6 +48,12 @@ STATUS OnnxReluParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::Nod MS_LOG(DEBUG) << "onnx LeakyReluParser"; attr->type = schema::ActivationType_LEAKY_RELU; } + for (const auto &onnx_node_attr : onnx_node.attribute()) { + const auto &attribute_name = onnx_node_attr.name(); + if (attribute_name == "alpha") { + attr->alpha = onnx_node_attr.f(); + } + } op->primitive->value.type = schema::PrimitiveType_Activation; op->primitive->value.value = attr.release(); diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_upsample_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_upsample_parser.cc index 562f6963c4b..48ab1855c3c 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_upsample_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_upsample_parser.cc @@ -15,6 +15,7 @@ */ #include "tools/converter/parser/onnx/onnx_upsample_parser.h" + #include namespace mindspore { @@ -54,7 +55,7 @@ STATUS OnnxUpsampleParser::Parse(const onnx::GraphProto &onnx_graph, const onnx: attr->newWidth = 1; attr->newHeight = 1; attr->alignCorners = false; - op->primitive->value.type = schema::PrimitiveType_Upsample; + op->primitive->value.type = schema::PrimitiveType_Resize; op->primitive->value.value = attr.release(); return RET_OK; }