Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【SCU】【Paddle TensorRT No.4】Add pd_op.stanh converter #69539

Merged
merged 12 commits into from
Dec 11, 2024
2 changes: 2 additions & 0 deletions paddle/fluid/pir/transforms/tensorrt/trt_op_marker_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ DEFINE_GENERAL_PATTERN(Swish, paddle::dialect::SwishOp)
DEFINE_GENERAL_PATTERN(Log, paddle::dialect::LogOp)
DEFINE_GENERAL_PATTERN(Floor, paddle::dialect::FloorOp)
DEFINE_GENERAL_PATTERN(Roll, paddle::dialect::RollOp)
DEFINE_GENERAL_PATTERN(Stanh, paddle::dialect::StanhOp)
DEFINE_GENERAL_PATTERN(Softplus, paddle::dialect::SoftplusOp)
DEFINE_GENERAL_PATTERN(ThresholdedRelu, paddle::dialect::ThresholdedReluOp)
DEFINE_GENERAL_PATTERN(Flip, paddle::dialect::FlipOp)
Expand Down Expand Up @@ -2194,6 +2195,7 @@ class TrtOpMarkerPass : public pir::PatternRewritePass {
ADD_PATTERN(Log)
ADD_PATTERN(Floor)
ADD_PATTERN(Roll)
ADD_PATTERN(Stanh)
ADD_PATTERN(Softplus)
ADD_PATTERN(ThresholdedRelu)
ADD_PATTERN(Flip)
Expand Down
11 changes: 11 additions & 0 deletions python/paddle/tensorrt/impls/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ def swish_silu_converter(network, paddle_op, inputs):
return trt_prod(network, inputs[0], layer_output)


@converter_registry.register("pd_op.stanh", trt_version="8.x")
def stanh_converter(network, paddle_op, inputs):
x = inputs[0]
scale_a = paddle_op.attrs()["scale_a"]
scale_b = paddle_op.attrs()["scale_b"]
stanh_layer = network.add_activation(x, trt.ActivationType.SCALED_TANH)
stanh_layer.alpha = scale_b
stanh_layer.beta = scale_a
return stanh_layer.get_output(0)


@converter_registry.register("pd_op.mish", trt_version="8.x")
def mish_converter(network, paddle_op, inputs):
x = inputs[0]
Expand Down
16 changes: 16 additions & 0 deletions test/tensorrt/test_converter_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ def test_trt_result(self):
self.check_trt_result()


class TestStanhFloatTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.stanh
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
"scale_a": 0.67,
"scale_b": 1.7159,
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}

def test_trt_result(self):
self.check_trt_result()


class TestCeluTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.celu
Expand Down