From d5ff367bf9f0ecfb913701bed6fedef9d576be8e Mon Sep 17 00:00:00 2001 From: Pei Yang Date: Wed, 9 Dec 2020 07:22:27 +0800 Subject: [PATCH] conflict (#29498) --- .../tensorrt/convert/hard_swish_op.cc | 20 +++++++++++++------ .../ir/inference/test_trt_subgraph_pass.py | 6 ++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/inference/tensorrt/convert/hard_swish_op.cc b/paddle/fluid/inference/tensorrt/convert/hard_swish_op.cc index 967f79a1643..57f8fa13515 100644 --- a/paddle/fluid/inference/tensorrt/convert/hard_swish_op.cc +++ b/paddle/fluid/inference/tensorrt/convert/hard_swish_op.cc @@ -65,13 +65,21 @@ class HardSwishOpConverter : public OpConverter { const float offset = op_desc.HasAttr("offset") ? BOOST_GET_CONST(float, op_desc.GetAttr("offset")) : 3.0f; - nvinfer1::ILayer* layer = nullptr; - - plugin::HardSwishPlugin* plugin = - new plugin::HardSwishPlugin(threshold, scale, offset); - layer = engine_->AddPlugin(&input, input_num, plugin); - + if (threshold == scale) { + auto* hsig_layer = TRT_ENGINE_ADD_LAYER( + engine_, Activation, *input, nvinfer1::ActivationType::kHARD_SIGMOID); + hsig_layer->setAlpha(1.0 / scale); + hsig_layer->setBeta(offset / scale); + nvinfer1::IElementWiseLayer* eltwise_layer = TRT_ENGINE_ADD_LAYER( + engine_, ElementWise, *input, *(hsig_layer->getOutput(0)), + nvinfer1::ElementWiseOperation::kPROD); + layer = eltwise_layer; + } else { + plugin::HardSwishPlugin* plugin = + new plugin::HardSwishPlugin(threshold, scale, offset); + layer = engine_->AddPlugin(&input, input_num, plugin); + } auto output_name = op_desc.Output("Out")[0]; RreplenishLayerAndOutput(layer, "hard_swish", {output_name}, test_mode); } diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_subgraph_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_subgraph_pass.py index f26a565f611..159c3bc74f0 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_subgraph_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_subgraph_pass.py @@ -341,6 +341,12 @@ class TensorRTSubgraphPassHardSwishTest(TensorRTSubgraphPassActivationTest): return fluid.layers.hard_swish(x) +class TensorRTSubgraphPassHardSwishPluginTest( + TensorRTSubgraphPassActivationTest): + def append_act(self, x): + return fluid.layers.hard_swish(x, threshold=4.0, scale=8.0) + + class TensorRTSubgraphPassHardSigmoidTest(TensorRTSubgraphPassActivationTest): def append_act(self, x): return fluid.layers.hard_sigmoid(x) -- GitLab