Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
102fc859
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
102fc859
编写于
4月 16, 2019
作者:
Q
Qiyang Min
提交者:
GitHub
4月 16, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16777 from velconia/dygraph_untrack_op
Imperative tracer does not hold op any more
上级
f4626ee4
9a3848a2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
289 addition
and
34 deletion
+289
-34
python/paddle/fluid/dygraph/layers.py
python/paddle/fluid/dygraph/layers.py
+12
-0
python/paddle/fluid/dygraph/tracer.py
python/paddle/fluid/dygraph/tracer.py
+59
-8
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+17
-26
python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py
...addle/fluid/tests/unittests/test_dygraph_multi_forward.py
+201
-0
未找到文件。
python/paddle/fluid/dygraph/layers.py
浏览文件 @
102fc859
...
...
@@ -48,6 +48,12 @@ class Layer(core.Layer):
self
.
_helper
=
LayerObjectHelper
(
self
.
_full_name
)
def
train
(
self
):
framework
.
_dygraph_tracer
().
_train_mode
()
def
eval
(
self
):
framework
.
_dygraph_tracer
().
_eval_mode
()
def
full_name
(
self
):
"""Full name for this layers.
...
...
@@ -254,6 +260,12 @@ class PyLayer(core.PyLayer):
def
__init__
(
self
):
super
(
PyLayer
,
self
).
__init__
()
def
train
(
self
):
framework
.
_dygraph_tracer
().
_train_mode
()
def
eval
(
self
):
framework
.
_dygraph_tracer
().
_eval_mode
()
@
classmethod
def
_do_forward
(
cls
,
inputs
):
return
cls
.
_to_tuple
(
cls
.
forward
(
inputs
))
...
...
python/paddle/fluid/dygraph/tracer.py
浏览文件 @
102fc859
...
...
@@ -24,7 +24,9 @@ __all__ = ['Tracer']
def
release_op
(
op
):
del
framework
.
_dygraph_tracer
().
_ops
[
op
.
_trace_id
]
del
framework
.
_dygraph_tracer
().
_ops
[
op
.
_trace_id
].
inputs
del
framework
.
_dygraph_tracer
().
_ops
[
op
.
_trace_id
].
outputs
del
framework
.
_dygraph_tracer
().
_ops
[
op
.
_trace_id
].
backward_refs
class
Tracer
(
core
.
Tracer
):
...
...
@@ -38,6 +40,7 @@ class Tracer(core.Tracer):
self
.
_ops
=
defaultdict
()
self
.
_vars
=
defaultdict
()
self
.
_trace_id
=
0
self
.
_train_mode
=
True
def
trace_var
(
self
,
name
,
var
):
self
.
_vars
[
name
]
=
var
...
...
@@ -46,15 +49,57 @@ class Tracer(core.Tracer):
return
list
((
item
for
name
,
item
in
six
.
iteritems
(
self
.
_vars
)
if
isinstance
(
item
,
framework
.
Parameter
)))
def
trace_op
(
self
,
op
,
stop_gradient
=
False
):
def
trace_op
(
self
,
op
,
inputs
,
outputs
,
stop_gradient
=
False
):
# TODO(minqiyang): remove this line after we take apart all
# backward grads and forward variables
if
self
.
_train_mode
:
op
.
inputs
=
inputs
inps
=
defaultdict
(
list
)
for
k
,
vars
in
six
.
iteritems
(
inputs
):
if
isinstance
(
vars
,
framework
.
Variable
):
inps
[
k
].
append
(
vars
.
_ivar
)
elif
isinstance
(
vars
,
list
)
or
isinstance
(
vars
,
tuple
):
for
var
in
vars
:
inps
[
k
].
append
(
var
.
_ivar
)
op
.
outputs
=
outputs
outs
=
defaultdict
(
list
)
for
k
,
vars
in
six
.
iteritems
(
outputs
):
if
isinstance
(
vars
,
framework
.
Variable
):
outs
[
k
].
append
(
vars
.
_ivar
)
elif
isinstance
(
vars
,
list
)
or
isinstance
(
vars
,
tuple
):
for
var
in
vars
:
outs
[
k
].
append
(
var
.
_ivar
)
else
:
inps
=
defaultdict
(
list
)
for
k
,
vars
in
six
.
iteritems
(
inputs
):
if
isinstance
(
vars
,
framework
.
Variable
):
op
.
previous_ops
.
append
(
vars
.
op
)
inps
[
k
].
append
(
vars
.
_ivar
)
elif
isinstance
(
vars
,
list
)
or
isinstance
(
vars
,
tuple
):
for
var
in
vars
:
op
.
previous_ops
.
append
(
var
.
op
)
inps
[
k
].
append
(
var
.
_ivar
)
op
.
outputs
=
outputs
outs
=
defaultdict
(
list
)
for
k
,
vars
in
six
.
iteritems
(
outputs
):
if
isinstance
(
vars
,
framework
.
Variable
):
vars
.
op
=
op
outs
[
k
].
append
(
vars
.
_ivar
)
elif
isinstance
(
vars
,
list
)
or
isinstance
(
vars
,
tuple
):
for
var
in
vars
:
var
.
op
=
op
outs
[
k
].
append
(
var
.
_ivar
)
# record op's trace id
op
.
iop
.
_trace_id
=
self
.
_trace_id
backward_refs
=
self
.
trace
(
op
.
iop
,
op
.
inputs
,
op
.
outp
uts
,
op
.
attrs
,
backward_refs
=
self
.
trace
(
op
.
iop
,
inps
,
o
uts
,
op
.
attrs
,
framework
.
_current_expected_place
(),
stop_gradient
)
if
not
stop_gradient
:
if
not
stop_gradient
and
self
.
_train_mode
:
self
.
_trace_id
+=
1
self
.
_ops
[
op
.
iop
.
_trace_id
]
=
op
...
...
@@ -65,10 +110,16 @@ class Tracer(core.Tracer):
# TODO(minqiyang): remove all inputs and outputs after separate
# var and grad
op
.
backward_refs
=
defaultdict
(
list
)
for
k
,
v
in
six
.
iteritems
(
op
.
inputs
):
for
k
,
v
in
six
.
iteritems
(
inputs
):
if
k
in
backward_refs
:
op
.
backward_refs
[
k
]
=
op
.
inputs
[
k
]
op
.
backward_refs
[
k
]
=
inputs
[
k
]
for
k
,
v
in
six
.
iteritems
(
o
p
.
o
utputs
):
for
k
,
v
in
six
.
iteritems
(
outputs
):
if
k
in
backward_refs
:
op
.
backward_refs
[
k
]
=
op
.
outputs
[
k
]
op
.
backward_refs
[
k
]
=
outputs
[
k
]
def
_train_mode
(
self
):
self
.
_train_mode
=
True
def
_eval_mode
(
self
):
self
.
_train_mode
=
False
python/paddle/fluid/framework.py
浏览文件 @
102fc859
...
...
@@ -411,6 +411,7 @@ class Variable(object):
if
persistable
else
False
)
if
persistable
:
_dygraph_tracer
().
trace_var
(
name
,
self
)
self
.
op
=
None
else
:
self
.
error_clip
=
error_clip
...
...
@@ -939,24 +940,7 @@ class Operator(object):
raise
ValueError
(
"`type` to initialized an Operator can not be None."
)
self
.
iop
=
core
.
OpBase
(
type
)
# TODO(minqiyang): remove these lines after we take apart all
# backward grads and forward variables
self
.
inputs
=
defaultdict
(
list
)
if
inputs
is
not
None
:
for
k
,
v
in
six
.
iteritems
(
inputs
):
if
isinstance
(
v
,
Variable
):
self
.
inputs
[
k
].
append
(
v
.
_ivar
)
elif
isinstance
(
v
,
list
)
or
isinstance
(
v
,
tuple
):
self
.
inputs
[
k
].
extend
([
var
.
_ivar
for
var
in
v
])
self
.
outputs
=
defaultdict
(
list
)
if
outputs
is
not
None
:
for
k
,
v
in
six
.
iteritems
(
outputs
):
if
isinstance
(
v
,
Variable
):
self
.
outputs
[
k
].
append
(
v
.
_ivar
)
elif
isinstance
(
v
,
list
)
or
isinstance
(
v
,
tuple
):
self
.
outputs
[
k
].
extend
([
var
.
_ivar
for
var
in
v
])
self
.
previous_ops
=
[]
self
.
attrs
=
attrs
if
attrs
else
{}
else
:
...
...
@@ -1647,15 +1631,18 @@ class Block(object):
block
=
self
,
desc
=
None
,
type
=
kwargs
.
get
(
"type"
,
None
),
inputs
=
kwargs
.
get
(
"inputs"
,
None
)
,
outputs
=
kwargs
.
get
(
"outputs"
,
None
)
,
attrs
=
kwargs
.
get
(
"attrs"
,
None
))
inputs
=
None
,
outputs
=
None
,
attrs
=
kwargs
.
get
(
"attrs"
,
{}
))
# record ops in tracer rather than blocks
#
# TODO(minqiyang): add op stop_gradient support in static mode too.
# currently, we only support stop_gradient in dygraph mode.
_dygraph_tracer
().
trace_op
(
op
,
kwargs
.
get
(
"stop_gradient"
,
False
))
_dygraph_tracer
().
trace_op
(
op
,
kwargs
.
get
(
"inputs"
,
{}),
kwargs
.
get
(
"outputs"
,
{}),
kwargs
.
get
(
"stop_gradient"
,
False
))
else
:
op_desc
=
self
.
desc
.
append_op
()
op
=
Operator
(
...
...
@@ -1719,10 +1706,14 @@ class Block(object):
self
,
None
,
type
=
kwargs
.
get
(
"type"
,
None
),
inputs
=
kwargs
.
get
(
"inputs"
,
None
),
outputs
=
kwargs
.
get
(
"outputs"
,
None
),
attrs
=
kwargs
.
get
(
"attrs"
,
None
))
_dygraph_tracer
().
trace_op
(
op
,
kwargs
.
get
(
"stop_gradient"
,
False
))
inputs
=
None
,
outputs
=
None
,
attrs
=
kwargs
.
get
(
"attrs"
,
{}))
_dygraph_tracer
().
trace_op
(
op
,
kwargs
.
get
(
"inputs"
,
{}),
kwargs
.
get
(
"outputs"
,
{}),
kwargs
.
get
(
"stop_gradient"
,
False
))
else
:
op_desc
=
self
.
desc
.
_prepend_op
()
op
=
Operator
(
...
...
python/paddle/fluid/tests/unittests/test_dygraph_multi_forward.py
0 → 100644
浏览文件 @
102fc859
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
print_function
import
contextlib
import
unittest
import
numpy
as
np
import
six
import
paddle
import
paddle.fluid
as
fluid
from
paddle.fluid
import
core
from
paddle.fluid.optimizer
import
SGDOptimizer
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Pool2D
,
FC
from
paddle.fluid.dygraph.base
import
to_variable
from
test_imperative_base
import
new_program_scope
class
SimpleImgConvPool
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
name_scope
,
num_channels
,
num_filters
,
filter_size
,
pool_size
,
pool_stride
,
pool_padding
=
0
,
pool_type
=
'max'
,
global_pooling
=
False
,
conv_stride
=
1
,
conv_padding
=
0
,
conv_dilation
=
1
,
conv_groups
=
1
,
act
=
None
,
use_cudnn
=
False
,
param_attr
=
None
,
bias_attr
=
None
):
super
(
SimpleImgConvPool
,
self
).
__init__
(
name_scope
)
self
.
_conv2d
=
Conv2D
(
self
.
full_name
(),
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
conv_stride
,
padding
=
conv_padding
,
dilation
=
conv_dilation
,
groups
=
conv_groups
,
param_attr
=
None
,
bias_attr
=
None
,
use_cudnn
=
use_cudnn
)
self
.
_pool2d
=
Pool2D
(
self
.
full_name
(),
pool_size
=
pool_size
,
pool_type
=
pool_type
,
pool_stride
=
pool_stride
,
pool_padding
=
pool_padding
,
global_pooling
=
global_pooling
,
use_cudnn
=
use_cudnn
)
def
forward
(
self
,
inputs
):
x
=
self
.
_conv2d
(
inputs
)
x
=
self
.
_pool2d
(
x
)
return
x
class
MNIST
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
name_scope
):
super
(
MNIST
,
self
).
__init__
(
name_scope
)
self
.
_simple_img_conv_pool_1
=
SimpleImgConvPool
(
self
.
full_name
(),
1
,
20
,
5
,
2
,
2
,
act
=
"relu"
)
self
.
_simple_img_conv_pool_2
=
SimpleImgConvPool
(
self
.
full_name
(),
20
,
50
,
5
,
2
,
2
,
act
=
"relu"
)
pool_2_shape
=
50
*
4
*
4
SIZE
=
10
scale
=
(
2.0
/
(
pool_2_shape
**
2
*
SIZE
))
**
0.5
self
.
_fc
=
FC
(
self
.
full_name
(),
10
,
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
NormalInitializer
(
loc
=
0.0
,
scale
=
scale
)),
act
=
"softmax"
)
def
forward
(
self
,
inputs
):
x
=
self
.
_simple_img_conv_pool_1
(
inputs
)
x
=
self
.
_simple_img_conv_pool_2
(
x
)
x
=
self
.
_fc
(
x
)
return
x
class
TestDygraphMultiForward
(
unittest
.
TestCase
):
def
test_mnist_forward_float32
(
self
):
seed
=
90
epoch_num
=
1
with
fluid
.
dygraph
.
guard
():
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
mnist
=
MNIST
(
"mnist"
)
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
train
(),
batch_size
=
128
,
drop_last
=
True
)
dy_param_init_value
=
{}
mnist
.
eval
()
for
epoch
in
range
(
epoch_num
):
for
batch_id
,
data
in
enumerate
(
train_reader
()):
dy_x_data
=
np
.
array
(
[
x
[
0
].
reshape
(
1
,
28
,
28
)
for
x
in
data
]).
astype
(
'float32'
)
y_data
=
np
.
array
(
[
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
(
128
,
1
)
img
=
to_variable
(
dy_x_data
)
label
=
to_variable
(
y_data
)
label
.
stop_gradient
=
True
cost
=
mnist
(
img
)
loss
=
fluid
.
layers
.
cross_entropy
(
cost
,
label
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
dy_out
=
avg_loss
.
numpy
()
if
epoch
==
0
and
batch_id
==
0
:
for
param
in
mnist
.
parameters
():
dy_param_init_value
[
param
.
name
]
=
param
.
numpy
()
with
new_program_scope
():
fluid
.
default_startup_program
().
random_seed
=
seed
fluid
.
default_main_program
().
random_seed
=
seed
exe
=
fluid
.
Executor
(
fluid
.
CPUPlace
(
)
if
not
core
.
is_compiled_with_cuda
()
else
fluid
.
CUDAPlace
(
0
))
mnist
=
MNIST
(
"mnist"
)
sgd
=
SGDOptimizer
(
learning_rate
=
1e-3
)
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
mnist
.
train
(),
batch_size
=
128
,
drop_last
=
True
)
img
=
fluid
.
layers
.
data
(
name
=
'pixel'
,
shape
=
[
1
,
28
,
28
],
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
cost
=
mnist
(
img
)
loss
=
fluid
.
layers
.
cross_entropy
(
cost
,
label
)
avg_loss
=
fluid
.
layers
.
mean
(
loss
)
# initialize params and fetch them
static_param_init_value
=
{}
static_param_name_list
=
[]
for
param
in
mnist
.
parameters
():
static_param_name_list
.
append
(
param
.
name
)
out
=
exe
.
run
(
fluid
.
default_startup_program
(),
fetch_list
=
static_param_name_list
)
for
i
in
range
(
len
(
static_param_name_list
)):
static_param_init_value
[
static_param_name_list
[
i
]]
=
out
[
i
]
for
epoch
in
range
(
epoch_num
):
for
batch_id
,
data
in
enumerate
(
train_reader
()):
static_x_data
=
np
.
array
(
[
x
[
0
].
reshape
(
1
,
28
,
28
)
for
x
in
data
]).
astype
(
'float32'
)
y_data
=
np
.
array
(
[
x
[
1
]
for
x
in
data
]).
astype
(
'int64'
).
reshape
([
128
,
1
])
fetch_list
=
[
avg_loss
.
name
]
out
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
"pixel"
:
static_x_data
,
"label"
:
y_data
},
fetch_list
=
fetch_list
)
static_out
=
out
[
0
]
self
.
assertTrue
(
np
.
allclose
(
dy_x_data
.
all
(),
static_x_data
.
all
()))
for
key
,
value
in
six
.
iteritems
(
static_param_init_value
):
self
.
assertTrue
(
np
.
allclose
(
value
,
dy_param_init_value
[
key
]))
self
.
assertTrue
(
np
.
allclose
(
static_out
,
dy_out
))
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录