Skip to content

Commit 4d51cd7

Browse files
author
Pei Yang
authored
support clip op trt converter (#29411) (#29496)
1 parent d5ff367 commit 4d51cd7

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

paddle/fluid/inference/api/analysis_predictor.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ USE_TRT_CONVERTER(skip_layernorm);
11001100
USE_TRT_CONVERTER(slice);
11011101
USE_TRT_CONVERTER(scale);
11021102
USE_TRT_CONVERTER(stack);
1103+
USE_TRT_CONVERTER(clip);
11031104
#endif
11041105

11051106
namespace paddle_infer {

paddle/fluid/inference/api/paddle_api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class PD_INFER_DECL PaddlePredictor {
307307
/// This will save the IO copy for transfering inputs and outputs to predictor
308308
/// workspace
309309
/// and get some performance improvement.
310-
/// To use it, one should call the AnalysisConfig.SwitchUseFeedFetchOp(true)
310+
/// To use it, one should call the AnalysisConfig.SwitchUseFeedFetchOp(false)
311311
/// and then use the `GetInputTensor` and `GetOutputTensor`
312312
/// to directly write or read the input/output tensors.
313313
/// \return Whether the run is successful

paddle/fluid/inference/tensorrt/convert/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ nv_library(tensorrt_converter
44
batch_norm_op.cc activation_op.cc softmax_op.cc concat_op.cc dropout_op.cc
55
pad_op.cc split_op.cc prelu_op.cc leaky_relu_op.cc gelu_op.cc layer_norm_op.cc multihead_matmul_op.cc
66
shuffle_channel_op.cc swish_op.cc instance_norm_op.cc stack_op.cc
7-
emb_eltwise_layernorm.cc skip_layernorm.cc scale_op.cc slice_op.cc hard_sigmoid_op.cc hard_swish_op.cc
7+
emb_eltwise_layernorm.cc skip_layernorm.cc scale_op.cc slice_op.cc hard_sigmoid_op.cc hard_swish_op.cc clip_op.cc
88
DEPS tensorrt_engine tensorrt_plugin operator scope framework_proto op_registry)
99

1010
nv_test(test_op_converter SRCS test_op_converter.cc DEPS
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
16+
17+
namespace paddle {
18+
namespace framework {
19+
class Scope;
20+
namespace proto {
21+
class OpDesc;
22+
} // namespace proto
23+
} // namespace framework
24+
} // namespace paddle
25+
26+
namespace paddle {
27+
namespace inference {
28+
namespace tensorrt {
29+
30+
/*
31+
* ClipOp
32+
*/
33+
class ClipOpConverter : public OpConverter {
34+
public:
35+
void operator()(const framework::proto::OpDesc& op,
36+
const framework::Scope& scope, bool test_mode) override {
37+
#if IS_TRT_VERSION_GE(5130)
38+
VLOG(3) << "convert a paddle clip op to tensorrt IActivationLayer.";
39+
framework::OpDesc op_desc(op, nullptr);
40+
// Declare inputs
41+
auto* input = engine_->GetITensor(op_desc.Input("X")[0]);
42+
float min = BOOST_GET_CONST(float, op_desc.GetAttr("min"));
43+
float max = BOOST_GET_CONST(float, op_desc.GetAttr("max"));
44+
auto* layer = TRT_ENGINE_ADD_LAYER(engine_, Activation, *input,
45+
nvinfer1::ActivationType::kCLIP);
46+
layer->setAlpha(min);
47+
layer->setBeta(max);
48+
49+
auto output_name = op_desc.Output("Out")[0];
50+
RreplenishLayerAndOutput(layer, "clip", {output_name}, test_mode);
51+
#else
52+
PADDLE_THROW(
53+
platform::errors::Fatal("clip TRT converter is only supported on TRT "
54+
"5.1.3.0 or higher version."));
55+
#endif
56+
}
57+
};
58+
59+
} // namespace tensorrt
60+
} // namespace inference
61+
} // namespace paddle
62+
63+
REGISTER_TRT_OP_CONVERTER(clip, ClipOpConverter);

paddle/fluid/inference/tensorrt/op_teller.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ struct SimpleOpTypeSetTeller : public Teller {
3232
#if IS_TRT_VERSION_GE(5130)
3333
teller_set.insert("relu6");
3434
teller_set.insert("hard_sigmoid");
35+
teller_set.insert("clip");
3536
int8_teller_set.insert("relu6");
3637
int8_teller_set.insert("hard_sigmoid");
38+
int8_teller_set.insert("clip");
3739
#endif
3840
#if IS_TRT_VERSION_GE(6000)
3941
teller_set.insert("fused_embedding_eltwise_layernorm");
@@ -132,8 +134,9 @@ bool OpTeller::Tell(const std::string& op_type, const framework::OpDesc& desc,
132134
auto* var_desc = block->FindVar(var_name);
133135
const auto shape = var_desc->GetShape();
134136
if (shape.size() < 3) {
135-
VLOG(1) << "matmul op dims < 3 not supported in tensorrt, but got dims "
136-
<< shape.size() << ", so jump it.";
137+
VLOG(1)
138+
<< "matmul op dims < 3 not supported in tensorrt, but got dims "
139+
<< shape.size() << ", so jump it.";
137140
return false;
138141
}
139142
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,11 @@ def append_act(self, x):
352352
return fluid.layers.hard_sigmoid(x)
353353

354354

355+
class TensorRTSubgraphPassClipTest(TensorRTSubgraphPassActivationTest):
356+
def append_act(self, x):
357+
return fluid.layers.clip(x, 0, 1)
358+
359+
355360
class TensorRTSubgraphPassTanhTest(TensorRTSubgraphPassActivationTest):
356361
def append_act(self, x):
357362
return fluid.layers.tanh(x)

0 commit comments

Comments
 (0)