Repository navigation
Commit d0da5df
committed
perf(wan): XLA-fused RMSNorm + RoPE producer for self-attention Q/K
Adds `fused_rmsnorm_rope` (kernels/fused_producers.py) and uses it in
`FlaxWanAttention.__call__` for the self-attention Q/K producer chain.
What
- `fused_rmsnorm_rope(raw_q, raw_k, q_scale, k_scale, freqs_cis, ...)` does
FP32 RMSNorm, casts back to the input dtype, then applies RoPE and the
head transposition in input dtype, so no FP32 RoPE intermediates are
materialised at long sequence lengths. It mirrors nnx.RMSNorm's association
(`x * (rsqrt(var + eps) * scale)`): bit-for-bit in f32, and in bf16 when the
norm scale is bf16. With bf16 activations and an f32 scale, nnx.RMSNorm's
`promote_dtype` rounds the scale to bf16 before folding it into the rsqrt,
while the fused producer keeps the scale in f32 and rounds only the final
product; the two then differ by at most one bf16 ulp. It takes separate q/k
epsilons, accepts an optional `kv_heads` for GQA (defaults to `q_heads`), and
validates `freqs_cis.shape[2] == Sq == Sk`.
- It is `jax.jit`-wrapped with heads/dim/eps static: under the pipeline's
outer jit it is simply inlined, but when a full-size block is called
eagerly (as wan_vace_transformer_test does at 75,600 tokens) XLA fuses the
chain instead of materialising ~1.5 GB of extra intermediates, which OOMed
that test on the v4-8 CI runner.
- `FlaxWanAttention` takes the fused path when `rotary_emb` is given,
`qk_norm` is on and the call is self-attention, reading `self.norm_q.epsilon`
and `self.norm_k.epsilon` directly; cross-attention and the no-RoPE path keep
the separate norm_q / norm_k / `_apply_rope` steps.
Tests
- fused_producers_test.py (10, new, CPU): bit-for-bit match with
nnx.RMSNorm in f32 and in bf16 (bf16 scale), the bf16-activation /
f32-scale case pinned to <= 1 bf16 ulp of nnx.RMSNorm, the reassociated
form is shown to differ, fused Q matches flax RMSNorm, separate q/k
epsilons are applied, non-trivial RoPE in bfloat16 matches the unfused
reference, GQA shapes/values, `freqs_cis` sequence-length mismatch guard,
VJP parity against unfused grads, and end-to-end `FlaxWanAttention` module
wiring + unfused forward parity.
- Added to end_to_end/tpu/run_wan_stack_tests.sh (139 tests cumulative).1 parent a33bb3d commit d0da5df
4 files changed
Lines changed: 535 additions & 13 deletions
File tree
- end_to_end/tpu
- src/maxdiffusion
- kernels
- models
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | 41 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
| |||
3717 | 3718 | | |
3718 | 3719 | | |
3719 | 3720 | | |
3720 | | - | |
3721 | | - | |
3722 | | - | |
3723 | | - | |
3724 | | - | |
3725 | | - | |
3726 | | - | |
3727 | | - | |
3728 | | - | |
3729 | | - | |
3730 | | - | |
| 3721 | + | |
| 3722 | + | |
| 3723 | + | |
| 3724 | + | |
| 3725 | + | |
| 3726 | + | |
| 3727 | + | |
| 3728 | + | |
| 3729 | + | |
| 3730 | + | |
| 3731 | + | |
| 3732 | + | |
| 3733 | + | |
| 3734 | + | |
| 3735 | + | |
| 3736 | + | |
| 3737 | + | |
| 3738 | + | |
3731 | 3739 | | |
3732 | | - | |
3733 | | - | |
| 3740 | + | |
| 3741 | + | |
| 3742 | + | |
| 3743 | + | |
| 3744 | + | |
| 3745 | + | |
| 3746 | + | |
| 3747 | + | |
| 3748 | + | |
| 3749 | + | |
| 3750 | + | |
| 3751 | + | |
| 3752 | + | |
| 3753 | + | |
| 3754 | + | |
3734 | 3755 | | |
3735 | 3756 | | |
3736 | 3757 | | |
| |||
0 commit comments