From 4b63cf59120d7e3f5897d262b580d1ee5e12fbcf Mon Sep 17 00:00:00 2001 From: wenbin Date: Tue, 15 Feb 2022 08:38:22 +0000 Subject: [PATCH 1/7] first commit --- .../ir/adaptive_pool2d_convert_global_pass.cc | 4 ++++ ...ptive_pool2d_convert_global_pass_autoscan.py | 17 ++++++----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc index c280b7c32ed21..03a7c96e55d11 100644 --- a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc +++ b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc @@ -73,6 +73,10 @@ void AdaptivePool2dConvertGlobalPass::ApplyImpl(ir::Graph* graph) const { if (n->IsOp()) { auto* op = n->Op(); if (op->HasAttr("adaptive") && op->HasAttr("ksize")) { + bool global_pooling = + BOOST_GET_CONST(bool, op->GetAttr("global_pooling")); + if (global_pooling) return; + bool adaptive = BOOST_GET_CONST(bool, op->GetAttr("adaptive")); std::vector ksize = BOOST_GET_CONST(std::vector, op->GetAttr("ksize")); diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_adaptive_pool2d_convert_global_pass_autoscan.py b/python/paddle/fluid/tests/unittests/ir/inference/test_adaptive_pool2d_convert_global_pass_autoscan.py index 96c2a175208fa..ebf52e64d4861 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_adaptive_pool2d_convert_global_pass_autoscan.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_adaptive_pool2d_convert_global_pass_autoscan.py @@ -42,10 +42,14 @@ def sample_program_config(self, draw): st.integers( min_value=1, max_value=4), min_size=2, max_size=2)) - paddings = [0, 0] # only 0 0 is right + paddings = draw( + st.lists( + st.integers( + min_value=1, max_value=4), min_size=2, max_size=2)) + ceil_mode = draw(st.booleans()) exclusive = draw(st.booleans()) - global_pooling = False #only false is right + global_pooling = draw(st.booleans()) padding_algorithm = draw(st.sampled_from(["EXPLICIT", "SAME", "VAILD"])) pool_op = OpConfig( @@ -92,19 +96,10 @@ def teller1(program_config, predictor_config): return True return False - def teller2(program_config, predictor_config): - if program_config.ops[0].attrs["padding_algorithm"] == "SAME": - return True - return False - self.add_ignore_check_case( teller1, IgnoreReasons.PASS_ACCURACY_ERROR, "max pooling has diff if H or W is not equals to 1", ) - self.add_ignore_check_case( - teller2, - IgnoreReasons.PASS_ACCURACY_ERROR, - "output has wrong result if padding_algorithm equals to SAME", ) def test(self): self.run_and_statis( From 0e0c67bbc20522a85de8c36e48b1f3e668e07d04 Mon Sep 17 00:00:00 2001 From: wenbin Date: Tue, 15 Feb 2022 08:47:59 +0000 Subject: [PATCH 2/7] teller fix --- paddle/fluid/inference/tensorrt/op_teller.cc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index f9fc8dcb4891c..7fc139ff62ff4 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -223,6 +223,13 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8, << desc.Output("Out").size(); return false; } + if (desc.HasAttr("data_format")) { + std::string data_format = + BOOST_GET_CONST(std::string, desc.GetAttr("data_format")); + if (data_format == "NHWC" || data_format == "NDHWC") { + return false; + } + } if (!desc.HasAttr("pooling_type")) { return false; } else { From cf0271b89403a7bf4040dc1d89519787848443c7 Mon Sep 17 00:00:00 2001 From: wenbin Date: Wed, 16 Feb 2022 03:02:30 +0000 Subject: [PATCH 3/7] bug fix --- .../ir/adaptive_pool2d_convert_global_pass.cc | 5 ++++- ...adaptive_pool2d_convert_global_pass_autoscan.py | 14 -------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc index 03a7c96e55d11..08e18fba8bbf7 100644 --- a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc +++ b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc @@ -76,7 +76,10 @@ void AdaptivePool2dConvertGlobalPass::ApplyImpl(ir::Graph* graph) const { bool global_pooling = BOOST_GET_CONST(bool, op->GetAttr("global_pooling")); if (global_pooling) return; - + std::string type = + BOOST_GET_CONST(std::string, op->GetAttr("pooling_type")); + // adaptive has no effect on max pooling + if (type == "max") return; bool adaptive = BOOST_GET_CONST(bool, op->GetAttr("adaptive")); std::vector ksize = BOOST_GET_CONST(std::vector, op->GetAttr("ksize")); diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_adaptive_pool2d_convert_global_pass_autoscan.py b/python/paddle/fluid/tests/unittests/ir/inference/test_adaptive_pool2d_convert_global_pass_autoscan.py index ebf52e64d4861..a8c3009a5aea1 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_adaptive_pool2d_convert_global_pass_autoscan.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_adaptive_pool2d_convert_global_pass_autoscan.py @@ -87,20 +87,6 @@ def sample_predictor_configs(self, program_config): use_calib_mode=False) yield config, ['pool2d'], (1e-5, 1e-5) - def add_ignore_pass_case(self): - # Here we put some skip rules to avoid known bugs - def teller1(program_config, predictor_config): - if program_config.ops[0].attrs["pooling_type"] == "max": - x_shape = list(program_config.inputs["input_data"].shape) - if x_shape[-1] != 1 or x_shape[-2] != 1: - return True - return False - - self.add_ignore_check_case( - teller1, - IgnoreReasons.PASS_ACCURACY_ERROR, - "max pooling has diff if H or W is not equals to 1", ) - def test(self): self.run_and_statis( quant=False, From 7a42f071caea3239a6f2953005d0eced28d3f22f Mon Sep 17 00:00:00 2001 From: wenbin Date: Wed, 16 Feb 2022 04:59:59 +0000 Subject: [PATCH 4/7] enable for pool2d only --- .../fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc index 08e18fba8bbf7..8506079842d0f 100644 --- a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc +++ b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc @@ -72,7 +72,8 @@ void AdaptivePool2dConvertGlobalPass::ApplyImpl(ir::Graph* graph) const { for (const Node* n : graph->Nodes()) { if (n->IsOp()) { auto* op = n->Op(); - if (op->HasAttr("adaptive") && op->HasAttr("ksize")) { + if (op->Type() == "pool2d" && op->HasAttr("adaptive") && + op->HasAttr("ksize")) { bool global_pooling = BOOST_GET_CONST(bool, op->GetAttr("global_pooling")); if (global_pooling) return; From 2e06ba2a5c93f2524d385b705ded138fd1f0366d Mon Sep 17 00:00:00 2001 From: wenbin Date: Wed, 16 Feb 2022 05:44:25 +0000 Subject: [PATCH 5/7] fix global_pooling issue --- .../framework/ir/adaptive_pool2d_convert_global_pass.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc index 8506079842d0f..d18bfde21660a 100644 --- a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc +++ b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc @@ -74,9 +74,11 @@ void AdaptivePool2dConvertGlobalPass::ApplyImpl(ir::Graph* graph) const { auto* op = n->Op(); if (op->Type() == "pool2d" && op->HasAttr("adaptive") && op->HasAttr("ksize")) { - bool global_pooling = - BOOST_GET_CONST(bool, op->GetAttr("global_pooling")); - if (global_pooling) return; + if (op->HasAttr("global_pooling")) { + bool global_pooling = + BOOST_GET_CONST(bool, op->GetAttr("global_pooling")); + if (global_pooling) return; + } std::string type = BOOST_GET_CONST(std::string, op->GetAttr("pooling_type")); // adaptive has no effect on max pooling From 3ce51d4f0869e1caaeb57fcc1bf9960db5c30da6 Mon Sep 17 00:00:00 2001 From: wenbin Date: Wed, 16 Feb 2022 06:44:02 +0000 Subject: [PATCH 6/7] pooling_type --- paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc index d18bfde21660a..7846016d7e7b2 100644 --- a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc +++ b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass.cc @@ -79,6 +79,7 @@ void AdaptivePool2dConvertGlobalPass::ApplyImpl(ir::Graph* graph) const { BOOST_GET_CONST(bool, op->GetAttr("global_pooling")); if (global_pooling) return; } + if (!op->HasAttr("pooling_type")) return; std::string type = BOOST_GET_CONST(std::string, op->GetAttr("pooling_type")); // adaptive has no effect on max pooling From dd5ede84ce5221d0f0c927990ca3d98fff3e4857 Mon Sep 17 00:00:00 2001 From: wenbin Date: Wed, 16 Feb 2022 07:33:42 +0000 Subject: [PATCH 7/7] fix test --- .../framework/ir/adaptive_pool2d_convert_global_pass_tester.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass_tester.cc b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass_tester.cc index 19b0c5ca7fc2b..8870b68fbc5c5 100644 --- a/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass_tester.cc +++ b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass_tester.cc @@ -29,6 +29,8 @@ TEST(AdaptivePool2dConvertGlobalPass, basic) { AttributeMap attrs; attrs["adaptive"] = true; attrs["ksize"] = std::vector{1, 1}; + attrs["pooling_type"] = + std::string("avg"); // adaptive has no effect on max pooling layers.pool2d(x, false, &attrs); std::unique_ptr graph(new ir::Graph(layers.main_program()));