forked from HKBU-HPML/MG-WFBP
/
distributed_optimizer.py
622 lines (553 loc) · 25.3 KB
/
distributed_optimizer.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
# Copyright 2018 Uber Technologies, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from horovod.torch.mpi_ops import allreduce_async_
from horovod.torch.mpi_ops import allgather_async
from horovod.torch.mpi_ops import broadcast_async_
from horovod.torch.mpi_ops import synchronize
from horovod.torch.mpi_ops import size, local_size, rank, local_rank
from horovod.torch.mpi_ops import init, broadcast
import time
import torch
import numpy as np
import utils
import collections
import settings
from settings import logger, ADAPTIVE_MERGE, DEBUG
from profiling import CommunicationProfiler
from sklearn.linear_model import LinearRegression
class _DistributedOptimizer(torch.optim.Optimizer):
def __init__(self, params, named_parameters, compression, seq_layernames=None, layerwise_times=None, norm_clip=None, threshold=0, writer=None, gradient_path=None):
super(self.__class__, self).__init__(params)
self._compression = compression
self._density = 1
self._profiling = False
self._seq_layernames = seq_layernames
self._layerwise_times = layerwise_times
self._original_layerwise_times_kv = None
self._norm_clip = norm_clip
self._threshold = threshold
self._writer = writer
self._gradient_path = gradient_path
self.alpha = None
self.beta = None
if self._layerwise_times is not None and self._seq_layernames is not None:
self._original_layerwise_times_kv = dict(zip(self._seq_layernames, self._layerwise_times))
self._compression_timers = {} # compression
self._allreduce_timers = {} # allreduce times
self._update_times = {} # allreduce times
self.train_epoch = 0
self.train_iter = 0
self._dynamic_densities = None
self._layerwise_compressors= None
if named_parameters is not None:
named_parameters = list(named_parameters)
else:
named_parameters = []
self._named_parameters = {k: v for k, v
in named_parameters}
if self._seq_layernames is not None:
self._sequential_keys = self._seq_layernames
else:
self._sequential_keys = [k for k, v in named_parameters]
self.size_commtime_dict = None
self._debug_seq_keys = []
# make sure that named_parameters are tuples
if any([not isinstance(p, tuple) for p in named_parameters]):
raise ValueError('named_parameters should be a sequence of '
'tuples (name, parameter), usually produced by '
'model.named_parameters().')
if len(named_parameters) > 0:
self._parameter_names = {v: k for k, v
in sorted(named_parameters)}
else:
self._parameter_names = {v: 'allreduce.noname.%s' % i
for param_group in self.param_groups
for i, v in enumerate(param_group['params'])}
self._generate_merged_parameters()
self._handles = {}
self._grad_accs = []
self._requires_update = set()
self.local = False
self._hook_checked_idx = 0
if size() > 1:
self._register_hooks()
def _benchmark_communication(self):
logger.info('Benchmarking communication performance...')
comm_profiler = CommunicationProfiler(allreduce_async_, synchronize)
sizes, times = comm_profiler.benchmark(num_iters=10)
def _fit_linear_function(x, y):
X = np.array(x).reshape((-1, 1)) * 4
Y = np.array(y)
model = LinearRegression()
model.fit(X, Y)
alpha = model.intercept_
beta = model.coef_[0]
return alpha, beta
alpha, beta = _fit_linear_function(sizes, times)
self.alpha = alpha
self.beta = beta
alpha_tensor = torch.ones(1) * alpha
beta_tensor = torch.ones(1) * beta
alpha_tensor = broadcast(alpha_tensor, root_rank=0)
beta_tensor = broadcast(beta_tensor, root_rank=0)
if rank() != 0:
self.alpha = float(alpha_tensor[0])
self.beta = float(beta_tensor[0])
logger.info('[rank:{}] Communication performance fitted with f(p)=a+b*p, where a={} and b={}'.format(rank(), self.alpha, self.beta))
def _register_hooks(self):
for param_group in self.param_groups:
for p in param_group['params']:
if p.requires_grad:
p.grad = p.data.new(p.size()).zero_()
self._requires_update.add(p)
p_tmp = p.expand_as(p)
grad_acc = p_tmp.grad_fn.next_functions[0][0]
grad_acc.register_hook(self._make_hook(p))
self._grad_accs.append(grad_acc)
def _generate_groups_with_threshold(self, threshold):
sizes = [self._named_parameters[k].data.numel() for k in self._sequential_keys][::-1] # reverse order
self._sizes = sizes
sub_size = 0
groups = []
group = []
key_groupidx_maps = {}
idx = 0
for k in self._sequential_keys[::-1]:
numel = self._named_parameters[k].data.numel()
sub_size += numel
key_groupidx_maps[k] = idx
if sub_size < threshold:
group.append(k)
else:
idx += 1
group.append(k)
groups.append(group)
group = []
sub_size = 0
if len(group) > 0:
groups.append(group)
return groups, key_groupidx_maps
def _generate_groups_mgwfbp(self):
num_of_workers = size()
p_alpha_beta_56Gbps = {
16: (0.00023583677659915685, 4.0594787739537565e-10),
8: (9.75367204301171e-05, 3.0568230536676206e-10),
4: (4.204298980348825e-05, 2.0589360830118177e-10),
2: (2.554691138304671e-06, 9.837548167872609e-11)
}
p_alpha_beta_10Gbps = {
16: (0.0009080981007148093, 7.395651186836712e-10),
8: (0.0005230272768511732, 8.570746975492128e-10),
4: (4.204298980348825e-05, 2.0589360830118177e-10),
2: (2.554691138304671e-06, 9.837548167872609e-11)
}
if self.alpha is not None:
alpha, beta = self.alpha, self.beta
else:
if settings.CONNECTION == '10GbE':
alpha, beta = p_alpha_beta_10Gbps[num_of_workers]
else:
alpha, beta = p_alpha_beta_56Gbps[num_of_workers]
nbytes = 2 if settings.FP16 else 4
def __calculate_comm_start(tc, tb, taob, L):
taoc = [0] * L
taoc[L-1] = taob[L-1] + tb[L-1]
for l in range(L-1)[::-1]:
taoc[l] = max(taoc[l+1] + tc[l+1], taob[l] + tb[l])
return taoc
def __merge(taob, tc, p, l):
tc[l] = 0
p[l-1] = p[l-1]+p[l]
p[l] = 0
if self.size_commtime_dict is not None:
tc[l-1] = self.size_commtime_dict[l-1]
else:
tc[l-1] = utils.predict_allreduce_time_with_size(alpha, beta, p[l-1]*nbytes, num_of_workers)
sizes = [self._named_parameters[k].data.numel() for k in self._seq_layernames]
seq_layernames = self._seq_layernames
if not utils.check_unique(seq_layernames):
raise ValueError
self._sizes = sizes
p = sizes[:]
L = len(sizes)
if self.size_commtime_dict is not None:
tc = [self.size_commtime_dict[s] for s in sizes]
else:
tc = [utils.predict_allreduce_time_with_size(alpha, beta, s*nbytes, num_of_workers) for s in sizes]
tb = list(self._layerwise_times)
taob = [0]*L
for l in range(0,L-1)[::-1]:
taob[l] = taob[l+1] + tb[l+1]
taoc = __calculate_comm_start(tc, tb, taob, L)
if rank() == 0:
logger.info('tc sum: %f', np.sum(tc))
groups = []
group = []
idx = 0
key_groupidx_maps = {}
l = L-1
key = seq_layernames[l]
key_groupidx_maps[key] = idx
for l in range(1, L)[::-1]:
key = seq_layernames[l]
group.append(key)
key_groupidx_maps[key] = idx
current_taob = taob[l-1] + tb[l-1]
merged=False
if current_taob < taoc[l]+tc[l]:
if taoc[l] > current_taob:
__merge(taob, tc, p, l)
taoc = __calculate_comm_start(tc, tb, taob, L)
merged=True
else:
t_wait = current_taob - taoc[l]
t_saved = alpha
if t_wait < t_saved:
__merge(taob, tc, p, l)
taoc = __calculate_comm_start(tc, tb, taob, L)
merged=True
if not merged:
idx += 1
groups.append(group)
group = []
l = 0
key = seq_layernames[l]
key_groupidx_maps[key] = idx
group.append(key)
if len(group) > 0:
groups.append(group)
if rank() == 0:
logger.info('Predicted non-overlapped time: %f', taoc[0]+tc[0]-(taob[0]+tb[0]))
logger.info('Predicted tb+tc= %f', taoc[0]+tc[0])
logger.info('Merged tc sum: %f', np.sum(tc))
return groups, key_groupidx_maps
def _generate_merged_parameters(self):
self._merged_parameters = {}
self._merged_parameter_names = {}
if ADAPTIVE_MERGE and self._layerwise_times is not None:
groups, key_groupidx_maps = self._generate_groups_mgwfbp()
else:
groups, key_groupidx_maps = self._generate_groups_with_threshold(self._threshold)
logger.info('# of parameters: %d', np.sum(self._sizes))
logger.info('Total number of tensors: %s', len(self._sizes))
logger.info('Merged Number of groups: %s', len(groups))
new_keys = []
self._merged_parameter_offsets = {}
self._layerwise_compressors = None
self._layerwise_compressors = {}
for g in groups:
sub_size = 0
offsets = []
for k in g:
offsets.append(sub_size)
numel = self._named_parameters[k].data.numel()
sub_size += numel
new_key = ':'.join(g)
new_keys.append(new_key)
t = torch.zeros(sub_size, device=self._named_parameters[g[0]].device, dtype=self._named_parameters[g[0]].dtype, requires_grad=False)
self._merged_parameters[new_key] = t
self._merged_parameter_names[t] = new_key
self._merged_parameter_offsets[new_key] = offsets
self._groups = groups
self._key_groupidx_maps = key_groupidx_maps
self._groups_flags = []
for g in self._groups:
flags = []
for k in g:
flags.append(0)
self._groups_flags.append(flags)
def _push_to_buffer(self, name, tensor):
with torch.no_grad():
if len(self._groups) == len(self._sequential_keys):
new_tensor = tensor.data.view(-1)
return name, new_tensor
group_idx = self._key_groupidx_maps[name]
g = self._groups[group_idx]
new_key = ':'.join(g)
layer_idx = g.index(name)
offset = self._merged_parameter_offsets[new_key][layer_idx]
numel = tensor.data.numel()
self._merged_parameters[new_key].data[offset:offset+numel].copy_(tensor.view(numel))
self._groups_flags[group_idx][layer_idx] = 1
for idx in self._groups_flags[group_idx]:
if idx == 0:
return name, None
return new_key, self._merged_parameters[new_key]
def _pull_from_buffer(self, name, merged_tensor):
if len(self._groups) == len(self._sequential_keys):
shape = self._named_parameters[name].data.shape
return {name: merged_tensor.view(shape)}
offsets = self._merged_parameter_offsets[name]
g = name.split(':')
group_idx = self._key_groupidx_maps[g[0]]
self._groups_flags[group_idx] = [0]*len(self._groups_flags[group_idx])
tensors = {}
for i, k in enumerate(g):
offset = offsets[i]
original_tensor = self._named_parameters[k]
numel = original_tensor.numel()
tensors[k] = merged_tensor.data[offset:offset+numel].view(original_tensor.shape)
return tensors
def _allreduce_grad_async(self, p, name):
tensor = p.data.view(-1)
allreduce_name = name
if len(name) > 100:
allreduce_name = name[0:50]+'...'+name[50:100]
handle = allreduce_async_(tensor, average=True, name=allreduce_name)
return handle, None
def check_hooked_tensor_sequence(self, name):
if self._seq_layernames is None:
return
ntensors = len(self._seq_layernames)
idx = self._seq_layernames.index(name)
if idx == ntensors-self._hook_checked_idx-1:
self._hook_checked_idx += 1
if idx == 0:
self._hook_checked_idx = 0
else:
logger.info('Hook checked error, name: %s should be in the index of %d, which it runs at %d',
name, self._hook_checked_idx, idx)
raise
def _make_hook(self, p):
def hook(*ignore):
assert p not in self._handles
assert not p.grad.requires_grad
if not self.local:
name = self._parameter_names.get(p)
self.check_hooked_tensor_sequence(name)
new_name, new_tensor = self._push_to_buffer(name, p.grad.data)
if new_tensor is not None:
handle, ctx = self._allreduce_grad_async(new_tensor, new_name)
self._handles[new_tensor] = (handle, ctx, 1)
return hook
def synchronize(self):
for p, value in self._handles.items():
name = self._merged_parameter_names.get(p)
handle, ctx, density = value
stime = time.time()
output = synchronize(handle)
if self._profiling:
utils.force_insert_item(self._allreduce_timers, name, time.time()-stime)
stime = time.time()
if self._norm_clip is not None:
norm_clip = np.sqrt(1.0/size()) * self._norm_clip
norm_type = 2.0
param_norm = output.norm(norm_type)
total_norm = param_norm.item()
clip_coef = norm_clip / (total_norm + 1e-6)
if clip_coef < 1:
output.mul_(clip_coef)
p.set_(output)
if self._profiling:
utils.force_insert_item(self._update_times, name, time.time()-stime)
if len(self._groups) != len(self._sequential_keys):
for merged_p, value in self._handles.items():
new_name = self._merged_parameter_names.get(merged_p)
tensors = self._pull_from_buffer(new_name, merged_p)
for n in tensors:
p = self._named_parameters.get(n)
if settings.FP16:
p.grad.set_(tensors[n].data.type(p.grad.type()))
else:
p.grad.set_(tensors[n].data)
self.train_iter += 1
self._handles.clear()
self._print_profiling()
def _print_profiling(self):
if self._profiling and rank() == 0 and len(self._allreduce_timers.keys()) > 0 and len(self._allreduce_timers.get(self._allreduce_timers.keys()[0], [])) == 40:
cps = self._compression_timers # compression
ars = self._allreduce_timers # allreduce times
ups = self._update_times # update times
r = rank()
tcp = 0.0; tar = 0.0; tup = 0.0; total=0.0
for k in cps:
acp = np.mean(cps[k])
tcp += acp
aar = np.mean(ars[k])
tar += aar
aup = np.mean(ups[k])
tup += aup
total = tcp+tar+tup
logger.info('[%d]: Total compress: %f, allreduce: %f, update: %f, total: %f', r, tcp, tar, tup, total)
cps.clear()
ars.clear()
ups.clear()
def step(self, closure=None):
if not self.local:
self.synchronize()
return super(self.__class__, self).step(closure)
def DistributedOptimizer(optimizer, named_parameters=None, compression=None, density=0.001, seq_layernames=None, layerwise_times=None, norm_clip=None, threshold=0, writer=None, gradient_path=None):
"""
An optimizer that wraps another torch.optim.Optimizer, using an allreduce to
average gradient values before applying gradients to model weights.
Allreduce operations are executed after each gradient is computed by `loss.backward()`
in parallel with each other. The `step()` method ensures that all allreduce operations are
finished before applying gradients to the model.
DistributedOptimizer exposes the `synchronize()` method, which forces allreduce operations
to finish before continuing the execution. It's useful in conjunction with gradient
clipping, or other operations that modify gradients in place before `step()` is executed.
Example of gradient clipping:
```
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.synchronize()
torch.nn.utils.clip_grad_norm(model.parameters(), args.clip)
optimizer.step()
```
Arguments:
optimizer: Optimizer to use for computing gradients and applying updates.
named_parameters: A mapping between parameter names and values. Used for naming of
allreduce operations. Typically just `model.named_parameters()`.
compression: Compression algorithm used during allreduce to reduce the amount
of data sent during the each parameter update step. Defaults to
not using compression.
"""
# We dynamically create a new class that inherits from the optimizer that was passed in.
# The goal is to override the `step()` method with an allreduce implementation.
cls = type(optimizer.__class__.__name__, (optimizer.__class__,),
dict(_DistributedOptimizer.__dict__))
return cls(optimizer.param_groups, named_parameters, compression, seq_layernames=seq_layernames, layerwise_times=layerwise_times, norm_clip=None, threshold=threshold, writer=writer, gradient_path=gradient_path)
def broadcast_parameters(params, root_rank):
"""
Broadcasts the parameters from root rank to all other processes.
Typical usage is to broadcast the `model.state_dict()`,
`model.named_parameters()`, or `model.parameters()`.
Arguments:
params: One of the following:
- list of parameters to broadcast
- dict of parameters to broadcast
root_rank: The rank of the process from which parameters will be
broadcasted to all other processes.
"""
if isinstance(params, dict):
params = sorted(params.items())
elif isinstance(params, list):
# support both named_parameters() and regular parameters()
params = [p if isinstance(p, tuple) else (None, p) for p in params]
else:
raise ValueError('invalid params of type: %s' % type(params))
# Run asynchronous broadcasts.
handles = []
for name, p in params:
handle = broadcast_async_(p, root_rank, name)
handles.append(handle)
# Wait for completion.
for handle in handles:
synchronize(handle)
def broadcast_optimizer_state(optimizer, root_rank):
"""
Broadcasts an optimizer state from root rank to all other processes.
Arguments:
optimizer: An optimizer.
root_rank: The rank of the process from which the optimizer will be
broadcasted to all other processes.
"""
if isinstance(optimizer, torch.optim.LBFGS):
# TODO(travis): L-BFGS cannot be easily supported without serializing
# the entire state_dict, as its structure is deeply nested and contains
# None type parameter values
raise ValueError('cannot broadcast torch.optim.LBFGS state')
state_dict = optimizer.state_dict()
# Newly created optimizers will not have their state initialized, so
# do that initialization here
if len(state_dict['state']) == 0:
for group in optimizer.param_groups:
for p in group['params']:
p.grad = p.data.new(p.size()).zero_()
# This function accepts a torch.optim.Optimizer or a DistributedOptimizer
# wrapped around a torch optimizer. Calling step() with a DistributedOptimizer
# forces allreduce on all model parameters, which will result in deadlock
# unless every rank calls step(). Therefore, to finish state initialization
# only call optimizer.step() with a torch.optim.Optimizer.
if optimizer.__module__ == DistributedOptimizer.__module__:
super(optimizer.__class__, optimizer).step()
else:
optimizer.step()
state_dict = optimizer.state_dict()
# If the state_dict is still empty after initialization, then
# the optimizer is stateless, and there is nothing to broadcast.
# Furthermore, attempting to access the state dict would result in
# an error.
if len(state_dict['state']) == 0:
return
params = []
callbacks = {}
occurrences = collections.defaultdict(int)
# Returns the full type structure of the possibly nested objects for recursive casting back
def _get_types(x):
if isinstance(x, collections.Iterable):
return type(x), [_get_types(xi) for xi in x]
else:
return type(x)
# Casts an object encoded in a tensor back into its original type and subtypes
def _recursive_cast(x, dtype):
if isinstance(dtype, tuple):
t, dtypes = dtype
x = t(x)
return t([_recursive_cast(x[i], dtypes[i]) for i in range(len(x))])
else:
return dtype(x)
# Some optimizer parameters may be represented as scalars instead of
# tensors. In such cases, we need to wrap the scalar in a tensor, then
# broadcast, then update the appropriate value in the state_dict with the
# new unwrapped scalar value via a callback.
def _create_callback(pid, name, t, p):
def _from_tensor():
state_dict['state'][pid][name] = t(p.numpy()[0])
return _from_tensor
def _create_option_callback(index, option_key, option_tensor, dtypes):
def _from_tensor():
optimizer.param_groups[index][option_key] = _recursive_cast(option_tensor.numpy()[0], dtypes)
return _from_tensor
# Param groups are an ordered list, normally there is only one per model,
# but users can add additional param groups for example to train
# previously frozen layers
for index, group in enumerate(state_dict['param_groups']):
# Broadcast options like learning rate
for option_key, option_value in group.items():
if option_key == 'params':
continue
# Options like the learning rate are scalar, and need to be wrapped in tensors
key = '%s.%d' % (option_key, index)
dtypes = _get_types(option_value)
option_tensor = torch.Tensor([option_value])
callbacks[key] = _create_option_callback(index, option_key, option_tensor, dtypes)
params.append((key, option_tensor))
# The params list here is ordered by the layers in the model
for pid in group['params']:
param_state = state_dict['state'][pid]
for name, p in param_state.items():
# Some parameter names may appear more than once, in which
# case we ensure they have a unique identifier defined by
# their order
occurrences[name] += 1
key = '%s.%d' % (str(name), occurrences[name])
if not torch.is_tensor(p):
# Wrap the scalar in a FloatTensor, and remember its type
# so we can cast it back after unwrapping
t = type(p)
p = torch.Tensor([p])
callbacks[key] = _create_callback(pid, name, t, p)
params.append((key, p))
# Synchronized broadcast of all parameters
broadcast_parameters(params, root_rank)
# Post-broadcast clenaup for non-tensor parameters
for key, p in params:
if key in callbacks:
callbacks[key]()