-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_hmda.py
177 lines (154 loc) · 7.32 KB
/
d_hmda.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
import numpy as np
import torch
import torch.nn as nn
from d_model import *
from d_eventTimer import EventTimer
from d_algo import *
import torch.distributed as dist
import torchopt
import torch.nn.functional as F
from torch.utils.data import DataLoader
def d_HMDA_train(cur_rank,
d_trainset_X,
d_trainset_y,
func_per_example_grad,
fmodel,
params,
buffers,
optimizer,
opt_state,
sorter,
counter,
eventTimer: EventTimer,
epoch,
n,
microbatch,
d,
device=None):
with eventTimer(f'epoch-{epoch}'):
with eventTimer('sorter'):
perm_list = sorter.sort()
if isinstance(sorter, CD_GraB):
with eventTimer(f'epoch-{epoch}'):
with eventTimer("communication"):
gathered_grads = torch.empty(n, microbatch, d, device=device)
for idx in range(0, d_trainset_X.shape[1], microbatch):
batch = torch.arange(
idx, min(idx + microbatch, d_trainset_X.shape[1]), device=device)
# Using the obtained order, we get the training examples
with eventTimer(f'epoch-{epoch}'):
with eventTimer("dataset"):
X = d_trainset_X[cur_rank][perm_list[batch]]
y = d_trainset_y[cur_rank][perm_list[batch]]
if isinstance(sorter, D_RR):
with eventTimer(f'epoch-{epoch}'):
with eventTimer("forward-backward"):
avg_grads = torch.autograd.grad(F.binary_cross_entropy_with_logits(
fmodel(params, buffers, X).squeeze(), y), params)
with torch.no_grad():
avg_grads = torch.cat([g.view(-1) for g in avg_grads])
with torch.no_grad():
with eventTimer("communication"):
dist.all_reduce(avg_grads, op=dist.ReduceOp.SUM)
avg_grads /= n
elif isinstance(sorter, CD_GraB):
with eventTimer(f'epoch-{epoch}'):
with eventTimer("forward-backward"):
avg_grads = torch.autograd.grad(F.binary_cross_entropy_with_logits(fmodel(params, buffers, X).squeeze(), y), params)
with torch.no_grad():
avg_grads = torch.cat([g.view(-1) for g in avg_grads])
with torch.no_grad():
with eventTimer(f'epoch-{epoch}'):
with eventTimer("communication"):
dist.all_gather_into_tensor(gathered_grads, avg_grads, async_op=False)
avg_grads = gathered_grads.mean(dim=0)
with eventTimer("sorter"):
sorter.step(gathered_grads, batch)
else:
raise NotImplementedError()
with torch.no_grad():
with eventTimer(f'epoch-{epoch}'):
with eventTimer("SGD"):
# compute gradient and do SGD step
avg_grad_list = []
grad_cnt = 0
for p in params:
avg_grad_list.append(
avg_grads[grad_cnt: p.numel() + grad_cnt].view(p.shape))
grad_cnt += p.numel()
updates, opt_state = optimizer.update(
avg_grad_list, opt_state, params=params)
torchopt.apply_updates(
params, tuple(updates), inplace=True)
if cur_rank == 0:
counter.update(len(batch))
def d_HMDA_train(cur_rank,
d_trainset_X,
d_trainset_y,
func_per_example_grad,
fmodel,
params,
buffers,
optimizer,
opt_state,
sorter,
counter,
eventTimer: EventTimer,
epoch,
n,
microbatch,
d,
device=None):
with eventTimer(f'epoch-{epoch}'):
with eventTimer('sorter'):
perm_list = sorter.sort()
if isinstance(sorter, CD_GraB):
with eventTimer(f'epoch-{epoch}'):
with eventTimer("communication"):
gathered_grads = torch.empty(n, microbatch, d, device=device)
for idx in range(0, d_trainset_X.shape[1], microbatch):
batch = torch.arange(idx, min(idx + microbatch, d_trainset_X.shape[1]), device=device)
# Using the obtained order, we get the training examples
with eventTimer(f'epoch-{epoch}'):
with eventTimer("dataset"):
X = d_trainset_X[cur_rank][perm_list[batch]]
y = d_trainset_y[cur_rank][perm_list[batch]]
if isinstance(sorter, D_RR):
with eventTimer(f'epoch-{epoch}'):
with eventTimer("forward-backward"):
avg_grads = torch.autograd.grad(F.binary_cross_entropy_with_logits(fmodel(params, buffers, X).squeeze(), y), params)
with torch.no_grad():
avg_grads = torch.cat([g.view(-1) for g in avg_grads])
with torch.no_grad():
with eventTimer("communication"):
dist.all_reduce(avg_grads, op=dist.ReduceOp.SUM)
avg_grads /= n
elif isinstance(sorter, CD_GraB):
with eventTimer(f'epoch-{epoch}'):
with eventTimer("forward-backward"):
per_example_grads = func_per_example_grad(params, buffers, X, y)
with torch.no_grad():
per_example_grads = torch.hstack(
[g.view(g.shape[0], g.numel() // g.shape[0]) for g in per_example_grads])
with torch.no_grad():
with eventTimer(f'epoch-{epoch}'):
with eventTimer("communication"):
dist.all_gather_into_tensor(gathered_grads, per_example_grads, async_op=False)
avg_grads = gathered_grads.mean(dim=(0, 1))
with eventTimer("sorter"):
sorter.step(gathered_grads, batch)
else:
raise NotImplementedError()
with torch.no_grad():
with eventTimer(f'epoch-{epoch}'):
with eventTimer("SGD"):
# compute gradient and do SGD step
avg_grad_list = []
grad_cnt = 0
for p in params:
avg_grad_list.append(avg_grads[grad_cnt: p.numel() + grad_cnt].view(p.shape))
grad_cnt += p.numel()
updates, opt_state = optimizer.update(avg_grad_list, opt_state, params=params)
torchopt.apply_updates(params, tuple(updates), inplace=True)
if cur_rank == 0:
counter.update(len(batch))