Skip to content

Commit b47433e

Browse files
committed
docs(splash-ring): document mhpt kernel + dispatch design rationale
1 parent 049210a commit b47433e

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/maxdiffusion/kernels/splash_attention/splash_attention_kernel.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,21 @@ def flash_attention_kernel_mhpt(
526526
heads_per_tile: int,
527527
config: SplashConfig,
528528
):
529+
"""Multi-head-per-tile (mhpt) forward kernel for ring splash attention.
530+
531+
Processes `heads_per_tile` heads per grid program (a Python loop over `h_local`)
532+
instead of one, amortizing per-tile launch/prefetch/pipelining overhead. Each head
533+
keeps its own online-softmax scratch slice `(heads_per_tile, bq, ...)` and, on the
534+
last kv step, writes the raw residuals (`o`, `l_linear`, `max_logits`) that the ring
535+
wrapper merges across shards.
536+
537+
This is a lean, dedicated kernel rather than a branch inside `flash_attention_kernel`
538+
on purpose: mhpt supports only a narrow regime (full MHA, static FullMask,
539+
HEAD_DIM_MINOR, no sinks / max-logit / soft-cap), so keeping it separate avoids adding
540+
a head loop crossed with all of `flash_attention_kernel`'s branches to the hot path.
541+
Mirrors `custom_splash_attention._flash_attention_kernel_mhpt` (the non-ring mhpt path).
542+
Supported regime is enforced by guards in `_splash_attention_forward_ring_raw`.
543+
"""
529544
del active_rows_ref, active_cols_ref, mask_next_ref
530545
del bounds_start_ref, bounds_end_ref, block_mask_ref
531546
del sinks_ref, mask_ref, q_sequence_ref, max_logit_value_ref
@@ -1253,6 +1268,11 @@ def _fwd_cost_estimate(
12531268
grid = (head_programs, kv_steps * (q_seq_len // bq))
12541269
is_empty_attention_block = False
12551270

1271+
# Dispatch to the dedicated mhpt kernel when tiling >1 head per program. The mhpt
1272+
# path is a guarded branch *inside* this forward (rather than a separate forward like
1273+
# custom_splash_attention._splash_attention_forward_mhpt) so it reuses all the shared
1274+
# setup below/above -- index maps, in/out specs, mask & segment plumbing -- and only
1275+
# the kernel fn, scratch shapes, and head-dim block size differ.
12561276
kernel = flash_attention_kernel_mhpt if use_heads_per_tile else flash_attention_kernel
12571277
kernel_kwargs = {
12581278
"mask_value": mask_value,

0 commit comments

Comments
 (0)