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

[Paddle TensorRT No.1] Add pd_op.softplus converter #69315

Merged
merged 2 commits into from
Nov 14, 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
12 changes: 8 additions & 4 deletions paddle/fluid/pir/transforms/tensorrt/trt_op_marker_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1453,13 +1453,14 @@ class StackOpPattern : public pir::OpRewritePattern<paddle::dialect::StackOp> {
}
};

class TanhOpPattern : public pir::OpRewritePattern<paddle::dialect::TanhOp> {
template <typename OpType>
class ActOpPattern : public pir::OpRewritePattern<OpType> {
public:
using pir::OpRewritePattern<paddle::dialect::TanhOp>::OpRewritePattern;
bool MatchAndRewrite(paddle::dialect::TanhOp op,
using pir::OpRewritePattern<OpType>::OpRewritePattern;
bool MatchAndRewrite(OpType op,
pir::PatternRewriter &rewriter) const override {
if (op->HasAttribute(kCanRunTrtAttr) &&
op.attribute<pir::BoolAttribute>(kCanRunTrtAttr).data()) {
op->template attribute<pir::BoolAttribute>(kCanRunTrtAttr).data()) {
return false;
}
#if IS_TRT_VERSION_LT(8600)
Expand All @@ -1477,6 +1478,8 @@ class TanhOpPattern : public pir::OpRewritePattern<paddle::dialect::TanhOp> {
return true;
}
};
using TanhOpPattern = ActOpPattern<paddle::dialect::TanhOp>;
using SoftplusOpPatten = ActOpPattern<paddle::dialect::SoftplusOp>;

class WherePattern : public pir::OpRewritePattern<paddle::dialect::WhereOp> {
public:
Expand Down Expand Up @@ -1783,6 +1786,7 @@ class TrtOpMarkerPass : public pir::PatternRewritePass {
ps.Add(std::make_unique<FullWithTensorPattern>(context));
ps.Add(std::make_unique<StridedSliceOpPattern>(context));
ps.Add(std::make_unique<TopkOpPattern>(context));
ps.Add(std::make_unique<SoftplusOpPatten>(context));
ps.Add(std::make_unique<EqualOpPattern>(context));
ps.Add(std::make_unique<NotEqualOpPattern>(context));
return ps;
Expand Down
17 changes: 17 additions & 0 deletions python/paddle/tensorrt/impls/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ def hardswish_converter(network, paddle_op, inputs):
return hardswish_layer.get_output(0)


@converter_registry.register("pd_op.softplus", trt_version="8.x")
def softplus_converter(network, paddle_op, inputs):
x = inputs[0]
beta = paddle_op.attrs()["beta"]
threshold = paddle_op.attrs()["threshold"]
layer_clip = network.add_activation(x, trt.ActivationType.CLIP)
layer_clip.alpha = -3.40282e038
layer_clip.beta = threshold / beta

softplus_layer = network.add_activation(
layer_clip.get_output(0), trt.ActivationType.SOFTPLUS
)
softplus_layer.alpha = 1.0 / beta
softplus_layer.beta = beta
return softplus_layer.get_output(0)


@converter_registry.register("pd_op.swish", trt_version="8.x")
@converter_registry.register("pd_op.silu", trt_version="8.x")
def swish_silu_converter(network, paddle_op, inputs):
Expand Down
28 changes: 21 additions & 7 deletions test/tensorrt/test_converter_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def test_trt_result(self):
self.check_trt_result()


class TestRELUTRTPattern(TensorRTBaseTest):
class TestReluTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.relu
self.api_args = {"x": np.random.randn(3).astype(np.float32)}
self.api_args = {"x": np.random.randn(3).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.max_shape = {"x": [5]}
Expand All @@ -60,10 +60,10 @@ def test_trt_result(self):
self.check_trt_result()


class TestTANHTRTPattern(TensorRTBaseTest):
class TestTanhTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.tanh
self.api_args = {"x": np.random.randn(3).astype(np.float32)}
self.api_args = {"x": np.random.randn(3).astype("float32")}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1]}
self.max_shape = {"x": [5]}
Expand All @@ -76,11 +76,25 @@ class TestSigmoidTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.functional.sigmoid
self.api_args = {
"x": np.random.randn(2, 3).astype(np.float32),
"x": np.random.randn(2, 3).astype("float32"),
}
self.program_config = {"feed_list": ["x"]}
self.min_shape = {"x": [1, 3], "y": [1, 3]}
self.max_shape = {"x": [5, 3], "y": [5, 3]}
self.min_shape = {"x": [1, 3]}
self.max_shape = {"x": [5, 3]}

def test_trt_result(self):
self.check_trt_result()


class TestSoftplusTRTPattern(TensorRTBaseTest):
def setUp(self):
self.python_api = paddle.nn.Softplus()
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()
Expand Down