-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathattention_flax.py
More file actions
1529 lines (1311 loc) · 55.9 KB
/
attention_flax.py
File metadata and controls
1529 lines (1311 loc) · 55.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
# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# 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 functools
import math
from typing import Optional, Callable, Tuple
import flax.linen as nn
from flax import nnx
import jax
from jax.ad_checkpoint import checkpoint_name
from jax.sharding import PartitionSpec
import jax.numpy as jnp
from jax.experimental import shard_map
from jax.experimental.pallas.ops.tpu.splash_attention import splash_attention_mask
from jax.experimental.pallas.ops.tpu.splash_attention import splash_attention_kernel
from einops import rearrange
from .. import common_types, max_logging
from . import quantizations
Array = common_types.Array
Mesh = common_types.Mesh
DType = common_types.DType
BlockSizes = common_types.BlockSizes
AxisNames = common_types.AxisNames
BATCH = common_types.BATCH
LENGTH = common_types.LENGTH
KV_LENGTH = common_types.KV_LENGTH
HEAD = common_types.HEAD
D_KV = common_types.D_KV
EMBED = common_types.EMBED
Quant = quantizations.AqtQuantization
def _maybe_aqt_einsum(quant: Quant):
return jnp.einsum if quant is None else quant.einsum()
def _check_attention_inputs(query: Array, key: Array, value: Array) -> None:
"""Check attention inputs."""
assert key.ndim == value.ndim, "k, v must have same rank."
assert query.shape[:-3] == key.shape[:-3] == value.shape[:-3], "q, k, v batch dims must match."
assert key.shape[-2] == value.shape[-2], "k, v num_kv_heads must match."
assert key.shape[-3] == value.shape[-3], "k, v lengths must match."
assert query.shape[-1] == key.shape[-1], "q, k depths must match."
def _reshape_data_from_cudnn_flash(tensor):
# reshapes from [b, s, h, d] back to [b, s, h * d]
return tensor.reshape(tensor.shape[0], tensor.shape[1], -1)
def _reshape_data_for_cudnn_flash(tensor, heads):
# reshapes from [b, s, h * d] to [b, s, h, d] (input format to flash format)
batch, seq, heads_and_dim_head = tensor.shape
tensor = tensor.reshape(batch, seq, heads, heads_and_dim_head // heads)
return tensor
def _reshape_batch_dim_to_heads(tensor, heads):
batch_size, seq_len, dim = tensor.shape
head_size = heads
tensor = tensor.reshape(batch_size // head_size, head_size, seq_len, dim)
tensor = jnp.transpose(tensor, (0, 2, 1, 3))
reshaped_tensor = tensor.reshape(batch_size // head_size, seq_len, dim * head_size)
return jax.lax.with_sharding_constraint(reshaped_tensor, PartitionSpec("data", "fsdp", "tensor"))
def _reshape_heads_to_batch_dim(tensor, heads):
if tensor.ndim == 3:
batch_size, seq_len, dim = tensor.shape
head_size = heads
tensor = tensor.reshape(batch_size, seq_len, head_size, dim // head_size)
tensor = jnp.transpose(tensor, (0, 2, 1, 3))
reshaped_tensor = tensor.reshape(batch_size * head_size, seq_len, dim // head_size)
else:
batch_size, head_size, seq_len, head_dim = tensor.shape
reshaped_tensor = tensor.reshape(batch_size * head_size, seq_len, head_dim)
return jax.lax.with_sharding_constraint(reshaped_tensor, PartitionSpec("data", "fsdp", "tensor"))
def _reshape_heads_to_head_dim(tensor):
# takes a tensor of shape [b, h, s, d] and reshapes to [b, s, h * d]
# This is used to transform the output of flash attention back into the format of other attention outputs
b, h, s, d = tensor.shape
tensor = jnp.transpose(tensor, axes=[0, 2, 1, 3])
reshaped_tensor = jnp.reshape(tensor, (b, -1, h * d))
return jax.lax.with_sharding_constraint(reshaped_tensor, PartitionSpec("data", "fsdp", "tensor"))
def _unflatten_heads(tensor, heads):
# reshapes from [b, s, h * d] to [b, h, s, d] (input format to flash format)
batch, seq, heads_and_dim_head = tensor.shape
tensor = tensor.reshape(batch, seq, heads, heads_and_dim_head // heads)
# Transpose to ('batch', 'heads', 'length', 'kv')
tensor = jnp.transpose(tensor, (0, 2, 1, 3))
return tensor
def _reshape_data_for_flash(tensor, heads):
"""
Reshapes tensors for pallas flash attention adding padding to both seq_len and head_dim.
Pads seq_len to a multiple of flash_block_size, and ensures the resulting number of
blocks is divisible by the number of shards.
"""
if tensor.ndim != 4:
tensor = _unflatten_heads(tensor, heads)
return tensor
def _pad_data_for_flash(tensor, heads, flash_block_size, num_shards: int = 1):
"""
Reshapes tensors for pallas flash attention adding padding to both seq_len and head_dim.
Pads seq_len to a multiple of flash_block_size, and ensures the resulting number of
blocks is divisible by the number of shards.
"""
tensor = _reshape_data_for_flash(tensor, heads)
# Pad head_dim to 128 if less than that.
kv_size = tensor.shape[-1]
head_dim_pad = 0
if kv_size < 128:
head_dim_pad = 128 - kv_size
# Pad seq_len with sharding constraints.
seq_len = tensor.shape[2]
# 1. First, pad seq_len to be a multiple of flash_block_size
rem = seq_len % flash_block_size
if rem != 0:
seq_len_padded_pre = seq_len + (flash_block_size - rem)
else:
seq_len_padded_pre = seq_len
# 2. Ensure num_blocks is divisible by num_shards
num_blocks = seq_len_padded_pre // flash_block_size
if num_blocks % num_shards != 0:
num_blocks += num_shards - (num_blocks % num_shards)
final_padded_len = num_blocks * flash_block_size
seq_len_pad = final_padded_len - seq_len
if kv_size < 128 or seq_len_pad != 0:
npad = ((0, 0), (0, 0), (0, seq_len_pad), (0, head_dim_pad))
tensor = jnp.pad(tensor, npad)
return tensor, kv_size, seq_len
def _tpu_flash_attention(
query: jax.Array,
key: jax.Array,
value: jax.Array,
heads: int,
mesh: Mesh,
axis_names_q: AxisNames,
axis_names_kv: AxisNames,
flash_block_sizes: BlockSizes,
dtype: jnp.dtype = jnp.float32,
attention_kernel: str = "flash",
) -> jax.Array:
"""TPU Flash Attention"""
q_max_block_size = 1024 if dtype == jnp.bfloat16 else 512
# This is the case for cross-attn.
if key.shape[1] != query.shape[1]:
assert key.shape[1] % 128 == 0
kv_max_block_size = key.shape[1]
else:
kv_max_block_size = q_max_block_size
if flash_block_sizes:
block_sizes = flash_block_sizes
else:
block_sizes = splash_attention_kernel.BlockSizes(
block_q=min(q_max_block_size, query.shape[2]),
block_kv_compute=min(kv_max_block_size, key.shape[2]),
block_kv=min(kv_max_block_size, key.shape[2]),
block_q_dkv=min(q_max_block_size, query.shape[2]),
block_kv_dkv=min(kv_max_block_size, key.shape[2]),
block_kv_dkv_compute=min(kv_max_block_size, query.shape[2]),
block_q_dq=min(q_max_block_size, query.shape[2]),
block_kv_dq=min(kv_max_block_size, query.shape[2]),
)
num_fsdp_shards = mesh.shape["fsdp"]
query = _reshape_data_for_flash(query, heads)
key = _reshape_data_for_flash(key, heads)
value = _reshape_data_for_flash(value, heads)
q_axis_names = nn.logical_to_mesh_axes(axis_names_q)
kv_axis_names = nn.logical_to_mesh_axes(axis_names_kv)
@functools.partial(
shard_map.shard_map,
mesh=mesh,
in_specs=(q_axis_names, kv_axis_names, kv_axis_names),
out_specs=q_axis_names,
check_rep=False,
)
def wrap_flash_attention(query, key, value):
query, kv_size, query_seq_len = _pad_data_for_flash(query, heads, block_sizes.block_q)
key, _, key_seq_len = _pad_data_for_flash(key, heads, block_sizes.block_kv_compute)
value, _, _ = _pad_data_for_flash(value, heads, block_sizes.block_kv_compute)
mask = splash_attention_mask.FullMask(_shape=(query.shape[2], key.shape[2]))
multi_head_mask = splash_attention_mask.MultiHeadMask(masks=(mask,) * query.shape[1])
q_padded_len = query.shape[2]
q_indices = jax.lax.broadcasted_iota(jnp.int32, (q_padded_len,), 0)
q_segment_ids = (q_indices < query_seq_len).astype(jnp.int32)
kv_padded_len = key.shape[2]
kv_indices = jax.lax.broadcasted_iota(jnp.int32, (kv_padded_len,), 0)
kv_segment_ids = (kv_indices < key_seq_len).astype(jnp.int32)
segment_ids = splash_attention_kernel.SegmentIds(q=q_segment_ids, kv=kv_segment_ids)
# make_splash_mha is wrapped around shardmap and seq and head is already
# sharded based on in_specs, therefore setting head_shards=1 and q_seq_shards=1.
splash_kernel = splash_attention_kernel.make_splash_mha(
mask=multi_head_mask,
head_shards=1, # the sizes of the axis is sharding over heads
q_seq_shards=1, # the sizes of the axis is sharding over seq_len
block_sizes=block_sizes,
save_residuals=True if attention_kernel == "ring" else False,
)
vmapped_splash = jax.vmap(splash_kernel, in_axes=(0, 0, 0, None))
if attention_kernel == "flash":
attention_output = vmapped_splash(query, key, value, segment_ids)
else:
if num_fsdp_shards > 1:
out, (lse,) = vmapped_splash(query, key, value, segment_ids)
m = lse.astype(jnp.float32)
l = jnp.exp(lse - m)
o = out.astype(jnp.float32) * l[..., None]
perm = [(j, (j + 1) % num_fsdp_shards) for j in range(num_fsdp_shards)]
k1 = jax.lax.ppermute(key, axis_name="fsdp", perm=perm)
v1 = jax.lax.ppermute(value, axis_name="fsdp", perm=perm)
def ring_scan_body(carry, _):
m, l, o, k_current, v_current = carry
k_next = jax.lax.ppermute(k_current, axis_name="fsdp", perm=perm)
v_next = jax.lax.ppermute(v_current, axis_name="fsdp", perm=perm)
out_chunk, (lse_chunk,) = vmapped_splash(query, k_current, v_current, segment_ids)
m_chunk = lse_chunk.astype(jnp.float32)
m_old = m
m = jnp.maximum(m_old, m_chunk)
exp_m_diff = jnp.exp(m_old - m)
exp_m_chunk_diff = jnp.exp(m_chunk - m)
l = l * exp_m_diff + jnp.exp(lse_chunk - m)
o = o * exp_m_diff[..., None]
o += exp_m_chunk_diff[..., None] * out_chunk.astype(jnp.float32)
# Return the updated state for the next iteration
return (m, l, o, k_next, v_next), None
initial_carry = (m, l, o, k1, v1)
(m_final, l_final, o_final, _, _), _ = jax.lax.scan(ring_scan_body, initial_carry, None, length=num_fsdp_shards - 1)
attention_output = o_final / l_final[..., None]
return attention_output[:, :, :query_seq_len, :kv_size].astype(query.dtype)
devices_in_data_fsdp = mesh.shape["data"] * mesh.shape["fsdp"]
# This warning might show up when doing model eval for example, when calculating model flops
# and that is expected.
if not (query.shape[0] / devices_in_data_fsdp).is_integer():
max_logging.log(
"Warning, batch dimension should be shardable among the devices in data and fsdp"
f" axis, batch dimension: {query.shape[0]}, devices_in_data_fsdp: {devices_in_data_fsdp}"
)
x = wrap_flash_attention(query, key, value)
x = _reshape_heads_to_head_dim(x)
return x
def _apply_attention_dot(
query: Array,
key: Array,
value: Array,
dtype: jnp.dtype,
heads: int,
dim_head: int,
scale: float,
split_head_dim: bool,
float32_qk_product: bool,
use_memory_efficient_attention: bool,
):
"""Apply Attention."""
if split_head_dim:
b = key.shape[0]
query_states = jnp.reshape(query, (b, -1, heads, dim_head))
key_states = jnp.reshape(key, (b, -1, heads, dim_head))
value_states = jnp.reshape(value, (b, -1, heads, dim_head))
else:
query_states = _reshape_heads_to_batch_dim(query, heads)
key_states = _reshape_heads_to_batch_dim(key, heads)
value_states = _reshape_heads_to_batch_dim(value, heads)
if float32_qk_product:
query_states = query_states.astype(jnp.float32)
key_states = key_states.astype(jnp.float32)
if use_memory_efficient_attention:
query_states = query_states.transpose(1, 0, 2)
key_states = key_states.transpose(1, 0, 2)
value_states = value_states.transpose(1, 0, 2)
# this if statement create a chunk size for each layer of the unet
# the chunk size is equal to the query_length dimension of the deepest layer of the unet
flatten_latent_dim = query_states.shape[-3]
if flatten_latent_dim % 64 == 0:
query_chunk_size = int(flatten_latent_dim / 64)
elif flatten_latent_dim % 16 == 0:
query_chunk_size = int(flatten_latent_dim / 16)
elif flatten_latent_dim % 4 == 0:
query_chunk_size = int(flatten_latent_dim / 4)
else:
query_chunk_size = int(flatten_latent_dim)
hidden_states = jax_memory_efficient_attention(
query_states, key_states, value_states, query_chunk_size=query_chunk_size, key_chunk_size=4096 * 4
)
hidden_states = hidden_states.transpose(1, 0, 2)
else:
if split_head_dim:
attention_scores = jnp.einsum("b t n h, b f n h -> b n f t", key_states, query_states)
else:
attention_scores = jnp.einsum("b i d, b j d->b i j", query_states, key_states)
attention_scores = attention_scores * scale
attention_probs = nn.softmax(attention_scores, axis=-1 if split_head_dim else 2)
attention_probs = attention_probs.astype(dtype)
# attend to values
if split_head_dim:
hidden_states = jnp.einsum("b n f t, b t n h -> b f n h", attention_probs, value_states)
b = hidden_states.shape[0]
hidden_states = jnp.reshape(hidden_states, (b, -1, heads * dim_head))
else:
hidden_states = jnp.einsum("b i j, b j d -> b i d", attention_probs, value_states)
hidden_states = _reshape_batch_dim_to_heads(hidden_states, heads)
return hidden_states
def _cudnn_flash_attention(query: Array, key: Array, value: Array, heads: int, mesh: Mesh, dpa_layer: Callable) -> Array:
"""CUDNN Flash Attention with Transformer Engine.
1. Stable API, supports GQA
2. Supports head_dim till 128; head_dim=256 support will be added soon
"""
# These imports are only meant to work in a GPU build.
# copied from tpu_flash_attention
query = _reshape_data_for_cudnn_flash(query, heads)
key = _reshape_data_for_cudnn_flash(key, heads)
value = _reshape_data_for_cudnn_flash(value, heads)
cudnn_flash_axis_names = (BATCH, LENGTH, HEAD, D_KV)
axis_names = nn.logical_to_mesh_axes(cudnn_flash_axis_names)
query = nn.with_logical_constraint(query, axis_names)
key = nn.with_logical_constraint(key, axis_names)
value = nn.with_logical_constraint(value, axis_names)
@functools.partial(
shard_map.shard_map,
mesh=mesh,
in_specs=(axis_names, axis_names, axis_names),
out_specs=axis_names,
check_rep=False,
)
def wrap_flash_attention(query, key, value):
return jax.vmap(dpa_layer)(query, key, value, mask=None)
out = wrap_flash_attention(query, key, value)
return _reshape_data_from_cudnn_flash(out)
def _apply_attention(
query: Array,
key: Array,
value: Array,
heads: int,
dim_head: int,
split_head_dim: bool,
float32_qk_product: bool,
attention_kernel: str,
flash_min_seq_length: int,
use_memory_efficient_attention: bool,
scale: float,
dtype: jnp.dtype,
mesh: Mesh,
axis_names_q: AxisNames,
axis_names_kv: AxisNames,
flash_block_sizes: BlockSizes,
dpa_layer: Callable,
):
"""Routes to different attention kernels."""
_check_attention_inputs(query, key, value)
seq_len_idx = 1
if query.ndim == 4:
seq_len_idx = 2
if attention_kernel == "flash":
can_use_flash_attention = (
query.shape[seq_len_idx] >= flash_min_seq_length
and key.shape[seq_len_idx] >= flash_min_seq_length
and value.shape[seq_len_idx] >= flash_min_seq_length
)
else:
can_use_flash_attention = True
if attention_kernel == "dot_product" or use_memory_efficient_attention or not can_use_flash_attention:
return _apply_attention_dot(
query, key, value, dtype, heads, dim_head, scale, split_head_dim, float32_qk_product, use_memory_efficient_attention
)
elif attention_kernel == "flash":
return _tpu_flash_attention(
query, key * scale, value, heads, mesh, axis_names_q, axis_names_kv, flash_block_sizes, dtype
)
elif attention_kernel == "ring":
return _tpu_flash_attention(
query, key * scale, value, heads, mesh, axis_names_q, axis_names_kv, flash_block_sizes, dtype, attention_kernel
)
elif attention_kernel == "cudnn_flash_te":
return _cudnn_flash_attention(query, key, value, heads, mesh, dpa_layer)
else:
raise ValueError(f"Unexpected attention kernel {attention_kernel=}.")
def _query_chunk_attention(query, key, value, precision, key_chunk_size: int = 4096):
"""Multi-head dot product attention with a limited number of queries."""
num_kv, num_heads, k_features = key.shape[-3:]
v_features = value.shape[-1]
key_chunk_size = min(key_chunk_size, num_kv)
query = query / jnp.sqrt(k_features)
@functools.partial(jax.checkpoint, prevent_cse=False)
def summarize_chunk(query, key, value):
attn_weights = jnp.einsum("...qhd,...khd->...qhk", query, key, precision=precision)
max_score = jnp.max(attn_weights, axis=-1, keepdims=True)
max_score = jax.lax.stop_gradient(max_score)
exp_weights = jnp.exp(attn_weights - max_score)
exp_values = jnp.einsum("...vhf,...qhv->...qhf", value, exp_weights, precision=precision)
max_score = jnp.einsum("...qhk->...qh", max_score)
return (exp_values, exp_weights.sum(axis=-1), max_score)
def chunk_scanner(chunk_idx):
# julienne key array
key_chunk = jax.lax.dynamic_slice(
operand=key,
start_indices=[0] * (key.ndim - 3) + [chunk_idx, 0, 0], # [...,k,h,d]
slice_sizes=list(key.shape[:-3]) + [key_chunk_size, num_heads, k_features], # [...,k,h,d]
)
# julienne value array
value_chunk = jax.lax.dynamic_slice(
operand=value,
start_indices=[0] * (value.ndim - 3) + [chunk_idx, 0, 0], # [...,v,h,d]
slice_sizes=list(value.shape[:-3]) + [key_chunk_size, num_heads, v_features], # [...,v,h,d]
)
return summarize_chunk(query, key_chunk, value_chunk)
chunk_values, chunk_weights, chunk_max = jax.lax.map(f=chunk_scanner, xs=jnp.arange(0, num_kv, key_chunk_size))
global_max = jnp.max(chunk_max, axis=0, keepdims=True)
max_diffs = jnp.exp(chunk_max - global_max)
chunk_values *= jnp.expand_dims(max_diffs, axis=-1)
chunk_weights *= max_diffs
all_values = chunk_values.sum(axis=0)
all_weights = jnp.expand_dims(chunk_weights, -1).sum(axis=0)
return all_values / all_weights
def jax_memory_efficient_attention(
query, key, value, precision=jax.lax.Precision.HIGHEST, query_chunk_size: int = 1024, key_chunk_size: int = 4096
):
r"""
Flax Memory-efficient multi-head dot product attention. https://arxiv.org/abs/2112.05682v2
https://github.com/AminRezaei0x443/memory-efficient-attention
Args:
query (`jnp.ndarray`): (batch..., query_length, head, query_key_depth_per_head)
key (`jnp.ndarray`): (batch..., key_value_length, head, query_key_depth_per_head)
value (`jnp.ndarray`): (batch..., key_value_length, head, value_depth_per_head)
precision (`jax.lax.Precision`, *optional*, defaults to `jax.lax.Precision.HIGHEST`):
numerical precision for computation
query_chunk_size (`int`, *optional*, defaults to 1024):
chunk size to divide query array value must divide query_length equally without remainder
key_chunk_size (`int`, *optional*, defaults to 4096):
chunk size to divide key and value array value must divide key_value_length equally without remainder
Returns:
(`jnp.ndarray`) with shape of (batch..., query_length, head, value_depth_per_head)
"""
num_q, num_heads, q_features = query.shape[-3:]
def chunk_scanner(chunk_idx, _):
# julienne query array
query_chunk = jax.lax.dynamic_slice(
operand=query,
start_indices=([0] * (query.ndim - 3)) + [chunk_idx, 0, 0], # [...,q,h,d]
slice_sizes=list(query.shape[:-3]) + [min(query_chunk_size, num_q), num_heads, q_features], # [...,q,h,d]
)
return (
chunk_idx + query_chunk_size, # unused ignore it
_query_chunk_attention(query=query_chunk, key=key, value=value, precision=precision, key_chunk_size=key_chunk_size),
)
_, res = jax.lax.scan(
f=chunk_scanner, init=0, xs=None, length=math.ceil(num_q / query_chunk_size) # start counter # stop counter
)
return jnp.concatenate(res, axis=-3) # fuse the chunked result back
def apply_rope(xq: Array, xk: Array, freqs_cis: Array) -> tuple[Array, Array]:
xq_ = xq.reshape(*xq.shape[:-1], -1, 1, 2)
xk_ = xk.reshape(*xk.shape[:-1], -1, 1, 2)
xq_out = freqs_cis[..., 0] * xq_[..., 0] + freqs_cis[..., 1] * xq_[..., 1]
xk_out = freqs_cis[..., 0] * xk_[..., 0] + freqs_cis[..., 1] * xk_[..., 1]
return xq_out.reshape(*xq.shape).astype(xq.dtype), xk_out.reshape(*xk.shape).astype(xk.dtype)
class NNXAttentionOp(nnx.Module):
def __init__(
self,
mesh: Mesh,
attention_kernel: str,
scale: int,
heads: int,
dim_head: int,
use_memory_efficient_attention: bool = False,
split_head_dim: bool = True,
float32_qk_product: bool = True,
axis_names_q: AxisNames = (BATCH, HEAD, LENGTH, D_KV),
axis_names_kv: AxisNames = (BATCH, HEAD, KV_LENGTH, D_KV),
# Uses splash attention on cross attention.
flash_min_seq_length: int = 0,
flash_block_sizes: BlockSizes = None,
dtype: DType = jnp.float32,
quant: Quant = None,
):
self.dpa_layer = None
if attention_kernel == "cudnn_flash_te":
raise NotImplementedError(f"{self} has not been tested with {attention_kernel}")
self.mesh = mesh
self.scale = scale
self.heads = heads
self.dim_head = dim_head
self.attention_kernel = attention_kernel
self.use_memory_efficient_attention = use_memory_efficient_attention
self.split_head_dim = split_head_dim
self.float32_qk_product = float32_qk_product
self.axis_names_q = axis_names_q
self.axis_names_kv = axis_names_kv
self.flash_min_seq_length = flash_min_seq_length
self.flash_block_sizes = flash_block_sizes
self.dtype = dtype
self.quant = quant
def apply_attention(self, query: Array, key: Array, value: Array):
return _apply_attention(
query=query,
key=key,
value=value,
heads=self.heads,
dim_head=self.dim_head,
split_head_dim=self.split_head_dim,
float32_qk_product=self.float32_qk_product,
attention_kernel=self.attention_kernel,
flash_min_seq_length=self.flash_min_seq_length,
use_memory_efficient_attention=self.use_memory_efficient_attention,
scale=self.scale,
dtype=self.dtype,
mesh=self.mesh,
axis_names_q=self.axis_names_q,
axis_names_kv=self.axis_names_kv,
flash_block_sizes=self.flash_block_sizes,
dpa_layer=self.dpa_layer,
)
class AttentionOp(nn.Module):
mesh: Mesh
attention_kernel: str
scale: int
heads: int
dim_head: int
use_memory_efficient_attention: bool = False
split_head_dim: bool = False
float32_qk_product: bool = True
axis_names_q: AxisNames = (BATCH, HEAD, LENGTH, D_KV)
axis_names_kv: AxisNames = (BATCH, HEAD, KV_LENGTH, D_KV)
flash_min_seq_length: int = 4096
flash_block_sizes: BlockSizes = None
dtype: DType = jnp.float32
quant: Quant = None
def setup(self):
self.dpa_layer = None
if self.attention_kernel == "cudnn_flash_te":
from transformer_engine.jax.flax.transformer import DotProductAttention # pytype: disable=import-error
self.dpa_layer = DotProductAttention(
head_dim=self.dim_head,
num_attention_heads=self.heads,
num_gqa_groups=self.heads,
attn_mask_type="no_mask", # 'no_mask', 'padding', 'causal', or 'padding_causal'
attn_bias_type="NO_BIAS", # 'no_bias', 'pre_scale_bias' or 'post_scale_bias'
# attention_dropout=self.dropout_rate,
dropout_rng_name="aqt",
dtype=self.dtype,
# float32_logits=self.float32_logits,
qkv_layout="BSHD_BSHD_BSHD", # 'BS3HD', 'BSHD_BS2HD' or 'BSHD_BSHD_BSHD'
scale_factor=self.scale,
transpose_batch_sequence=False,
)
def apply_attention(self, query: Array, key: Array, value: Array):
return _apply_attention(
query=query,
key=key,
value=value,
heads=self.heads,
dim_head=self.dim_head,
split_head_dim=self.split_head_dim,
float32_qk_product=self.float32_qk_product,
attention_kernel=self.attention_kernel,
flash_min_seq_length=self.flash_min_seq_length,
use_memory_efficient_attention=self.use_memory_efficient_attention,
scale=self.scale,
dtype=self.dtype,
mesh=self.mesh,
axis_names_q=self.axis_names_q,
axis_names_kv=self.axis_names_kv,
flash_block_sizes=self.flash_block_sizes,
dpa_layer=self.dpa_layer,
)
class FlaxWanAttention(nnx.Module):
def __init__(
self,
rngs: nnx.Rngs,
query_dim: int,
cross_attention_dim: Optional[int] = None,
heads: int = 8,
dim_head: int = 64,
dropout: float = 0.0,
eps: float = 1e-6,
qk_norm: str = "rms_norm_across_heads",
use_memory_efficient_attention: bool = False,
split_head_dim: bool = False,
attention_kernel: str = "flash",
flash_min_seq_length: int = 0,
flash_block_sizes: BlockSizes = None,
mesh: jax.sharding.Mesh = None,
dtype: jnp.dtype = jnp.float32,
weights_dtype: jnp.dtype = jnp.float32,
query_axis_names: AxisNames = (BATCH, LENGTH, HEAD),
key_axis_names: AxisNames = (BATCH, LENGTH, HEAD),
value_axis_names: AxisNames = (BATCH, LENGTH, HEAD),
out_axis_names: AxisNames = (BATCH, LENGTH, EMBED),
precision: jax.lax.Precision = None,
qkv_bias: bool = False,
quant: Quant = None,
):
if attention_kernel == "cudnn_flash_te":
raise NotImplementedError(f"Wan 2.1 has not been tested with {attention_kernel}")
if attention_kernel in {"flash", "cudnn_flash_te"} and mesh is None:
raise ValueError(f"The flash attention kernel requires a value for mesh, but mesh is {self.mesh}")
self.dim_head = dim_head
self.heads = heads
self.inner_dim = dim_head * heads
scale = dim_head**-0.5
self.qk_norm = qk_norm
self.query_axis_names = query_axis_names
self.key_axis_names = key_axis_names
self.value_axis_names = value_axis_names
self.out_axis_names = out_axis_names
self.attention_op = NNXAttentionOp(
mesh=mesh,
attention_kernel=attention_kernel,
scale=scale,
heads=heads,
dim_head=dim_head,
use_memory_efficient_attention=use_memory_efficient_attention,
split_head_dim=split_head_dim,
float32_qk_product=False,
flash_min_seq_length=flash_min_seq_length,
flash_block_sizes=flash_block_sizes,
dtype=dtype,
quant=quant,
)
# None axes corresponds to the stacked weights across all blocks
# because of the use of nnx.vmap and nnx.scan.
# Dims are [num_blocks, embed, heads]
kernel_axes = (None, "embed", "heads")
qkv_init_kernel = nnx.with_partitioning(nnx.initializers.lecun_normal(), kernel_axes)
self.query = nnx.Linear(
rngs=rngs,
in_features=self.inner_dim,
out_features=self.inner_dim,
kernel_init=qkv_init_kernel,
dtype=dtype,
param_dtype=weights_dtype,
precision=precision,
bias_init=nnx.with_partitioning(
nnx.initializers.zeros,
(
None,
"embed",
),
),
)
self.key = nnx.Linear(
rngs=rngs,
in_features=self.inner_dim,
out_features=self.inner_dim,
kernel_init=qkv_init_kernel,
dtype=dtype,
param_dtype=weights_dtype,
precision=precision,
bias_init=nnx.with_partitioning(
nnx.initializers.zeros,
(
None,
"embed",
),
),
)
self.value = nnx.Linear(
rngs=rngs,
in_features=self.inner_dim,
out_features=self.inner_dim,
kernel_init=qkv_init_kernel,
dtype=dtype,
param_dtype=weights_dtype,
precision=precision,
bias_init=nnx.with_partitioning(
nnx.initializers.zeros,
(
None,
"embed",
),
),
)
self.proj_attn = nnx.Linear(
rngs=rngs,
in_features=self.inner_dim,
out_features=self.inner_dim,
kernel_init=nnx.with_partitioning(nnx.initializers.lecun_normal(), (None, "heads", "embed")),
dtype=dtype,
param_dtype=weights_dtype,
precision=precision,
)
self.norm_q = None
self.norm_k = None
if qk_norm is not None:
self.norm_q = nnx.RMSNorm(
num_features=self.inner_dim,
rngs=rngs,
epsilon=eps,
dtype=dtype,
scale_init=nnx.with_partitioning(
nnx.initializers.ones,
(
None,
"norm",
),
),
param_dtype=weights_dtype,
)
self.norm_k = nnx.RMSNorm(
num_features=self.inner_dim,
rngs=rngs,
dtype=dtype,
scale_init=nnx.with_partitioning(
nnx.initializers.ones,
(
None,
"norm",
),
),
param_dtype=weights_dtype,
)
def _apply_rope(self, xq: jax.Array, xk: jax.Array, freqs_cis: jax.Array) -> Tuple[jax.Array, jax.Array]:
dtype = xq.dtype
reshape_xq = xq.astype(jnp.float32).reshape(*xq.shape[:-1], -1, 2)
reshape_xk = xk.astype(jnp.float32).reshape(*xk.shape[:-1], -1, 2)
xq_ = jax.lax.complex(reshape_xq[..., 0], reshape_xq[..., 1])
xk_ = jax.lax.complex(reshape_xk[..., 0], reshape_xk[..., 1])
xq_out_complex = xq_ * freqs_cis
xk_out_complex = xk_ * freqs_cis
xq_out = jnp.stack([jnp.real(xq_out_complex), jnp.imag(xq_out_complex)], axis=-1).reshape(xq.shape).astype(dtype)
xk_out = jnp.stack([jnp.real(xk_out_complex), jnp.imag(xk_out_complex)], axis=-1).reshape(xk.shape).astype(dtype)
return xq_out, xk_out
def __call__(
self, hidden_states: jax.Array, encoder_hidden_states: jax.Array = None, rotary_emb: Optional[jax.Array] = None
) -> jax.Array:
hidden_states = jax.lax.with_sharding_constraint(hidden_states, PartitionSpec("data", "fsdp", "tensor"))
encoder_hidden_states = jax.lax.with_sharding_constraint(encoder_hidden_states, PartitionSpec("data", "fsdp", "tensor"))
dtype = hidden_states.dtype
if encoder_hidden_states is None:
encoder_hidden_states = hidden_states
query_proj = self.query(hidden_states)
key_proj = self.key(encoder_hidden_states)
value_proj = self.value(encoder_hidden_states)
if self.qk_norm:
query_proj = self.norm_q(query_proj)
key_proj = self.norm_k(key_proj)
if rotary_emb is not None:
query_proj = _unflatten_heads(query_proj, self.heads)
key_proj = _unflatten_heads(key_proj, self.heads)
value_proj = _unflatten_heads(value_proj, self.heads)
# output of _unflatten_heads Batch, heads, seq_len, head_dim
query_proj, key_proj = self._apply_rope(query_proj, key_proj, rotary_emb)
query_proj = checkpoint_name(query_proj, "query_proj")
key_proj = checkpoint_name(key_proj, "key_proj")
value_proj = checkpoint_name(value_proj, "value_proj")
attn_output = self.attention_op.apply_attention(query_proj, key_proj, value_proj)
attn_output = attn_output.astype(dtype=dtype)
attn_output = checkpoint_name(attn_output, "attn_output")
hidden_states = self.proj_attn(attn_output)
return hidden_states
class FlaxFluxAttention(nn.Module):
query_dim: int
heads: int = 8
dim_head: int = 64
dropout: float = 0.0
use_memory_efficient_attention: bool = False
split_head_dim: bool = False
attention_kernel: str = "dot_product"
flash_min_seq_length: int = 4096
flash_block_sizes: BlockSizes = None
mesh: jax.sharding.Mesh = None
dtype: jnp.dtype = jnp.float32
weights_dtype: jnp.dtype = jnp.float32
query_axis_names: AxisNames = (BATCH, LENGTH, HEAD)
key_axis_names: AxisNames = (BATCH, LENGTH, HEAD)
value_axis_names: AxisNames = (BATCH, LENGTH, HEAD)
out_axis_names: AxisNames = (BATCH, LENGTH, EMBED)
precision: jax.lax.Precision = None
qkv_bias: bool = False
def setup(self):
if self.attention_kernel in {"flash", "cudnn_flash_te"} and self.mesh is None:
raise ValueError(f"The flash attention kernel requires a value for mesh, but mesh is {self.mesh}")
inner_dim = self.dim_head * self.heads
scale = self.dim_head**-0.5
self.attention_op = AttentionOp(
mesh=self.mesh,
attention_kernel=self.attention_kernel,
scale=scale,
heads=self.heads,
dim_head=self.dim_head,
flash_min_seq_length=self.flash_min_seq_length,
use_memory_efficient_attention=self.use_memory_efficient_attention,
split_head_dim=self.split_head_dim,
flash_block_sizes=self.flash_block_sizes,
dtype=self.dtype,
float32_qk_product=False,
)
kernel_axes = ("embed", "heads")
qkv_init_kernel = nn.with_logical_partitioning(nn.initializers.lecun_normal(), kernel_axes)
self.qkv = nn.Dense(
inner_dim * 3,
kernel_init=qkv_init_kernel,
use_bias=self.qkv_bias,
bias_init=nn.with_logical_partitioning(nn.initializers.zeros, ("heads",)),
dtype=self.dtype,
param_dtype=self.weights_dtype,
name="i_qkv",
precision=self.precision,
)
self.encoder_qkv = nn.Dense(
inner_dim * 3,
kernel_init=qkv_init_kernel,
use_bias=self.qkv_bias,
bias_init=nn.with_logical_partitioning(nn.initializers.zeros, ("heads",)),
dtype=self.dtype,
param_dtype=self.weights_dtype,
name="e_qkv",
precision=self.precision,
)
self.proj_attn = nn.Dense(
self.query_dim,
kernel_init=nn.with_logical_partitioning(nn.initializers.lecun_normal(), kernel_axes),
use_bias=True,
bias_init=nn.with_logical_partitioning(nn.initializers.zeros, ("heads",)),
dtype=self.dtype,
param_dtype=self.weights_dtype,
name="i_proj",
precision=self.precision,
)
self.encoder_proj_attn = nn.Dense(
self.query_dim,
kernel_init=nn.with_logical_partitioning(nn.initializers.lecun_normal(), kernel_axes),
use_bias=True,
bias_init=nn.with_logical_partitioning(nn.initializers.zeros, ("heads",)),
dtype=self.dtype,
param_dtype=self.weights_dtype,
name="e_proj",
precision=self.precision,
)
self.query_norm = nn.RMSNorm(
dtype=self.dtype,
scale_init=nn.with_logical_partitioning(nn.initializers.ones, ("heads",)),
param_dtype=self.weights_dtype,
)
self.key_norm = nn.RMSNorm(
dtype=self.dtype,
scale_init=nn.with_logical_partitioning(nn.initializers.ones, ("heads",)),
param_dtype=self.weights_dtype,
)
self.encoder_query_norm = nn.RMSNorm(
dtype=self.dtype,
scale_init=nn.with_logical_partitioning(nn.initializers.ones, ("heads",)),
param_dtype=self.weights_dtype,
)
self.encoder_key_norm = nn.RMSNorm(
dtype=self.dtype,
scale_init=nn.with_logical_partitioning(nn.initializers.ones, ("heads",)),
param_dtype=self.weights_dtype,
)
def __call__(self, hidden_states, encoder_hidden_states=None, attention_mask=None, image_rotary_emb=None):
qkv_proj = self.qkv(hidden_states)
B, L = hidden_states.shape[:2]
H, D, K = self.heads, qkv_proj.shape[-1] // (self.heads * 3), 3
qkv_proj = qkv_proj.reshape(B, L, K, H, D).transpose(2, 0, 3, 1, 4)
query_proj, key_proj, value_proj = qkv_proj
query_proj = self.query_norm(query_proj)