Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
82c73884
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
82c73884
编写于
5月 11, 2023
作者:
Y
Yuanle Liu
提交者:
GitHub
5月 11, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[inference Zero-Dim]prelu trt converter support zero dim tensor (#53634)
* prelu op trt converter support zero dim
上级
5417382d
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
148 addition
and
173 deletion
+148
-173
paddle/fluid/framework/ir/trt_support_nhwc_pass.cc
paddle/fluid/framework/ir/trt_support_nhwc_pass.cc
+2
-0
paddle/fluid/inference/tensorrt/convert/prelu_op.cc
paddle/fluid/inference/tensorrt/convert/prelu_op.cc
+0
-1
paddle/fluid/inference/tensorrt/op_teller.cc
paddle/fluid/inference/tensorrt/op_teller.cc
+14
-14
paddle/phi/infermeta/binary.cc
paddle/phi/infermeta/binary.cc
+1
-1
test/ir/inference/test_trt_convert_prelu.py
test/ir/inference/test_trt_convert_prelu.py
+131
-157
未找到文件。
paddle/fluid/framework/ir/trt_support_nhwc_pass.cc
浏览文件 @
82c73884
...
...
@@ -356,6 +356,8 @@ void TrtSupportNHWCPass::ApplyImpl(Graph *graph) const {
}
};
InsertTransposeOp
();
AddStatis
(
transposed_ops
.
size
());
}
}
// namespace ir
...
...
paddle/fluid/inference/tensorrt/convert/prelu_op.cc
浏览文件 @
82c73884
...
...
@@ -87,7 +87,6 @@ class PReluOpConverter : public OpConverter {
if
(
hw_tensor
!=
nullptr
)
{
shape_tensor
=
Concat
(
std
::
vector
<
nvinfer1
::
ITensor
*>
{
n_tensor
,
c_tensor
,
hw_tensor
});
}
else
{
shape_tensor
=
Concat
(
std
::
vector
<
nvinfer1
::
ITensor
*>
{
n_tensor
,
c_tensor
});
...
...
paddle/fluid/inference/tensorrt/op_teller.cc
浏览文件 @
82c73884
...
...
@@ -1837,28 +1837,28 @@ struct SimpleOpTypeSetTeller : public Teller {
"the pass."
;
return
false
;
}
auto
*
var_desc
=
block
->
FindVar
(
desc
.
Input
(
"Alpha"
)[
0
]);
if
(
!
var_desc
)
{
auto
*
alpha_var
=
block
->
FindVar
(
desc
.
Input
(
"Alpha"
)[
0
]);
if
(
!
alpha_var
)
{
VLOG
(
3
)
<<
"Variable Alpha of prelu TRT converter not found."
;
return
false
;
}
auto
x_var_name
=
desc
.
Input
(
"X"
)[
0
];
auto
*
x_var_desc
=
block
->
FindVar
(
x_var_name
);
const
auto
x_shape
=
x_var_desc
->
GetShape
();
if
(
!
with_dynamic_shape
&&
x_shape
.
size
()
==
1
)
{
VLOG
(
3
)
<<
"prelu op does not support input's dim is 1 in tensorrt "
"with static shape."
;
auto
alpha_shape
=
alpha_var
->
GetShape
();
if
(
!
with_dynamic_shape
&&
alpha_shape
.
size
()
==
0
)
{
VLOG
(
3
)
<<
op_type
<<
" op does not support alpha's dim is 0 in tensorrt "
"static shape mode."
;
return
false
;
}
#if IS_TRT_VERSION_LT(7000)
if
(
!
with_dynamic_shape
)
{
// TODO(inference): fix trt6 static plugin error.
VLOG
(
3
)
<<
"prelu static plugin in trt6 has bug."
;
auto
x_var_name
=
desc
.
Input
(
"X"
)[
0
];
auto
*
x_var
=
block
->
FindVar
(
x_var_name
);
const
auto
x_shape
=
x_var
->
GetShape
();
if
(
!
with_dynamic_shape
&&
(
x_shape
.
size
()
==
1
||
x_shape
.
size
()
==
0
))
{
VLOG
(
3
)
<<
op_type
<<
" op does not support input's dim is 1 or 0 in tensorrt "
"with static shape."
;
return
false
;
}
#endif
}
if
(
op_type
==
"mish"
)
{
...
...
paddle/phi/infermeta/binary.cc
浏览文件 @
82c73884
...
...
@@ -2340,7 +2340,7 @@ void PReluInferMeta(const MetaTensor& x,
1
,
phi
::
errors
::
InvalidArgument
(
"For mode 'element', rank of input X must be "
"equal or larger than
2
. But recevied X's "
"equal or larger than
1
. But recevied X's "
"rank: %d"
,
x_rank
));
PADDLE_ENFORCE_EQ
(
...
...
test/ir/inference/test_trt_convert_prelu.py
浏览文件 @
82c73884
...
...
@@ -18,7 +18,7 @@ from typing import Any, Dict, List
import
numpy
as
np
from
program_config
import
ProgramConfig
,
TensorConfig
from
trt_layer_auto_scan_test
import
SkipReasons
,
TrtLayerAutoScanTest
from
trt_layer_auto_scan_test
import
TrtLayerAutoScanTest
import
paddle.inference
as
paddle_infer
...
...
@@ -28,170 +28,165 @@ class TrtConvertPreluTest(TrtLayerAutoScanTest):
return
True
def
sample_program_configs
(
self
):
def
generate_input
(
batch
,
dim1
,
dim2
,
dim3
):
shape
=
[
batch
]
if
dim1
!=
0
:
shape
.
append
(
dim1
)
if
dim2
!=
0
:
shape
.
append
(
dim2
)
if
dim3
!=
0
:
shape
.
append
(
dim3
)
return
np
.
random
.
random
(
shape
).
astype
(
np
.
float32
)
def
generate_alpha
(
attrs
:
List
[
Dict
[
str
,
Any
]],
dim1
,
dim2
,
dim3
):
def
generate_input
(
attrs
:
List
[
Dict
[
str
,
Any
]],
batch
):
if
self
.
dims
==
0
:
return
np
.
random
.
random
([]).
astype
(
np
.
float32
)
elif
self
.
dims
==
1
:
return
np
.
random
.
random
([
16
]).
astype
(
np
.
float32
)
elif
self
.
dims
==
2
:
return
np
.
random
.
random
([
1
,
3
]).
astype
(
np
.
float32
)
elif
self
.
dims
==
3
:
if
attrs
[
0
][
"data_format"
]
==
"NCHW"
:
return
np
.
random
.
random
([
batch
,
3
,
16
]).
astype
(
np
.
float32
)
elif
attrs
[
0
][
"data_format"
]
==
"NHWC"
:
return
np
.
random
.
random
([
batch
,
16
,
3
]).
astype
(
np
.
float32
)
else
:
raise
AssertionError
()
else
:
if
attrs
[
0
][
"data_format"
]
==
"NCHW"
:
return
np
.
random
.
random
([
batch
,
3
,
16
,
32
]).
astype
(
np
.
float32
)
else
:
return
np
.
random
.
random
([
batch
,
16
,
32
,
3
]).
astype
(
np
.
float32
)
def
generate_alpha
(
attrs
:
List
[
Dict
[
str
,
Any
]]):
if
self
.
dims
==
0
:
return
np
.
random
.
random
([]).
astype
(
np
.
float32
)
if
attrs
[
0
][
"mode"
]
==
"all"
:
return
np
.
random
.
random
(
size
=
(
1
)).
astype
(
np
.
float32
)
elif
(
attrs
[
0
][
"mode"
]
==
"channel"
and
attrs
[
0
][
"data_format"
]
==
"NCHW"
):
shape
=
[
1
]
if
dim1
!=
0
:
shape
.
append
(
dim1
)
if
dim2
!=
0
:
shape
.
append
(
dim2
)
if
dim3
!=
0
:
shape
.
append
(
dim3
)
return
np
.
random
.
random
(
size
=
shape
[
1
]).
astype
(
np
.
float32
)
elif
(
attrs
[
0
][
"mode"
]
==
"channel"
and
attrs
[
0
][
"data_format"
]
==
"NHWC"
):
shape
=
[
1
]
if
dim1
!=
0
:
shape
.
append
(
dim1
)
if
dim2
!=
0
:
shape
.
append
(
dim2
)
if
dim3
!=
0
:
shape
.
append
(
dim3
)
return
np
.
random
.
random
(
size
=
shape
[
-
1
]).
astype
(
np
.
float32
)
return
np
.
random
.
random
([
1
]).
astype
(
np
.
float32
)
elif
attrs
[
0
][
"mode"
]
==
"channel"
:
return
np
.
random
.
random
([
3
]).
astype
(
np
.
float32
)
elif
attrs
[
0
][
"mode"
]
==
"element"
:
shape
=
[
1
]
if
dim1
!=
0
:
shape
.
append
(
dim1
)
if
dim2
!=
0
:
shape
.
append
(
dim2
)
if
dim3
!=
0
:
shape
.
append
(
dim3
)
return
np
.
random
.
random
(
size
=
shape
).
astype
(
np
.
float32
)
if
self
.
dims
==
1
:
return
np
.
random
.
random
([
16
]).
astype
(
np
.
float32
)
elif
self
.
dims
==
2
:
return
np
.
random
.
random
([
1
,
3
]).
astype
(
np
.
float32
)
elif
self
.
dims
==
3
:
if
attrs
[
0
][
"data_format"
]
==
"NCHW"
:
return
np
.
random
.
random
([
1
,
3
,
16
]).
astype
(
np
.
float32
)
elif
attrs
[
0
][
"data_format"
]
==
"NHWC"
:
return
np
.
random
.
random
([
1
,
16
,
3
]).
astype
(
np
.
float32
)
else
:
raise
AssertionError
()
else
:
if
attrs
[
0
][
"data_format"
]
==
"NCHW"
:
return
np
.
random
.
random
([
1
,
3
,
16
,
32
]).
astype
(
np
.
float32
)
elif
attrs
[
0
][
"data_format"
]
==
"NHWC"
:
return
np
.
random
.
random
([
1
,
16
,
32
,
3
]).
astype
(
np
.
float32
)
else
:
raise
AssertionError
()
for
batch
in
[
1
,
4
]:
for
dim1
in
[
0
,
3
]:
for
dim2
in
[
0
,
16
]:
for
dim3
in
[
0
,
32
]:
self
.
dim1
=
dim1
self
.
dim2
=
dim2
self
.
dim3
=
dim3
if
dim1
==
0
and
dim2
!=
0
:
for
dims
in
[
0
,
1
,
2
,
3
,
4
]:
for
mode
in
[
"all"
,
"element"
,
"channel"
]:
for
data_format
in
[
"NCHW"
,
"NHWC"
]:
if
(
mode
==
"element"
or
mode
==
"all"
)
and
dims
==
0
:
continue
if
dim1
==
0
and
dim2
==
0
and
dim3
!=
0
:
if
mode
==
"channel"
and
dims
!=
4
:
continue
for
mode
in
[
"all"
,
"channel"
,
"element"
]:
for
data_format
in
[
'NCHW'
,
'NHWC'
]:
if
(
mode
==
"channel"
and
dim1
==
0
and
data_format
==
"NCHW"
):
continue
if
(
mode
==
"channel"
and
dim3
==
0
and
data_format
==
"NHWC"
):
continue
dics
=
[
{
"mode"
:
mode
,
"data_format"
:
data_format
}
]
ops_config
=
[
{
"op_type"
:
"prelu"
,
"op_inputs"
:
{
"X"
:
[
"input_data"
],
"Alpha"
:
[
"alpha_weight"
],
},
"op_outputs"
:
{
"Out"
:
[
"output_data"
]},
"op_attrs"
:
dics
[
0
],
}
]
ops
=
self
.
generate_op_config
(
ops_config
)
program_config
=
ProgramConfig
(
ops
=
ops
,
weights
=
{
"alpha_weight"
:
TensorConfig
(
data_gen
=
partial
(
generate_alpha
,
dics
,
dim1
,
dim2
,
dim3
,
)
)
},
inputs
=
{
"input_data"
:
TensorConfig
(
data_gen
=
partial
(
generate_input
,
batch
,
dim1
,
dim2
,
dim3
,
)
),
},
outputs
=
[
"output_data"
],
self
.
dims
=
dims
dics
=
[{
"mode"
:
mode
,
"data_format"
:
data_format
}]
ops_config
=
[
{
"op_type"
:
"prelu"
,
"op_inputs"
:
{
"X"
:
[
"input_data"
],
"Alpha"
:
[
"alpha_weight"
],
},
"op_outputs"
:
{
"Out"
:
[
"output_data"
]},
"op_attrs"
:
dics
[
0
],
}
]
ops
=
self
.
generate_op_config
(
ops_config
)
program_config
=
ProgramConfig
(
ops
=
ops
,
weights
=
{
"alpha_weight"
:
TensorConfig
(
data_gen
=
partial
(
generate_alpha
,
dics
)
)
yield
program_config
},
inputs
=
{
"input_data"
:
TensorConfig
(
data_gen
=
partial
(
generate_input
,
dics
,
batch
)
),
},
outputs
=
[
"output_data"
],
)
yield
program_config
def
sample_predictor_configs
(
self
,
program_config
)
->
(
paddle_infer
.
Config
,
List
[
int
],
float
):
def
generate_dynamic_shape
(
attrs
):
if
self
.
dim1
==
0
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
],
}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
4
],
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
2
],
}
else
:
if
self
.
dim2
==
0
and
self
.
dim3
==
0
:
if
self
.
dims
==
0
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[]}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[]}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[]}
elif
self
.
dims
==
1
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
16
]}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
16
]}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
16
]}
elif
self
.
dims
==
2
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
,
3
]}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
1
,
3
]}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
1
,
3
]}
elif
self
.
dims
==
3
:
if
attrs
[
0
][
"data_format"
]
==
"NCHW"
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
,
1
],
"input_data"
:
[
1
,
3
,
16
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
4
,
3
2
],
"input_data"
:
[
4
,
3
,
16
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
2
,
3
],
"input_data"
:
[
1
,
3
,
16
]
}
elif
self
.
dim2
!=
0
and
self
.
dim3
!=
0
:
elif
attrs
[
0
][
"data_format"
]
==
"NHWC"
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
,
1
,
1
,
1
],
"input_data"
:
[
1
,
1
6
,
3
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
4
,
3
,
16
,
32
],
"input_data"
:
[
4
,
16
,
3
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
2
,
3
,
16
,
32
],
"input_data"
:
[
1
,
16
,
3
]
}
elif
self
.
dim3
==
0
:
else
:
raise
AssertionError
()
else
:
if
attrs
[
0
][
"data_format"
]
==
"NCHW"
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
,
1
,
1
],
"input_data"
:
[
1
,
3
,
16
,
32
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
4
,
3
,
32
],
"input_data"
:
[
4
,
3
,
16
,
32
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
2
,
3
,
16
],
"input_data"
:
[
1
,
3
,
16
,
32
]
}
elif
attrs
[
0
][
"data_format"
]
==
"NHWC"
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
,
16
,
32
,
3
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
4
,
16
,
32
,
3
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
1
,
16
,
32
,
3
]
}
else
:
raise
AssertionError
()
def
clear_dynamic_shape
():
self
.
dynamic_shape
.
max_input_shape
=
{}
...
...
@@ -203,12 +198,7 @@ class TrtConvertPreluTest(TrtLayerAutoScanTest):
]
def
generate_trt_nodes_num
(
attrs
,
dynamic_shape
):
if
(
not
dynamic_shape
and
self
.
dim1
==
0
and
self
.
dim2
==
0
and
self
.
dim3
==
0
):
if
not
dynamic_shape
and
(
self
.
dims
==
1
or
self
.
dims
==
0
):
return
0
,
3
return
1
,
2
...
...
@@ -234,23 +224,7 @@ class TrtConvertPreluTest(TrtLayerAutoScanTest):
attrs
,
True
),
(
1e-3
,
1e-3
)
def
add_skip_trt_case
(
self
):
ver
=
paddle_infer
.
get_trt_compile_version
()
if
ver
[
0
]
*
1000
+
ver
[
1
]
*
100
+
ver
[
0
]
*
10
<
7000
:
def
teller
(
program_config
,
predictor_config
):
if
not
predictor_config
.
tensorrt_dynamic_shape_enabled
():
return
True
return
False
self
.
add_skip_case
(
teller
,
SkipReasons
.
TRT_NOT_IMPLEMENTED
,
"Need to repair the case: the output of GPU and tensorrt has diff in trt6, the prelu static plugin has bug."
,
)
def
test
(
self
):
self
.
add_skip_trt_case
()
self
.
run_test
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录