Skip to content

Commit 12fa921

Browse files
committed
add test
1 parent 56c5e21 commit 12fa921

File tree

2 files changed

+293
-46
lines changed

2 files changed

+293
-46
lines changed

paddle/fluid/inference/tensorrt/op_teller.cc

Lines changed: 143 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -90,51 +90,51 @@ struct SimpleOpTypeSetTeller : public Teller {
9090
"elementwise_mul",
9191
"conv2d_transpose",
9292
"hard_swish"};
93-
std::unordered_set<std::string> teller_set{
94-
"mul",
95-
"matmul",
96-
"conv2d",
97-
"conv2d_fusion",
98-
"pool2d",
99-
"relu",
100-
"softmax",
101-
"sigmoid",
102-
"hard_swish",
103-
"depthwise_conv2d",
104-
"batch_norm",
105-
"concat",
106-
"tanh",
107-
"pad",
108-
"elementwise_add",
109-
"elementwise_mul",
110-
"dropout",
111-
"prelu",
112-
"conv2d_transpose",
113-
"depthwise_conv2d_transpose",
114-
"leaky_relu",
115-
"fc",
116-
"shuffle_channel",
117-
"swish",
118-
"split",
119-
"instance_norm",
120-
"gelu",
121-
"layer_norm",
122-
"scale",
123-
"stack",
124-
"transpose2",
125-
"transpose",
126-
"flatten2",
127-
"flatten",
128-
"gather",
129-
"gather_nd",
130-
"yolo_box",
131-
"roi_align",
132-
"affine_channel",
133-
"nearest_interp",
134-
"anchor_generator",
135-
"reduce_sum",
136-
"reduce_mean",
137-
};
93+
std::unordered_set<std::string> teller_set{"mul",
94+
"matmul",
95+
"conv2d",
96+
"conv2d_fusion",
97+
"pool2d",
98+
"relu",
99+
"softmax",
100+
"sigmoid",
101+
"hard_swish",
102+
"depthwise_conv2d",
103+
"batch_norm",
104+
"concat",
105+
"tanh",
106+
"pad",
107+
"elementwise_add",
108+
"elementwise_mul",
109+
"dropout",
110+
"prelu",
111+
"conv2d_transpose",
112+
"depthwise_conv2d_transpose",
113+
"leaky_relu",
114+
"fc",
115+
"shuffle_channel",
116+
"swish",
117+
"split",
118+
"instance_norm",
119+
"gelu",
120+
"layer_norm",
121+
"scale",
122+
"stack",
123+
"transpose2",
124+
"transpose",
125+
"flatten2",
126+
"flatten",
127+
"gather",
128+
"gather_nd",
129+
"yolo_box",
130+
"roi_align",
131+
"affine_channel",
132+
"nearest_interp",
133+
"anchor_generator",
134+
"reduce_sum",
135+
"reduce_mean",
136+
"conv3d",
137+
"conv3d_transpose"};
138138
};
139139

140140
bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
@@ -512,7 +512,7 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
512512

513513
if (op_type == "slice") {
514514
if (!desc.HasAttr("axes") || !desc.HasAttr("starts") ||
515-
!desc.HasAttr("ends")) {
515+
!desc.HasAttr("ends") || !desc.HasAttr("decrease_axis")) {
516516
return false;
517517
} else {
518518
std::vector<int> axes =
@@ -521,9 +521,16 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
521521
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("starts"));
522522
std::vector<int> ends =
523523
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("ends"));
524+
std::vector<int> decrease_axis =
525+
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("decrease_axis"));
524526
if (axes.size() != starts.size() || axes.size() != ends.size()) {
525527
return false;
526528
}
529+
if (decrease_axis.size() > 0) {
530+
VLOG(3) << "Invalid slice decrease_axis. decrease_axis.size() > 0"
531+
"is not supported in TensorRT";
532+
return false;
533+
}
527534
if (!with_dynamic_shape) {
528535
for (size_t i = 0; i < axes.size(); i++) {
529536
if (axes[i] == 0) {
@@ -532,6 +539,15 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
532539
return false;
533540
}
534541
}
542+
} else {
543+
for (size_t i = 0; i < axes.size(); i++) {
544+
if (starts[i] < 0 || ends[i] < 0) {
545+
VLOG(3) << "Invalid slice attribute 'starts' or 'ends'. "
546+
"Negative starts or ends not supported in TensorRT "
547+
"when running in dynamic shape mode.";
548+
return false;
549+
}
550+
}
535551
}
536552
}
537553
}
@@ -652,6 +668,28 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
652668
<< desc.Output("Out").size() << ".";
653669
return false;
654670
}
671+
672+
auto* block = desc.Block();
673+
auto* var_desc = block->FindVar(desc.Input("Alpha")[0]);
674+
if (!var_desc) {
675+
VLOG(3) << "Variable Alpha of prelu TRT converter not found.";
676+
return false;
677+
}
678+
679+
auto x_var_name = desc.Input("X")[0];
680+
auto* x_var_desc = block->FindVar(x_var_name);
681+
const auto x_shape = x_var_desc->GetShape();
682+
if (x_shape.size() == 1) {
683+
VLOG(3) << "prelu op does not support input's dim is 1 in tensorrt.";
684+
return false;
685+
}
686+
687+
if (!with_dynamic_shape) {
688+
if (x_shape.size() == 2) {
689+
VLOG(3) << "prelu op does not support input's dim is 2 in tensorrt.";
690+
return false;
691+
}
692+
}
655693
}
656694

657695
if (op_type == "roi_align") {
@@ -758,6 +796,65 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
758796
}
759797
#endif
760798

799+
if (op_type == "conv3d" || op_type == "conv3d_transpose") {
800+
if (desc.HasAttr("padding_algorithm")) {
801+
std::string padding_algorithm =
802+
BOOST_GET_CONST(std::string, desc.GetAttr("padding_algorithm"));
803+
804+
// trt error is arised if conv3d_transpose and SAME
805+
if (op_type == "conv3d_transpose" && padding_algorithm == "SAME" &&
806+
!with_dynamic_shape) {
807+
return false;
808+
}
809+
}
810+
811+
#if !IS_TRT_VERSION_GE(7000)
812+
// looks like some issues with trt6.0
813+
if (with_dynamic_shape) {
814+
return false;
815+
}
816+
#endif
817+
std::vector<int> paddings =
818+
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("paddings"));
819+
820+
// conv3d and conv3d_transpose need padding check
821+
if (paddings.size() > 3) return false;
822+
823+
if (desc.Input("Input").size() != 1) {
824+
VLOG(3) << "TRT Conv3d expect 1 input, but got "
825+
<< desc.Input("Input").size() << " input.";
826+
return false;
827+
}
828+
829+
if (desc.Input("Filter").size() != 1) {
830+
VLOG(3) << "TRT Conv3d expect 1 filter, but got "
831+
<< desc.Input("Filter").size() << " filter.";
832+
return false;
833+
}
834+
835+
if (op_type == "conv3d_transpose") {
836+
if (!desc.HasAttr("dilations")) {
837+
return false;
838+
} else {
839+
const std::vector<int> dilations =
840+
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("dilations"));
841+
if (dilations[0] != 1 || dilations[1] != 1 || dilations[2] != 1) {
842+
VLOG(3) << "In conv3d_transpose, Dilations must be (1, 1, 1) for "
843+
"tensorRT, but given ("
844+
<< dilations[0] << ", " << dilations[1] << ", "
845+
<< dilations[2] << ")";
846+
return false;
847+
}
848+
}
849+
}
850+
851+
if (desc.Output("Output").size() != 1) {
852+
VLOG(3) << "TRT Conv3d expect 1 output, but got "
853+
<< desc.Output("Output").size() << " output.";
854+
return false;
855+
}
856+
}
857+
761858
if ((*teller)(op_type, desc, use_no_calib_int8)) return true;
762859
}
763860

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
import unittest
22+
23+
24+
class TrtConvertSliceTest(TrtLayerAutoScanTest):
25+
def is_program_valid(self, program_config: ProgramConfig) -> bool:
26+
inputs = program_config.inputs
27+
weights = program_config.weights
28+
attrs = [
29+
program_config.ops[i].attrs
30+
for i in range(len(program_config.ops))
31+
]
32+
33+
for x in attrs[0]["decrease_axis"]:
34+
if x < 0:
35+
return False
36+
for x in range(len(attrs[0]["axes"])):
37+
start = 0
38+
end = 0
39+
if attrs[0]["starts"][x] < 0:
40+
start = attrs[0]["starts"][x] + inputs['input_data'].shape[
41+
attrs[0]["axes"][x]]
42+
else:
43+
start = attrs[0]["starts"][x]
44+
if attrs[0]["ends"][x] < 0:
45+
end = attrs[0]["ends"][x] + inputs['input_data'].shape[attrs[0][
46+
"axes"][x]]
47+
else:
48+
end = attrs[0]["ends"][x]
49+
start = max(0, start)
50+
end = max(0, end)
51+
if start >= end:
52+
return False
53+
54+
return True
55+
56+
def sample_program_configs(self):
57+
def generate_input1(attrs: List[Dict[str, Any]]):
58+
return np.ones([1, 3, 64, 64]).astype(np.float32)
59+
60+
for axes in [[0, 1], [1, 3], [2, 3]]:
61+
for starts in [[0, 1], [-4, -3]]:
62+
for ends in [[2, 2], [-1, -2], [5, 5]]:
63+
for decrease_axis in [[], [1], [2], [-1], [-100]]:
64+
for infer_flags in [[-1]]:
65+
dics = [{
66+
"axes": axes,
67+
"starts": starts,
68+
"ends": ends,
69+
"decrease_axis": decrease_axis,
70+
"infer_flags": infer_flags
71+
}]
72+
73+
ops_config = [{
74+
"op_type": "slice",
75+
"op_inputs": {
76+
"Input": ["input_data"]
77+
},
78+
"op_outputs": {
79+
"Out": ["slice_output_data"]
80+
},
81+
"op_attrs": dics[0]
82+
}]
83+
ops = self.generate_op_config(ops_config)
84+
85+
program_config = ProgramConfig(
86+
ops=ops,
87+
weights={},
88+
inputs={
89+
"input_data": TensorConfig(data_gen=partial(
90+
generate_input1, dics))
91+
},
92+
outputs=["slice_output_data"])
93+
94+
yield program_config
95+
96+
def sample_predictor_configs(
97+
self, program_config) -> (paddle_infer.Config, List[int], float):
98+
def generate_dynamic_shape(attrs):
99+
self.dynamic_shape.min_input_shape = {"input_data": [1, 3, 32, 32]}
100+
self.dynamic_shape.max_input_shape = {"input_data": [4, 3, 64, 64]}
101+
self.dynamic_shape.opt_input_shape = {"input_data": [1, 3, 64, 64]}
102+
103+
def clear_dynamic_shape():
104+
self.dynamic_shape.min_input_shape = {}
105+
self.dynamic_shape.max_input_shape = {}
106+
self.dynamic_shape.opt_input_shape = {}
107+
108+
def generate_trt_nodes_num(attrs, dynamic_shape):
109+
inputs = program_config.inputs
110+
if len(attrs[0]["decrease_axis"]) != 0:
111+
return 0, 3
112+
if dynamic_shape:
113+
for i in range(len(attrs[0]["starts"])):
114+
if attrs[0]["starts"][i] < 0 or attrs[0]["ends"][i] < 0:
115+
return 0, 3
116+
if not dynamic_shape:
117+
for x in attrs[0]["axes"]:
118+
if x == 0:
119+
return 0, 3
120+
return 1, 2
121+
122+
attrs = [
123+
program_config.ops[i].attrs
124+
for i in range(len(program_config.ops))
125+
]
126+
127+
# for static_shape
128+
clear_dynamic_shape()
129+
self.trt_param.precision = paddle_infer.PrecisionType.Float32
130+
yield self.create_inference_config(), generate_trt_nodes_num(
131+
attrs, False), 1e-5
132+
self.trt_param.precision = paddle_infer.PrecisionType.Half
133+
yield self.create_inference_config(), generate_trt_nodes_num(
134+
attrs, False), 1e-4
135+
136+
# for dynamic_shape
137+
generate_dynamic_shape(attrs)
138+
self.trt_param.precision = paddle_infer.PrecisionType.Float32
139+
yield self.create_inference_config(), generate_trt_nodes_num(attrs,
140+
True), 1e-5
141+
self.trt_param.precision = paddle_infer.PrecisionType.Half
142+
yield self.create_inference_config(), generate_trt_nodes_num(attrs,
143+
True), 1e-4
144+
145+
def test(self):
146+
self.run_test()
147+
148+
149+
if __name__ == "__main__":
150+
unittest.main()

0 commit comments

Comments
 (0)