diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/local_response_norm_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/local_response_norm_fp32.cc index 31341b0f273..24d7677e145 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/local_response_norm_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/local_response_norm_fp32.cc @@ -53,10 +53,10 @@ int LocalResponseNormCPUKernel::DoLocalResponseNorm(int task_id) const { auto in_shape = input_tensor->shape(); MS_CHECK_TRUE_RET(in_shape.size() == C4NUM, RET_ERROR); - int batch = in_shape.at(0); - int height = in_shape.at(1); - int width = in_shape.at(2); - int channel = in_shape.at(3); + int batch = in_shape.at(kNHWC_N); + int height = in_shape.at(kNHWC_H); + int width = in_shape.at(kNHWC_W); + int channel = in_shape.at(kNHWC_C); int outer_size = batch * width * height; MS_CHECK_TRUE_RET(thread_count_ != 0, RET_ERROR); diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/lstm_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/lstm_fp32.cc index 373d28b50d5..2eb45040a80 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/lstm_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/lstm_fp32.cc @@ -31,6 +31,11 @@ using mindspore::lite::RET_OK; using mindspore::schema::PrimitiveType_LSTM; namespace mindspore::kernel { +namespace { +constexpr int kOutputHiddenStatusIndex = 1; +constexpr int kOutputCellStatusIndex = 2; +} // namespace + int LstmInputMulWeightRun(void *cdata, int task_id, float, float) { auto kernel = reinterpret_cast(cdata); CHECK_NULL_RETURN(kernel); @@ -93,7 +98,7 @@ int LstmCPUKernel::InitInputWeightBias() { int offset = weight_batch_ * (cw_size + hh_size); float *bias_data = (has_bias) ? weight_i_data + offset : nullptr; - int dir_mul = lstm_param_->bidirectional_ ? 2 : 1; + int dir_mul = lstm_param_->bidirectional_ ? C2NUM : C1NUM; int b_stride = (gpu_orig_state_) ? gate_num * (dir_mul * b_size) : gate_num * (b_size); if (in_tensors_.size() > mindir_input_tensors) { bias_data = reinterpret_cast(in_tensors_.at(onnx_bias_index)->data()); @@ -209,9 +214,9 @@ int LstmCPUKernel::InitParam() { } else { lstm_param_->hidden_size_ = w_shape.at(SECOND_INPUT) / gate_num; } - lstm_param_->output_step_ = lstm_param_->bidirectional_ ? 2 * lstm_param_->batch_ * lstm_param_->hidden_size_ + lstm_param_->output_step_ = lstm_param_->bidirectional_ ? C2NUM * lstm_param_->batch_ * lstm_param_->hidden_size_ : lstm_param_->batch_ * lstm_param_->hidden_size_; - weight_batch_ = lstm_param_->bidirectional_ ? 2 * gate_num : gate_num; + weight_batch_ = lstm_param_->bidirectional_ ? C2NUM * gate_num : gate_num; state_is_vec_ = lstm_param_->batch_ == 1; // determine FB origin gpu_orig_state_ = false; @@ -428,8 +433,8 @@ void LstmCPUKernel::LstmUnidirectional(float *output, const float *weight_h, con float *buffer[], bool is_backward) { float *gate = buffer[input_gate_index]; float *input_gate = gate; - float *forget_gate = gate + lstm_param_->seq_len_ * lstm_param_->batch_ * lstm_param_->hidden_size_ * 2; - float *cell_gate = gate + lstm_param_->seq_len_ * lstm_param_->batch_ * lstm_param_->hidden_size_ * 3; + float *forget_gate = gate + lstm_param_->seq_len_ * lstm_param_->batch_ * lstm_param_->hidden_size_ * C2NUM; + float *cell_gate = gate + lstm_param_->seq_len_ * lstm_param_->batch_ * lstm_param_->hidden_size_ * C3NUM; float *output_gate = gate + lstm_param_->seq_len_ * lstm_param_->batch_ * lstm_param_->hidden_size_; float *tmp = buffer[tmp_hidden_output_index]; int dir_mult = lstm_param_->bidirectional_ ? C2NUM : C1NUM; @@ -568,10 +573,10 @@ int LstmCPUKernel::Run() { auto cell_state = in_tensors_.at(cell_state_input_index_); CHECK_NULL_RETURN(cell_state->data()); - auto output_hidden_state = out_tensors_[1]; + auto output_hidden_state = out_tensors_[kOutputHiddenStatusIndex]; CHECK_NULL_RETURN(output_hidden_state->data()); (void)memcpy(output_hidden_state->data(), hidden_state->data(), hidden_state->ElementsNum() * sizeof(float)); - auto output_cell_state = out_tensors_[2]; + auto output_cell_state = out_tensors_[kOutputCellStatusIndex]; CHECK_NULL_RETURN(output_cell_state->data()); (void)memcpy(output_cell_state->data(), cell_state->data(), cell_state->ElementsNum() * sizeof(float)); diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/nonzero_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/nonzero_fp32.cc index a512877ee3b..e8898c6f6a8 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/nonzero_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/nonzero_fp32.cc @@ -47,7 +47,7 @@ int NonZeroCPUKernel::Run() { CHECK_NULL_RETURN(input_data); CHECK_NULL_RETURN(output_data); auto input_dim_size = in_tensor->shape().size(); - if (out_tensor->shape().size() != 2) { + if (out_tensor->shape().size() != C2NUM) { MS_LOG(ERROR) << "out tensor shape size must be equal to 2!"; return RET_ERROR; } diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/pad_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/pad_fp32.cc index 3807d5fccd4..dde6006d437 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/pad_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/pad_fp32.cc @@ -97,7 +97,7 @@ void PadCPUKernel::InitMirrorPadBlock() { mirror_pad_block_.clear(); 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]; + left_pads[i] = pad_param_->paddings_[C2NUM * i]; } std::vector input_separate_dims; std::vector output_separate_dims; @@ -160,17 +160,17 @@ void PadCPUKernel::InitMirrorPadBlock() { continue; } switch (pad_cord[i]) { - case 0: + case C0NUM: dst_offset += separate_offset[si] * output_separate_stride[si]; block.size_[di] = input_separate_dims[si]; block.out_stride_[di] = output_separate_stride[si]; break; - case 2: + case C2NUM: dst_offset += (separate_offset[si] + input_separate_dims[si]) * output_separate_stride[si]; block.size_[di] = right_pads[si]; block.out_stride_[di] = output_separate_stride[si]; break; - case 1: + case C1NUM: if (separate_offset[si] > 0) { block.size_[di] = separate_offset[si]; block.out_stride_[di] = output_separate_stride[si]; @@ -312,12 +312,12 @@ int PadCPUKernel::CheckPaddings(const int *paddings, int length, const int *inpu } for (auto i = 0; i < length; ++i) { int max_valid = input_shape[i] - offset; - if (paddings[i * 2] > max_valid) { - MS_LOG(WARNING) << prefix << "paddings " << paddings[i * 2] << " should be less than " << max_valid + 1; + if (paddings[i * C2NUM] > max_valid) { + MS_LOG(WARNING) << prefix << "paddings " << paddings[i * C2NUM] << " should be less than " << max_valid + 1; MS_LOG(WARNING) << "Running mirror pad with padding bigger than shape."; } - if (paddings[i * 2 + 1] > max_valid) { - MS_LOG(WARNING) << prefix << "paddings " << paddings[i * 2 + 1] << " should be less than " << max_valid + 1; + if (paddings[i * C2NUM + 1] > max_valid) { + MS_LOG(WARNING) << prefix << "paddings " << paddings[i * C2NUM + 1] << " should be less than " << max_valid + 1; MS_LOG(WARNING) << "Running mirror pad with padding bigger than shape."; } } @@ -334,8 +334,8 @@ int PadCPUKernel::CopyPaddingFromInput() { CHECK_NULL_RETURN(paddings); auto input_shape = in_tensors_.at(0)->shape(); int rank = static_cast(input_shape.size()); - if (padding_tensor->ElementsNum() != rank * 2) { - MS_LOG(ERROR) << "Pad second input elements num" << padding_tensor->ElementsNum() << ", should be " << rank * 2; + if (padding_tensor->ElementsNum() != rank * C2NUM) { + MS_LOG(ERROR) << "Pad second input elements num" << padding_tensor->ElementsNum() << ", should be " << rank * C2NUM; return RET_ERROR; } @@ -353,14 +353,14 @@ int PadCPUKernel::CopyPaddingFromInput() { void PadCPUKernel::CalculateStrides() { pad_param_->in_strides[DEFAULT_PAD_NDIMS - 1] = 1; - for (auto i = DEFAULT_PAD_NDIMS - 2; i >= 0; --i) { + for (auto i = DEFAULT_PAD_NDIMS - C2NUM; i >= 0; --i) { pad_param_->in_strides[i] = in_[i + 1] * pad_param_->in_strides[i + 1]; } for (auto i = 0; i < DEFAULT_PAD_NDIMS; ++i) { - out_[i] = in_[i] + pad_param_->paddings_[i * 2] + pad_param_->paddings_[i * 2 + 1]; + out_[i] = in_[i] + pad_param_->paddings_[i * C2NUM] + pad_param_->paddings_[i * C2NUM + 1]; } pad_param_->out_strides[DEFAULT_PAD_NDIMS - 1] = 1; - for (auto i = DEFAULT_PAD_NDIMS - 2; i >= 0; --i) { + for (auto i = DEFAULT_PAD_NDIMS - C2NUM; i >= 0; --i) { pad_param_->out_strides[i] = out_[i + 1] * pad_param_->out_strides[i + 1]; } } @@ -393,7 +393,7 @@ int PadCPUKernel::HandleMirrorPad() { int PadCPUKernel::Run() { if (in_tensors_.size() == kInputSize2) { - auto pad_value = in_tensors_.at(2); + auto pad_value = in_tensors_.at(kPadCommonInputSize); auto value_num = pad_value->ElementsNum(); if (value_num != 1) { MS_LOG(ERROR) << "The number of padding value should be only one, but got " << value_num; diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/relative_position_attention_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/relative_position_attention_fp32.cc index 20045199361..545d972f3e1 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/relative_position_attention_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/relative_position_attention_fp32.cc @@ -30,6 +30,7 @@ namespace mindspore::kernel { RelativePositionAttentionCPUKernel::~RelativePositionAttentionCPUKernel() { FreeAllPackData(); } namespace { +constexpr int kWeightQTensorIndex = 4; constexpr int kActivationTensorShapeSize = 3; constexpr int kActivationTensorBatch = 1; constexpr int kTensorShapeBatchIndex = 0; @@ -58,18 +59,18 @@ int RelativePositionAttentionCPUKernel::CheckInputs() { MS_LOG(ERROR) << "input_k is abnormal."; return RET_ERROR; } - input_v_tensor_ = this->in_tensors_.at(2); + input_v_tensor_ = this->in_tensors_.at(k3DimsLeftMatrixDeepIndex); if (!AttentionActivationTensorCheck(input_v_tensor_)) { MS_LOG(ERROR) << "input_v is abnormal."; return RET_ERROR; } - input_p_tensor_ = this->in_tensors_.at(3); + input_p_tensor_ = this->in_tensors_.at(kActivationTensorShapeSize); if (!AttentionActivationTensorCheck(input_p_tensor_)) { MS_LOG(ERROR) << "input_p is abnormal."; return RET_ERROR; } // Sequence length Q / 2 should be equal to sequence length of K - if (input_p_tensor_->shape().at(1) / 2 != input_k_tensor_->shape().at(1)) { + if (input_p_tensor_->shape().at(1) / C2NUM != input_k_tensor_->shape().at(1)) { MS_LOG(ERROR) << "Sequence length of input_p / 2 != sequence length of input_k"; return RET_ERROR; } @@ -109,37 +110,37 @@ bool AttentionWeightTensorCheck(const lite::Tensor *tensor) { } // namespace int RelativePositionAttentionCPUKernel::CheckWeights() { - weight_q_tensor_ = this->in_tensors_.at(4); + weight_q_tensor_ = this->in_tensors_.at(kWeightQTensorIndex); if (!AttentionWeightTensorCheck(weight_q_tensor_)) { MS_LOG(ERROR) << "weight_q is abnormal."; return RET_ERROR; } - weight_k_tensor_ = this->in_tensors_.at(5); + weight_k_tensor_ = this->in_tensors_.at(5); // inputs: 5:WK if (!AttentionWeightTensorCheck(weight_k_tensor_)) { MS_LOG(ERROR) << "weight_k is abnormal."; return RET_ERROR; } - weight_v_tensor_ = this->in_tensors_.at(6); + weight_v_tensor_ = this->in_tensors_.at(6); // inputs: 6:WV if (!AttentionWeightTensorCheck(weight_v_tensor_)) { MS_LOG(ERROR) << "weight_v is abnormal."; return RET_ERROR; } - weight_p_tensor_ = this->in_tensors_.at(7); + weight_p_tensor_ = this->in_tensors_.at(7); // inputs: 7:WP if (!AttentionWeightTensorCheck(weight_p_tensor_)) { MS_LOG(ERROR) << "weight_p is abnormal."; return RET_ERROR; } - pos_u_tensor_ = this->in_tensors_.at(8); + pos_u_tensor_ = this->in_tensors_.at(8); // inputs: 8:PU if (!AttentionWeightTensorCheck(pos_u_tensor_)) { MS_LOG(ERROR) << "pos_u is abnormal."; return RET_ERROR; } - pos_v_tensor_ = this->in_tensors_.at(9); + pos_v_tensor_ = this->in_tensors_.at(9); // inputs: 9:PV if (!AttentionWeightTensorCheck(pos_v_tensor_)) { MS_LOG(ERROR) << "pos_v is abnormal."; return RET_ERROR; } - weight_o_tensor_ = this->in_tensors_.at(10); + weight_o_tensor_ = this->in_tensors_.at(10); // inputs: 10:WO if (!AttentionWeightTensorCheck(weight_o_tensor_)) { MS_LOG(ERROR) << "weight_o is abnormal."; return RET_ERROR; @@ -168,10 +169,10 @@ int RelativePositionAttentionCPUKernel::CheckBiases() { if (!param_->use_bias_) { return RET_OK; } - bias_q_tensor_ = this->in_tensors_.at(11); - bias_k_tensor_ = this->in_tensors_.at(12); - bias_v_tensor_ = this->in_tensors_.at(13); - bias_o_tensor_ = this->in_tensors_.at(14); + bias_q_tensor_ = this->in_tensors_.at(11); // inputs : 11:BQ + bias_k_tensor_ = this->in_tensors_.at(12); // inputs : 12:BK + bias_v_tensor_ = this->in_tensors_.at(13); // inputs : 13:BV + bias_o_tensor_ = this->in_tensors_.at(14); // inputs : 14:BO if (!AttentionBiasTensorCheck(bias_q_tensor_)) { MS_LOG(ERROR) << "bias_q is abnormal."; return RET_ERROR; @@ -536,7 +537,7 @@ int RelativePositionAttentionCPUKernel::PackRunBuffersLogits(int batch, int num_ return RET_ERROR; } // relative shift output shape is [batch * num_heads, q_seq, p_seq / 2] - (void)InitMatrix(&logits_with_v_shifted_mat_, batch * num_heads, param_->q_seq_, param_->p_seq_ / 2, false); + (void)InitMatrix(&logits_with_v_shifted_mat_, batch * num_heads, param_->q_seq_, param_->p_seq_ / C2NUM, false); ret = MallocLeftTensor(&logits_with_v_shifted_mat_, param_->row_tile_, ms_context_->allocator, false); if (ret != RET_OK) { MS_LOG(ERROR) << "Malloc logits_with_v_shifted buffer failed"; diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/resize_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/resize_fp32.cc index c413ff0fef9..5cbc11b9f5f 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/resize_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/resize_fp32.cc @@ -59,8 +59,8 @@ int ResizeCPUKernel::ReSize() { } if (!const_shape_) { - new_height_ = out_tensors_.at(0)->shape()[1]; - new_width_ = out_tensors_.at(0)->shape()[2]; + new_height_ = out_tensors_.at(0)->shape()[kNHWC_H]; + new_width_ = out_tensors_.at(0)->shape()[kNHWC_W]; } auto ret = MallocTmpBuffer(); @@ -95,10 +95,10 @@ void ResizeCPUKernel::CalTmpBufferLen(int *x_len, int *y_len, int *x_weight_len, *y_weight_len = new_height_; } if (method_ == static_cast(schema::ResizeMethod_CUBIC)) { - *x_len = new_width_ * 4; - *y_len = new_height_ * 4; - *x_weight_len = new_width_ * 4; - *y_weight_len = new_height_ * 4; + *x_len = new_width_ * C4NUM; + *y_len = new_height_ * C4NUM; + *x_weight_len = new_width_ * C4NUM; + *y_weight_len = new_height_ * C4NUM; } } @@ -191,10 +191,10 @@ int ResizeCPUKernel::RunImpl(int task_id) { int unit = UP_DIV(new_height_, op_parameter_->thread_num_); int h_begin = unit * task_id; int h_end = std::min(h_begin + unit, new_height_); - int c = input_shape.at(3); + int c = input_shape.at(kNHWC_C); switch (method_) { case static_cast(schema::ResizeMethod_LINEAR): { - float *line0 = static_cast(line_buffer_) + new_width_ * c * 2 * task_id; + float *line0 = static_cast(line_buffer_) + new_width_ * c * C2NUM * task_id; float *line1 = line0 + new_width_ * c; return ResizeBilinear(input_data, output_data, input_shape.data(), out_tensors_.at(0)->shape().data(), coordinate_.y_bottoms_, coordinate_.y_tops_, coordinate_.x_lefts_, coordinate_.x_rights_, diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/roi_pooling_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/roi_pooling_fp32.cc index 9b5919819e1..4e93b186214 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/roi_pooling_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/roi_pooling_fp32.cc @@ -57,17 +57,17 @@ int ROIPoolingCPUKernel::ReSize() { return RET_ERROR; } param_->ndim_ = ndims; - param_->input_n_ = in_shape.at(0); - param_->input_h_ = in_shape.at(1); - param_->input_w_ = in_shape.at(2); - param_->input_c_ = in_shape.at(3); - param_->output_n_ = out_shape.at(0); - param_->output_h_ = out_shape.at(1); - param_->output_w_ = out_shape.at(2); - param_->output_c_ = out_shape.at(3); + param_->input_n_ = in_shape.at(kNHWC_N); + param_->input_h_ = in_shape.at(kNHWC_H); + param_->input_w_ = in_shape.at(kNHWC_W); + param_->input_c_ = in_shape.at(kNHWC_C); + param_->output_n_ = out_shape.at(kNHWC_N); + param_->output_h_ = out_shape.at(kNHWC_H); + param_->output_w_ = out_shape.at(kNHWC_W); + param_->output_c_ = out_shape.at(kNHWC_C); param_->in_strides_[ndims - 1] = 1; param_->out_strides_[ndims - 1] = 1; - for (int i = ndims - 2; i >= 0; --i) { + for (int i = ndims - C2NUM; i >= 0; --i) { param_->in_strides_[i] = in_shape.at(i + 1) * param_->in_strides_[i + 1]; param_->out_strides_[i] = out_shape.at(i + 1) * param_->out_strides_[i + 1]; } diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/topk_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/topk_fp32.cc index 33dedd2140b..29355c12df3 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/topk_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/topk_fp32.cc @@ -56,7 +56,7 @@ int TopKCPUKernel::Run() { auto output_index = reinterpret_cast(out_tensors_.at(1)->data()); CHECK_NULL_RETURN(output_index); - if (in_tensors_.size() == 2) { + if (in_tensors_.size() == C2NUM) { auto input_k = reinterpret_cast(in_tensors_.at(1)->data()); CHECK_NULL_RETURN(input_k); topk_param_->k_ = input_k[0]; diff --git a/mindspore/lite/src/runtime/kernel/cpu/fp32/where_fp32.cc b/mindspore/lite/src/runtime/kernel/cpu/fp32/where_fp32.cc index e017dbb9166..57a1f352b4e 100644 --- a/mindspore/lite/src/runtime/kernel/cpu/fp32/where_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/cpu/fp32/where_fp32.cc @@ -155,7 +155,7 @@ int WhereCPUKernel::RunWithTripleInputs() { CHECK_NULL_RETURN(condition); auto x = in_tensors_.at(1); CHECK_NULL_RETURN(x); - auto y = in_tensors_.at(2); + auto y = in_tensors_.at(C2NUM); CHECK_NULL_RETURN(y); int condition_nums = condition->ElementsNum(); int x_num = x->ElementsNum();