forked from k2-fsa/icefall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscaling.py
1872 lines (1580 loc) · 67.5 KB
/
scaling.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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2022 Xiaomi Corp. (authors: Daniel Povey)
#
# See ../../../../LICENSE for clarification regarding multiple authors
#
# 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.
import collections
import logging
import math
import random
from functools import reduce
from itertools import repeat
from typing import Optional, Tuple, Union
import k2
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import Tensor
from torch.cuda.amp import custom_bwd, custom_fwd
from torch.nn import Embedding as ScaledEmbedding
class PiecewiseLinear(object):
"""
Piecewise linear function, from float to float, specified as nonempty list of (x,y) pairs with
the x values in order. x values <[initial x] or >[final x] are map to [initial y], [final y]
respectively.
"""
def __init__(self, *args):
assert len(args) >= 1
if len(args) == 1 and isinstance(args[0], PiecewiseLinear):
self.pairs = list(args[0].pairs)
else:
self.pairs = [(float(x), float(y)) for x, y in args]
for (x, y) in self.pairs:
assert isinstance(x, float) or isinstance(x, int)
assert isinstance(y, float) or isinstance(y, int)
for i in range(len(self.pairs) - 1):
assert self.pairs[i + 1][0] > self.pairs[i][0], self.pairs
def __str__(self):
# e.g. 'PiecewiseLinear((0., 10.), (100., 0.))'
return f"PiecewiseLinear({str(self.pairs)[1:-1]})"
def __call__(self, x):
if x <= self.pairs[0][0]:
return self.pairs[0][1]
elif x >= self.pairs[-1][0]:
return self.pairs[-1][1]
else:
cur_x, cur_y = self.pairs[0]
for i in range(1, len(self.pairs)):
next_x, next_y = self.pairs[i]
if x >= cur_x and x <= next_x:
return cur_y + (next_y - cur_y) * (x - cur_x) / (next_x - cur_x)
cur_x, cur_y = next_x, next_y
assert False
def __mul__(self, alpha):
return PiecewiseLinear(*[(x, y * alpha) for x, y in self.pairs])
def __add__(self, x):
if isinstance(x, float) or isinstance(x, int):
return PiecewiseLinear(*[(p[0], p[1] + x) for p in self.pairs])
s, x = self.get_common_basis(x)
return PiecewiseLinear(
*[(sp[0], sp[1] + xp[1]) for sp, xp in zip(s.pairs, x.pairs)]
)
def max(self, x):
if isinstance(x, float) or isinstance(x, int):
x = PiecewiseLinear((0, x))
s, x = self.get_common_basis(x, include_crossings=True)
return PiecewiseLinear(
*[(sp[0], max(sp[1], xp[1])) for sp, xp in zip(s.pairs, x.pairs)]
)
def min(self, x):
if isinstance(x, float) or isinstance(x, int):
x = PiecewiseLinear((0, x))
s, x = self.get_common_basis(x, include_crossings=True)
return PiecewiseLinear(
*[(sp[0], min(sp[1], xp[1])) for sp, xp in zip(s.pairs, x.pairs)]
)
def __eq__(self, other):
return self.pairs == other.pairs
def get_common_basis(self, p: "PiecewiseLinear", include_crossings: bool = False):
"""
Returns (self_mod, p_mod) which are equivalent piecewise lienar
functions to self and p, but with the same x values.
p: the other piecewise linear function
include_crossings: if true, include in the x values positions
where the functions indicate by this and p crosss.
"""
assert isinstance(p, PiecewiseLinear)
# get sorted x-values without repetition.
x_vals = sorted(set([x for x, y in self.pairs] + [x for x, y in p.pairs]))
y_vals1 = [self(x) for x in x_vals]
y_vals2 = [p(x) for x in x_vals]
if include_crossings:
extra_x_vals = []
for i in range(len(x_vals) - 1):
if (y_vals1[i] > y_vals2[i]) != (y_vals1[i + 1] > y_vals2[i + 1]):
# if the two lines in this subsegment potentially cross each other..
diff_cur = abs(y_vals1[i] - y_vals2[i])
diff_next = abs(y_vals1[i + 1] - y_vals2[i + 1])
# `pos`, between 0 and 1, gives the relative x position,
# with 0 being x_vals[i] and 1 being x_vals[i+1].
pos = diff_cur / (diff_cur + diff_next)
extra_x_val = x_vals[i] + pos * (x_vals[i + 1] - x_vals[i])
extra_x_vals.append(extra_x_val)
if len(extra_x_vals) > 0:
x_vals = sorted(set(x_vals + extra_x_vals))
y_vals1 = [self(x) for x in x_vals]
y_vals2 = [p(x) for x in x_vals]
return (
PiecewiseLinear(*zip(x_vals, y_vals1)),
PiecewiseLinear(*zip(x_vals, y_vals2)),
)
class ScheduledFloat(torch.nn.Module):
"""
This object is a torch.nn.Module only because we want it to show up in [top_level module].modules();
it does not have a working forward() function. You are supposed to cast it to float, as
in, float(parent_module.whatever), and use it as something like a dropout prob.
It is a floating point value whose value changes depending on the batch count of the
training loop. It is a piecewise linear function where you specifiy the (x,y) pairs
in sorted order on x; x corresponds to the batch index. For batch-index values before the
first x or after the last x, we just use the first or last y value.
Example:
self.dropout = ScheduledFloat((0.0, 0.2), (4000.0, 0.0), default=0.0)
`default` is used when self.batch_count is not set or in training or mode or in
torch.jit scripting mode.
"""
def __init__(self, *args, default: float = 0.0):
super().__init__()
# self.batch_count and self.name will be written to in the training loop.
self.batch_count = None
self.name = None
self.default = default
self.schedule = PiecewiseLinear(*args)
def extra_repr(self) -> str:
return (
f"batch_count={self.batch_count}, schedule={str(self.schedule.pairs[1:-1])}"
)
def __float__(self):
batch_count = self.batch_count
if batch_count is None or not self.training or torch.jit.is_scripting():
return float(self.default)
else:
ans = self.schedule(self.batch_count)
if random.random() < 0.0002:
logging.info(
f"ScheduledFloat: name={self.name}, batch_count={self.batch_count}, ans={ans}"
)
return ans
def __add__(self, x):
if isinstance(x, float) or isinstance(x, int):
return ScheduledFloat(self.schedule + x, default=self.default)
else:
return ScheduledFloat(
self.schedule + x.schedule, default=self.default + x.default
)
def max(self, x):
if isinstance(x, float) or isinstance(x, int):
return ScheduledFloat(self.schedule.max(x), default=self.default)
else:
return ScheduledFloat(
self.schedule.max(x.schedule), default=max(self.default, x.default)
)
FloatLike = Union[float, ScheduledFloat]
def random_cast_to_half(x: Tensor, min_abs: float = 5.0e-06) -> Tensor:
"""
A randomized way of casting a floating point value to half precision.
"""
if x.dtype == torch.float16:
return x
x_abs = x.abs()
is_too_small = x_abs < min_abs
# for elements where is_too_small is true, random_val will contain +-min_abs with
# probability (x.abs() / min_abs), and 0.0 otherwise. [so this preserves expectations,
# for those elements].
random_val = min_abs * x.sign() * (torch.rand_like(x) * min_abs < x_abs)
return torch.where(is_too_small, random_val, x).to(torch.float16)
class CutoffEstimator:
"""
Estimates cutoffs of an arbitrary numerical quantity such that a specified
proportion of items will be above the cutoff on average.
p is the proportion of items that should be above the cutoff.
"""
def __init__(self, p: float):
self.p = p
# total count of items
self.count = 0
# total count of items that were above the cutoff
self.count_above = 0
# initial cutoff value
self.cutoff = 0
def __call__(self, x: float) -> bool:
"""
Returns true if x is above the cutoff.
"""
ans = x > self.cutoff
self.count += 1
if ans:
self.count_above += 1
cur_p = self.count_above / self.count
delta_p = cur_p - self.p
if (delta_p > 0) == ans:
q = abs(delta_p)
self.cutoff = x * q + self.cutoff * (1 - q)
return ans
class SoftmaxFunction(torch.autograd.Function):
"""
Tries to handle half-precision derivatives in a randomized way that should
be more accurate for training than the default behavior.
"""
@staticmethod
def forward(ctx, x: Tensor, dim: int):
ans = x.softmax(dim=dim)
# if x dtype is float16, x.softmax() returns a float32 because
# (presumably) that op does not support float16, and autocast
# is enabled.
if torch.is_autocast_enabled():
ans = ans.to(torch.float16)
ctx.save_for_backward(ans)
ctx.x_dtype = x.dtype
ctx.dim = dim
return ans
@staticmethod
def backward(ctx, ans_grad: Tensor):
(ans,) = ctx.saved_tensors
with torch.cuda.amp.autocast(enabled=False):
ans_grad = ans_grad.to(torch.float32)
ans = ans.to(torch.float32)
x_grad = ans_grad * ans
x_grad = x_grad - ans * x_grad.sum(dim=ctx.dim, keepdim=True)
return x_grad, None
def softmax(x: Tensor, dim: int):
return SoftmaxFunction.apply(x, dim)
class MaxEigLimiterFunction(torch.autograd.Function):
@staticmethod
def forward(
ctx,
x: Tensor,
coeffs: Tensor,
direction: Tensor,
channel_dim: int,
grad_scale: float,
) -> Tensor:
ctx.channel_dim = channel_dim
ctx.grad_scale = grad_scale
ctx.save_for_backward(x.detach(), coeffs.detach(), direction.detach())
return x
@staticmethod
def backward(ctx, x_grad, *args):
with torch.enable_grad():
(x_orig, coeffs, new_direction) = ctx.saved_tensors
x_orig.requires_grad = True
num_channels = x_orig.shape[ctx.channel_dim]
x = x_orig.transpose(ctx.channel_dim, -1).reshape(-1, num_channels)
new_direction.requires_grad = False
x = x - x.mean(dim=0)
x_var = (x**2).mean()
x_residual = x - coeffs * new_direction
x_residual_var = (x_residual**2).mean()
# `variance_proportion` is the proportion of the variance accounted for
# by the top eigen-direction. This is to be minimized.
variance_proportion = (x_var - x_residual_var) / (x_var + 1.0e-20)
variance_proportion.backward()
x_orig_grad = x_orig.grad
x_extra_grad = (
x_orig.grad
* ctx.grad_scale
* x_grad.norm()
/ (x_orig_grad.norm() + 1.0e-20)
)
return x_grad + x_extra_grad.detach(), None, None, None, None
class BiasNormFunction(torch.autograd.Function):
# This computes:
# scales = (torch.mean((x - bias) ** 2, keepdim=True)) ** -0.5 * log_scale.exp()
# return (x - bias) * scales
# (after unsqueezing the bias), but it does it in a memory-efficient way so that
# it can just store the returned value (chances are, this will also be needed for
# some other reason, related to the next operation, so we can save memory).
@staticmethod
def forward(
ctx,
x: Tensor,
bias: Tensor,
log_scale: Tensor,
channel_dim: int,
store_output_for_backprop: bool,
) -> Tensor:
assert bias.ndim == 1
if channel_dim < 0:
channel_dim = channel_dim + x.ndim
ctx.store_output_for_backprop = store_output_for_backprop
ctx.channel_dim = channel_dim
for _ in range(channel_dim + 1, x.ndim):
bias = bias.unsqueeze(-1)
scales = (
torch.mean((x - bias) ** 2, dim=channel_dim, keepdim=True) ** -0.5
) * log_scale.exp()
ans = x * scales
ctx.save_for_backward(
ans.detach() if store_output_for_backprop else x,
scales.detach(),
bias.detach(),
log_scale.detach(),
)
return ans
@staticmethod
def backward(ctx, ans_grad: Tensor) -> Tensor:
ans_or_x, scales, bias, log_scale = ctx.saved_tensors
if ctx.store_output_for_backprop:
x = ans_or_x / scales
else:
x = ans_or_x
x = x.detach()
x.requires_grad = True
bias.requires_grad = True
log_scale.requires_grad = True
with torch.enable_grad():
# recompute scales from x, bias and log_scale.
scales = (
torch.mean((x - bias) ** 2, dim=ctx.channel_dim, keepdim=True) ** -0.5
) * log_scale.exp()
ans = x * scales
ans.backward(gradient=ans_grad)
return x.grad, bias.grad.flatten(), log_scale.grad, None, None
class BiasNorm(torch.nn.Module):
"""
This is intended to be a simpler, and hopefully cheaper, replacement for
LayerNorm. The observation this is based on, is that Transformer-type
networks, especially with pre-norm, sometimes seem to set one of the
feature dimensions to a large constant value (e.g. 50), which "defeats"
the LayerNorm because the output magnitude is then not strongly dependent
on the other (useful) features. Presumably the weight and bias of the
LayerNorm are required to allow it to do this.
Instead, we give the BiasNorm a trainable bias that it can use when
computing the scale for normalization. We also give it a (scalar)
trainable scale on the output.
Args:
num_channels: the number of channels, e.g. 512.
channel_dim: the axis/dimension corresponding to the channel,
interprted as an offset from the input's ndim if negative.
shis is NOT the num_channels; it should typically be one of
{-2, -1, 0, 1, 2, 3}.
log_scale: the initial log-scale that we multiply the output by; this
is learnable.
log_scale_min: FloatLike, minimum allowed value of log_scale
log_scale_max: FloatLike, maximum allowed value of log_scale
store_output_for_backprop: only possibly affects memory use; recommend
to set to True if you think the output of this module is more likely
than the input of this module to be required to be stored for the
backprop.
"""
def __init__(
self,
num_channels: int,
channel_dim: int = -1, # CAUTION: see documentation.
log_scale: float = 1.0,
log_scale_min: float = -1.5,
log_scale_max: float = 1.5,
store_output_for_backprop: bool = False,
) -> None:
super(BiasNorm, self).__init__()
self.num_channels = num_channels
self.channel_dim = channel_dim
self.log_scale = nn.Parameter(torch.tensor(log_scale))
self.bias = nn.Parameter(torch.zeros(num_channels))
self.log_scale_min = log_scale_min
self.log_scale_max = log_scale_max
self.store_output_for_backprop = store_output_for_backprop
def forward(self, x: Tensor) -> Tensor:
assert x.shape[self.channel_dim] == self.num_channels
if torch.jit.is_scripting():
channel_dim = self.channel_dim
if channel_dim < 0:
channel_dim += x.ndim
bias = self.bias
for _ in range(channel_dim + 1, x.ndim):
bias = bias.unsqueeze(-1)
scales = (
torch.mean((x - bias) ** 2, dim=channel_dim, keepdim=True) ** -0.5
) * self.log_scale.exp()
return x * scales
log_scale = limit_param_value(
self.log_scale,
min=float(self.log_scale_min),
max=float(self.log_scale_max),
training=self.training,
)
return BiasNormFunction.apply(
x, self.bias, log_scale, self.channel_dim, self.store_output_for_backprop
)
def ScaledLinear(*args, initial_scale: float = 1.0, **kwargs) -> nn.Linear:
"""
Behaves like a constructor of a modified version of nn.Linear
that gives an easy way to set the default initial parameter scale.
Args:
Accepts the standard args and kwargs that nn.Linear accepts
e.g. in_features, out_features, bias=False.
initial_scale: you can override this if you want to increase
or decrease the initial magnitude of the module's output
(affects the initialization of weight_scale and bias_scale).
Another option, if you want to do something like this, is
to re-initialize the parameters.
"""
ans = nn.Linear(*args, **kwargs)
with torch.no_grad():
ans.weight[:] *= initial_scale
if ans.bias is not None:
torch.nn.init.uniform_(ans.bias, -0.1 * initial_scale, 0.1 * initial_scale)
return ans
def ScaledConv1d(*args, initial_scale: float = 1.0, **kwargs) -> nn.Conv1d:
"""
Behaves like a constructor of a modified version of nn.Conv1d
that gives an easy way to set the default initial parameter scale.
Args:
Accepts the standard args and kwargs that nn.Linear accepts
e.g. in_features, out_features, bias=False.
initial_scale: you can override this if you want to increase
or decrease the initial magnitude of the module's output
(affects the initialization of weight_scale and bias_scale).
Another option, if you want to do something like this, is
to re-initialize the parameters.
"""
ans = nn.Conv1d(*args, **kwargs)
with torch.no_grad():
ans.weight[:] *= initial_scale
if ans.bias is not None:
torch.nn.init.uniform_(ans.bias, -0.1 * initial_scale, 0.1 * initial_scale)
return ans
def ScaledConv2d(*args, initial_scale: float = 1.0, **kwargs) -> nn.Conv2d:
"""
Behaves like a constructor of a modified version of nn.Conv2d
that gives an easy way to set the default initial parameter scale.
Args:
Accepts the standard args and kwargs that nn.Linear accepts
e.g. in_features, out_features, bias=False, but:
NO PADDING-RELATED ARGS.
initial_scale: you can override this if you want to increase
or decrease the initial magnitude of the module's output
(affects the initialization of weight_scale and bias_scale).
Another option, if you want to do something like this, is
to re-initialize the parameters.
"""
ans = nn.Conv2d(*args, **kwargs)
with torch.no_grad():
ans.weight[:] *= initial_scale
if ans.bias is not None:
torch.nn.init.uniform_(ans.bias, -0.1 * initial_scale, 0.1 * initial_scale)
return ans
class ChunkCausalDepthwiseConv1d(torch.nn.Module):
"""
Behaves like a depthwise 1d convolution, except that it is causal in
a chunkwise way, as if we had a block-triangular attention mask.
The chunk size is provided at test time (it should probably be
kept in sync with the attention mask).
This has a little more than twice the parameters of a conventional
depthwise conv1d module: we implement it by having one
depthwise convolution, of half the width, that is causal (via
right-padding); and one depthwise convolution that is applied only
within chunks, that we multiply by a scaling factor which depends
on the position within the chunk.
Args:
Accepts the standard args and kwargs that nn.Linear accepts
e.g. in_features, out_features, bias=False.
initial_scale: you can override this if you want to increase
or decrease the initial magnitude of the module's output
(affects the initialization of weight_scale and bias_scale).
Another option, if you want to do something like this, is
to re-initialize the parameters.
"""
def __init__(
self,
channels: int,
kernel_size: int,
initial_scale: float = 1.0,
bias: bool = True,
):
super().__init__()
assert kernel_size % 2 == 1
half_kernel_size = (kernel_size + 1) // 2
# will pad manually, on one side.
self.causal_conv = nn.Conv1d(
in_channels=channels,
out_channels=channels,
groups=channels,
kernel_size=half_kernel_size,
padding=0,
bias=True,
)
self.chunkwise_conv = nn.Conv1d(
in_channels=channels,
out_channels=channels,
groups=channels,
kernel_size=kernel_size,
padding=kernel_size // 2,
bias=bias,
)
# first row is correction factors added to the scale near the left edge of the chunk,
# second row is correction factors added to the scale near the right edge of the chunk,
# both of these are added to a default scale of 1.0.
self.chunkwise_conv_scale = nn.Parameter(torch.zeros(2, channels, kernel_size))
self.kernel_size = kernel_size
with torch.no_grad():
self.causal_conv.weight[:] *= initial_scale
self.chunkwise_conv.weight[:] *= initial_scale
if bias:
torch.nn.init.uniform_(
self.causal_conv.bias, -0.1 * initial_scale, 0.1 * initial_scale
)
def forward(self, x: Tensor, chunk_size: int = -1) -> Tensor:
"""
Forward function. Args:
x: a Tensor of shape (batch_size, channels, seq_len)
chunk_size: the chunk size, in frames; does not have to divide seq_len exactly.
"""
(batch_size, num_channels, seq_len) = x.shape
half_kernel_size = self.kernel_size + 1 // 2
# left_pad is half_kernel_size - 1 where half_kernel_size is the size used
# in the causal conv. It's the amount by which we must pad on the left,
# to make the convolution causal.
left_pad = self.kernel_size // 2
if chunk_size < 0 or chunk_size > seq_len:
chunk_size = seq_len
right_pad = -seq_len % chunk_size
x = torch.nn.functional.pad(x, (left_pad, right_pad))
x_causal = self.causal_conv(x[..., : left_pad + seq_len])
assert x_causal.shape == (batch_size, num_channels, seq_len)
x_chunk = x[..., left_pad:]
num_chunks = x_chunk.shape[2] // chunk_size
x_chunk = x_chunk.reshape(batch_size, num_channels, num_chunks, chunk_size)
x_chunk = x_chunk.permute(0, 2, 1, 3).reshape(
batch_size * num_chunks, num_channels, chunk_size
)
x_chunk = self.chunkwise_conv(x_chunk) # does not change shape
chunk_scale = self._get_chunk_scale(chunk_size)
x_chunk = x_chunk * chunk_scale
x_chunk = x_chunk.reshape(
batch_size, num_chunks, num_channels, chunk_size
).permute(0, 2, 1, 3)
x_chunk = x_chunk.reshape(batch_size, num_channels, num_chunks * chunk_size)[
..., :seq_len
]
return x_chunk + x_causal
def _get_chunk_scale(self, chunk_size: int):
"""Returns tensor of shape (num_channels, chunk_size) that will be used to
scale the output of self.chunkwise_conv."""
left_edge = self.chunkwise_conv_scale[0]
right_edge = self.chunkwise_conv_scale[1]
if chunk_size < self.kernel_size:
left_edge = left_edge[:, :chunk_size]
right_edge = right_edge[:, -chunk_size:]
else:
t = chunk_size - self.kernel_size
channels = left_edge.shape[0]
pad = torch.zeros(
channels, t, device=left_edge.device, dtype=left_edge.dtype
)
left_edge = torch.cat((left_edge, pad), dim=-1)
right_edge = torch.cat((pad, right_edge), dim=-1)
return 1.0 + (left_edge + right_edge)
class BalancerFunction(torch.autograd.Function):
@staticmethod
def forward(
ctx,
x: Tensor,
min_mean: float,
max_mean: float,
min_rms: float,
max_rms: float,
grad_scale: float,
channel_dim: int,
) -> Tensor:
if channel_dim < 0:
channel_dim += x.ndim
ctx.channel_dim = channel_dim
ctx.save_for_backward(x)
ctx.config = (min_mean, max_mean, min_rms, max_rms, grad_scale, channel_dim)
return x
@staticmethod
def backward(ctx, x_grad: Tensor) -> Tuple[Tensor, None, None, None, None, None]:
(x,) = ctx.saved_tensors
(min_mean, max_mean, min_rms, max_rms, grad_scale, channel_dim) = ctx.config
try:
with torch.enable_grad():
with torch.cuda.amp.autocast(enabled=False):
x = x.to(torch.float32)
x = x.detach()
x.requires_grad = True
mean_dims = [i for i in range(x.ndim) if i != channel_dim]
uncentered_var = (x**2).mean(dim=mean_dims, keepdim=True)
mean = x.mean(dim=mean_dims, keepdim=True)
stddev = (uncentered_var - (mean * mean)).clamp(min=1.0e-20).sqrt()
rms = uncentered_var.clamp(min=1.0e-20).sqrt()
m = mean / stddev
# part of loss that relates to mean / stddev
m_loss = (m - m.clamp(min=min_mean, max=max_mean)).abs()
# put a much larger scale on the RMS-max-limit loss, so that if both it and the
# m_loss are violated we fix the RMS loss first.
rms_clamped = rms.clamp(min=min_rms, max=max_rms)
r_loss = (rms_clamped / rms).log().abs()
loss = m_loss + r_loss
loss.backward(gradient=torch.ones_like(loss))
loss_grad = x.grad
loss_grad_rms = (
(loss_grad**2)
.mean(dim=mean_dims, keepdim=True)
.sqrt()
.clamp(min=1.0e-20)
)
loss_grad = loss_grad * (grad_scale / loss_grad_rms)
x_grad_float = x_grad.to(torch.float32)
# scale each element of loss_grad by the absolute value of the corresponding
# element of x_grad, which we view as a noisy estimate of its magnitude for that
# (frame and dimension). later we can consider factored versions.
x_grad_mod = x_grad_float + (x_grad_float.abs() * loss_grad)
x_grad = x_grad_mod.to(x_grad.dtype)
except Exception as e:
logging.info(
f"Caught exception in Balancer backward: {e}, size={list(x_grad.shape)}, will continue."
)
return x_grad, None, None, None, None, None, None
class Balancer(torch.nn.Module):
"""
Modifies the backpropped derivatives of a function to try to encourage, for
each channel, that it is positive at least a proportion `threshold` of the
time. It does this by multiplying negative derivative values by up to
(1+max_factor), and positive derivative values by up to (1-max_factor),
interpolated from 1 at the threshold to those extremal values when none
of the inputs are positive.
Args:
num_channels: the number of channels
channel_dim: the dimension/axis corresponding to the channel, e.g.
-1, 0, 1, 2; will be interpreted as an offset from x.ndim if negative.
min_positive: the minimum, per channel, of the proportion of the time
that (x > 0), below which we start to modify the derivatives.
max_positive: the maximum, per channel, of the proportion of the time
that (x > 0), above which we start to modify the derivatives.
scale_gain_factor: determines the 'gain' with which we increase the
change in gradient once the constraints on min_abs and max_abs
are violated.
min_abs: the minimum average-absolute-value difference from the mean
value per channel, which we allow, before we start to modify
the derivatives to prevent this.
max_abs: the maximum average-absolute-value difference from the mean
value per channel, which we allow, before we start to modify
the derivatives to prevent this.
prob: determines the minimum probability with which we modify the
gradients for the {min,max}_positive and {min,max}_abs constraints,
on each forward(). This is done randomly to prevent all layers
from doing it at the same time.
"""
def __init__(
self,
num_channels: int,
channel_dim: int,
min_positive: FloatLike = 0.05,
max_positive: FloatLike = 0.95,
min_abs: FloatLike = 0.2,
max_abs: FloatLike = 100.0,
grad_scale: FloatLike = 0.04,
prob: Optional[FloatLike] = None,
):
super().__init__()
if prob is None:
prob = ScheduledFloat((0.0, 0.5), (8000.0, 0.125), default=0.4)
self.prob = prob
# 5% of the time we will return and do nothing because memory usage is
# too high.
self.mem_cutoff = CutoffEstimator(0.05)
# actually self.num_channels is no longer needed except for an assertion.
self.num_channels = num_channels
self.channel_dim = channel_dim
self.min_positive = min_positive
self.max_positive = max_positive
self.min_abs = min_abs
self.max_abs = max_abs
self.grad_scale = grad_scale
def forward(self, x: Tensor) -> Tensor:
if (
torch.jit.is_scripting()
or not x.requires_grad
or (x.is_cuda and self.mem_cutoff(torch.cuda.memory_allocated()))
):
return _no_op(x)
prob = float(self.prob)
if random.random() < prob:
# The following inner-functions convert from the way we historically specified
# these limitations, as limits on the absolute value and the proportion of positive
# values, to limits on the RMS value and the (mean / stddev).
def _abs_to_rms(x):
# for normally distributed data, if the expected absolute value is x, the
# expected rms value will be sqrt(pi/2) * x.
return 1.25331413732 * x
def _proportion_positive_to_mean(x):
def _atanh(x):
eps = 1.0e-10
# eps is to prevent crashes if x is exactly 0 or 1.
# we'll just end up returning a fairly large value.
return (math.log(1 + x + eps) - math.log(1 - x + eps)) / 2.0
def _approx_inverse_erf(x):
# 1 / (sqrt(pi) * ln(2)),
# see https://math.stackexchange.com/questions/321569/approximating-the-error-function-erf-by-analytical-functions
# this approximation is extremely crude and gets progressively worse for
# x very close to -1 or +1, but we mostly care about the "middle" region
# e.g. _approx_inverse_erf(0.05) = 0.0407316414078772,
# and math.erf(0.0407316414078772) = 0.045935330944660666,
# which is pretty close to 0.05.
return 0.8139535143 * _atanh(x)
# first convert x from the range 0..1 to the range -1..1 which the error
# function returns
x = -1 + (2 * x)
return _approx_inverse_erf(x)
min_mean = _proportion_positive_to_mean(float(self.min_positive))
max_mean = _proportion_positive_to_mean(float(self.max_positive))
min_rms = _abs_to_rms(float(self.min_abs))
max_rms = _abs_to_rms(float(self.max_abs))
grad_scale = float(self.grad_scale)
assert x.shape[self.channel_dim] == self.num_channels
return BalancerFunction.apply(
x, min_mean, max_mean, min_rms, max_rms, grad_scale, self.channel_dim
)
else:
return _no_op(x)
def penalize_abs_values_gt(
x: Tensor, limit: float, penalty: float, name: str = None
) -> Tensor:
"""
Returns x unmodified, but in backprop will put a penalty for the excess of
the absolute values of elements of x over the limit "limit". E.g. if
limit == 10.0, then if x has any values over 10 it will get a penalty.
Caution: the value of this penalty will be affected by grad scaling used
in automatic mixed precision training. For this reasons we use this,
it shouldn't really matter, or may even be helpful; we just use this
to disallow really implausible values of scores to be given to softmax.
The name is for randomly printed debug info.
"""
x_sign = x.sign()
over_limit = (x.abs() - limit) > 0
# The following is a memory efficient way to penalize the absolute values of
# x that's over the limit. (The memory efficiency comes when you think
# about which items torch needs to cache for the autograd, and which ones it
# can throw away). The numerical value of aux_loss as computed here will
# actually be larger than it should be, by limit * over_limit.sum(), but it
# has the same derivative as the real aux_loss which is penalty * (x.abs() -
# limit).relu().
aux_loss = penalty * ((x_sign * over_limit).to(torch.int8) * x)
# note: we don't do sum() here on aux)_loss, but it's as if we had done
# sum() due to how with_loss() works.
x = with_loss(x, aux_loss, name)
# you must use x for something, or this will be ineffective.
return x
def _diag(x: Tensor): # like .diag(), but works for tensors with 3 dims.
if x.ndim == 2:
return x.diag()
else:
(batch, dim, dim) = x.shape
x = x.reshape(batch, dim * dim)
x = x[:, :: dim + 1]
assert x.shape == (batch, dim)
return x
def _whitening_metric(x: Tensor, num_groups: int):
"""
Computes the "whitening metric", a value which will be 1.0 if all the eigenvalues of
of the centered feature covariance are the same within each group's covariance matrix
and also between groups.
Args:
x: a Tensor of shape (*, num_channels)
num_groups: the number of groups of channels, a number >=1 that divides num_channels
Returns:
Returns a scalar Tensor that will be 1.0 if the data is "perfectly white" and
greater than 1.0 otherwise.
"""
assert x.dtype != torch.float16
x = x.reshape(-1, x.shape[-1])
(num_frames, num_channels) = x.shape
assert num_channels % num_groups == 0
channels_per_group = num_channels // num_groups
x = x.reshape(num_frames, num_groups, channels_per_group).transpose(0, 1)
# x now has shape (num_groups, num_frames, channels_per_group)
# subtract the mean so we use the centered, not uncentered, covariance.
# My experience has been that when we "mess with the gradients" like this,
# it's better not do anything that tries to move the mean around, because
# that can easily cause instability.
x = x - x.mean(dim=1, keepdim=True)
# x_covar: (num_groups, channels_per_group, channels_per_group)
x_covar = torch.matmul(x.transpose(1, 2), x)
x_covar_mean_diag = _diag(x_covar).mean()
# the following expression is what we'd get if we took the matrix product
# of each covariance and measured the mean of its trace, i.e.
# the same as _diag(torch.matmul(x_covar, x_covar)).mean().
x_covarsq_mean_diag = (x_covar**2).sum() / (num_groups * channels_per_group)
# this metric will be >= 1.0; the larger it is, the less 'white' the data was.
metric = x_covarsq_mean_diag / (x_covar_mean_diag**2 + 1.0e-20)
return metric
class WhiteningPenaltyFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, x: Tensor, module: nn.Module) -> Tensor:
ctx.save_for_backward(x)
ctx.module = module
return x
@staticmethod
def backward(ctx, x_grad: Tensor):
(x_orig,) = ctx.saved_tensors
w = ctx.module
try:
with torch.enable_grad():
with torch.cuda.amp.autocast(enabled=False):
x_detached = x_orig.to(torch.float32).detach()
x_detached.requires_grad = True
metric = _whitening_metric(x_detached, w.num_groups)
if random.random() < 0.005 or __name__ == "__main__":
logging.info(
f"Whitening: name={w.name}, num_groups={w.num_groups}, num_channels={x_orig.shape[-1]}, "
f"metric={metric.item():.2f} vs. limit={float(w.whitening_limit)}"
)
if metric < float(w.whitening_limit):
w.prob = w.min_prob
return x_grad, None
else:
w.prob = w.max_prob
metric.backward()
penalty_grad = x_detached.grad
scale = w.grad_scale * (
x_grad.to(torch.float32).norm()
/ (penalty_grad.norm() + 1.0e-20)
)
penalty_grad = penalty_grad * scale
return x_grad + penalty_grad.to(x_grad.dtype), None
except Exception as e:
logging.info(
f"Caught exception in Whiten backward: {e}, size={list(x_grad.shape)}, will continue."
)
return x_grad, None
class Whiten(nn.Module):
def __init__(
self,
num_groups: int,
whitening_limit: FloatLike,
prob: Union[float, Tuple[float, float]],
grad_scale: FloatLike,
):
"""
Args:
num_groups: the number of groups to divide the channel dim into before
whitening. We will attempt to make the feature covariance
within each group, after mean subtraction, as "white" as possible,
while having the same trace across all groups.
whitening_limit: a value greater than 1.0, that dictates how much
freedom we have to violate the constraints. 1.0 would mean perfectly
white, with exactly the same trace across groups; larger values
give more freedom. E.g. 2.0.
prob: the probability with which we apply the gradient modification
(also affects the grad scale). May be supplied as a float,
or as a pair (min_prob, max_prob)
grad_scale: determines the scale on the gradient term from this object,
relative to the rest of the gradient on the attention weights.
E.g. 0.02 (you may want to use smaller values than this if prob is large)
"""