Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
d6be9000
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
d6be9000
编写于
11月 16, 2022
作者:
X
xiaoxiaohehe001
提交者:
GitHub
11月 16, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Paddle Inference] Add fill_any_like trt converter. (#47974)
* add_fill_any_like * add_fill_any_like
上级
b4b78060
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
309 addition
and
0 deletion
+309
-0
paddle/fluid/inference/api/analysis_predictor.cc
paddle/fluid/inference/api/analysis_predictor.cc
+1
-0
paddle/fluid/inference/tensorrt/convert/CMakeLists.txt
paddle/fluid/inference/tensorrt/convert/CMakeLists.txt
+1
-0
paddle/fluid/inference/tensorrt/convert/fill_any_like_op.cc
paddle/fluid/inference/tensorrt/convert/fill_any_like_op.cc
+93
-0
paddle/fluid/inference/tensorrt/op_teller.cc
paddle/fluid/inference/tensorrt/op_teller.cc
+24
-0
python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_fill_any_like.py
.../unittests/ir/inference/test_trt_convert_fill_any_like.py
+190
-0
未找到文件。
paddle/fluid/inference/api/analysis_predictor.cc
浏览文件 @
d6be9000
...
...
@@ -2259,6 +2259,7 @@ USE_TRT_CONVERTER(pad);
USE_TRT_CONVERTER
(
hard_sigmoid
);
USE_TRT_CONVERTER
(
hard_swish
);
USE_TRT_CONVERTER
(
split
);
USE_TRT_CONVERTER
(
fill_any_like
);
USE_TRT_CONVERTER
(
prelu
);
USE_TRT_CONVERTER
(
conv2d_transpose
);
USE_TRT_CONVERTER
(
leaky_relu
);
...
...
paddle/fluid/inference/tensorrt/convert/CMakeLists.txt
浏览文件 @
d6be9000
...
...
@@ -25,6 +25,7 @@ list(
multihead_matmul_op.cc
multihead_matmul_roformer_op.cc
shuffle_channel_op.cc
fill_any_like_op.cc
where_op.cc
swish_op.cc
silu_op.cc
...
...
paddle/fluid/inference/tensorrt/convert/fill_any_like_op.cc
0 → 100644
浏览文件 @
d6be9000
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/inference/tensorrt/convert/op_converter.h"
namespace
paddle
{
namespace
framework
{
class
Scope
;
namespace
proto
{
class
OpDesc
;
}
// namespace proto
}
// namespace framework
}
// namespace paddle
namespace
paddle
{
namespace
inference
{
namespace
tensorrt
{
class
FillAnyLikeOpConverter
:
public
OpConverter
{
public:
void
operator
()(
const
framework
::
proto
::
OpDesc
&
op
,
const
framework
::
Scope
&
scope
,
bool
test_mode
)
override
{
VLOG
(
3
)
<<
"convert fill_any_like op to tensorrt layer "
;
framework
::
OpDesc
op_desc
(
op
,
nullptr
);
auto
*
input
=
engine_
->
GetITensor
(
op_desc
.
Input
(
"X"
).
front
());
auto
output_name
=
op_desc
.
Output
(
"Out"
).
front
();
auto
input_dims
=
input
->
getDimensions
();
auto
nbDims_num
=
input_dims
.
nbDims
;
nvinfer1
::
ITensor
*
value_tensor
;
const
int
dtype
=
PADDLE_GET_CONST
(
int
,
op_desc
.
GetAttr
(
"dtype"
));
float
value
=
PADDLE_GET_CONST
(
float
,
op_desc
.
GetAttr
(
"value"
));
if
((
dtype
==
2
)
||
(
dtype
==
-
1
&&
input
->
getType
()
==
nvinfer1
::
DataType
::
kINT32
))
{
value_tensor
=
Add1DConstantLayer
(
static_cast
<
int32_t
>
(
value
),
output_name
+
"_value_tensor_"
);
}
else
{
value_tensor
=
Add1DConstantLayer
(
value
,
output_name
+
"_value_tensor_"
);
}
auto
shape_tensor
=
Shape
(
input
);
auto
*
one_rank_tensor
=
Add1DConstantLayer
(
std
::
vector
<
int32_t
>
(
nbDims_num
,
1
),
output_name
+
"_one_rank_tensor_"
);
auto
input_shape_tensor
=
one_rank_tensor
;
auto
*
shuffle
=
TRT_ENGINE_ADD_LAYER
(
engine_
,
Shuffle
,
*
value_tensor
);
shuffle
->
setInput
(
1
,
*
input_shape_tensor
);
std
::
vector
<
int32_t
>
start_vec
(
nbDims_num
,
0
);
nvinfer1
::
Dims
start
;
start
.
nbDims
=
nbDims_num
;
for
(
int32_t
i
=
0
;
i
<
nbDims_num
;
++
i
)
{
start
.
d
[
i
]
=
start_vec
[
i
];
}
nvinfer1
::
Dims
size
;
size
.
nbDims
=
nbDims_num
;
nvinfer1
::
Dims
stride
;
stride
.
nbDims
=
nbDims_num
;
auto
starts_tensor
=
Add1DConstantLayer
(
start_vec
,
output_name
+
"_start_tensor_"
);
auto
one_tensor
=
Add1DConstantLayer
(
1
,
output_name
+
"_one_tensor_"
);
auto
sizes_tensor
=
Max
(
input_shape_tensor
,
shape_tensor
);
auto
input_sub_tensor
=
Sub
(
input_shape_tensor
,
one_tensor
);
auto
strides_tensor
=
Min
(
one_tensor
,
input_sub_tensor
);
auto
layer
=
TRT_ENGINE_ADD_LAYER
(
engine_
,
Slice
,
*
shuffle
->
getOutput
(
0
),
start
,
size
,
stride
);
layer
->
setInput
(
1
,
*
starts_tensor
);
layer
->
setInput
(
2
,
*
sizes_tensor
);
layer
->
setInput
(
3
,
*
strides_tensor
);
RreplenishLayerAndOutput
(
layer
,
"fill_any_like"
,
{
output_name
},
test_mode
);
}
};
}
// namespace tensorrt
}
// namespace inference
}
// namespace paddle
REGISTER_TRT_OP_CONVERTER
(
fill_any_like
,
FillAnyLikeOpConverter
);
paddle/fluid/inference/tensorrt/op_teller.cc
浏览文件 @
d6be9000
...
...
@@ -1161,6 +1161,28 @@ struct SimpleOpTypeSetTeller : public Teller {
}
}
if
(
op_type
==
"fill_any_like"
)
{
if
(
!
with_dynamic_shape
)
{
VLOG
(
3
)
<<
"the fill_any_like does not support static shape yet"
;
return
false
;
}
int
dtype
=
PADDLE_GET_CONST
(
int
,
desc
.
GetAttr
(
"dtype"
));
if
(
dtype
!=
-
1
&&
dtype
!=
2
&&
dtype
!=
5
)
{
VLOG
(
3
)
<<
"the fill_any_like only supports int32 and float32"
;
return
false
;
}
if
(
dtype
==
-
1
)
{
auto
*
block
=
desc
.
Block
();
auto
*
x_var_desc
=
block
->
FindVar
(
desc
.
Input
(
"X"
)[
0
]);
auto
input_type
=
x_var_desc
->
GetDataType
();
if
(
input_type
!=
framework
::
proto
::
VarType
::
INT32
&&
input_type
!=
framework
::
proto
::
VarType
::
FP32
)
{
VLOG
(
3
)
<<
"the fill_any_like only supports int32 and float32"
;
return
false
;
}
}
}
if
(
op_type
==
"slice"
)
{
if
(
desc
.
HasAttr
(
"decrease_axis"
))
{
std
::
vector
<
int
>
decrease_axis
=
...
...
@@ -2291,6 +2313,7 @@ struct SimpleOpTypeSetTeller : public Teller {
"elementwise_floordiv"
,
"equal"
,
"dropout"
,
"fill_any_like"
,
"prelu"
,
"conv2d_transpose"
,
"depthwise_conv2d_transpose"
,
...
...
@@ -2417,6 +2440,7 @@ struct SimpleOpTypeSetTeller : public Teller {
"elementwise_floordiv"
,
"equal"
,
"dropout"
,
"fill_any_like"
,
"prelu"
,
"conv2d_transpose"
,
"depthwise_conv2d_transpose"
,
...
...
python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_fill_any_like.py
0 → 100644
浏览文件 @
d6be9000
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
trt_layer_auto_scan_test
import
TrtLayerAutoScanTest
from
program_config
import
TensorConfig
,
ProgramConfig
import
numpy
as
np
import
paddle.inference
as
paddle_infer
from
functools
import
partial
from
typing
import
List
,
Dict
,
Any
import
unittest
class
TrtConvertExpandV2Test
(
TrtLayerAutoScanTest
):
def
is_program_valid
(
self
,
program_config
:
ProgramConfig
)
->
bool
:
if
self
.
dtype
in
[
0
,
3
,
4
]:
return
False
if
self
.
dims
!=
4
and
self
.
dtype
!=
2
:
return
False
return
True
def
sample_program_configs
(
self
):
def
generate_input1
(
attrs
:
List
[
Dict
[
str
,
Any
]]):
if
self
.
dims
==
4
:
self
.
input_shape
=
[
1
,
1
,
4
,
6
]
if
self
.
dtype
==
0
:
return
np
.
random
.
random
([
1
,
1
,
4
,
6
]).
astype
(
np
.
bool
)
elif
self
.
dtype
==
2
or
self
.
dtype
==
-
1
:
return
np
.
random
.
random
([
1
,
1
,
4
,
6
]).
astype
(
np
.
int32
)
elif
self
.
dtype
==
3
:
return
np
.
random
.
random
([
1
,
1
,
4
,
6
]).
astype
(
np
.
int64
)
elif
self
.
dtype
==
4
:
return
np
.
random
.
random
([
1
,
1
,
4
,
6
]).
astype
(
np
.
float16
)
else
:
return
np
.
random
.
random
([
1
,
1
,
4
,
6
]).
astype
(
np
.
float32
)
elif
self
.
dims
==
3
:
self
.
input_shape
=
[
1
,
8
,
6
]
return
np
.
random
.
random
([
1
,
8
,
6
]).
astype
(
np
.
int32
)
elif
self
.
dims
==
2
:
self
.
input_shape
=
[
1
,
48
]
return
np
.
random
.
random
([
1
,
48
]).
astype
(
np
.
int32
)
elif
self
.
dims
==
1
:
self
.
input_shape
=
[
48
]
return
np
.
random
.
random
([
48
]).
astype
(
np
.
int32
)
def
generate_weight1
(
attrs
:
List
[
Dict
[
str
,
Any
]]):
return
np
.
array
([
1
,
48
]).
astype
(
np
.
int32
)
def
generate_shapeT1_data
(
attrs
:
List
[
Dict
[
str
,
Any
]]):
return
np
.
array
([
2
]).
astype
(
np
.
int32
)
def
generate_shapeT2_data
(
attrs
:
List
[
Dict
[
str
,
Any
]]):
return
np
.
array
([
24
]).
astype
(
np
.
int32
)
for
dims
in
[
1
,
2
,
3
,
4
]:
for
value
in
[
2
]:
for
dtype
in
[
-
1
,
0
,
2
,
3
,
4
,
5
]:
dics
=
[
{
"value"
:
value
,
"dtype"
:
dtype
,
},
]
self
.
dims
=
dims
self
.
dtype
=
dtype
dics_intput
=
[{
"X"
:
[
"fill_any_like_input"
]}]
ops_config
=
[
{
"op_type"
:
"fill_any_like"
,
"op_inputs"
:
dics_intput
[
0
],
"op_outputs"
:
{
"Out"
:
[
"fill_any_like_out"
]},
"op_attrs"
:
dics
[
0
],
}
]
ops
=
self
.
generate_op_config
(
ops_config
)
program_config
=
ProgramConfig
(
ops
=
ops
,
weights
=
{},
inputs
=
{
"fill_any_like_input"
:
TensorConfig
(
data_gen
=
partial
(
generate_input1
,
dics
)
)
},
outputs
=
[
"fill_any_like_out"
],
)
yield
program_config
def
sample_predictor_configs
(
self
,
program_config
)
->
(
paddle_infer
.
Config
,
List
[
int
],
int
):
def
generate_dynamic_shape
(
attrs
):
if
self
.
dims
==
4
:
self
.
dynamic_shape
.
min_input_shape
=
{
"fill_any_like_input"
:
[
1
,
1
,
4
,
6
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"fill_any_like_input"
:
[
10
,
1
,
4
,
6
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"fill_any_like_input"
:
[
1
,
1
,
4
,
6
]
}
elif
self
.
dims
==
3
:
self
.
dynamic_shape
.
min_input_shape
=
{
"fill_any_like_input"
:
[
1
,
8
,
6
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"fill_any_like_input"
:
[
4
,
8
,
6
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"fill_any_like_input"
:
[
1
,
8
,
6
]
}
elif
self
.
dims
==
2
:
self
.
dynamic_shape
.
min_input_shape
=
{
"fill_any_like_input"
:
[
1
,
48
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"fill_any_like_input"
:
[
4
,
48
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"fill_any_like_input"
:
[
1
,
48
]
}
elif
self
.
dims
==
1
:
self
.
dynamic_shape
.
min_input_shape
=
{
"fill_any_like_input"
:
[
48
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"fill_any_like_input"
:
[
48
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"fill_any_like_input"
:
[
48
]
}
def
clear_dynamic_shape
():
self
.
dynamic_shape
.
min_input_shape
=
{}
self
.
dynamic_shape
.
max_input_shape
=
{}
self
.
dynamic_shape
.
opt_input_shape
=
{}
def
generate_trt_nodes_num
(
attrs
,
dynamic_shape
):
if
not
dynamic_shape
:
return
0
,
3
else
:
return
1
,
2
attrs
=
[
program_config
.
ops
[
i
].
attrs
for
i
in
range
(
len
(
program_config
.
ops
))
]
clear_dynamic_shape
()
self
.
trt_param
.
precision
=
paddle_infer
.
PrecisionType
.
Float32
yield
self
.
create_inference_config
(),
generate_trt_nodes_num
(
attrs
,
False
),
1e-5
self
.
trt_param
.
precision
=
paddle_infer
.
PrecisionType
.
Half
yield
self
.
create_inference_config
(),
generate_trt_nodes_num
(
attrs
,
False
),
1e-5
# for dynamic_shape
generate_dynamic_shape
(
attrs
)
self
.
trt_param
.
precision
=
paddle_infer
.
PrecisionType
.
Float32
yield
self
.
create_inference_config
(),
generate_trt_nodes_num
(
attrs
,
True
),
1e-5
self
.
trt_param
.
precision
=
paddle_infer
.
PrecisionType
.
Half
yield
self
.
create_inference_config
(),
generate_trt_nodes_num
(
attrs
,
True
),
1e-5
def
add_skip_trt_case
(
self
):
pass
def
test
(
self
):
self
.
add_skip_trt_case
()
self
.
run_test
()
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录