From c3d14d7dac1476f00ec501d6f1eededa54c2f0d2 Mon Sep 17 00:00:00 2001 From: wangshaocong Date: Tue, 22 Mar 2022 19:18:19 +0800 Subject: [PATCH] [MSLITE] Fix bug of shape fusion --- .../src/runtime/runtime_shape_fusion_pass.cc | 72 +++++++------------ .../test/config_level1/models_tensorrt.cfg | 1 + 2 files changed, 27 insertions(+), 46 deletions(-) diff --git a/mindspore/lite/src/runtime/runtime_shape_fusion_pass.cc b/mindspore/lite/src/runtime/runtime_shape_fusion_pass.cc index 7661c4915ef..1e4608e1e32 100644 --- a/mindspore/lite/src/runtime/runtime_shape_fusion_pass.cc +++ b/mindspore/lite/src/runtime/runtime_shape_fusion_pass.cc @@ -247,71 +247,51 @@ int ShapeFusionPass::GenerateFusedShapeFusionMatrix(Model::Node *shape_fusion, c ShapeFusionMatrix *shape_fusion_matrix) { MS_ASSERT(shape_fusion != nullptr && post_node != nullptr && shape_fusion_matrix != nullptr); std::vector fused_inputs; - std::vector const_inputs; std::set shape_fusion_outputs(shape_fusion->output_indices_.begin(), shape_fusion->output_indices_.end()); std::set post_inputs(post_node->input_indices_.begin(), post_node->input_indices_.end()); std::set_intersection(post_inputs.begin(), post_inputs.end(), shape_fusion_outputs.begin(), shape_fusion_outputs.end(), std::inserter(fused_inputs, fused_inputs.begin())); - std::set_difference(post_inputs.begin(), post_inputs.end(), shape_fusion_outputs.begin(), shape_fusion_outputs.end(), - std::inserter(const_inputs, const_inputs.begin())); MS_CHECK_TRUE_RET(!fused_inputs.empty(), RET_ERROR); MS_CHECK_TRUE_RET(shape_fusion_matrices_.find(fused_inputs.at(0)) != shape_fusion_matrices_.end(), RET_ERROR); *shape_fusion_matrix = shape_fusion_matrices_[fused_inputs.at(0)]; - if (post_node->node_type_ == schema::PrimitiveType_Concat) { - auto input_index = post_node->input_indices_.at(0); - if (std::find(fused_inputs.begin(), fused_inputs.end(), input_index) != fused_inputs.end()) { + auto input_index = post_node->input_indices_.at(0); + if (std::find(fused_inputs.begin(), fused_inputs.end(), input_index) != fused_inputs.end()) { + input_indices->push_back(input_index); + } else { + std::vector shape = {shape_fusion_matrix->shape_matrix.size(), + shape_fusion_matrix->shape_matrix.front().size()}; + auto const_tensor = src_tensors_->at(input_index); + MS_CHECK_TRUE_RET(const_tensor != nullptr && const_tensor->data() != nullptr, RET_ERROR); + if (GetFusionMatrixFromConstantTensor(const_tensor, shape, post_node->node_type_, shape_fusion_matrix) != RET_OK) { + MS_LOG(ERROR) << "GetMatrixFromConstantTensor failed."; + return RET_ERROR; + } + } + + for (size_t i = 1; i < post_node->input_indices_.size(); i++) { + ShapeFusionMatrix const_matrix; + input_index = post_node->input_indices_.at(i); + if (std::find(shape_fusion->output_indices_.begin(), shape_fusion->output_indices_.end(), input_index) != + shape_fusion->output_indices_.end()) { + MS_CHECK_TRUE_RET(shape_fusion_matrices_.find(input_index) != shape_fusion_matrices_.end(), RET_ERROR); + const_matrix = shape_fusion_matrices_[input_index]; input_indices->push_back(input_index); } else { std::vector shape = {shape_fusion_matrix->shape_matrix.size(), shape_fusion_matrix->shape_matrix.front().size()}; auto const_tensor = src_tensors_->at(input_index); MS_CHECK_TRUE_RET(const_tensor != nullptr && const_tensor->data() != nullptr, RET_ERROR); - if (GetFusionMatrixFromConstantTensor(const_tensor, shape, post_node->node_type_, shape_fusion_matrix) != - RET_OK) { - MS_LOG(ERROR) << "GetMatrixFromConstantTensor failed."; - return RET_ERROR; - } - } - - for (size_t i = 1; i < post_node->input_indices_.size(); i++) { - input_index = post_node->input_indices_.at(i); - if (std::find(shape_fusion->output_indices_.begin(), shape_fusion->output_indices_.end(), input_index) != - shape_fusion->output_indices_.end()) { - MS_CHECK_TRUE_RET(shape_fusion_matrices_.find(input_index) != shape_fusion_matrices_.end(), RET_ERROR); - shape_fusion_matrix->Append(shape_fusion_matrices_[input_index]); - input_indices->push_back(input_index); - } else { - ShapeFusionMatrix const_matrix; - std::vector shape = {shape_fusion_matrix->shape_matrix.size(), - shape_fusion_matrix->shape_matrix.front().size()}; - auto const_tensor = src_tensors_->at(input_index); - MS_CHECK_TRUE_RET(const_tensor != nullptr && const_tensor->data() != nullptr, RET_ERROR); - if (GetFusionMatrixFromConstantTensor(const_tensor, shape, post_node->node_type_, &const_matrix) != RET_OK) { - MS_LOG(ERROR) << "GetMatrixFromConstantTensor failed."; - return RET_ERROR; - } - shape_fusion_matrix->Append(const_matrix); - } - } - } else { - ShapeFusionMatrix const_matrix; - input_indices->push_back(fused_inputs.at(0)); - if (fused_inputs.size() == DIMENSION_2D) { - MS_CHECK_TRUE_RET(shape_fusion_matrices_.find(fused_inputs.at(1)) != shape_fusion_matrices_.end(), RET_ERROR); - const_matrix = shape_fusion_matrices_[fused_inputs.at(1)]; - input_indices->push_back(fused_inputs.at(1)); - } else { - MS_CHECK_TRUE_RET(!const_inputs.empty(), RET_ERROR); - std::vector shape = {shape_fusion_matrix->shape_matrix.size(), - shape_fusion_matrix->shape_matrix.front().size()}; - auto const_tensor = src_tensors_->at(const_inputs.at(0)); if (GetFusionMatrixFromConstantTensor(const_tensor, shape, post_node->node_type_, &const_matrix) != RET_OK) { MS_LOG(ERROR) << "GetMatrixFromConstantTensor failed."; return RET_ERROR; } } - shape_fusion_matrix->Arithmetic(const_matrix, static_cast(post_node->node_type_)); + if (post_node->node_type_ == schema::PrimitiveType_Concat) { + shape_fusion_matrix->Append(const_matrix); + } else { + shape_fusion_matrix->Arithmetic(const_matrix, static_cast(post_node->node_type_)); + } } return RET_OK; } diff --git a/mindspore/lite/test/config_level1/models_tensorrt.cfg b/mindspore/lite/test/config_level1/models_tensorrt.cfg index 65bf0ae8c1e..6729a52bcae 100644 --- a/mindspore/lite/test/config_level1/models_tensorrt.cfg +++ b/mindspore/lite/test/config_level1/models_tensorrt.cfg @@ -1,3 +1,4 @@ # model_info accuracy_limit # model_name;input_num:input_name;input_shapes;spec_threads;mode accuracy_limit/CONVERTER dis_matmul_.mindir CONVERTER +dis_matmul_1.mindir CONVERTER