diff --git a/mindspore/lite/nnacl/infer/random_standard_normal_infer.c b/mindspore/lite/nnacl/infer/random_standard_normal_infer.c index db5d25e2d95..584621087ea 100644 --- a/mindspore/lite/nnacl/infer/random_standard_normal_infer.c +++ b/mindspore/lite/nnacl/infer/random_standard_normal_infer.c @@ -25,7 +25,8 @@ int RandomStandardNormalInferShape(const TensorC *const *inputs, size_t inputs_s return check_ret; } #endif - + outputs[0]->data_type_ = kNumberTypeFloat32; + outputs[0]->format_ = inputs[0]->format_; if (!parameter->infer_flag_) { return NNACL_INFER_INVALID; } @@ -41,8 +42,7 @@ int RandomStandardNormalInferShape(const TensorC *const *inputs, size_t inputs_s ShapePush(output_shape, &output_shape_size, input_data[i]); } SetShapeArray(outputs[0], output_shape, output_shape_size); - outputs[0]->data_type_ = kNumberTypeFloat32; - outputs[0]->format_ = inputs[0]->format_; + return NNACL_OK; } diff --git a/mindspore/lite/nnacl/infer/switch_infer.c b/mindspore/lite/nnacl/infer/switch_infer.c index 84c395162f3..3636329ddee 100644 --- a/mindspore/lite/nnacl/infer/switch_infer.c +++ b/mindspore/lite/nnacl/infer/switch_infer.c @@ -21,85 +21,47 @@ int SwitchInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, OpParameter *parameter) { #ifdef Debug - int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter); - if (check_ret != NNACL_OK) { - return check_ret; + for (size_t i = 0; i < inputs_size; i++) { + if (inputs[i] == NULL) { + return NNACL_NULL_PTR; + } } if (2 * (inputs_size - 1) != outputs_size) { return NNACL_ERR; } #endif - for (size_t i = 0; i < outputs_size / 2; i++) { - const TensorC *input = inputs[i + 1]; - TensorC *output_true = outputs[i]; - TensorC *output_false = outputs[i + outputs_size / 2]; - - SetDataTypeFormat(output_false, input); - SetDataTypeFormat(output_true, input); - - if (input->data_type_ == kObjectTypeTensorType) { - TensorListC *input_tensorlist = (TensorListC *)(input); - TensorListC *output_true_tensorlist = (TensorListC *)(output_true); - TensorListC *output_false_tensorlist = (TensorListC *)(output_false); - - ShapeSet(output_true_tensorlist->element_shape_, &output_true_tensorlist->element_shape_size_, - input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); - ShapeSet(output_false_tensorlist->element_shape_, &output_false_tensorlist->element_shape_size_, - input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); - output_true_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; - output_false_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; - output_true_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; - output_false_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; - - // note: need delete below? - for (size_t j = 0; j < output_false_tensorlist->element_num_; j++) { - memcpy(&output_true_tensorlist->tensors_[j], &input_tensorlist->tensors_[j], sizeof(TensorC)); - memcpy(&output_false_tensorlist->tensors_[j], &input_tensorlist->tensors_[j], sizeof(TensorC)); - } - - } else { - } - } - if (!parameter->infer_flag_) { return NNACL_INFER_INVALID; } for (size_t i = 0; i < outputs_size / 2; i++) { - const TensorC *input = inputs[i + 1]; - TensorC *output_true = outputs[i]; - TensorC *output_false = outputs[i + outputs_size / 2]; - - SetDataTypeFormat(output_false, input); - SetDataTypeFormat(output_true, input); - - if (input->data_type_ == kObjectTypeTensorType) { - TensorListC *input_tensorlist = (TensorListC *)(input); - TensorListC *output_true_tensorlist = (TensorListC *)(output_true); - TensorListC *output_false_tensorlist = (TensorListC *)(output_false); - - ShapeSet(output_true_tensorlist->element_shape_, &output_true_tensorlist->element_shape_size_, - input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); - ShapeSet(output_false_tensorlist->element_shape_, &output_false_tensorlist->element_shape_size_, - input_tensorlist->element_shape_, input_tensorlist->element_shape_size_); - output_true_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; - output_false_tensorlist->max_elements_num_ = input_tensorlist->max_elements_num_; - output_true_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; - output_false_tensorlist->tensors_data_type_ = input_tensorlist->tensors_data_type_; - - output_false_tensorlist->element_num_ = input_tensorlist->element_num_; - output_true_tensorlist->element_num_ = input_tensorlist->element_num_; - - for (size_t j = 0; j < output_false_tensorlist->element_num_; j++) { - memcpy(&output_true_tensorlist->tensors_[j], &input_tensorlist->tensors_[j], sizeof(TensorC)); - memcpy(&output_false_tensorlist->tensors_[j], &input_tensorlist->tensors_[j], sizeof(TensorC)); + outputs[i] = (TensorC *)inputs[i + 1]; + if (inputs[i + 1]->data_type_ == kObjectTypeTensorType) { + TensorListC *input = (TensorListC *)inputs[i + 1]; + TensorListC *mirror_tensorlist = (TensorListC *)malloc(sizeof(TensorListC)); // free in infer_manager + if (mirror_tensorlist == NULL) { + return NNACL_ERR; // memory that has been applied will be free in infer_manager } + memcpy(mirror_tensorlist, input, sizeof(TensorListC)); + TensorC *tensor_buffer = (TensorC *)malloc(input->element_num_ * sizeof(TensorC)); + if (tensor_buffer == NULL) { + free(mirror_tensorlist); + return NNACL_ERR; + } + memcpy(tensor_buffer, input->tensors_, input->element_num_ * sizeof(TensorC)); + mirror_tensorlist->tensors_ = tensor_buffer; + outputs[i + outputs_size / 2] = (TensorC *)(mirror_tensorlist); } else { - SetShapeTensor(output_true, input); - SetShapeTensor(output_false, input); + TensorC *mirror_tensor = (TensorC *)malloc(sizeof(TensorC)); + if (mirror_tensor == NULL) { + return NNACL_ERR; + } + memcpy(mirror_tensor, inputs[i + 1], sizeof(TensorC)); + outputs[i + outputs_size / 2] = mirror_tensor; } + *((const TensorC **)inputs + i + 1) = NULL; } return NNACL_OK; diff --git a/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.c b/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.c index d232d4aab9e..e4b6c1c84a4 100644 --- a/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.c +++ b/mindspore/lite/nnacl/infer/tensorlist_fromtensor_infer.c @@ -26,6 +26,10 @@ int TensorListFromTensorInferShape(const TensorC *const *inputs, size_t inputs_s } #endif + TensorListC *output = (TensorListC *)(outputs[0]); + output->data_type_ = kObjectTypeTensorType; + output->format_ = Format_NHWC; + if (!parameter->infer_flag_) { return NNACL_INFER_INVALID; } @@ -43,7 +47,6 @@ int TensorListFromTensorInferShape(const TensorC *const *inputs, size_t inputs_s return NNACL_NULL_PTR; } int *ele_shape_ptr = (int *)(input1->data_); - TensorListC *output = (TensorListC *)(outputs[0]); vvector tensor_shape; tensor_shape.size_ = dim0; @@ -64,8 +67,6 @@ int TensorListFromTensorInferShape(const TensorC *const *inputs, size_t inputs_s ShapeSet(output->element_shape_, &(output->element_shape_size_), ele_shape_ptr, GetElementNum(input1)); output->element_num_ = dim0; - output->data_type_ = kObjectTypeTensorType; - output->format_ = Format_NHWC; MallocTensorListData(output, input0->data_type_, &tensor_shape); free(tensor_shape.shape_); free(tensor_shape.shape_size_); diff --git a/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.c b/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.c index 12c9997d841..ae0f4c8ed0f 100644 --- a/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.c +++ b/mindspore/lite/nnacl/infer/tensorlist_getitem_infer.c @@ -26,9 +26,6 @@ int TensorListGetItemInferShape(const TensorC *const *inputs, size_t inputs_size } #endif - if (!parameter->infer_flag_) { - return NNACL_INFER_INVALID; - } TensorListC *input0 = (TensorListC *)(inputs[0]); const TensorC *get_index = inputs[1]; if (GetElementNum(get_index) != 1) { @@ -42,9 +39,20 @@ int TensorListGetItemInferShape(const TensorC *const *inputs, size_t inputs_size return NNACL_ERR; } TensorC *tensor_index = &input0->tensors_[index]; + TensorC *output = outputs[0]; if (tensor_index->data_type_ != kTypeUnknown) { output->data_type_ = tensor_index->data_type_; + } else { + output->data_type_ = input0->tensors_data_type_; + } + output->format_ = input0->tensors_[index].format_; + + if (!parameter->infer_flag_) { + return NNACL_INFER_INVALID; + } + + if (tensor_index->data_type_ != kTypeUnknown) { ShapeSet(output->shape_, &(output->shape_size_), tensor_index->shape_, tensor_index->shape_size_); } else { const TensorC *input2 = inputs[2]; @@ -76,10 +84,10 @@ int TensorListGetItemInferShape(const TensorC *const *inputs, size_t inputs_size if (!TensorListIsFullyDefined(element_shape, element_shape_size)) { // the pre is the same judge condition return NNACL_ERR; } - output->data_type_ = input0->tensors_data_type_; + SetShapeArray(output, element_shape, element_shape_size); } - output->format_ = input0->tensors_[index].format_; + return NNACL_OK; } diff --git a/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.c b/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.c index d9e5db287d7..3a7381f3acd 100644 --- a/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.c +++ b/mindspore/lite/nnacl/infer/tensorlist_reserve_infer.c @@ -31,6 +31,11 @@ int TensorListReserveInferShape(const TensorC *const *inputs, size_t inputs_size if (ele_shape_type != kNumberTypeInt && ele_shape_type != kNumberTypeInt32) { return NNACL_ERR; } + + TensorListC *output = (TensorListC *)(outputs[0]); + output->data_type_ = kObjectTypeTensorType; + output->format_ = Format_NHWC; + if (input0->data_ == NULL) { return NNACL_INFER_INVALID; } @@ -48,9 +53,6 @@ int TensorListReserveInferShape(const TensorC *const *inputs, size_t inputs_size return NNACL_INFER_INVALID; } int num_elements = ((int *)(input1->data_))[0]; - TensorListC *output = (TensorListC *)(outputs[0]); - output->data_type_ = kObjectTypeTensorType; - output->format_ = Format_NHWC; ShapeSet(output->element_shape_, &(output->element_shape_size_), ele_shape_ptr, GetElementNum(input0)); output->element_num_ = num_elements; diff --git a/mindspore/lite/nnacl/infer/uniform_real_infer.c b/mindspore/lite/nnacl/infer/uniform_real_infer.c index 873b6132b2d..ea6997a14a9 100644 --- a/mindspore/lite/nnacl/infer/uniform_real_infer.c +++ b/mindspore/lite/nnacl/infer/uniform_real_infer.c @@ -19,6 +19,8 @@ int UniformRealInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, OpParameter *parameter) { + outputs[0]->data_type_ = kNumberTypeFloat32; + outputs[0]->format_ = inputs[0]->format_; if (!parameter->infer_flag_) { return NNACL_INFER_INVALID; } @@ -33,7 +35,6 @@ int UniformRealInferShape(const TensorC *const *inputs, size_t inputs_size, Tens output_shape[i] = input_data[i]; } SetShapeArray(outputs[0], output_shape, output_shape_size); - outputs[0]->data_type_ = kNumberTypeFloat32; return NNACL_OK; } diff --git a/mindspore/lite/src/common/tensor_util.cc b/mindspore/lite/src/common/tensor_util.cc index a38de8a4686..46a35b56883 100644 --- a/mindspore/lite/src/common/tensor_util.cc +++ b/mindspore/lite/src/common/tensor_util.cc @@ -96,19 +96,6 @@ void FreeTensorListC(TensorListC *tensorlist_c) { free(tensorlist_c); } -TensorC *NewTensorC() { - auto *tensor_c = static_cast(malloc(sizeof(TensorC))); - if (tensor_c == nullptr) { - MS_LOG(ERROR) << "malloc tensor fail!"; - return nullptr; - } - tensor_c->data_type_ = kNumberTypeFloat32; - tensor_c->format_ = schema::Format::Format_NCHW; - tensor_c->data_ = nullptr; - tensor_c->shape_size_ = 0; - return tensor_c; -} - void Tensor2TensorC(Tensor *src, TensorC *dst) { dst->is_ready_ = src->IsReady(); dst->format_ = src->format(); @@ -165,8 +152,8 @@ void TensorListC2TensorList(TensorListC *src, TensorList *dst) { dst->set_max_elements_num(src->max_elements_num_); } -int GenerateMergeOutTensorC(const std::vector &inputs, std::vector *outputs, - std::vector *out_tensor_c) { +int GenerateMergeSwitchOutTensorC(const std::vector &inputs, std::vector *outputs, + std::vector *out_tensor_c) { int ret = RET_OK; for (size_t i = 0; i < outputs->size(); i++) { out_tensor_c->push_back(nullptr); @@ -174,73 +161,6 @@ int GenerateMergeOutTensorC(const std::vector &inputs, std::vect return ret; } -int GenerateSwitchOutTensorC(const std::vector &inputs, std::vector *outputs, - std::vector *out_tensor_c) { - int ret = RET_OK; - MS_ASSERT(inputs.size() == outputs->size() / 2 + 1); - out_tensor_c->resize(outputs->size()); - for (size_t i = 0; i < outputs->size() / 2; i++) { - if (inputs.at(i + 1)->data_type() == kObjectTypeTensorType) { - auto *output_tensorlist1 = reinterpret_cast(malloc(sizeof(TensorListC))); - if (output_tensorlist1 == nullptr) { - MS_LOG(ERROR) << "malloc tensorlist_c failed"; - ret = RET_ERROR; - break; - } - - memset(output_tensorlist1, 0, sizeof(TensorListC)); - output_tensorlist1->element_num_ = inputs[i + 1]->shape().empty() ? 0 : inputs[i + 1]->shape().at(0); - if (output_tensorlist1->element_num_ != 0) { - output_tensorlist1->tensors_ = - reinterpret_cast(malloc(output_tensorlist1->element_num_ * sizeof(TensorC))); - if (output_tensorlist1->tensors_ == nullptr) { - free(output_tensorlist1); - output_tensorlist1 = nullptr; - return RET_ERROR; - } - memset(output_tensorlist1->tensors_, 0, output_tensorlist1->element_num_ * sizeof(TensorC)); - } - - out_tensor_c->at(i) = reinterpret_cast(output_tensorlist1); - - auto *output_tensorlist2 = reinterpret_cast(malloc(sizeof(TensorListC))); - if (output_tensorlist2 == nullptr) { - return RET_ERROR; - } - memset(output_tensorlist2, 0, sizeof(TensorListC)); - output_tensorlist2->element_num_ = inputs[i + 1]->shape().empty() ? 0 : inputs[i + 1]->shape().at(0); - if (output_tensorlist2->element_num_ != 0) { - output_tensorlist2->tensors_ = - reinterpret_cast(malloc(output_tensorlist2->element_num_ * sizeof(TensorC))); - if (output_tensorlist2->tensors_ == nullptr) { - free(output_tensorlist2); - output_tensorlist2 = nullptr; - return RET_ERROR; - } - memset(output_tensorlist2->tensors_, 0, output_tensorlist2->element_num_ * sizeof(TensorC)); - } - - out_tensor_c->at(i + outputs->size() / 2) = reinterpret_cast(output_tensorlist2); - } else { - auto *output_tensor1 = NewTensorC(); - if (output_tensor1 == nullptr) { - MS_LOG(ERROR) << "malloc tensor_c failed"; - ret = RET_ERROR; - break; - } - out_tensor_c->at(i) = reinterpret_cast(output_tensor1); - auto *output_tensor2 = NewTensorC(); - if (output_tensor2 == nullptr) { - MS_LOG(ERROR) << "malloc tensor_c failed"; - ret = RET_ERROR; - break; - } - out_tensor_c->at(i + outputs->size() / 2) = reinterpret_cast(output_tensor2); - } - } - return ret; -} - int GenerateOutTensorC(const OpParameter *const parameter, const std::vector &inputs, std::vector *outputs, std::vector *out_tensor_c) { int ret = RET_OK; @@ -254,10 +174,9 @@ int GenerateOutTensorC(const OpParameter *const parameter, const std::vectorpush_back(reinterpret_cast(tensor_list_c)); - } else if (parameter->type_ == mindspore::schema::PrimitiveType_Merge) { - ret = GenerateMergeOutTensorC(inputs, outputs, out_tensor_c); - } else if (parameter->type_ == mindspore::schema::PrimitiveType_Switch) { - ret = GenerateSwitchOutTensorC(inputs, outputs, out_tensor_c); + } else if (parameter->type_ == mindspore::schema::PrimitiveType_Merge || + parameter->type_ == mindspore::schema::PrimitiveType_Switch) { + ret = GenerateMergeSwitchOutTensorC(inputs, outputs, out_tensor_c); } else { ret = OutputTensor2TensorC(*outputs, out_tensor_c); } diff --git a/mindspore/lite/src/common/tensor_util.h b/mindspore/lite/src/common/tensor_util.h index 58bb8e7f4e1..7d2e6ecd257 100644 --- a/mindspore/lite/src/common/tensor_util.h +++ b/mindspore/lite/src/common/tensor_util.h @@ -29,15 +29,12 @@ int OutputTensor2TensorC(const std::vector &tensors_in, std::vec void SetOutputTensorAttr(const std::vector &tensors_in, std::vector *tensors_out); void FreeAllTensorC(std::vector *tensors_in); void FreeTensorListC(TensorListC *tensorListC); -TensorC *NewTensorC(); void Tensor2TensorC(Tensor *src, TensorC *dst); void TensorC2Tensor(TensorC *src, Tensor *dst); int TensorList2TensorListC(TensorList *src, TensorListC *dst); void TensorListC2TensorList(TensorListC *src, TensorList *dst); -int GenerateMergeOutTensorC(const std::vector &inputs, std::vector *outputs, - std::vector *out_tensor_c); -int GenerateSwitchOutTensorC(const std::vector &inputs, std::vector *outputs, - std::vector *out_tensor_c); +int GenerateMergeSwitchOutTensorC(const std::vector &inputs, std::vector *outputs, + std::vector *out_tensor_c); int GenerateInTensorC(const OpParameter *const parameter, const std::vector &inputs, std::vector *outputs, std::vector *in_tensor_c); int GenerateOutTensorC(const OpParameter *const parameter, const std::vector &inputs,