Skip to content

fix(attention): dot-product fallback layout for 4-D inputs; tile_size_grid_search iters/bkv_compute guards - #507

Open
Perseus14 wants to merge 1 commit into
feat/fixed-m-kernelfrom
feat/attn-dot-fallback-fixes
Open

Perseus14 wants to merge 1 commit into
feat/fixed-m-kernelfrom
feat/attn-dot-fallback-fixes

Conversation

@Perseus14

@Perseus14 Perseus14 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #477 (feat/fixed-m-kernel). Two small fixes peeled out of #478 so they can be reviewed on their own:

  1. _apply_attention_dot: sequences below flash_min_seq_length take the dot-product fallback, and callers that apply RoPE hand it [B, H, S, D], but the split_head_dim path reshaped that as if it were [B, S, H*D] — same element count, so it silently returned wrong values. 4-D inputs are now transposed (_to_bshd); the flat 3-D contract is unchanged, an optional kv_heads repeats K/V on the head axis after the transpose (for both split_head_dim=True and False), and heads % kv_heads != 0 raises ValueError.
  2. tile_size_grid_search: time_callable rejects iters < 1, and _broadcast_winner round-trips bkv_compute=None through the multihost broadcast.

Tests: dot_fallback_layout_test.py (6, new, CPU — includes the counterexample, split_head_dim in {True, False}, the heads % kv_heads guard, and a spied _apply_attention dispatcher test) and tile_size_grid_search_test.py (+2); both added to end_to_end/tpu/run_wan_stack_tests.sh.

E2E latency (TPU v6e-8, Wan 2.2 T2V-A14B, 720p / 81 frames / 40 steps, CP=4, DP=2, 1D Ulysses U=4, DVFS unpinned): 138.5 s generate / 136.4 s denoise (neutral vs #477's 139.3 s / 136.6 s; correctness & guard PR).

Stack: #477 → #507 → #478 → #508 → #479 → #488 → #509 → #510 → #511 → #491

@Perseus14
Perseus14 requested a review from entrpn as a code owner October 15, 2026 15:54
@github-actions

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a silent data corruption bug in the short-sequence dot-product fallback path (_apply_attention_dot) by correctly transposing 4-D inputs instead of reshaping them, and adds support for Grouped-Query Attention (GQA) via kv_heads. It also fixes an issue in the tile-size grid-search utility when bkv_compute is None and adds corresponding unit tests. The review feedback identifies a critical issue where kv_heads is missing from the signature and context of _apply_attention, making the GQA fallback path unreachable in practice. Additionally, it is recommended to add defensive checks to ensure heads and effective_kv_heads are positive integers to prevent potential division-by-zero errors.

Comment thread src/maxdiffusion/models/attention_flax.py
Comment thread src/maxdiffusion/models/attention_flax.py
@Perseus14
Perseus14 added this pull request to stack #512 October 15, 2026 16:00
…_grid_search iters/bkv_compute guards

Two small fixes peeled out of the Ulysses x Ring PR so that they can be
reviewed on their own.

Dot-product fallback (`_apply_attention_dot`)
- Sequences below `flash_min_seq_length` bypass the flash/ulysses kernels and
  run the dot-product fallback. Callers that apply rotary embeddings hand the
  dispatcher `[B, H, S, D]` (the dispatcher reads S from axis 2 when
  `ndim == 4`), but the `split_head_dim` path reshaped those tensors as if
  they were `[B, S, H*D]`. Both layouts hold the same number of elements, so
  the reshape succeeded and returned a wrong answer silently. 4-D inputs are
  now transposed to `[B, S, H, D]` (`_to_bshd`); the flat 3-D contract is
  unchanged.
- `kv_heads` (optional, default None) is accepted so that K/V with fewer heads
  than Q are repeated on the head axis after the transpose, on both the
  `split_head_dim` and batched-heads paths; `heads % kv_heads != 0` raises
  ValueError instead of silently truncating the repeat factor.
  `dot_product_kernel` forwards `context.get("kv_heads")`.

tile_size_grid_search
- `time_callable` rejects `iters < 1` instead of dividing by zero.
- `_broadcast_winner` encodes `bkv_compute=None` as -1 in the broadcast
  payload and decodes it back, so a winner without an explicit compute block
  is found again on the non-source hosts.

Tests
- dot_fallback_layout_test.py (6, new, CPU): the reviewer's counterexample
  (uniform softmax must give per-head token means), random 4-D inputs vs a
  dense reference and GQA repeat on 4-D (both over split_head_dim in
  {True, False}), 3-D/4-D agreement, the heads % kv_heads guard, and the
  dispatcher seq-axis contract: `_apply_attention` on a [B, H=128, S=32, D]
  query with flash_min_seq_length=64 must route to a spied dot_product kernel
  (S read from axis 2) that then returns the dense reference.
- tile_size_grid_search_test.py (16, +2): `iters=0` raises; `_broadcast_winner`
  round-trips `bkv_compute=None`.
- Both files are added to end_to_end/tpu/run_wan_stack_tests.sh.
Verified (this head): TPU v6e-8 (jax 0.11.2), every file of
end_to_end/tpu/run_wan_stack_tests.sh at this head run directly with unittest:
custom_splash_fixed_m 31, ring_fixed_m 18, dot_fallback_layout 6,
tile_size_grid_search 16 -- 71 tests, all OK.
@Perseus14
Perseus14 force-pushed the feat/attn-dot-fallback-fixes branch from 167b48c to ee90fa1 Compare October 15, 2026 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant