Repository navigation
Commit df7da19
committed
perf(attention): shard-major Ulysses output all-to-all (wan_ulysses_out_a2a="chunked"/"shard_major")
Third of four pieces split out of the Wan fast-inference optimizations PR.
The switch defaults off ("flat") in the Wan YAMLs and in wan_runtime_options;
it is a plain config key plumbed to FlaxWanAttention / NNXAttentionOp /
AttentionOp and down to the Ulysses kernels through the dispatch context.
- `_ulysses_seq_to_heads` is the inverse Ulysses exchange (attention output,
head-sharded -> sequence-sharded). "flat" is the previous
`all_to_all(split_axis=seq, concat_axis=1)`; XLA reorders the whole output
so the sequence axis is major before the collective and back again after,
four relayout copies per layer at the 720p shard shape (~1.5 ms on v6e,
~0.7 ms on tpu7x). "chunked" / "shard_major" is a single layout-preserving
all_to_all over a reshaped (num_shards, chunk) leading axis (not chunked
compute/comm overlap), so no relayout copies are inserted. Same data
movement, same values.
- With the custom splash kernel (and S_local % block_q == 0, D == 128,
heads_per_tile == 1) the forward Q exchange also stays shard-major (4-D q,
`[B, U, H/U, S_local, D]`) and the kernel writes the shard-major output
directly (`out_num_shards`), on both the 1D Ulysses path (v6e) and the 2D
Ulysses+Ring path (tpu7x): custom_splash_attention.py gains a 4-D q
BlockSpec and a 4-D shard-major output for `_splash_attention_forward` and
`_splash_attention_forward_ring`; ring_attention_kernel.py threads
`out_num_shards` through `_custom_ring_attention_forward` /
`make_custom_ring_attention` and sizes the o/l/m accumulators accordingly
(`save_residuals` with `out_num_shards > 1` raises NotImplementedError:
the lse contract stays `(num_q_heads, orig_q_seq_len)`).
`_compute_fixed_m_metadata` accepts the 5-D shard-major query.
- Reachability: the Pallas shard-major Q/out path requires
S_local % block_q == 0. At 720p/81f with CP=4 the per-device sequence is
18,900 tokens, which no launcher block_q divides (v6e BQ=9472, v7 BQ=6400
at this PR), so on this PR alone the launcher's `chunked` only changes the
inverse all-to-all; the 4-D q / `out_num_shards` kernel path engages once
feat/wan-fast-inference-optimizations pads the sequence to 18,944
(`wan_seq_pad=lane`) and sets BQ=4736 (4736 x 4 = 18,944). The rough
1-1.5 s per 40-step 720p run measured for the kernel path on a pre-review
revision was therefore taken with that padding active (DVFS state
unrecorded; not re-measured since).
- run_wan_fast_inference.sh: the v6e and v7 profiles set
WAN_ULYSSES_OUT_A2A=chunked (overridable); the generic profile keeps flat.
Tests
- ulysses_out_a2a_test.py (6): flat vs chunked vs shard_major parity of the
inverse exchange, the mode guard, the save_residuals + shard-major guard,
the 4-D q ring kernel bit-exact vs 3-D (interpret mode off TPU, compiled on
TPU), and bit-exact wrapper output for the shard-major 1D Ulysses and 2D
Ulysses+Ring paths vs the flat layout -- both wrapper tests spy on
make_splash_mha / make_custom_ring_attention and assert out_num_shards == U,
i.e. that the Pallas shard-major path was actually taken.
- wan_runtime_options_test.py (12): the new switch's YAML default and enum
validation.
- CI: the three kernel bit-exact tests skip in GitHub Actions.
- end_to_end/tpu/run_wan_stack_tests.sh: adds ulysses_out_a2a_test.py.
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:
338 tests, all OK, 1 skipped (cross_attention_pallas_test: off-TPU fallback only);
ulysses_out_a2a_test.py 6 (all 4 multi-device tests ran), wan_runtime_options_test.py 12.1 parent f26b420 commit df7da19
14 files changed
Lines changed: 769 additions & 90 deletions
File tree
- end_to_end/tpu
- src/maxdiffusion
- configs
- kernels
- splash_attention
- models
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| |||
171 | 171 | | |
172 | 172 | | |
173 | 173 | | |
| 174 | + | |
174 | 175 | | |
175 | 176 | | |
176 | 177 | | |
| |||
189 | 190 | | |
190 | 191 | | |
191 | 192 | | |
| 193 | + | |
192 | 194 | | |
193 | 195 | | |
194 | 196 | | |
| |||
209 | 211 | | |
210 | 212 | | |
211 | 213 | | |
| 214 | + | |
212 | 215 | | |
213 | 216 | | |
214 | 217 | | |
| |||
242 | 245 | | |
243 | 246 | | |
244 | 247 | | |
| 248 | + | |
245 | 249 | | |
246 | 250 | | |
247 | 251 | | |
| |||
306 | 310 | | |
307 | 311 | | |
308 | 312 | | |
| 313 | + | |
309 | 314 | | |
310 | 315 | | |
311 | 316 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
146 | 151 | | |
147 | 152 | | |
148 | 153 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
143 | 148 | | |
144 | 149 | | |
145 | 150 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
175 | 180 | | |
176 | 181 | | |
177 | 182 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
125 | 130 | | |
126 | 131 | | |
127 | 132 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
127 | 132 | | |
128 | 133 | | |
129 | 134 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
127 | 132 | | |
128 | 133 | | |
129 | 134 | | |
| |||
0 commit comments