-
Notifications
You must be signed in to change notification settings - Fork 1
/
issues_with_ddp.py
85 lines (59 loc) · 2.25 KB
/
issues_with_ddp.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
import torch
import torch.utils.data as data
import torch.distributed as dist
def replace_print():
import builtins as __builtin__
builtin_print = __builtin__.print
def print(*args, **kwargs):
for rank in range(dist.get_world_size()):
if rank == dist.get_rank():
builtin_print(f"[GPU {rank}]", *args, **kwargs)
dist.barrier()
__builtin__.print = print
class MyMapStyleDS:
def __init__(self, size=100):
self.size = size
def __getitem__(self, i): # Returns the i'th sample
s = i
return s
def __len__(self):
return self.size
class MyIterableDS(data.IterableDataset):
def __init__(self, size=100):
self.size = size
def __iter__(self): # iterate over samples
for s in range(self.size):
yield s
# # Need to shard across DDP workers
# num_ddp_workers = dist.get_world_size()
# ddp_worker_id = dist.get_rank()
# for i, s in enumerate(range(self.size)):
# if i % num_ddp_workers == ddp_worker_id:
# yield s
# # # But that's no enough!!
# # # Need to shard across DDP workers **and** accross DataLoader workers
# # worker_info = data.get_worker_info()
# # num_dl_workers = worker_info.num_workers
# # dl_worker_id = worker_info.id
# # num_ddp_workers = dist.get_world_size()
# # ddp_worker_id = dist.get_rank()
# # for i, s in enumerate(range(self.size)):
# # if i % num_ddp_workers == ddp_worker_id:
# # if i % num_dl_workers == dl_worker_id:
# # yield s
# # # That's **two** levels of (embedded) sharding!
def __len__(self):
return self.size
# Setting up DDP - you can ignore this
dist.init_process_group(backend="gloo")
replace_print()
dist.barrier()
# Map-style dataset
# ds = MyMapStyleDS()
# sampler = data.DistributedSampler(ds, shuffle=False)
# dl = torch.utils.data.DataLoader(ds, batch_size=10, num_workers=4, sampler=sampler)
# Indexable dataset
ds = MyIterableDS()
dl = torch.utils.data.DataLoader(ds, batch_size=10, num_workers=4)
for i, batch in enumerate(dl):
print(batch)