Skip to content

Commit d4d6ff6

Browse files
committed
feat(attention): 2D Ulysses+Ring custom attention with fixed-m (pre-a2a norm reduction, unpadded ring K/V)
Adds fixed-m to the 2D Ulysses + Ring custom-kernel path (`_ulysses_ring_custom_attention`, attention=ulysses_ring_custom_fixed_m and the new ulysses_ring_custom_fixed_m_per_q_block), plus the supporting guards and GQA plumbing it needed. (The dot-product fallback layout fix and the tile_size_grid_search guards are in feat/attn-dot-fallback-fixes below this PR; the fused RMSNorm+RoPE producer is in feat/fused-rmsnorm-rope-xla above it.) Ring fixed-m (R > 1) - `_ring_fixed_m_norms_pre_a2a` computes every fixed-m input on the PRE-a2a shards (each device holds all heads x 1/(U*R) of the sequence): Q row norms, K max norms and the V max are reduced in ONE pmax over (ulysses, ring), then sliced to the heads this Ulysses rank owns after the a2a. Keeping these off the post-a2a layout lets them overlap the all-to-all instead of sitting between it and the `lax.cond`. With per_q_block=True a small ulysses-axis all_to_all of the Q row norms is also issued. - V safety: `v_ok = (max|V|^2 from that pmax <= 256^2) & dtype_safe`. It is derived from the pmax of V norms, so it is bit-identical on every (ulysses, ring) rank with no further collective. (The ring kernel's own pmin, which reduces global-bound eligibility, only runs when the caller omits `all_fixed_global`; the production caller always passes it.) - `all_fixed_global = all(qn * kn <= bound(N_total)) & v_ok`, also reduced over batch, is passed to the ring kernel and is the top-level accumulate-vs-LSE `lax.cond` predicate (`pregathered_mk=True`). It is mesh-uniform by construction. Blast radius: one ineligible head / Q-block on any device sends every rank to `_lse_scan` for that layer; per_q_block=True only refines dispatch inside the LSE path. - Only Q and K go through the optimization_barrier that stops XLA duplicating their producer chains into the norm reductions; V is left out so the Q/K a2a does not wait on V's producer. - Ring K/V are passed unpadded when the ring-shard KV length is 8-aligned (`kv_pad_size=1`; the kernel slices the tail, see custom_splash_unpadded_test). - Q * log2(e) is applied before the Ulysses a2a (exact; fuses into Q's producer instead of being wrapped in relayout copies after the collective). Virtual K-centering - Centering is virtual on both R == 1 and R > 1: k_mean goes to the kernel and q . k_bar is added to the fixed bound in VMEM; K is never re-written. For R > 1, k_mean = pmean(mean(K)) over (ulysses, ring) on the pre-a2a shards, and K norms are taken around that global mean. - Controlled by `use_k_centering` ("auto" | bool, new key in base_wan_27b.yml, defaulted in pyconfig). "auto" (`resolve_k_centering`) is ON for non-ring ulysses_custom_fixed_m* and OFF on every ring path, R == 1 included. Other changes - GQA: kv_heads plumbed through the custom Ulysses / ring paths; flash, tokamax, non-custom ulysses and ulysses_ring raise NotImplementedError on GQA. - Guards: `_validate_ring_sequence_divisibility` rejects `S % (U*R) != 0` when `R > 1`; non-ring ulysses kernels reject a ulysses_shards they cannot honour; ring kernels warn once when U == CP makes the ring degenerate (R = 1) and suggest a valid U for the mesh and head count; `_apply_attention` logs a warn-once message when falling back to `dot_product` for sequences shorter than `flash_min_seq_length`. - wan_block_benchmark / LTX2 / pyconfig / tile_size_grid_search know the new kernel name. Tests - ring_fixed_m_test.py (29): U=2/R=2 integration class through `_ulysses_ring_custom_attention` vs a dense f32 reference (including `use_fixed_m=False`, `bidirectional=True`, VJP for online ring, and fixed-m/bidir no-VJP guards, plus `S % (U*R) != 0` rejection at R > 1). - custom_splash_fixed_m_test.py (38), attention_config_guards_test.py (25), dot_fallback_layout_test.py (9), tile_size_grid_search_test.py (16), custom_splash_unpadded_test.py (12, TPU-only) -- 129 tests cumulative. - CI: `ring_fixed_m_test.py`, `custom_splash_fixed_m_test.py`, `attention_config_guards_test.py`, and `dot_fallback_layout_test.py` run unconditionally in GitHub Actions; only the 12 TPU-only tests in `custom_splash_unpadded_test.py` skip in CI.
1 parent 62f5ed3 commit d4d6ff6

16 files changed

Lines changed: 2473 additions & 369 deletions

‎end_to_end/tpu/run_wan_stack_tests.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ TESTS=(
3232
# Dot-product fallback layout and tile-size grid-search fixes (feat/attn-dot-fallback-fixes)
3333
"$T/dot_fallback_layout_test.py"
3434
"$T/tile_size_grid_search_test.py"
35+
# Ulysses x Ring attention (feat/ring-attention)
36+
"$T/attention_config_guards_test.py"
37+
"$T/custom_splash_unpadded_test.py"
3538
)
3639
PYTHONPATH="src${PYTHONPATH:+:$PYTHONPATH}" exec "${PYTHON:-python3}" -m pytest -q -rs "${TESTS[@]}" "$@"

‎src/maxdiffusion/configs/base_wan_27b.yml‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ svg_low_noise_density: -1.0
103103
# {"block_q":3328,"block_kv":2816,"block_kv_compute":256,
104104
# "block_kv_compute_in":256,"heads_per_tile":1,"vmem_limit_bytes":67108864}.
105105
svg_flash_block_sizes: {}
106-
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, tokamax_ring_custom, ulysses, ulysses_custom, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_bidir
106+
attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cudnn_flash_te, ring, tokamax_ring, tokamax_ring_custom, ulysses, ulysses_custom, ulysses_custom_fixed_m, ulysses_custom_fixed_m_per_q_block, ulysses_ring, ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_fixed_m_per_q_block, ulysses_ring_custom_bidir
107107

108108

109109
#
@@ -112,9 +112,27 @@ attention: 'flash' # Supported attention: dot_product, flash, tokamax_flash, cud
112112
# CP4 (v7x-8): ulysses_shards=2 (R=2), BQ=9472
113113
# CP8 (v7x-8): ulysses_shards=4 (R=2), BQ=9472
114114
# CP16 (v7x-16): ulysses_shards=8 (R=2), BQ=9472
115+
#
116+
# WARNING: ulysses_shards splits the context axis into (U, R = CP / U) on the
117+
# *ring* variants and is validated (must be -1 or equal to CP) on the non-ring
118+
# ulysses/ulysses_custom* variants:
119+
# - Setting U == CP on a ring variant gives R=1, which is a DEGENERATE ring:
120+
# no KV is rotated and the result is mathematically equivalent to the
121+
# non-ring ulysses_custom* kernel (fixed-m numerics may differ because
122+
# "auto" K-centering is off for ring variants and on for non-ring). Such a
123+
# run must not be reported as a ring result. The attention layer logs a
124+
# warning when this happens.
125+
# - The non-ring ulysses/ulysses_custom* kernels always use the full context
126+
# axis, so their Ulysses degree is fixed at CP. Passing ulysses_shards > 0
127+
# that does not equal CP raises ValueError instead of being silently ignored.
115128
use_base2_exp: True
116129
use_experimental_scheduler: True
117-
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
130+
# auto: on for non-ring ulysses_custom_fixed_m* (virtual, no copy), off for ring
131+
# paths (virtual K-centering via k_mean, plus a pmean across ring shards when R>1).
132+
# Note: the Wan pipelines do not pass this key to the attention layer yet, so
133+
# the layer's default ("auto") applies.
134+
use_k_centering: "auto"
135+
# For attention=ulysses_ring*, hidden Ulysses shard count; ring shards are context / this.
118136
ulysses_shards: -1
119137
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.
120138
# For communication-compute overlap to be effective, enable the following XLA flags:

‎src/maxdiffusion/configs/ltx2_3_video.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ skip_jax_distributed_system: False
55
# dot_product, flash, tokamax_flash, tokamax_ring, tokamax_ring_custom,
66
# ulysses, ulysses_custom, ulysses_custom_fixed_m,
77
# ulysses_custom_fixed_m_per_q_block, ulysses_ring,
8-
# ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_bidir,
8+
# ulysses_ring_custom, ulysses_ring_custom_fixed_m,
9+
# ulysses_ring_custom_fixed_m_per_q_block, ulysses_ring_custom_bidir,
910
# and cudnn_flash_te (GPU only).
1011
attention: 'flash'
1112
use_base2_exp: False

‎src/maxdiffusion/configs/ltx2_video.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ skip_jax_distributed_system: False
55
# dot_product, flash, tokamax_flash, tokamax_ring, tokamax_ring_custom,
66
# ulysses, ulysses_custom, ulysses_custom_fixed_m,
77
# ulysses_custom_fixed_m_per_q_block, ulysses_ring,
8-
# ulysses_ring_custom, ulysses_ring_custom_fixed_m, ulysses_ring_custom_bidir,
8+
# ulysses_ring_custom, ulysses_ring_custom_fixed_m,
9+
# ulysses_ring_custom_fixed_m_per_q_block, ulysses_ring_custom_bidir,
910
# and cudnn_flash_te (GPU only).
1011
attention: 'flash'
1112
use_base2_exp: False

‎src/maxdiffusion/kernels/splash_attention/ring_attention_kernel.py‎

Lines changed: 131 additions & 77 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)