Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
a0631364
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
a0631364
编写于
11月 30, 2021
作者:
X
xiongkun
提交者:
GitHub
11月 30, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix test calc gradient (#37672)
* add scope_guard * 1. fix control flow cases 2. fix calc_gradient
上级
74fdba7c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
41 addition
and
17 deletion
+41
-17
paddle/fluid/framework/new_executor/interpretercore_util.cc
paddle/fluid/framework/new_executor/interpretercore_util.cc
+24
-4
paddle/fluid/framework/new_executor/standalone_executor.cc
paddle/fluid/framework/new_executor/standalone_executor.cc
+3
-1
python/paddle/fluid/tests/unittests/test_calc_gradient.py
python/paddle/fluid/tests/unittests/test_calc_gradient.py
+14
-12
未找到文件。
paddle/fluid/framework/new_executor/interpretercore_util.cc
浏览文件 @
a0631364
...
...
@@ -16,6 +16,9 @@
#include "paddle/fluid/framework/executor_gc_helper.h"
#include "paddle/fluid/framework/new_executor/data_transfer.h"
#include "paddle/fluid/operators/controlflow/conditional_block_op_helper.h"
#include "paddle/fluid/operators/controlflow/recurrent_op_helper.h"
#include "paddle/fluid/operators/controlflow/while_op_helper.h"
namespace
paddle
{
namespace
framework
{
...
...
@@ -127,6 +130,9 @@ void build_variable_scope(const framework::BlockDesc& block,
for
(
auto
&
var_desc
:
block
.
AllVars
())
{
auto
var_name
=
var_desc
->
Name
();
// TODO(xiongkun): user may create a variable with name that exists before.
// under such circumstances, we should raise a error. Currently we can't
// get the var_desc of startup_program, so leave it later.
if
(
var_name
==
framework
::
kEmptyVarName
)
{
continue
;
}
...
...
@@ -149,7 +155,7 @@ void build_variable_scope(const framework::BlockDesc& block,
}
void
create_all_ops
(
const
framework
::
BlockDesc
&
block
,
std
::
vector
<
std
::
shared
_ptr
<
OperatorBase
>>*
ops
)
{
std
::
vector
<
std
::
unique
_ptr
<
OperatorBase
>>*
ops
)
{
for
(
auto
&
op
:
block
.
AllOps
())
{
VLOG
(
3
)
<<
"CreateOp from : "
<<
op
->
Type
();
...
...
@@ -164,7 +170,7 @@ void create_all_ops(const framework::BlockDesc& block,
}
auto
op_base
=
info
.
Creator
()(
op
->
Type
(),
inputs_names
,
outputs_names
,
op_attr_map
);
ops
->
emplace_back
(
std
::
shared
_ptr
<
OperatorBase
>
(
op_base
));
ops
->
emplace_back
(
std
::
unique
_ptr
<
OperatorBase
>
(
op_base
));
}
}
...
...
@@ -260,10 +266,24 @@ void build_op_func_list(const platform::Place& place,
Scope
*
local_scope
=
use_local_scope
?
var_scope
->
GetMutableLocalScope
()
:
var_scope
->
GetMutableScope
();
auto
&
all_op_kernels
=
OperatorWithKernel
::
AllOpKernels
();
std
::
vector
<
std
::
unique_ptr
<
OperatorBase
>>
ops_unique
;
// its elements will be moved to vec_func_list
// Step 1: create all ops for current block.
create_all_ops
(
block
,
&
ops_unique
);
// If gc is enabled and block size > 1
const
ProgramDesc
&
main_program
=
*
block
.
Program
();
operators
::
PrepareSafeEagerDeletionOnConditionalOpAndConditionalGradOp
(
main_program
,
block
.
ID
(),
ops_unique
);
operators
::
PrepareSafeEagerDeletionOnWhileOpAndWhileGradOp
(
main_program
,
block
.
ID
(),
ops_unique
);
operators
::
PrepareSafeEagerDeletionOnRecurrentOpAndRecurrentGradOp
(
main_program
,
block
.
ID
(),
ops_unique
);
std
::
vector
<
std
::
shared_ptr
<
OperatorBase
>>
ops
;
// its elements will be moved to vec_func_list
// Step 1: create all ops for current block.
create_all_ops
(
block
,
&
ops
);
for
(
auto
&
op_unique
:
ops_unique
)
{
ops
.
emplace_back
(
std
::
move
(
op_unique
));
}
auto
unused_var_map
=
get_unused_vars
(
block
,
ops
);
for
(
size_t
i
=
0
;
i
<
ops
.
size
();
++
i
)
{
...
...
paddle/fluid/framework/new_executor/standalone_executor.cc
浏览文件 @
a0631364
...
...
@@ -33,6 +33,7 @@ StandaloneExecutor::StandaloneExecutor(const platform::Place& place,
if
(
scope
)
{
auto
name_list
=
scope
->
LocalVarNames
();
for
(
auto
name
:
name_list
)
{
VLOG
(
4
)
<<
"Sync Variable from variable scope: "
<<
name
;
auto
v
=
scope
->
Var
(
name
);
if
(
!
global_scope_
.
HasVar
(
name
))
{
global_scope_
.
AddVar
(
name
,
*
v
);
...
...
@@ -87,8 +88,9 @@ void StandaloneExecutor::BuildVariableScope(const framework::ProgramDesc& pdesc,
if
(
var
->
Name
()
==
framework
::
kEmptyVarName
)
{
continue
;
}
if
(
!
var_scope
->
HasVar
(
var
->
Name
()))
{
VLOG
(
4
)
<<
"Create variable from startup_prog: "
<<
var
->
Proto
()
->
SerializeAsString
();
var_scope
->
AddVar
(
var
->
Name
(),
var
);
}
}
...
...
python/paddle/fluid/tests/unittests/test_calc_gradient.py
浏览文件 @
a0631364
...
...
@@ -14,6 +14,7 @@
from
__future__
import
print_function
import
paddle
import
unittest
import
numpy
as
np
import
paddle.fluid
as
fluid
...
...
@@ -83,19 +84,20 @@ class TestDoubleGrad(unittest.TestCase):
class
TestGradientWithPrune
(
unittest
.
TestCase
):
def
test_prune
(
self
):
x
=
fluid
.
data
(
name
=
'x'
,
shape
=
[
3
],
dtype
=
'float32'
)
x
.
stop_gradient
=
False
x1
,
x2
,
x3
=
fluid
.
layers
.
split
(
x
,
dim
=
0
,
num_or_sections
=
3
)
y
=
x1
*
2
x1_grad
=
fluid
.
gradients
(
y
,
x
)
with
paddle
.
fluid
.
scope_guard
(
paddle
.
static
.
Scope
()):
x
=
fluid
.
data
(
name
=
'x'
,
shape
=
[
3
],
dtype
=
'float32'
)
x
.
stop_gradient
=
False
x1
,
x2
,
x3
=
fluid
.
layers
.
split
(
x
,
dim
=
0
,
num_or_sections
=
3
)
y
=
x1
*
2
x1_grad
=
fluid
.
gradients
(
y
,
x
)
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
main
=
fluid
.
default_main_program
()
exe
.
run
(
fluid
.
default_startup_program
())
out
=
exe
.
run
(
main
,
feed
=
{
'x'
:
np
.
ones
([
3
]).
astype
(
'float32'
)},
fetch_list
=
[
x1_grad
])
self
.
assertTrue
(
np
.
array_equal
(
out
[
0
],
[
2.
,
0.
,
0.
]))
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
())
main
=
fluid
.
default_main_program
()
exe
.
run
(
fluid
.
default_startup_program
())
out
=
exe
.
run
(
main
,
feed
=
{
'x'
:
np
.
ones
([
3
]).
astype
(
'float32'
)},
fetch_list
=
[
x1_grad
])
self
.
assertTrue
(
np
.
array_equal
(
out
[
0
],
[
2.
,
0.
,
0.
]))
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录