Skip to content

Commit ea44157

Browse files
committed
Add APIs
1 parent f97c5d4 commit ea44157

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed

python/paddle/fluid/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import transpiler
4545
from param_attr import ParamAttr, WeightNormParamAttr
4646
from data_feeder import DataFeeder
47-
from core import LoDTensor, CPUPlace, CUDAPlace, CUDAPinnedPlace
47+
from core import LoDTensor, CPUPlace, CUDAPlace, CUDAPinnedPlace, Scope
4848
from transpiler import DistributeTranspiler, InferenceTranspiler, \
4949
memory_optimize, release_memory
5050
from concurrency import (Go, make_channel, channel_send, channel_recv,
@@ -83,6 +83,7 @@
8383
'profiler',
8484
'unique_name',
8585
'recordio_writer',
86+
'Scope',
8687
]
8788

8889

python/paddle/fluid/executor.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525

2626

2727
def global_scope():
28+
"""
29+
Get the global/default scope instance. There are a lot of APIs use
30+
:code:`global_scope` as its default value, e.g., :code:`Executor.run`
31+
32+
Returns:
33+
Scope: The global/default scope instance.
34+
"""
2835
return g_scope
2936

3037

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

3845
@contextlib.contextmanager
3946
def scope_guard(scope):
47+
"""
48+
Change the global/default scope instance by Python `with` statement. All
49+
variable in runtime will assigned to the new scope.
50+
51+
Examples:
52+
>>> import paddle.fluid as fluid
53+
>>> new_scope = fluid.Scope()
54+
>>> with fluid.scope_guard(new_scope):
55+
>>> ...
56+
57+
Args:
58+
scope: The new global/default scope.
59+
"""
4060
ex = switch_scope(scope)
4161
yield
4262
switch_scope(ex)

python/paddle/fluid/framework.py

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
'default_startup_program',
3131
'default_main_program',
3232
'program_guard',
33-
'switch_startup_program',
34-
'switch_main_program',
3533
'get_var',
3634
]
3735

@@ -1578,8 +1576,15 @@ def to_string(self, throw_on_error, with_details=False):
15781576

15791577
def default_startup_program():
15801578
"""
1581-
Get default startup program. In startup program, Paddle will initialize
1582-
parameters, initialize nccl handle, etc.
1579+
Get default/global startup program.
1580+
1581+
The layer function in :code:`fluid.layers` will create parameters, readers,
1582+
NCCL handles as global variables. The :code:`startup_program` will
1583+
initialize them by the operators in startup program. The layer function will
1584+
append these initialization operators into startup program.
1585+
1586+
This method will return the :code:`default` or the :code:`current` startup
1587+
program. Users can use :code:`fluid.program_guard` to switch program.
15831588
15841589
Returns:
15851590
Program: startup program
@@ -1589,7 +1594,15 @@ def default_startup_program():
15891594

15901595
def default_main_program():
15911596
"""
1592-
Get default main program. The main program is used for training or testing.
1597+
Get default/global main program. The main program is used for training or
1598+
testing.
1599+
1600+
All layer function in :code:`fluid.layers` will append operators and
1601+
variables to the :code:`default_main_program`.
1602+
1603+
The :code:`default_main_program` is the default program in a lot of APIs.
1604+
For example, the :code:`Executor.run()` will execute the
1605+
:code:`default_main_program` when the program is not specified.
15931606
15941607
Returns:
15951608
Program: main program
@@ -1631,20 +1644,34 @@ def switch_startup_program(program):
16311644
@contextlib.contextmanager
16321645
def program_guard(main_program, startup_program=None):
16331646
"""
1634-
Switch program with `with` statement
1647+
Change the global main program and startup program with `with` statement.
1648+
Layer functions in the Python `with` block will append operators and
1649+
variables to the new main programs.
1650+
1651+
Examples:
1652+
1653+
>>> import paddle.fluid as fluid
1654+
>>> main_program = fluid.Program()
1655+
>>> startup_program = fluid.Program()
1656+
>>> with fluid.program_guard(main_program, startup_program):
1657+
>>> data = fluid.layers.data(...)
1658+
>>> hidden = fluid.layers.fc(...)
1659+
1660+
Notes: The temporary :code:`Program` can be used if the user does not need
1661+
to construct either of startup program or main program.
16351662
16361663
Examples:
1637-
>>> with program_guard(Program()):
1638-
>>> data = fluid.layers.data(...)
1639-
>>> hidden = fluid.layers.fc(...)
1664+
1665+
>>> import paddle.fluid as fluid
1666+
>>> main_program = fluid.Program()
1667+
>>> # does not care about startup program. Just pass a temporary value.
1668+
>>> with fluid.program_guard(main_program, fluid.Program()):
1669+
>>> data = ...
16401670
16411671
Args:
1642-
main_program(Program): New main program inside `with` statement
1672+
main_program(Program): New main program inside `with` statement.
16431673
startup_program(Program): New startup program inside `with` statement.
16441674
None means do not change startup program.
1645-
1646-
Returns:
1647-
None
16481675
"""
16491676
if not isinstance(main_program, Program):
16501677
raise TypeError("main_program should be Program")
@@ -1661,7 +1688,8 @@ def program_guard(main_program, startup_program=None):
16611688

16621689
def get_var(name, program=None):
16631690
"""
1664-
Get a variable by name from the global block of a program
1691+
Get a variable by name from the global block of a program.
1692+
16651693
Args:
16661694
name(str): name of the variable
16671695
program(Program|None): program object.

python/paddle/fluid/transpiler/memory_optimization_transpiler.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ def memory_optimize(input_program, skip_opt_set=None, print_log=False, level=0):
383383

384384

385385
def release_memory(input_program, skip_opt_set=None):
386+
"""
387+
Modify the input program and insert :code:`delete_op` to early drop not used
388+
variables. The modification will be performed inplace.
389+
390+
Notes: This is an experimental API and could be removed in next few
391+
releases. Users should not use this API.
392+
393+
Args:
394+
input_program(Program): The program will be inserted :code:`delete_op`.
395+
"""
386396
cfgs = _get_cfgs(input_program)
387397
for cfg in cfgs:
388398
cfg.release_memory(skip_opt_set=skip_opt_set)

0 commit comments

Comments
 (0)