-
Notifications
You must be signed in to change notification settings - Fork 1
/
losses.py
323 lines (265 loc) · 9.23 KB
/
losses.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
import os
import math
import torch
import torch.distributed as dist
from logging import getLogger
logger = getLogger()
def gpu_timer(closure, log_timings=True):
""" Helper to time gpu-time to execute closure() """
elapsed_time = -1.
if log_timings:
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
result = closure()
if log_timings:
end.record()
torch.cuda.synchronize()
elapsed_time = start.elapsed_time(end)
return result, elapsed_time
def init_distributed(port=40101, rank_and_world_size=(None, None)):
if dist.is_available() and dist.is_initialized():
return dist.get_world_size(), dist.get_rank()
rank, world_size = rank_and_world_size
os.environ['MASTER_ADDR'] = 'localhost'
if (rank is None) or (world_size is None):
try:
world_size = int(os.environ['SLURM_NTASKS'])
rank = int(os.environ['SLURM_PROCID'])
os.environ['MASTER_ADDR'] = os.environ['HOSTNAME']
except Exception:
logger.info('distributed training not available')
world_size, rank = 1, 0
return world_size, rank
try:
os.environ['MASTER_PORT'] = str(port)
torch.distributed.init_process_group(
backend='nccl',
world_size=world_size,
rank=rank)
except Exception:
world_size, rank = 1, 0
logger.info('distributed training not available')
return world_size, rank
class WarmupCosineSchedule(torch.optim.lr_scheduler.LambdaLR):
def __init__(
self,
optimizer,
warmup_steps,
start_lr,
ref_lr,
T_max,
last_epoch=-1,
final_lr=0.
):
self.start_lr = start_lr
self.ref_lr = ref_lr
self.final_lr = final_lr
self.warmup_steps = warmup_steps
self.T_max = T_max - warmup_steps
super(WarmupCosineSchedule, self).__init__(
optimizer,
self.lr_lambda,
last_epoch=last_epoch)
def lr_lambda(self, step):
if step < self.warmup_steps:
progress = float(step) / float(max(1, self.warmup_steps))
new_lr = self.start_lr + progress * (self.ref_lr - self.start_lr)
return new_lr / self.ref_lr
# -- progress after warmup
progress = float(step - self.warmup_steps) / float(max(1, self.T_max))
new_lr = max(self.final_lr,
self.final_lr + (self.ref_lr - self.final_lr) * 0.5 * (1. + math.cos(math.pi * progress)))
return new_lr / self.ref_lr
class CSVLogger(object):
def __init__(self, fname, *argv):
self.fname = fname
self.types = []
# -- print headers
with open(self.fname, '+a') as f:
for i, v in enumerate(argv, 1):
self.types.append(v[0])
if i < len(argv):
print(v[1], end=',', file=f)
else:
print(v[1], end='\n', file=f)
def log(self, *argv):
with open(self.fname, '+a') as f:
for i, tv in enumerate(zip(self.types, argv), 1):
end = ',' if i < len(argv) else '\n'
print(tv[0] % tv[1], end=end, file=f)
class AverageMeter(object):
"""computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.max = float('-inf')
self.min = float('inf')
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.max = max(val, self.max)
self.min = min(val, self.min)
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class AllGather(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
if (
dist.is_available()
and dist.is_initialized()
and (dist.get_world_size() > 1)
):
outputs = [torch.zeros_like(x) for _ in range(dist.get_world_size())]
dist.all_gather(outputs, x)
return torch.cat(outputs, 0)
return x
@staticmethod
def backward(ctx, grads):
if (
dist.is_available()
and dist.is_initialized()
and (dist.get_world_size() > 1)
):
s = (grads.shape[0] // dist.get_world_size()) * dist.get_rank()
e = (grads.shape[0] // dist.get_world_size()) * (dist.get_rank() + 1)
grads = grads.contiguous()
dist.all_reduce(grads)
return grads[s:e]
return grads
class AllReduce(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
if (
dist.is_available()
and dist.is_initialized()
and (dist.get_world_size() > 1)
):
x = x.contiguous() / dist.get_world_size()
dist.all_reduce(x)
return x
@staticmethod
def backward(ctx, grads):
return grads
logger = getLogger()
def init_partion_loss(
multicrop=6,
tau=0.1,
T=0.25,
me_max=True
):
softmax = torch.nn.Softmax(dim=1)
def sharpen(p):
sharp_p = p**(1./T)
sharp_p /= torch.sum(sharp_p, dim=1, keepdim=True)
return sharp_p
def snn(query, supports, labels):
query = torch.nn.functional.normalize(query)
supports = torch.nn.functional.normalize(supports)
supports = AllGather.apply(supports)
return softmax(query @ supports.T / tau) @ (labels.float())
def loss(
anchor_views,
anchor_supports,
anchor_support_labels,
target_views,
target_supports,
target_support_labels,
sharpen=sharpen,
snn=snn
):
batch_size = len(anchor_views) // (2+multicrop)
probs = snn(anchor_views, anchor_supports, anchor_support_labels)
targets = snn(target_views, target_supports, target_support_labels)
targets = sharpen(targets)
targets[targets < 1e-4] *= 0
loss = torch.mean(torch.sum(torch.log(probs**(-targets)), dim=1))
rloss = 0.
if me_max:
avg_probs = AllReduce.apply(torch.mean(sharpen(probs), dim=0))
rloss -= torch.sum(torch.log(avg_probs**(-avg_probs)))
return loss, rloss
return loss
def make_labels_matrix(
num_classes,
s_batch_size,
world_size,
device,
unique_classes=False,
smoothing=0.0
):
"""
Make one-hot labels matrix for labeled samples
"""
local_images = s_batch_size*num_classes
total_images = local_images*world_size
off_value = smoothing/(num_classes*world_size) if unique_classes else smoothing/num_classes
if unique_classes:
labels = torch.zeros(total_images, num_classes*world_size).to(device) + off_value
for r in range(world_size):
s1 = r * local_images
e1 = s1 + local_images
offset = r * num_classes
for i in range(num_classes):
labels[s1:e1][i::num_classes][:, offset+i] = 1. - smoothing + off_value
else:
labels = torch.zeros(total_images, num_classes*world_size).to(device) + off_value
for i in range(num_classes):
labels[i::num_classes][:, i] = 1. - smoothing + off_value
return labels
def gather_from_all(tensor):
gathered_tensors = gather_tensors_from_all(tensor)
gathered_tensor = torch.cat(gathered_tensors, 0)
return gathered_tensor
def gather_tensors_from_all(tensor):
"""
Wrapper over torch.distributed.all_gather for performing
'gather' of 'tensor' over all processes in both distributed /
non-distributed scenarios.
"""
if tensor.ndim == 0:
# 0 dim tensors cannot be gathered. so unsqueeze
tensor = tensor.unsqueeze(0)
if (
torch.distributed.is_available()
and torch.distributed.is_initialized()
and (torch.distributed.get_world_size() > 1)
):
tensor, orig_device = convert_to_distributed_tensor(tensor)
gathered_tensors = [
torch.zeros_like(tensor) for _ in range(torch.distributed.get_world_size())
]
torch.distributed.all_gather(gathered_tensors, tensor)
gathered_tensors = [
convert_to_normal_tensor(_tensor, orig_device)
for _tensor in gathered_tensors
]
else:
gathered_tensors = [tensor]
return gathered_tensors
def convert_to_distributed_tensor(tensor):
"""
For some backends, such as NCCL, communication only works if the
tensor is on the GPU. This helper function converts to the correct
device and returns the tensor + original device.
"""
orig_device = 'cpu' if not tensor.is_cuda else 'gpu'
if (
torch.distributed.is_available()
and torch.distributed.get_backend() == torch.distributed.Backend.NCCL
and not tensor.is_cuda
):
tensor = tensor.cuda()
return (tensor, orig_device)
def convert_to_normal_tensor(tensor, orig_device):
"""
For some backends, such as NCCL, communication only works if the
tensor is on the GPU. This converts the tensor back to original device.
"""
if tensor.is_cuda and orig_device == 'cpu':
tensor = tensor.cpu()
return tensor