Skip to content

Commit 09a4896

Browse files
authored
add test (#35100)
1 parent ec422ea commit 09a4896

File tree

2 files changed

+158
-1
lines changed

2 files changed

+158
-1
lines changed

paddle/fluid/inference/tensorrt/op_teller.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
562562

563563
if (op_type == "slice") {
564564
if (!desc.HasAttr("axes") || !desc.HasAttr("starts") ||
565-
!desc.HasAttr("ends")) {
565+
!desc.HasAttr("ends") || !desc.HasAttr("decrease_axis")) {
566566
return false;
567567
} else {
568568
std::vector<int> axes =
@@ -571,9 +571,16 @@ bool OpTeller::Tell(const framework::ir::Node* node, bool use_no_calib_int8,
571571
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("starts"));
572572
std::vector<int> ends =
573573
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("ends"));
574+
std::vector<int> decrease_axis =
575+
BOOST_GET_CONST(std::vector<int>, desc.GetAttr("decrease_axis"));
574576
if (axes.size() != starts.size() || axes.size() != ends.size()) {
575577
return false;
576578
}
579+
if (decrease_axis.size() > 0) {
580+
VLOG(3) << "Invalid slice decrease_axis. decrease_axis.size() > 0"
581+
"is not supported in TensorRT";
582+
return false;
583+
}
577584
if (!with_dynamic_shape) {
578585
for (size_t i = 0; i < axes.size(); i++) {
579586
if (axes[i] == 0) {
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)