Skip to content

Commit da3dbfc

Browse files
committed
Follow up fix custom flash block sizes fallback in Ulysses attention
Ensure that block sizes and heads_per_tile fall back to default values when resolved as None from CustomFlashBlockSizes dataclass. This fixes a TypeError in ulysses_custom attention when heads_per_tile is not specified.
1 parent 0956041 commit da3dbfc

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

src/maxdiffusion/models/attention_flax.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -363,24 +363,25 @@ def _extract_custom_block_sizes(flash_block_sizes):
363363
if flash_block_sizes is not None:
364364
if isinstance(flash_block_sizes, dict):
365365
get = flash_block_sizes.get
366-
bq = get("block_q", bq)
367-
bkv = get("block_kv", bkv)
368-
bkv_compute = get("block_kv_compute", bkv_compute)
369-
bkv_compute_in = get("block_kv_compute_in", bkv_compute_in)
370-
heads_per_tile = get("heads_per_tile", heads_per_tile)
371-
vmem_limit_bytes = get("vmem_limit_bytes", vmem_limit_bytes)
366+
bq = get("block_q", None) or bq
367+
bkv = get("block_kv", None) or bkv
368+
bkv_compute = get("block_kv_compute", None) or bkv_compute
369+
bkv_compute_in = get("block_kv_compute_in", None) or bkv_compute_in
370+
# A BlockSizes object carries heads_per_tile=None when the config dict omitted
371+
# it; getattr then returns that None instead of the default, so coerce it back
372+
# to 1 (the custom-kernel default) to keep the `heads_per_tile > 1` guards safe.
373+
heads_per_tile = get("heads_per_tile", None) or heads_per_tile
374+
vmem_limit_bytes = get("vmem_limit_bytes", None) or vmem_limit_bytes
372375
else:
373-
bq = getattr(flash_block_sizes, "block_q", bq)
374-
bkv = getattr(flash_block_sizes, "block_kv", bkv)
375-
bkv_compute = getattr(flash_block_sizes, "block_kv_compute", bkv_compute)
376-
bkv_compute_in = getattr(flash_block_sizes, "block_kv_compute_in", bkv_compute_in)
377-
heads_per_tile = getattr(flash_block_sizes, "heads_per_tile", heads_per_tile)
378-
vmem_limit_bytes = getattr(flash_block_sizes, "vmem_limit_bytes", vmem_limit_bytes)
379-
# A BlockSizes object carries heads_per_tile=None when the config dict omitted
380-
# it; getattr then returns that None instead of the default, so coerce it back
381-
# to 1 (the custom-kernel default) to keep the `heads_per_tile > 1` guards safe.
382-
if heads_per_tile is None:
383-
heads_per_tile = 1
376+
bq = getattr(flash_block_sizes, "block_q", None) or bq
377+
bkv = getattr(flash_block_sizes, "block_kv", None) or bkv
378+
bkv_compute = getattr(flash_block_sizes, "block_kv_compute", None) or bkv_compute
379+
bkv_compute_in = getattr(flash_block_sizes, "block_kv_compute_in", None) or bkv_compute_in
380+
# A BlockSizes object carries heads_per_tile=None when the config dict omitted
381+
# it; getattr then returns that None instead of the default, so coerce it back
382+
# to 1 (the custom-kernel default) to keep the `heads_per_tile > 1` guards safe.
383+
heads_per_tile = getattr(flash_block_sizes, "heads_per_tile", None) or heads_per_tile
384+
vmem_limit_bytes = getattr(flash_block_sizes, "vmem_limit_bytes", None) or vmem_limit_bytes
384385
return bq, bkv, bkv_compute, bkv_compute_in, heads_per_tile, vmem_limit_bytes
385386

386387

@@ -1113,9 +1114,7 @@ def wrap_ulysses_ring_attention(query, key, value):
11131114
use_fixed_m=use_fixed_m,
11141115
)
11151116
if use_fixed_m:
1116-
attention_output = jnp.swapaxes(
1117-
jax.vmap(splash_kernel, in_axes=(0, 0, 0, None))(query, key, value, mk_arr), 2, 3
1118-
)
1117+
attention_output = jnp.swapaxes(jax.vmap(splash_kernel, in_axes=(0, 0, 0, None))(query, key, value, mk_arr), 2, 3)
11191118
else:
11201119
attention_output = jnp.swapaxes(jax.vmap(splash_kernel, in_axes=(0, 0, 0))(query, key, value), 2, 3)
11211120
else:

0 commit comments

Comments
 (0)