Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
4a69a536
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
4a69a536
编写于
5月 11, 2023
作者:
Z
Zhang Jun
提交者:
GitHub
5月 11, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[inference][trt]add trt sparse weights switch (#53562)
上级
04e5e7b7
变更
11
显示空白变更内容
内联
并排
Showing
11 changed file
with
51 addition
and
16 deletion
+51
-16
paddle/fluid/inference/analysis/argument.h
paddle/fluid/inference/analysis/argument.h
+3
-0
paddle/fluid/inference/analysis/ir_pass_manager.cc
paddle/fluid/inference/analysis/ir_pass_manager.cc
+2
-0
paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc
...id/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc
+8
-9
paddle/fluid/inference/api/analysis_config.cc
paddle/fluid/inference/api/analysis_config.cc
+5
-0
paddle/fluid/inference/api/analysis_predictor.cc
paddle/fluid/inference/api/analysis_predictor.cc
+1
-0
paddle/fluid/inference/api/paddle_analysis_config.h
paddle/fluid/inference/api/paddle_analysis_config.h
+4
-0
paddle/fluid/inference/tensorrt/engine.cc
paddle/fluid/inference/tensorrt/engine.cc
+4
-7
paddle/fluid/inference/tensorrt/engine.h
paddle/fluid/inference/tensorrt/engine.h
+4
-0
paddle/fluid/inference/tensorrt/helper.h
paddle/fluid/inference/tensorrt/helper.h
+4
-0
paddle/fluid/pybind/inference_api.cc
paddle/fluid/pybind/inference_api.cc
+4
-0
test/ir/inference/test_trt_inference_predictor.py
test/ir/inference/test_trt_inference_predictor.py
+12
-0
未找到文件。
paddle/fluid/inference/analysis/argument.h
浏览文件 @
4a69a536
...
...
@@ -250,6 +250,9 @@ struct Argument {
TensorRtAllowBuildAtRuntime
,
bool
);
DECL_ARGUMENT_FIELD
(
tensorrt_use_inspector
,
TensorRtUseInspector
,
bool
);
DECL_ARGUMENT_FIELD
(
tensorrt_use_sparse_weights
,
TensorRtUseSparseWeights
,
bool
);
DECL_ARGUMENT_FIELD
(
use_dlnne
,
UseDlnne
,
bool
);
DECL_ARGUMENT_FIELD
(
dlnne_min_subgraph_size
,
DlnneMinSubgraphSize
,
int
);
...
...
paddle/fluid/inference/analysis/ir_pass_manager.cc
浏览文件 @
4a69a536
...
...
@@ -213,6 +213,8 @@ void IRPassManager::CreatePasses(Argument *argument,
pass
->
Set
(
"use_static_engine"
,
new
bool
(
use_static_engine
));
pass
->
Set
(
"model_from_memory"
,
new
bool
(
argument
->
model_from_memory
()));
pass
->
Set
(
"use_inspector"
,
new
bool
(
argument
->
tensorrt_use_inspector
()));
pass
->
Set
(
"use_sparse_weights"
,
new
bool
(
argument
->
tensorrt_use_sparse_weights
()));
// tuned trt dynamic_shape
pass
->
Set
(
"trt_shape_range_info_path"
,
...
...
paddle/fluid/inference/analysis/ir_passes/tensorrt_subgraph_pass.cc
浏览文件 @
4a69a536
...
...
@@ -523,6 +523,7 @@ std::string TensorRtSubgraphPass::CreateTensorRTOp(
op_desc
->
SetAttr
(
"allow_build_at_runtime"
,
allow_build_at_runtime
);
op_desc
->
SetAttr
(
"shape_range_info_path"
,
shape_range_info_path
);
op_desc
->
SetAttr
(
"use_inspector"
,
Get
<
bool
>
(
"use_inspector"
));
op_desc
->
SetAttr
(
"use_sparse_weights"
,
Get
<
bool
>
(
"use_sparse_weights"
));
op_desc
->
SetAttr
(
"model_precision"
,
Get
<
int
>
(
"model_precision"
));
op_desc
->
SetAttr
(
"with_dynamic_shape"
,
with_dynamic_shape
);
...
...
@@ -614,17 +615,14 @@ std::string TensorRtSubgraphPass::CreateTensorRTOp(
opt_input_shape
=
{};
}
auto
to_major_version
=
[
&
](
int
full_version
)
->
float
{
return
(
full_version
/
100
)
/
10.0
;
};
const
float
compile_time_trt_version
=
to_major_version
(
TRT_VERSION
);
const
float
run_time_trt_version
=
to_major_version
(
tensorrt
::
GetInferLibVersion
());
if
(
compile_time_trt_version
!=
run_time_trt_version
)
{
const
float
trt_compile_version
=
tensorrt
::
TrtMajorVersion
(
TRT_VERSION
);
const
float
trt_runtime_version
=
tensorrt
::
TrtMajorVersion
(
tensorrt
::
GetInferLibVersion
());
if
(
trt_compile_version
!=
trt_runtime_version
)
{
LOG_FIRST_N
(
WARNING
,
1
)
<<
"The Paddle Inference library is compiled with "
<<
compile_time_trt
_version
<<
" version TensorRT, "
<<
"but the runtime TensorRT you are using is "
<<
run_time_trt
_version
<<
trt_compile
_version
<<
" version TensorRT, "
<<
"but the runtime TensorRT you are using is "
<<
trt_runtime
_version
<<
" version. "
"This might cause serious compatibility issues. We strongly "
"recommend using the same TRT version at runtime."
;
...
...
@@ -666,6 +664,7 @@ std::string TensorRtSubgraphPass::CreateTensorRTOp(
trt_engine
->
SetUseDLA
(
Get
<
bool
>
(
"trt_use_dla"
));
trt_engine
->
SetDLACore
(
Get
<
int
>
(
"trt_dla_core"
));
trt_engine
->
SetUseInspector
(
Get
<
bool
>
(
"use_inspector"
));
trt_engine
->
SetUseSparseWeights
(
Get
<
bool
>
(
"use_sparse_weights"
));
trt_engine
->
SetWithErnie
(
graph
->
Has
(
framework
::
ir
::
kEmbEltwiseLayernormPass
)
&&
graph
->
Has
(
framework
::
ir
::
kMultiheadMatmulPass
));
...
...
paddle/fluid/inference/api/analysis_config.cc
浏览文件 @
4a69a536
...
...
@@ -451,6 +451,7 @@ AnalysisConfig::AnalysisConfig(const AnalysisConfig &other) {
CP_MEMBER
(
collect_shape_range_info_
);
CP_MEMBER
(
shape_range_info_path_
);
CP_MEMBER
(
trt_use_inspector_
);
CP_MEMBER
(
trt_use_sparse_weights_
);
CP_MEMBER
(
trt_engine_memory_sharing_
);
CP_MEMBER
(
trt_engine_memory_sharing_identifier_
);
// Dlnne related
...
...
@@ -805,6 +806,10 @@ void AnalysisConfig::EnableTensorRtDLA(int dla_core) {
void
AnalysisConfig
::
EnableTensorRtInspector
()
{
trt_use_inspector_
=
true
;
}
void
AnalysisConfig
::
EnableTensorRtSparseWeights
()
{
trt_use_sparse_weights_
=
true
;
}
void
AnalysisConfig
::
Exp_DisableTensorRtOPs
(
const
std
::
vector
<
std
::
string
>
&
ops
)
{
trt_disabled_ops_
.
insert
(
trt_disabled_ops_
.
end
(),
ops
.
begin
(),
ops
.
end
());
...
...
paddle/fluid/inference/api/analysis_predictor.cc
浏览文件 @
4a69a536
...
...
@@ -1358,6 +1358,7 @@ void AnalysisPredictor::PrepareArgument() {
argument_
->
SetTensorRtAllowBuildAtRuntime
(
config_
.
trt_allow_build_at_runtime
());
argument_
->
SetTensorRtUseInspector
(
config_
.
trt_use_inspector_
);
argument_
->
SetTensorRtUseSparseWeights
(
config_
.
trt_use_sparse_weights_
);
argument_
->
SetTrtEngineMemorySharing
(
config_
.
trt_engine_memory_sharing
());
}
...
...
paddle/fluid/inference/api/paddle_analysis_config.h
浏览文件 @
4a69a536
...
...
@@ -742,6 +742,9 @@ struct PD_INFER_DECL AnalysisConfig {
void
EnableTensorRtInspector
();
bool
tensorrt_inspector_enabled
()
{
return
trt_use_inspector_
;
}
void
EnableTensorRtSparseWeights
();
bool
tensorrt_sparse_weights_enabled
()
{
return
trt_use_sparse_weights_
;
}
void
EnableDlnne
(
int
min_subgraph_size
=
3
,
int
max_batch_size
=
1
,
...
...
@@ -1118,6 +1121,7 @@ struct PD_INFER_DECL AnalysisConfig {
// tune to get dynamic_shape info.
bool
trt_tuned_dynamic_shape_
{
false
};
bool
trt_use_inspector_
{
false
};
bool
trt_use_sparse_weights_
{
false
};
// In CollectShapeInfo mode, we will collect the shape information of
// all intermediate tensors in the compute graph and calculate the
...
...
paddle/fluid/inference/tensorrt/engine.cc
浏览文件 @
4a69a536
...
...
@@ -207,12 +207,6 @@ void TensorRTEngine::FreezeNetwork() {
infer_builder_config_
->
setMaxWorkspaceSize
(
max_workspace_
);
#endif
#if IS_TRT_VERSION_GE(8500)
infer_builder_config_
->
setPreviewFeature
(
nvinfer1
::
PreviewFeature
::
kFASTER_DYNAMIC_SHAPES_0805
,
true
);
#else
#endif
bool
enable_fp16
=
(
precision_
==
AnalysisConfig
::
Precision
::
kHalf
);
if
(
enable_fp16
)
{
bool
support_fp16
=
infer_builder_
->
platformHasFastFp16
();
...
...
@@ -363,6 +357,7 @@ void TensorRTEngine::FreezeNetwork() {
"opt_shape, false /*disable_trt_plugin_fp16*/)'"
;
}
}
#if IS_TRT_VERSION_GE(8200)
if
(
use_inspector_
)
{
infer_builder_config_
->
setProfilingVerbosity
(
...
...
@@ -374,7 +369,9 @@ void TensorRTEngine::FreezeNetwork() {
infer_engine_
.
reset
(
infer_builder_
->
buildEngineWithConfig
(
*
network
(),
*
infer_builder_config_
));
#else
if
(
use_sparse_weights_
)
{
infer_builder_config_
->
setFlag
(
nvinfer1
::
BuilderFlag
::
kSPARSE_WEIGHTS
);
}
ihost_memory_
.
reset
(
infer_builder_
->
buildSerializedNetwork
(
*
network
(),
*
infer_builder_config_
));
infer_ptr
<
nvinfer1
::
IRuntime
>
runtime
(
createInferRuntime
(
&
logger_
));
...
...
paddle/fluid/inference/tensorrt/engine.h
浏览文件 @
4a69a536
...
...
@@ -739,6 +739,9 @@ class TensorRTEngine {
void
GetEngineInfo
();
void
SetUseInspector
(
bool
use_inspector
)
{
use_inspector_
=
use_inspector
;
}
void
SetUseSparseWeights
(
bool
use_sparse_weights
)
{
use_sparse_weights_
=
use_sparse_weights
;
}
void
SetScope
(
const
framework
::
Scope
&
scope
)
{
scope_
=
&
scope
;
}
void
SetContextMemorySharing
(
bool
context_memory_sharing
)
{
...
...
@@ -827,6 +830,7 @@ class TensorRTEngine {
#endif
std
::
mutex
mutex_
;
bool
use_inspector_
;
bool
use_sparse_weights_
{
false
};
public:
thread_local
static
int
predictor_id_per_thread
;
...
...
paddle/fluid/inference/tensorrt/helper.h
浏览文件 @
4a69a536
...
...
@@ -96,6 +96,10 @@ static std::tuple<int, int, int> GetTrtCompileVersion() {
NV_TENSORRT_MAJOR
,
NV_TENSORRT_MINOR
,
NV_TENSORRT_PATCH
};
}
static
float
TrtMajorVersion
(
int
full_version
)
{
return
(
full_version
/
100
)
/
10.0
;
}
template
<
typename
T
>
struct
Destroyer
{
void
operator
()(
T
*
x
)
{
...
...
paddle/fluid/pybind/inference_api.cc
浏览文件 @
4a69a536
...
...
@@ -879,6 +879,10 @@ void BindAnalysisConfig(py::module *m) {
&
AnalysisConfig
::
EnableTensorRtInspector
)
.
def
(
"tensorrt_inspector_enabled"
,
&
AnalysisConfig
::
tensorrt_inspector_enabled
)
.
def
(
"enable_tensorrt_sparse_weights"
,
&
AnalysisConfig
::
EnableTensorRtSparseWeights
)
.
def
(
"tensorrt_sparse_weights_enabled"
,
&
AnalysisConfig
::
tensorrt_sparse_weights_enabled
)
.
def
(
"tensorrt_engine_enabled"
,
&
AnalysisConfig
::
tensorrt_engine_enabled
)
.
def
(
"enable_dlnne"
,
&
AnalysisConfig
::
EnableDlnne
,
...
...
test/ir/inference/test_trt_inference_predictor.py
浏览文件 @
4a69a536
...
...
@@ -84,6 +84,8 @@ class BackendPaddle:
# enable memory optim
if
not
self
.
args
.
enable_tune
:
config
.
enable_memory_optim
()
if
self
.
args
.
enable_trt_sparse_weights
:
config
.
enable_tensorrt_sparse_weights
()
config
.
set_cpu_math_library_num_threads
(
self
.
args
.
cpu_threads
)
config
.
switch_ir_optim
(
True
)
...
...
@@ -258,6 +260,9 @@ def parse_args():
parser
.
add_argument
(
'--enable_dynamic_shape'
,
type
=
str2bool
,
default
=
True
)
parser
.
add_argument
(
'--enable_tune'
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
'--enable_profile'
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
'--enable_trt_sparse_weights'
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
'--enable_benchmark'
,
type
=
str2bool
,
default
=
True
)
parser
.
add_argument
(
'--save_result'
,
type
=
str2bool
,
default
=
False
)
parser
.
add_argument
(
'--return_result'
,
type
=
str2bool
,
default
=
False
)
...
...
@@ -308,6 +313,13 @@ def run_infer(model_path):
backend
.
load
(
conf
)
backend
.
predict
()
# run inference predictor, enable trt sparse weights
conf
.
enable_tune
=
False
conf
.
enable_trt_sparse_weights
=
True
backend
=
BackendPaddle
()
backend
.
load
(
conf
)
backend
.
predict
()
class
ConvBNLayer
(
paddle
.
nn
.
Layer
):
def
__init__
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录