Skip to content

Commit 7413fa4

Browse files
authored
Merge pull request #13838 from NHZlX/add_trt_pad_op
Add trt pad op converter
2 parents 77e9339 + bf7a278 commit 7413fa4

File tree

5 files changed

+126
-3
lines changed

5 files changed

+126
-3
lines changed

paddle/fluid/inference/analysis/analyzer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class DfgPassManagerImpl final : public DfgPassManager {
7070
auto trt_teller = [&](const Node* node) {
7171
std::unordered_set<std::string> teller_set(
7272
{"mul", "conv2d", "pool2d", "relu", "softmax", "sigmoid",
73-
"depthwise_conv2d", "batch_norm", "concat", "tanh",
73+
"depthwise_conv2d", "batch_norm", "concat", "tanh", "pad",
7474
"elementwise_add", "dropout"});
7575
if (!node->IsFunction()) return false;
7676

paddle/fluid/inference/api/api_tensorrt_subgraph_engine.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,4 @@ USE_TRT_CONVERTER(softmax);
185185
USE_TRT_CONVERTER(batch_norm);
186186
USE_TRT_CONVERTER(concat);
187187
USE_TRT_CONVERTER(dropout);
188+
USE_TRT_CONVERTER(pad);

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Add TRT tests
22
nv_library(tensorrt_converter
33
SRCS mul_op.cc conv2d_op.cc fc_op.cc pool2d_op.cc elementwise_op.cc
4-
batch_norm_op.cc activation_op.cc softmax_op.cc concat_op.cc dropout_op.cc
4+
batch_norm_op.cc activation_op.cc softmax_op.cc concat_op.cc dropout_op.cc pad_op.cc
55
DEPS tensorrt_engine operator scope framework_proto op_registry)
66

77
nv_test(test_op_converter SRCS test_op_converter.cc DEPS
@@ -26,6 +26,8 @@ nv_test(test_trt_batch_norm_op SRCS test_batch_norm_op.cc batch_norm_op.cc
2626
DEPS ${FLUID_CORE_MODULES} tensorrt_engine batch_norm_op SERIAL)
2727
nv_test(test_trt_concat_op SRCS test_concat_op.cc concat_op.cc
2828
DEPS ${FLUID_CORE_MODULES} tensorrt_engine concat_op SERIAL)
29-
3029
nv_test(test_trt_dropout_op SRCS test_dropout_op.cc dropout_op.cc
3130
DEPS ${FLUID_CORE_MODULES} tensorrt_engine dropout_op SERIAL)
31+
32+
nv_test(test_trt_pad_op SRCS test_pad_op.cc pad_op.cc
33+
DEPS ${FLUID_CORE_MODULES} tensorrt_engine pad_op SERIAL)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
/*
22+
* PadOp.
23+
*/
24+
class PadOpConverter : public OpConverter {
25+
public:
26+
void operator()(const framework::proto::OpDesc& op,
27+
const framework::Scope& scope, bool test_mode) override {
28+
VLOG(4) << "convert a fluid transpose op to tensorrt tranpose layer";
29+
30+
framework::OpDesc op_desc(op, nullptr);
31+
// Declare inputs
32+
auto* input = engine_->GetITensor(op_desc.Input("X")[0]);
33+
34+
const std::vector<int> paddings =
35+
boost::get<std::vector<int>>(op_desc.GetAttr("paddings"));
36+
const float pad_value = boost::get<float>(op_desc.GetAttr("pad_value"));
37+
38+
nvinfer1::Dims input_shape = input->getDimensions();
39+
int nbDims = input_shape.nbDims;
40+
int pad_size = static_cast<int>(paddings.size());
41+
PADDLE_ENFORCE_GE(nbDims, 2);
42+
PADDLE_ENFORCE_EQ((nbDims + 1) * 2, pad_size);
43+
PADDLE_ENFORCE(pad_value == 0.0, "The pad layer of TRT only support zero.");
44+
45+
nvinfer1::DimsHW pre_pad(paddings[pad_size - 4], paddings[pad_size - 2]);
46+
nvinfer1::DimsHW post_pad(paddings[pad_size - 3], paddings[pad_size - 1]);
47+
48+
auto* layer = TRT_ENGINE_ADD_LAYER(engine_, Padding,
49+
*const_cast<nvinfer1::ITensor*>(input),
50+
pre_pad, post_pad);
51+
52+
PADDLE_ENFORCE(layer != nullptr);
53+
auto output_name = op_desc.Output("Out")[0];
54+
engine_->SetITensor(output_name, layer->getOutput(0));
55+
layer->setName(("scale (Output: " + output_name + ")").c_str());
56+
layer->getOutput(0)->setName(output_name.c_str());
57+
if (test_mode) { // the test framework can not determine which is the
58+
// output, so place the declaration inside.
59+
engine_->DeclareOutput(output_name);
60+
}
61+
}
62+
};
63+
64+
} // namespace tensorrt
65+
} // namespace inference
66+
} // namespace paddle
67+
68+
REGISTER_TRT_OP_CONVERTER(pad, PadOpConverter);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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/framework/op_registry.h"
17+
#include "paddle/fluid/inference/tensorrt/convert/ut_helper.h"
18+
19+
namespace paddle {
20+
namespace inference {
21+
namespace tensorrt {
22+
23+
TEST(PadConverter, main) {
24+
framework::Scope scope;
25+
std::unordered_set<std::string> parameters;
26+
TRTConvertValidation validator(10, parameters, scope, 1000);
27+
validator.DeclInputVar("pad-X", nvinfer1::Dims3(3, 2, 2));
28+
validator.DeclOutputVar("pad-Out", nvinfer1::Dims3(3, 3, 5));
29+
30+
// Prepare Op description
31+
framework::OpDesc desc;
32+
desc.SetType("pad");
33+
desc.SetInput("X", {"pad-X"});
34+
desc.SetOutput("Out", {"pad-Out"});
35+
36+
std::vector<int> paddings = {0, 0, 0, 0, 0, 1, 1, 2};
37+
float pad_value = 0.0;
38+
desc.SetAttr("paddings", paddings);
39+
desc.SetAttr("pad_value", pad_value);
40+
41+
LOG(INFO) << "set OP";
42+
validator.SetOp(*desc.Proto());
43+
LOG(INFO) << "execute";
44+
45+
validator.Execute(2);
46+
}
47+
48+
} // namespace tensorrt
49+
} // namespace inference
50+
} // namespace paddle
51+
52+
USE_OP(pad);

0 commit comments

Comments
 (0)