Skip to content

Studio LTX-2.x: drop the all-ones text mask so text cross-attention runs without a bias - #18804

Merged
danielhanchen merged 3 commits into
mainfrom
studio-ltx25-l4-overhead
Nov 22, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-ltx25-l4-overhead

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

LTX-2.x's text connectors replace prompt padding with learned registers and return an all-ones attention mask. The DiT turns that mask into an additive bias of zeros ((1 - mask) * -10000) and passes it to both text cross-attentions in every block. Adding zero changes nothing, but any attn_mask keeps SDPA off the flash kernel and on the mem-efficient one. Under torch.compile, Inductor also materialises the bias at [batch, heads, video tokens, text tokens], which is about 0.9 GB of bf16 zeros written and read on every block at 1280x704x121.

On the L4 profile of Studio's LTX-2.5 defaults this showed up as fmha_cutlassF_bf16_aligned_64x128 (1.75 s per profiled render, one call per block) plus a fused norm + clone_expand kernel that writes the bias (0.73 s).

Change

  • video_ltx2.install_ltx2_full_mask_drop(pipe) registers a forward pre-hook on every LTX2VideoTransformer3DModel in the pipe. When encoder_attention_mask / audio_encoder_attention_mask is a 2-D mask equal to 1 everywhere, the hook passes None in its place. The maths is the same, and SDPA can now pick flash.
  • A padded mask, a 3-D bias, or a positional argument is passed through unchanged. The all-ones check is memoised per tensor and version, so there is one host read per render rather than one per step. If the check raises (for example during a capture), the mask it was checking is kept.
  • video.py installs it next to the HunyuanVideo-1.5 padded-text trim, on every speed tier except off, and reports ltx2_full_mask_drop in speed_optims. UNSLOTH_LTX2_DROP_FULL_TEXT_MASK=0 turns it off.

The output is not bit-identical to main: flash and mem-efficient attention round differently. This is the same trade the HunyuanVideo-1.5 trim already makes.

Tests

New tests/test_video_ltx2_full_mask.py (CPU, a tiny real LTX2VideoTransformer3DModel):

  • test_all_ones_mask_is_dropped_and_the_output_is_unchanged: the stock forward passes a bias to SDPA, while the hooked forward passes none, drops both masks, and gives the same output (allclose 1e-5).
  • test_padded_mask_is_kept: a padded mask still reaches SDPA and the output is bit-identical.
  • Also covered: bool, float and int masks, batches, and a 3-D bias; memoisation per tensor and version; the kill switch and non-LTX DiTs; idempotent install; and a failing check leaving the call unchanged.

Added to tests/test_video_backend.py: test_ltx2_full_mask_drop_installed_and_reported (eager / default / max) fails on main and passes here. test_ltx2_full_mask_drop_skipped_on_the_off_tier passes on both.

test_differential.py: DISCRIMINATING, fixed=3, head_fail=0, 547 unchanged. tests/test_video_backend.py, test_video_ltx25.py, test_video_ltx2_prequant.py, test_diffusion_attention_trim.py: 758 passed, 2 skipped.

GPU results (Colab L4, LTX-2.5 Studio defaults, 1280x704x121, 8 + 3 distilled steps, INT8 DiT streamed)

One VM, one fresh process per arm, in the order main, branch, branch, main, main, branch. Each arm runs a cold render and then two timed renders (seeds 11 and 12). The first arm of each tree starts with an empty home, and later arms reuse that tree's compile cache.

main this branch
Denoise, second render (3 arms) 44.10 s (44.02 to 44.20) 42.44 s (42.36 to 42.49)
Denoise, all six renders, median 44.49 s 42.80 s
Stage-1 step, median 1.993 s 1.955 s
Render wall, median 56.10 s 55.13 s
Cold render (first after load) 173.5 / 185.9 / 174.3 s 175.4 / 179.0 / 179.6 s
peak_alloc_gib 19.009 19.009

The steady denoise is 1.66 s per clip faster (3.8%). The main-vs-main spread is 0.18 s and the two ranges do not overlap. Peak allocation is set elsewhere in the render and does not change. The cold render is a tie.

Profile of one render (2 stage-1 + 3 stage-2 steps):

  • Mem-efficient attention calls: 248 on main, 8 here. The 8 that remain are the connectors' own, which run once per prompt.
  • Bias kernel: the clone_expand kernel that writes the bias (240 calls, 0.6 s) is gone.
  • Denoise time: attention kernel time drops from 11.00 to 9.28 s, and the denoise span from 32.98 to 31.34 s.
  • Kernel count: 24686 kernels on main, 24420 here.
  • GPU busy: 98.4% of the denoise on both, so the step is not launch-bound.

Accuracy. Every main arm gives the same frames (md5 equal across all three), and so does every branch arm. Main vs branch: LPIPS 0.114 / 0.052 and PSNR 20.8 / 32.8 dB on the two clips. That is the same size of change #17680 produced (0.117 / 0.056), which is how strongly this distilled sampler amplifies one-ulp differences. The maths is unchanged: the bias that is removed is all zeros.

Accuracy against a higher-precision reference. I added a third arm on its own L4 VM: main with every attention call run on fp32 q/k/v (fp32 mem-efficient kernel, the mask kept as main passes it, output cast back). Fresh process per arm, order ref / main / branch / ref, same prompts, seeds and shape. LPIPS (alex) and PSNR over all saved frames:

Pair clip 1 LPIPS / PSNR clip 2 LPIPS / PSNR
main vs fp32 ref 0.1059 / 21.46 dB 0.0661 / 31.18 dB
this branch vs fp32 ref 0.1055 / 21.47 dB 0.0609 / 31.43 dB
main vs this branch 0.1140 / 20.79 dB 0.0524 / 32.78 dB
ref vs ref (fresh process) 0.0 (identical) 0.0 (identical)

The branch is no further from the fp32-attention reference than main on either clip, and slightly closer on both.

Not proven here

  • LTX-2.0 / LTX-2.3 renders were not run on a GPU. They do take this change: the text connectors return an all-ones mask for every LTX-2.x config Studio loads (all ship 128 registers, and the connector's final < 1e-6 binarisation maps padded positions to 1 as well), so the bias is dropped there too. A padded mask from any other caller is kept (test_padded_mask_is_kept).
  • With the opt-in INT8, Sage or FlashAttention 4 attention, or ROCm flash attention, the text cross-attentions no longer take the masked native route. They now go to the selected kernel (on ROCm auto, through the timed per-length gate) like every other call. This was not measured here.
  • An explicit FlashAttention 2 / 3 pick on NVIDIA, which diffusers refuses for a masked call, should now get past the text cross-attentions. This was not run.

…uns without a bias

The connectors replace padding with learned registers and hand the DiT an
all-ones mask, which it turns into an all-zero additive bias. That bias
sends every text cross-attention to the mem-efficient kernel and Inductor
materialises it per call. A forward pre-hook replaces an all-ones 2-D mask
with None (same maths); any padded mask is kept. Off on the reference speed
tier and with UNSLOTH_LTX2_DROP_FULL_TEXT_MASK=0.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T20:43:26.448016Z 527fdea Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: da58fd98cf

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 527fdeab6d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit c904f54 into main Nov 22, 2026
@danielhanchen
danielhanchen deleted the studio-ltx25-l4-overhead branch November 22, 2026 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant