Skip to content

Perf(LTX2): Comprehensive XLA, Memory, and Transformer Code Quality Optimizations#422

Open
Perseus14 wants to merge 1 commit into
mainfrom
ltx2-improvements
Open

Perf(LTX2): Comprehensive XLA, Memory, and Transformer Code Quality Optimizations#422
Perseus14 wants to merge 1 commit into
mainfrom
ltx2-improvements

Conversation

@Perseus14

@Perseus14 Perseus14 commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

LTX2 Improvements, Optimizations & Bug Fixes

This PR introduces a comprehensive overhaul to the LTX2 implementation in MaxDiffusion, targeting architectural cleanliness, layout sharding stability, distributed attention compatibility, and massive improvements to compilation and model-loading speeds.

🌟 Architectural & JAX Optimizations

  • Dataclass Contexts: Replaced dictionary-based contexts with flax.struct.dataclass (LTX2BlockContext), significantly improving code readability and type safety across attention mechanisms.
  • Dynamic Float Scales: Shifted floating-point parameters (guidance_scale, stg_scale) out of static_argnames and introduced boolean triggers (do_cfg, do_stg). This drastically minimizes recompilation overhead and tracing stalls.

⚡ Compilation & Loading Optimizations (Ports of PRs #438 & #443)

  • AOT Caching: Swapped standard @jax.jit for @aot_cache.cached_jit on the main diffusion operations. Included a 2-step fast-warmup logic (with aot_cache.warmup_mode()) in generate_ltx2.py that fully bypasses the prolonged tracing compilations on application startup.
  • Fast Safetensor Weight Loading: Eliminated memory-heavy torch.load and sequential torch2jax routines in ltx2_utils.py. The loader now concurrently maps chunks of .safetensors using a ThreadPoolExecutor and uses a zero-copy reinterpretation technique (tensor.view(torch.uint16).numpy().view(ml_dtypes.bfloat16)) to aggressively parallelize memory ingestion.
  • ICI-Staged Device Put: Replaced raw parameter broadcasts with a staged approach when jax.process_count() == 1. Large tensors (>=64MB) are now pushed directly to device 0 and replicated over ICI rather than enduring slow host-to-device PCIe copies.
  • Parallel Initializations: Common components (VAE, text encoder, scheduler) and the Transformer are now loaded in parallel via background executors during _load_and_init.

🌀 Distributed Attention

  • Ulysses & Ring Attention Support: Fully wired ring_attention and ulysses_attention parameters down through all LTX2VideoTransformerBlock instantiations (affecting both nnx.scan and un-scanned eager block initializations).
  • Cross-Attention Axis Fallbacks: Implemented dynamic axis-mapping for NNXAttentionOp in LTX2 cross-attention mechanisms, ensuring layout rules bind properly when Ulysses sharding configurations are enabled.

@Perseus14
Perseus14 requested a review from entrpn as a code owner June 19, 2026 21:51
@github-actions

Copy link
Copy Markdown

@Perseus14 Perseus14 self-assigned this Jun 19, 2026
@Perseus14
Perseus14 requested a review from prishajain1 June 19, 2026 21:51
@Perseus14 Perseus14 changed the title LTX2.3 improvements and bug fixes Perf(LTX2): Comprehensive XLA, Memory, and Transformer Code Quality Optimizations Jun 19, 2026
@Perseus14
Perseus14 force-pushed the ltx2-improvements branch 4 times, most recently from 3a13196 to 36e0f5d Compare June 20, 2026 09:59
@github-actions

Copy link
Copy Markdown

🤖 Hi @Perseus14, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 I'm sorry @Perseus14, but I was unable to process your request. Please see the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 Hi @Perseus14, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 I'm sorry @Perseus14, but I was unable to process your request. Please see the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 Hi @Perseus14, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 I'm sorry @Perseus14, but I was unable to process your request. Please see the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 Hi @Perseus14, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 I'm sorry @Perseus14, but I was unable to process your request. Please see the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 Hi @Perseus14, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

## 📋 Review Summary

This pull request introduces a highly comprehensive set of optimizations and refactorings for the LTX2 model family, delivering significant enhancements to memory usage, model modularity, and JAX compilation behavior. By transforming static continuous hyperparameters into dynamic inputs, the sweep successfully eliminates expensive compilation timeouts while resolving underlying layout mismatches and unneeded computational overhead. The architectural quality of the codebase is substantially improved, particularly in the block consolidation and cleaner RoPE logic.

🔍 General Feedback

  • Exceptional Block Refactoring: The transition of LTX2VideoTransformer3DModel block application to a single unified apply_block helper and an immutable LTX2BlockContext is a masterclass in reducing hazard-prone code duplication.
  • RoPE & Coordinates Cleanup: Removing the duplicated and dead coordinate generation block in prepare_video_coords and simplifying split-RoPE dimension manipulation are excellent and highly effective hygiene improvements.
  • Robustness in Fallbacks & Dtypes: Incorporating proper warning logs for the noise generator fallback and upfront dtype casting for prompt embeddings ensures both runtime reliability and optimal HBM performance.

Comment thread src/maxdiffusion/pipelines/ltx2/ltx2_pipeline.py
Comment thread src/maxdiffusion/generate_ltx2.py
Comment thread src/maxdiffusion/models/ltx2/text_encoders/torchax_text_encoder.py Outdated
Comment thread src/maxdiffusion/pipelines/ltx2/ltx2_pipeline.py Outdated
Comment thread src/maxdiffusion/models/ltx2/text_encoders/torchax_text_encoder.py Outdated
Comment thread src/maxdiffusion/models/ltx2/text_encoders/torchax_text_encoder.py Outdated
Comment thread src/maxdiffusion/pipelines/ltx2/ltx2_pipeline.py Outdated
Comment thread src/maxdiffusion/models/ltx2/text_encoders/torchax_text_encoder.py Outdated
@github-actions

Copy link
Copy Markdown

🤖 I'm sorry @Perseus14, but I was unable to process your request. Please see the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 Hi @Perseus14, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 I'm sorry @Perseus14, but I was unable to process your request. Please see the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 Hi @Perseus14, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

Copy link
Copy Markdown

🤖 I'm sorry @Perseus14, but I was unable to process your request. Please see the logs for more details.

@Perseus14

Copy link
Copy Markdown
Collaborator Author

@mbohlool Can you please review this?

Comment thread src/maxdiffusion/pipelines/ltx2/ltx2_pipeline.py
entrpn
entrpn previously approved these changes Jul 16, 2026
Comment thread src/maxdiffusion/models/ltx2/transformer_ltx2.py Outdated
Comment thread src/maxdiffusion/models/ltx2/transformer_ltx2.py
@Perseus14
Perseus14 force-pushed the ltx2-improvements branch 6 times, most recently from 0b094c9 to 868e94d Compare July 23, 2026 16:22
@Perseus14
Perseus14 force-pushed the ltx2-improvements branch from ccf5589 to 9c77827 Compare July 23, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants