-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Feature/switch program #5932
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
Merged
reyoung
merged 13 commits into
PaddlePaddle:develop
from
reyoung:feature/switch_program
Nov 30, 2017
Merged
Feature/switch program #5932
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a712cd5
Unify fluid submodules to fluid module
reyoung 012f8cd
Remove g_main_program/g_startup_program
reyoung 8f03f2e
Merge branch 'develop' of github.com:baidu/Paddle into feature/remove…
reyoung 0819ef2
Typo
reyoung 758517c
Add API for switch default program
reyoung 979eeda
Merge branch 'develop' into feature/switch_program
reyoung 3c231e2
Merge branch 'develop' of github.com:baidu/Paddle into feature/switch…
reyoung 46b2deb
Merge branch 'develop' of github.com:baidu/Paddle into feature/switch…
reyoung fe7e2b4
Fix CI
reyoung 0ccd123
Fix CI
reyoung 63945ae
Merge branch 'develop' into feature/switch_program
reyoung a4e0533
Merge branch 'develop' into feature/switch_program
reyoung 6cde98d
Fix CI
reyoung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ | |
import numpy as np | ||
from . import core | ||
import proto.framework_pb2 as framework_pb2 | ||
import contextlib | ||
|
||
__all__ = [ | ||
'Block', 'Variable', 'Program', 'Operator', 'default_startup_program', | ||
'default_main_program' | ||
'default_main_program', 'program_guard', 'switch_startup_program', | ||
'switch_main_program' | ||
] | ||
|
||
|
||
|
@@ -659,8 +661,83 @@ def __init__(self, block, shape, dtype, **kwargs): | |
|
||
|
||
def default_startup_program(): | ||
""" | ||
Get default startup program. In startup program, Paddle will initialize | ||
parameters, initialize nccl handle, etc. | ||
|
||
Returns: | ||
Program: startup program | ||
""" | ||
return _startup_program_ | ||
|
||
|
||
def default_main_program(): | ||
""" | ||
Get default main program. The main program is used for training or testing. | ||
|
||
Returns: | ||
Program: main program | ||
""" | ||
return _main_program_ | ||
|
||
|
||
def switch_main_program(program): | ||
""" | ||
Switch the main program to a new program. | ||
|
||
Args: | ||
program(Program): The new main program | ||
|
||
Returns: | ||
Program: The previous main program | ||
""" | ||
global _main_program_ | ||
prev_program = _main_program_ | ||
_main_program_ = program | ||
return prev_program | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, why return |
||
|
||
|
||
def switch_startup_program(program): | ||
""" | ||
Switch the startup program to a new program | ||
Args: | ||
program(Program): The new startup program | ||
|
||
Returns: | ||
Program: The previous startup program | ||
""" | ||
global _startup_program_ | ||
prev_program = _startup_program_ | ||
_startup_program_ = program | ||
return prev_program | ||
|
||
|
||
@contextlib.contextmanager | ||
def program_guard(main_program, startup_program=None): | ||
""" | ||
Switch program with `with` statement | ||
|
||
Examples: | ||
>>> with program_guard(Program()): | ||
>>> data = fluid.layers.data(...) | ||
>>> hidden = fluid.layers.fc(...) | ||
|
||
Args: | ||
main_program(Program): New main program inside `with` statement | ||
startup_program(Program): New startup program inside `with` statement. | ||
None means do not change startup program. | ||
|
||
Returns: | ||
None | ||
""" | ||
if not isinstance(main_program, Program): | ||
raise TypeError("main_program should be Program") | ||
main_program = switch_main_program(main_program) | ||
if startup_program is not None: | ||
if not isinstance(startup_program, Program): | ||
raise TypeError("startup_program should be Program") | ||
startup_program = switch_startup_program(startup_program) | ||
yield | ||
switch_main_program(main_program) | ||
if startup_program is not None: | ||
switch_startup_program(startup_program) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need this
default_main_program()
function, not use_main_program_
directly?