Repository navigation
Commit da317cc
committed
feat(wan): short-KV text cross-attention Pallas kernel (wan_cross_attn_kernel="pallas")
First of four pieces split out of the Wan fast-inference optimizations PR.
The switch defaults off ("xla") in the Wan YAMLs and in wan_runtime_options;
it is a plain config key plumbed to FlaxWanAttention through
`attention_config_entries(config)`.
- kernels/cross_attention_pallas.py keeps a head block's whole 512-token text
KV resident in VMEM and computes an exact single-pass softmax per query tile
(full-row max, exp2, one numerator/denominator, normalised after PV; no
running max / online rescaling). It replaces only what would otherwise be
the unmasked XLA dot-product fallback (`_would_use_unmasked_dot_product`,
which shares `_FLASH_MIN_SEQ_LENGTH_KERNELS` with the dispatcher).
- `FlaxWanAttention._pallas_cross_attention` falls back to XLA (logging the
reason once) when the inputs or sharding don't fit, and also when the VMEM
budget is not validated or too small: 64 MiB is requested only when every
mesh TPU is v6e or tpu7x (otherwise Mosaic's default), and
`estimate_vmem_bytes` (Q/out/K/V double buffers + one head's FP32 scores) is
checked against it (~42 MiB at the Wan shard shape; MAX_KV_LEN KV does not
fit and falls back rather than failing to compile). The estimate has only
been checked against real compiles at Wan's KV length (512). The kernel
itself raises a ValueError for an over-budget working set only when a budget
resolves (an explicit vmem_limit_bytes, or the v6e/tpu7x default); under
Mosaic's default it does no check of its own, and callers that can fall back
use `vmem_unusable_reason`. The two fallback reasons (unusable inputs, VMEM)
log under separate warn-once keys; the "ACTIVE" log is keyed on the query
shape, so T2V and the I2V text branch are each reported. The sharded wrapper is cached
(`_build_sharded_pallas_cross_attention`) and wrapped with
`with_xla_backward` since `pallas_call` has no transpose rule.
- Dispatch: T2V cross-attention, and the text half of I2V cross-attention
(both the image-token split and the text-only path), try the kernel first
and use `attention_op.apply_attention` when it returns None.
- `wan_cross_attn_cpu_interpret` (default False, tests only) lets the kernel
run under Pallas interpret mode on CPU.
- In isolation the kernel is 0.51 ms vs 3.94 ms (v6e) and 0.49 ms vs 1.81 ms
(tpu7x) per call for the XLA path inside the model.
- run_wan_fast_inference.sh: the v6e and v7 profiles set
WAN_CROSS_ATTN_KERNEL=pallas (overridable); the generic profile keeps xla.
- wan_block_benchmark.py passes `split_head_dim`, `ulysses_attention_chunks`
and `use_k_centering` from the config to the benchmarked block (shared
benchmark-fidelity fix carried in this first piece; note it defaults
`split_head_dim` to True when the config lacks the key, whereas the Wan
ymls set False).
Tests
- cross_attention_pallas_test.py (32 test methods, 40 parameterized cases):
golden parity (row/tile max, mxu/vpu
sum, ragged tiles, k_prescaled, batch), guards, VMEM budget resolution /
working-set estimate / fallback reasons, and FlaxWanAttention dispatch (CPU
interpret integration, fallbacks, I2V text-vs-image split). The three tests
that expect the kernel to be used skip on TPUs outside the validated kinds
(there FlaxWanAttention falls back by design); that fallback is covered by
test_falls_back_to_xla_on_unvalidated_tpu_kind.
- wan_runtime_options_test.py (12): the new switch's YAML default and enum
validation (test_enum_options_validation).
- CI: kernel parity and the TPU Pallas-active dispatch
tests skip in GitHub Actions (19 of the 40 cases); guards, VMEM budget logic, CPU interpret integration and the
XLA fallback dispatch tests run in CI.
- end_to_end/tpu/run_wan_stack_tests.sh: adds cross_attention_pallas_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:
322 tests, all OK, 1 skipped (cross_attention_pallas_test: off-TPU fallback only);
cross_attention_pallas_test.py 40 cases (1 skipped), wan_runtime_options_test.py 12.1 parent ba0b938 commit da317cc
14 files changed
Lines changed: 1095 additions & 16 deletions
File tree
- end_to_end/tpu
- src/maxdiffusion
- configs
- kernels
- models
- tests
- utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
| 72 | + | |
71 | 73 | | |
72 | 74 | | |
73 | 75 | | |
| |||
180 | 182 | | |
181 | 183 | | |
182 | 184 | | |
| 185 | + | |
183 | 186 | | |
184 | 187 | | |
185 | 188 | | |
| |||
196 | 199 | | |
197 | 200 | | |
198 | 201 | | |
| 202 | + | |
199 | 203 | | |
200 | 204 | | |
201 | 205 | | |
| |||
214 | 218 | | |
215 | 219 | | |
216 | 220 | | |
| 221 | + | |
217 | 222 | | |
218 | 223 | | |
219 | 224 | | |
| |||
245 | 250 | | |
246 | 251 | | |
247 | 252 | | |
| 253 | + | |
248 | 254 | | |
249 | 255 | | |
250 | 256 | | |
| |||
311 | 317 | | |
312 | 318 | | |
313 | 319 | | |
| 320 | + | |
314 | 321 | | |
315 | 322 | | |
316 | 323 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
48 | 50 | | |
49 | 51 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
135 | 139 | | |
136 | 140 | | |
137 | 141 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
132 | 136 | | |
133 | 137 | | |
134 | 138 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
169 | 173 | | |
170 | 174 | | |
171 | 175 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
111 | 111 | | |
112 | 112 | | |
113 | 113 | | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
114 | 118 | | |
115 | 119 | | |
116 | 120 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
116 | 120 | | |
117 | 121 | | |
118 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
116 | 120 | | |
117 | 121 | | |
118 | 122 | | |
| |||
0 commit comments