Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
eba057e3
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
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看板
未验证
提交
eba057e3
编写于
4月 19, 2023
作者:
C
csy0225
提交者:
GitHub
4月 19, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Convert to mixed precision support serialize params if a origin model doesn't have params. (#52994)
上级
c29dc34e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
44 addition
and
7 deletion
+44
-7
paddle/fluid/inference/analysis/passes/CMakeLists.txt
paddle/fluid/inference/analysis/passes/CMakeLists.txt
+2
-1
paddle/fluid/inference/analysis/passes/convert_to_mixed_precision.cc
...d/inference/analysis/passes/convert_to_mixed_precision.cc
+34
-3
python/paddle/inference/wrapper.py
python/paddle/inference/wrapper.py
+8
-3
未找到文件。
paddle/fluid/inference/analysis/passes/CMakeLists.txt
浏览文件 @
eba057e3
...
...
@@ -13,7 +13,8 @@ cc_library(
cc_library
(
convert_to_mixed_precision
SRCS convert_to_mixed_precision.cc
DEPS analysis_pass ir_graph_build_pass auto_mixed_precision_pass
)
DEPS analysis_pass ir_graph_build_pass auto_mixed_precision_pass
constant_folding_pass
)
cc_library
(
ir_params_sync_among_devices_pass
SRCS ir_params_sync_among_devices_pass.cc
...
...
paddle/fluid/inference/analysis/passes/convert_to_mixed_precision.cc
浏览文件 @
eba057e3
...
...
@@ -16,6 +16,7 @@
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/ir/auto_mixed_precision_pass.h"
#include "paddle/fluid/framework/ir/constant_folding_pass.h"
#include "paddle/fluid/framework/ir/graph_helper.h"
#include "paddle/fluid/inference/io.h"
#include "paddle/phi/common/backend.h"
...
...
@@ -71,8 +72,13 @@ ConvertToMixedPrecisionPass::ConvertToMixedPrecisionPass(
void
ConvertToMixedPrecisionPass
::
LoadModel
()
{
framework
::
Executor
exe
{
platform
::
CPUPlace
{}};
auto
program_desc
=
inference
::
Load
(
&
exe
,
&
scope_
,
model_file_
,
params_file_
);
// If we did not find the provided weight path,
// we assume that the model to be converted only has a model file and no
// params file, we believe this situation is reasonable. In this case, weight
// data may not be loaded.
bool
load_params
=
!
params_file_
.
empty
();
auto
program_desc
=
inference
::
Load
(
&
exe
,
&
scope_
,
model_file_
,
params_file_
,
load_params
);
main_graph_
=
std
::
unique_ptr
<
framework
::
ir
::
Graph
>
(
new
framework
::
ir
::
Graph
(
*
program_desc
));
main_graph_
->
SetNotOwned
(
framework
::
ir
::
kParamScopeAttr
,
&
scope_
);
...
...
@@ -81,6 +87,8 @@ void ConvertToMixedPrecisionPass::LoadModel() {
void
ConvertToMixedPrecisionPass
::
Run
()
{
LoadModel
();
framework
::
ir
::
ConstantFoldingPass
constant_folding_pass
;
constant_folding_pass
.
Apply
(
main_graph_
.
get
());
framework
::
ir
::
AutoMixedPrecisionPass
pass
;
pass
.
Set
(
"mixed_precision_mode"
,
new
int
{
static_cast
<
int
>
(
mixed_precision_
)});
if
(
backend_
==
phi
::
Backend
::
GPU
)
{
...
...
@@ -117,6 +125,7 @@ void ConvertToMixedPrecisionPass::SaveMixedModel() {
const
auto
&
global_block
=
mixed_program_desc
.
Block
(
0
);
std
::
vector
<
std
::
string
>
save_var_list
;
bool
has_persistable_var
=
false
;
for
(
framework
::
VarDesc
*
var
:
global_block
.
AllVars
())
{
if
(
IsPersistable
(
var
))
{
framework
::
VarDesc
*
new_var
=
save_block
->
Var
(
var
->
Name
());
...
...
@@ -127,13 +136,35 @@ void ConvertToMixedPrecisionPass::SaveMixedModel() {
new_var
->
SetPersistable
(
true
);
save_var_list
.
push_back
(
new_var
->
Name
());
has_persistable_var
=
true
;
}
}
std
::
string
save_params_path
=
path
;
if
(
save_params_path
.
empty
()
&&
has_persistable_var
)
{
LOG
(
WARNING
)
<<
"The [SerializeParams] function did not find the provided weight "
"path, "
"so we assume that the model to be converted only has a model "
"file and no params file, "
"we believe this situation is reasonable. After constant folding, "
"a weight file will be generated, which is saved in the same "
"level file directory "
"as the model file by default and ends in pdiparams."
;
save_params_path
=
mixed_model_file_
;
std
::
string
::
size_type
pos
=
save_params_path
.
rfind
(
".pdmodel"
);
if
(
pos
!=
std
::
string
::
npos
)
{
save_params_path
.
replace
(
pos
,
8
,
".pdiparams"
);
LOG
(
WARNING
)
<<
" The storage path of the converted mixed-precision "
"params has been created: ["
<<
save_params_path
<<
"]"
;
}
}
std
::
sort
(
save_var_list
.
begin
(),
save_var_list
.
end
());
auto
*
op
=
save_block
->
AppendOp
();
op
->
SetType
(
"save_combine"
);
op
->
SetInput
(
"X"
,
save_var_list
);
op
->
SetAttr
(
"file_path"
,
path
);
op
->
SetAttr
(
"file_path"
,
save_params_
path
);
op
->
CheckAttrs
();
framework
::
Executor
exe
(
platform
::
CPUPlace
{});
...
...
python/paddle/inference/wrapper.py
浏览文件 @
eba057e3
...
...
@@ -94,9 +94,14 @@ def convert_to_mixed_precision(
black_list: Operators that do not convert precision.
'''
mixed_model_dirname
=
os
.
path
.
dirname
(
mixed_model_file
)
mixed_params_dirname
=
os
.
path
.
dirname
(
mixed_params_file
)
if
not
os
.
path
.
exists
(
mixed_model_dirname
):
os
.
makedirs
(
mixed_model_dirname
)
# Support mixed_params_file is empty, because some models don't have params, but convert_to_mixed_precision will call
# constant_folding_pass, it will generate a new params file to save persistable vars, which is saved in the same
# level file directory as the model file by default and ends in pdiparams.
mixed_params_dirname
=
(
os
.
path
.
dirname
(
mixed_params_file
)
if
len
(
mixed_params_file
)
!=
0
else
mixed_model_dirname
)
if
not
os
.
path
.
exists
(
mixed_params_dirname
):
os
.
makedirs
(
mixed_params_dirname
)
convert_to_mixed_precision_bind
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录