Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
00f747f2
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
未验证
提交
00f747f2
编写于
4月 25, 2023
作者:
Y
Yuanle Liu
提交者:
GitHub
4月 25, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Paddle Inference] add generic plugin for p_norm (#53278)
上级
f6f48780
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
177 addition
and
11 deletion
+177
-11
paddle/fluid/inference/tensorrt/dynamic_shape_infermeta.cc
paddle/fluid/inference/tensorrt/dynamic_shape_infermeta.cc
+41
-10
paddle/fluid/inference/tensorrt/dynamic_shape_infermeta_registry.h
...uid/inference/tensorrt/dynamic_shape_infermeta_registry.h
+1
-0
paddle/fluid/inference/tensorrt/plugin/generic_plugin.cu
paddle/fluid/inference/tensorrt/plugin/generic_plugin.cu
+1
-1
test/ir/inference/test_trt_convert_p_norm.py
test/ir/inference/test_trt_convert_p_norm.py
+134
-0
未找到文件。
paddle/fluid/inference/tensorrt/dynamic_shape_infermeta.cc
浏览文件 @
00f747f2
...
...
@@ -14,7 +14,7 @@
#include "paddle/fluid/inference/tensorrt/dynamic_shape_infermeta_factory.h"
#include "paddle/fluid/inference/tensorrt/helper.h"
#include "paddle/
fluid/platform
/enforce.h"
#include "paddle/
phi/core
/enforce.h"
#include "paddle/phi/kernels/funcs/unfold_functor.h"
namespace
paddle
{
...
...
@@ -322,20 +322,51 @@ nvinfer1::DimsExprs PNormInferMeta(
int
nb_inputs
,
nvinfer1
::
IExprBuilder
&
expr_builder
,
// NOLINT
const
framework
::
OpDesc
&
op_desc
)
{
const
nvinfer1
::
DimsExprs
x_dim
=
inputs
[
0
];
std
::
vector
<
const
nvinfer1
::
IDimensionExpr
*>
reduce_dims
;
std
::
vector
<
const
nvinfer1
::
IDimensionExpr
*>
keep_dims
;
bool
asvector
=
PADDLE_GET_CONST
(
bool
,
op_desc
.
GetAttr
(
"asvector"
));
bool
keepdim
=
PADDLE_GET_CONST
(
bool
,
op_desc
.
GetAttr
(
"keepdim"
));
int
axis
=
PADDLE_GET_CONST
(
int
,
op_desc
.
GetAttr
(
"axis"
));
auto
x_dim
=
inputs
[
0
];
auto
x_rank
=
x_dim
.
nbDims
;
PADDLE_ENFORCE_GE
(
axis
,
-
x_rank
,
phi
::
errors
::
InvalidArgument
(
"Attr(axis) value should be in range [-R, R-1], R is "
"the rank of Input(X). But received axis: %d, R: %d. "
"Current Input(X)'s shape is=[%s]."
,
axis
,
x_rank
,
x_dim
.
d
));
PADDLE_ENFORCE_LT
(
axis
,
x_rank
,
phi
::
errors
::
InvalidArgument
(
"Attr(axis) value should be in range [-R, R-1], R is "
"the rank of Input(X). But received axis: %d, R: %d. "
"Current Input(X)'s shape is=[%s]."
,
axis
,
x_rank
,
x_dim
.
d
));
// TODO(liuyuanle): support asvector = True
PADDLE_ENFORCE_EQ
(
asvector
,
false
,
phi
::
errors
::
InvalidArgument
(
"p_norm only support asvector=false, but received asvector: %d."
,
asvector
));
std
::
vector
<
const
nvinfer1
::
IDimensionExpr
*>
reduce_dims
;
if
(
asvector
)
{
reduce_dims
.
emplace_back
(
expr_builder
.
constant
(
1
));
keep_dims
.
emplace_back
(
expr_builder
.
constant
(
1
));
if
(
keepdim
)
{
for
(
int
i
=
1
;
i
<
x_dim
.
nbDims
;
++
i
)
{
keep_dims
.
emplace_back
(
expr_builder
.
constant
(
1
));
reduce_dims
.
emplace_back
(
expr_builder
.
constant
(
1
));
}
x_dim
.
nbDims
=
reduce_dims
.
size
();
for
(
size_t
i
=
0
;
i
<
reduce_dims
.
size
();
i
++
)
{
x_dim
.
d
[
i
]
=
reduce_dims
[
i
];
}
}
}
else
{
...
...
@@ -347,12 +378,11 @@ nvinfer1::DimsExprs PNormInferMeta(
reduce_dims
.
emplace_back
(
expr_builder
.
constant
(
1
));
}
}
keep_dims
[
axis
]
=
expr_builder
.
constant
(
1
);
x_dim
.
d
[
axis
]
=
expr_builder
.
constant
(
1
);
nvinfer1
::
DimsExprs
output
;
if
(
keepdim
)
{
output
.
nbDims
=
keep_dims
.
size
();
for
(
int
i
=
0
;
i
<
output
.
nbDims
;
i
++
)
output
.
d
[
i
]
=
keep_dims
[
i
];
output
=
x_dim
;
}
else
{
output
.
nbDims
=
reduce_dims
.
size
();
for
(
int
i
=
0
;
i
<
output
.
nbDims
;
i
++
)
output
.
d
[
i
]
=
reduce_dims
[
i
];
...
...
@@ -396,6 +426,7 @@ PD_REGISTER_DYNAMIC_INFER_META_FN(inverse, UnchangedInferMeta);
PD_REGISTER_DYNAMIC_INFER_META_FN
(
moe
,
MoeInferMeta
);
PD_REGISTER_DYNAMIC_INFER_META_FN
(
pad3d
,
Pad3dInferMeta
);
PD_REGISTER_DYNAMIC_INFER_META_FN
(
grid_sampler
,
GridSamplerInferMeta
);
PD_REGISTER_DYNAMIC_INFER_META_FN
(
p_norm
,
PNormInferMeta
);
}
// namespace tensorrt
}
// namespace inference
}
// namespace paddle
paddle/fluid/inference/tensorrt/dynamic_shape_infermeta_registry.h
浏览文件 @
00f747f2
...
...
@@ -28,6 +28,7 @@ USE_TRT_DYNAMIC_INFER_META_FN(scatter_nd_add);
USE_TRT_DYNAMIC_INFER_META_FN
(
pad3d
);
USE_TRT_DYNAMIC_INFER_META_FN
(
inverse
);
USE_TRT_DYNAMIC_INFER_META_FN
(
grid_sampler
);
USE_TRT_DYNAMIC_INFER_META_FN
(
p_norm
);
}
// namespace tensorrt
}
// namespace inference
}
// namespace paddle
paddle/fluid/inference/tensorrt/plugin/generic_plugin.cu
浏览文件 @
00f747f2
...
...
@@ -472,7 +472,7 @@ int GenericPlugin::enqueue(const nvinfer1::PluginTensorDesc* input_desc,
cudaStream_t
stream
)
TRT_NOEXCEPT
{
platform
::
CUDAPlace
place
(
platform
::
GetCurrentDeviceId
());
// [TODO]now generic plugin do not support
FP16 and
INT8 precision
// [TODO]now generic plugin do not support INT8 precision
auto
protoType2PhiType
=
[
&
](
int
proto_type
,
nvinfer1
::
DataType
nv_dtype
)
->
std
::
pair
<
phi
::
DataType
,
int
>
{
...
...
test/ir/inference/test_trt_convert_p_norm.py
0 → 100644
浏览文件 @
00f747f2
# Copyright (c) 2023 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.
import
unittest
from
functools
import
partial
from
typing
import
Any
,
Dict
,
List
import
numpy
as
np
from
program_config
import
ProgramConfig
,
TensorConfig
from
trt_layer_auto_scan_test
import
TrtLayerAutoScanTest
import
paddle.inference
as
paddle_infer
class
TrtConvertCeluTest
(
TrtLayerAutoScanTest
):
def
sample_program_configs
(
self
):
def
generate_input1
(
dims
,
attrs
:
List
[
Dict
[
str
,
Any
]]):
if
dims
==
1
:
return
np
.
ones
([
3
]).
astype
(
np
.
float32
)
elif
dims
==
2
:
return
np
.
ones
([
3
,
64
]).
astype
(
np
.
float32
)
elif
dims
==
3
:
return
np
.
ones
([
3
,
64
,
64
]).
astype
(
np
.
float32
)
else
:
return
np
.
ones
([
1
,
3
,
64
,
64
]).
astype
(
np
.
float32
)
for
dims
in
[
2
,
3
,
4
]:
# TODO(liuyuanle): support asvector = True
for
asvector
in
[
False
]:
for
keepdim
in
[
False
,
True
]:
for
porder
in
[
0
,
1
,
2
,
3
]:
for
axis
in
[
-
1
]:
self
.
dims
=
dims
dics
=
[
{
"asvector"
:
asvector
,
"keepdim"
:
keepdim
,
"axis"
:
axis
,
"porder"
:
porder
,
}
]
ops_config
=
[
{
"op_type"
:
"p_norm"
,
"op_inputs"
:
{
"X"
:
[
"input_data"
],
},
"op_outputs"
:
{
"Out"
:
[
"output_data"
]},
"op_attrs"
:
dics
[
0
],
}
]
ops
=
self
.
generate_op_config
(
ops_config
)
program_config
=
ProgramConfig
(
ops
=
ops
,
weights
=
{},
inputs
=
{
"input_data"
:
TensorConfig
(
data_gen
=
partial
(
generate_input1
,
dims
,
dics
)
)
},
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
.
dims
==
1
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
]}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
128
]}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
64
]}
elif
self
.
dims
==
2
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
,
32
]}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
4
,
64
]}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
3
,
64
]}
elif
self
.
dims
==
3
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
,
32
,
32
]}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
10
,
64
,
64
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
3
,
64
,
64
]}
else
:
self
.
dynamic_shape
.
min_input_shape
=
{
"input_data"
:
[
1
,
3
,
32
,
32
]
}
self
.
dynamic_shape
.
max_input_shape
=
{
"input_data"
:
[
4
,
3
,
64
,
64
]
}
self
.
dynamic_shape
.
opt_input_shape
=
{
"input_data"
:
[
1
,
3
,
64
,
64
]
}
def
generate_trt_nodes_num
(
attrs
,
dynamic_shape
):
return
1
,
2
attrs
=
[
program_config
.
ops
[
i
].
attrs
for
i
in
range
(
len
(
program_config
.
ops
))
]
# for dynamic_shape mode
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-3
,
1e-3
)
def
test
(
self
):
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录