forked from Soptq/Dynamic_Load_Balance_DistributedDNN
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbs.py
More file actions
363 lines (296 loc) · 11.9 KB
/
dbs.py
File metadata and controls
363 lines (296 loc) · 11.9 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import random, math
import os
import time
import numpy as np
import torch
torch.multiprocessing.set_start_method('spawn', force=True)
import torch.nn.functional as F
import torch.optim as optim
import torch.distributed as dist
from torch.multiprocessing import Process
import parser
import dataloader
import dbs_logging
args = parser.get_parser().parse_args()
"""
##########################################################################################
#
# Get Arguments From Parser.
#
##########################################################################################
"""
debug_mode_enabled = args.debug
world_size = args.world_size
batch_size = args.batch_size
lr = args.learning_rate
epoch_size = args.epoch_size
dbs_enabled = args.dynamic_batch_size
gpu = args.gpu
training_model = args.model
ft_enable = args.fault_tolerance
ftc = args.fault_tolerance_chance
"""
##########################################################################################
#
# Initialize Useful Variables
#
##########################################################################################
"""
# Configure Processing Unit
if debug_mode_enabled:
DEVICE = "cpu"
elif isinstance(gpu, int):
DEVICE = "cuda:{}".format(gpu)
torch.cuda.set_device(gpu)
else:
# Will configure it when the worker process is spawned.
DEVICE = None
# Fault-Tolerance-Related Variables
fault_wait = False # Flag that indicates if current worker is in a random waiting phase.
fault_round = 0 # Random integer that indicates when will current worker stop waiting.
fault_wait_time = 0 # Random integer that indicates how many seconds current worker needs to wait.
current_epoch = -1 # A variable that stores current epoch number.
# Log-Related Variables
logger = None
"""
##########################################################################################
#
# Code For Fault Tolerance Test
#
# This snippet of code will automatically decide whether current worker will be
# slowed down.
#
##########################################################################################
"""
def fault_tolerance_wait(epoch, batch_num, rank):
global fault_round, fault_wait, ftc, ft_enable, fault_wait_time, saved_epoch
if not ft_enable:
return
if fault_wait: # Current worker is in a waiting phase
if epoch <= fault_round: # waiting is not completed, wait.
# Need to split the fault_wait_time into batch_num parts, as fault_wait_time is for a epoch not a iteration.
time.sleep(float(fault_wait_time) / float(batch_num))
return
else:
fault_wait = False
# Current worker is not waiting.
if saved_epoch != epoch:
saved_epoch = epoch
else:
return # A worker can only enter below code once a epoch.
# fault_wait is false, should try worker's luck to see if he needs to wait.
luck = random.random()
logger.info(f"Rank {rank} got a luck of {luck}, limit is {ftc}")
if luck < ftc:
# Back luck!
# generate a wait round and a wait time
fault_wait_time = random.randint(5, 10) # generate a wait time between 5 seconds to 10 seconds.
fault_round = random.randint(4, 20) # generate a wait round between 4 iterations to 20 iterations.
fault_round += epoch # wait until fault_round epoch.
fault_wait = True # start waiting on next iterations.
logger.info(
f"Rank {rank} starts to have a {fault_wait_time} seconds more waiting until epoch {fault_round} !")
return
else:
# Lucky! there is no waiting.
return
"""
##########################################################################################
#
# Model Validation
#
##########################################################################################
"""
def validate(val_loader, model, criterion, epoch, num_batches):
model.eval()
total = 0
correct = 0
val_loss = 0
with torch.no_grad():
for data in val_loader:
inputs, target = data
inputs = inputs.to(DEVICE)
target = target.to(DEVICE)
output = model(inputs)
val_loss += criterion(output, target).item()
_, predicted = torch.max(output, 1)
total += target.size(0)
correct += (predicted == target).sum().item()
val_loss /= total
accuracy = 100 * correct / total
logger.info(
f'Rank {dist.get_rank()}, epoch {epoch}, val_loss {val_loss / num_batches}, accuracy {accuracy}')
"""
##########################################################################################
#
# Model Training
#
##########################################################################################
"""
def train(trainloader, model, optimizer, criterion, epoch, num_batches):
model.train()
epoch_loss = 0.0
running_loss = 0.0
average_time = 0.0
dist.barrier()
start_time = time.time()
for i, data in enumerate(trainloader, 0):
inputs, target = data
inputs = inputs.to(DEVICE)
target = target.to(DEVICE)
optimizer.zero_grad()
output = model(inputs)
loss = criterion(output, target)
loss.backward()
fault_tolerance_wait(epoch, num_batches, dist.get_rank()) # Tolerance test
wait_time = SSGD(model) # Model averaging
optimizer.step()
epoch_loss += loss.item()
running_loss += loss.item()
train_time = time.time() - start_time
average_time += wait_time
if i % 10 == 0:
logger.info(
f'Rank {dist.get_rank()}, epoch {epoch}: {i}, train_time {train_time}, average_time {average_time}, train_loss {running_loss / (10.0 if i is not 0 else 1.0)}')
running_loss = 0.0
train_time = time.time() - start_time
logger.info(
f'Rank {dist.get_rank()}, epoch {epoch}, train_time {train_time}, train_loss {epoch_loss / num_batches}')
return train_time - average_time
def SSGD(model):
wait_time = 0.0
for param in model.parameters():
req = dist.all_reduce(param.grad.data, op=dist.ReduceOp.SUM, async_op=True)
send_start = time.time()
req.wait()
wait_time += time.time() - send_start
param.grad.data /= float(world_size)
return wait_time
"""
##########################################################################################
#
# Distributed Simulating Code
#
##########################################################################################
"""
def run(rank, size):
global lr, debug_mode_enabled, dbs_enabled
logger.info(f'Initiating Rank {rank}, World Size {size}')
torch.manual_seed(1234)
# Configure training model
if debug_mode_enabled:
import Net.MnistNet
model = Net.MnistNet.MnistNet()
else:
if args.model == "resnet":
import Net.Resnet
model = Net.Resnet.ResNet101()
if args.model == "densenet":
import Net.Densenet
model = Net.Densenet.DenseNet121()
if args.model == "googlenet":
import Net.GoogleNet
model = Net.GoogleNet.GoogLeNet()
if args.model == "regnet":
import Net.RegNet
model = Net.RegNet.RegNetY_400MF()
model = model.to(DEVICE)
optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.5)
# Initialize default batch size distribution
# At the beginning we assume all workers have the same performance.
nodes_time = np.array([1.0 for _ in range(size)]) # Training time of workers
partition_size = np.array([1.0 / size for _ in range(size)]) # Dataset partition ratio
# Start training
logger.info(f'Rank {rank} start training')
total_train_time = 0 # Count total train time
for epoch in range(epoch_size):
if dbs_enabled:
# Calculated dataset partition ratio based on workers' training time and last epoch's partition ratio.
partition_size = get_size(nodes_time, partition_size)
logger.info(f"Rank {rank}, adjusted partition size to {partition_size}")
# Using calculated partition size to split dataset, getting train_set, val_set, as well as corresponding
# batch size of current worker
train_set, val_set, bsz = dataloader.partition_dataset(partition_size, rank, debug_mode_enabled, batch_size)
num_batches = math.ceil(len(train_set.dataset) / float(bsz)) # Calculate how many iterations in this epoch.
logger.info(
f"Rank {rank}, number of batches {num_batches}, batch size {train_set.batch_size}, "
f"length {train_set.batch_size * num_batches}")
epoch_start_time = time.time()
# train() returned train_time excludes the communication time.
train_time = train(train_set, model, optimizer, F.cross_entropy, epoch, num_batches)
total_train_time += time.time() - epoch_start_time # Get time that includes communication time.
validate(val_set, model, F.cross_entropy, epoch, num_batches)
if dbs_enabled:
# Exchange pure train time for dataset partition ratio calculating in the next epoch.
nodes_time = time_allreduce(torch.tensor([train_time], dtype=torch.float32).cpu(), rank, size)
logger.info(f"Rank {rank}, total time {nodes_time}")
logger.info(f'Rank {rank} Terminated')
logger.info(f'Rank {rank} Total Time:')
logger.info(total_train_time)
"""
##########################################################################################
#
# DBS Algorithm
#
##########################################################################################
"""
def get_size(nodes_time: np.ndarray, partition_size: np.ndarray):
_sum = 0.0
for i in range(world_size):
_sum += (partition_size[i] / nodes_time[i])
cons_k = 1 / _sum # get constant_k
distribution_ratio = np.divide(partition_size * cons_k, nodes_time)
# get the most accurate batch_size split
norm_batch = distribution_ratio * batch_size / distribution_ratio.sum()
floor_norm_batch = np.floor(norm_batch)
floor_sum = int(floor_norm_batch.sum())
ceil_counter = batch_size - floor_sum # will pick top k to ceil
idx_ceil = (norm_batch - floor_norm_batch).argsort()[-ceil_counter:]
idx_round = np.argwhere(norm_batch - floor_norm_batch >= 0.5).reshape(-1)
_, idx_inter, _ = np.intersect1d(idx_ceil, idx_round, return_indices=True)
idx = idx_ceil[idx_inter]
floor_norm_batch[idx] += 1
norm = floor_norm_batch / floor_norm_batch.sum()
return norm
def time_allreduce(send_buff, rank, size):
recv_buff = send_buff.clone()
left = ((rank - 1) + size) % size
right = (rank + 1) % size
result = [send_buff.item()]
for i in range(size - 1):
# Send send_buff
send_req = dist.isend(send_buff, right)
dist.recv(recv_buff, left)
result.append(recv_buff.item())
send_req.wait()
send_buff = recv_buff.clone()
for i in range(rank, size - 1):
result.insert(0, result.pop())
result.reverse()
return result
"""
##########################################################################################
#
# Distributed Simulating Code
#
##########################################################################################
"""
def init_processes(rank, size, fn, backend='gloo'):
global DEVICE, logger
os.environ['MASTER_ADDR'] = '127.0.0.1'
os.environ['MASTER_PORT'] = '29500'
dist.init_process_group(backend, rank=rank, world_size=size)
# Configuring multiple GPU
if not debug_mode_enabled and isinstance(gpu, list):
DEVICE = "cuda:{}".format(gpu[rank])
torch.cuda.set_device(gpu[rank])
logger = dbs_logging.init_logger(args, rank)
fn(rank, size)
if __name__ == "__main__":
processes = []
for rank in range(world_size):
p = Process(target=init_processes, args=(rank, world_size, run))
p.start()
processes.append(p)
for p in processes:
p.join()