30
30
'default_startup_program' ,
31
31
'default_main_program' ,
32
32
'program_guard' ,
33
- 'switch_startup_program' ,
34
- 'switch_main_program' ,
35
33
'get_var' ,
36
34
]
37
35
@@ -1578,8 +1576,15 @@ def to_string(self, throw_on_error, with_details=False):
1578
1576
1579
1577
def default_startup_program ():
1580
1578
"""
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.
1583
1588
1584
1589
Returns:
1585
1590
Program: startup program
@@ -1589,7 +1594,15 @@ def default_startup_program():
1589
1594
1590
1595
def default_main_program ():
1591
1596
"""
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.
1593
1606
1594
1607
Returns:
1595
1608
Program: main program
@@ -1631,20 +1644,34 @@ def switch_startup_program(program):
1631
1644
@contextlib .contextmanager
1632
1645
def program_guard (main_program , startup_program = None ):
1633
1646
"""
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.
1635
1662
1636
1663
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 = ...
1640
1670
1641
1671
Args:
1642
- main_program(Program): New main program inside `with` statement
1672
+ main_program(Program): New main program inside `with` statement.
1643
1673
startup_program(Program): New startup program inside `with` statement.
1644
1674
None means do not change startup program.
1645
-
1646
- Returns:
1647
- None
1648
1675
"""
1649
1676
if not isinstance (main_program , Program ):
1650
1677
raise TypeError ("main_program should be Program" )
@@ -1661,7 +1688,8 @@ def program_guard(main_program, startup_program=None):
1661
1688
1662
1689
def get_var (name , program = None ):
1663
1690
"""
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
+
1665
1693
Args:
1666
1694
name(str): name of the variable
1667
1695
program(Program|None): program object.
0 commit comments