Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3e2a3488
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
3e2a3488
编写于
7月 28, 2020
作者:
M
mapingshuo
提交者:
GitHub
7月 28, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add string variable support for RecomputeOptimizer (#25728)
上级
0f623ad7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
40 addition
and
4 deletion
+40
-4
python/paddle/fluid/optimizer.py
python/paddle/fluid/optimizer.py
+22
-4
python/paddle/fluid/tests/unittests/test_dgc_optimizer.py
python/paddle/fluid/tests/unittests/test_dgc_optimizer.py
+1
-0
python/paddle/fluid/tests/unittests/test_optimizer.py
python/paddle/fluid/tests/unittests/test_optimizer.py
+17
-0
未找到文件。
python/paddle/fluid/optimizer.py
浏览文件 @
3e2a3488
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
six
import
logging
import
logging
from
collections
import
defaultdict
from
collections
import
defaultdict
...
@@ -4554,6 +4555,17 @@ class RecomputeOptimizer(Optimizer):
...
@@ -4554,6 +4555,17 @@ class RecomputeOptimizer(Optimizer):
self
.
_learning_rate_map
=
self
.
_optimizer
.
_learning_rate_map
self
.
_learning_rate_map
=
self
.
_optimizer
.
_learning_rate_map
def
_set_checkpoints
(
self
,
checkpoints
):
def
_set_checkpoints
(
self
,
checkpoints
):
"""
Args:
checkpoints (list): List of Variable or string
"""
assert
isinstance
(
checkpoints
,
list
),
"_checkpoints should be a list of Variable or a list of String"
for
ckpt
in
checkpoints
:
assert
(
isinstance
(
ckpt
,
six
.
string_types
)
or
isinstance
(
ckpt
,
Variable
)
),
"_checkpoints should be a list of Variable or a list of String"
self
.
_checkpoints
=
checkpoints
self
.
_checkpoints
=
checkpoints
def
load
(
self
,
stat_dict
):
def
load
(
self
,
stat_dict
):
...
@@ -4690,6 +4702,8 @@ class RecomputeOptimizer(Optimizer):
...
@@ -4690,6 +4702,8 @@ class RecomputeOptimizer(Optimizer):
no_grad_set=None)
no_grad_set=None)
print("Finished backward")
print("Finished backward")
"""
"""
assert
(
self
.
_checkpoints
is
not
None
),
"You should call _set_checkpoints first"
if
framework
.
in_dygraph_mode
():
if
framework
.
in_dygraph_mode
():
raise
NotImplementedError
(
raise
NotImplementedError
(
...
@@ -4698,11 +4712,15 @@ class RecomputeOptimizer(Optimizer):
...
@@ -4698,11 +4712,15 @@ class RecomputeOptimizer(Optimizer):
self
.
_dtype
=
loss
.
dtype
self
.
_dtype
=
loss
.
dtype
program
=
loss
.
block
.
program
program
=
loss
.
block
.
program
with
program_guard
(
program
,
startup_program
):
with
program_guard
(
program
,
startup_program
):
checkpoint_vars
=
[]
for
ckpt
in
self
.
_checkpoints
:
if
isinstance
(
ckpt
,
Variable
):
checkpoint_vars
.
append
(
ckpt
)
else
:
checkpoint_vars
.
append
(
loss
.
block
.
var
(
ckpt
))
params_grads
=
append_backward
(
params_grads
=
append_backward
(
loss
,
loss
,
parameter_list
,
no_grad_set
,
checkpoints
=
checkpoint_vars
)
parameter_list
,
no_grad_set
,
checkpoints
=
self
.
_checkpoints
)
# Note: since we can't use all_reduce_op now,
# Note: since we can't use all_reduce_op now,
# dgc_op should be the last op of one grad.
# dgc_op should be the last op of one grad.
if
hasattr
(
self
.
_optimizer
,
"_append_dgc_ops"
):
if
hasattr
(
self
.
_optimizer
,
"_append_dgc_ops"
):
...
...
python/paddle/fluid/tests/unittests/test_dgc_optimizer.py
浏览文件 @
3e2a3488
...
@@ -77,6 +77,7 @@ class TestDGCMomentumOptimizer(unittest.TestCase):
...
@@ -77,6 +77,7 @@ class TestDGCMomentumOptimizer(unittest.TestCase):
if
use_recompute
:
if
use_recompute
:
dgc_momentum_optimizer
=
optimizer
.
RecomputeOptimizer
(
dgc_momentum_optimizer
=
optimizer
.
RecomputeOptimizer
(
dgc_momentum_optimizer
)
dgc_momentum_optimizer
)
dgc_momentum_optimizer
.
_set_checkpoints
([])
dgc_momentum_optimizer
.
get_accumulators
=
dgc_momentum_optimizer
.
_optimizer
.
get_accumulators
dgc_momentum_optimizer
.
get_accumulators
=
dgc_momentum_optimizer
.
_optimizer
.
get_accumulators
dgc_momentum_optimizer
.
get_velocity_str
=
dgc_momentum_optimizer
.
_optimizer
.
get_velocity_str
dgc_momentum_optimizer
.
get_velocity_str
=
dgc_momentum_optimizer
.
_optimizer
.
get_velocity_str
...
...
python/paddle/fluid/tests/unittests/test_optimizer.py
浏览文件 @
3e2a3488
...
@@ -714,6 +714,23 @@ class TestRecomputeOptimizer(unittest.TestCase):
...
@@ -714,6 +714,23 @@ class TestRecomputeOptimizer(unittest.TestCase):
"elementwise_add_grad"
,
"mul_grad"
,
"sgd"
,
"sgd"
,
"sgd"
"elementwise_add_grad"
,
"mul_grad"
,
"sgd"
,
"sgd"
,
"sgd"
])
])
def
test_str_checkpoints
(
self
):
mul_out
,
b1_out
,
b2_out
,
mean_out
=
self
.
net
()
self
.
assertEqual
(
len
(
mean_out
.
block
.
ops
),
4
)
self
.
assertEqual
([
op
.
type
for
op
in
mean_out
.
block
.
ops
],
[
"mul"
,
"elementwise_add"
,
"elementwise_add"
,
"mean"
])
sgd_optimizer
=
optimizer
.
SGD
(
learning_rate
=
1.0
)
recompute_optimizer
=
optimizer
.
RecomputeOptimizer
(
sgd_optimizer
)
recompute_optimizer
.
_set_checkpoints
([
b1_out
.
name
])
opts
,
params_grads
=
recompute_optimizer
.
minimize
(
mean_out
)
self
.
assertEqual
(
len
(
mean_out
.
block
.
ops
),
13
)
self
.
assertEqual
([
op
.
type
for
op
in
mean_out
.
block
.
ops
],
[
"mul"
,
"elementwise_add"
,
"elementwise_add"
,
"mean"
,
"fill_constant"
,
"mean_grad"
,
"elementwise_add_grad"
,
"mul"
,
"elementwise_add_grad"
,
"mul_grad"
,
"sgd"
,
"sgd"
,
"sgd"
])
def
test_multi_checkpoint
(
self
):
def
test_multi_checkpoint
(
self
):
mul_out
,
b1_out
,
b2_out
,
mean_out
=
self
.
net
()
mul_out
,
b1_out
,
b2_out
,
mean_out
=
self
.
net
()
self
.
assertEqual
(
len
(
mean_out
.
block
.
ops
),
4
)
self
.
assertEqual
(
len
(
mean_out
.
block
.
ops
),
4
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录