-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtransformer_flow.py
More file actions
1356 lines (1149 loc) · 57.9 KB
/
transformer_flow.py
File metadata and controls
1356 lines (1149 loc) · 57.9 KB
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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2025 Apple Inc. All Rights Reserved.
#
import copy
import tqdm
import numpy as np
import torch
import torch.nn.functional as F
from typing import List, Tuple
from misc.pe import VisionRotaryEmbeddingFast, apply_rope, get_positions
from misc import print
from functools import partial
from einops import rearrange, repeat
from torch.utils.checkpoint import checkpoint
INV_SOFTPLUS_1 = 0.541324854612918
def modulate(x, shift, scale):
if shift is None:
return x * (1 + scale)
return x * (1 + scale) + shift
def stable_neg_log_softplus(x):
return torch.where(
x > 20, # softplus(x) ≈ x → log ≈ log(x)
-x.log(), # so -log(softplus(x)) ≈ -log(x)
-F.softplus(x).log()
)
class KVCache:
def __init__(self):
self._is_empty = True
self.prefix_cache = None
self.meta_data = {}
def initialize(self, num_blocks, *size):
self._is_empty = False
self.num_blocks = num_blocks
self.size = size
self.kv_caches = [torch.zeros(2, *size) for _ in range(num_blocks)]
self.kv_index = [0] * num_blocks
def register_prefix_cache(self, prefix_cache):
self.prefix_cache = prefix_cache
@property
def is_empty(self):
return self._is_empty
@property
def is_full(self):
if self.is_empty:
return False
return all(index == self.size[2] for index in self.kv_index)
def delete(self):
if not self.is_empty:
self._is_empty = True
del self.kv_caches
del self.kv_index
def to(self, device, dtype=torch.bfloat16):
for i in range(self.num_blocks):
self.kv_caches[i] = self.kv_caches[i].to(device=device, dtype=dtype)
def extend_length(self, length):
assert not self.is_empty, "KVCache is empty, cannot extend length"
self.size = (self.size[0], self.size[1], self.size[2] + length, self.size[3])
for i in range(self.num_blocks):
pad = self.kv_caches[i].new_zeros((2, *self.size))
pad[:, :, :, :self.kv_caches[i].size(3)] = self.kv_caches[i]
self.kv_caches[i] = pad
def expand_batch(self, ratio=2):
self.size = (self.size[0] * ratio, *self.size[1:])
for i in range(self.num_blocks):
self.kv_caches[i] = torch.cat([self.kv_caches[i] for _ in range(ratio)], dim=1)
def remove_negative_cache(self):
self.size = (self.size[0] // 2, *self.size[1:])
for i in range(self.num_blocks):
self.kv_caches[i] = self.kv_caches[i].chunk(2, dim=1)[0]
def backward_in_time(self, l):
for i in range(self.num_blocks):
self.kv_index[i] = max(0, self.kv_index[i] - l)
def reset_kv_index(self):
for i in range(self.num_blocks):
self.kv_index[i] = 0
def __call__(self, block_idx, k, v):
assert block_idx < self.num_blocks, f'block_idx {block_idx} out of range {self.num_blocks}'
# write cache
l = k.size(2)
kv_index = self.kv_index[block_idx]
if kv_index + l > self.size[2]:
raise NotImplementedError("Overflow mode is not implemented")
self.kv_caches[block_idx][0][:, :, kv_index: kv_index+l] = k
self.kv_caches[block_idx][1][:, :, kv_index: kv_index+l] = v
self.kv_index[block_idx] = kv_index + l
# read cache
kv_index = self.kv_index[block_idx]
return self.kv_caches[block_idx][0][:, :, :kv_index], self.kv_caches[block_idx][1][:, :, :kv_index]
class Permutation(torch.nn.Module):
def __init__(self, seq_length: int):
super().__init__()
self.seq_length = seq_length
self.input_shape = None
def forward(self, x: torch.Tensor | List[torch.Tensor], dim: int = 1, inverse: bool = False):
if not inverse:
self.input_shape = x.shape
x = rearrange(x, 'b t h w c -> b (t h w) c' if x.dim() == 5 else 'b h w c -> b (h w) c')
x = self.permute(x, dim, self.input_shape, inverse=False)
else:
x = self.permute(x, dim, self.input_shape, inverse=True)
x = x.reshape(-1, *self.input_shape[1:])
return x
def permute(self, x: torch.Tensor, dim: int = 1, shape=None, inverse: bool = False) -> torch.Tensor:
raise NotImplementedError('Overload me')
class PermutationIdentity(Permutation):
def permute(self, x: torch.Tensor, dim: int = 1, shape=None, inverse: bool = False) -> torch.Tensor:
return x.clone()
class PermutationFlip(Permutation):
def permute(self, x: torch.Tensor, dim: int = 1, shape=None, inverse: bool = False) -> torch.Tensor:
return x.flip(dims=[dim])
class PermutationFlipInBlock(Permutation):
def permute(self, x: torch.Tensor, dim: int = 1, shape=None, inverse: bool = False) -> torch.Tensor:
assert shape is not None, "shape must be provided for PermutationFlipInBlock"
if len(shape) == 5:
assert dim == 1, "dim must be 1 for 5D tensor in PermutationFlipInBlock"
# flip the tensor within blocks of size `block_size`, globally still in the same order
x = x.view(x.size(0), shape[1], -1, x.size(-1)).flip(dims=[2]).view_as(x)
else:
x = x.flip(dims=[dim])
return x
class RMSNorm(torch.nn.Module):
def __init__(
self,
dim: int,
eps: float = 1e-6,
add_unit_offset: bool = True,
):
super().__init__()
self.eps = eps
self.add_unit_offset = add_unit_offset
self.weight = torch.nn.Parameter(torch.zeros(dim))
def _norm(self, x):
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
def forward(self, x):
# Llama does x.to(float16) * w whilst Gemma2 is (x * w).to(float16)
# See https://github.com/huggingface/transformers/pull/29402
output = self._norm(x.float())
if self.add_unit_offset:
output = output * (1 + self.weight.float())
else:
output = output * self.weight.float()
return output.type_as(x)
class Attention(torch.nn.Module):
def __init__(self, in_channels: int, head_channels: int, norm_type: str = "layer_norm",
num_heads=None, num_kv_heads=None, use_qk_norm=False,
use_post_norm=False, use_bias=True, hf_style_rope=False, non_causal=False):
super().__init__()
if norm_type == "layer_norm":
self.norm = torch.nn.LayerNorm(in_channels)
elif norm_type == "rms_norm":
self.norm = RMSNorm(in_channels)
else:
self.norm = torch.nn.Identity()
self.head_channels = head_channels
self.num_heads = num_heads if num_heads is not None else in_channels // head_channels
self.num_kv_heads = num_kv_heads if num_kv_heads is not None else self.num_heads # GQA
self.q_size = self.num_heads * head_channels
self.kv_size = self.num_kv_heads * head_channels
self.qkv = torch.nn.Linear(in_channels, self.q_size + 2 * self.kv_size, bias=use_bias)
self.proj = torch.nn.Linear(self.q_size, in_channels, bias=use_bias)
self.query_norm = (RMSNorm(self.head_channels) if use_qk_norm else None)
self.key_norm = (RMSNorm(self.head_channels) if use_qk_norm else None)
self.post_norm = (RMSNorm(in_channels) if use_post_norm else None)
self.sqrt_scale = head_channels ** (-0.25)
self.hf_style_rope = hf_style_rope
self.non_causal = non_causal
def apply_rope(self, x: torch.Tensor, freqs_cis: torch.Tensor) -> torch.Tensor:
if self.hf_style_rope:
return rearrange(apply_rope(rearrange(x, '... (u d) -> ... (d u)', u=2), freqs_cis), '... (d u) -> ... (u d)', u=2)
return apply_rope(x, freqs_cis)
def prepare_for_attention(self, x: torch.Tensor, freqs_cis=None, kv_cache=None):
B, T, _ = x.size()
q, k, v = self.qkv(self.norm(x)).split([self.q_size, self.kv_size, self.kv_size], dim=-1)
q = q.view(B, T, self.num_heads, self.head_channels).transpose(1, 2) # (b, h, t, d)
k = k.view(B, T, self.num_kv_heads, self.head_channels).transpose(1, 2) # (b, h, t, d)
v = v.view(B, T, self.num_kv_heads, self.head_channels).transpose(1, 2) # (b, h, t, d)
if self.query_norm is not None and self.key_norm is not None:
q, k = self.query_norm(q), self.key_norm(k)
if kv_cache is not None:
k, v = kv_cache(k, v)
if freqs_cis is not None:
lq, lk = q.size(2), k.size(2)
q, k = self.apply_rope(q, freqs_cis[lk-lq:lk]), self.apply_rope(k, freqs_cis[:lk])
if self.num_kv_heads != self.num_heads: # GQA (b, h, t, d)
k = torch.repeat_interleave(k, self.num_heads // self.num_kv_heads, dim=1)
v = torch.repeat_interleave(v, self.num_heads // self.num_kv_heads, dim=1)
return q.to(x.dtype), k.to(x.dtype), v.to(x.dtype)
def output_after_attention(self, x: torch.Tensor):
B, _, T, _ = x.shape
x = x.transpose(1, 2).reshape(B, T, self.q_size)
x = self.proj(x)
if self.post_norm is not None:
x = self.post_norm(x)
return x
def apply_attention(self, q, k, v, mask=None, temp=1.0):
scale = self.sqrt_scale**2 / temp
is_causal = not self.non_causal
if is_causal and q.size(2) < k.size(2) and mask is None:
prefix_len = k.size(2) - q.size(2)
mask = torch.tril(torch.ones(q.size(2), k.size(2), device=q.device, dtype=torch.bool), diagonal=prefix_len)
if mask is not None:
mask = mask.bool()
is_causal = False
# spda
x = torch.nn.functional.scaled_dot_product_attention(
q, k, v, attn_mask=mask, is_causal=is_causal, scale=scale)
return x
def forward(self, x: torch.Tensor, mask: torch.Tensor | None = None, temp: float = 1.0, freqs_cis=None, kv_cache=None,
) -> torch.Tensor:
q, k, v = self.prepare_for_attention(x, freqs_cis, kv_cache)
x = self.apply_attention(q, k, v, mask, temp)
x = self.output_after_attention(x)
return x
class MLP(torch.nn.Module):
def __init__(self, channels: int, expansion: float, use_swiglu=False, norm_type="layer_norm", use_post_norm=False, use_bias=True):
super().__init__()
if norm_type == "layer_norm":
self.norm = torch.nn.LayerNorm(channels)
elif norm_type == "rms_norm":
self.norm = RMSNorm(channels)
else:
self.norm = torch.nn.Identity()
self.post_norm = (RMSNorm(channels) if use_post_norm else None)
self.use_swiglu = use_swiglu
intermediate_channels = int(channels * expansion)
if use_swiglu:
self.gate_proj = torch.nn.Linear(channels, intermediate_channels, bias=use_bias)
self.up_proj = torch.nn.Linear(channels, intermediate_channels, bias=use_bias)
self.down_proj = torch.nn.Linear(intermediate_channels, channels, bias=use_bias)
else:
self.main = torch.nn.Sequential(
torch.nn.Linear(channels, intermediate_channels, bias=use_bias),
torch.nn.GELU(), torch.nn.Linear(intermediate_channels, channels, bias=use_bias)
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
if self.use_swiglu:
x = self.norm(x)
x = self.down_proj(F.gelu(self.gate_proj(x), approximate='tanh') * self.up_proj(x))
else:
x = self.main(self.norm(x))
return self.post_norm(x) if self.post_norm is not None else x
class AttentionBlock(torch.nn.Module):
def __init__(self, channels: int, head_channels: int, expansion: float = 4, use_adaln: bool = False,
use_swiglu=False, norm_type="layer_norm", num_heads=None, num_kv_heads=None,
use_qk_norm=False, use_post_norm=False, use_bias=True, hf_style_rope=False, non_causal=False):
super().__init__()
if use_adaln:
self.adaLN_modulation = torch.nn.Sequential(
torch.nn.SiLU(),
torch.nn.Linear(channels, 4 * channels, bias=True),
)
self.norm1 = torch.nn.LayerNorm(channels, elementwise_affine=False, eps=1e-6)
self.norm2 = torch.nn.LayerNorm(channels, elementwise_affine=False, eps=1e-6)
torch.nn.init.constant_(self.adaLN_modulation[-1].weight, 0)
torch.nn.init.constant_(self.adaLN_modulation[-1].bias, 0)
# Hard-coded norm_type=="none" for adaLN
norm_type = 'none'
else:
self.adaLN_modulation = None
self.attention = Attention(channels, head_channels, norm_type, num_heads, num_kv_heads, use_qk_norm, use_post_norm, use_bias, hf_style_rope, non_causal)
self.mlp = MLP(channels, expansion, use_swiglu, norm_type, use_post_norm, use_bias)
def forward(
self, x: torch.Tensor, y: torch.Tensor | None = None, attn_mask: torch.Tensor | None = None,
attn_temp: float = 1.0, c=None, freqs_cis=None, kv_cache=None,
checkpoint_attn: bool = False, checkpoint_mlp: bool = False
) -> torch.Tensor:
assert (x is not None) or (y is not None), "x or y must be provided"
z = torch.cat([y, x], 1) if (x is not None) and (y is not None) else x if x is not None else y
if self.adaLN_modulation is not None and c is not None:
shift_msa, scale_msa, shift_mlp, scale_mlp = self.adaLN_modulation(c).chunk(4, dim=-1)
z = z + self._forward_attention(z, attn_mask, attn_temp, freqs_cis, kv_cache, checkpoint_attn, shift_msa, scale_msa)
z = z + self._forward_mlp(z, checkpoint_mlp, shift_mlp, scale_mlp)
else:
z = z + self._forward_attention(z, attn_mask, attn_temp, freqs_cis, kv_cache, checkpoint_attn)
z = z + self._forward_mlp(z, checkpoint_mlp)
x, y = (z[:, y.size(1):], z[:, :y.size(1)]) if (x is not None) and (y is not None) \
else (z, None) if x is not None else (None, z)
return x, y
def _forward_attention(self, z, attn_mask, attn_temp, freqs_cis, kv_cache, checkpoint_attn, shift=None, scale=None):
def attn_fn(z_in):
if shift is not None and scale is not None:
z_in = modulate(self.norm1(z_in), shift, scale)
return self.attention(z_in, attn_mask, attn_temp, freqs_cis, kv_cache)
return checkpoint(attn_fn, z, use_reentrant=False) if checkpoint_attn and self.training else attn_fn(z)
def _forward_mlp(self, z, checkpoint_mlp, shift=None, scale=None):
def mlp_fn(z_in):
if shift is not None and scale is not None:
z_in = modulate(self.norm2(z_in), shift, scale)
return self.mlp(z_in)
return checkpoint(mlp_fn, z, use_reentrant=False) if checkpoint_mlp and self.training else mlp_fn(z)
class MetaBlock(torch.nn.Module):
attn_mask: torch.Tensor
def __init__(
self,
in_channels: int,
channels: int,
img_size: int,
permutation: Permutation,
pt_seq_len: int | None = None,
num_layers: int = 1,
head_dim: int = 64,
num_heads: None | int = None,
num_kv_heads: None | int = None,
txt_size: int = 0,
txt_dim: int = 0,
expansion: float = 4,
use_rope: bool = False,
use_sos: bool = False,
use_softplus: bool = False,
use_swiglu: bool = False,
use_qk_norm: bool =False,
use_post_norm: bool = False,
use_final_norm: bool = False,
use_bias: bool = True,
use_proj_txt: bool = True,
hf_style_rope: bool = False,
norm_type: str ="layer_norm",
use_mm_attn: bool = False,
use_checkpoint: int = False,
use_checkpoint_mlp: int = None,
soft_clip: float = 0,
local_attn_window: int = None,
):
super().__init__()
out_channels = in_channels * 2
self.proj_in = torch.nn.Linear(in_channels, channels)
self.proj_out = torch.nn.Linear(channels, out_channels)
if use_sos:
self.sos_embed = torch.nn.Parameter(torch.randn(1, 1, in_channels))
torch.nn.init.constant_(self.proj_out.weight, 0)
self.txt_size = txt_size
self.img_size = img_size
self.txt_dim = txt_dim
self.pt_seq_len = pt_seq_len or img_size
# KV cache configurations
num_kv_heads = num_kv_heads or (num_heads or channels // head_dim)
self.kv_cache_size = [num_kv_heads, head_dim]
if not use_rope:
self.pos_embed = torch.nn.Parameter(torch.randn(img_size ** 2, channels) * 1e-2)
else:
self.pos_embed = None
if txt_dim > 0:
self.proj_txt = torch.nn.Linear(txt_dim, channels) if use_proj_txt else torch.nn.Identity()
assert use_proj_txt or (txt_dim == channels), 'text dimension must equal channels when not using projection'
self.attn_blocks = torch.nn.ModuleList(
[AttentionBlock(channels, head_dim, expansion, False, use_swiglu,
norm_type, num_heads, num_kv_heads, use_qk_norm, use_post_norm, use_bias, hf_style_rope)
for _ in range(num_layers)])
self.use_final_norm = use_final_norm
if use_final_norm:
self.final_norm = RMSNorm(channels)
self.use_softplus = use_softplus
self.permutation = permutation
self.use_checkpoint = use_checkpoint
self.use_checkpoint_mlp = use_checkpoint_mlp
self.use_sos = use_sos
self.soft_clip = soft_clip
self.local_attn_window = local_attn_window
self.block_masks = {} # for local attention
# ---- DEPRECATED: do not pass mask to enable flash attention ----- For compatibility ----- #
self.register_buffer('attn_mask', torch.tril(torch.ones(pt_seq_len ** 2 + txt_size, pt_seq_len ** 2 + txt_size)))
def get_freqs_cis(self, x, y, rope):
# get the input shape
h, w = x.size(-3), x.size(-2)
d = x.size(1) if x.dim() == 5 else 0
txt_size = y.size(1) if self.txt_size > 0 and y is not None else 0
if not rope.is_1d: # prepare 2D RoPE
if self.txt_size > 0 or d > 0: # prepare 3D RoPE
if self.txt_dim > 0: # text is conditioned
pos = get_positions(h, w, txt_size, rope.pt_seq_len, d, mode='3d')
else: # text is not conditioned
pos = get_positions(h, w, 0, rope.pt_seq_len, d, mode='3d')
else:
pos = get_positions(h, w, 0, rope.pt_seq_len, mode='2d')
else: # prepare 1D RoPE
pos = get_positions(h, w, txt_size, rope.pt_seq_len, mode='1d')
return rope(pos.type_as(x))
def get_sos_embed(self, x):
sos_embed = self.sos_embed.expand(x.size(0), -1, -1)
return sos_embed
def get_prepared(self, x):
# input, output, freqs_cis
x_in = x.clone()
if self.use_sos: # add SOS token, predict the first token sos->x_in[0]
x = torch.cat([self.get_sos_embed(x), x[:, :-1]], dim=1)
return x_in, x
def get_proj_in(self, x):
x = self.proj_in(x)
return x
def get_proj_out(self, x):
x = self.proj_out(x)
if hasattr(self, "soft_clip") and self.soft_clip > 0:
x = self.soft_clip * torch.tanh(x / self.soft_clip)
return x
def get_local_window_mask(self, x, y):
_, T, H, W, _ = x.shape
L = y.size(1) if y is not None else 0
B = H * W
N = T * B
S = L + N
G = self.local_attn_window
def mask(q, k):
return (k <= q) & ((k < L) | ((k - L) // B > (q - L) // B - G))
return mask(torch.arange(S, device=x.device)[:, None], torch.arange(S, device=x.device)[None, :])
def initialize_kv_cache(self, kv_cache, x, freqs_cis, reuse_kv_cache=False):
if self.local_attn_window is not None and self.local_attn_window > 0:
video_frame_size = x.size(-3) * x.size(-2)
kv_cache_length = self.local_attn_window * video_frame_size
kv_cache_length += self.txt_size if self.txt_dim > 0 else 0
kv_cache.meta_data.update(
{"frame_size": video_frame_size, "txt_size": self.txt_size + 1 if self.txt_dim > 0 else 0})
else:
kv_cache_length = freqs_cis.size(0)
kv_cache_size = (x.size(0), self.kv_cache_size[0], kv_cache_length, self.kv_cache_size[1])
if kv_cache.is_empty:
kv_cache.initialize(len(self.attn_blocks), *kv_cache_size)
kv_cache.to(x.device, x.dtype)
else:
target_size = kv_cache_size[-2]
if reuse_kv_cache:
target_size = target_size - kv_cache.kv_index[0]
kv_cache.extend_length(target_size)
return kv_cache
def forward(self, x: torch.Tensor | List[torch.Tensor], y: torch.Tensor | None = None, rope=None, kv_cache=None, guidance=None):
freqs_cis = self.get_freqs_cis(x, y, rope) if rope is not None else None
attn_mask = None
if kv_cache is not None:
kv_cache = self.initialize_kv_cache(kv_cache, x, freqs_cis)
x = self.permutation(x)
pos_embed = self.permutation(self.pos_embed, dim=0) if self.pos_embed is not None else None
# prepare input
x_in, x = self.get_prepared(x)
if kv_cache is not None:
kv_cache.register_prefix_cache(x_in)
# input projection
x = self.get_proj_in(x)
if pos_embed is not None:
x = x + pos_embed
# conditioning
if self.txt_dim > 0:
y = self.proj_txt(y)
else:
y = None
# main block
for it, block in enumerate(self.attn_blocks):
_kv_cache = partial(kv_cache, it) if kv_cache is not None else None
# Frequency-based checkpointing strategy:
# - Checkpoint attention every use_checkpoint blocks (if use_checkpoint > 0)
# - Checkpoint MLP every use_checkpoint_mlp blocks (if provided), otherwise every use_checkpoint blocks
checkpoint_attn = self.training and self.use_checkpoint > 0 and ((it + 1) % self.use_checkpoint == 0)
if self.use_checkpoint_mlp is not None:
checkpoint_mlp = self.training and self.use_checkpoint_mlp > 0 and ((it + 1) % self.use_checkpoint_mlp == 0)
else:
checkpoint_mlp = self.training and self.use_checkpoint > 0 and ((it + 1) % self.use_checkpoint == 0)
x, y = block(x, y, attn_mask, 1.0, None, freqs_cis, _kv_cache,
checkpoint_attn=checkpoint_attn,
checkpoint_mlp=checkpoint_mlp)
# final norm
if self.use_final_norm:
x, y = self.final_norm(x), self.final_norm(y) if y is not None else None
x = self.get_proj_out(x)
if not self.use_sos: # no SOS token, we need to shift the sequence
x = torch.cat([torch.zeros_like(x[:, :1]), x[:, :-1]], dim=1)
xa, xb = x.chunk(2, dim=-1)
# Store original dtype for output conversion
original_dtype = xa.dtype
# Convert to fp32 for numerical stability
xa, xb, x_in = xa.float(), xb.float(), x_in.float()
if not self.use_softplus:
xa = xa.exp()
else:
xa = F.softplus(xa + INV_SOFTPLUS_1)
if guidance is not None and guidance > 0:
xb, xa = self.guidance(xa, xb, guidance, 1.0, 'ab')
# NOTE: this "scale" is in fact 1/sigma, not sigma
x = self.permutation((x_in - xb) / xa, inverse=True)
logdet = -torch.log(xa) # keep all the dimensions
# Convert back to original precision
x = x.to(original_dtype)
return x, y, logdet
def guidance(self, za, zb, guidance, r=1.0, guide_what='ab'):
za, za_u = [torch.cat([a, a]) for a in za.chunk(2, dim=0)]
zb, zb_u = [torch.cat([a, a]) for a in zb.chunk(2, dim=0)]
g = r * guidance
def logits_guided(mu_c, sigma_c, mu_u, sigma_u, w):
# inspired from: (1+w) * logP_cond - w * logP_uncond
# sigma_c = torch.minimum(sigma_c, sigma_u)
s = (sigma_c / sigma_u).clip(max=1.0).square()
sigma_eff = sigma_c / (1 + w - w * s).sqrt()
mu_eff = ((1 + w) * mu_c - (w * s) * mu_u) / (1 + w - w * s)
return mu_eff, sigma_eff
def original_guidance(mu_c, sigma_c, mu_u, sigma_u, w):
if 'a' in guide_what:
sigma_c = sigma_c + g * (sigma_c - sigma_u)
if 'b' in guide_what:
mu_c = mu_c + g * (mu_c - mu_u)
return mu_c, sigma_c
#zb, za = original_guidance(zb, za, zb_u, za_u, guidance)
zb, za = logits_guided(zb, za, zb_u, za_u, guidance)
return zb, za
def reverse_step(
self, x: torch.Tensor, t: int, kv_cache: KVCache,
pos_embed: torch.Tensor | None = None, y: torch.Tensor | None = None,
attn_temp: float = 1.0, freqs_cis=None
) -> tuple[torch.Tensor, torch.Tensor]:
# Store original dtype for sampling tensor
original_dtype = x.dtype
if self.use_sos: # get i-th patch but keep the sequence dimension
x_in = self.get_sos_embed(x[:, :1]) if t == 0 else x[:, t - 1 : t]
else:
x_in = x[:, t : t + 1]
# Convert to model's dtype for neural network computation
if hasattr(self.proj_in, 'weight'):
target_dtype = self.proj_in.weight.dtype
x_in = x_in.to(target_dtype)
x = self.get_proj_in(x_in)
# if positional embedding
if pos_embed is not None:
x = x + pos_embed[t: t+1]
# main block
for i, block in enumerate(self.attn_blocks):
x, _ = block(x, None, attn_temp=attn_temp, freqs_cis=freqs_cis, kv_cache=partial(kv_cache, i))
# final norm
if self.use_final_norm:
x = self.final_norm(x)
x = self.get_proj_out(x)
xa, xb = x.chunk(2, dim=-1)
# Convert back to original dtype for sampling computations
return xa.to(original_dtype), xb.to(original_dtype)
def reverse_step_condition(self, y, kv_cache, pos_embed=None, attn_temp: float = 1.0, freqs_cis=None):
# Convert to model's dtype for neural network computation
if hasattr(self.proj_txt, 'weight'):
target_dtype = self.proj_txt.weight.dtype
y = y.to(target_dtype)
y = self.proj_txt(y)
for i, block in enumerate(self.attn_blocks):
_, y = block(None, y, attn_temp=attn_temp, freqs_cis=freqs_cis, kv_cache=partial(kv_cache, i))
return y
def reverse(
self,
z: torch.Tensor,
y: torch.Tensor | None = None,
guidance: float = 0,
guide_what: str = 'ab',
attn_temp: float = 1.0,
annealed_guidance: bool = False,
rope=None,
verbose=False,
kv_cache: KVCache=KVCache(),
**unused_kwargs
) -> torch.Tensor:
# Ensure sampling tensors are in float32 for numerical stability
original_dtype = z.dtype
z = z.float()
freqs_cis = self.get_freqs_cis(z, y, rope) if rope is not None else None
if guidance > 0:
z = torch.cat([z, z], 0)
# kv cache
reuse_kv_cache = kv_cache.prefix_cache is not None and kv_cache.kv_index[0] > 0
kv_cache = self.initialize_kv_cache(kv_cache, z, freqs_cis, reuse_kv_cache)
# permute the input
z = self.permutation(z)
pos_embed = self.permutation(self.pos_embed, dim=0) if self.pos_embed is not None else None
# run additional text condition, results will be used in KV cache.
if self.txt_dim > 0:
if not reuse_kv_cache:
self.reverse_step_condition(y, kv_cache, pos_embed, attn_temp, freqs_cis)
txt_size = y.size(1) if self.txt_dim > 0 else 0
# run the reverse process
x = z.clone()
if reuse_kv_cache:
x[:, :kv_cache.prefix_cache.size(1)] = kv_cache.prefix_cache # fill the prefix cache
T = x.size(1) - 1 if not self.use_sos else x.size(1)
for t in tqdm.trange(T, disable=not verbose, desc='Sub-flow Sampling', leave=False):
if reuse_kv_cache and kv_cache.kv_index[0] > t + txt_size:
continue
za, zb = self.reverse_step(x, t, kv_cache, pos_embed, y, attn_temp, freqs_cis)
# Ensure sampling computations stay in float32
za, zb = za.float(), zb.float()
if not self.use_softplus:
za, zb = za.exp().squeeze(1), zb.squeeze(1)
else:
za, zb = F.softplus(za + INV_SOFTPLUS_1).squeeze(1), zb.squeeze(1)
if guidance > 0 and guide_what:
r = (t + 1) / T if annealed_guidance else 1.0
zb, za = self.guidance(za, zb, guidance, r, guide_what)
if self.use_sos:
x[:, t] = z[:, t] * za + zb
else:
x[:, t + 1] = z[:, t + 1] * za + zb
if guidance > 0:
x = x.chunk(2, dim=0)[0]
kv_cache.remove_negative_cache() # remove the second half of the cache
x = self.permutation(x, inverse=True)
# Convert back to original dtype if needed
return x.to(original_dtype)
def jacobi(self,
z: torch.Tensor,
y: torch.Tensor | None = None,
guidance: float = 0,
rope=None,
kv_cache=None,
verbose=False,
jacobi_block_size: int = 32,
jacobi_max_iter: int = 32,
jacobi_th: float = 0.001,
context_length: int = None,
**unused_kwargs) -> torch.Tensor:
assert self.use_sos, "Jacobi iteration requires SOS token to be used"
assert self.pos_embed is None, "Jacobi iteration does not support positional embedding"
# Ensure sampling tensors are in float32 for numerical stability
original_dtype = z.dtype
z = z.float()
freqs_cis = self.get_freqs_cis(z, y, rope) if rope is not None else None
if guidance > 0:
z = torch.cat([z, z], 0)
# kv cache
reuse_kv_cache = kv_cache.prefix_cache is not None and kv_cache.kv_index[0] > 0
kv_cache = self.initialize_kv_cache(kv_cache, z, freqs_cis, reuse_kv_cache)
video_length = z.size(1) if z.dim() == 5 else 1
# permute the input
z = self.permutation(z)
# prepare input
x_full = torch.cat([self.get_sos_embed(z), z.clone()], dim=1)
if reuse_kv_cache:
x_full[:, 1: kv_cache.prefix_cache.size(1) + 1] = kv_cache.prefix_cache # fill the prefix cache
# conditioning
if self.txt_dim > 0:
if not reuse_kv_cache:
self.reverse_step_condition(y, kv_cache, freqs_cis=freqs_cis)
txt_size = y.size(1) if self.txt_dim > 0 else 0
video_frame_size = z.size(1) // video_length
start_idx = 0
if reuse_kv_cache:
start_idx = kv_cache.kv_index[0] - txt_size # start from the last cached index
prog_bar = tqdm.tqdm(total=z.size(1), disable=not verbose, desc='Block-wise Jacobi Iteration', leave=False)
prog_bar.update(start_idx)
local_attn_window = self.local_attn_window * video_frame_size if self.local_attn_window is not None else None
target_frame_size = z.size(1) if local_attn_window is None else min(z.size(1), local_attn_window)
context_size = None if local_attn_window is None else context_length * video_frame_size
while target_frame_size <= z.size(1):
while start_idx < target_frame_size:
chunk_size = jacobi_block_size if start_idx <= video_frame_size else jacobi_block_size * 4
local_done = torch.zeros((), dtype=torch.bool, device=x_full.device)
for i in tqdm.tqdm(range(jacobi_max_iter), disable=True, desc='Jacobi Iteration', leave=False):
if start_idx + chunk_size >= target_frame_size:
chunk_size = target_frame_size - start_idx
if i == 0 and start_idx > video_frame_size: # optional to use past frame to initialize the current frame
x = x_full[:, start_idx - video_frame_size: start_idx + chunk_size - video_frame_size]
else:
x = x_full[:, start_idx: start_idx + chunk_size]
# main forward - convert to model dtype for neural network computation
if hasattr(self.proj_in, 'weight'):
target_dtype = self.proj_in.weight.dtype
x = x.to(target_dtype)
x = self.get_proj_in(x)
for it, block in enumerate(self.attn_blocks):
_kv_cache = partial(kv_cache, it) if kv_cache is not None else None
x = block(x, None, freqs_cis=freqs_cis, kv_cache=_kv_cache)[0]
if self.use_final_norm:
x = self.final_norm(x)
x = self.get_proj_out(x)
xa, xb = x.chunk(2, dim=-1)
# Convert back to float32 for sampling computations
xa, xb = xa.float(), xb.float()
if not self.use_softplus:
xa = xa.exp()
else:
xa = F.softplus(xa + INV_SOFTPLUS_1)
if guidance > 0:
xb, xa = self.guidance(xa, xb, guidance, 1.0, 'ab')
# compute the Jacobi Iteration - all in float32
new_x = xb + xa * z[:, start_idx: start_idx+chunk_size]
diff = ((new_x - x_full[:, start_idx+1: start_idx+1+chunk_size]) ** 2).mean() / (new_x ** 2).mean()
x_full[:, start_idx+1: start_idx+1+chunk_size] = new_x
if diff < jacobi_th or i == jacobi_max_iter - 1: # do not clean the cache on the last iteration
local_done.fill_(1)
global_done = local_done.clone()
torch.distributed.all_reduce(global_done, op=torch.distributed.ReduceOp.MIN)
if int(global_done.item()) == 1:
break
kv_cache.backward_in_time(chunk_size)
start_idx += chunk_size
prog_bar.update(chunk_size)
if target_frame_size >= z.size(1):
break
target_frame_size += local_attn_window - context_size if local_attn_window is not None else video_frame_size
target_frame_size = min(target_frame_size, z.size(1))
# re-encode the context with attention blocks
print(f're-encoding the context {start_idx+1-context_size}:{start_idx+1}')
kv_cache.reset_kv_index()
if self.txt_dim > 0:
self.reverse_step_condition(y, kv_cache, freqs_cis=freqs_cis)
x_context = x_full[:, start_idx+1-context_size: start_idx+1]
x_context_in, x_context = self.get_prepared(x_context)
x_context = self.get_proj_in(x_context)
for it, block in enumerate(self.attn_blocks):
_kv_cache = partial(kv_cache, it) if kv_cache is not None else None
x_context = block(x_context, None, freqs_cis=freqs_cis, kv_cache=_kv_cache)[0]
x = x_full[:, 1:]
if guidance > 0:
x = x.chunk(2, dim=0)[0] # remove SOS token
x = self.permutation(x, inverse=True)
# Convert back to original dtype if needed
return x.to(original_dtype)
class IdentityBlock(MetaBlock):
def __init__(self, *args, **kwargs):
super(MetaBlock, self).__init__()
def forward(self, x, y=None, rope=None, **unused):
return x, y, x.new_zeros(x.size(0))
def reverse(self,
z: torch.Tensor,
y: torch.Tensor | None = None,
guidance: float = 0,
guide_what: str = 'ab',
attn_temp: float = 1.0,
annealed_guidance: bool = False,
rope=None,
verbose=False,
kv_cache: KVCache=KVCache(), **unused):
# Preserve original dtype
return z
def jacobi(self,
z: torch.Tensor,
y: torch.Tensor | None = None,
guidance: float = 0,
rope=None,
kv_cache=None,
verbose=False,
jacobi_block_size: int = 64,
jacobi_th: float = 0.005, **unused_kwargs) -> torch.Tensor:
return z
class NonCausalBlock(MetaBlock):
def __init__(
self,
in_channels: int,
channels: int,
img_size: int,
pt_seq_len: int | None = None,
num_layers: int = 8,
head_dim: int = 64,
num_heads: None | int = None,
num_kv_heads: None | int = None,
txt_size: int = 0,
txt_dim: int = 0,
expansion: float = 4,
use_rope: bool = False,
use_swiglu: bool = False,
use_qk_norm: bool =False,
use_post_norm: bool = False,
use_final_norm: bool = False,
use_bias: bool = True,
hf_style_rope: bool = False,
norm_type: str ="layer_norm",
use_checkpoint: int = False,
use_checkpoint_mlp: int = None,
block_causal: int = 0,
window: int = None,
**unused_kwargs,
):
super(MetaBlock, self).__init__()
out_channels = in_channels
self.proj_in = torch.nn.Linear(in_channels, channels)
self.proj_out = torch.nn.Linear(channels, out_channels)
torch.nn.init.constant_(self.proj_out.weight, 0)
self.txt_size = txt_size
self.img_size = img_size
self.txt_dim = txt_dim
self.pt_seq_len = pt_seq_len or img_size
self.block_causal = block_causal
self.window = window
# KV cache configurations
num_kv_heads = num_kv_heads or (num_heads or channels // head_dim)
self.kv_cache_size = [num_kv_heads, head_dim]
if txt_dim > 0:
self.proj_txt = torch.nn.Linear(txt_dim, channels)
self.attn_blocks = torch.nn.ModuleList(
[AttentionBlock(channels, head_dim, expansion, False, use_swiglu, norm_type, num_heads, num_kv_heads,
use_qk_norm, use_post_norm, use_bias, hf_style_rope, non_causal=True) for _ in range(num_layers)])
self.use_final_norm = use_final_norm
if use_final_norm:
self.final_norm = RMSNorm(channels)
self.use_checkpoint = use_checkpoint
self.use_checkpoint_mlp = use_checkpoint_mlp
self.block_masks = {} # for local attention
def get_local_window_mask(self, x, y):
_, T, H, W, _ = x.shape
L = y.size(1) if y is not None else 0
B = H * W
N = T * B
S = L + N
A = self.block_causal
G = self.window if self.window is not None else 10000
def mask(q, k):
return (k < L) | (
((k - L) // B >= (q - L) // B + A - 1 - G) &
((k - L) // B <= torch.relu(q - L) // B + A - 1)
)
return mask(torch.arange(S, device=x.device)[:, None], torch.arange(S, device=x.device)[None, :])
def forward(self, x, y, rope, **unused):
freqs_cis = self.get_freqs_cis(x, y, rope) if rope is not None else None
if self.block_causal > 0 and x.dim() == 5:
attn_mask = self.get_local_window_mask(x, y if self.txt_dim > 0 else None)
else:
attn_mask = None
if x.dim() == 5: # video input
N, H, W, x = x.size(1), x.size(2), x.size(3), rearrange(x, 'b t h w c -> b (t h w) c') # flatten x
else:
N, H, W, x = 0, x.size(1), x.size(2), rearrange(x, 'b h w c -> b (h w) c') # flatten x
x = self.get_proj_in(x)
y = self.proj_txt(y) if self.txt_dim > 0 else None
for it, block in enumerate(self.attn_blocks):
# Frequency-based checkpointing strategy:
# - Checkpoint attention every use_checkpoint blocks (if use_checkpoint > 0)
# - Checkpoint MLP every use_checkpoint_mlp blocks (if provided), otherwise every use_checkpoint blocks
checkpoint_attn = self.training and self.use_checkpoint > 0 and ((it + 1) % self.use_checkpoint == 0)
if self.use_checkpoint_mlp is not None:
checkpoint_mlp = self.training and self.use_checkpoint_mlp > 0 and ((it + 1) % self.use_checkpoint_mlp == 0)
else:
checkpoint_mlp = self.training and self.use_checkpoint > 0 and ((it + 1) % self.use_checkpoint == 0)
x, y = block(x, y, attn_mask, 1.0, None, freqs_cis,
checkpoint_attn=checkpoint_attn, checkpoint_mlp=checkpoint_mlp)
if self.use_final_norm:
x = self.final_norm(x)
x = self.get_proj_out(x)
if N > 0:
x = rearrange(x, 'b (t h w) d -> b t h w d', t=N, h=H, w=W)
else:
x = rearrange(x, 'b (h w) d -> b h w d', h=H, w=W)
return x
class Model(torch.nn.Module):
def __init__(
self,