-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Python implementation for a proposed Go Op. #8434
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
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
497321a
Adding Python boilerplate code for Go op
f2a95fa
Add very basic test case
d785ca9
Adding the python logic for go routine
313ffb0
Fix syntax
a43924e
Changing test to notest
c97cf4a
Rename Routine to Go
39f22cf
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
fc2593c
Combining GoGuard and Go in one class
24737a6
Merge branch 'go_op' of https://github.com/abhinavarora/Paddle into g…
8343a35
Modify test
d0e26f7
Adding fluid close channel
ccb37b9
Merge branch 'go_op' of https://github.com/abhinavarora/Paddle into g…
a37a1e1
Fixing __init__.py for calling fluid.go()
2fb93ca
Merge branch 'go_op' of https://github.com/abhinavarora/Paddle into g…
f5f6370
Adding stubs for channel methods and updating test case
323a2e2
Removing import *
d02873b
Merge branch 'go_op' of github.com:abhinavarora/Paddle into go_op
04cb0f3
Merge remote-tracking branch 'origin/develop' into go_op
47f9680
Adding imports from concurrency
ee593e2
Merge remote-tracking branch 'origin/develop' into go_op
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
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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| # TODO: Variables: make_channel | ||
| # TODO: Operators: send, close_channel, recv, go, select | ||
| from layers.control_flow import BlockGuard | ||
| from layer_helper import LayerHelper | ||
|
|
||
| __all__ = [ | ||
| 'Go', | ||
| 'make_channel', | ||
| 'channel_send', | ||
| 'channel_recv', | ||
| 'channel_close', | ||
| ] | ||
|
|
||
|
|
||
| class Go(BlockGuard): | ||
| def __init__(self, name=None): | ||
| self.helper = LayerHelper("go", name=name) | ||
| super(Go, self).__init__(self.helper.main_program) | ||
|
|
||
| def __enter__(self): | ||
| super(Go, self).__enter__() | ||
|
|
||
| def __exit__(self, exc_type, exc_val, exc_tb): | ||
| if exc_type is not None: | ||
| return False | ||
| self.construct_go_op() | ||
| return super(Go, self).__exit__(exc_type, exc_val, exc_tb) | ||
|
|
||
| def construct_go_op(self): | ||
| main_program = self.helper.main_program | ||
| go_block = main_program.current_block() | ||
| parent_block = main_program.block(main_program.current_block() | ||
| .parent_idx) | ||
|
|
||
| x_name_list = set() | ||
| out_vars = set() | ||
| for op in go_block.ops: | ||
| # Iterate over all operators, get all the inputs | ||
| # and add as input to the Go operator. | ||
| for iname in op.input_names: | ||
| for in_var_name in op.input(iname): | ||
| x_name_list.add(in_var_name) | ||
|
|
||
| # Iterate over all operators , get all the outputs | ||
| # add to the output list of Go operator only if | ||
| # they exist in the parent block. | ||
| for oname in op.output_names: | ||
| for out_var_name in op.output(oname): | ||
| if out_var_name in parent_block.vars: | ||
| out_vars.add(parent_block.var(out_var_name)) | ||
|
|
||
| parent_block.append_op( | ||
| type='go', | ||
| inputs={'X': [parent_block.var(x_name) for x_name in x_name_list]}, | ||
| outputs={'Out': out_vars}, | ||
| attrs={'sub_block': go_block}) | ||
|
|
||
|
|
||
| def make_channel(dtype, size=0): | ||
| return True | ||
|
|
||
|
|
||
| def channel_send(channel, value): | ||
| return True | ||
|
|
||
|
|
||
| def channel_recv(channel): | ||
| return True | ||
|
|
||
|
|
||
| def channel_close(channel): | ||
| return True | ||
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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
| import paddle.v2.fluid as fluid | ||
| import paddle.v2.fluid.core as core | ||
| from paddle.v2.fluid.executor import Executor | ||
|
|
||
|
|
||
| class TestRoutineOp(unittest.TestCase): | ||
| def test_simple_routine(self): | ||
| ch = fluid.make_channel(dtype=bool) | ||
| with fluid.Go(): | ||
| fluid.channel_send(ch, True) | ||
|
|
||
| result = fluid.channel_recv(ch) | ||
| fluid.channel_close(ch) | ||
|
|
||
| cpu = core.CPUPlace() | ||
| exe = Executor(cpu) | ||
|
|
||
| outs = exe.run(fetch_list=[result]) | ||
| self.assertEqual(outs[0], True) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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.
I don't get it why would we need to define Go as a class?
In https://github.com/PaddlePaddle/Paddle/tree/develop/python/paddle/v2/fluid, I don't see a While class or an IfThenElse class.
Uh oh!
There was an error while loading. Please reload this page.
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.
Oh the While and IfElse classes are in https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/fluid/layers/control_flow.py . We thought we'll put all CSP related operators in
concurrency.pyand they need not be a part oflayersaswhileandifelseare so we put it right under thefluiddirectory.