-
Notifications
You must be signed in to change notification settings - Fork 225
Share persistent values among processes #486
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
16 commits
Select commit
Hold shift + click to select a range
9a26e2a
Share persistent values among processes
muupan 5c120ba
Add namedpersistent function
muupan fbafd1a
Support persistent values of child links
muupan ae1efd1
Add namedpersistent under chainerrl.misc
muupan 70a2df1
Restore import
muupan 03f46cc
Check if all the keys are consumed
muupan 2b9066d
Fix error in test
muupan ae87f41
Fix error of splitting persistent_name
muupan ae4b6be
Test with a deeper chain structure
muupan 6ad630d
Merge branch 'master' into share-persistent-values
muupan bafbfad
Merge branch 'master' into share-persistent-values
muupan 5ff9d8d
Merge branch 'master' into share-persistent-values
muupan 5755ccb
Fix the no keyword argument error of python 2
muupan 5256c93
Explain why a 1-dim array is used
muupan 4aaf766
Update chainerrl/misc/async_.py
muupan b82b710
Simplify namedpersistent by _namedchildren
muupan 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
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,40 @@ | ||
| from __future__ import unicode_literals | ||
| from __future__ import print_function | ||
| from __future__ import division | ||
| from __future__ import absolute_import | ||
| from builtins import * # NOQA | ||
| from future import standard_library | ||
| standard_library.install_aliases() # NOQA | ||
|
|
||
| import chainer | ||
|
|
||
|
|
||
| def _namedchildren(link): | ||
| if isinstance(link, chainer.Chain): | ||
| for name in sorted(link._children): | ||
| yield name, link.__dict__[name] | ||
| elif isinstance(link, chainer.ChainList): | ||
| for idx, child in enumerate(link._children): | ||
| yield str(idx), child | ||
|
|
||
|
|
||
| def namedpersistent(link): | ||
| """Return a generator of all (path, persistent) pairs for a given link. | ||
|
|
||
| This function is adopted from https://github.com/chainer/chainer/pull/6788. | ||
| Once it is merged into Chainer, we should use the property instead. | ||
|
|
||
| Args: | ||
| link (chainer.Link): Link. | ||
|
|
||
| Returns: | ||
| A generator object that generates all (path, persistent) pairs. | ||
| The paths are relative from this link. | ||
| """ | ||
| d = link.__dict__ | ||
| for name in sorted(link._persistent): | ||
| yield '/' + name, d[name] | ||
| for name, child in _namedchildren(link): | ||
| prefix = '/' + name | ||
| for path, persistent in namedpersistent(child): | ||
| yield prefix + path, persistent |
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,52 @@ | ||
| from __future__ import unicode_literals | ||
| from __future__ import print_function | ||
| from __future__ import division | ||
| from __future__ import absolute_import | ||
| from builtins import * # NOQA | ||
| from future import standard_library | ||
| standard_library.install_aliases() # NOQA | ||
|
|
||
| import chainer | ||
| import numpy | ||
|
|
||
| import chainerrl | ||
|
|
||
|
|
||
| def test_namedpersistent(): | ||
| # This test case is adopted from | ||
| # https://github.com/chainer/chainer/pull/6788 | ||
|
|
||
| l1 = chainer.Link() | ||
| with l1.init_scope(): | ||
| l1.x = chainer.Parameter(shape=(2, 3)) | ||
|
|
||
| l2 = chainer.Link() | ||
| with l2.init_scope(): | ||
| l2.x = chainer.Parameter(shape=2) | ||
| l2.add_persistent( | ||
| 'l2_a', numpy.array([1, 2, 3], dtype=numpy.float32)) | ||
|
|
||
| l3 = chainer.Link() | ||
| with l3.init_scope(): | ||
| l3.x = chainer.Parameter() | ||
| l3.add_persistent( | ||
| 'l3_a', numpy.array([1, 2, 3], dtype=numpy.float32)) | ||
|
|
||
| c1 = chainer.Chain() | ||
| with c1.init_scope(): | ||
| c1.l1 = l1 | ||
| c1.add_link('l2', l2) | ||
| c1.add_persistent( | ||
| 'c1_a', numpy.array([1, 2, 3], dtype=numpy.float32)) | ||
|
|
||
| c2 = chainer.Chain() | ||
| with c2.init_scope(): | ||
| c2.c1 = c1 | ||
| c2.l3 = l3 | ||
| c2.add_persistent( | ||
| 'c2_a', numpy.array([1, 2, 3], dtype=numpy.float32)) | ||
| namedpersistent = list(chainerrl.misc.namedpersistent(c2)) | ||
| assert ( | ||
| [(name, id(p)) for name, p in namedpersistent] == | ||
| [('/c2_a', id(c2.c2_a)), ('/c1/c1_a', id(c2.c1.c1_a)), | ||
| ('/c1/l2/l2_a', id(c2.c1.l2.l2_a)), ('/l3/l3_a', id(c2.l3.l3_a))]) |
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.
Uh oh!
There was an error while loading. Please reload this page.