Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
f884f288
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
1 年多 前同步成功
通知
116
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
f884f288
编写于
5月 23, 2023
作者:
G
gaotingquan
提交者:
Tingquan Gao
5月 25, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor amp
上级
b2cb4178
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
61 addition
and
56 deletion
+61
-56
ppcls/engine/engine.py
ppcls/engine/engine.py
+61
-56
未找到文件。
ppcls/engine/engine.py
浏览文件 @
f884f288
...
...
@@ -242,61 +242,8 @@ class Engine(object):
self
.
config
[
"Optimizer"
],
self
.
config
[
"Global"
][
"epochs"
],
self
.
iter_per_epoch
//
self
.
update_freq
,
[
self
.
model
,
self
.
train_loss_func
])
# AMP training and evaluating
self
.
amp
=
"AMP"
in
self
.
config
and
self
.
config
[
"AMP"
]
is
not
None
self
.
amp_eval
=
False
# for amp
if
self
.
amp
:
AMP_RELATED_FLAGS_SETTING
=
{
'FLAGS_max_inplace_grad_add'
:
8
,
}
if
paddle
.
is_compiled_with_cuda
():
AMP_RELATED_FLAGS_SETTING
.
update
({
'FLAGS_cudnn_batchnorm_spatial_persistent'
:
1
})
paddle
.
set_flags
(
AMP_RELATED_FLAGS_SETTING
)
self
.
scale_loss
=
self
.
config
[
"AMP"
].
get
(
"scale_loss"
,
1.0
)
self
.
use_dynamic_loss_scaling
=
self
.
config
[
"AMP"
].
get
(
"use_dynamic_loss_scaling"
,
False
)
self
.
scaler
=
paddle
.
amp
.
GradScaler
(
init_loss_scaling
=
self
.
scale_loss
,
use_dynamic_loss_scaling
=
self
.
use_dynamic_loss_scaling
)
self
.
amp_level
=
self
.
config
[
'AMP'
].
get
(
"level"
,
"O1"
)
if
self
.
amp_level
not
in
[
"O1"
,
"O2"
]:
msg
=
"[Parameter Error]: The optimize level of AMP only support 'O1' and 'O2'. The level has been set 'O1'."
logger
.
warning
(
msg
)
self
.
config
[
'AMP'
][
"level"
]
=
"O1"
self
.
amp_level
=
"O1"
self
.
amp_eval
=
self
.
config
[
"AMP"
].
get
(
"use_fp16_test"
,
False
)
# TODO(gaotingquan): Paddle not yet support FP32 evaluation when training with AMPO2
if
self
.
mode
==
"train"
and
self
.
config
[
"Global"
].
get
(
"eval_during_train"
,
True
)
and
self
.
amp_level
==
"O2"
and
self
.
amp_eval
==
False
:
msg
=
"PaddlePaddle only support FP16 evaluation when training with AMP O2 now. "
logger
.
warning
(
msg
)
self
.
config
[
"AMP"
][
"use_fp16_test"
]
=
True
self
.
amp_eval
=
True
if
self
.
mode
==
"train"
:
self
.
model
,
self
.
optimizer
=
paddle
.
amp
.
decorate
(
models
=
self
.
model
,
optimizers
=
self
.
optimizer
,
level
=
self
.
amp_level
,
save_dtype
=
'float32'
)
elif
self
.
amp_eval
:
self
.
model
=
paddle
.
amp
.
decorate
(
models
=
self
.
model
,
level
=
self
.
amp_level
,
save_dtype
=
'float32'
)
if
self
.
mode
==
"train"
and
len
(
self
.
train_loss_func
.
parameters
(
))
>
0
:
self
.
train_loss_func
=
paddle
.
amp
.
decorate
(
models
=
self
.
train_loss_func
,
level
=
self
.
amp_level
,
save_dtype
=
'float32'
)
# amp
self
.
_init_amp
()
# build EMA model
self
.
ema
=
"EMA"
in
self
.
config
and
self
.
mode
==
"train"
...
...
@@ -513,7 +460,9 @@ class Engine(object):
batch_tensor
=
paddle
.
to_tensor
(
batch_data
)
if
self
.
amp
and
self
.
amp_eval
:
with
paddle
.
amp
.
auto_cast
(
level
=
self
.
amp_level
):
with
paddle
.
amp
.
auto_cast
(
level
=
self
.
amp_level
,
use_promote
=
self
.
use_promote
):
out
=
self
.
model
(
batch_tensor
)
else
:
out
=
self
.
model
(
batch_tensor
)
...
...
@@ -578,6 +527,62 @@ class Engine(object):
f
"Export succeeded! The inference model exported has been saved in
\"
{
self
.
config
[
'Global'
][
'save_inference_dir'
]
}
\"
."
)
def
_init_amp
(
self
):
self
.
amp
=
"AMP"
in
self
.
config
and
self
.
config
[
"AMP"
]
is
not
None
self
.
amp_eval
=
False
if
self
.
amp
:
AMP_RELATED_FLAGS_SETTING
=
{
'FLAGS_max_inplace_grad_add'
:
8
,
}
if
paddle
.
is_compiled_with_cuda
():
AMP_RELATED_FLAGS_SETTING
.
update
({
'FLAGS_cudnn_batchnorm_spatial_persistent'
:
1
})
paddle
.
set_flags
(
AMP_RELATED_FLAGS_SETTING
)
self
.
scale_loss
=
self
.
config
[
"AMP"
].
get
(
"scale_loss"
,
1.0
)
self
.
use_dynamic_loss_scaling
=
self
.
config
[
"AMP"
].
get
(
"use_dynamic_loss_scaling"
,
False
)
self
.
scaler
=
paddle
.
amp
.
GradScaler
(
init_loss_scaling
=
self
.
scale_loss
,
use_dynamic_loss_scaling
=
self
.
use_dynamic_loss_scaling
)
self
.
use_promote
=
self
.
config
[
'AMP'
].
get
(
"use_promote"
,
False
)
self
.
amp_level
=
self
.
config
[
'AMP'
].
get
(
"level"
,
"O1"
)
if
self
.
amp_level
not
in
[
"O1"
,
"O2"
]:
msg
=
"[Parameter Error]: The optimize level of AMP only support 'O1' and 'O2'. The level has been set 'O1'."
logger
.
warning
(
msg
)
self
.
config
[
'AMP'
][
"level"
]
=
"O1"
self
.
amp_level
=
"O1"
self
.
amp_eval
=
self
.
config
[
"AMP"
].
get
(
"use_fp16_test"
,
False
)
# TODO(gaotingquan): Paddle not yet support FP32 evaluation when training with AMPO2
if
self
.
mode
==
"train"
and
self
.
config
[
"Global"
].
get
(
"eval_during_train"
,
True
)
and
self
.
amp_level
==
"O2"
and
self
.
amp_eval
==
False
:
msg
=
"PaddlePaddle only support FP16 evaluation when training with AMP O2 now. "
logger
.
warning
(
msg
)
self
.
config
[
"AMP"
][
"use_fp16_test"
]
=
True
self
.
amp_eval
=
True
if
self
.
mode
==
"train"
:
self
.
model
,
self
.
optimizer
=
paddle
.
amp
.
decorate
(
models
=
self
.
model
,
optimizers
=
self
.
optimizer
,
level
=
self
.
amp_level
,
save_dtype
=
'float32'
)
elif
self
.
amp_eval
:
self
.
model
=
paddle
.
amp
.
decorate
(
models
=
self
.
model
,
level
=
self
.
amp_level
,
save_dtype
=
'float32'
)
if
self
.
mode
==
"train"
and
len
(
self
.
train_loss_func
.
parameters
(
))
>
0
:
self
.
train_loss_func
=
paddle
.
amp
.
decorate
(
models
=
self
.
train_loss_func
,
level
=
self
.
amp_level
,
save_dtype
=
'float32'
)
class
ExportModel
(
TheseusLayer
):
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录