Repository navigation
Commit d4d6ff6
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
File tree
- end_to_end/tpu
- src/maxdiffusion
- configs
- kernels/splash_attention
- models
- ltx2
- tests
- ltx2
- utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
35 | 38 | | |
36 | 39 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
106 | | - | |
| 106 | + | |
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
115 | 128 | | |
116 | 129 | | |
117 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
118 | 136 | | |
119 | 137 | | |
120 | 138 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
Large diffs are not rendered by default.
0 commit comments