Skip to content

Commit b807e40

Browse files
authored
[Paddle-TRT] add anchor generator op plugin (#31730)
* add anchor generator op plugin * add anchor generator unit_test * remove dbg info * remove redundant line * replace assertion with paddle enforce * dynamic plugin replaces assertion with paddle enforce * anchor generator support dynamic shape on spatial axis * anchor generator test with fp16, dynamic shape * add anchor generator test all * add back main * reduce test input size to not exceed the timelimit of ci * change super to InferencePassTest for python2 compatibility * reuse paddle operator anchor generator * move creator construct to header with default * add cuda ifdef * reduce line * change super to InferencePassTest for python2 compatibility * fix anchor generator fp16 serialize setting * split unittest from test_all * restrict anchor generator input format before version 7234 * anchor generator only support greater than trt7.1 * change min_graph_size to 2 * min_graph size to 3 if dynamic shape * reduce dynamic shape size to avoid trt search tactic too long to exceed time limit * remove anchor from fetch list * anchor generator support all trt version * fix memory not allocated but if serialized
1 parent 4acc87b commit b807e40

File tree

10 files changed

+990
-8
lines changed

10 files changed

+990
-8
lines changed

paddle/fluid/inference/api/analysis_predictor.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ USE_TRT_CONVERTER(scale);
11921192
USE_TRT_CONVERTER(stack);
11931193
USE_TRT_CONVERTER(clip);
11941194
USE_TRT_CONVERTER(gather);
1195+
USE_TRT_CONVERTER(anchor_generator);
11951196
USE_TRT_CONVERTER(yolo_box);
11961197
USE_TRT_CONVERTER(roi_align);
11971198
USE_TRT_CONVERTER(affine_channel);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ nv_library(tensorrt_converter
66
shuffle_channel_op.cc swish_op.cc instance_norm_op.cc stack_op.cc transpose_op.cc flatten_op.cc
77
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
gather_op.cc
9+
anchor_generator_op.cc
910
yolo_box_op.cc
1011
roi_align_op.cc
1112
affine_channel_op.cc
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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/anchor_generator_op_plugin.h"
17+
18+
namespace paddle {
19+
namespace inference {
20+
namespace tensorrt {
21+
22+
/* Anchor Generator Op */
23+
class AnchorGeneratorOpConverter : public OpConverter {
24+
public:
25+
void operator()(const paddle::framework::proto::OpDesc& op,
26+
const paddle::framework::Scope& scope,
27+
bool test_mode) override {
28+
VLOG(3) << "convert a fluid anchor generator op to tensorrt plugin";
29+
framework::OpDesc op_desc(op, nullptr);
30+
std::string input_name = op_desc.Input("Input").front();
31+
std::string anchor_name = op_desc.Output("Anchors").front();
32+
std::string variance_name = op_desc.Output("Variances").front();
33+
34+
auto* input = engine_->GetITensor(input_name);
35+
const auto input_dims = input->getDimensions(); // C, H, W
36+
std::vector<std::string> output_names{anchor_name, variance_name};
37+
38+
const auto anchor_sizes =
39+
BOOST_GET_CONST(std::vector<float>, op_desc.GetAttr("anchor_sizes"));
40+
const auto aspect_ratios =
41+
BOOST_GET_CONST(std::vector<float>, op_desc.GetAttr("aspect_ratios"));
42+
const auto stride =
43+
BOOST_GET_CONST(std::vector<float>, op_desc.GetAttr("stride"));
44+
const auto variances =
45+
BOOST_GET_CONST(std::vector<float>, op_desc.GetAttr("variances"));
46+
const auto offset = BOOST_GET_CONST(float, op_desc.GetAttr("offset"));
47+
const int num_anchors = aspect_ratios.size() * anchor_sizes.size();
48+
bool is_dynamic = engine_->with_dynamic_shape();
49+
const auto height = input_dims.d[1];
50+
const auto width = input_dims.d[2];
51+
const int box_num = width * height * num_anchors;
52+
const nvinfer1::DataType data_type = nvinfer1::DataType::kFLOAT;
53+
54+
nvinfer1::IPluginV2* anchor_generator_plugin = nullptr;
55+
if (is_dynamic) {
56+
anchor_generator_plugin = new plugin::AnchorGeneratorPluginDynamic(
57+
data_type, anchor_sizes, aspect_ratios, stride, variances, offset,
58+
num_anchors);
59+
} else {
60+
anchor_generator_plugin = new plugin::AnchorGeneratorPlugin(
61+
data_type, anchor_sizes, aspect_ratios, stride, variances, offset,
62+
height, width, num_anchors, box_num);
63+
}
64+
65+
std::vector<nvinfer1::ITensor*> anchor_generator_inputs{input};
66+
auto* anchor_generator_layer = engine_->network()->addPluginV2(
67+
anchor_generator_inputs.data(), anchor_generator_inputs.size(),
68+
*anchor_generator_plugin);
69+
70+
RreplenishLayerAndOutput(anchor_generator_layer, "anchor_generator",
71+
output_names, test_mode);
72+
}
73+
};
74+
75+
} // namespace tensorrt
76+
} // namespace inference
77+
} // namespace paddle
78+
79+
REGISTER_TRT_OP_CONVERTER(anchor_generator, AnchorGeneratorOpConverter);

paddle/fluid/inference/tensorrt/op_teller.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ struct SimpleOpTypeSetTeller : public Teller {
116116
"affine_channel",
117117
"multiclass_nms",
118118
"nearest_interp",
119+
"anchor_generator",
119120
};
120121
};
121122

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ nv_library(tensorrt_plugin
55
instance_norm_op_plugin.cu emb_eltwise_layernorm_plugin.cu
66
qkv_to_context_plugin.cu skip_layernorm_op_plugin.cu slice_op_plugin.cu
77
hard_swish_op_plugin.cu stack_op_plugin.cu special_slice_plugin.cu
8+
anchor_generator_op_plugin.cu
89
yolo_box_op_plugin.cu
910
roi_align_op_plugin.cu
1011
DEPS enforce tensorrt_engine prelu tensor bert_encoder_functor)

0 commit comments

Comments
 (0)