diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/common_func.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/common_func.c index a6e3f265939..7f4e7817a93 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/common_func.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/common_func.c @@ -16,15 +16,19 @@ #include "nnacl/common_func.h" -int offset(const int *shape, const int dim0, const int dim1, const int dim2, const int dim3) { +int Offset(const int *shape, const int dim0, const int dim1, const int dim2, const int dim3) { return ((dim0 * shape[1] + dim1) * shape[2] + dim2) * shape[3] + dim3; } -int offsetComm(const int *shape, const int dim0, const int dim1, const int dim2) { +int OffsetComm(const int *shape, const int dim0, const int dim1, const int dim2) { return ((dim0 * shape[1] + dim1) * shape[2] + dim2) * shape[3]; } -int offset4d(const int *shape, const int *dims) { return offset(shape, dims[0], dims[1], dims[2], dims[3]); } +int Offset4d(const int *shape, const int *dims) { return Offset(shape, dims[0], dims[1], dims[2], dims[3]); } + +int Offset6d(const int *shape, const int *dims) { + return ((OffsetComm(shape, dims[0], dims[1], dims[2]) + dims[3]) * shape[4] + dims[4]) * shape[5]; +} int8_t MinInt8(int8_t a, int8_t b) { return b ^ ((a ^ b) & -(a < b)); } diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/common_func.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/common_func.h index f7ca4f0b2c6..74f418d430a 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/common_func.h +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/common_func.h @@ -36,9 +36,10 @@ void ReluFp32C8(float *data, float *dst, int ele_num); void Relu6Fp32C8(float *data, float *dst, int ele_num); #endif #endif -int offset(const int *shape, const int dim0, const int dim1, const int dim2, const int dim3); -int offsetComm(const int *shape, const int dim0, const int dim1, const int dim2); -int offset4d(const int *shape, const int *dims); +int Offset(const int *shape, const int dim0, const int dim1, const int dim2, const int dim3); +int OffsetComm(const int *shape, const int dim0, const int dim1, const int dim2); +int Offset4d(const int *shape, const int *dims); +int Offset6d(const int *shape, const int *dims); static inline bool isAddOverflow(int32_t x, int32_t y) { int32_t sum = x + y; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp16/pad_fp16.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp16/pad_fp16.c index 0dd833af6bc..e0d69be8409 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp16/pad_fp16.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp16/pad_fp16.c @@ -19,16 +19,22 @@ void PadFp16(const float16_t *input_data, float16_t *output_data, const int *input_shape, const int *output_shape, const int *paddings, const int tid, const int thread_num) { - int in[4], out[4]; + int in[DEFAULT_PAD_NDIMS], out[DEFAULT_PAD_NDIMS]; for (in[0] = 0; in[0] < input_shape[0]; in[0]++) { out[0] = in[0] + paddings[0]; for (in[1] = tid; in[1] < input_shape[1]; in[1] += thread_num) { out[1] = in[1] + paddings[2]; for (in[2] = 0; in[2] < input_shape[2]; in[2]++) { out[2] = in[2] + paddings[4]; - float16_t *dst = output_data + offset(output_shape, out[0], out[1], out[2], paddings[6]); - const float16_t *src = input_data + offset(input_shape, in[0], in[1], in[2], 0); - memcpy(dst, src, input_shape[3] * sizeof(float16_t)); + for (in[3] = 0; in[3] < input_shape[3]; in[3]++) { + out[3] = in[3] + paddings[6]; + for (in[4] = 0; in[4] < input_shape[4]; in[4]++) { + out[4] = in[4] + paddings[8]; + float16_t *dst = output_data + Offset6d(output_shape, out) + paddings[10]; + const float16_t *src = input_data + Offset6d(input_shape, in); + memcpy(dst, src, input_shape[5] * sizeof(float16_t)); + } + } } } } diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/pad_fp32.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/pad_fp32.c index f80bb5657d3..10b42386f06 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/pad_fp32.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/pad_fp32.c @@ -23,16 +23,22 @@ void Pad(const float *input_data, float *output_data, const int *input_shape, co if (thread_num == 0) { return; } - int in[4], out[4]; + int in[DEFAULT_PAD_NDIMS], out[DEFAULT_PAD_NDIMS]; for (in[0] = 0; in[0] < input_shape[0]; in[0]++) { out[0] = in[0] + paddings[0]; for (in[1] = tid; in[1] < input_shape[1]; in[1] += thread_num) { out[1] = in[1] + paddings[2]; for (in[2] = 0; in[2] < input_shape[2]; in[2]++) { out[2] = in[2] + paddings[4]; - float *dst = output_data + offset(output_shape, out[0], out[1], out[2], paddings[6]); - const float *src = input_data + offset(input_shape, in[0], in[1], in[2], 0); - memcpy(dst, src, input_shape[3] * sizeof(float)); + for (in[3] = 0; in[3] < input_shape[3]; in[3]++) { + out[3] = in[3] + paddings[6]; + for (in[4] = 0; in[4] < input_shape[4]; in[4]++) { + out[4] = in[4] + paddings[8]; + float *dst = output_data + Offset6d(output_shape, out) + paddings[10]; + const float *src = input_data + Offset6d(input_shape, in); + memcpy(dst, src, input_shape[5] * sizeof(float)); + } + } } } } @@ -57,8 +63,7 @@ int TransOut2InputDimIndex(int out_dim_index, int left_pad, int in_dim, int offs int GetInputFlattenIndex(int out_flatten_index, const int *input_shape, const PadParameter *pad_param) { int in_flatten_index = 0; - int i; - for (i = 0; i < COMM_SHAPE_SIZE; ++i) { + for (int i = 0; i < DEFAULT_PAD_NDIMS; ++i) { int left_pad = pad_param->paddings_[i * 2]; NNACL_CHECK_ZERO_RETURN_ERR(pad_param->out_strides[i]) int out_dim_index = out_flatten_index / pad_param->out_strides[i]; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/resize_fp32.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/resize_fp32.c index 13f98915e35..89de95ff7f5 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/resize_fp32.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/fp32/resize_fp32.c @@ -510,8 +510,8 @@ int ResizeNearestNeighbor(const float *input_data, float *output_data, const int } else { input_x = (int)(floorf(actual_x)); } - int in_offset = offset(input_shape, batch, input_y, input_x, 0); - int out_offset = offset(output_shape, batch, y, x, 0); + int in_offset = Offset(input_shape, batch, input_y, input_x, 0); + int out_offset = Offset(output_shape, batch, y, x, 0); memcpy(output_data + out_offset, input_data + in_offset, c * sizeof(float)); } } diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/infer/pad_infer.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/infer/pad_infer.c index a436621f49c..f26ece39dc1 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/infer/pad_infer.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/infer/pad_infer.c @@ -32,7 +32,7 @@ int PadInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **ou return NNACL_INFER_INVALID; } - if (input->shape_size_ > 4) { + if (input->shape_size_ > DEFAULT_PAD_NDIMS) { return NNACL_INPUT_TENSOR_ERROR; } const TensorC *paddings = inputs[1]; @@ -48,7 +48,7 @@ int PadInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **ou param->paddings_[i] = ((int *)paddings->data_)[i]; } - int output_shape[MAX_SHAPE_SIZE] = {0}; + int output_shape[DEFAULT_PAD_NDIMS] = {0}; size_t output_shape_size = 0; for (size_t i = 0; i < input->shape_size_; i++) { int shape = input->shape_[i] + param->paddings_[2 * i] + param->paddings_[2 * i + 1]; diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pad_int8.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pad_int8.c index 10f648882a7..1494ac7e58d 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pad_int8.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/pad_int8.c @@ -24,8 +24,8 @@ int PadConstant4D(const int8_t *in_data, int8_t *out_data, const int32_t *in_dim for (int n = 0; n < in_dims[0]; n++) { for (int h = tid; h < in_dims[1]; h += thread_num) { for (int w = 0; w < in_dims[2]; w++) { - const int8_t *in = in_data + offset(in_dims, n, h, w, 0); - int8_t *out = out_data + offset(out_dims, n + paddings[0], h + paddings[2], w + paddings[4], paddings[6]); + const int8_t *in = in_data + Offset(in_dims, n, h, w, 0); + int8_t *out = out_data + Offset(out_dims, n + paddings[0], h + paddings[2], w + paddings[4], paddings[6]); memcpy(out, in, copy_size * sizeof(int8_t)); } } diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/resize_int8.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/resize_int8.c index 31dd3e92b1d..1e7cb91c2a9 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/resize_int8.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/int8/resize_int8.c @@ -173,8 +173,8 @@ int ResizeNearestNeighborInt8Simple(const int8_t *input_data, int8_t *output_dat for (x = 0; x < output_shape[2]; x++) { int input_x = 0; ComputeNearestNeighborInt(x, in_w, new_width, align_corners, &input_x); - int in_offset = offset(input_shape, batch, input_y, input_x, 0); - int out_offset = offset(output_shape, batch, y, x, 0); + int in_offset = Offset(input_shape, batch, input_y, input_x, 0); + int out_offset = Offset(output_shape, batch, y, x, 0); memcpy(output_data + out_offset, input_data + in_offset, c * sizeof(int8_t)); } } @@ -214,8 +214,8 @@ int ResizeNearestNeighborInt8(const int8_t *input_data, int8_t *output_data, con int input_x = 0; ComputeNearestNeighborInt(x, in_w, new_width, align_corners, &input_x); for (c = 0; c < output_shape[3]; c++) { - int in_offset = offset(input_shape, batch, input_y, input_x, c); - int out_offset = offset(output_shape, batch, y, x, c); + int in_offset = Offset(input_shape, batch, input_y, input_x, c); + int out_offset = Offset(output_shape, batch, y, x, c); int32_t out_value = MultiplyByQuantizedMultiplier( input_data[in_offset] - quant_in->zp_, multiplier->multiplier_, diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/pad_parameter.h b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/pad_parameter.h index a6f2a1b5e41..c741599512c 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/pad_parameter.h +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/pad_parameter.h @@ -18,8 +18,8 @@ #include "nnacl/op_base.h" -#define MAX_PAD_SIZE 8 -#define DEFAULT_PAD_NDIMS 4 +#define MAX_PAD_SIZE 12 +#define DEFAULT_PAD_NDIMS 6 typedef struct PadQuantArg { QuantArg *in_quant_args_; @@ -30,13 +30,13 @@ typedef struct PadQuantArg { typedef struct PadParameter { // Primitive parameter OpParameter op_parameter_; - int paddings_[MAX_SHAPE_SIZE]; + int paddings_[MAX_PAD_SIZE]; int pad_mode_; float constant_value_; // shape correlative int padding_length; // other parameter - int in_strides[COMM_SHAPE_SIZE]; + int in_strides[DEFAULT_PAD_NDIMS]; int out_strides[DEFAULT_PAD_NDIMS]; int mirror_offset_; PadQuantArg pad_quant_arg_; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc index 0d09536eac5..0a35595eebb 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc @@ -26,7 +26,8 @@ using mindspore::schema::PrimitiveType_PadFusion; namespace mindspore::kernel { namespace { -constexpr size_t kPadMaxInputSize = 2; +constexpr size_t kPadCommonInputSize = 2; +constexpr size_t kPadMaxInputSize = 3; } // namespace int PadFp16CPUKernel::RunImpl(int task_id) { PadFp16(input_, output_, in_, out_, pad_param_->paddings_, task_id, op_parameter_->thread_num_); @@ -53,8 +54,14 @@ int PadFp16CPUKernel::RunMirrorPadImpl(int task_id) { for (int b = 0; b < block.size_[1]; b++) { int out_b_index = out_a_index + b * block.out_stride_[1]; for (int c = 0; c < block.size_[2]; ++c) { - int output_index = out_b_index + c * block.out_stride_[2]; - MirrorPadFp16(input_data, output_data, in_, pad_param_, output_index, output_index + block.size_[3]); + int out_c_index = out_b_index + c * block.out_stride_[2]; + for (int d = 0; d < block.size_[3]; ++d) { + int out_d_index = out_c_index + d * block.out_stride_[3]; + for (int e = 0; e < block.size_[4]; ++e) { + int output_index = out_d_index + e * block.out_stride_[4]; + MirrorPadFp16(input_data, output_data, in_, pad_param_, output_index, output_index + block.size_[5]); + } + } } } } @@ -88,13 +95,16 @@ int PadFp16CPUKernel::Run() { MS_ASSERT(output_ != nullptr); int ret = 0; if (pad_param_->pad_mode_ == static_cast(schema::PaddingMode_CONSTANT)) { - if (in_tensors_.size() == kPadMaxInputSize) { + if (in_tensors_.size() >= kPadCommonInputSize) { ret = CopyPaddingFromInput(); if (ret != RET_OK) { MS_LOG(ERROR) << "PadFp16CPUKernel CopyPaddingFromInput failed"; return RET_ERROR; } } + if (in_tensors_.size() == kPadMaxInputSize) { + pad_param_->constant_value_ = reinterpret_cast(in_tensors_.at(2)->data_c())[0]; + } if (pad_param_->constant_value_ - 0.0f < 1e-5) { memset(output_, 0, output_tensor->ElementsNum() * sizeof(float16_t)); } else { diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.cc index 8d01d2fe911..749b2b9c8b7 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.cc @@ -28,7 +28,8 @@ using mindspore::schema::PrimitiveType_PadFusion; namespace mindspore::kernel { namespace { constexpr size_t kMirrorPadInputSize = 2; -constexpr size_t kPadMaxInputSize = 2; +constexpr size_t kPadCommonInputSize = 2; +constexpr size_t kPadMaxInputSize = 3; } // namespace int PadCPUKernel::Init() { if (!InferShapeDone()) { @@ -40,30 +41,30 @@ int PadCPUKernel::Init() { int PadCPUKernel::ReSize() { auto input = in_tensors_.at(0); auto rank = input->shape().size(); - if (rank > COMM_SHAPE_SIZE) { - MS_LOG(ERROR) << "Pad input rank should <= " << COMM_SHAPE_SIZE << ", got " << rank; + if (rank > DEFAULT_PAD_NDIMS) { + MS_LOG(ERROR) << "Pad input rank should <= " << DEFAULT_PAD_NDIMS << ", got " << rank; return RET_ERROR; } auto output = out_tensors_.at(0); if (pad_param_->pad_mode_ == static_cast(schema::PaddingMode_CONSTANT)) { - auto ret = ExtendShape(in_, COMM_SHAPE_SIZE, input->shape().data(), rank); + auto ret = ExtendShape(in_, DEFAULT_PAD_NDIMS, input->shape().data(), rank); if (ret != RET_OK) { return ret; } - ret = ExtendShape(out_, COMM_SHAPE_SIZE, output->shape().data(), rank); + ret = ExtendShape(out_, DEFAULT_PAD_NDIMS, output->shape().data(), rank); if (ret != RET_OK) { return ret; } - if (pad_param_->padding_length < MAX_SHAPE_SIZE) { - int ori_paddings[MAX_SHAPE_SIZE]; + if (pad_param_->padding_length < MAX_PAD_SIZE) { + int ori_paddings[MAX_PAD_SIZE]; for (auto i = 0; i < pad_param_->padding_length; ++i) { ori_paddings[i] = pad_param_->paddings_[i]; } - ret = ExtendPaddings(pad_param_->paddings_, MAX_SHAPE_SIZE, ori_paddings, pad_param_->padding_length); + ret = ExtendPaddings(pad_param_->paddings_, MAX_PAD_SIZE, ori_paddings, pad_param_->padding_length); if (ret != RET_OK) { return ret; } - pad_param_->padding_length = MAX_SHAPE_SIZE; + pad_param_->padding_length = MAX_PAD_SIZE; } } return RET_OK; @@ -71,8 +72,8 @@ int PadCPUKernel::ReSize() { void PadCPUKernel::InitMirrorPadBlock() { mirror_pad_block_.clear(); - std::vector left_pads(COMM_SHAPE_SIZE); - for (size_t i = 0; i < COMM_SHAPE_SIZE; ++i) { + std::vector left_pads(DEFAULT_PAD_NDIMS); + for (size_t i = 0; i < DEFAULT_PAD_NDIMS; ++i) { left_pads[i] = pad_param_->paddings_[2 * i]; } @@ -83,7 +84,7 @@ void PadCPUKernel::InitMirrorPadBlock() { /* init separate dims */ int cur_input = 1; int cur_output = 1; - for (size_t i = 0; i < COMM_SHAPE_SIZE; ++i) { + for (size_t i = 0; i < DEFAULT_PAD_NDIMS; ++i) { if (cur_input > 1) { input_separate_dims.emplace_back(cur_input); output_separate_dims.emplace_back(cur_output); @@ -149,7 +150,7 @@ void PadCPUKernel::InitMirrorPadBlock() { } MirrorPadBlock block; - const int size_offset = COMM_SHAPE_SIZE - static_cast(pad_region.size()); + const int size_offset = DEFAULT_PAD_NDIMS - static_cast(pad_region.size()); for (size_t i = 0; i < pad_region.size(); ++i) { int di = size_offset + i; int si = remain_dim_offset + i; @@ -265,8 +266,14 @@ int PadCPUKernel::RunMirrorPadImpl(int task_id) { for (int b = 0; b < block.size_[1]; b++) { int out_b_index = out_a_index + b * block.out_stride_[1]; for (int c = 0; c < block.size_[2]; ++c) { - int output_index = out_b_index + c * block.out_stride_[2]; - MirrorPad(input_data, output_data, in_, pad_param_, output_index, output_index + block.size_[3]); + int out_c_index = out_b_index + c * block.out_stride_[2]; + for (int d = 0; d < block.size_[3]; ++d) { + int out_d_index = out_c_index + d * block.out_stride_[3]; + for (int e = 0; e < block.size_[4]; ++e) { + int output_index = out_d_index + e * block.out_stride_[4]; + MirrorPad(input_data, output_data, in_, pad_param_, output_index, output_index + block.size_[5]); + } + } } } } @@ -310,8 +317,8 @@ int PadCPUKernel::CheckPaddings(int *paddings, int length, int *input_shape, int } int PadCPUKernel::CopyPaddingFromInput() { - if (in_tensors_.size() != kMirrorPadInputSize) { - MS_LOG(ERROR) << "Pad Reflect or Symmetric mode need 2 inputs, got " << in_tensors_.size(); + if (in_tensors_.size() < kMirrorPadInputSize) { + MS_LOG(ERROR) << "Pad Reflect or Symmetric mode need at least 2 inputs, got " << in_tensors_.size(); return RET_ERROR; } auto padding_tensor = in_tensors_.at(1); @@ -327,28 +334,28 @@ int PadCPUKernel::CopyPaddingFromInput() { return RET_ERROR; } - auto ret = ExtendShape(in_, COMM_SHAPE_SIZE, input_shape.data(), rank); + auto ret = ExtendShape(in_, DEFAULT_PAD_NDIMS, input_shape.data(), rank); if (ret != RET_OK) { return ret; } - ret = ExtendPaddings(pad_param_->paddings_, MAX_SHAPE_SIZE, paddings, padding_tensor->ElementsNum()); + ret = ExtendPaddings(pad_param_->paddings_, MAX_PAD_SIZE, paddings, padding_tensor->ElementsNum()); if (ret != RET_OK) { return ret; } - pad_param_->padding_length = MAX_SHAPE_SIZE; + pad_param_->padding_length = MAX_PAD_SIZE; return RET_OK; } void PadCPUKernel::CalculateStrides() { - pad_param_->in_strides[COMM_SHAPE_SIZE - 1] = 1; - for (auto i = COMM_SHAPE_SIZE - 2; i >= 0; --i) { + pad_param_->in_strides[DEFAULT_PAD_NDIMS - 1] = 1; + for (auto i = DEFAULT_PAD_NDIMS - 2; i >= 0; --i) { pad_param_->in_strides[i] = in_[i + 1] * pad_param_->in_strides[i + 1]; } - for (auto i = 0; i < COMM_SHAPE_SIZE; ++i) { + for (auto i = 0; i < DEFAULT_PAD_NDIMS; ++i) { out_[i] = in_[i] + pad_param_->paddings_[i * 2] + pad_param_->paddings_[i * 2 + 1]; } - pad_param_->out_strides[COMM_SHAPE_SIZE - 1] = 1; - for (auto i = COMM_SHAPE_SIZE - 2; i >= 0; --i) { + pad_param_->out_strides[DEFAULT_PAD_NDIMS - 1] = 1; + for (auto i = DEFAULT_PAD_NDIMS - 2; i >= 0; --i) { pad_param_->out_strides[i] = out_[i + 1] * pad_param_->out_strides[i + 1]; } } @@ -358,7 +365,7 @@ int PadCPUKernel::HandleMirrorPad() { if (in_tensors_.size() == 1) { auto input_shape = in_tensors_.at(0)->shape(); int rank = static_cast(input_shape.size()); - ret = ExtendShape(in_, COMM_SHAPE_SIZE, input_shape.data(), rank); + ret = ExtendShape(in_, DEFAULT_PAD_NDIMS, input_shape.data(), rank); if (ret != RET_OK) { return ret; } @@ -368,7 +375,7 @@ int PadCPUKernel::HandleMirrorPad() { return ret; } } - ret = CheckPaddings(pad_param_->paddings_, COMM_SHAPE_SIZE, in_, pad_param_->pad_mode_); + ret = CheckPaddings(pad_param_->paddings_, DEFAULT_PAD_NDIMS, in_, pad_param_->pad_mode_); if (ret != RET_OK) { return ret; } @@ -391,13 +398,16 @@ int PadCPUKernel::Run() { } int error_code = 0; if (pad_param_->pad_mode_ == static_cast(schema::PaddingMode_CONSTANT)) { - if (in_tensors_.size() == kPadMaxInputSize) { + if (in_tensors_.size() >= kPadCommonInputSize) { error_code = CopyPaddingFromInput(); if (error_code != RET_OK) { MS_LOG(ERROR) << "Pad run error, error_code[" << error_code << "]"; return RET_ERROR; } } + if (in_tensors_.size() == kPadMaxInputSize) { + pad_param_->constant_value_ = reinterpret_cast(in_tensors_.at(2)->data_c())[0]; + } auto output = out_tensors_.at(0); int output_size = output->ElementsNum(); auto output_data = reinterpret_cast(output->data_c()); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.h b/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.h index 97ff8ae7802..a02dc80d0c0 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.h +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/pad_fp32.h @@ -55,8 +55,8 @@ class PadCPUKernel : public InnerKernel { int HandleMirrorPad(); int CopyPaddingFromInput(); PadParameter *pad_param_ = nullptr; - int in_[4] = {0}; - int out_[4] = {0}; + int in_[DEFAULT_PAD_NDIMS] = {0}; + int out_[DEFAULT_PAD_NDIMS] = {0}; std::vector mirror_pad_block_; }; diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc index a64f4d0e7a7..897f201d2fd 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc @@ -164,7 +164,7 @@ ops::PrimitiveC *OnnxConvParser::Parse(const onnx::GraphProto &onnx_graph, const prim->set_pad({0, 0, 0, 0}); mindspore::Format format = mindspore::Format::NCHW; mindspore::PadMode pad_mode = mindspore::PadMode::PAD; - int64_t channel_out = 1, channel_in = 1, group = 1; + int64_t channel_out = -1, channel_in = -1, group = 1; std::vector kernels, strides, dilation, pads; for (const auto &onnx_node_attr : onnx_node.attribute()) { diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_conv_transpose_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_conv_transpose_parser.cc index 4412e0d992a..0c06bd5df21 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_conv_transpose_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_conv_transpose_parser.cc @@ -77,24 +77,27 @@ ops::PrimitiveC *OnnxDeConvParser::Parse(const onnx::GraphProto &onnx_graph, con std::find_if(onnx_graph.initializer().begin(), onnx_graph.initializer().end(), [onnx_conv_weight](const onnx::TensorProto &proto) { return proto.name() == onnx_conv_weight; }); if (node_iter == onnx_graph.initializer().end()) { - MS_LOG(ERROR) << "not find node: " << onnx_conv_weight.c_str(); - return nullptr; - } - std::vector weight_shape; - auto size = (*node_iter).dims_size(); - weight_shape.reserve(size); - for (int i = 0; i < size; ++i) { - weight_shape.emplace_back((*node_iter).dims(i)); - } - if (weight_shape.size() != 4) { - MS_LOG(ERROR) << "weight_shape.size() should be 4, but is " << weight_shape.size(); - return nullptr; - } - prim->set_in_channel(weight_shape[0]); - prim->set_out_channel(weight_shape[1] * group); + // in_channel and out_channnel is set to -1 by default. + prim->set_in_channel(-1); + prim->set_out_channel(-1); + MS_LOG(WARNING) << "parsing of channelIn/Out is delayed."; + } else { + std::vector weight_shape; + auto size = (*node_iter).dims_size(); + weight_shape.reserve(size); + for (int i = 0; i < size; ++i) { + weight_shape.emplace_back((*node_iter).dims(i)); + } + if (weight_shape.size() != 4) { + MS_LOG(ERROR) << "weight_shape.size() should be 4, but is " << weight_shape.size(); + return nullptr; + } + prim->set_in_channel(weight_shape[0]); + prim->set_out_channel(weight_shape[1] * group); - if (group != 1 && weight_shape[1] == 1) { - prim->AddAttr(ops::kIsDepthWise, MakeValue(true)); + if (group != 1 && weight_shape[1] == 1) { + prim->AddAttr(ops::kIsDepthWise, MakeValue(true)); + } } return prim.release(); diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_pad_adjust.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_pad_adjust.cc index ea01385c10e..d48cce87626 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_pad_adjust.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_pad_adjust.cc @@ -98,8 +98,8 @@ bool OnnxPadAdjust::Run(const FuncGraphPtr &func_graph) { if (!input_node->isa()) { continue; } - // reshape the padding of pad operator to 2 x 4. - std::vector shape_pre = {2, 4}; + // reshape the padding of pad operator to 2 x i. + std::vector shape_pre = {2, -1}; auto reshape_pre = NewReshapeOpNode(func_graph, input_node, shape_pre); if (reshape_pre == nullptr) { MS_LOG(ERROR) << "create reshape failed."; diff --git a/mindspore/lite/tools/converter/parser/parser_utils.cc b/mindspore/lite/tools/converter/parser/parser_utils.cc index 5e3d9cbb8e1..14f06fbefcf 100644 --- a/mindspore/lite/tools/converter/parser/parser_utils.cc +++ b/mindspore/lite/tools/converter/parser/parser_utils.cc @@ -223,7 +223,9 @@ int TransposeInsertForWeightConst(const FuncGraphPtr &graph, const CNodePtr &con prim->AddAttr("quant_params", std::make_shared(1, 1)); auto transpose_node = graph->NewCNode(prim, {weight_node, perm_node}); transpose_node->set_fullname_with_scope(weight_node->fullname_with_scope() + "_const_post"); - conv_node->set_input(kNumWeightIndex, transpose_node); + auto tr = manager->Transact(); + tr.SetEdge(conv_node, kNumWeightIndex, transpose_node); + tr.Commit(); return lite::RET_OK; }