Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
a7c38367
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
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看板
未验证
提交
a7c38367
编写于
8月 04, 2021
作者:
C
chentianyu03
提交者:
GitHub
8月 04, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix backward bug (#34582)
* fix backward bug * format code style * add test case for grad tensor accumulator
上级
54b6c390
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
5 deletion
+44
-5
paddle/fluid/imperative/basic_engine.cc
paddle/fluid/imperative/basic_engine.cc
+19
-5
python/paddle/fluid/tests/unittests/test_custom_grad_input.py
...on/paddle/fluid/tests/unittests/test_custom_grad_input.py
+25
-0
未找到文件。
paddle/fluid/imperative/basic_engine.cc
浏览文件 @
a7c38367
...
...
@@ -49,11 +49,17 @@ void BasicEngine::Init(
"the size of tensors is %s, but the size of grad_tensors is %s."
,
tensors
.
size
(),
grad_tensors
.
size
()));
PADDLE_ENFORCE_EQ
(
accumulators_
.
empty
(),
true
,
platform
::
errors
::
AlreadyExists
(
"Accumulators are not empty before preparing it for "
"backward network execution."
));
for
(
size_t
i
=
0
;
i
<
tensors
.
size
();
++
i
)
{
auto
var
=
tensors
[
i
];
auto
grad_tensor
=
grad_tensors
[
i
];
auto
init_node
=
var
->
GradVarBase
()
->
GradNode
();
PADDLE_ENFORCE_EQ
(
var
->
GradVarBase
()
->
GraphIsFreed
(),
false
,
platform
::
errors
::
Unavailable
(
...
...
@@ -101,6 +107,16 @@ void BasicEngine::Init(
*
dev_ctx
,
grad_var
);
}
VariableWrapper
*
init_grad_var
=
var
->
GradVarBase
()
->
SharedVar
().
get
();
auto
&
accumulator
=
accumulators_
[
init_grad_var
];
if
(
!
accumulator
)
{
if
(
FLAGS_sort_sum_gradient
)
{
accumulator
.
reset
(
new
SortedGradientAccumulator
(
init_grad_var
));
}
else
{
accumulator
.
reset
(
new
EagerGradientAccumulator
(
init_grad_var
));
}
}
init_nodes_
.
push_back
(
init_node
);
}
}
...
...
@@ -237,10 +253,6 @@ void BasicEngine::PrepareDeps() {
node_deps_
.
empty
(),
true
,
platform
::
errors
::
AlreadyExists
(
"Op deps are not empty before preparing "
"it for backward network execution."
));
PADDLE_ENFORCE_EQ
(
accumulators_
.
empty
(),
true
,
platform
::
errors
::
AlreadyExists
(
"Accumulators are not empty before preparing it for "
"backward network execution."
));
PADDLE_ENFORCE_EQ
(
accumulators_with_grad_node_
.
empty
(),
true
,
platform
::
errors
::
AlreadyExists
(
"Accumulators with grad_node as the key are not empty "
...
...
@@ -311,7 +323,9 @@ void BasicEngine::Execute() {
// Start execute Computation graph
std
::
queue
<
std
::
shared_ptr
<
GradOpNode
>>
q
;
for
(
size_t
i
=
0
;
i
<
init_nodes_
.
size
();
++
i
)
{
q
.
push
(
std
::
move
(
init_nodes_
[
i
]));
if
(
node_deps_
[
init_nodes_
[
i
].
get
()]
==
0
)
{
q
.
push
(
std
::
move
(
init_nodes_
[
i
]));
}
}
size_t
op_num
=
0
;
...
...
python/paddle/fluid/tests/unittests/test_custom_grad_input.py
浏览文件 @
a7c38367
...
...
@@ -115,6 +115,31 @@ class TestBackwardAPI(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
x_grad
,
x_tensor
.
grad
.
numpy
()))
def
test_backward_accumulator_with_init_grad
(
self
):
for
dtype
in
self
.
_dtypes
:
x
=
np
.
random
.
random
([
10
,
]).
astype
(
dtype
)
y_grad
=
np
.
random
.
random
([
10
,
]).
astype
(
dtype
)
z_grad
=
np
.
random
.
random
([
10
,
]).
astype
(
dtype
)
self
.
_places
=
[
paddle
.
CPUPlace
()]
for
place
in
self
.
_places
:
with
dg
.
guard
(
place
):
x_tensor
=
paddle
.
to_tensor
(
x
,
stop_gradient
=
False
)
y_tensor
=
x_tensor
**
2
z_tensor
=
y_tensor
**
3
y_grad_tensor
=
paddle
.
to_tensor
(
y_grad
)
z_grad_tensor
=
paddle
.
to_tensor
(
z_grad
)
paddle
.
autograd
.
backward
([
y_tensor
,
z_tensor
],
[
y_grad_tensor
,
z_grad_tensor
])
y
=
x
**
2
z
=
x
**
3
x_grad
=
2
*
x_tensor
*
(
y_grad_tensor
+
3
*
y_tensor
*
y_tensor
*
z_grad_tensor
)
self
.
assertTrue
(
np
.
allclose
(
x_grad
.
numpy
(),
x_tensor
.
grad
.
numpy
()))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录