-
Notifications
You must be signed in to change notification settings - Fork 57
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
Support a wider range of dynamically initialized models for MultiNodeOptimizer #148
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
980a67f
add a test for dynamic network
shu65 ef19f3e
fix MultiNodeOptimizer
shu65 62bebe1
delte needs_broadcast
shu65 1ac8f9e
fix flake8 errors
shu65 fa61449
refactoring
shu65 a395236
Merge branch 'master' into test_for_dynamic_network
shu65 4e5a7fd
Merge branch 'master' into test_for_dynamic_network
shu65 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 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 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,231 @@ | ||
import chainer | ||
import chainer.testing | ||
import chainer.testing.attr | ||
import chainermn | ||
import mock | ||
import mpi4py.MPI | ||
import nose | ||
import numpy as np | ||
import unittest | ||
|
||
|
||
from chainermn.communicators import _communication_utility | ||
|
||
|
||
class ExampleModel(chainer.Chain): | ||
|
||
def __init__(self): | ||
super(ExampleModel, self).__init__( | ||
a=chainer.links.Linear(2, 3), | ||
b=chainer.links.Linear(3, 4), | ||
c=chainer.links.Linear(4, 5), | ||
) | ||
|
||
|
||
class TestMultiNodeOptimizer(unittest.TestCase): | ||
|
||
def setup_cpu(self): | ||
self.comm = chainermn.create_communicator('naive') | ||
self.target = ExampleModel() | ||
self.target.a.W.data[:] = self.comm.rank | ||
self.target.b.W.data[:] = self.comm.rank + 1 | ||
self.target.c.W.data[:] = self.comm.rank + 2 | ||
self.target.a.W.grad[:] = 0 | ||
self.target.b.W.grad[:] = 0 | ||
self.target.c.W.grad[:] = 0 | ||
self.actual_optimizer = chainer.GradientMethod() | ||
self.actual_optimizer.create_update_rule = mock.MagicMock | ||
|
||
def setup_gpu(self, device=None): | ||
self.comm = chainermn.create_communicator('hierarchical') | ||
device = self.comm.intra_rank | ||
chainer.cuda.get_device(device).use() | ||
self.target = ExampleModel() | ||
self.target.to_gpu() | ||
self.target.a.W.data[:] = self.comm.rank | ||
self.target.b.W.data[:] = self.comm.rank + 1 | ||
self.target.c.W.data[:] = self.comm.rank + 2 | ||
self.target.a.W.grad[:] = 0 | ||
self.target.b.W.grad[:] = 0 | ||
self.target.c.W.grad[:] = 0 | ||
self.actual_optimizer = chainer.GradientMethod() | ||
self.actual_optimizer.create_update_rule = mock.MagicMock | ||
|
||
def test_update_with_cpu(self): | ||
self.setup_cpu() | ||
self.optimizer = chainermn.create_multi_node_optimizer( | ||
self.actual_optimizer, self.comm) | ||
self.optimizer.setup(self.target) | ||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 0) | ||
self.optimizer.target.a.W.grad[:] = self.comm.rank | ||
self.optimizer.target.b.W.grad[:] = self.comm.rank + 1 | ||
self.optimizer.target.c.W.grad[:] = self.comm.rank + 2 | ||
|
||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 1) | ||
self.optimizer.target.a.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.a.W) | ||
self.optimizer.target.b.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.b.W) | ||
self.optimizer.target.c.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.c.W) | ||
|
||
base = (self.comm.size - 1.0) / 2 | ||
chainer.testing.assert_allclose(self.optimizer.target.a.W.grad, | ||
(base + 0) * np.ones((3, 2))) | ||
chainer.testing.assert_allclose(self.optimizer.target.b.W.grad, | ||
(base + 1) * np.ones((4, 3))) | ||
chainer.testing.assert_allclose(self.optimizer.target.c.W.grad, | ||
(base + 2) * np.ones((5, 4))) | ||
|
||
@chainer.testing.attr.gpu | ||
def test_update_with_gpu(self): | ||
self.setup_gpu() | ||
self.optimizer = chainermn.create_multi_node_optimizer( | ||
self.actual_optimizer, self.comm) | ||
self.optimizer.setup(self.target) | ||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 0) | ||
self.optimizer.target.a.W.grad[:] = self.comm.rank | ||
self.optimizer.target.b.W.grad[:] = self.comm.rank + 1 | ||
self.optimizer.target.c.W.grad[:] = self.comm.rank + 2 | ||
|
||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 1) | ||
self.optimizer.target.a.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.a.W) | ||
self.optimizer.target.b.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.b.W) | ||
self.optimizer.target.c.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.c.W) | ||
|
||
base = (self.comm.size - 1.0) / 2 | ||
chainer.testing.assert_allclose(self.optimizer.target.a.W.grad, | ||
(base + 0) * np.ones((3, 2))) | ||
chainer.testing.assert_allclose(self.optimizer.target.b.W.grad, | ||
(base + 1) * np.ones((4, 3))) | ||
chainer.testing.assert_allclose(self.optimizer.target.c.W.grad, | ||
(base + 2) * np.ones((5, 4))) | ||
|
||
|
||
|
||
class DynamicExampleModel(chainer.Chain): | ||
|
||
def __init__(self): | ||
super(DynamicExampleModel, self).__init__() | ||
with self.init_scope(): | ||
self.a=chainer.links.Linear(2, 3) | ||
self.b=chainer.links.Linear(3, 4) | ||
|
||
|
||
class TestMultiNodeOptimizerWithDynamicModel(unittest.TestCase): | ||
|
||
def setup_cpu(self): | ||
self.comm = chainermn.create_communicator('naive') | ||
self.target = DynamicExampleModel() | ||
self.target.a.W.data[:] = self.comm.rank | ||
self.target.b.W.data[:] = self.comm.rank + 1 | ||
self.target.a.W.grad[:] = 0 | ||
self.target.b.W.grad[:] = 0 | ||
self.actual_optimizer = chainer.GradientMethod() | ||
self.actual_optimizer.create_update_rule = mock.MagicMock | ||
|
||
def setup_gpu(self, device=None): | ||
self.comm = chainermn.create_communicator('hierarchical') | ||
device = self.comm.intra_rank | ||
chainer.cuda.get_device(device).use() | ||
self.target = DynamicExampleModel() | ||
self.target.to_gpu() | ||
self.target.a.W.data[:] = self.comm.rank | ||
self.target.b.W.data[:] = self.comm.rank + 1 | ||
self.target.a.W.grad[:] = 0 | ||
self.target.b.W.grad[:] = 0 | ||
self.actual_optimizer = chainer.GradientMethod() | ||
self.actual_optimizer.create_update_rule = mock.MagicMock | ||
|
||
def test_update_with_cpu(self): | ||
self.setup_cpu() | ||
self.optimizer = chainermn.create_multi_node_optimizer( | ||
self.actual_optimizer, self.comm) | ||
self.optimizer.setup(self.target) | ||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 0) | ||
|
||
with self.target.init_scope(): | ||
self.target.c = chainer.links.Linear(4, 4) | ||
if self.comm.rank == 0: | ||
self.target.c.W.data[:] = self.comm.rank + 2 | ||
self.optimizer.setup(self.target) | ||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 0) | ||
|
||
send_buf = chainer.cuda.to_cpu(self.optimizer.target.c.W.data) | ||
recv_buf = self.comm.mpi_comm.allgather(send_buf) | ||
for i in range(1, self.comm.size): | ||
chainer.testing.assert_allclose(recv_buf[0], recv_buf[i]) | ||
|
||
self.optimizer.target.a.W.grad[:] = self.comm.rank | ||
self.optimizer.target.b.W.grad[:] = self.comm.rank + 1 | ||
self.optimizer.target.c.W.grad[:] = self.comm.rank + 2 | ||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 1) | ||
self.optimizer.target.a.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.a.W) | ||
self.optimizer.target.b.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.b.W) | ||
self.optimizer.target.c.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.c.W) | ||
|
||
base = (self.comm.size - 1.0) / 2 | ||
chainer.testing.assert_allclose(self.optimizer.target.a.W.grad, | ||
(base + 0) * np.ones((3, 2))) | ||
chainer.testing.assert_allclose(self.optimizer.target.b.W.grad, | ||
(base + 1) * np.ones((4, 3))) | ||
chainer.testing.assert_allclose(self.optimizer.target.c.W.grad, | ||
(base + 2) * np.ones((4, 4))) | ||
|
||
@chainer.testing.attr.gpu | ||
def test_update_with_gpu(self): | ||
self.setup_gpu() | ||
self.optimizer = chainermn.create_multi_node_optimizer( | ||
self.actual_optimizer, self.comm) | ||
self.optimizer.setup(self.target) | ||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 0) | ||
|
||
with self.target.init_scope(): | ||
c = chainer.links.Linear(4, 4) | ||
c.to_gpu() | ||
self.target.c = c | ||
if self.comm.rank == 0: | ||
self.target.c.W.data[:] = self.comm.rank + 2 | ||
self.optimizer.setup(self.target) | ||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 0) | ||
|
||
send_buf = chainer.cuda.to_cpu(self.optimizer.target.c.W.data) | ||
recv_buf = self.comm.mpi_comm.allgather(send_buf) | ||
for i in range(1, self.comm.size): | ||
chainer.testing.assert_allclose(recv_buf[0], recv_buf[i]) | ||
|
||
self.optimizer.target.a.W.grad[:] = self.comm.rank | ||
self.optimizer.target.b.W.grad[:] = self.comm.rank + 1 | ||
self.optimizer.target.c.W.grad[:] = self.comm.rank + 2 | ||
self.optimizer.update() | ||
self.assertEqual(self.actual_optimizer.t, 1) | ||
self.optimizer.target.a.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.a.W) | ||
self.optimizer.target.b.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.b.W) | ||
self.optimizer.target.c.W.update_rule.update.assert_called_once_with( | ||
self.optimizer.target.c.W) | ||
|
||
base = (self.comm.size - 1.0) / 2 | ||
chainer.testing.assert_allclose(self.optimizer.target.a.W.grad, | ||
(base + 0) * np.ones((3, 2))) | ||
chainer.testing.assert_allclose(self.optimizer.target.b.W.grad, | ||
(base + 1) * np.ones((4, 3))) | ||
chainer.testing.assert_allclose(self.optimizer.target.c.W.grad, | ||
(base + 2) * np.ones((4, 4))) | ||
|
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.
Let us make the case analysis as easy as possible. Here, how about using "early return" as follows (for details, please refer to the book 'The Art of Readable Code'):
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.
Thanks, I will fix it.