-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvLSTM.py
581 lines (496 loc) · 20.5 KB
/
vLSTM.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
import torch
import torch.nn as nn
import einops
from enum import Enum
import math
import torch.nn.functional as F
def interpolate_sincos(embed, seqlens, mode="bicubic"):
assert embed.ndim - 2 == len(seqlens)
embed = F.interpolate(
einops.rearrange(embed, "1 ... dim -> 1 dim ..."),
size=seqlens,
mode=mode,
)
embed = einops.rearrange(embed, "1 dim ... -> 1 ... dim")
return embed
class DropPath(nn.Sequential):
"""
Efficiently drop paths (Stochastic Depth) per sample such that dropped samples are not processed.
This is a subclass of nn.Sequential and can be used either as standalone Module or like nn.Sequential.
Examples::
>>> # use as nn.Sequential module
>>> sequential_droppath = DropPath(nn.Linear(4, 4), drop_prob=0.2)
>>> y = sequential_droppath(torch.randn(10, 4))
>>> # use as standalone module
>>> standalone_layer = nn.Linear(4, 4)
>>> standalone_droppath = DropPath(drop_prob=0.2)
>>> y = standalone_droppath(torch.randn(10, 4), standalone_layer)
"""
def __init__(self, *args, drop_prob: float = 0., scale_by_keep: bool = True, stochastic_drop_prob: bool = False):
super().__init__(*args)
assert 0. <= drop_prob < 1.
self._drop_prob = drop_prob
self.scale_by_keep = scale_by_keep
self.stochastic_drop_prob = stochastic_drop_prob
@property
def drop_prob(self):
return self._drop_prob
@drop_prob.setter
def drop_prob(self, value):
assert 0. <= value < 1.
self._drop_prob = value
@property
def keep_prob(self):
return 1. - self.drop_prob
def forward(self, x, residual_path=None, residual_path_kwargs=None):
assert (len(self) == 0) ^ (residual_path is None)
residual_path_kwargs = residual_path_kwargs or {}
if self.drop_prob == 0. or not self.training:
if residual_path is None:
return x + super().forward(x, **residual_path_kwargs)
else:
return x + residual_path(x, **residual_path_kwargs)
# generate indices to keep (propagated through transform path)
bs = len(x)
if self.stochastic_drop_prob:
perm = torch.empty(bs, device=x.device).bernoulli_(self.keep_prob).nonzero().squeeze(1)
scale = 1 / self.keep_prob
else:
keep_count = max(int(bs * self.keep_prob), 1)
scale = bs / keep_count
perm = torch.randperm(bs, device=x.device)[:keep_count]
# propagate
if self.scale_by_keep:
alpha = scale
else:
alpha = 1.
# reduce kwargs (e.g. used for DiT block where scale/shift/gate is passed and also has to be reduced)
residual_path_kwargs = {
key: value[perm] if torch.is_tensor(value) else value
for key, value in residual_path_kwargs.items()
}
if residual_path is None:
residual = super().forward(x[perm], **residual_path_kwargs)
else:
residual = residual_path(x[perm], **residual_path_kwargs)
return torch.index_add(
x.flatten(start_dim=1),
dim=0,
index=perm,
source=residual.to(x.dtype).flatten(start_dim=1),
alpha=alpha,
).view_as(x)
def extra_repr(self):
return f'drop_prob={round(self.drop_prob, 3):0.3f}'
class SequenceTraversal(Enum):
ROWWISE_FROM_TOP_LEFT = "rowwise_from_top_left"
ROWWISE_FROM_BOT_RIGHT = "rowwise_from_bot_right"
def bias_linspace_init_(param: torch.Tensor, start: float = 3.4, end: float = 6.0) -> torch.Tensor:
"""Linearly spaced bias init across dimensions."""
assert param.dim() == 1, f"param must be 1-dimensional (typically a bias), got {param.dim()}"
n_dims = param.shape[0]
init_vals = torch.linspace(start, end, n_dims)
with torch.no_grad():
param.copy_(init_vals)
return param
def small_init_(param: torch.Tensor, dim: int) -> torch.Tensor:
"""
Fills the input Tensor with values according to the method described in Transformers without Tears: Improving
the Normalization of Self-Attention - Nguyen, T. & Salazar, J. (2019), using a normal distribution.
Adopted from https://github.com/EleutherAI/gpt-neox/blob/main/megatron/model/init_functions.py.
"""
std = math.sqrt(2 / (5 * dim))
torch.nn.init.normal_(param, mean=0.0, std=std)
return param
def wang_init_(param: torch.Tensor, dim: int, num_blocks: int):
""" Adopted from https://github.com/EleutherAI/gpt-neox/blob/main/megatron/model/init_functions.py. """
std = 2 / num_blocks / math.sqrt(dim)
torch.nn.init.normal_(param, mean=0.0, std=std)
return param
def parallel_stabilized_simple(
queries: torch.Tensor,
keys: torch.Tensor,
values: torch.Tensor,
igate_preact: torch.Tensor,
fgate_preact: torch.Tensor,
lower_triangular_matrix: torch.Tensor = None,
stabilize_rowwise: bool = True,
eps: float = 1e-6,
) -> torch.Tensor:
"""
This is the mLSTM cell in parallel form.
This version is stabilized. We control the range of exp() arguments by
ensuring that they are always smaller than 0.0 by subtracting the maximum.
Args:
:param queries: (torch.Tensor) (B, NH, S, DH)
:param keys: (torch.Tensor) (B, NH, S, DH)
:param values: (torch.Tensor) (B, NH, S, DH)
:param igate_preact: (torch.Tensor) (B, NH, S, 1)
:param fgate_preact: (torch.Tensor) (B, NH, S, 1)
:param lower_triangular_matrix: (torch.Tensor) (S,S). Defaults to None.
:param stabilize_rowwise: (bool) Wether to stabilize the combination matrix C rowwise (take maximum per row).
Alternative: Subtract the maximum over all rows. Defaults to True.
:param eps: (float) small constant to avoid division by 0. Defaults to 1e-6.
Returns:
torch.Tensor: (B, NH, S, DH), h_tilde_state
"""
B, NH, S, DH = queries.shape
_dtype, _device = queries.dtype, queries.device
# forget gate matrix
log_fgates = torch.nn.functional.logsigmoid(fgate_preact) # (B, NH, S, 1)
if lower_triangular_matrix is None or S < lower_triangular_matrix.size(-1):
ltr = torch.tril(torch.ones((S, S), dtype=torch.bool, device=_device))
else:
ltr = lower_triangular_matrix
assert ltr.dtype == torch.bool, f"lower_triangular_matrix must be of dtype bool, got {ltr.dtype}"
log_fgates_cumsum = torch.cat(
[
torch.zeros((B, NH, 1, 1), dtype=_dtype, device=_device),
torch.cumsum(log_fgates, dim=-2),
],
dim=-2,
) # (B, NH, S+1, 1)
# for each batch/head this is a matrix of shape (S+1, S+1) containing the cumsum of the log forget gate values
# in the second dimension (colum dimension). Each row has the same is a copy of the first row.
# First entry of each row is zero.
rep_log_fgates_cumsum = log_fgates_cumsum.repeat(1, 1, 1, S + 1) # (B, NH, S+1, S+1)
# Now in each row cut off / subtract the forgetgate values of the later timesteps
# where col j > row i
_log_fg_matrix = rep_log_fgates_cumsum - rep_log_fgates_cumsum.transpose(-2, -1) # (B, NH, S+1, S+1)
# Causal masking & selection of the correct submatrix, such that forgetgate at timestep t is not applied
# to the input at timestep t
log_fg_matrix = torch.where(ltr, _log_fg_matrix[:, :, 1:, 1:], -float("inf")) # (B, NH, S, S)
# gate decay matrix D (combination of forget gate and input gate)
log_D_matrix = log_fg_matrix + igate_preact.transpose(-2, -1) # (B, NH, S, S)
# D matrix stabilization
if stabilize_rowwise:
max_log_D, _ = torch.max(log_D_matrix, dim=-1, keepdim=True) # (B, NH, S, 1)
else:
max_log_D = torch.max(log_D_matrix.view(B, NH, -1), dim=-1, keepdim=True)[0].unsqueeze(-1)
# (B, NH, 1, 1)
log_D_matrix_stabilized = log_D_matrix - max_log_D # (B, NH, S, S)
D_matrix = torch.exp(log_D_matrix_stabilized) # (B, NH, S, S)
keys_scaled = keys / math.sqrt(DH)
# combination matrix C
qk_matrix = queries @ keys_scaled.transpose(-2, -1) # (B, NH, S, S)
C_matrix = qk_matrix * D_matrix # (B, NH, S, S)
normalizer = torch.maximum(C_matrix.sum(dim=-1, keepdim=True).abs(), torch.exp(-max_log_D)) # (B, NH, S, 1)
# (B, NH, S, S)
C_matrix_normalized = C_matrix / (normalizer + eps)
# retrieved values
h_tilde_state = C_matrix_normalized @ values # (B, NH, S, DH)
return h_tilde_state
class LinearHeadwiseExpand(nn.Module):
"""
This is a structured projection layer that projects the input to a higher dimension.
It only allows integer up-projection factors, i.e. the output dimension is a multiple of the input dimension.
"""
def __init__(self, dim, num_heads, bias=False):
super().__init__()
assert dim % num_heads == 0
self.dim = dim
self.num_heads = num_heads
dim_per_head = dim // num_heads
self.weight = nn.Parameter(torch.empty(num_heads, dim_per_head, dim_per_head))
if bias:
self.bias = nn.Parameter(torch.empty(dim))
else:
self.bias = None
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.weight.data, mean=0.0, std=math.sqrt(2 / 5 / self.weight.shape[-1]))
if self.bias is not None:
nn.init.zeros_(self.bias.data)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = einops.rearrange(x, "... (nh d) -> ... nh d", nh=self.num_heads)
x = einops.einsum(
x,
self.weight,
"... nh d, nh out_d d -> ... nh out_d",
)
x = einops.rearrange(x, "... nh out_d -> ... (nh out_d)")
if self.bias is not None:
x = x + self.bias
return x
def extra_repr(self):
return (
f"dim={self.dim}, "
f"num_heads={self.num_heads}, "
f"bias={self.bias is not None}, "
)
class CausalConv1d(nn.Module):
"""
Implements causal depthwise convolution of a time series tensor.
Input: Tensor of shape (B,T,F), i.e. (batch, time, feature)
Output: Tensor of shape (B,T,F)
Args:
feature_dim: number of features in the input tensor
kernel_size: size of the kernel for the depthwise convolution
causal_conv_bias: whether to use bias in the depthwise convolution
channel_mixing: whether to use channel mixing (i.e. groups=1) or not (i.e. groups=feature_dim)
If True, it mixes the convolved features across channels.
If False, all the features are convolved independently.
"""
def __init__(self, dim, kernel_size=4, bias=True):
super().__init__()
self.dim = dim
self.kernel_size = kernel_size
self.bias = bias
# padding of this size assures temporal causality.
self.pad = kernel_size - 1
self.conv = nn.Conv1d(
in_channels=dim,
out_channels=dim,
kernel_size=kernel_size,
padding=self.pad,
groups=dim,
bias=bias,
)
self.reset_parameters()
def reset_parameters(self):
self.conv.reset_parameters()
def forward(self, x: torch.Tensor) -> torch.Tensor:
# conv requires dim first
x = einops.rearrange(x, "b l d -> b d l")
# causal conv1d
x = self.conv(x)
x = x[:, :, :-self.pad]
# back to dim last
x = einops.rearrange(x, "b d l -> b l d")
return x
class LayerNorm(nn.Module):
""" LayerNorm but with an optional bias. PyTorch doesn't support simply bias=False. """
def __init__(
self,
ndim: int = -1,
weight: bool = True,
bias: bool = False,
eps: float = 1e-5,
residual_weight: bool = True,
):
super().__init__()
self.weight = nn.Parameter(torch.zeros(ndim)) if weight else None
self.bias = nn.Parameter(torch.zeros(ndim)) if bias else None
self.eps = eps
self.residual_weight = residual_weight
self.ndim = ndim
self.reset_parameters()
@property
def weight_proxy(self) -> torch.Tensor:
if self.weight is None:
return None
if self.residual_weight:
return 1.0 + self.weight
else:
return self.weight
def forward(self, x: torch.Tensor) -> torch.Tensor:
return F.layer_norm(
x,
normalized_shape=(self.ndim,),
weight=self.weight_proxy,
bias=self.bias,
eps=self.eps,
)
def reset_parameters(self):
if self.weight_proxy is not None:
if self.residual_weight:
nn.init.zeros_(self.weight)
else:
nn.init.ones_(self.weight)
if self.bias is not None:
nn.init.zeros_(self.bias)
class MultiHeadLayerNorm(LayerNorm):
def forward(self, x: torch.Tensor) -> torch.Tensor:
assert x.ndim == 4, "Input must be 4D tensor (B, NH, S, DH)"
B, NH, S, DH = x.shape
gn_in_1 = x.transpose(1, 2) # (B, S, NH, DH)
gn_in_2 = gn_in_1.reshape(B * S, NH * DH) # (B * S, NH * DH)
out = F.group_norm(
gn_in_2,
num_groups=NH,
weight=self.weight_proxy,
bias=self.bias,
eps=self.eps,
) # .to(x.dtype)
# (B * S), (NH * DH) -> (B, S, NH, DH) -> (B, NH, S, DH)
out = out.view(B, S, NH, DH).transpose(1, 2)
return out
class MatrixLSTMCell(nn.Module):
def __init__(self, dim, num_heads):
super().__init__()
self.dim = dim
self.num_heads = num_heads
self.igate = nn.Linear(3 * dim, num_heads)
self.fgate = nn.Linear(3 * dim, num_heads)
self.outnorm = MultiHeadLayerNorm(ndim=dim, weight=True, bias=False)
self.causal_mask_cache = {}
self.reset_parameters()
def forward(self, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor) -> torch.Tensor:
B, S, _ = q.shape # (B, S, H)
if_gate_input = torch.cat([q, k, v], dim=-1)
q = q.view(B, S, self.num_heads, -1) # (B, S, NH, DH)
k = k.view(B, S, self.num_heads, -1) # (B, S, NH, DH)
v = v.view(B, S, self.num_heads, -1) # (B, S, NH, DH)
q = q.transpose(1, 2) # (B, NH, S, DH)
k = k.transpose(1, 2) # (B, NH, S, DH)
v = v.transpose(1, 2) # (B, NH, S, DH)
# compute input and forget gate pre-activations
igate_preact = self.igate(if_gate_input) # (B, S, NH)
igate_preact = igate_preact.transpose(-1, -2).unsqueeze(-1) # (B, NH, S, 1)
fgate_preact = self.fgate(if_gate_input) # (B, S, NH)
fgate_preact = fgate_preact.transpose(-1, -2).unsqueeze(-1) # (B, NH, S, 1)#
# cache causal mask to avoid memory allocation in every iteration
if S in self.causal_mask_cache:
causal_mask = self.causal_mask_cache[(S, str(q.device))]
else:
causal_mask = torch.tril(torch.ones(S, S, dtype=torch.bool, device=q.device))
self.causal_mask_cache[(S, str(q.device))] = causal_mask
h_state = parallel_stabilized_simple(
queries=q,
keys=k,
values=v,
igate_preact=igate_preact,
fgate_preact=fgate_preact,
lower_triangular_matrix=causal_mask,
) # (B, NH, S, DH)
h_state_norm = self.outnorm(h_state) # (B, NH, S, DH)
h_state_norm = h_state_norm.transpose(1, 2).reshape(B, S, -1) # (B, NH, S, DH) -> (B, S, NH, DH) -> (B, S, H)
return h_state_norm
def reset_parameters(self):
self.outnorm.reset_parameters()
# forget gate initialization
torch.nn.init.zeros_(self.fgate.weight)
bias_linspace_init_(self.fgate.bias, start=3.0, end=6.0)
# input gate initialization
torch.nn.init.zeros_(self.igate.weight)
torch.nn.init.normal_(self.igate.bias, mean=0.0, std=0.1)
class ViLLayer(nn.Module):
def __init__(
self,
dim,
direction,
expansion=2,
qkv_block_size=4,
proj_bias=False,
conv_bias=True,
kernel_size=4,
):
super().__init__()
assert dim % qkv_block_size == 0
self.dim = dim
self.direction = direction
self.expansion = expansion
self.qkv_block_size = qkv_block_size
self.proj_bias = proj_bias
self.conv_bias = conv_bias
self.kernel_size = kernel_size
inner_dim = expansion * dim
num_heads = inner_dim // qkv_block_size
self.proj_up = nn.Linear(
in_features=dim,
out_features=2 * inner_dim,
bias=proj_bias,
)
self.q_proj = LinearHeadwiseExpand(
dim=inner_dim,
num_heads=num_heads,
bias=proj_bias,
)
self.k_proj = LinearHeadwiseExpand(
dim=inner_dim,
num_heads=num_heads,
bias=proj_bias,
)
self.v_proj = LinearHeadwiseExpand(
dim=inner_dim,
num_heads=num_heads,
bias=proj_bias,
)
self.conv1d = CausalConv1d(
dim=inner_dim,
kernel_size=kernel_size,
bias=conv_bias,
)
self.mlstm_cell = MatrixLSTMCell(
dim=inner_dim,
num_heads=qkv_block_size,
)
self.learnable_skip = nn.Parameter(torch.ones(inner_dim))
self.proj_down = nn.Linear(
in_features=inner_dim,
out_features=dim,
bias=proj_bias,
)
self.reset_parameters()
def forward(self, x: torch.Tensor) -> torch.Tensor:
B, S, _ = x.shape
# alternate direction in successive layers
if self.direction == SequenceTraversal.ROWWISE_FROM_TOP_LEFT:
pass
elif self.direction == SequenceTraversal.ROWWISE_FROM_BOT_RIGHT:
x = x.flip(dims=[1])
else:
raise NotImplementedError
# up-projection
x_inner = self.proj_up(x)
x_mlstm, z = torch.chunk(x_inner, chunks=2, dim=-1)
# mlstm branch
x_mlstm_conv = self.conv1d(x_mlstm)
x_mlstm_conv_act = F.silu(x_mlstm_conv)
q = self.q_proj(x_mlstm_conv_act)
k = self.k_proj(x_mlstm_conv_act)
v = self.v_proj(x_mlstm)
h_tilde_state = self.mlstm_cell(q=q, k=k, v=v)
h_tilde_state_skip = h_tilde_state + (self.learnable_skip * x_mlstm_conv_act)
# output / z branch
h_state = h_tilde_state_skip * F.silu(z)
# down-projection
x = self.proj_down(h_state)
# reverse alternating flip
if self.direction == SequenceTraversal.ROWWISE_FROM_TOP_LEFT:
pass
elif self.direction == SequenceTraversal.ROWWISE_FROM_BOT_RIGHT:
x = x.flip(dims=[1])
else:
raise NotImplementedError
return x
def reset_parameters(self):
# init inproj
small_init_(self.proj_up.weight, dim=self.dim)
if self.proj_up.bias is not None:
nn.init.zeros_(self.proj_up.bias)
# init outproj (original mLSTM uses num_blocks=1)
wang_init_(self.proj_down.weight, dim=self.dim, num_blocks=1)
if self.proj_down.bias is not None:
nn.init.zeros_(self.proj_down.bias)
nn.init.ones_(self.learnable_skip)
def _init_qkv_proj(qkv_proj: LinearHeadwiseExpand):
# use the embedding dim instead of the inner embedding dim
small_init_(qkv_proj.weight, dim=self.dim)
if qkv_proj.bias is not None:
nn.init.zeros_(qkv_proj.bias)
_init_qkv_proj(self.q_proj)
_init_qkv_proj(self.k_proj)
_init_qkv_proj(self.v_proj)
self.mlstm_cell.reset_parameters()
class ViLBlock(nn.Module):
def __init__(self, dim, direction, drop_path=0.1, norm_bias=False):
super().__init__()
self.dim = dim
self.direction = direction
self.drop_path = drop_path
self.norm_bias = norm_bias
self.drop_path = DropPath(drop_prob=drop_path)
self.norm = LayerNorm(ndim=dim, weight=True, bias=norm_bias)
self.layer = ViLLayer(dim=dim, direction=direction)
self.reset_parameters()
def _forward_path(self, x):
x = self.norm(x)
x = self.layer(x)
return x
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.drop_path(x, self._forward_path)
return x
def reset_parameters(self):
self.layer.reset_parameters()
self.norm.reset_parameters()