Skip to content

fix(sp): keep sequence-parallel padding out of attention (Wan, HunyuanVideo-1.5, Qwen-Image, Flux.2) - #1713

Open
klin-amd wants to merge 10 commits into
ModelTC:mainfrom
klin-amd:dev/klin/wan-sp-padding-kv
Open

klin-amd wants to merge 10 commits into
ModelTC:mainfrom
klin-amd:dev/klin/wan-sp-padding-kv

Conversation

@klin-amd

@klin-amd klin-amd commented Nov 14, 2026 •

Copy link
Copy Markdown

Problem

When the token count is not a multiple of the sequence-parallel size, the models' _seq_parallel_pre_process pads the image/video tokens with zeros before splitting them across ranks. After norm, modulation and the QKV projections these tokens have non-zero keys and values. Ulysses and ring attention attend to them without a mask, so the outputs of the real tokens change. The padding is cropped at the end, so the error is silent, except in Qwen-Image t2i, which does not crop it and crashes in the scheduler:

RuntimeError: The size of tensor a (6889) must match the size of tensor b (6890)

Some shipped configs pad on every run, and image-, video- and custom-size inputs often do:

  • Wan2.2-TI2V: with configs/dist_infer/wan22_ti2v_i2v_ulysses.json (704x1280 HxW target, 121 frames, SP4), a 3:4 portrait input such as assets/inputs/imgs/img_0.jpg gives 1088x800 (HxW), 26350 tokens, 2 padding tokens on 4 GPUs.
  • VACE with a source video follows the video's aspect ratio.
  • HunyuanVideo-1.5: the official hy15_t2v_480p_bf16_dist.json (480p 16:9, 121 frames, SP8) has 49290 tokens, padded with 6 to 49296, on every run.
  • Qwen-Image: the built-in 1:1 size (1328x1328, 6889 tokens) pads on any SP size.
  • Flux.2: custom sizes, and i2i / edit with several inputs, whose reference tokens keep each input's own size.

Fix

  • Record the unpadded token count when padding is added and pass it to the parallel attention as valid_seq_len:
    • Ulysses: Add full ulysses sp #1720 added valid_seq_len for self-attention without auxiliary tokens and raises when they are present. This PR lets it drop the trailing padding of the A2A sequence around auxiliary tokens placed before or after it (joint text-image attention in HunyuanVideo-1.5, Qwen-Image and Flux.2) and put zero rows back in its place, so the communication layout does not change and every attention backend sees the real token count. Without auxiliary tokens the result is the same as on main. All Ulysses paths (bulk, head-parallel, torch and Triton pre/post) go through this code.
    • Ring takes the same keyword and drops the padding from the K/V block of the rank that holds it.
    • valid_seq_len defaults to None, so other callers are not affected.
  • Wan: WanPreInferModuleOutput.seq_p_valid_len, set in WanModel._seq_parallel_pre_process and read in the Wan and VACE transformers.
  • HunyuanVideo-1.5, Qwen-Image and Flux.2: the model sets transformer_infer.seq_p_valid_len and the joint attention calls pass it. Qwen-Image also crops the gathered noise_pred back to the unpadded length.

Other models that pad for sequence parallelism (e.g. LTX-2, in #1718) are not changed here; they keep the old behavior.

Test

Step-1 noise prediction compared with a single-GPU run, H100, on main c840dad and on this branch.

Model Config Size Padding main This PR
Wan2.1-T2V-1.3B Ulysses SP4 480x848 2 rel L2 1.37e-2 0 (bit-exact)
Wan2.1-T2V-1.3B Ulysses SP4, head_parallel 480x848 2 0 (bit-exact)
Wan2.1-T2V-1.3B Ulysses SP4, tensor_fusion + Triton pre/post 480x848 2 0 (bit-exact)
Wan2.1-T2V-1.3B Ring SP4 (41 frames) 480x848 2 rel L2 1.29e-2 5.15e-3
Wan2.2-TI2V-5B wan22_ti2v_i2v_ulysses.json with SP2, 4 steps img_0.jpg cropped to 576x832 (HxW) 1 rel L2 1.12e-2 0 (bit-exact)
Wan2.1-VACE-1.3B Ulysses SP2, source video + 2 reference images 1696x912 source 1 rel L2 6.36e-3 0 (bit-exact)
HunyuanVideo-1.5 T2V Ulysses SP2, 33 frames 720p 16:9 1 rel L2 1.51e-2 0 (bit-exact)
Qwen-Image-2512 T2I Ulysses SP2 1:1, 1328x1328 1 crash 0 (bit-exact)
FLUX.2-klein-4B T2I Ulysses SP2 880x1200 1 rel L2 1.29e-2 0 (bit-exact)
FLUX.2-klein-4B edit flux2_klein_i2i_distill_edit_ulysses.json, img_0.jpg + img_1.jpg 7603 tokens 1 rel L2 1.48e-2 0 (bit-exact)

Without padding the results are bit-exact with a single GPU: Wan 480x832 on main and on this PR, Qwen-Image 16:9 on this PR. Ring is not bit-exact with a single GPU even without padding (5.16e-3 at 480x832, LSE merge); the padded case is back to that level. The VACE and Flux.2 single-GPU references use OMP_NUM_THREADS=1 as torchrun does, since their CPU input resize differs by 1 ulp with the thread count; the VACE rows use vae_parallel: false.

Ulysses valid_seq_len on its own, SP2 and SP4, random Q/K/V with non-zero padding rows, flash_attn3: no auxiliary tokens, auxiliary Q/K/V before or after the A2A sequence, and auxiliary K/V only (before or after), each on the bulk, head-parallel and tensor_fusion + Triton paths, with and without padding. In all 45 cases per SP size the real-token output and the auxiliary output are bit-exact with single-GPU attention on the unpadded sequence, and the padding rows are zero. On main the cases with auxiliary tokens and padding raise ValueError.

pre-commit run passes on the changed files.

When the Wan token count is not a multiple of the sequence-parallel size,
_seq_parallel_pre_process pads x with zero tokens. After norm, modulation
and the QKV projections these tokens have non-zero keys and values, and
both Ulysses and ring attention attend to them without a mask, so the
real tokens' outputs change. unpatchify crops the padding later, so the
error is silent.

Record the unpadded length and pass it to the parallel attention as
kv_valid_len. Ulysses drops the trailing padded K/V rows after the
all-to-all; ring drops them from the K/V block of the rank that holds
the padding. Q keeps its padded length, so the communication layout is
unchanged. The argument defaults to None, so other callers are not
affected.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
Slicing only K/V made attention see a longer Q than K/V. Dense backends
accept that, but sparse ones build their block layout from q.shape[0].
With nbhd_attn_flashinfer under Ulysses SP2 at 464x848 (1 padding token,
measured with the separate fix that puts its workspace on the current
device; without it rank 1 fails with an illegal address), K/V-only
slicing ran but moved the output further from the single-GPU
result (step-1 rel L2 1.9e-2, vs 7.6e-3 with no fix at all). From the
code, nbhd_attn reshapes K with Q's length and radial_attn indexes past
K; sol_attn falls back to dense when the shapes differ.

Ulysses now drops the trailing padding from Q, K and V before attention
and puts zero rows back in the output, so the all-to-all layout is
unchanged and every backend sees the real token count. With that,
nbhd_attn_flashinfer matches the single-GPU result bit for bit. Ring
keeps dropping it from K/V only (it uses dense apply_with_lse). The
argument is renamed kv_valid_len -> valid_len since it now covers Q as
well.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
WanVaceTransformerInfer overrides infer() and never read
seq_p_valid_len, so VACE kept attending to the sequence-parallel
padding. With a source video the output shape follows the video's
aspect ratio, so the token count need not divide by the SP size.

Wan2.1-VACE-1.3B, 1696x912 source video, two reference images, Ulysses
SP2 (23 latent frames of 29x53 tokens, 1 padding token), step-1
noise_pred_cond vs. single GPU: rel L2 6.36e-3 before, bit-exact after.
A 1664x960 source (no padding) is bit-exact before and after.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
WanAudioModel (Seko) and WanInfiniteTalkModel have their own
_seq_parallel_pre_process, which pads x like WanModel does but did not
set seq_p_valid_len, so their self-attention still attended to the
padding. InfiniteTalk also calls Ulysses from its own infer_self_attn,
which now passes valid_len. The audio AR model keeps its separate
KV-cache attention, which already handles the true length.

When it pads: Seko's adaptive resize picks bucket sizes whose per-frame
token counts are multiples of 8 except 480x480 (900 tokens), which pads
on SP8 when the latent frame count is odd; keep_ratio_fixed_area and
the other ratio-following modes can give any count.

Not measured on these models (no weights on the test host); they go
through the same Ulysses valid_len path that is bit-exact for Wan T2V,
TI2V and VACE.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
@klin-amd klin-amd changed the title fix(sp): keep sequence-parallel padding out of Wan self-attention K/V Nov 15, 2026
…tion

HunyuanVideo-1.5 pads the image tokens to a multiple of the SP size and
crops them only in unpatchify, so the padded tokens took part in the
joint image/text attention under Ulysses. With the official 480p 16:9
SP8 config, 49290 tokens are padded with 6 to 49296; 720p 16:9 (110205
tokens) pads 1 on SP2 and 3 on SP4/SP8.

Record the unpadded length when padding is added and pass it as
valid_len to the parallel attention, which drops the padding from Q/K/V
and zero-fills its output rows. WorldPlay and WorldPlayBI inherit this
model and its attention call, so they get the same change (not
measured on them).

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
…ttention

Qwen-Image pads the image tokens to a multiple of the SP size but only
crops noise_pred back to the latent length for i2i. For t2i the padded
noise_pred reached the scheduler and failed with "The size of tensor a
(6889) must match the size of tensor b (6890)" whenever the token count
does not divide evenly: the built-in 1:1 size (1328x1328, 6889 tokens) on
any SP size, 3:2 and 2:3 on SP4/SP8, 4:3 and 3:4 on SP8. The padded
tokens also took part in the joint attention.

Record the unpadded length when padding is added, crop the gathered
noise_pred back to it in _seq_parallel_post_process, and pass it as
valid_len to the parallel attention.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
Flux.2 pads the image tokens to a multiple of the SP size and crops
noise_pred afterwards, but the padded tokens took part in the joint
image/text attention under Ulysses. The built-in t2i aspect ratios
divide evenly; custom sizes and i2i inputs do not always. i2i takes the
target size from the first input and appends the inputs as reference
tokens, so one input gives twice its token count (pads on SP4 when that
count is odd, on SP8 when it is not a multiple of 4), and several
inputs, or edit mode where the first input initializes the target, can
give any count.

Record the unpadded length when padding is added and pass it as
valid_len to the double- and single-stream parallel attention.

FLUX.2-klein-4B, Ulysses SP2, step-1 noise_pred vs. single GPU:
- t2i 880x1200 (4125 tokens, 1 padding token): rel L2 1.29e-2 before,
  bit-exact after.
- edit (flux2_klein_i2i_distill_edit_ulysses.json) with img_0.jpg and
  img_1.jpg (7603 tokens, 1 padding token): rel L2 1.13e-2 before,
  bit-exact after.
- i2i with one input (no padding on SP2): bit-exact before and after.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
@klin-amd klin-amd changed the title fix(sp): keep sequence-parallel padding out of Wan attention Nov 15, 2026
…Talk too"

This reverts commit 34a1e19.

Neither change could be checked on a real run. InfiniteTalk picks its
output size from resolution buckets whose sides are multiples of 64, so
the token count always divides by the SP size and the new path is never
taken. Seko's weights are not public. Keep this PR to models that were
run with padding.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>

# Conflicts:
#	lightx2v/common/ops/attn/ulysses_attn.py
…liary tokens

ModelTC#1720 added valid_seq_len to Ulysses for self-attention without auxiliary
tokens. Use it instead of this branch's own valid_len, and let it drop the
padding around auxiliary tokens placed before or after the A2A sequence, as
the joint attention of HunyuanVideo-1.5, Qwen-Image and Flux.2 needs.
Without auxiliary tokens the result is the same as before. Ring attention
takes the same keyword name.

Signed-off-by: Lin, KuanTing <Kuanting.Lin@amd.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant