From c1c5c1fc66b2e32e523201ad587d6112059e6d3f Mon Sep 17 00:00:00 2001 From: wenbin Date: Thu, 17 Feb 2022 17:15:10 +0800 Subject: [PATCH] adaptive pool2d pass fix (#39600) * first commit * teller fix * bug fix * enable for pool2d only * fix global_pooling issue * pooling_type * fix test --- .../ir/adaptive_pool2d_convert_global_pass.cc | 13 +++++++- ...ptive_pool2d_convert_global_pass_tester.cc | 2 ++ paddle/fluid/inference/tensorrt/op_teller.cc | 7 +++++ ...ive_pool2d_convert_global_pass_autoscan.py | 31 ++++--------------- 4 files changed, 27 insertions(+), 26 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 c280b7c32e..7846016d7e 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,18 @@ 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")) { + if (op->HasAttr("global_pooling")) { + bool global_pooling = + 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 + 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/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass_tester.cc b/paddle/fluid/framework/ir/adaptive_pool2d_convert_global_pass_tester.cc index 19b0c5ca7f..8870b68fbc 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())); diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index 799c6c55bb..436c80d9a6 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -225,6 +225,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 { 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 96c2a17520..a8c3009a5a 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 @@ class TestAdaptivePool2dConvertGlobalPass(PassAutoScanTest): 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( @@ -83,29 +87,6 @@ class TestAdaptivePool2dConvertGlobalPass(PassAutoScanTest): 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 - - 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( quant=False, -- GitLab