4
from caffe2.python import core, workspace, muji, test_util
7
@unittest.skipIf(not workspace.has_gpu_support, "no gpu")
8
class TestMuji(test_util.TestCase):
9
def RunningAllreduceWithGPUs(self, gpu_ids, allreduce_function):
10
"""A base function to test different scenarios."""
11
net = core.Net("mujitest")
15
"testblob_gpu_" + str(id),
18
device_option=muji.OnGPU(id)
21
net, ["testblob_gpu_" + str(i)
22
for i in gpu_ids], "_reduced", gpu_ids
24
workspace.RunNetOnce(net)
25
target_value = sum(gpu_ids) + len(gpu_ids)
26
all_blobs = workspace.Blobs()
28
for blob in all_blobs:
29
print('{} {}'.format(blob, workspace.FetchBlob(blob)))
32
blob = workspace.FetchBlob("testblob_gpu_" + str(idx) + "_reduced")
33
np.testing.assert_array_equal(
36
err_msg="gpu id %d of %s" % (idx, str(gpu_ids))
39
def testAllreduceFallback(self):
40
self.RunningAllreduceWithGPUs(
41
list(range(workspace.NumGpuDevices())), muji.AllreduceFallback
44
def testAllreduceSingleGPU(self):
45
for i in range(workspace.NumGpuDevices()):
46
self.RunningAllreduceWithGPUs([i], muji.Allreduce)
48
def testAllreduceWithTwoGPUs(self):
49
pattern = workspace.GetGpuPeerAccessPattern()
50
if pattern.shape[0] >= 2 and np.all(pattern[:2, :2]):
51
self.RunningAllreduceWithGPUs([0, 1], muji.Allreduce2)
53
print('Skipping allreduce with 2 gpus. Not peer access ready.')
55
def testAllreduceWithFourGPUs(self):
56
pattern = workspace.GetGpuPeerAccessPattern()
57
if pattern.shape[0] >= 4 and np.all(pattern[:4, :4]):
58
self.RunningAllreduceWithGPUs([0, 1, 2, 3], muji.Allreduce4)
60
print('Skipping allreduce with 4 gpus. Not peer access ready.')
62
def testAllreduceWithFourGPUsAndTwoGroups(self):
63
pattern = workspace.GetGpuPeerAccessPattern()
64
if pattern.shape[0] >= 4 and np.all(pattern[:2, :2]) and np.all(pattern[2:4, 2:4]):
65
self.RunningAllreduceWithGPUs([0, 1, 2, 3], muji.Allreduce4Group2)
67
print('Skipping allreduce with 4 gpus and 2 groups. Not peer access ready.')
69
def testAllreduceWithEightGPUs(self):
70
pattern = workspace.GetGpuPeerAccessPattern()
72
pattern.shape[0] >= 8 and np.all(pattern[:4, :4]) and
73
np.all(pattern[4:, 4:])
75
self.RunningAllreduceWithGPUs(
76
list(range(8)), muji.Allreduce8)
78
print('Skipping allreduce with 8 gpus. Not peer access ready.')
81
if __name__ == '__main__':