From e90f3006399dae029af6a01dffe459540d605267 Mon Sep 17 00:00:00 2001 From: wenbin Date: Fri, 28 May 2021 20:40:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=8C=96=E9=9D=9Etrt=20conv=E5=88=A4?= =?UTF-8?q?=E6=96=AD=20(#33150)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add more conditions * dynamic shape * ut * correct contidions * commnent * remove rebandadnt op type * remove rebandant if --- paddle/fluid/inference/tensorrt/op_teller.cc | 36 +++++----- .../ir/inference/test_trt_conv_pass.py | 65 +++++++++++++++++++ 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/op_teller.cc b/paddle/fluid/inference/tensorrt/op_teller.cc index 9df3ec0445a..5ed79aa7ea4 100644 --- a/paddle/fluid/inference/tensorrt/op_teller.cc +++ b/paddle/fluid/inference/tensorrt/op_teller.cc @@ -143,19 +143,6 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8, BOOST_GET_CONST(std::vector, desc.GetAttr("paddings")); if (paddings.size() > 2) return false; -// strides > 1 is only supported by trt7.0 above -#if !IS_TRT_VERSION_GE(7000) - if (desc.HasAttr("strides")) { - const std::vector strides = - BOOST_GET_CONST(std::vector, desc.GetAttr("strides")); - // there is no issue if strides.size() less than 2 - if (strides.size() > 1) { - for (size_t i = 0; i < strides.size(); i++) { - if (strides[i] > 1) return false; - } - } - } -#endif } if (op_type == "pool2d") { @@ -239,15 +226,22 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8, return false; } -// strides > 1 is only supported by trt7.0 above +// strides > 1 and 'SAME' is only supported by trt7.0 above #if !IS_TRT_VERSION_GE(7000) - if (desc.HasAttr("strides")) { - const std::vector strides = - BOOST_GET_CONST(std::vector, desc.GetAttr("strides")); - // there is no issue if strides.size() less than 2 - if (strides.size() > 1) { - for (size_t i = 0; i < strides.size(); i++) { - if (strides[i] > 1) return false; + if (op_type == "conv2d" || op_type == "conv2d_fusion" || + op_type == "depthwise_conv2d") { + if (desc.HasAttr("padding_algorithm") && with_dynamic_shape) { + auto padding_algorithm = + BOOST_GET_CONST(std::string, desc.GetAttr("padding_algorithm")); + if (padding_algorithm == "SAME" && desc.HasAttr("strides")) { + const std::vector strides = + BOOST_GET_CONST(std::vector, desc.GetAttr("strides")); + // there is no issue if strides.size() less than 2 + if (strides.size() > 1) { + for (size_t i = 0; i < strides.size(); i++) { + if (strides[i] > 1) return false; + } + } } } } diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_pass.py index ec3955a9ae1..7f613c47659 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_conv_pass.py @@ -161,5 +161,70 @@ class TensorRTSubgraphPassDepthwiseConvTransposeTest( self.use_cudnn = False +class DynamicShapeTensorRTSubgraphPassConvTest(InferencePassTest): + def setUp(self): + self.set_params() + with fluid.program_guard(self.main_program, self.startup_program): + data = fluid.data( + name="data", shape=[-1, 6, -1, -1], dtype="float32") + conv_out = fluid.layers.conv2d( + input=data, + num_filters=self.conv_num_filters, + filter_size=self.conv_filter_size, + groups=self.conv_groups, + padding=self.conv_padding, + bias_attr=False, + use_cudnn=self.use_cudnn, + stride=self.stride, + act=None) + self.feeds = { + "data": np.random.random([32, 6, 64, 64]).astype("float32"), + } + self.enable_trt = True + self.trt_parameters = DynamicShapeTensorRTSubgraphPassConvTest.TensorRTParam( + 1 << 30, 32, 0, AnalysisConfig.Precision.Float32, False, False) + self.dynamic_shape_params = DynamicShapeTensorRTSubgraphPassConvTest.DynamicShapeParam( + { + "conv2d_0.tmp_0": [1, 6, 8, 8], + "data": [1, 6, 8, 8], + "depthwise_conv2d_0.tmp_0": [1, 6, 8, 8] + }, { + "conv2d_0.tmp_0": [32, 6, 64, 64], + "data": [32, 6, 64, 64], + "depthwise_conv2d_0.tmp_0": [32, 6, 64, 64] + }, { + "conv2d_0.tmp_0": [16, 6, 16, 16], + "data": [16, 6, 16, 16], + "depthwise_conv2d_0.tmp_0": [32, 6, 64, 64] + }, False) + self.fetch_list = [conv_out] + + def set_params(self): + self.conv_num_filters = 6 + self.conv_filter_size = 6 + self.conv_groups = 6 + self.conv_padding = 'SAME' + self.use_cudnn = True + self.stride = [2, 2] + + def test_check_output(self): + if core.is_compiled_with_cuda(): + use_gpu = True + self.check_output_with_option(use_gpu) + self.assertTrue( + PassVersionChecker.IsCompatible('tensorrt_subgraph_pass')) + + +class DynamicShapeTensorRTSubgraphPassDepthwiseConvTransposeTest( + DynamicShapeTensorRTSubgraphPassConvTest): + def set_params(self): + self.conv_num_filters = 6 + self.conv_filter_size = 6 + self.conv_groups = 6 + self.conv_padding = 'SAME' + self.use_cudnn = False + self.stride = [2, 2] + + if __name__ == "__main__": unittest.main() -- GitLab