Skip to content

Commit fe85f8b

Browse files
committed
feat(wan): lane sequence padding for self-attention (wan_seq_pad="lane")
Fourth of four pieces split out of the Wan fast-inference optimizations PR, rebased directly onto main. 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. With padding, `tiled_seq_lens` reports what the kernel tiles: Q over the padded length, K/V over the real one. - `run_wan_fast_inference.sh`: the v6e and v7 profiles set `WAN_SEQ_PAD=lane` (overridable); the generic profile keeps `off`. 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` (20): 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 aligned real_len U=2 x R=2), with the padded side of the 1D Ulysses and aligned U=2 x R=2 cases also run in the `shard_major` output layout, fixed-m metadata ignoring pad query rows, the block benchmark's tiled lengths, `NotImplementedError` kernel guards, and end-to-end `WanModel` lane padding parity. All 20 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`.
1 parent 6629957 commit fe85f8b

17 files changed

Lines changed: 1217 additions & 31 deletions

‎end_to_end/tpu/run_wan_fast_inference.sh‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
# WAN_PATCH_EMBED_MODE
7474
# WAN_CROSS_ATTN_KERNEL
7575
# override Wan fast-inference kernel/layout switches
76+
# WAN_SEQ_PAD
77+
# override Wan fast-inference self-attention sequence padding ("off" | "lane")
7678
# DP / CP / PER_DEVICE_BATCH / SEED
7779
# override mesh parallelism, per-device batch, or RNG seed (default 12345)
7880
set -euo pipefail
@@ -149,8 +151,10 @@ if [ -z "${TPU_PROFILE:-}" ]; then
149151
fi
150152

151153
# Keep LIBTPU flags single-line (literal backslashes truncate libtpu flag parsing).
152-
COMMON_LIBTPU=${COMMON_LIBTPU:-"--xla_tpu_spmd_rng_bit_generator_unsafe=true --xla_tpu_enable_async_collective_fusion=true --xla_tpu_enable_async_collective_fusion_fuse_all_gather=false --xla_tpu_enable_async_collective_fusion_multiple_steps=true --xla_tpu_memory_bound_loop_optimizer_options=enabled:true --xla_tpu_enable_dot_strength_reduction=true --xla_enable_async_collective_permute=true --xla_tpu_enable_data_parallel_all_reduce_opt=true --xla_tpu_data_parallel_opt_different_sized_ops=true --xla_tpu_overlap_compute_collective_tc=true --xla_enable_async_all_gather=true --xla_tpu_scoped_vmem_limit_kib=65536 --xla_tpu_enable_async_all_to_all=true --xla_tpu_enable_all_experimental_scheduler_features=true --xla_tpu_enable_scheduler_memory_pressure_tracking=true --xla_tpu_host_transfer_overlap_limit=24 --xla_tpu_aggressive_opt_barrier_removal=ENABLED --xla_lhs_prioritize_async_depth_over_stall=ENABLED --xla_should_allow_loop_variant_parameter_in_chain=ENABLED --xla_should_add_loop_invariant_op_in_chain=ENABLED --xla_tpu_enable_ici_ag_pipelining=true --xla_max_concurrent_host_send_recv=100 --xla_tpu_scheduler_percent_shared_memory_limit=100 --xla_latency_hiding_scheduler_rerun=2 --xla_tpu_use_minor_sharding_for_major_trivial_input=true --xla_tpu_relayout_group_size_threshold_for_reduce_scatter=1 --xla_tpu_enable_latency_hiding_scheduler=true --xla_tpu_enable_ag_backward_pipelining=true --xla_tpu_use_single_sparse_core_for_all_gather_offload=true --xla_tpu_sparse_core_all_gather_latency_multiplier=1 --xla_tpu_sparse_core_reduce_scatter_latency_multiplier=3 --xla_tpu_enable_sparse_core_collective_aggregator=true --xla_tpu_enable_sparse_core_offload_queuing_in_lhs=true --xla_tpu_enable_sparse_core_reduce_scatter_v2=true --xla_tpu_enable_sparse_core_collective_offload_all_gather=true --xla_tpu_enable_sparse_core_collective_offload_2d_all_gather=true --xla_tpu_enable_sparse_core_collective_offload_all_reduce=true --xla_tpu_enable_sparse_core_collective_offload_reduce_scatter=true --xla_tpu_enable_sparse_core_collective_offload_3d_all_gather=true --xla_tpu_enable_concurrent_sparse_core_offloading=true --xla_tpu_assign_all_reduce_scatter_layout=true"}
153-
V6E_LIBTPU=${V6E_LIBTPU:-""}
154+
# ici_ag_pipelining and ag_backward_pipelining are configured per-profile below
155+
# (disabled on v6e, enabled on v7).
156+
COMMON_LIBTPU=${COMMON_LIBTPU:-"--xla_tpu_spmd_rng_bit_generator_unsafe=true --xla_tpu_enable_async_collective_fusion=true --xla_tpu_enable_async_collective_fusion_fuse_all_gather=false --xla_tpu_enable_async_collective_fusion_multiple_steps=true --xla_tpu_memory_bound_loop_optimizer_options=enabled:true --xla_tpu_enable_dot_strength_reduction=true --xla_enable_async_collective_permute=true --xla_tpu_enable_data_parallel_all_reduce_opt=true --xla_tpu_data_parallel_opt_different_sized_ops=true --xla_tpu_overlap_compute_collective_tc=true --xla_enable_async_all_gather=true --xla_tpu_scoped_vmem_limit_kib=65536 --xla_tpu_enable_async_all_to_all=true --xla_tpu_enable_all_experimental_scheduler_features=true --xla_tpu_enable_scheduler_memory_pressure_tracking=true --xla_tpu_host_transfer_overlap_limit=24 --xla_tpu_aggressive_opt_barrier_removal=ENABLED --xla_lhs_prioritize_async_depth_over_stall=ENABLED --xla_should_allow_loop_variant_parameter_in_chain=ENABLED --xla_should_add_loop_invariant_op_in_chain=ENABLED --xla_max_concurrent_host_send_recv=100 --xla_tpu_scheduler_percent_shared_memory_limit=100 --xla_latency_hiding_scheduler_rerun=2 --xla_tpu_use_minor_sharding_for_major_trivial_input=true --xla_tpu_relayout_group_size_threshold_for_reduce_scatter=1 --xla_tpu_enable_latency_hiding_scheduler=true --xla_tpu_use_single_sparse_core_for_all_gather_offload=true --xla_tpu_sparse_core_all_gather_latency_multiplier=1 --xla_tpu_sparse_core_reduce_scatter_latency_multiplier=3 --xla_tpu_enable_sparse_core_collective_aggregator=true --xla_tpu_enable_sparse_core_offload_queuing_in_lhs=true --xla_tpu_enable_sparse_core_reduce_scatter_v2=true --xla_tpu_enable_sparse_core_collective_offload_all_gather=true --xla_tpu_enable_sparse_core_collective_offload_2d_all_gather=true --xla_tpu_enable_sparse_core_collective_offload_all_reduce=true --xla_tpu_enable_sparse_core_collective_offload_reduce_scatter=true --xla_tpu_enable_sparse_core_collective_offload_3d_all_gather=true --xla_tpu_enable_concurrent_sparse_core_offloading=true --xla_tpu_assign_all_reduce_scatter_layout=true"}
157+
V6E_LIBTPU=${V6E_LIBTPU:-"--xla_tpu_enable_ici_ag_pipelining=false --xla_tpu_enable_ag_backward_pipelining=false"}
154158
# fuse_all_gather is false in every profile (see COMMON_LIBTPU); libtpu fails
155159
# backend init with it enabled on any non-v5e platform, including v6e and v7
156160
# ("Continuation fusion for AllGather ... not supported ... other than Viperlite").
@@ -161,13 +165,14 @@ V7_DVFS_FLAG=""
161165
if [ "$PIN_DVFS_P_STATE" = "true" ]; then
162166
V7_DVFS_FLAG=" --xla_tpu_dvfs_p_state=7"
163167
fi
164-
V7_LIBTPU=${V7_LIBTPU:-"--xla_tpu_enable_megacore_fusion=true --xla_tpu_megacore_fusion_allow_ags=true${V7_DVFS_FLAG}"}
168+
V7_LIBTPU=${V7_LIBTPU:-"--xla_tpu_enable_ici_ag_pipelining=true --xla_tpu_enable_ag_backward_pipelining=true --xla_tpu_enable_megacore_fusion=true --xla_tpu_megacore_fusion_allow_ags=true --xla_tpu_enable_offloading_copy_to_sparsecore=false${V7_DVFS_FLAG}"}
165169

166-
# Reference benchmarks (Wan 2.2 T2V-A14B, 720p/81f/40-step, CP=4, DP=2, warm AOT
167-
# cache, these launcher defaults, measured at this PR without the later PRs):
168-
# tpu7x-8: 115.5s generate (denoise 112.8s) DVFS unpinned;
169-
# 102.0s generate (denoise 99.4s) with PIN_DVFS_P_STATE=true
170-
# v6e-8: 140.1s generate (denoise 137.0s)
170+
# Reference benchmarks (Wan 2.2 T2V-A14B, 720p/81f/40-step, CP=4, DP=2, full
171+
# stack, these launcher defaults, warm AOT cache, DVFS unpinned):
172+
# v6e-8: 126.6s - 127.5s generate (denoise 124.5s - 125.0s)
173+
# tpu7x-8: 107.8s generate (denoise 104.9s; 109.6s / 106.6s with fused RoPE off)
174+
# tpu7x-8 with PIN_DVFS_P_STATE=true: 94.6s generate (denoise 91.7s) with the
175+
# fused RoPE kernel on, and 96.4s generate (denoise 93.4s) with it off.
171176
case "$TPU_PROFILE" in
172177
v6e)
173178
PLATFORM_LIBTPU="$V6E_LIBTPU"
@@ -187,12 +192,13 @@ case "$TPU_PROFILE" in
187192
DEFAULT_ULYSSES_OUT_A2A=chunked
188193
DEFAULT_PATCH_EMBED_MODE=tokens
189194
DEFAULT_CROSS_ATTN_KERNEL=pallas
195+
DEFAULT_SEQ_PAD=lane
190196
;;
191197
v7)
192198
PLATFORM_LIBTPU="$V7_LIBTPU"
193199
DEFAULT_ATTENTION="ulysses_ring_custom_fixed_m"
194200
DEFAULT_U=2
195-
DEFAULT_BQ=6400
201+
DEFAULT_BQ=4736
196202
DEFAULT_BKV=2048
197203
DEFAULT_BKV_COMPUTE=2048
198204
DEFAULT_BKV_COMPUTE_IN=2048
@@ -206,6 +212,7 @@ case "$TPU_PROFILE" in
206212
DEFAULT_ULYSSES_OUT_A2A=chunked
207213
DEFAULT_PATCH_EMBED_MODE=tokens
208214
DEFAULT_CROSS_ATTN_KERNEL=pallas
215+
DEFAULT_SEQ_PAD=lane
209216
;;
210217
*)
211218
echo "== warning: unrecognised or non-v6e/v7 accelerator '${ACCEL_TYPE:-unknown}';" \
@@ -227,6 +234,7 @@ case "$TPU_PROFILE" in
227234
DEFAULT_ULYSSES_OUT_A2A=flat
228235
DEFAULT_PATCH_EMBED_MODE=tokens
229236
DEFAULT_CROSS_ATTN_KERNEL=xla
237+
DEFAULT_SEQ_PAD=off
230238
;;
231239
esac
232240

@@ -261,6 +269,7 @@ FUSED_ROPE_ARGS=()
261269
WAN_ULYSSES_OUT_A2A=${WAN_ULYSSES_OUT_A2A:-$DEFAULT_ULYSSES_OUT_A2A}
262270
WAN_PATCH_EMBED_MODE=${WAN_PATCH_EMBED_MODE:-$DEFAULT_PATCH_EMBED_MODE}
263271
WAN_CROSS_ATTN_KERNEL=${WAN_CROSS_ATTN_KERNEL:-$DEFAULT_CROSS_ATTN_KERNEL}
272+
WAN_SEQ_PAD=${WAN_SEQ_PAD:-$DEFAULT_SEQ_PAD}
264273

265274
# Mesh: context parallelism carries the Ulysses shards, data parallelism takes
266275
# whatever devices remain. Defaults to CP=4 / DP=2 on an 8-device slice. On a
@@ -330,6 +339,7 @@ python src/maxdiffusion/generate_wan.py "$CONFIG" \
330339
wan_ulysses_out_a2a="$WAN_ULYSSES_OUT_A2A" \
331340
wan_patch_embed_mode="$WAN_PATCH_EMBED_MODE" \
332341
wan_cross_attn_kernel="$WAN_CROSS_ATTN_KERNEL" \
342+
wan_seq_pad="$WAN_SEQ_PAD" \
333343
fps=16 ${GUIDANCE_ARGS[@]+"${GUIDANCE_ARGS[@]}"} \
334344
seed="${SEED:-12345}" \
335345
flash_block_sizes="$FLASH_BLOCK_SIZES" \

‎end_to_end/tpu/run_wan_stack_tests.sh‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,7 @@ TESTS=(
5151
"$T/wan_patch_embed_test.py"
5252
# Short-KV cross-attention Pallas kernel (feat/wan-cross-attn-pallas)
5353
"$T/cross_attention_pallas_test.py"
54+
# Lane sequence padding (feat/wan-fast-inference-optimizations)
55+
"$T/wan_seq_pad_test.py"
5456
)
5557
PYTHONPATH="src${PYTHONPATH:+:$PYTHONPATH}" exec "${PYTHON:-python3}" -m pytest -q -rs "${TESTS[@]}" "$@"

‎src/maxdiffusion/configs/base_wan_14b.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ wan_patch_embed_mode: "conv"
147147
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
148148
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
149149
wan_cross_attn_kernel: "xla"
150+
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
151+
# of the video token sequence (or at the end of each ring segment on the ring
152+
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
153+
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
154+
# Only the custom Ulysses / Ulysses-ring kernels use it.
155+
wan_seq_pad: "off"
150156
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
151157
ulysses_shards: -1
152158
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.

‎src/maxdiffusion/configs/base_wan_1_3b.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ wan_patch_embed_mode: "conv"
144144
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
145145
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
146146
wan_cross_attn_kernel: "xla"
147+
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
148+
# of the video token sequence (or at the end of each ring segment on the ring
149+
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
150+
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
151+
# Only the custom Ulysses / Ulysses-ring kernels use it.
152+
wan_seq_pad: "off"
147153
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
148154
ulysses_shards: -1
149155
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.

‎src/maxdiffusion/configs/base_wan_27b.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ wan_patch_embed_mode: "conv"
181181
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
182182
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
183183
wan_cross_attn_kernel: "xla"
184+
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
185+
# of the video token sequence (or at the end of each ring segment on the ring
186+
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
187+
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
188+
# Only the custom Ulysses / Ulysses-ring kernels use it.
189+
wan_seq_pad: "off"
184190
# For attention=ulysses_ring*, hidden Ulysses shard count; ring shards are context / this.
185191
ulysses_shards: -1
186192
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.

‎src/maxdiffusion/configs/base_wan_animate.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ wan_patch_embed_mode: "conv"
126126
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
127127
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
128128
wan_cross_attn_kernel: "xla"
129+
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
130+
# of the video token sequence (or at the end of each ring segment on the ring
131+
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
132+
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
133+
# Only the custom Ulysses / Ulysses-ring kernels use it.
134+
wan_seq_pad: "off"
129135
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
130136
ulysses_shards: -1
131137
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.

‎src/maxdiffusion/configs/base_wan_i2v_14b.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ wan_patch_embed_mode: "conv"
128128
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
129129
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
130130
wan_cross_attn_kernel: "xla"
131+
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
132+
# of the video token sequence (or at the end of each ring segment on the ring
133+
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
134+
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
135+
# Only the custom Ulysses / Ulysses-ring kernels use it.
136+
wan_seq_pad: "off"
131137
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
132138
ulysses_shards: -1
133139
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.

‎src/maxdiffusion/configs/base_wan_i2v_27b.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ wan_patch_embed_mode: "conv"
128128
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
129129
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
130130
wan_cross_attn_kernel: "xla"
131+
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
132+
# of the video token sequence (or at the end of each ring segment on the ring
133+
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
134+
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
135+
# Only the custom Ulysses / Ulysses-ring kernels use it.
136+
wan_seq_pad: "off"
131137
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
132138
ulysses_shards: -1
133139
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.

0 commit comments

Comments
 (0)