You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3c4b194
Browse filesBrowse the repository at this point in the historyBrowse files
feat(wan): lane sequence padding for self-attention (wan_seq_pad="lane")
Last of four pieces split out of the Wan fast-inference optimizations PR
(the cross-attention Pallas kernel, token-sharded patch embedding and
shard-major Ulysses A2A are the three PRs below this one). The switch
defaults off in the Wan YAMLs and in wan_runtime_options; it is a plain
config key plumbed to WanModel through `attention_config_entries(config)`.
- Lane sequence padding (`wan_seq_pad="lane"`): `WanModel` pads the video tokens
at the tail to a per-shard multiple of 128 (`75,600 -> 75,776` at
720p/CP=4; on the Ulysses-ring path each ring segment is padded at its own
tail) so the Ulysses reshapes are copy-free, pads the rotary embedding to
match, and unpads the output and residual. The padding descriptor
(`TokenPadding`) is planned via `plan_token_padding` and passed explicitly as
a `token_padding` argument from `WanModel.__call__` ->
`WanTransformerBlock.__call__` -> `FlaxWanAttention.__call__` ->
`NNXAttentionOp.apply_attention` (no process/thread-global `ContextVar`).
- The custom `ulysses_custom*` / `ulysses_ring_custom*` kernels mask the pad
keys by passing the real length as the ragged KV tail; the fixed-m norm
reductions (`_compute_fixed_m_metadata` via `q_valid_len` on both the 1-D
Ulysses path and the R == 1 ring path, `_ring_fixed_m_norms_pre_a2a` via
`row_ok` for R > 1) exclude the pad Q/K/V rows so pad-token values cannot
push a block onto the online-softmax path or move the `k_mean`. The ring's
`ring_kv_seq_len` is always the real length.
- Fails closed on invalid static/mode configs: when `wan_seq_pad="lane"` is
requested, incompatible static configurations (`per_token_t=True` TI2V
timesteps, `use_svg_attention`, `use_memory_efficient_attention`, unsupported
kernels, or `ulysses_shards` that do not divide the context axis) raise
`ValueError` in `plan_token_padding` (checked at `WanModel.__init__` time as
well as per call); `_resolve_token_padding` raises `ValueError` on any
`TokenPadding.total_len != seq_len` mismatch; and attention kernels that
cannot mask active token padding raise `NotImplementedError` via
`_reject_token_padding`. Dynamic short (`S < flash_min_seq_length`) or
indivisible (`S % context_shards != 0`) sequences log a warn-once message and
run unpadded.
- `wan_block_benchmark.py` applies `plan_token_padding` to the benchmarked
sequence length and passes `token_padding` explicitly to the block.
- `run_wan_fast_inference.sh`: the v6e and v7 profiles set `WAN_SEQ_PAD=lane`
and `DEFAULT_ULYSSES_OUT_A2A=shard_major` (overridable); the generic profile
keeps `off` and `flat`. LIBTPU: v6e disables
`--xla_tpu_enable_ici_ag_pipelining` / `--xla_tpu_enable_ag_backward_pipelining`,
v7 enables them and adds `--xla_tpu_enable_offloading_copy_to_sparsecore=false`
and `BQ=4736`.
Performance, measured at the stack tip (Wan 2.2 T2V-A14B, 720p/81f/40 steps,
warm AOT, DVFS unpinned unless noted; CP=4, DP=2, launcher defaults):
v6e-8: denoise 124.5s - 125.0s, generate 126.6s - 127.5s
tpu7x-8: denoise 104.9s, generate 107.8s (106.6s / 109.6s with fused RoPE off)
tpu7x-8, DVFS pinned (`PIN_DVFS_P_STATE=true`):
denoise 91.7s, generate 94.6s with the fused RoPE kernel on
denoise 93.4s, generate 96.4s with the fused RoPE kernel off
Tests
- `wan_seq_pad_test.py` (19): padding plan, `ValueError` guards for invalid
static/mode configs (including `per_token_t=True`) and `_resolve_token_padding`
length mismatches, warn-once logging for short/indivisible sequences, pad-key
masking for `ulysses_custom*` and `ulysses_ring_custom*` (U=2 x R=2, U=4 x R=1,
and shard-major U=2 x R=2), fixed-m metadata ignoring pad query rows,
`NotImplementedError` kernel guards, and end-to-end `WanModel` lane padding
parity. All 19 tests run unconditionally in GitHub Actions (no
`_SKIP_IN_GITHUB_ACTIONS`).
- `wan_runtime_options_test.py` (12): the new switch's YAML default and enum
validation; `wan_seq_pad` is excluded from the `FlaxWanAttention` attribute
check since `WanModel` consumes it.
- `end_to_end/tpu/run_wan_stack_tests.sh`: adds `wan_seq_pad_test.py`.
0 commit comments