Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions end_to_end/tpu/run_wan_fast_inference.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
# v6e seed 777, 17.92 dB / 0.6556 on v6e seed 12345, and
# 17.69 dB / 0.6918 on tpu7x seed 12345 (visually clean).
# USE_FUSED_ROPE_KERNEL=false restores the XLA producer.
# WAN_CROSS_ATTN_KERNEL / WAN_PATCH_EMBED_MODE / WAN_ULYSSES_OUT_A2A / WAN_SEQ_PAD
# override Wan fast-inference kernel/layout switches
# DP / CP / PER_DEVICE_BATCH / SEED
# override mesh parallelism, per-device batch, or RNG seed (default 12345)
set -euo pipefail
Expand Down Expand Up @@ -144,8 +146,10 @@ if [ -z "${TPU_PROFILE:-}" ]; then
fi

# Keep LIBTPU flags single-line (literal backslashes truncate libtpu flag parsing).
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"}
V6E_LIBTPU=${V6E_LIBTPU:-""}
# ici_ag_pipelining and ag_backward_pipelining are configured per-profile below
# (disabled on v6e, enabled on v7).
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"}
V6E_LIBTPU=${V6E_LIBTPU:-"--xla_tpu_enable_ici_ag_pipelining=false --xla_tpu_enable_ag_backward_pipelining=false"}
# fuse_all_gather is false in every profile (see COMMON_LIBTPU); libtpu fails
# backend init with it enabled on any non-v5e platform, including v6e and v7
# ("Continuation fusion for AllGather ... not supported ... other than Viperlite").
Expand All @@ -156,13 +160,14 @@ V7_DVFS_FLAG=""
if [ "$PIN_DVFS_P_STATE" = "true" ]; then
V7_DVFS_FLAG=" --xla_tpu_dvfs_p_state=7"
fi
V7_LIBTPU=${V7_LIBTPU:-"--xla_tpu_enable_megacore_fusion=true --xla_tpu_megacore_fusion_allow_ags=true${V7_DVFS_FLAG}"}
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}"}

# Reference benchmarks (Wan 2.2 T2V-A14B, 720p/81f/40-step, CP=4, DP=2, warm AOT
# cache, these launcher defaults, measured at this PR without the later PRs):
# tpu7x-8: 115.5s generate (denoise 112.8s) DVFS unpinned;
# 102.0s generate (denoise 99.4s) with PIN_DVFS_P_STATE=true
# v6e-8: 140.1s generate (denoise 137.0s)
# Reference benchmarks (Wan 2.2 T2V-A14B, 720p/81f/40-step, CP=4, DP=2, full
# stack, these launcher defaults, warm AOT cache, DVFS unpinned):
# v6e-8: 126.6s - 127.5s generate (denoise 124.5s - 125.0s)
# tpu7x-8: 107.8s generate (denoise 104.9s; 109.6s / 106.6s with fused RoPE off)
# tpu7x-8 with PIN_DVFS_P_STATE=true: 94.6s generate (denoise 91.7s) with the
# fused RoPE kernel on, and 96.4s generate (denoise 93.4s) with it off.
case "$TPU_PROFILE" in
v6e)
PLATFORM_LIBTPU="$V6E_LIBTPU"
Expand All @@ -179,12 +184,16 @@ case "$TPU_PROFILE" in
DEFAULT_VAE_CHUNK=1
DEFAULT_VAE_SPATIAL=8
DEFAULT_FUSED_ROPE=true
DEFAULT_CROSS_ATTN_KERNEL=pallas
DEFAULT_PATCH_EMBED_MODE=tokens
DEFAULT_ULYSSES_OUT_A2A=shard_major
DEFAULT_SEQ_PAD=lane
;;
v7)
PLATFORM_LIBTPU="$V7_LIBTPU"
DEFAULT_ATTENTION="ulysses_ring_custom_fixed_m"
DEFAULT_U=2
DEFAULT_BQ=6400
DEFAULT_BQ=4736
DEFAULT_BKV=2048
DEFAULT_BKV_COMPUTE=2048
DEFAULT_BKV_COMPUTE_IN=2048
Expand All @@ -195,6 +204,10 @@ case "$TPU_PROFILE" in
DEFAULT_VAE_CHUNK=1
DEFAULT_VAE_SPATIAL=8
DEFAULT_FUSED_ROPE=true
DEFAULT_CROSS_ATTN_KERNEL=pallas
DEFAULT_PATCH_EMBED_MODE=tokens
DEFAULT_ULYSSES_OUT_A2A=shard_major
DEFAULT_SEQ_PAD=lane
;;
*)
echo "== warning: unrecognised or non-v6e/v7 accelerator '${ACCEL_TYPE:-unknown}';" \
Expand All @@ -213,6 +226,10 @@ case "$TPU_PROFILE" in
DEFAULT_VAE_CHUNK=1
DEFAULT_VAE_SPATIAL=8
DEFAULT_FUSED_ROPE=false
DEFAULT_CROSS_ATTN_KERNEL=xla
DEFAULT_PATCH_EMBED_MODE=tokens
DEFAULT_ULYSSES_OUT_A2A=flat
DEFAULT_SEQ_PAD=off
;;
esac

Expand Down Expand Up @@ -244,6 +261,10 @@ USE_FUSED_ROPE_KERNEL=${USE_FUSED_ROPE_KERNEL:-$DEFAULT_FUSED_ROPE}
FUSED_ROPE_ARGS=()
[ -n "${FUSED_ROPE_BLOCK_S:-}" ] && FUSED_ROPE_ARGS+=("fused_rope_block_s=$FUSED_ROPE_BLOCK_S")
[ -n "${FUSED_ROPE_HEAD_BLOCK:-}" ] && FUSED_ROPE_ARGS+=("fused_rope_head_block=$FUSED_ROPE_HEAD_BLOCK")
WAN_CROSS_ATTN_KERNEL=${WAN_CROSS_ATTN_KERNEL:-$DEFAULT_CROSS_ATTN_KERNEL}
WAN_PATCH_EMBED_MODE=${WAN_PATCH_EMBED_MODE:-$DEFAULT_PATCH_EMBED_MODE}
WAN_ULYSSES_OUT_A2A=${WAN_ULYSSES_OUT_A2A:-$DEFAULT_ULYSSES_OUT_A2A}
WAN_SEQ_PAD=${WAN_SEQ_PAD:-$DEFAULT_SEQ_PAD}

# Mesh: context parallelism carries the Ulysses shards, data parallelism takes
# whatever devices remain. Defaults to CP=4 / DP=2 on an 8-device slice. On a
Expand Down Expand Up @@ -310,6 +331,10 @@ python src/maxdiffusion/generate_wan.py "$CONFIG" \
use_kv_cache=true use_base2_exp=true use_experimental_scheduler=true \
use_fused_rope_kernel="$USE_FUSED_ROPE_KERNEL" \
${FUSED_ROPE_ARGS[@]+"${FUSED_ROPE_ARGS[@]}"} \
wan_cross_attn_kernel="$WAN_CROSS_ATTN_KERNEL" \
wan_patch_embed_mode="$WAN_PATCH_EMBED_MODE" \
wan_ulysses_out_a2a="$WAN_ULYSSES_OUT_A2A" \
wan_seq_pad="$WAN_SEQ_PAD" \
fps=16 ${GUIDANCE_ARGS[@]+"${GUIDANCE_ARGS[@]}"} \
seed="${SEED:-12345}" \
flash_block_sizes="$FLASH_BLOCK_SIZES" \
Expand Down
8 changes: 8 additions & 0 deletions end_to_end/tpu/run_wan_stack_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,13 @@ TESTS=(
# Fused RMSNorm+RoPE producer, Wan switches in config (feat/wan-custom-kernels)
"$T/fused_rmsnorm_rope_pallas_test.py"
"$T/wan_runtime_options_test.py"
# Short-KV cross-attention Pallas kernel (feat/wan-cross-attn-pallas)
"$T/cross_attention_pallas_test.py"
# Token-sharded patch embedding (feat/wan-token-patch-embed)
"$T/wan_patch_embed_test.py"
# Shard-major Ulysses output A2A (feat/ulysses-shard-major-a2a)
"$T/ulysses_out_a2a_test.py"
# Lane sequence padding (feat/wan-fast-inference-optimizations)
"$T/wan_seq_pad_test.py"
)
PYTHONPATH="src${PYTHONPATH:+:$PYTHONPATH}" exec "${PYTHON:-python3}" -m pytest -q -rs "${TESTS[@]}" "$@"
21 changes: 21 additions & 0 deletions src/maxdiffusion/configs/base_wan_14b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ wan_cfg_before_unpatchify: True
wan_cross_attn_prescale_kv: False
# Fused RoPE accumulation: "auto" (measured per-platform default), "dtype", "f32".
wan_rope_accum: "auto"
# Text cross-attention backend where it would otherwise fall back to XLA dot-product
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
wan_cross_attn_kernel: "xla"
# Patch embedding: "conv" (nnx.Conv; its channel-sharded output is resharded by
# token, an all-gather on tpu7x) or "tokens" (token-sharded patches times the
# replicated kernel, no activation collective; equal up to accumulation order).
wan_patch_embed_mode: "conv"
# Inverse Ulysses all-to-all (attention output, head-sharded -> sequence-sharded):
# "flat" splits the sequence axis directly; "chunked" exchanges over an explicit
# leading sequence-chunk axis so XLA does not relayout the output around the
# collective (and opportunistically uses the Pallas 4D-q / out_num_shards path
# when S_local % block_q == 0); "shard_major" strictly requires the Pallas
# 4D-q / out_num_shards path and raises if any kernel precondition fails.
wan_ulysses_out_a2a: "flat"
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
# of the video token sequence (or at the end of each ring segment on the ring
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
# Only the custom Ulysses / Ulysses-ring kernels use it.
wan_seq_pad: "off"
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
ulysses_shards: -1
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.
Expand Down
21 changes: 21 additions & 0 deletions src/maxdiffusion/configs/base_wan_1_3b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,27 @@ wan_cfg_before_unpatchify: True
wan_cross_attn_prescale_kv: False
# Fused RoPE accumulation: "auto" (measured per-platform default), "dtype", "f32".
wan_rope_accum: "auto"
# Text cross-attention backend where it would otherwise fall back to XLA dot-product
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
wan_cross_attn_kernel: "xla"
# Patch embedding: "conv" (nnx.Conv; its channel-sharded output is resharded by
# token, an all-gather on tpu7x) or "tokens" (token-sharded patches times the
# replicated kernel, no activation collective; equal up to accumulation order).
wan_patch_embed_mode: "conv"
# Inverse Ulysses all-to-all (attention output, head-sharded -> sequence-sharded):
# "flat" splits the sequence axis directly; "chunked" exchanges over an explicit
# leading sequence-chunk axis so XLA does not relayout the output around the
# collective (and opportunistically uses the Pallas 4D-q / out_num_shards path
# when S_local % block_q == 0); "shard_major" strictly requires the Pallas
# 4D-q / out_num_shards path and raises if any kernel precondition fails.
wan_ulysses_out_a2a: "flat"
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
# of the video token sequence (or at the end of each ring segment on the ring
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
# Only the custom Ulysses / Ulysses-ring kernels use it.
wan_seq_pad: "off"
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
ulysses_shards: -1
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.
Expand Down
21 changes: 21 additions & 0 deletions src/maxdiffusion/configs/base_wan_27b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,27 @@ wan_cfg_before_unpatchify: True
wan_cross_attn_prescale_kv: False
# Fused RoPE accumulation: "auto" (measured per-platform default), "dtype", "f32".
wan_rope_accum: "auto"
# Text cross-attention backend where it would otherwise fall back to XLA dot-product
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
wan_cross_attn_kernel: "xla"
# Patch embedding: "conv" (nnx.Conv; its channel-sharded output is resharded by
# token, an all-gather on tpu7x) or "tokens" (token-sharded patches times the
# replicated kernel, no activation collective; equal up to accumulation order).
wan_patch_embed_mode: "conv"
# Inverse Ulysses all-to-all (attention output, head-sharded -> sequence-sharded):
# "flat" splits the sequence axis directly; "chunked" exchanges over an explicit
# leading sequence-chunk axis so XLA does not relayout the output around the
# collective (and opportunistically uses the Pallas 4D-q / out_num_shards path
# when S_local % block_q == 0); "shard_major" strictly requires the Pallas
# 4D-q / out_num_shards path and raises if any kernel precondition fails.
wan_ulysses_out_a2a: "flat"
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
# of the video token sequence (or at the end of each ring segment on the ring
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
# Only the custom Ulysses / Ulysses-ring kernels use it.
wan_seq_pad: "off"
# For attention=ulysses_ring*, hidden Ulysses shard count; ring shards are context / this.
ulysses_shards: -1
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.
Expand Down
21 changes: 21 additions & 0 deletions src/maxdiffusion/configs/base_wan_animate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ wan_cfg_before_unpatchify: True
wan_cross_attn_prescale_kv: False
# Fused RoPE accumulation: "auto" (measured per-platform default), "dtype", "f32".
wan_rope_accum: "auto"
# Text cross-attention backend where it would otherwise fall back to XLA dot-product
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
wan_cross_attn_kernel: "xla"
# Patch embedding: "conv" (nnx.Conv; its channel-sharded output is resharded by
# token, an all-gather on tpu7x) or "tokens" (token-sharded patches times the
# replicated kernel, no activation collective; equal up to accumulation order).
wan_patch_embed_mode: "conv"
# Inverse Ulysses all-to-all (attention output, head-sharded -> sequence-sharded):
# "flat" splits the sequence axis directly; "chunked" exchanges over an explicit
# leading sequence-chunk axis so XLA does not relayout the output around the
# collective (and opportunistically uses the Pallas 4D-q / out_num_shards path
# when S_local % block_q == 0); "shard_major" strictly requires the Pallas
# 4D-q / out_num_shards path and raises if any kernel precondition fails.
wan_ulysses_out_a2a: "flat"
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
# of the video token sequence (or at the end of each ring segment on the ring
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
# Only the custom Ulysses / Ulysses-ring kernels use it.
wan_seq_pad: "off"
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
ulysses_shards: -1
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.
Expand Down
21 changes: 21 additions & 0 deletions src/maxdiffusion/configs/base_wan_i2v_14b.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,27 @@ wan_cfg_before_unpatchify: True
wan_cross_attn_prescale_kv: False
# Fused RoPE accumulation: "auto" (measured per-platform default), "dtype", "f32".
wan_rope_accum: "auto"
# Text cross-attention backend where it would otherwise fall back to XLA dot-product
# (KV shorter than flash_min_seq_length): "xla", or "pallas" for the fused kernel that
# keeps the KV in VMEM (within bf16 rounding of "xla", not bit-identical).
wan_cross_attn_kernel: "xla"
# Patch embedding: "conv" (nnx.Conv; its channel-sharded output is resharded by
# token, an all-gather on tpu7x) or "tokens" (token-sharded patches times the
# replicated kernel, no activation collective; equal up to accumulation order).
wan_patch_embed_mode: "conv"
# Inverse Ulysses all-to-all (attention output, head-sharded -> sequence-sharded):
# "flat" splits the sequence axis directly; "chunked" exchanges over an explicit
# leading sequence-chunk axis so XLA does not relayout the output around the
# collective (and opportunistically uses the Pallas 4D-q / out_num_shards path
# when S_local % block_q == 0); "shard_major" strictly requires the Pallas
# 4D-q / out_num_shards path and raises if any kernel precondition fails.
wan_ulysses_out_a2a: "flat"
# Self-attention token padding: "off", or "lane" to append pad tokens at the end
# of the video token sequence (or at the end of each ring segment on the ring
# path) so every context shard has a multiple of 128 tokens. Aligned shards make
# the Ulysses all-to-all reshapes free; the custom kernels mask the pad keys.
# Only the custom Ulysses / Ulysses-ring kernels use it.
wan_seq_pad: "off"
# For attention=ulysses_ring, hidden Ulysses shard count; ring shards are context / this.
ulysses_shards: -1
# Splits Ulysses all-to-all into head-group chunks. The last chunk carries any remainder.
Expand Down
Loading
Loading