Skip to content

fix(ltx2): keep sequence-parallel padding out of video attention - #1718

Open
klin-amd wants to merge 13 commits into
ModelTC:mainfrom
klin-amd:dev/klin/ltx2-sp-padding
Open

klin-amd wants to merge 13 commits into
ModelTC:mainfrom
klin-amd:dev/klin/ltx2-sp-padding

Conversation

@klin-amd

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

Copy link
Copy Markdown

Depends on #1713: this branch is stacked on it and passes the unpadded length to the Ulysses attention as valid_seq_len. The LTX-2 changes are the two fix(ltx2) commits.

Problem

LTX2Model._seq_parallel_pre_process pads the video tokens to a multiple of the sequence-parallel size, and the padded tokens take part in two attentions under Ulysses:

  • the video self-attention, and
  • the video-to-audio cross-attention: _gather_cross_attn_context all-gathers the padded video tokens and their rope as keys for the audio queries, so the audio output changes too.

All shipped config sizes divide evenly; others do not, e.g. 544x960 at 241 frames (15810 video tokens) pads 2 on SP4 and 6 on SP8.

Fix

  • Record the unpadded video length when padding is added.
  • Pass it as valid_seq_len to the Ulysses video self-attention.
  • Crop the gathered video context and key rope back to it in _gather_cross_attn_context.

Test

LTX-2 19B dev, t2av, ltx2_ulysses.json settings, 97 frames, seed 42, H100, flash_attn3 on all runs, compared with a single-GPU run on main c840dad. Step-1 noise prediction:

Size Padding on SP2 main This PR
480x800 (4875 video tokens) 1 video rel L2 1.90e-2, audio 1.99e-2 video and audio bit-exact
512x768 (4992 video tokens) 0 video and audio bit-exact video and audio bit-exact

Full 40-step run at 480x800, final video vs. single GPU, measured on the previous revision of this branch (same step-1 result):

Per-frame PSNR, mean / min
main 35.0 / 34.7 dB: visible content change (the headland has a different shape)
This PR 45.5 / 39.8 dB: no visible difference

Step 1 matches bit for bit; where the remaining difference in the final video comes from was not investigated.

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>
…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>
…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>
LTX-2 pads the video tokens to a multiple of the SP size. The padded
tokens took part in two attentions under Ulysses: the video
self-attention, and the video-to-audio cross-attention, which
all-gathers the padded video tokens and their rope as keys for the
audio queries.

Record the unpadded video length when padding is added, pass it as
valid_len to the Ulysses video self-attention, and crop the gathered
video context and key rope back to it.

LTX-2 19B dev, t2av, ltx2_ulysses.json at 480x800, 97 frames (4875
video tokens, 1 padding token on SP2), step-1 noise prediction vs.
single GPU: video rel L2 1.90e-2 and audio 1.99e-2 before, both
bit-exact after. 512x768 (no padding) is bit-exact before and after.
Full 40-step run at 480x800: final video vs. single GPU per-frame PSNR
35.0 dB before (the headland has a different shape and an extra rock)
and 45.5 dB after, with no visible difference.

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>
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