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.63] Add pd_op.mish converter #69705

Merged
merged 15 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -258,6 +258,7 @@ class ActOpPattern : public pir::OpRewritePattern<OpType> {
};
using TanhOpPattern = ActOpPattern<paddle::dialect::TanhOp>;
using CeluOpPattern = ActOpPattern<paddle::dialect::CeluOp>;
using MishOpPattern = ActOpPattern<paddle::dialect::MishOp>;

class Pool2dOpPattern
: public pir::OpRewritePattern<paddle::dialect::Pool2dOp> {
Expand Down Expand Up @@ -2204,6 +2205,7 @@ class TrtOpMarkerPass : public pir::PatternRewritePass {
ps.Add(std::make_unique<NotEqualOpPattern>(context));
ps.Add(std::make_unique<TanhOpPattern>(context));
ps.Add(std::make_unique<CeluOpPattern>(context));
ps.Add(std::make_unique<MishOpPattern>(context));
ps.Add(std::make_unique<OneHotOpPattern>(context));
ps.Add(std::make_unique<AssignValueOpPattern>(context));
ps.Add(std::make_unique<AssignValue_OpPattern>(context));
Expand Down
14 changes: 14 additions & 0 deletions python/paddle/tensorrt/impls/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ def swish_silu_converter(network, paddle_op, inputs):
return trt_prod(network, inputs[0], layer_output)


@converter_registry.register("pd_op.mish", trt_version="8.x")
def mish_converter(network, paddle_op, inputs):
x = inputs[0]
softplus_layer = network.add_activation(x, trt.ActivationType.SOFTPLUS)
softplus_output = softplus_layer.get_output(0)

tanh_layer = network.add_activation(
softplus_output, trt.ActivationType.TANH
)
tanh_output = tanh_layer.get_output(0)

return trt_prod(network, x, tanh_output)


@converter_registry.register("pd_op.celu", trt_version="8.x")
def celu_converter(network, paddle_op, inputs):
input_tensor = inputs[0]
Expand Down
56 changes: 56 additions & 0 deletions test/tensorrt/test_converter_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,61 @@ def test_trt_result(self):
self.check_trt_result()


class TestMishCase1TRTPattern(TensorRTBaseTest):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

输入支持int64吗,如果支持也需要加一下

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

查阅了paddle文档,仅支持float32和float64

def setUp(self):
self.python_api = paddle.nn.functional.mish
self.api_args = {
"x": np.random.randn(2).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.max_shape = {"x": [5]}

def test_trt_result(self):
self.check_trt_result()


class TestMishCase2TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.mish
self.api_args = {
"x": np.random.randn(2, 3).astype("float32"),
}
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 TestMishCase3TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.mish
self.api_args = {
"x": np.random.randn(2, 3, 4).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4]}
self.max_shape = {"x": [5, 3, 4]}

def test_trt_result(self):
self.check_trt_result()


class TestMishCase4TRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.mish
self.api_args = {
"x": np.random.randn(2, 3, 4, 2).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3, 4, 2]}
self.max_shape = {"x": [5, 3, 4, 2]}

def test_trt_result(self):
self.check_trt_result()


if __name__ == '__main__':
unittest.main()