Skip to content

Commit d5ff367

Browse files
author
Pei Yang
authored
conflict (#29498)
1 parent 6bfc572 commit d5ff367

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

paddle/fluid/inference/tensorrt/convert/hard_swish_op.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,21 @@ class HardSwishOpConverter : public OpConverter {
6565
const float offset = op_desc.HasAttr("offset")
6666
? BOOST_GET_CONST(float, op_desc.GetAttr("offset"))
6767
: 3.0f;
68-
6968
nvinfer1::ILayer* layer = nullptr;
70-
71-
plugin::HardSwishPlugin* plugin =
72-
new plugin::HardSwishPlugin(threshold, scale, offset);
73-
layer = engine_->AddPlugin(&input, input_num, plugin);
74-
69+
if (threshold == scale) {
70+
auto* hsig_layer = TRT_ENGINE_ADD_LAYER(
71+
engine_, Activation, *input, nvinfer1::ActivationType::kHARD_SIGMOID);
72+
hsig_layer->setAlpha(1.0 / scale);
73+
hsig_layer->setBeta(offset / scale);
74+
nvinfer1::IElementWiseLayer* eltwise_layer = TRT_ENGINE_ADD_LAYER(
75+
engine_, ElementWise, *input, *(hsig_layer->getOutput(0)),
76+
nvinfer1::ElementWiseOperation::kPROD);
77+
layer = eltwise_layer;
78+
} else {
79+
plugin::HardSwishPlugin* plugin =
80+
new plugin::HardSwishPlugin(threshold, scale, offset);
81+
layer = engine_->AddPlugin(&input, input_num, plugin);
82+
}
7583
auto output_name = op_desc.Output("Out")[0];
7684
RreplenishLayerAndOutput(layer, "hard_swish", {output_name}, test_mode);
7785
}

python/paddle/fluid/tests/unittests/ir/inference/test_trt_subgraph_pass.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ def append_act(self, x):
341341
return fluid.layers.hard_swish(x)
342342

343343

344+
class TensorRTSubgraphPassHardSwishPluginTest(
345+
TensorRTSubgraphPassActivationTest):
346+
def append_act(self, x):
347+
return fluid.layers.hard_swish(x, threshold=4.0, scale=8.0)
348+
349+
344350
class TensorRTSubgraphPassHardSigmoidTest(TensorRTSubgraphPassActivationTest):
345351
def append_act(self, x):
346352
return fluid.layers.hard_sigmoid(x)

0 commit comments

Comments
 (0)