Skip to content

Commit a8c077d

Browse files
committed
Implement leaky relu tensorRT converter
1 parent 2825685 commit a8c077d

File tree

6 files changed

+148
-3
lines changed

6 files changed

+148
-3
lines changed

paddle/fluid/inference/analysis/passes/ir_analysis_compose_pass.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ void IrAnalysisComposePass::InitTensorRTAttrs(Argument *argument) {
4545
std::unordered_set<std::string> teller_set(
4646
{"mul", "conv2d", "pool2d", "relu", "softmax", "sigmoid",
4747
"depthwise_conv2d", "batch_norm", "concat", "tanh", "pad",
48-
"elementwise_add", "dropout", "split", "prelu", "conv2d_transpose"});
48+
"elementwise_add", "dropout", "split", "prelu", "conv2d_transpose",
49+
"leaky_relu"});
4950
if (!node->IsOp()) return false;
5051

5152
if (teller_set.count(node->Op()->Type())) {

paddle/fluid/inference/api/analysis_predictor.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,4 +551,5 @@ USE_TRT_CONVERTER(pad);
551551
USE_TRT_CONVERTER(split);
552552
USE_TRT_CONVERTER(prelu);
553553
USE_TRT_CONVERTER(conv2d_transpose);
554+
USE_TRT_CONVERTER(leaky_relu);
554555
#endif

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
nv_library(tensorrt_converter
33
SRCS mul_op.cc conv2d_op.cc fc_op.cc pool2d_op.cc elementwise_op.cc
44
batch_norm_op.cc activation_op.cc softmax_op.cc concat_op.cc dropout_op.cc
5-
pad_op.cc split_op.cc prelu_op.cc
5+
pad_op.cc split_op.cc prelu_op.cc leaky_relu_op.cc
66
DEPS tensorrt_engine tensorrt_plugin operator scope framework_proto op_registry)
77

88
nv_test(test_op_converter SRCS test_op_converter.cc DEPS
@@ -37,3 +37,5 @@ nv_test(test_trt_split_op SRCS test_split_op.cc split_op.cc
3737
nv_test(test_trt_prelu_op SRCS test_prelu_op.cc prelu_op.cc
3838
DEPS ${FLUID_CORE_MODULES} ${GLOB_OPERATOR_DEPS} tensorrt_engine tensorrt_plugin
3939
prelu_op SERIAL)
40+
nv_test(test_trt_leaky_relu_op SRCS test_leaky_relu_op.cc leaky_relu_op.cc
41+
DEPS ${FLUID_CORE_MODULES} ${GLOB_OPERATOR_DEPS} tensorrt_engine activation_op SERIAL)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* Copyright (c) 2018 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+
#include "paddle/fluid/inference/tensorrt/plugin/prelu_op_plugin.h"
17+
18+
namespace paddle {
19+
namespace inference {
20+
namespace tensorrt {
21+
22+
// LeakyRelu converter from fluid to tensorRT
23+
class LeakyReluOpConverter : public OpConverter {
24+
public:
25+
void operator()(const framework::proto::OpDesc& op,
26+
const framework::Scope& scope, bool test_mode) override {
27+
VLOG(4) << "convert fluid leaky_relu op to tensorrt layer";
28+
29+
framework::OpDesc op_desc(op, nullptr);
30+
// Declare inputs
31+
int input_num = op_desc.Input("X").size();
32+
PADDLE_ENFORCE(input_num == 1);
33+
auto* input = engine_->GetITensor(op_desc.Input("X")[0]);
34+
// Get output
35+
size_t output_num = op_desc.Output("Out").size();
36+
PADDLE_ENFORCE(output_num == 1);
37+
// Get attrs
38+
float alpha = boost::get<float>(op_desc.GetAttr("alpha"));
39+
40+
platform::CPUPlace place;
41+
std::unique_ptr<framework::LoDTensor> alpha_tensor(
42+
new framework::LoDTensor());
43+
alpha_tensor->Resize(framework::make_ddim({2}));
44+
float* alpha_data = alpha_tensor->mutable_data<float>(place);
45+
alpha_data[0] = alpha;
46+
alpha_data[1] = 1.f - alpha;
47+
// the leaky relu formula y = (x > 0) ? x : alpha * x is equal to
48+
// y = alpha * x + (x > 0) ? (1 - alpha) * x : 0
49+
TensorRTEngine::Weight scale{nvinfer1::DataType::kFLOAT, &alpha_data[0], 1};
50+
TensorRTEngine::Weight shift{nvinfer1::DataType::kFLOAT, nullptr, 0};
51+
TensorRTEngine::Weight power{nvinfer1::DataType::kFLOAT, nullptr, 0};
52+
// y_scale = alpha * x
53+
auto* scale_layer = TRT_ENGINE_ADD_LAYER(
54+
engine_, Scale, *input, nvinfer1::ScaleMode::kUNIFORM, shift.get(),
55+
scale.get(), power.get());
56+
PADDLE_ENFORCE(nullptr != scale_layer);
57+
// y_relu = (x > 0) : x : 0
58+
auto* relu_layer = TRT_ENGINE_ADD_LAYER(engine_, Activation, *input,
59+
nvinfer1::ActivationType::kRELU);
60+
PADDLE_ENFORCE(nullptr != relu_layer);
61+
//
62+
TensorRTEngine::Weight sub_scale{nvinfer1::DataType::kFLOAT, &alpha_data[1],
63+
1};
64+
auto* scale_relu_layer =
65+
TRT_ENGINE_ADD_LAYER(engine_, Scale, *(relu_layer->getOutput(0)),
66+
nvinfer1::ScaleMode::kUNIFORM, shift.get(),
67+
sub_scale.get(), power.get());
68+
PADDLE_ENFORCE(nullptr != scale_relu_layer);
69+
auto* output_layer =
70+
TRT_ENGINE_ADD_LAYER(engine_, ElementWise, *(scale_layer->getOutput(0)),
71+
*(scale_relu_layer->getOutput(0)),
72+
nvinfer1::ElementWiseOperation::kSUM);
73+
PADDLE_ENFORCE(nullptr != output_layer);
74+
// keep alpha tensor to avoid release it's memory
75+
engine_->weight_map[op_desc.Input("alpha")[0]] = std::move(alpha_tensor);
76+
77+
std::string layer_name = "leaky_relu (Output: ";
78+
auto output_name = op_desc.Output("Out")[0];
79+
output_layer->getOutput(0)->setName(output_name.c_str());
80+
engine_->SetITensor(output_name, output_layer->getOutput(0));
81+
layer_name += output_name;
82+
if (test_mode) {
83+
engine_->DeclareOutput(output_name);
84+
}
85+
output_layer->setName((layer_name + ")").c_str());
86+
}
87+
};
88+
89+
} // namespace tensorrt
90+
} // namespace inference
91+
} // namespace paddle
92+
93+
REGISTER_TRT_OP_CONVERTER(leaky_relu, LeakyReluOpConverter);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright (c) 2018 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 <gtest/gtest.h>
16+
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
17+
#include "paddle/fluid/inference/tensorrt/convert/ut_helper.h"
18+
19+
namespace paddle {
20+
namespace inference {
21+
namespace tensorrt {
22+
23+
TEST(leaky_relu_op, test_channel_wise) {
24+
std::unordered_set<std::string> parameters({"leaky_relu_alpha"});
25+
framework::Scope scope;
26+
TRTConvertValidation validator(10, parameters, scope, 1000);
27+
validator.DeclInputVar("leaky_relu_input", nvinfer1::DimsCHW(3, 2, 2));
28+
validator.DeclOutputVar("leaky_relu_out", nvinfer1::DimsCHW(3, 2, 2));
29+
30+
// Prepare Op description
31+
framework::OpDesc desc;
32+
desc.SetType("leaky_relu");
33+
desc.SetInput("X", {"leaky_relu_input"});
34+
desc.SetOutput("Out", {"leaky_relu_out"});
35+
36+
desc.SetAttr("alpha", 0.1f);
37+
38+
validator.SetOp(*desc.Proto());
39+
40+
validator.Execute(1);
41+
}
42+
43+
} // namespace tensorrt
44+
} // namespace inference
45+
} // namespace paddle
46+
47+
// USE_OP(leaky_relu);
48+
USE_OP(leaky_relu);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nv_library(tensorrt_plugin SRCS trt_plugin.cc split_op_plugin.cu prelu_op_plugin.cu DEPS enforce device_context)
1+
nv_library(tensorrt_plugin SRCS trt_plugin.cc split_op_plugin.cu prelu_op_plugin.cu DEPS enforce tensorrt_engine)

0 commit comments

Comments
 (0)