From c5b3758a49defee46d40a8cee0fca0044216bd2a Mon Sep 17 00:00:00 2001 From: 0x3878f <1757941716@qq.com> Date: Tue, 21 Jan 2025 06:57:00 +0000 Subject: [PATCH 1/5] fix ReduceMin input error in multiclass_nms3 exposed during the inference of the PicoDet models. --- paddle2onnx/mapper/detection/multiclass_nms.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle2onnx/mapper/detection/multiclass_nms.cc b/paddle2onnx/mapper/detection/multiclass_nms.cc index 8d3f52f46..220ba228a 100644 --- a/paddle2onnx/mapper/detection/multiclass_nms.cc +++ b/paddle2onnx/mapper/detection/multiclass_nms.cc @@ -138,7 +138,7 @@ void NMSMapper::KeepTopK(const std::string& selected_indices) { AddAttribute(ensemble_value, "axis", int64_t(0)); std::shared_ptr new_top_k; - if (OnnxHelper::GetOpsetVersion() > 13) { + if (OnnxHelper::GetOpsetVersion() >= 18) { std::string reduce_min_axis = helper_->Constant( {1}, ONNX_NAMESPACE::TensorProto::INT64, static_cast(0)); new_top_k = helper_->MakeNode( From 0c11bfea3ad1cc363a6774ee5e8b41b8dedd635b Mon Sep 17 00:00:00 2001 From: 0x3878f <1757941716@qq.com> Date: Wed, 22 Jan 2025 07:15:43 +0000 Subject: [PATCH 2/5] fix the issue with 0-D tensor in linspace op exposed in SOLOv2 --- paddle2onnx/mapper/nn/interpolate.cc | 2 +- paddle2onnx/mapper/tensor/linspace.cc | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/paddle2onnx/mapper/nn/interpolate.cc b/paddle2onnx/mapper/nn/interpolate.cc index 8ae2a947d..4c95931b8 100755 --- a/paddle2onnx/mapper/nn/interpolate.cc +++ b/paddle2onnx/mapper/nn/interpolate.cc @@ -132,7 +132,7 @@ void InterpolateMapper::Opset11() { } std::shared_ptr node; if (size != "") { - node = helper_->MakeNode("Resize", {x_info[0].name, roi, scale, size}, + node = helper_->MakeNode("Resize", {x_info[0].name, roi, "", size}, {out_info[0].name}); } else { node = helper_->MakeNode("Resize", {x_info[0].name, roi, scale}, diff --git a/paddle2onnx/mapper/tensor/linspace.cc b/paddle2onnx/mapper/tensor/linspace.cc index 961b8c0f1..568c7b2d2 100644 --- a/paddle2onnx/mapper/tensor/linspace.cc +++ b/paddle2onnx/mapper/tensor/linspace.cc @@ -53,6 +53,9 @@ void LinspaceMapper::Opset9() { std::string range_tensor = helper_->AutoCast( num_info[0].name, num_info[0].dtype, P2ODataType::INT64); + if(num_info[0].Rank() == 0) { + range_tensor = helper_->Unsqueeze(range_tensor, std::vector(1, 0)); + } std::string one_like_node = helper_->ConstOfShape( range_tensor, GetOnnxDtype(P2ODataType::FP32), static_cast(1)); From 5dd1119885286009b1d347a0033378cbee730f56 Mon Sep 17 00:00:00 2001 From: 0x3878f <1757941716@qq.com> Date: Wed, 22 Jan 2025 11:35:00 +0000 Subject: [PATCH 3/5] fix the issue with opset version when cf ops are present --- paddle2onnx/mapper/exporter.cc | 77 ++++++++++++++++++++++++++------- paddle2onnx/mapper/exporter.h | 7 ++- paddle2onnx/parser/pir_parser.h | 2 +- 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/paddle2onnx/mapper/exporter.cc b/paddle2onnx/mapper/exporter.cc index e20fb38ad..32417620c 100644 --- a/paddle2onnx/mapper/exporter.cc +++ b/paddle2onnx/mapper/exporter.cc @@ -200,32 +200,76 @@ int32_t ModelExporter::GetMinOpsetVersion(const PaddleParser& parser) { return max_opset; } -int32_t ModelExporter::GetMinOpsetVersion(const PaddlePirParser& pir_parser) { +int32_t ModelExporter::GetCfBlockMinOpsetVersion( + const PaddlePirParser& pir_parser, pir::Block& block) { + std::vector sub_blocks_ops_copy(pir_parser.sub_blocks_ops); + pir_parser.sub_blocks_ops.clear(); + std::vector block_ops; + for (auto& op : block.ops()) { + if (op->name() != "builtin.parameter") { + pir_parser.sub_blocks_ops.push_back(op); + } + } + auto max_opset = GetMinOpsetVersion(pir_parser, &block, true); + pir_parser.sub_blocks_ops.clear(); + pir_parser.sub_blocks_ops = sub_blocks_ops_copy; + return max_opset; +} + +int32_t ModelExporter::GetMinOpsetVersion(const PaddlePirParser& pir_parser, + pir::Block* block, + bool if_in_sublock) { int32_t max_opset = 7; std::set verbose_log; OnnxHelper helper; - // TODO(wangmingkai02): consider the case of cf op - for (auto i = 0; i < pir_parser.global_blocks_ops.size(); i++) { - std::string op_name = pir_parser.global_blocks_ops[i]->name(); - if (op_name == "pd_op.data" || op_name == "pd_op.fetch") { + std::vector block_ops; + for (auto& op : + block->ops()) { // it's necessary to be same with global/sub_blocks_ops + if (op->name() == "builtin.parameter") { continue; } - if (op_name == "pd_op.if" || op_name == "pd_op.while") { + block_ops.push_back(op); + } + for (auto i = 0; i < block_ops.size(); ++i) { + auto op = block_ops[i]; + std::string op_name = op->name(); + if (op_name == "pd_op.data" || op_name == "pd_op.fetch" || + op_name == "pd_op.yeild") { continue; } int current_opset = 7; - auto mapper = MapperHelper::Get()->CreateMapper( - convert_pir_op_name(op_name), pir_parser, &helper, i, false); - current_opset = mapper->GetMinOpsetVersion(verbose_); - delete mapper; + if (op_name == "pd_op.if") { + auto if_op = op->dyn_cast(); + pir::Block& true_block = if_op.true_block(); + auto true_block_opset_version = + GetCfBlockMinOpsetVersion(pir_parser, true_block); + pir::Block& false_block = if_op.false_block(); + auto false_block_opset_version = + GetCfBlockMinOpsetVersion(pir_parser, false_block); + current_opset = true_block_opset_version > false_block_opset_version + ? true_block_opset_version + : false_block_opset_version; + current_opset = current_opset > 11 ? current_opset : 11; + } else if (op_name == "pd_op.while") { + auto while_op = op->dyn_cast(); + current_opset = GetCfBlockMinOpsetVersion(pir_parser, while_op.body()); + current_opset = current_opset > 11 ? current_opset : 11; + } else { + auto mapper = MapperHelper::Get()->CreateMapper( + convert_pir_op_name(op_name), pir_parser, &helper, i, if_in_sublock); + current_opset = mapper->GetMinOpsetVersion(verbose_); + delete mapper; + } if (current_opset > max_opset) { max_opset = current_opset; if (current_opset > opset_version_) { - verbose_log.insert( - "Due to the operator: " + pir_parser.global_blocks_ops[i]->name() + - " " + "requires opset_version >= " + std::to_string(current_opset) + - "."); + if (opset_version_ < 11 || + (op_name != "pd_op.if" && op_name != "pd_op.while")) { + verbose_log.insert("Due to the operator: " + op_name + " " + + "requires opset_version >= " + + std::to_string(current_opset) + "."); + } } } } @@ -240,7 +284,8 @@ void ModelExporter::SetOpsetVersion(const PaddlePirParser& pir_parser, bool auto_upgrade_opset) { bool opset_is_legal = true; // here - int32_t min_opset = GetMinOpsetVersion(pir_parser); + int32_t min_opset = + GetMinOpsetVersion(pir_parser, pir_parser.pir_program_->block(), false); if (min_opset < 7 || min_opset > MAX_ONNX_OPSET_VERSION) { P2OLogger(verbose_) << "The Opset Version must be between 7 and " << MAX_ONNX_OPSET_VERSION << std::endl; @@ -249,7 +294,7 @@ void ModelExporter::SetOpsetVersion(const PaddlePirParser& pir_parser, if (!auto_upgrade_opset) { if (min_opset > opset_version_) { P2OLogger(verbose_) << "Please set the opset_version to " - << std::to_string(opset_version_) + << std::to_string(min_opset) << " or set auto_upgrade_opset=true." << std::endl; opset_is_legal = false; } diff --git a/paddle2onnx/mapper/exporter.h b/paddle2onnx/mapper/exporter.h index f791324ba..44e6fb59b 100644 --- a/paddle2onnx/mapper/exporter.h +++ b/paddle2onnx/mapper/exporter.h @@ -157,8 +157,13 @@ class ModelExporter { ONNX_NAMESPACE::ModelProto onnx_model_; // Opset Version + + int32_t GetCfBlockMinOpsetVersion(const PaddlePirParser& pir_parser, + pir::Block& block); int32_t GetMinOpsetVersion(const PaddleParser& parser); - int32_t GetMinOpsetVersion(const PaddlePirParser& parser); + int32_t GetMinOpsetVersion(const PaddlePirParser& pir_parser, + pir::Block* block, + bool if_in_sublock); void SetOpsetVersion(const PaddleParser& parser, bool auto_upgrade_opset); void SetOpsetVersion(const PaddlePirParser& pir_parser, bool auto_upgrade_opset); diff --git a/paddle2onnx/parser/pir_parser.h b/paddle2onnx/parser/pir_parser.h index eb7ced8d9..a4b8775e9 100644 --- a/paddle2onnx/parser/pir_parser.h +++ b/paddle2onnx/parser/pir_parser.h @@ -35,7 +35,7 @@ class PaddlePirParser { // recoring set of operators for global block std::vector global_blocks_ops; // recoring set of operators for sub block - std::vector + mutable std::vector sub_blocks_ops; // todo(wangmingkai02): delete sub_blocks_ops // recording args of while op body name info std::unordered_map From 6f88ae065c41c95f67706d902060a32fc3bfbebf Mon Sep 17 00:00:00 2001 From: 0x3878f <1757941716@qq.com> Date: Wed, 22 Jan 2025 11:36:38 +0000 Subject: [PATCH 4/5] upgrade set_value's opset version from 12 to 17 --- paddle2onnx/mapper/tensor/set_value.cc | 4 ++-- paddle2onnx/mapper/tensor/set_value.h | 2 +- tests/run.bat | 1 - tests/run.sh | 1 - tests/test_auto_scan_set_value.py | 4 ++-- 5 files changed, 5 insertions(+), 7 deletions(-) diff --git a/paddle2onnx/mapper/tensor/set_value.cc b/paddle2onnx/mapper/tensor/set_value.cc index d83f1f09c..636f626c2 100644 --- a/paddle2onnx/mapper/tensor/set_value.cc +++ b/paddle2onnx/mapper/tensor/set_value.cc @@ -44,10 +44,10 @@ int32_t SetValueMapper::GetMinOpsetVersion(bool verbose) { << std::endl; return -1; } - return 12; + return 17; } -void SetValueMapper::Opset12() { +void SetValueMapper::Opset17() { auto input_info = GetInput("Input"); auto output_info = GetOutput("Out"); std::string starts = ""; diff --git a/paddle2onnx/mapper/tensor/set_value.h b/paddle2onnx/mapper/tensor/set_value.h index 3a96db018..6494e6aba 100644 --- a/paddle2onnx/mapper/tensor/set_value.h +++ b/paddle2onnx/mapper/tensor/set_value.h @@ -102,7 +102,7 @@ class SetValueMapper : public Mapper { } int32_t GetMinOpsetVersion(bool verbose) override; - void Opset12() override; + void Opset17() override; private: std::vector axes_; diff --git a/tests/run.bat b/tests/run.bat index d7bbbdd32..ca6571f13 100644 --- a/tests/run.bat +++ b/tests/run.bat @@ -38,7 +38,6 @@ set ignore=!ignore! test_auto_scan_isx_ops.py set ignore=!ignore! test_auto_scan_masked_select.py set ignore=!ignore! test_auto_scan_pad2d.py set ignore=!ignore! test_auto_scan_roll.py -set ignore=!ignore! test_auto_scan_set_value.py set ignore=!ignore! test_auto_scan_unfold.py set ignore=!ignore! test_auto_scan_uniform_random_batch_size_like.py set ignore=!ignore! test_auto_scan_uniform_random.py diff --git a/tests/run.sh b/tests/run.sh index ac62e4fdf..7929fef3b 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -32,7 +32,6 @@ ignore="test_auto_scan_multiclass_nms.py test_auto_scan_masked_select.py \ test_auto_scan_pad2d.py \ test_auto_scan_roll.py \ - test_auto_scan_set_value.py \ test_auto_scan_unfold.py \ test_auto_scan_uniform_random_batch_size_like.py \ test_auto_scan_uniform_random.py \ diff --git a/tests/test_auto_scan_set_value.py b/tests/test_auto_scan_set_value.py index e6f51527b..bc751f40f 100755 --- a/tests/test_auto_scan_set_value.py +++ b/tests/test_auto_scan_set_value.py @@ -38,7 +38,7 @@ def forward(self, inputs, update_input): class TestSetValueConvert(OPConvertAutoScanTest): """ api: set_value - OPset version: 12, 13, 15 + OPset version: 17, 19 """ def sample_convert_config(self, draw): @@ -54,7 +54,7 @@ def sample_convert_config(self, draw): "op_names": ["set_value"], "test_data_shapes": [input_shape, update_input_shape], "test_data_types": [[dtype], [dtype]], - "opset_version": [12, 13, 14, 15], + "opset_version": [17, 19], "input_spec_shape": [], } From b971758e8dcacbeefe68cac3764de58cf4501213 Mon Sep 17 00:00:00 2001 From: 0x3878f <1757941716@qq.com> Date: Wed, 22 Jan 2025 17:21:14 +0000 Subject: [PATCH 5/5] fix --- paddle2onnx/mapper/exporter.cc | 21 ++++++++++++--------- paddle2onnx/mapper/nn/interpolate.cc | 2 +- paddle2onnx/mapper/while.cc | 3 ++- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/paddle2onnx/mapper/exporter.cc b/paddle2onnx/mapper/exporter.cc index 32417620c..795670a1a 100644 --- a/paddle2onnx/mapper/exporter.cc +++ b/paddle2onnx/mapper/exporter.cc @@ -210,6 +210,9 @@ int32_t ModelExporter::GetCfBlockMinOpsetVersion( pir_parser.sub_blocks_ops.push_back(op); } } + // Must generate All sub_block's op output names must be generated here + // because it's may used in OPMapper.GetMinOpsetVersion function. + pir_parser.GetAllSubBlockOpOutputName(pir_parser.sub_blocks_ops); auto max_opset = GetMinOpsetVersion(pir_parser, &block, true); pir_parser.sub_blocks_ops.clear(); pir_parser.sub_blocks_ops = sub_blocks_ops_copy; @@ -223,18 +226,17 @@ int32_t ModelExporter::GetMinOpsetVersion(const PaddlePirParser& pir_parser, std::set verbose_log; OnnxHelper helper; std::vector block_ops; - for (auto& op : - block->ops()) { // it's necessary to be same with global/sub_blocks_ops - if (op->name() == "builtin.parameter") { - continue; + // it's necessary to be same with global/sub_blocks_ops + for (auto& op : block->ops()) { + if (op->name() != "builtin.parameter") { + block_ops.push_back(op); } - block_ops.push_back(op); } for (auto i = 0; i < block_ops.size(); ++i) { auto op = block_ops[i]; std::string op_name = op->name(); if (op_name == "pd_op.data" || op_name == "pd_op.fetch" || - op_name == "pd_op.yeild") { + op_name == "cf.yield") { continue; } int current_opset = 7; @@ -470,9 +472,10 @@ ONNX_NAMESPACE::GraphProto ModelExporter::ExportIfBlock( pir_parser.sub_blocks_ops.push_back(op); } } - pir_parser.GetAllSubBlockOpOutputName(pir_parser.sub_blocks_ops); + // generate sub-block op outputs names in GetMinOpSetVersion() function. + // pir_parser.GetAllSubBlockOpOutputName(pir_parser.sub_blocks_ops); if (!pir_parser.sub_blocks_ops.empty()) { - // get cf.yeild op input + // get cf.yield op input pir::Operation* cf_yield_op = pir_parser.sub_blocks_ops.back(); // std::vector sub_block_outpus; for (int32_t idx = 0; idx < cf_yield_op->num_operands(); ++idx) { @@ -590,7 +593,7 @@ ONNX_NAMESPACE::GraphProto ModelExporter::ExportBlock( for (int32_t idx = 0; idx < outputs.size(); ++idx) { auto output_item = outputs[idx]; if (output_item->name() == input_item->name()) { - output_item->set_name(pir_parser.GenOpInputOutputName("yeild")); + output_item->set_name(pir_parser.GenOpInputOutputName("yield")); temp_helper.MakeNode( "Identity", {input_item->name()}, {output_item->name()}); outputs[idx] = std::move(output_item); diff --git a/paddle2onnx/mapper/nn/interpolate.cc b/paddle2onnx/mapper/nn/interpolate.cc index 4c95931b8..8ae2a947d 100755 --- a/paddle2onnx/mapper/nn/interpolate.cc +++ b/paddle2onnx/mapper/nn/interpolate.cc @@ -132,7 +132,7 @@ void InterpolateMapper::Opset11() { } std::shared_ptr node; if (size != "") { - node = helper_->MakeNode("Resize", {x_info[0].name, roi, "", size}, + node = helper_->MakeNode("Resize", {x_info[0].name, roi, scale, size}, {out_info[0].name}); } else { node = helper_->MakeNode("Resize", {x_info[0].name, roi, scale}, diff --git a/paddle2onnx/mapper/while.cc b/paddle2onnx/mapper/while.cc index 388d6f1da..a868e2e34 100644 --- a/paddle2onnx/mapper/while.cc +++ b/paddle2onnx/mapper/while.cc @@ -61,7 +61,8 @@ void ModelExporter::ExportWhile(PaddlePirParser& pir_parser, } } - pir_parser.GetAllSubBlockOpOutputName(pir_parser.sub_blocks_ops); + // generate sub-block op outputs names in GetMinOpSetVersion() function. + // pir_parser.GetAllSubBlockOpOutputName(pir_parser.sub_blocks_ops); if (!pir_parser.sub_blocks_ops.empty()) { // get cf.yeild op input pir::Operation* cf_yield_op = pir_parser.sub_blocks_ops.back();