/
test_multiprocess_parallel_updater.py
208 lines (159 loc) · 6.78 KB
/
test_multiprocess_parallel_updater.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import subprocess
import sys
import unittest
import numpy
import chainer
from chainer.backends import cuda
import chainer.functions.math.minmax
from chainer import initializers
import chainer.reporter
from chainer import testing
from chainer.testing import attr
import chainer.training.updaters.multiprocess_parallel_updater as mpu
import copy
class SimpleNet(chainer.Chain):
insize = 5
def __init__(self, dtype=numpy.float32):
super(SimpleNet, self).__init__()
self.dtype = dtype
W = initializers.HeNormal(1 / numpy.sqrt(2), self.dtype)
bias = initializers.Zero(self.dtype)
with self.init_scope():
self.conv = chainer.links.Convolution2D(2, 2, 3, initialW=W,
initial_bias=bias)
self.fc = chainer.links.Linear(18, 2, initialW=W,
initial_bias=bias)
self.train = True
def clear(self):
self.loss = None
self.accuracy = None
def __call__(self, x, t):
h = chainer.functions.relu(self.conv(x))
y = self.fc(h)
self.loss = chainer.functions.softmax_cross_entropy(y, t)
self.accuracy = chainer.functions.accuracy(y, t)
return self.loss
@testing.parameterize(*testing.product({
'dtype': [numpy.float32, numpy.float16],
}))
class TestGatherScatter(unittest.TestCase):
@attr.gpu
def test_gather_scatter_grads(self):
cupy = cuda.cupy
model0 = SimpleNet(dtype=self.dtype)
model1 = copy.deepcopy(model0)
with testing.assert_warns(DeprecationWarning):
model0.to_gpu()
with testing.assert_warns(DeprecationWarning):
model1.to_gpu()
optimizer0 = chainer.optimizers.SGD(lr=1.0)
optimizer0.setup(model0)
optimizer1 = chainer.optimizers.SGD(lr=1.0)
optimizer1.setup(model1)
bsize = 8
x = numpy.random.uniform(0, 1, (bsize, 2, 5, 5)).astype(self.dtype)
t = numpy.empty(bsize, dtype=numpy.int32)
for i in range(bsize):
t[i] = i % 2
x = chainer.Variable(chainer.backends.cuda.to_gpu(x))
t = chainer.Variable(chainer.backends.cuda.to_gpu(t))
loss0 = model0(x, t)
model0.cleargrads()
model1.cleargrads()
loss0.backward()
gg0 = mpu.gather_grads(model0)
mpu.scatter_grads(model1, gg0)
cupy.testing.assert_array_equal(model0.conv.W.grad, model1.conv.W.grad)
cupy.testing.assert_array_equal(model0.conv.b.grad, model1.conv.b.grad)
cupy.testing.assert_array_equal(model0.fc.W.grad, model1.fc.W.grad)
cupy.testing.assert_array_equal(model0.fc.b.grad, model1.fc.b.grad)
optimizer0.update()
optimizer1.update()
cupy.testing.assert_array_equal(model0.conv.W.data, model1.conv.W.data)
cupy.testing.assert_array_equal(model0.conv.b.data, model1.conv.b.data)
cupy.testing.assert_array_equal(model0.fc.W.data, model1.fc.W.data)
cupy.testing.assert_array_equal(model0.fc.b.data, model1.fc.b.data)
def test_gather_grads_raise_on_cpu(self):
model = SimpleNet(dtype=self.dtype)
with self.assertRaises(RuntimeError):
mpu.gather_grads(model)
@attr.gpu
def test_gather_scatter_params(self):
cupy = cuda.cupy
model0 = SimpleNet(dtype=self.dtype)
model1 = SimpleNet(dtype=self.dtype)
with testing.assert_warns(DeprecationWarning):
model0.to_gpu()
with testing.assert_warns(DeprecationWarning):
model1.to_gpu()
gp0 = mpu.gather_params(model0)
mpu.scatter_params(model1, gp0)
cupy.testing.assert_array_equal(model0.conv.W.data, model1.conv.W.data)
cupy.testing.assert_array_equal(model0.conv.b.data, model1.conv.b.data)
cupy.testing.assert_array_equal(model0.fc.W.data, model1.fc.W.data)
cupy.testing.assert_array_equal(model0.fc.b.data, model1.fc.b.data)
def test_gather_params_raise_on_cpu(self):
model = SimpleNet(dtype=self.dtype)
with self.assertRaises(RuntimeError):
mpu.gather_params(model)
def _run_test_snippet(name, *args):
script_path = os.path.join(
os.path.dirname(__file__), 'snippets/{}'.format(name))
proc = subprocess.Popen(
(sys.executable, script_path) + args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdoutdata, stderrdata = proc.communicate()
ret = proc.returncode
return (ret, stdoutdata, stderrdata)
class TestRawArray(unittest.TestCase):
@attr.gpu
@unittest.skipUnless(mpu.MultiprocessParallelUpdater.available(),
'MultiprocessParallelUpdater is not available.')
def test_update_uses_raw_array(self):
ret, stdoutdata, stderrdata = _run_test_snippet(
'raw_array.py', '@cupy:0')
assert ret == 0, (
'[stdout]:{!r}\n'
'[stderr]:{!r}'.format(stdoutdata, stderrdata))
class TestChildReporter(unittest.TestCase):
def check_with_devices(self, n_devices):
devices_str = ','.join([
'@cupy:{}'.format(device_id) for device_id in range(n_devices)])
ret, stdoutdata, stderrdata = _run_test_snippet(
'child_reporter.py', devices_str)
assert ret == 0, (
'[stdout]:{!r}\n'
'[stderr]:{!r}'.format(stdoutdata, stderrdata))
@attr.gpu
@unittest.skipUnless(mpu.MultiprocessParallelUpdater.available(),
'MultiprocessParallelUpdater is not available.')
def test_single_device(self):
self.check_with_devices(1)
@attr.multi_gpu(2)
@unittest.skipUnless(mpu.MultiprocessParallelUpdater.available(),
'MultiprocessParallelUpdater is not available.')
def test_multi_device(self):
self.check_with_devices(2)
class TestCUDAContext(unittest.TestCase):
@attr.gpu
@unittest.skipUnless(mpu.MultiprocessParallelUpdater.available(),
'MultiprocessParallelUpdater is not available.')
def test_cuda_init(self):
ret, stdoutdata, stderrdata = _run_test_snippet('cuda_init.py')
assert ret == 0, (
'[stdout]:{!r}\n'
'[stderr]:{!r}'.format(stdoutdata, stderrdata))
class TestDevicesByDeviceIds(unittest.TestCase):
@attr.gpu
@unittest.skipUnless(mpu.MultiprocessParallelUpdater.available(),
'MultiprocessParallelUpdater is not available.')
def test_devices_by_device_ids_array(self):
# Test passing devices to MultiprocessParallelUpdater by their ids.
ret, stdoutdata, stderrdata = _run_test_snippet(
'raw_array.py', '0')
assert ret == 0, (
'[stdout]:{!r}\n'
'[stderr]:{!r}'.format(stdoutdata, stderrdata))
testing.run_module(__name__, __file__)