Repository navigation
[Perf] diffusion: Project LTX-2 prompt K/V once per denoising loop - #46105
Merged
Merged
Conversation
LTX-2's prompt cross-attentions (attn2, audio_attn2) re-projected the caption embeddings and the prompt K/V in every block of every step, although neither the text nor the DiT weights change inside a denoising loop (the LoRA phase switches before it). The DiT now projects them once per loop into persistent per-block buffers (prepare_prompt_kv), and the forward reads them through cached_prompt_kv. The buffers keep their addresses, so a breakable CUDA graph captured at warmup keeps reading the current request's K/V. The same GEMMs run on the same inputs, so outputs are bitwise identical. LTX-2.3 modulates the prompt per step (cross_attention_adaln) and keeps the per-step projection; layerwise offload and cache-dit keep it too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
mickqian
requested review from
AgainstEntropy,
BBuf,
HaiShaw,
kevin-mii,
niehen6174,
ping1jing2 and
yichiche
as code owners
October 23, 2026 16:14
Collaborator
Author
|
/tag-and-rerun-ci |
Merged
1 task done
This was referenced Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Branch: mick/ltx2-prompt-kv-once-per-loop (on main 02b9836), commit 8037571
Motivation
LTX-2 re-projects the prompt in every block of every denoising step. The 1024-token caption goes through
caption_projection(two GEMMs, video and audio), and then each block's two prompt cross-attentions (attn2,audio_attn2) project it to K and V and RMS-normalize K. Neither the text nor the DiT weights change inside a denoising loop: the LoRA phase switch runs before the loop. On 4xB300 (CFG parallel, BCG) these are ~3.4 ms of every 55 ms stage-1 step. TensorRT-LLM caches the same projections per request.Modifications
models/dits/ltx_2.pyLTX2Attention.project_context_kv(context)returns the normed K and the V thatforwardwould project.forwardtakescontext_kv=(k, v)and skipsto_k/to_v/k_norm.LTX2VideoTransformer3DModel.prepare_prompt_kv(encoder_hidden_states, audio_encoder_hidden_states)runs the caption projections and every block's prompt K/V once. It writes them into persistent per-block buffers keyed by the text shapes and returns that key. The forward takescached_prompt_kv=<key>, skips the caption projections and hands each block its K/V. The buffers keep their addresses across requests, so a breakable CUDA graph captured at warmup reads the current request's K/V.caches_prompt_kvis false when the prompt is adaLN-modulated per step (cross_attention_adaln, LTX-2.3), whose K/V depend on the step.ltx_2/denoising.py: on the official CFG path,_use_cached_ltx2_prompt_kvprepares the K/V on the loop's first step (LTX2DenoisingContext.prompt_kv_key) and passes the key to every step. It keeps the per-step projection under layerwise offload (weights stream per block) and cache-dit.Tests
test_ltx2_prompt_kv_cache.pycached_prompt_kvis bitwise equal to the per-step projection.caches_prompt_kvis false withcross_attention_adaln.test_ltx2_bcg_coords.py: its stub DiT reads the prompt itself, so the step test stubs the new hook.Accuracy Tests
4xB300 (2 GPUs used), LTX-2 two-stage 768x512x121, 40 steps, CFG parallel + breakable CUDA graphs. MP4s of 4 requests are byte-identical (md5) to main + #45965 / #45977 / #46047.
Speed Tests and Profiling
Same setup,
SGLANG_DIFFUSION_SYNC_STAGE_PROFILING=1, median of 3 steady requests:Checklist
🤖 Generated with Claude Code
CI States
Latest PR Test (Base): ✅ Run #39985162727
Latest PR Test (Extra): ❌ Run #39985162307
Latest PR Test (AMD ROCm 10): ❌ Run #39985162880