Skip to content

Commit 10ae3ba

Browse files
authored
Merge pull request #14493 from hjchen2/develop
Implement leaky relu converter from fluid to tensorRT
2 parents f17b05d + 33c6551 commit 10ae3ba

File tree

6 files changed

+149
-3
lines changed

6 files changed

+149
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void IrAnalysisComposePass::InitTensorRTAttrs(Argument *argument) {
4646
{"mul", "conv2d", "pool2d", "relu", "softmax", "sigmoid",
4747
"depthwise_conv2d", "batch_norm", "concat", "tanh", "pad",
4848
"elementwise_add", "elementwise_mul", "dropout", "split", "prelu",
49-
"conv2d_transpose"});
49+
"conv2d_transpose", "leaky_relu"});
5050
if (!node->IsOp()) return false;
5151

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

0 commit comments

Comments
 (0)