Skip to content

Commit 82f255d

Browse files
authored
add pool2d convert test (#35923)
* add pool2d convert test * modify error * modify error * modify error * modify error * modify error * modify error
1 parent 485b387 commit 82f255d

File tree

7 files changed

+918
-0
lines changed

7 files changed

+918
-0
lines changed

paddle/fluid/inference/tensorrt/convert/pool2d_op.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class Pool2dOpConverter : public OpConverter {
8787
bool adaptive = false;
8888
if (op_desc.HasAttr("adaptive"))
8989
adaptive = BOOST_GET_CONST(bool, op_desc.GetAttr("adaptive"));
90+
std::string padding_algorithm = "EXPLICIT";
91+
if (op_desc.HasAttr("padding_algorithm"))
92+
padding_algorithm =
93+
BOOST_GET_CONST(std::string, op_desc.GetAttr("padding_algorithm"));
9094

9195
nvinfer1::PoolingType nv_pool_type = nvinfer1::PoolingType::kMAX;
9296
nvinfer1::ReduceOperation reduce_operation =
@@ -124,6 +128,9 @@ class Pool2dOpConverter : public OpConverter {
124128
pool_layer->setStride(nv_strides);
125129
pool_layer->setPadding(nv_paddings);
126130
pool_layer->setAverageCountExcludesPadding(exclusive);
131+
if (padding_algorithm == "SAME") {
132+
pool_layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER);
133+
}
127134
layer = pool_layer;
128135
} else if (global_pooling) {
129136
auto *reduce_layer = TRT_ENGINE_ADD_LAYER(engine_, Reduce, *input1,
@@ -159,6 +166,9 @@ class Pool2dOpConverter : public OpConverter {
159166
auto output_name = op_desc.Output("Out")[0];
160167
pool_layer->setStride(nv_strides);
161168
pool_layer->setPadding(nv_paddings);
169+
if (padding_algorithm == "SAME") {
170+
pool_layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER);
171+
}
162172
pool_layer->setAverageCountExcludesPadding(exclusive);
163173
pool_layer->setName(("pool2d (Output: " + output_name + ")").c_str());
164174
pool_layer->getOutput(0)->setName(output_name.c_str());
@@ -198,6 +208,9 @@ class Pool2dOpConverter : public OpConverter {
198208
"trt pool layer in converter could not be created."));
199209
pool_layer->setStride(nv_strides);
200210
pool_layer->setPadding(nv_paddings);
211+
if (padding_algorithm == "SAME") {
212+
pool_layer->setPaddingMode(nvinfer1::PaddingMode::kSAME_UPPER);
213+
}
201214
pool_layer->setAverageCountExcludesPadding(exclusive);
202215
layer = pool_layer;
203216
} else {

paddle/fluid/inference/tensorrt/op_teller.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,22 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
172172
std::vector<int> paddings =
173173
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("paddings"));
174174
if (paddings.size() > 2) return false;
175+
if (desc.HasAttr("exclusive")) {
176+
if (BOOST_GET_CONST(bool, desc.GetAttr("exclusive"))) {
177+
std::vector<int> ksize =
178+
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("ksize"));
179+
for (size_t i = 0; i < ksize.size(); i++) {
180+
if (ksize[i] <= paddings[i]) {
181+
VLOG(3) << "the padding size should be less than the filter size "
182+
"for exclusive-counting pooling.";
183+
return false;
184+
}
185+
}
186+
}
187+
}
188+
if (desc.HasAttr("ceil_mode")) {
189+
if (BOOST_GET_CONST(bool, desc.GetAttr("ceil_mode"))) return false;
190+
}
175191
if (desc.Input("X").size() != 1) {
176192
VLOG(3) << "TRT Pool2d expect 1 input, but got "
177193
<< desc.Input("X").size();
@@ -440,6 +456,10 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
440456
}
441457
}
442458

459+
if (op_type == "anchor_generator") {
460+
if (!with_dynamic_shape) return false;
461+
}
462+
443463
if (op_type == "yolo_box") {
444464
if (with_dynamic_shape) return false;
445465
bool has_attrs =
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Copyright (c) 2021 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+
from trt_layer_auto_scan_test import TrtLayerAutoScanTest, SkipReasons
16+
from program_config import TensorConfig, ProgramConfig
17+
import numpy as np
18+
import paddle.inference as paddle_infer
19+
from functools import partial
20+
from typing import Optional, List, Callable, Dict, Any, Set
21+
22+
23+
class TrtConvertAnchorGeneratorTest(TrtLayerAutoScanTest):
24+
def is_program_valid(self, program_config: ProgramConfig) -> bool:
25+
return True
26+
27+
def sample_program_configs(self):
28+
def generate_input1(batch, attrs: List[Dict[str, Any]]):
29+
return np.random.random([batch, 3, 64, 64]).astype(np.float32)
30+
31+
for batch in [1, 2, 4]:
32+
for anchor_sizes in [[64.0, 128.0, 256.0, 512.0]]:
33+
for aspect_ratios in [[0.5, 1, 2], [0.4, 1.2, 3]]:
34+
for variances in [[1.0, 1.0, 1.0, 1.0],
35+
[0.5, 1.0, 0.5, 1.0]]:
36+
for stride in [[16.0, 16.0], [16.0, 32.0]]:
37+
for offset in [0.5, 0.8]:
38+
dics = [{
39+
"anchor_sizes": anchor_sizes,
40+
"aspect_ratios": aspect_ratios,
41+
"variances": variances,
42+
"stride": stride,
43+
"offset": offset
44+
}]
45+
46+
ops_config = [{
47+
"op_type": "anchor_generator",
48+
"op_inputs": {
49+
"Input": ["input_data"]
50+
},
51+
"op_outputs": {
52+
"Anchors": ["output_anchors"],
53+
"Variances": ["output_variances"]
54+
},
55+
"op_attrs": dics[0]
56+
}]
57+
ops = self.generate_op_config(ops_config)
58+
59+
program_config = ProgramConfig(
60+
ops=ops,
61+
weights={},
62+
inputs={
63+
"input_data": TensorConfig(
64+
data_gen=partial(generate_input1,
65+
batch, dics))
66+
},
67+
outputs=[
68+
"output_anchors", "output_variances"
69+
])
70+
71+
yield program_config
72+
73+
def sample_predictor_configs(
74+
self, program_config) -> (paddle_infer.Config, List[int], float):
75+
def generate_dynamic_shape(attrs):
76+
self.dynamic_shape.min_input_shape = {"input_data": [1, 3, 32, 32]}
77+
self.dynamic_shape.max_input_shape = {"input_data": [4, 3, 64, 64]}
78+
self.dynamic_shape.opt_input_shape = {"input_data": [1, 3, 64, 64]}
79+
80+
def clear_dynamic_shape():
81+
self.dynamic_shape.min_input_shape = {}
82+
self.dynamic_shape.max_input_shape = {}
83+
self.dynamic_shape.opt_input_shape = {}
84+
85+
def generate_trt_nodes_num(attrs, dynamic_shape):
86+
return 1, 3
87+
88+
attrs = [
89+
program_config.ops[i].attrs
90+
for i in range(len(program_config.ops))
91+
]
92+
93+
# for static_shape
94+
clear_dynamic_shape()
95+
self.trt_param.precision = paddle_infer.PrecisionType.Float32
96+
yield self.create_inference_config(), generate_trt_nodes_num(
97+
attrs, False), 1e-5
98+
self.trt_param.precision = paddle_infer.PrecisionType.Half
99+
yield self.create_inference_config(), generate_trt_nodes_num(
100+
attrs, False), 1e-5
101+
102+
# for dynamic_shape
103+
generate_dynamic_shape(attrs)
104+
self.trt_param.precision = paddle_infer.PrecisionType.Float32
105+
yield self.create_inference_config(), generate_trt_nodes_num(attrs,
106+
True), 1e-5
107+
self.trt_param.precision = paddle_infer.PrecisionType.Half
108+
yield self.create_inference_config(), generate_trt_nodes_num(attrs,
109+
True), 1e-5
110+
111+
def test(self):
112+
self.run_test()
113+
114+
115+
if __name__ == "__main__":
116+
unittest.main()

0 commit comments

Comments
 (0)