forked from pbelevich/pipeline_experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dist_main.py
206 lines (166 loc) · 7.04 KB
/
dist_main.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
import os
import time
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.optim as optim
import torch.distributed.autograd as dist_autograd
import torch.distributed.rpc as rpc
import torch.multiprocessing as mp
from torch.distributed.optim import DistributedOptimizer
import torchvision
import torchvision.transforms as transforms
from resnet import ResNet50Base, ResNet50OneGPU, ResNet50TwoGPUs, ResNet50SixGPUs
# from utils import progress_bar
import copy
from dist import DistModule
def run_master(split_size):
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = torchvision.datasets.CIFAR10(
root='./data', train=True, download=True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=100, shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(
root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(
testset, batch_size=100, shuffle=False, num_workers=2)
loss_fn = nn.CrossEntropyLoss()
model = ResNet50Base()
parts = [
model._part1(),
model._part2(),
model._part3(),
model._part4(),
model._part5(),
model._part6(),
]
local1_devices = [f"cuda:{i}" for i in range(6)]
local1_model = ResNet50SixGPUs(copy.deepcopy(parts), local1_devices)
local1_opt = optim.SGD(local1_model.parameters(), lr=0.05)
dist1_devices = [f"cuda:{i}" for i in range(6)]
dist1_workers = [f"worker{i+1}" for i in range(6)]
dist1_model = DistModule(copy.deepcopy(parts), dist1_devices, dist1_workers, split_size=100)
dist1_opt = DistributedOptimizer(
optim.SGD,
dist1_model.parameter_rrefs(),
lr=0.05,
)
def train(epoch):
total = 0
local1_model.train()
local1_train_loss = 0
local1_correct = 0
local1_start = 0
local1_finish = 0
dist1_model.train()
dist1_train_loss = 0
dist1_correct = 0
dist1_start = 0
dist1_finish = 0
pbar = tqdm(trainloader)
for batch_idx, (inputs, labels) in enumerate(pbar):
total += labels.size(0)
local1_start = time.time()
local1_labels = labels.to(local1_devices[-1])
local1_opt.zero_grad()
local1_outputs = local1_model(inputs.to(local1_devices[0]))
local1_loss = loss_fn(local1_outputs, local1_labels)
local1_loss.backward()
local1_opt.step()
local1_train_loss += local1_loss.item()
_, local1_predicted = local1_outputs.max(1)
local1_correct += local1_predicted.eq(local1_labels).sum().item()
local1_finish = time.time()
dist1_start = time.time()
# The distributed autograd context is the dedicated scope for the
# distributed backward pass to store gradients, which can later be
# retrieved using the context_id by the distributed optimizer.
with dist_autograd.context() as context_id:
dist1_outputs = dist1_model(inputs)
dist1_loss = loss_fn(dist1_outputs, labels)
dist_autograd.backward(context_id, [dist1_loss])
dist1_opt.step(context_id)
dist1_train_loss += dist1_loss.item()
_, dist1_predicted = dist1_outputs.max(1)
dist1_correct += dist1_predicted.eq(labels).sum().item()
dist1_finish = time.time()
pbar.set_postfix({'local': (local1_finish - local1_start), 'dist': (dist1_finish - dist1_start)})
# progress_bar(batch_idx, len(trainloader), '| %.3f | %.3f%% (%d/%d) | %.3f | %.3f%% (%d/%d)'
# % (local1_train_loss/(batch_idx+1), 100.*local1_correct/total, local1_correct, total,
# dist1_train_loss/(batch_idx+1), 100.*dist1_correct/total, dist1_correct, total))
# assert local1_train_loss == dist1_train_loss
# assert local1_correct == dist1_correct
def test(epoch):
total = 0
local1_model.eval()
local1_test_loss = 0
local1_correct = 0
local1_start = 0
local1_finish = 0
dist1_model.eval()
dist1_test_loss = 0
dist1_correct = 0
dist1_start = 0
dist1_finish = 0
pbar = tqdm(testloader)
with torch.no_grad():
for batch_idx, (inputs, labels) in enumerate(pbar):
total += labels.size(0)
local1_start = time.time()
local1_labels = labels.to(local1_devices[-1])
local1_outputs = local1_model(inputs.to(local1_devices[0]))
local1_loss = loss_fn(local1_outputs, local1_labels)
local1_test_loss += local1_loss.item()
_, local1_predicted = local1_outputs.max(1)
local1_correct += local1_predicted.eq(local1_labels).sum().item()
local1_finish = time.time()
dist1_start = time.time()
dist1_outputs = dist1_model(inputs)
dist1_loss = loss_fn(dist1_outputs, labels)
dist1_test_loss += dist1_loss.item()
_, dist1_predicted = dist1_outputs.max(1)
dist1_correct += dist1_predicted.eq(labels).sum().item()
dist1_finish = time.time()
pbar.set_postfix({'local': (local1_finish - local1_start), 'dist': (dist1_finish - dist1_start)})
# progress_bar(batch_idx, len(testloader), '| %.3f | %.3f%% (%d/%d) | %.3f | %.3f%% (%d/%d)'
# % (local1_test_loss/(batch_idx+1), 100.*local1_correct/total, local1_correct, total,
# dist1_test_loss/(batch_idx+1), 100.*dist1_correct/total, dist1_correct, total))
# assert local1_test_loss == dist1_test_loss
# assert local1_correct == dist1_correct
for epoch in range(10):
print('\nEpoch: %d' % epoch)
train(epoch)
test(epoch)
def run_worker(rank, world_size, num_split):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '29500'
options = rpc.TensorPipeRpcBackendOptions(num_worker_threads=256)
if rank == 0:
rpc.init_rpc(
"master",
rank=rank,
world_size=world_size,
rpc_backend_options=options
)
run_master(num_split)
else:
rpc.init_rpc(
f"worker{rank}",
rank=rank,
world_size=world_size,
rpc_backend_options=options
)
pass
# block until all rpcs finish
rpc.shutdown()
if __name__=="__main__":
world_size = 7
num_split = 1
mp.spawn(run_worker, args=(world_size, num_split), nprocs=world_size, join=True)