Skip to content

Commit de605cc

Browse files
authored
Merge pull request #16523 from junjun315/tensor_api
move imperative to dygraph
2 parents 1096746 + 1c9aaee commit de605cc

33 files changed

+209
-207
lines changed

python/paddle/fluid/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from . import evaluator
3535
from . import initializer
3636
from . import layers
37-
from . import imperative
37+
from . import dygraph
3838
from . import contrib
3939
from . import nets
4040
from . import optimizer
@@ -71,7 +71,7 @@
7171
'initializer',
7272
'layers',
7373
'contrib',
74-
'imperative',
74+
'dygraph',
7575
'transpiler',
7676
'nets',
7777
'optimizer',

python/paddle/fluid/imperative/base.py renamed to python/paddle/fluid/dygraph/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
def enabled():
25-
return framework._in_imperative_mode()
25+
return framework._in_dygraph_mode()
2626

2727

2828
@signature_safe_contextmanager
@@ -39,14 +39,14 @@ def guard(place=None):
3939

4040
with framework.program_guard(train, startup):
4141
with framework.unique_name.guard():
42-
with framework._imperative_guard(tracer):
43-
with framework._imperative_place_guard(place):
42+
with framework._dygraph_guard(tracer):
43+
with framework._dygraph_place_guard(place):
4444
yield
4545

4646

4747
def to_variable(value, block=None, name=None):
4848
if isinstance(value, np.ndarray):
49-
assert enabled(), "to_variable could only be called in imperative mode"
49+
assert enabled(), "to_variable could only be called in dygraph mode"
5050

5151
if not block:
5252
block = framework.default_main_program().current_block()

python/paddle/fluid/imperative/checkpoint.py renamed to python/paddle/fluid/dygraph/checkpoint.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def save_persistables(vardict, dirname, filename=None):
6868
dy_loss, last_hidden, last_cell = ptb_model(x, y, init_hidden,
6969
init_cell)
7070
param_path = "./my_paddle_model"
71-
fluid.imperative.checkpoint.save_persistables(ptb_model.state_dict(), dirname=param_path,
71+
fluid.dygraph.save_persistables(ptb_model.state_dict(), dirname=param_path,
7272
layer=ptb_model)
7373
"""
7474
if isinstance(vardict, collections.OrderedDict):
@@ -97,17 +97,17 @@ def load_persistables(vardict, dirname, filename=None):
9797
9898
Examples:
9999
.. code-block:: python
100-
my_layer = layer(fluid.imperative.Layer)
100+
my_layer = layer(fluid.dygraph.Layer)
101101
param_path = "./my_paddle_model"
102102
103-
param_dict = fluid.imperative.checkpoint.load_persistables(my_layer.parameters(), param_path)
103+
param_dict = fluid.dygraph.load_persistables(my_layer.parameters(), param_path)
104104
param_1 = param_dict['PtbModel_0.w_1']
105105
106106
or:
107-
my_layer = layer(fluid.imperative.Layer)
107+
my_layer = layer(fluid.dygraph.Layer)
108108
param_path = "./my_paddle_model"
109109
filename = "model.file"
110-
param_dict = fluid.imperative.checkpoint.load_persistables(my_layer.state_dict(), param_path,
110+
param_dict = fluid.dygraph.load_persistables(my_layer.state_dict(), param_path,
111111
filename=filename)
112112
param_1 = param_dict['PtbModel_0.w_1']
113113

python/paddle/fluid/imperative/layer_object_helper.py renamed to python/paddle/fluid/dygraph/layer_object_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import copy
1818
import six
19-
from ..framework import Parameter, _in_imperative_mode
19+
from ..framework import Parameter, _in_dygraph_mode
2020
from ..param_attr import ParamAttr
2121
from .. import core
2222
from six.moves import zip

python/paddle/fluid/imperative/layers.py renamed to python/paddle/fluid/dygraph/layers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def backward(*douts):
283283

284284
@classmethod
285285
def __call__(cls, *inputs):
286-
tracer = framework._imperative_tracer()
286+
tracer = framework._dygraph_tracer()
287287
block = framework.default_main_program().current_block()
288288
ivar_inputs = [x._ivar for x in inputs]
289289

python/paddle/fluid/imperative/nn.py renamed to python/paddle/fluid/dygraph/nn.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def forward(self, input):
133133
outputs={'Out': [pre_act]},
134134
attrs={'axis': 1})
135135

136-
# Currently, we don't support inplace in imperative mode
136+
# Currently, we don't support inplace in dygraph mode
137137
return self._helper.append_activation(pre_act, act=self._act)
138138

139139

@@ -265,7 +265,7 @@ def forward(self, input):
265265
attrs={'axis': self._num_flatten_dims})
266266
else:
267267
pre_activation = pre_bias
268-
# Currently, we don't support inplace in imperative mode
268+
# Currently, we don't support inplace in dygraph mode
269269
return self._helper.append_activation(pre_activation, act=self._act)
270270

271271

@@ -387,7 +387,7 @@ def forward(self, input):
387387
"use_global_stats": self._use_global_stats
388388
})
389389

390-
# Currently, we don't support inplace in imperative mode
390+
# Currently, we don't support inplace in dygraph mode
391391
return self._helper.append_activation(batch_norm_out, self._act)
392392

393393

@@ -426,7 +426,7 @@ class Embedding(layers.Layer):
426426
427427
dict_size = len(dataset.ids)
428428
input = fluid.layers.data(name='ids', shape=[32, 32], dtype='float32')
429-
embedding = fluid.imperative.Embedding(size=[dict_size, 16])
429+
embedding = fluid.dygraph.Embedding(size=[dict_size, 16])
430430
fc = embedding(input)
431431
"""
432432

python/paddle/fluid/imperative/tracer.py renamed to python/paddle/fluid/dygraph/tracer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424

2525

2626
def release_op(op):
27-
del framework._imperative_tracer()._ops[op._trace_id]
27+
del framework._dygraph_tracer()._ops[op._trace_id]
2828

2929

3030
class Tracer(core.Tracer):
3131
"""
32-
Python wrapper of imperative tracer
32+
Python wrapper of dygraph tracer
3333
"""
3434

3535
def __init__(self, block):

python/paddle/fluid/framework.py

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,20 @@
7575
ZERO_VAR_SUFFIX = core.kZeroVarSuffix()
7676
CONTROL_DEP_VAR_PREFIX = core.kControlDepVarName()
7777

78-
_imperative_tracer_ = None
79-
_imperative_current_expected_place_ = None
78+
_dygraph_tracer_ = None
79+
_dygraph_current_expected_place_ = None
8080

8181

82-
def _in_imperative_mode():
83-
return _imperative_tracer_ is not None
82+
def _in_dygraph_mode():
83+
return _dygraph_tracer_ is not None
8484

8585

86-
def _imperative_tracer():
87-
return _imperative_tracer_
86+
def _dygraph_tracer():
87+
return _dygraph_tracer_
8888

8989

9090
def _current_expected_place():
91-
return _imperative_current_expected_place_
91+
return _dygraph_current_expected_place_
9292

9393

9494
def _cpu_num():
@@ -396,7 +396,7 @@ def __init__(self,
396396
if not isinstance(dtype, core.VarDesc.VarType):
397397
dtype = convert_np_dtype_to_dtype_(dtype)
398398

399-
if _in_imperative_mode():
399+
if _in_dygraph_mode():
400400
# record vars in tracer rather than blocks
401401
self._ivar = kwargs.get("ivar", None)
402402
if not self._ivar:
@@ -406,7 +406,7 @@ def __init__(self,
406406
_current_expected_place(), stop_gradient, True
407407
if persistable else False)
408408
if persistable:
409-
_imperative_tracer().trace_var(name, self)
409+
_dygraph_tracer().trace_var(name, self)
410410
else:
411411
self.error_clip = error_clip
412412

@@ -515,8 +515,8 @@ def to_string(self, throw_on_error, with_details=False):
515515
Returns:
516516
str: The debug string.
517517
"""
518-
if _in_imperative_mode():
519-
# TODO(panyx0718): add more imperative debug info.
518+
if _in_dygraph_mode():
519+
# TODO(panyx0718): add more dygraph debug info.
520520
return 'name %s, dtype: %s shape: %s' % (self.name, self.dtype,
521521
self.shape)
522522

@@ -548,69 +548,69 @@ def _set_desc(self, input):
548548

549549
@property
550550
def _stop_gradient(self):
551-
if _in_imperative_mode():
551+
if _in_dygraph_mode():
552552
return self._ivar.stop_gradient
553553
else:
554554
return self.stop_gradient
555555

556556
@_stop_gradient.setter
557557
def _stop_gradient(self, s):
558-
if _in_imperative_mode():
558+
if _in_dygraph_mode():
559559
self._ivar.stop_gradient = s
560560
else:
561561
self.stop_gradient = s
562562

563563
@property
564564
def persistable(self):
565-
if _in_imperative_mode():
565+
if _in_dygraph_mode():
566566
return self._ivar.persistable
567567
else:
568568
return self.desc.persistable()
569569

570570
@persistable.setter
571571
def persistable(self, p):
572-
if _in_imperative_mode():
572+
if _in_dygraph_mode():
573573
return self._ivar.persistable
574574
else:
575575
self.desc.set_persistable(p)
576576

577577
@property
578578
def name(self):
579-
if _in_imperative_mode():
579+
if _in_dygraph_mode():
580580
return self._ivar.name
581581
else:
582582
return cpt.to_text(self.desc.name())
583583

584584
@name.setter
585585
def name(self, new_name):
586-
if _in_imperative_mode():
586+
if _in_dygraph_mode():
587587
self._ivar.name = new_name
588588
else:
589589
self.desc.set_name(new_name)
590590

591591
@property
592592
def shape(self):
593593
# convert to tuple, make it as same as numpy API.
594-
if _in_imperative_mode():
594+
if _in_dygraph_mode():
595595
return self._ivar.shape
596596
else:
597597
return tuple(self.desc.shape())
598598

599599
@property
600600
def dtype(self):
601-
if _in_imperative_mode():
601+
if _in_dygraph_mode():
602602
return self._ivar.dtype
603603
else:
604604
return self.desc.dtype()
605605

606606
@property
607607
def lod_level(self):
608-
# TODO(minqiyang): Support lod_level in imperative mode
608+
# TODO(minqiyang): Support lod_level in dygraph mode
609609
return self.desc.lod_level()
610610

611611
@property
612612
def type(self):
613-
if _in_imperative_mode():
613+
if _in_dygraph_mode():
614614
return self._ivar.dtype
615615
else:
616616
return self.desc.type()
@@ -918,7 +918,7 @@ def __init__(self,
918918
inputs=None,
919919
outputs=None,
920920
attrs=None):
921-
if _in_imperative_mode():
921+
if _in_dygraph_mode():
922922
if type is None:
923923
raise ValueError(
924924
"`type` to initialized an Operator can not be None.")
@@ -1037,7 +1037,7 @@ def find_name(var_list, name):
10371037
for arg in out_args:
10381038
out_arg_names.append(cpt.to_text(arg.name))
10391039
# TODO(minqiyang): could we remove variable's op in static mode?
1040-
if not _in_imperative_mode():
1040+
if not _in_dygraph_mode():
10411041
arg.op = self
10421042
self.desc.set_output(out_proto.name, out_arg_names)
10431043

@@ -1083,7 +1083,7 @@ def __str__(self):
10831083

10841084
@property
10851085
def type(self):
1086-
if _in_imperative_mode():
1086+
if _in_dygraph_mode():
10871087
return self.iop.type
10881088
else:
10891089
return self.desc.type()
@@ -1626,7 +1626,7 @@ def append_op(self, *args, **kwargs):
16261626
Returns:
16271627
Operator: the append Operator.
16281628
"""
1629-
if _in_imperative_mode():
1629+
if _in_dygraph_mode():
16301630
op = Operator(
16311631
block=self,
16321632
desc=None,
@@ -1638,9 +1638,8 @@ def append_op(self, *args, **kwargs):
16381638
# record ops in tracer rather than blocks
16391639
#
16401640
# TODO(minqiyang): add op stop_gradient support in static mode too.
1641-
# currently, we only support stop_gradient in imperative mode.
1642-
_imperative_tracer().trace_op(op,
1643-
kwargs.get("stop_gradient", False))
1641+
# currently, we only support stop_gradient in dygraph mode.
1642+
_dygraph_tracer().trace_op(op, kwargs.get("stop_gradient", False))
16441643
else:
16451644
op_desc = self.desc.append_op()
16461645
op = Operator(
@@ -1699,16 +1698,15 @@ def _slice_ops(self, start, end):
16991698
return self.ops[start:end]
17001699

17011700
def _prepend_op(self, *args, **kwargs):
1702-
if _in_imperative_mode():
1701+
if _in_dygraph_mode():
17031702
op = Operator(
17041703
self,
17051704
None,
17061705
type=kwargs.get("type", None),
17071706
inputs=kwargs.get("inputs", None),
17081707
outputs=kwargs.get("outputs", None),
17091708
attrs=kwargs.get("attrs", None))
1710-
_imperative_tracer().trace_op(op,
1711-
kwargs.get("stop_gradient", False))
1709+
_dygraph_tracer().trace_op(op, kwargs.get("stop_gradient", False))
17121710
else:
17131711
op_desc = self.desc._prepend_op()
17141712
op = Operator(
@@ -3511,22 +3509,22 @@ def _get_var(name, program=None):
35113509

35123510

35133511
@signature_safe_contextmanager
3514-
def _imperative_guard(tracer):
3515-
global _imperative_tracer_
3516-
tmp_trace = _imperative_tracer_
3517-
_imperative_tracer_ = tracer
3512+
def _dygraph_guard(tracer):
3513+
global _dygraph_tracer_
3514+
tmp_trace = _dygraph_tracer_
3515+
_dygraph_tracer_ = tracer
35183516

35193517
yield
35203518

3521-
_imperative_tracer_ = tmp_trace
3519+
_dygraph_tracer_ = tmp_trace
35223520

35233521

35243522
@signature_safe_contextmanager
3525-
def _imperative_place_guard(place):
3526-
global _imperative_current_expected_place_
3527-
tmp_place = _imperative_current_expected_place_
3528-
_imperative_current_expected_place_ = place
3523+
def _dygraph_place_guard(place):
3524+
global _dygraph_current_expected_place_
3525+
tmp_place = _dygraph_current_expected_place_
3526+
_dygraph_current_expected_place_ = place
35293527

35303528
yield
35313529

3532-
_imperative_current_expected_place_ = tmp_place
3530+
_dygraph_current_expected_place_ = tmp_place

0 commit comments

Comments
 (0)