!13334 add format and type before interrupt

From: @zhaodezan
Reviewed-by: 
Signed-off-by:
This commit is contained in:
mindspore-ci-bot 2021-03-19 10:03:57 +08:00 committed by Gitee
commit f2bf380b85
8 changed files with 60 additions and 170 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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_);

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -96,19 +96,6 @@ void FreeTensorListC(TensorListC *tensorlist_c) {
free(tensorlist_c);
}
TensorC *NewTensorC() {
auto *tensor_c = static_cast<TensorC *>(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<lite::Tensor *> &inputs, std::vector<lite::Tensor *> *outputs,
std::vector<TensorC *> *out_tensor_c) {
int GenerateMergeSwitchOutTensorC(const std::vector<lite::Tensor *> &inputs, std::vector<lite::Tensor *> *outputs,
std::vector<TensorC *> *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<lite::Tensor *> &inputs, std::vect
return ret;
}
int GenerateSwitchOutTensorC(const std::vector<lite::Tensor *> &inputs, std::vector<lite::Tensor *> *outputs,
std::vector<TensorC *> *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<TensorListC *>(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<TensorC *>(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<TensorC *const>(output_tensorlist1);
auto *output_tensorlist2 = reinterpret_cast<TensorListC *>(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<TensorC *>(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<TensorC *const>(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<TensorC *const>(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<TensorC *const>(output_tensor2);
}
}
return ret;
}
int GenerateOutTensorC(const OpParameter *const parameter, const std::vector<lite::Tensor *> &inputs,
std::vector<lite::Tensor *> *outputs, std::vector<TensorC *> *out_tensor_c) {
int ret = RET_OK;
@ -254,10 +174,9 @@ int GenerateOutTensorC(const OpParameter *const parameter, const std::vector<lit
}
memset(tensor_list_c, 0, sizeof(TensorListC));
out_tensor_c->push_back(reinterpret_cast<TensorC *const>(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);
}

View File

@ -29,15 +29,12 @@ int OutputTensor2TensorC(const std::vector<lite::Tensor *> &tensors_in, std::vec
void SetOutputTensorAttr(const std::vector<TensorC *> &tensors_in, std::vector<lite::Tensor *> *tensors_out);
void FreeAllTensorC(std::vector<TensorC *> *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<lite::Tensor *> &inputs, std::vector<lite::Tensor *> *outputs,
std::vector<TensorC *> *out_tensor_c);
int GenerateSwitchOutTensorC(const std::vector<lite::Tensor *> &inputs, std::vector<lite::Tensor *> *outputs,
std::vector<TensorC *> *out_tensor_c);
int GenerateMergeSwitchOutTensorC(const std::vector<lite::Tensor *> &inputs, std::vector<lite::Tensor *> *outputs,
std::vector<TensorC *> *out_tensor_c);
int GenerateInTensorC(const OpParameter *const parameter, const std::vector<lite::Tensor *> &inputs,
std::vector<lite::Tensor *> *outputs, std::vector<TensorC *> *in_tensor_c);
int GenerateOutTensorC(const OpParameter *const parameter, const std::vector<lite::Tensor *> &inputs,