Skip to content

Commit e665b2c

Browse files
committed
Refactor bkv_compute_in into _BlockSizes
This change reduces parameter clutter by keeping all flash block size configuration grouped together in the _BlockSizes data class. make_splash_mha, _splash_attention_forward, and _splash_attention_forward_mhpt now extract it natively.
1 parent 995a4b4 commit e665b2c

2 files changed

Lines changed: 6 additions & 9 deletions

File tree

src/maxdiffusion/kernels/custom_splash_attention.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@
3535

3636

3737
class _BlockSizes:
38-
__slots__ = ("block_q", "block_kv", "block_kv_compute")
38+
__slots__ = ("block_q", "block_kv", "block_kv_compute", "block_kv_compute_in")
3939

40-
def __init__(self, block_q: int, block_kv: int, block_kv_compute: int | None = None):
40+
def __init__(self, block_q: int, block_kv: int, block_kv_compute: int | None = None, block_kv_compute_in: int = DEFAULT_BKVCOMPUTEINSIZE):
4141
self.block_q = block_q
4242
self.block_kv = block_kv
4343
self.block_kv_compute = block_kv_compute if block_kv_compute is not None else block_kv
44+
self.block_kv_compute_in = block_kv_compute_in
4445

4546

4647
def _flash_attention_kernel(
@@ -342,7 +343,6 @@ def _splash_attention_forward(
342343
k: jax.Array,
343344
v: jax.Array,
344345
block_sizes: _BlockSizes,
345-
bkv_compute_in: int,
346346
q_seq_len: int | None = None,
347347
kv_seq_len: int | None = None,
348348
use_base2_exp: bool = True,
@@ -353,6 +353,7 @@ def _splash_attention_forward(
353353
head_dim_v = v.shape[-1]
354354
bq, bkv = block_sizes.block_q, block_sizes.block_kv
355355
bkv_compute = block_sizes.block_kv_compute
356+
bkv_compute_in = block_sizes.block_kv_compute_in
356357
num_kv_heads = k.shape[0]
357358
padded_kv_seq_len = k.shape[1]
358359

@@ -428,7 +429,6 @@ def _splash_attention_forward_mhpt(
428429
k: jax.Array,
429430
v: jax.Array,
430431
block_sizes: _BlockSizes,
431-
bkv_compute_in: int,
432432
heads_per_tile: int,
433433
q_seq_len: int | None = None,
434434
kv_seq_len: int | None = None,
@@ -440,6 +440,7 @@ def _splash_attention_forward_mhpt(
440440
head_dim_v = v.shape[-1]
441441
bq, bkv = block_sizes.block_q, block_sizes.block_kv
442442
bkv_compute = block_sizes.block_kv_compute
443+
bkv_compute_in = block_sizes.block_kv_compute_in
443444
num_kv_heads = k.shape[0]
444445
actual_q_seq_len = q_seq_len if q_seq_len is not None else padded_q_seq_len
445446
actual_kv_seq_len = kv_seq_len if kv_seq_len is not None else k.shape[1]
@@ -514,7 +515,6 @@ def out_index_map(h, i, j, *_):
514515

515516
def make_splash_mha(
516517
block_sizes: _BlockSizes,
517-
bkv_compute_in: int = DEFAULT_BKVCOMPUTEINSIZE,
518518
orig_q_seq_len: int | None = None,
519519
orig_kv_seq_len: int | None = None,
520520
heads_per_tile: int = 1,
@@ -529,7 +529,6 @@ def _splash_attention(q, k, v):
529529
k,
530530
v,
531531
block_sizes,
532-
bkv_compute_in,
533532
heads_per_tile,
534533
q_seq_len=orig_q_seq_len,
535534
kv_seq_len=orig_kv_seq_len,
@@ -542,7 +541,6 @@ def _splash_attention(q, k, v):
542541
k,
543542
v,
544543
block_sizes,
545-
bkv_compute_in,
546544
q_seq_len=orig_q_seq_len,
547545
kv_seq_len=orig_kv_seq_len,
548546
use_base2_exp=use_base2_exp,

src/maxdiffusion/models/attention_flax.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,10 @@ def wrap_ulysses_attention(query, key, value):
656656
key, _, key_seq_len = _pad_data_for_flash(key, heads, bkv)
657657
value, _, _ = _pad_data_for_flash(value, heads, bkv)
658658

659-
bsizes = custom_splash._BlockSizes(block_q=bq, block_kv=bkv, block_kv_compute=bkv_compute)
659+
bsizes = custom_splash._BlockSizes(block_q=bq, block_kv=bkv, block_kv_compute=bkv_compute, block_kv_compute_in=bkv_compute_in)
660660

661661
splash_kernel = custom_splash.make_splash_mha(
662662
block_sizes=bsizes,
663-
bkv_compute_in=bkv_compute_in,
664663
orig_q_seq_len=query_seq_len,
665664
orig_kv_seq_len=key_seq_len,
666665
heads_per_tile=heads_per_tile,

0 commit comments

Comments
 (0)