-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
827 lines (716 loc) · 30.6 KB
/
utils.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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
"""
This file is modified from:
https://github.com/facebookresearch/deit/blob/main/utils.py
"""
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
"""
Misc functions, including distributed helpers.
Mostly copy-paste from torchvision references.
"""
import io
import os
import time,random
import numpy as np
from collections import defaultdict, deque
import datetime
import torch
import torch.distributed as dist
import torch.nn.functional as F
import torch, math
import torch.nn as nn
from torch import Tensor
from torchvision.utils import save_image, make_grid
import cv2
import torch.nn.functional as nnf
from einops import rearrange, repeat
import pickle
import gzip
try:
from torchtext.vocab import build_vocab_from_iterator
except:
pass
from itertools import groupby
import tensorflow as tf
import matplotlib.pyplot as plt # For graphics
import seaborn as sns
from torchvision.utils import save_image, make_grid
def is_main_process():
return 'WORLD_SIZE' not in os.environ or os.environ['WORLD_SIZE']=='1' or os.environ['LOCAL_RANK']=='0'
class SmoothedValue(object):
"""Track a series of values and provide access to smoothed values over a
window or the global series average.
"""
def __init__(self, window_size=20, fmt=None):
if fmt is None:
fmt = "{median:.4f} ({global_avg:.4f})"
self.deque = deque(maxlen=window_size)
self.total = 0.0
self.count = 0
self.fmt = fmt
def update(self, value, n=1):
self.deque.append(value)
self.count += n
self.total += value * n
def synchronize_between_processes(self):
"""
Warning: does not synchronize the deque!
"""
if not is_dist_avail_and_initialized():
return
t = torch.tensor([self.count, self.total], dtype=torch.float64, device='cuda')
dist.barrier()
dist.all_reduce(t)
t = t.tolist()
self.count = int(t[0])
self.total = t[1]
@property
def median(self):
d = torch.tensor(list(self.deque))
return d.median().item()
@property
def avg(self):
d = torch.tensor(list(self.deque), dtype=torch.float32)
return d.mean().item()
@property
def global_avg(self):
return self.total / self.count
@property
def max(self):
return max(self.deque)
@property
def value(self):
return self.deque[-1]
def __str__(self):
return self.fmt.format(
median=self.median,
avg=self.avg,
global_avg=self.global_avg,
max=self.max,
value=self.value)
class MetricLogger(object):
def __init__(self, delimiter="\t"):
self.meters = defaultdict(SmoothedValue)
self.delimiter = delimiter
def update(self, **kwargs):
for k, v in kwargs.items():
if isinstance(v, torch.Tensor):
v = v.item()
assert isinstance(v, (float, int))
self.meters[k].update(v)
def __getattr__(self, attr):
if attr in self.meters:
return self.meters[attr]
if attr in self.__dict__:
return self.__dict__[attr]
raise AttributeError("'{}' object has no attribute '{}'".format(
type(self).__name__, attr))
def __str__(self):
loss_str = []
for name, meter in self.meters.items():
loss_str.append(
"{}: {}".format(name, str(meter))
)
return self.delimiter.join(loss_str)
def synchronize_between_processes(self):
for meter in self.meters.values():
meter.synchronize_between_processes()
def add_meter(self, name, meter):
self.meters[name] = meter
def log_every(self, iterable, print_freq, header=None):
i = 0
if not header:
header = ''
start_time = time.time()
end = time.time()
iter_time = SmoothedValue(fmt='{avg:.4f}')
data_time = SmoothedValue(fmt='{avg:.4f}')
space_fmt = ':' + str(len(str(len(iterable)))) + 'd'
log_msg = [
header,
'[{0' + space_fmt + '}/{1}]',
'eta: {eta}',
'{meters}',
'time: {time}',
'data: {data}'
]
if torch.cuda.is_available():
log_msg.append('max mem: {memory:.0f}')
log_msg = self.delimiter.join(log_msg)
MB = 1024.0 * 1024.0
for obj in iterable:
data_time.update(time.time() - end)
yield obj
iter_time.update(time.time() - end)
if i % print_freq == 0 or i == len(iterable) - 1:
eta_seconds = iter_time.global_avg * (len(iterable) - i)
eta_string = str(datetime.timedelta(seconds=int(eta_seconds)))
if torch.cuda.is_available() and torch.cuda.device_count() > 1:
if os.environ['LOCAL_RANK'] == '0':
if torch.cuda.is_available():
print(log_msg.format(
i, len(iterable), eta=eta_string,
meters=str(self),
time=str(iter_time), data=str(data_time),
memory=torch.cuda.max_memory_allocated() / MB))
else:
print(log_msg.format(
i, len(iterable), eta=eta_string,
meters=str(self),
time=str(iter_time), data=str(data_time)))
else:
if torch.cuda.is_available():
print(log_msg.format(
i, len(iterable), eta=eta_string,
meters=str(self),
time=str(iter_time), data=str(data_time),
memory=torch.cuda.max_memory_allocated() / MB))
else:
print(log_msg.format(
i, len(iterable), eta=eta_string,
meters=str(self),
time=str(iter_time), data=str(data_time)))
i += 1
end = time.time()
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
if torch.cuda.is_available() and torch.cuda.device_count() > 1:
if os.environ['LOCAL_RANK'] == '0':
print('{} Total time: {} ({:.4f} s / it)'.format(
header, total_time_str, total_time / len(iterable)))
else:
print('{} Total time: {} ({:.4f} s / it)'.format(
header, total_time_str, total_time / len(iterable)))
def count_parameters_in_MB(model):
# sum(p.numel() for p in model.parameters() if p.requires_grad)
return np.sum(np.prod(v.size()) for name, v in model.named_parameters())/1e6
def _load_checkpoint_for_ema(model_ema, checkpoint):
"""
Workaround for ModelEma._load_checkpoint to accept an already-loaded object
"""
mem_file = io.BytesIO()
torch.save(checkpoint, mem_file)
mem_file.seek(0)
model_ema._load_checkpoint(mem_file)
def setup_for_distributed(is_master):
"""
This function disables printing when not in master process
"""
import builtins as __builtin__
builtin_print = __builtin__.print
def print(*args, **kwargs):
force = kwargs.pop('force', False)
if is_master or force:
builtin_print(*args, **kwargs)
__builtin__.print = print
def is_dist_avail_and_initialized():
if not dist.is_available():
return False
if not dist.is_initialized():
return False
return True
def get_world_size():
if not is_dist_avail_and_initialized():
return 1
return dist.get_world_size()
def get_rank():
if not is_dist_avail_and_initialized():
return 0
return dist.get_rank()
def is_main_process():
return get_rank() == 0
def save_on_master(*args, **kwargs):
if is_main_process():
torch.save(*args, **kwargs)
def init_distributed_mode(args):
print(os.environ)
if 'RANK' in os.environ and 'WORLD_SIZE' in os.environ:
args.rank = int(os.environ["RANK"])
args.world_size = int(os.environ['WORLD_SIZE'])
args.gpu = int(os.environ['LOCAL_RANK'])
elif 'SLURM_PROCID' in os.environ:
args.rank = int(os.environ['SLURM_PROCID'])
args.gpu = args.rank % torch.cuda.device_count()
else:
print('Not using distributed mode')
args.distributed = False
return
args.distributed = True
torch.cuda.set_device(args.gpu)
args.dist_backend = 'nccl'
print('| distributed init (rank {}): {}'.format(
args.rank, args.dist_url), flush=True)
torch.distributed.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
world_size=args.world_size, rank=args.rank)
torch.distributed.barrier()
setup_for_distributed(args.rank == 0)
def sampler_func(clip, sn, random_choice=True):
if random_choice:
f = lambda n: [(lambda n, arr: n if arr == [] else np.random.choice(arr))(n * i / sn,
range(int(n * i / sn),
max(int(n * i / sn) + 1,
int(n * (
i + 1) / sn))))
for i in range(sn)]
else:
f = lambda n: [(lambda n, arr: n if arr == [] else int(np.mean(arr)))(n * i / sn, range(int(n * i / sn),
max(int(
n * i / sn) + 1,
int(n * (
i + 1) / sn))))
for i in range(sn)]
return f(clip)
def cosine_scheduler(base_value, final_value, epochs):
iters = np.arange(epochs)
schedule = final_value + 0.5 * (base_value - final_value) * (1 + np.cos(np.pi * iters / len(iters)))
return schedule
def cosine_scheduler_func(base_value, final_value, iters, epochs):
schedule = lambda x: final_value + 0.5 * (base_value - final_value) * (1 + np.cos(np.pi * x / epochs))
return schedule(iters)
def load_dataset_file(filename):
with open(filename, "rb") as f:
loaded_object = pickle.load(f)
return loaded_object
def build_vocab(file_path,UNK_IDX,specials_symbols):
vocab = build_vocab_from_iterator(yield_tokens(file_path), specials=specials_symbols,min_freq=1)
vocab.set_default_index(UNK_IDX)
return vocab
def yield_tokens(file_path):
with io.open(file_path, encoding='utf-8') as f:
for line in f:
yield line.strip().split()
@torch.no_grad()
def concat_all_gather(tensor):
"""
Performs all_gather operation on the provided tensors.
*** Warning ***: torch.distributed.all_gather has no gradient.
"""
tensors_gather = [torch.ones_like(tensor)
for _ in range(torch.distributed.get_world_size())]
torch.distributed.all_gather(tensors_gather, tensor, async_op=False)
output = torch.cat(tensors_gather,dim=0)
return output
def gloss_tokens_to_sequences(tokens,tgt_vocab,type = 'tensor'):
if type=='list':
sequences = []
for token in tokens:
sequence = tgt_vocab.lookup_tokens(token)
sequence = ' '.join(sequence)
sequences.append(sequence)
return sequences
else:
tokens = tokens.transpose(0,1)
sequences = []
for i in range(len(tokens)):
token = tokens[i,:].tolist()
for j1 in range(len(token)):
if token[j1] == PAD_IDX:
token = token[0:j1]
break
if j1 == len(token)-1:
token = token[0:j1]
sequence = tgt_vocab.lookup_tokens(token)
sequence = ' '.join(sequence)
sequences.append(sequence)
return sequences
def NoiseInjecting(raw_gloss, noise_rate=0.15, noise_type='omit_last', random_shuffle=False, is_train=True):
new_gloss = []
for ii, gloss in enumerate(raw_gloss):
text = gloss.split()
if noise_type == 'omit':
# del noise
if random.uniform(0, 1) <= 1. and is_train:
index = sampler_func(len(text), int(len(text)*(1. - noise_rate)), random_choice=is_train)
noise_gloss = []
noise_idx = []
for i, d in enumerate(text):
if i in index:
noise_gloss.append(d)
else:
noise_gloss.append(WORD_MASK)
noise_idx.append(i)
else:
noise_gloss = [d for d in text]
elif noise_type == 'omit_last' :
if random.uniform(0, 1) <= 1.0 and is_train:
index = np.arange(0, len(text) - int(np.ceil(len(text)*(np.random.uniform(0,noise_rate,(1,))))), 1, dtype=int)
noise_gloss = []
for i, d in enumerate(text):
if i in index:
noise_gloss.append(d)
else:
noise_gloss.append(WORD_MASK)
else:
noise_gloss = [d for d in text]
if is_train and random_shuffle and random.uniform(0, 1) > 0.5:
random.shuffle(noise_gloss) # random shuffle sequence
new_gloss.append(' '.join(noise_gloss))
return new_gloss
def GlossPadding(input_ids, gt_gloss, attention_mask):
new_input_ids, new_gt_gloss, new_mask = [], [], []
for NG, TG, MASK in zip(input_ids, gt_gloss, attention_mask):
if len(NG) > len(TG):
while len(NG) != len(TG):
TG.append(1)
if len(NG) < len(TG):
while len(NG) != len(TG):
NG.append(1)
MASK.append(0)
new_input_ids.append(NG)
new_gt_gloss.append(TG)
new_mask.append(MASK)
return torch.tensor(new_input_ids), torch.tensor(new_gt_gloss), torch.tensor(new_mask)
def ctc_decode(gloss_probabilities,sgn_lengths):
gloss_probabilities = gloss_probabilities.cpu().detach().numpy()
# tf_gloss_probabilities = np.concatenate(
# (gloss_probabilities[:, :, 1:], gloss_probabilities[:, :, 0, None]),
# axis=-1,
# )
# ctc_decode, _ = tf.nn.ctc_greedy_decoder(
# inputs=gloss_probabilities,
# sequence_length=np.array(sgn_lengths),
# blank_index=SI_IDX,
# merge_repeated = False
# )
ctc_decode, _ = tf.nn.ctc_beam_search_decoder(
inputs=gloss_probabilities,
sequence_length=np.array(sgn_lengths),
beam_width=5,
top_paths=1,
)
ctc_decode = ctc_decode[0]
# Create a decoded gloss list for each sample
tmp_gloss_sequences = [[] for i in range(gloss_probabilities.shape[1])]
for (value_idx, dense_idx) in enumerate(ctc_decode.indices):
if ctc_decode.values[value_idx].numpy() != SI_IDX:
tmp_gloss_sequences[dense_idx[0]].append(
ctc_decode.values[value_idx].numpy()
)
decoded_gloss_sequences = []
for seq_idx in range(0, len(tmp_gloss_sequences)):
decoded_gloss_sequences.append(
[x[0] for x in groupby(tmp_gloss_sequences[seq_idx])]
)
return decoded_gloss_sequences
def data_augmentation(resize=(320, 240), crop_size=224, is_train=True):
if is_train:
left, top = np.random.randint(0, resize[0] - crop_size), np.random.randint(0, resize[1] - crop_size)
else:
left, top = (resize[0] - crop_size) // 2, (resize[1] - crop_size) // 2
return (left, top, left + crop_size, top + crop_size), resize
class TemporalRescale(object):
def __init__(self, temp_scaling=0.2):
self.min_len = 32
self.max_len = 300
self.L = 1.0 - temp_scaling
self.U = 1.0 + temp_scaling
def __call__(self, clip):
vid_len = len(clip)
new_len = int(vid_len * (self.L + (self.U - self.L) * np.random.random()))
if new_len < self.min_len:
new_len = self.min_len
if new_len > self.max_len:
new_len = self.max_len
if (new_len - 4) % 4 != 0:
new_len += 4 - (new_len - 4) % 4
if new_len <= vid_len:
index = sorted(random.sample(range(vid_len), new_len))
else:
index = sorted(random.choices(range(vid_len), k=new_len))
return clip[index]
def visualization(atten_maps):
os.makedirs('./demo', exist_ok=True)
for ii, att in enumerate(atten_maps):
i = att.shape[0]
idx = [max(1, int((i**0.5))), i//max(1, int((i**0.5))), 1]
fig = plt.figure(figsize=(6*idx[1], 6*idx[0]))
if att.squeeze().dim() == 2:
ax = fig.add_subplot()
att = torch.softmax(att, dim=-1)
sns.heatmap(att.detach().cpu().numpy(), annot=False, yticklabels=False, xticklabels=False, fmt='g', ax=ax)
fig.savefig(os.path.join('./demo', f'Att_score_{ii}.jpg'), dpi=fig.dpi)
plt.close()
continue
for cmp in att:
ax = fig.add_subplot(*idx)
sns.heatmap(cmp.detach().cpu().numpy(), cbar=idx[-1] % idx[-2] == 0, annot=False, yticklabels=False, xticklabels=False, fmt='g', ax=ax)
idx[-1] += 1
fig.savefig(os.path.join('./demo', f'Att_score_{ii}.jpg'), dpi=fig.dpi)
plt.close()
def gen_label(labels):
num = len(labels)
gt = np.zeros(shape=(num,num))
for i, label in enumerate(labels):
for k in range(num):
if labels[k] == label:
gt[i,k] = 1
return gt
class KLLoss(torch.nn.Module):
"""Loss that uses a 'hinge' on the lower bound.
This means that for samples with a label value smaller than the threshold, the loss is zero if the prediction is
also smaller than that threshold.
args:
error_matric: What base loss to use (MSE by default).
threshold: Threshold to use for the hinge.
clip: Clip the loss if it is above this value.
"""
def __init__(self, error_metric=torch.nn.KLDivLoss(size_average=True, reduce=True)):
super().__init__()
print('=========using KL Loss=and has temperature and * bz==========')
self.error_metric = error_metric
def forward(self, prediction, label):
batch_size = prediction.shape[0]
probs1 = F.log_softmax(prediction, 1)
probs2 = F.softmax(label * 10, 1)
loss = self.error_metric(probs1, probs2) * batch_size
return loss
def loss_fn_kd(outputs, teacher_outputs, T=1.0, alpha=0.5):
"""
Compute the knowledge-distillation (KD) loss given outputs, labels.
"Hyperparameters": temperature and alpha
NOTE: the KL Divergence for PyTorch comparing the softmaxs of teacher
and student expects the input tensor to be log probabilities! See Issue #2
"""
KD_loss = torch.nn.KLDivLoss( reduction='sum')(F.log_softmax(outputs/T, dim=1),
F.softmax(teacher_outputs/T, dim=1)) * (T * T) #+ \
# F.cross_entropy(outputs, F.softmax(teacher_outputs, dim=1)) * (1. - alpha)
return KD_loss
class NativeScaler:
state_dict_key = "amp_scaler"
def __init__(self):
self._scaler = torch.cuda.amp.GradScaler()
def __call__(self, loss, optimizer, clip_grad=None, clip_mode='norm', parameters=None, create_graph=False):
self._scaler.scale(loss).backward(create_graph=create_graph)
if clip_grad is not None:
assert parameters is not None
self._scaler.unscale_(optimizer) # unscale the gradients of optimizer's assigned params in-place
dispatch_clip_grad(parameters, clip_grad, mode=clip_mode)
self._scaler.step(optimizer)
self._scaler.update()
def state_dict(self):
return self._scaler.state_dict()
def load_state_dict(self, state_dict):
self._scaler.load_state_dict(state_dict)
def InputMask(gloss_input_ids, gloss_attention_mask, noise_rate=0.1, is_train=True):
mask_matrix = torch.ones_like(gloss_attention_mask)
for i in range(mask_matrix.size(0)):
# index = random.sample(range(0, mask_matrix.size(-1)), int(mask_matrix.size(-1)*noise_rate))
sample = sampler_func(mask_matrix.size(-1)-2, int((mask_matrix.size(-1)-2)*noise_rate), random_choice=is_train)
index = [i+1 for i in sample]
mask_matrix[i, :].scatter_(0, torch.tensor(index, device=mask_matrix.device), 0)
gloss_attention_mask *= mask_matrix.cuda().type(torch.int)
gloss_input_ids = torch.where(mask_matrix==0, torch.ones_like(gloss_input_ids), gloss_input_ids)
# print(gloss_input_ids, gloss_attention_mask)
# gloss_input_ids_filp = gloss_input_ids.flip(0)
# mask_matrix = torch.ones_like(gloss_attention_mask)
# for i in range(mask_matrix.size(0)):
# index = random.sample(range(0, mask_matrix.size(-1)), int(mask_matrix.size(-1)*noise_rate))
# mask_matrix[i, :].scatter_(0, torch.tensor(index, device=mask_matrix.device), 0)
# gloss_input_ids = torch.where(mask_matrix==0, gloss_input_ids_filp, gloss_input_ids)
return gloss_input_ids, gloss_attention_mask
class Dict(dict):
__setattr__ = dict.__setitem__
__getattr__ = dict.__getitem__ # dict.k ==> dict[k]
# __getattr__ = dict.get # dict.k ==> dict.get(k)
# __getattr__ = lambda d, k: d.get(k, '') # dict.k ==> dict.get(k,default)
class PositionalEncoding(nn.Module):
"""
Pre-compute position encodings (PE).
In forward pass, this adds the position-encodings to the
input for as many time steps as necessary.
Implementation based on OpenNMT-py.
https://github.com/OpenNMT/OpenNMT-py
"""
def __init__(self, size: int = 0, max_len: int = 5000):
"""
Positional Encoding with maximum length max_len
:param size:
:param max_len:
:param dropout:
"""
if size % 2 != 0:
raise ValueError(
"Cannot use sin/cos positional encoding with "
"odd dim (got dim={:d})".format(size)
)
pe = torch.zeros(max_len, size)
position = torch.arange(0, max_len).unsqueeze(1)
div_term = torch.exp(
(torch.arange(0, size, 2, dtype=torch.float) * -(math.log(10000.0) / size))
)
pe[:, 0::2] = torch.sin(position.float() * div_term)
pe[:, 1::2] = torch.cos(position.float() * div_term)
pe = pe.unsqueeze(0) # shape: [1, size, max_len]
super(PositionalEncoding, self).__init__()
self.register_buffer("pe", pe)
self.dim = size
def forward(self, emb):
"""Embed inputs.
Args:
emb (FloatTensor): Sequence of word vectors
``(seq_len, batch_size, self.dim)``
"""
# Add position encodings
return emb + self.pe[:, : emb.size(1)]
class MaskedNorm(nn.Module):
"""
Original Code from:
https://discuss.pytorch.org/t/batchnorm-for-different-sized-samples-in-batch/44251/8
"""
def __init__(self, num_features=512, norm_type='sync_batch', num_groups=1):
super().__init__()
self.norm_type = norm_type
if self.norm_type == "batch":
# raise ValueError("Please use sync_batch")
self.norm = nn.BatchNorm1d(num_features=num_features)
elif self.norm_type == 'sync_batch':
self.norm = nn.SyncBatchNorm(num_features=num_features)
elif self.norm_type == "group":
self.norm = nn.GroupNorm(num_groups=num_groups, num_channels=num_features)
elif self.norm_type == "layer":
self.norm = nn.LayerNorm(normalized_shape=num_features)
else:
raise ValueError("Unsupported Normalization Layer")
self.num_features = num_features
def forward(self, x: Tensor, mask: Tensor):
if self.training:
reshaped = x.reshape([-1, self.num_features])
reshaped_mask = mask.reshape([-1, 1]) > 0
selected = torch.masked_select(reshaped, reshaped_mask).reshape(
[-1, self.num_features]
)
batch_normed = self.norm(selected)
scattered = reshaped.masked_scatter(reshaped_mask, batch_normed)
return scattered.reshape([x.shape[0], -1, self.num_features])
else:
reshaped = x.reshape([-1, self.num_features])
batched_normed = self.norm(reshaped)
return batched_normed.reshape([x.shape[0], -1, self.num_features])
class PositionwiseFeedForward(nn.Module):
"""
Position-wise Feed-forward layer
Projects to ff_size and then back down to input_size.
"""
def __init__(self, input_size, ff_size, dropout=0.1, kernel_size=1,
skip_connection=True):
"""
Initializes position-wise feed-forward layer.
:param input_size: dimensionality of the input.
:param ff_size: dimensionality of intermediate representation
:param dropout:
"""
super(PositionwiseFeedForward, self).__init__()
self.layer_norm = nn.LayerNorm(input_size, eps=1e-6)
self.kernel_size = kernel_size
if type(self.kernel_size)==int:
conv_1 = nn.Conv1d(input_size, ff_size, kernel_size=kernel_size, stride=1, padding='same')
conv_2 = nn.Conv1d(ff_size, input_size, kernel_size=kernel_size, stride=1, padding='same')
self.pwff_layer = nn.Sequential(
conv_1,
nn.ReLU(),
nn.Dropout(dropout),
conv_2,
nn.Dropout(dropout),
)
elif type(self.kernel_size)==list:
pwff = []
first_conv = nn.Conv1d(input_size, ff_size, kernel_size=kernel_size[0], stride=1, padding='same')
pwff += [first_conv, nn.ReLU(), nn.Dropout(dropout)]
for ks in kernel_size[1:-1]:
conv = nn.Conv1d(ff_size, ff_size, kernel_size=ks, stride=1, padding='same')
pwff += [conv, nn.ReLU(), nn.Dropout(dropout)]
last_conv = nn.Conv1d(ff_size, input_size, kernel_size=kernel_size[-1], stride=1, padding='same')
pwff += [last_conv, nn.Dropout(dropout)]
self.pwff_layer = nn.Sequential(
*pwff
)
else:
raise ValueError
self.skip_connection=skip_connection
if not skip_connection:
print('Turn off skip_connection in PositionwiseFeedForward')
def forward(self, x):
x_norm = self.layer_norm(x)
x_t = x_norm.transpose(1,2)
x_t = self.pwff_layer(x_t)
if self.skip_connection:
return x_t.transpose(1,2)+x
else:
return x_t.transpose(1,2)
class MLPHead(nn.Module):
def __init__(self, embedding_size, projection_hidden_size):
super().__init__()
self.embedding_size = embedding_size
self.net = nn.Sequential(nn.Linear(self.embedding_size, projection_hidden_size),
nn.BatchNorm1d(projection_hidden_size),
nn.ReLU(True),
nn.Linear(projection_hidden_size, self.embedding_size))
def forward(self, x):
b, l, c = x.shape
x = x.reshape(-1, c) # x.view(-1,c)
x = self.net(x)
return x.reshape(b, l, c) # x.view(b, l, c)
from torch.autograd import Variable
class XentLoss(nn.Module):
"""
Cross-Entropy Loss with optional label smoothing
"""
def __init__(self, pad_index: int, smoothing: float = 0.0):
super(XentLoss, self).__init__()
self.smoothing = smoothing
self.pad_index = pad_index
if self.smoothing <= 0.0:
# standard xent loss
self.criterion = nn.NLLLoss(ignore_index=self.pad_index, reduction="sum")
else:
# custom label-smoothed loss, computed with KL divergence loss
self.criterion = nn.KLDivLoss(reduction="sum")
def _smooth_targets(self, targets: Tensor, vocab_size: int):
"""
Smooth target distribution. All non-reference words get uniform
probability mass according to "smoothing".
:param targets: target indices, batch*seq_len
:param vocab_size: size of the output vocabulary
:return: smoothed target distributions, batch*seq_len x vocab_size
"""
# batch*seq_len x vocab_size
smooth_dist = targets.new_zeros((targets.size(0), vocab_size)).float()
# fill distribution uniformly with smoothing
smooth_dist.fill_(self.smoothing / (vocab_size - 2))
# assign true label the probability of 1-smoothing ("confidence")
smooth_dist.scatter_(1, targets.unsqueeze(1).data, 1.0 - self.smoothing)
# give padding probability of 0 everywhere
smooth_dist[:, self.pad_index] = 0
# masking out padding area (sum of probabilities for padding area = 0)
padding_positions = torch.nonzero(targets.data == self.pad_index)
# pylint: disable=len-as-condition
if len(padding_positions) > 0:
smooth_dist.index_fill_(0, padding_positions.squeeze(), 0.0)
return Variable(smooth_dist, requires_grad=False)
# pylint: disable=arguments-differ
def forward(self, log_probs, targets):
"""
Compute the cross-entropy between logits and targets.
If label smoothing is used, target distributions are not one-hot, but
"1-smoothing" for the correct target token and the rest of the
probability mass is uniformly spread across the other tokens.
:param log_probs: log probabilities as predicted by model
:param targets: target indices
:return:
"""
if self.smoothing > 0:
targets = self._smooth_targets(
targets=targets.contiguous().view(-1), vocab_size=log_probs.size(-1)
)
# targets: distributions with batch*seq_len x vocab_size
assert (
log_probs.contiguous().view(-1, log_probs.size(-1)).shape
== targets.shape
)
else:
# targets: indices with batch*seq_len
targets = targets.contiguous().view(-1)
loss = self.criterion(
log_probs.contiguous().view(-1, log_probs.size(-1)), targets
)
return loss