Skip to content

Commit bb4f777

Browse files
authored
[inference][trt]unary, bitwise_not,one_hot op and inspector ci adjust for TensorRT8.6 (#54251) (#54579)
* unary bitwise_not adapter tensorRT8.6 in Paddle-TensorRT * one_hot_op ci fix * Update test_trt_inspector.py delete log
1 parent 35de47b commit bb4f777

File tree

6 files changed

+67
-39
lines changed

6 files changed

+67
-39
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class UnaryOpConverter : public OpConverter {
4343
engine_->GetITensor(op_desc.Input("X")[0]);
4444
auto op_pair = ops.find(op_type_);
4545
nvinfer1::ILayer* layer = nullptr;
46-
#if !IS_TRT_VERSION_GE(8500)
46+
4747
nvinfer1::DataType org_type = input_tensor->getType();
4848
bool cast = org_type == nvinfer1::DataType::kINT8 ||
4949
org_type == nvinfer1::DataType::kINT32;
@@ -56,19 +56,19 @@ class UnaryOpConverter : public OpConverter {
5656
}
5757
input_tensor = layer->getOutput(0);
5858
}
59-
#endif
59+
6060
for (auto trt_op : op_pair->second) {
6161
layer = TRT_ENGINE_ADD_LAYER(engine_, Unary, *input_tensor, trt_op);
6262
input_tensor = layer->getOutput(0);
6363
}
64-
#if !IS_TRT_VERSION_GE(8500)
64+
6565
// type restore
6666
if (cast) {
6767
layer = TRT_ENGINE_ADD_LAYER(engine_, Identity, *input_tensor);
6868
layer->setOutputType(0, org_type);
6969
input_tensor = layer->getOutput(0);
7070
}
71-
#endif
71+
7272
auto output_name = op_desc.Output("Out")[0];
7373
RreplenishLayerAndOutput(layer, op_type_, {output_name}, test_mode);
7474
}

paddle/fluid/inference/tensorrt/op_teller.cc

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,18 +1881,28 @@ struct SimpleOpTypeSetTeller : public Teller {
18811881
}
18821882

18831883
if (op_type == "bitwise_not") {
1884-
#if !IS_TRT_VERSION_GE(8400)
18851884
auto* block = desc.Block();
18861885
auto x_var_name = desc.Input("X")[0];
18871886
auto* x_var_desc = block->FindVar(x_var_name);
18881887
auto dtype = x_var_desc->GetDataType();
1889-
if (dtype == framework::proto::VarType::BOOL ||
1890-
dtype == framework::proto::VarType::INT8 ||
1888+
if (dtype == framework::proto::VarType::INT8 ||
18911889
dtype == framework::proto::VarType::UINT8) {
1892-
VLOG(3) << "BOOL / INT8 / UINT8 type support requires TensorRT 8.4";
1890+
VLOG(3) << "INT8 / UINT8 type convert to trt is not supported";
18931891
return false;
18941892
}
1893+
if (dtype == framework::proto::VarType::BOOL) {
1894+
#if !IS_TRT_VERSION_GE(8400)
1895+
VLOG(3) << "BOOL type support requires TensorRT 8.4";
1896+
return false;
1897+
#elif !IS_TRT_VERSION_GE(8600)
1898+
const auto x_shape = x_var_desc->GetShape();
1899+
if (x_shape.size() == 0) {
1900+
VLOG(3)
1901+
<< "BOOL type does not support 0 dim input when TensorRT < 8.6.";
1902+
return false;
1903+
}
18951904
#endif
1905+
}
18961906
}
18971907

18981908
if (op_type == "one_hot" || op_type == "one_hot_v2") {

test/ir/inference/test_trt_convert_bitwise_not.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def sample_program_configs(self):
3131
self.trt_param.workspace_size = 1073741824
3232

3333
def generate_input1(dims, batch, attrs: List[Dict[str, Any]]):
34-
if dims == 1:
34+
if dims == 0:
35+
return np.random.random([]).astype(np.bool8)
36+
elif dims == 1:
3537
return np.random.random([32]).astype(np.bool8)
3638
elif dims == 2:
3739
return np.random.random([3, 32]).astype(np.int8)
@@ -40,7 +42,7 @@ def generate_input1(dims, batch, attrs: List[Dict[str, Any]]):
4042
else:
4143
return np.random.random([batch, 3, 32, 32]).astype(np.int64)
4244

43-
for dims in [1, 2, 3, 4]:
45+
for dims in [0, 1, 2, 3, 4]:
4446
for batch in [1, 4]:
4547
self.dims = dims
4648
dics = [{}]
@@ -65,13 +67,20 @@ def generate_input1(dims, batch, attrs: List[Dict[str, Any]]):
6567
},
6668
outputs=["output_data"],
6769
)
70+
program_config.input_type = program_config.inputs[
71+
'input_data'
72+
].dtype
6873
yield program_config
6974

7075
def sample_predictor_configs(
7176
self, program_config
7277
) -> (paddle_infer.Config, List[int], float):
7378
def generate_dynamic_shape(attrs):
74-
if self.dims == 1:
79+
if self.dims == 0:
80+
self.dynamic_shape.min_input_shape = {"input_data": []}
81+
self.dynamic_shape.max_input_shape = {"input_data": []}
82+
self.dynamic_shape.opt_input_shape = {"input_data": []}
83+
elif self.dims == 1:
7584
self.dynamic_shape.min_input_shape = {"input_data": [1]}
7685
self.dynamic_shape.max_input_shape = {"input_data": [64]}
7786
self.dynamic_shape.opt_input_shape = {"input_data": [32]}
@@ -102,16 +111,19 @@ def clear_dynamic_shape():
102111
def generate_trt_nodes_num(attrs, dynamic_shape):
103112
ver = paddle_infer.get_trt_compile_version()
104113
trt_version = ver[0] * 1000 + ver[1] * 100 + ver[2] * 10
105-
if trt_version >= 8400:
106-
if self.dims == 1:
114+
if not dynamic_shape:
115+
if self.dims == 1 or self.dims == 0:
107116
return 0, 3
108-
return 1, 2
109-
else:
110-
if self.dims <= 2 or (
111-
program_config.inputs['input_data'].dtype
112-
in ['bool', 'int8', 'uint8']
113-
):
117+
if program_config.input_type in ['int8', 'uint8']:
118+
return 0, 3
119+
elif program_config.input_type == 'bool':
120+
if trt_version <= 8600 and self.dims == 0:
114121
return 0, 3
122+
elif trt_version <= 8400:
123+
return 0, 3
124+
else:
125+
return 1, 2
126+
else:
115127
return 1, 2
116128

117129
attrs = [

test/ir/inference/test_trt_convert_one_hot.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def generate_depth(dims, batch):
5252
dics = [{"dtype": 5, "depth": 10}, {}]
5353
ops_config = [
5454
{
55-
"op_type": "one_hot",
55+
"op_type": "one_hot_v2",
5656
"op_inputs": {
57-
"X": ["input_x_data"],
58-
"depth_tensor": ["input_depth_data"],
57+
"X": ["indices_tensor"],
58+
"depth_tensor": ["depth_tensor_data"],
5959
},
6060
"op_outputs": {"Out": ["output_data"]},
6161
"op_attrs": dics[0],
@@ -67,7 +67,7 @@ def generate_depth(dims, batch):
6767
program_config = ProgramConfig(
6868
ops=ops,
6969
weights={
70-
"depth_tensor": TensorConfig(
70+
"depth_tensor_data": TensorConfig(
7171
data_gen=partial(generate_depth, dims, batch)
7272
),
7373
},
@@ -87,43 +87,43 @@ def sample_predictor_configs(
8787
def generate_dynamic_shape(attrs):
8888
if self.dims == 1:
8989
self.dynamic_shape.min_input_shape = {
90-
"input_x_data": [1],
90+
"indices_tensor": [1],
9191
}
9292
self.dynamic_shape.max_input_shape = {
93-
"input_x_data": [2],
93+
"indices_tensor": [2],
9494
}
9595
self.dynamic_shape.opt_input_shape = {
96-
"input_x_data": [1],
96+
"indices_tensor": [1],
9797
}
9898
elif self.dims == 2:
9999
self.dynamic_shape.min_input_shape = {
100-
"input_x_data": [1, 4],
100+
"indices_tensor": [1, 4],
101101
}
102102
self.dynamic_shape.max_input_shape = {
103-
"input_x_data": [2, 4],
103+
"indices_tensor": [2, 4],
104104
}
105105
self.dynamic_shape.opt_input_shape = {
106-
"input_x_data": [1, 4],
106+
"indices_tensor": [1, 4],
107107
}
108108
elif self.dims == 3:
109109
self.dynamic_shape.min_input_shape = {
110-
"input_x_data": [1, 4, 6],
110+
"indices_tensor": [1, 4, 6],
111111
}
112112
self.dynamic_shape.max_input_shape = {
113-
"input_x_data": [2, 4, 6],
113+
"indices_tensor": [2, 4, 6],
114114
}
115115
self.dynamic_shape.opt_input_shape = {
116-
"input_x_data": [1, 4, 6],
116+
"indices_tensor": [1, 4, 6],
117117
}
118118
elif self.dims == 4:
119119
self.dynamic_shape.min_input_shape = {
120-
"input_x_data": [1, 4, 6, 8],
120+
"indices_tensor": [1, 4, 6, 8],
121121
}
122122
self.dynamic_shape.max_input_shape = {
123-
"input_x_data": [2, 4, 6, 8],
123+
"indices_tensor": [2, 4, 6, 8],
124124
}
125125
self.dynamic_shape.opt_input_shape = {
126-
"input_x_data": [1, 4, 6, 8],
126+
"indices_tensor": [1, 4, 6, 8],
127127
}
128128

129129
def clear_dynamic_shape():

test/ir/inference/test_trt_convert_unary.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def sample_program_configs(self):
3737
def generate_input1(dims, batch, attrs: List[Dict[str, Any]]):
3838
if dims == 0:
3939
return np.random.random([]).astype(np.float32)
40-
if dims == 2:
40+
elif dims == 2:
4141
return np.random.random([3, 32]).astype(np.float32)
4242
elif dims == 3:
4343
return np.random.random([3, 32, 32]).astype(np.float32)
@@ -47,7 +47,7 @@ def generate_input1(dims, batch, attrs: List[Dict[str, Any]]):
4747
def generate_int_input(dims, batch, attrs: List[Dict[str, Any]]):
4848
if dims == 0:
4949
return np.random.random([]).astype(np.int32)
50-
if dims == 2:
50+
elif dims == 2:
5151
return np.random.random([3, 32]).astype(np.int32)
5252
elif dims == 3:
5353
return np.random.random([3, 32, 32]).astype(np.int32)
@@ -146,7 +146,11 @@ def sample_predictor_configs(
146146
self, program_config
147147
) -> (paddle_infer.Config, List[int], float):
148148
def generate_dynamic_shape(attrs):
149-
if self.dims == 1:
149+
if self.dims == 0:
150+
self.dynamic_shape.min_input_shape = {"input_data": []}
151+
self.dynamic_shape.max_input_shape = {"input_data": []}
152+
self.dynamic_shape.opt_input_shape = {"input_data": []}
153+
elif self.dims == 1:
150154
self.dynamic_shape.min_input_shape = {"input_data": [1]}
151155
self.dynamic_shape.max_input_shape = {"input_data": [64]}
152156
self.dynamic_shape.opt_input_shape = {"input_data": [32]}
@@ -193,6 +197,8 @@ def generate_trt_nodes_num(attrs, dynamic_shape):
193197
and self.dims == 0
194198
):
195199
return 0, 3
200+
if not dynamic_shape and (self == 1 or self.dims == 0):
201+
return 0, 3
196202
return 1, 2
197203

198204
attrs = [

test/ir/inference/test_trt_inspector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_check_output(self):
7272
self.assertTrue('====== engine info ======' in engine_info)
7373
self.assertTrue('====== engine info end ======' in engine_info)
7474
self.assertTrue('matmul' in engine_info)
75-
self.assertTrue('LayerType: Scale' in engine_info)
75+
self.assertTrue('"LayerType": "Scale"' in engine_info)
7676
self.assertTrue('batch_norm' in engine_info)
7777
else:
7878
self.assertTrue(

0 commit comments

Comments
 (0)