Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
ae92da87
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ae92da87
编写于
2月 16, 2022
作者:
J
Jiabin Yang
提交者:
GitHub
2月 16, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support GetGradAccumulator for reducer (#39537)
上级
831fd86e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
63 addition
and
0 deletion
+63
-0
paddle/fluid/eager/tests/task_tests/eager_utils_test.cc
paddle/fluid/eager/tests/task_tests/eager_utils_test.cc
+23
-0
paddle/fluid/eager/utils.cc
paddle/fluid/eager/utils.cc
+38
-0
paddle/fluid/eager/utils.h
paddle/fluid/eager/utils.h
+2
-0
未找到文件。
paddle/fluid/eager/tests/task_tests/eager_utils_test.cc
浏览文件 @
ae92da87
...
...
@@ -16,6 +16,7 @@
#include "gtest/gtest.h"
#include "paddle/fluid/eager/accumulation/accumulation_node.h"
#include "paddle/fluid/eager/eager_tensor.h"
#include "paddle/fluid/eager/grad_node_info.h"
#include "paddle/fluid/eager/tests/data_structure_tests/grad_node_test.h"
...
...
@@ -224,4 +225,26 @@ TEST(EagerUtils, CreateVars) {
CHECK
(
outs
[
0
]
->
Var
().
IsInitialized
()
==
false
);
}
TEST
(
EagerUtils
,
GetGradAccumulationNode
)
{
VLOG
(
6
)
<<
"Check GetGradAccumulationNode"
;
paddle
::
experimental
::
Tensor
t0
(
"test_tensor"
);
ASSERT_EQ
(
egr
::
EagerUtils
::
GetGradAccumulationNode
(
t0
),
nullptr
);
auto
autograd_ptr0
=
egr
::
EagerUtils
::
autograd_meta
(
&
t0
);
autograd_ptr0
->
SetStopGradient
(
true
);
ASSERT_EQ
(
egr
::
EagerUtils
::
GetGradAccumulationNode
(
t0
),
nullptr
);
autograd_ptr0
->
SetStopGradient
(
false
);
auto
res
=
std
::
dynamic_pointer_cast
<
egr
::
GradNodeAccumulation
>
(
egr
::
EagerUtils
::
GetGradAccumulationNode
(
t0
));
ASSERT_TRUE
(
res
!=
nullptr
);
auto
res2
=
egr
::
EagerUtils
::
GetGradAccumulationNode
(
t0
);
ASSERT_EQ
(
res2
.
get
(),
res
.
get
());
autograd_ptr0
->
SetStopGradient
(
true
);
auto
res3
=
egr
::
EagerUtils
::
GetGradAccumulationNode
(
t0
);
ASSERT_EQ
(
res3
,
nullptr
);
autograd_ptr0
->
SetStopGradient
(
false
);
autograd_ptr0
->
SetGradNode
(
std
::
make_shared
<
eager_test
::
GradTestNode
>
(
1
,
2.0
,
3
));
ASSERT_ANY_THROW
(
egr
::
EagerUtils
::
GetGradAccumulationNode
(
t0
));
}
}
// namespace egr
paddle/fluid/eager/utils.cc
浏览文件 @
ae92da87
...
...
@@ -21,6 +21,7 @@
#include "paddle/pten/common/layout.h"
#include "paddle/pten/core/tensor_meta.h"
#include "paddle/fluid/eager/accumulation/accumulation_node.h"
#include "paddle/fluid/framework/data_layout.h"
#include "paddle/fluid/framework/pten_utils.h"
#include "paddle/fluid/framework/variable.h"
...
...
@@ -303,4 +304,41 @@ void EagerUtils::CheckAndRetainGrad(
}
}
std
::
shared_ptr
<
egr
::
GradNodeBase
>
EagerUtils
::
GetGradAccumulationNode
(
const
paddle
::
experimental
::
Tensor
&
tensor
)
{
auto
*
autograd_ptr
=
nullable_autograd_meta
(
tensor
);
if
(
!
autograd_ptr
)
{
return
nullptr
;
}
auto
node_ptr
=
autograd_ptr
->
GetMutableGradNode
();
if
(
node_ptr
&&
node_ptr
.
get
())
{
if
(
!
autograd_ptr
->
StopGradient
())
{
auto
accumulation_ptr
=
std
::
dynamic_pointer_cast
<
GradNodeAccumulation
>
(
node_ptr
);
if
(
accumulation_ptr
)
{
return
accumulation_ptr
;
}
else
{
// Current GradNode is not a egr::GradNodeAccumulation
PADDLE_THROW
(
paddle
::
platform
::
errors
::
Fatal
(
"GetGradAccumulationNode should only be called on leaf tensor, but "
"target tensor: %s has GradNode which is not a "
"GradNodeAccumulation, and this should not happend unless target "
"tensor is modified by some ops and calling set history for it."
,
tensor
.
name
()));
}
}
else
{
// Current Tensor does not have grad since it's stop_gradient is true;
return
nullptr
;
}
}
else
{
if
(
!
autograd_ptr
->
StopGradient
())
{
VLOG
(
6
)
<<
"Add GradNodeAccumulation for tensor: "
<<
tensor
.
name
();
autograd_ptr
->
SetGradNode
(
std
::
make_shared
<
egr
::
GradNodeAccumulation
>
());
return
autograd_ptr
->
GetMutableGradNode
();
}
else
{
return
nullptr
;
}
}
}
}
// namespace egr
paddle/fluid/eager/utils.h
浏览文件 @
ae92da87
...
...
@@ -189,6 +189,8 @@ class EagerUtils {
static
void
CheckAndRetainGrad
(
const
paddle
::
experimental
::
Tensor
&
tensor
);
static
void
CheckAndRetainGrad
(
const
std
::
vector
<
paddle
::
experimental
::
Tensor
>&
tensors
);
static
std
::
shared_ptr
<
egr
::
GradNodeBase
>
GetGradAccumulationNode
(
const
paddle
::
experimental
::
Tensor
&
tensor
);
};
}
// namespace egr
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录