Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish Non-Layer API #11531

Merged
merged 10 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion python/paddle/fluid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import transpiler
from param_attr import ParamAttr, WeightNormParamAttr
from data_feeder import DataFeeder
from core import LoDTensor, CPUPlace, CUDAPlace, CUDAPinnedPlace
from core import LoDTensor, CPUPlace, CUDAPlace, CUDAPinnedPlace, Scope
from transpiler import DistributeTranspiler, InferenceTranspiler, \
memory_optimize, release_memory
from concurrency import (Go, make_channel, channel_send, channel_recv,
Expand Down Expand Up @@ -83,6 +83,7 @@
'profiler',
'unique_name',
'recordio_writer',
'Scope',
]


Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def set_gradient_clip(clip, param_list=None, program=None):
def append_gradient_clip_ops(param_grad):
context = dict()
for p, g in param_grad:
with p.block.program.optimized_guard(p):
with p.block.program.optimization_guard(p):
clip_attr = getattr(p, 'gradient_clip_attr', NullGradientClipAttr())
if clip_attr is None:
clip_attr = NullGradientClipAttr()
Expand All @@ -228,7 +228,7 @@ def append_gradient_clip_ops(param_grad):

res = []
for p, g in param_grad:
with p.block.program.optimized_guard(p):
with p.block.program.optimization_guard(p):
res.append(clip_attr.create_operators(param=p, grad=g))

return res
Expand Down
20 changes: 20 additions & 0 deletions python/paddle/fluid/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@


def global_scope():
"""
Get the global/default scope instance. There are a lot of APIs use
:code:`global_scope` as its default value, e.g., :code:`Executor.run`

Returns:
Scope: The global/default scope instance.
"""
return g_scope


Expand All @@ -37,6 +44,19 @@ def switch_scope(scope):

@contextlib.contextmanager
def scope_guard(scope):
"""
Change the global/default scope instance by Python `with` statement. All
variable in runtime will assigned to the new scope.

Examples:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use

.. code-block:: python

for examples?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only >>> style is used in the examples of napoleon.

>>> import paddle.fluid as fluid
>>> new_scope = fluid.Scope()
>>> with fluid.scope_guard(new_scope):
>>> ...

Args:
scope: The new global/default scope.
"""
ex = switch_scope(scope)
yield
switch_scope(ex)
Expand Down
Loading