forked from mps-ddp/mccl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_quick.py
More file actions
39 lines (31 loc) · 1.23 KB
/
test_quick.py
File metadata and controls
39 lines (31 loc) · 1.23 KB
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
#!/usr/bin/env python3
"""Quick test of MCCL multi-stream engine optimizations."""
import os
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
import mccl
def test_two_rank_allreduce(rank, world_size):
os.environ['MASTER_ADDR'] = '127.0.0.1'
os.environ['MASTER_PORT'] = '29500'
os.environ['MCCL_PORT_BASE'] = '29600'
os.environ['MCCL_LOG_LEVEL'] = 'INFO'
try:
dist.init_process_group('mccl', rank=rank, world_size=world_size)
# Test basic allreduce
x = torch.ones(1000, device='mps') * (rank + 1)
dist.all_reduce(x)
expected = sum(range(1, world_size + 1)) * torch.ones(1000, device='mps')
correct = torch.allclose(x, expected)
print(f'Rank {rank}: allreduce result correct = {correct}')
if not correct:
print(f' Expected: {expected[:5]}')
print(f' Got: {x[:5]}')
dist.destroy_process_group()
return correct
except Exception as e:
print(f'Rank {rank}: ERROR - {e}')
return False
if __name__ == '__main__':
print("Testing MCCL multi-stream engine optimizations...")
mp.spawn(test_two_rank_allreduce, args=(2,), nprocs=2, join=True)