feat(flux2klein): Faster Loading + Compliation - #456
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces several performance optimizations and robustness improvements to the FLUX.2-Klein pipeline, including concurrent Ahead-of-Time (AOT) compilation of XLA graphs, fused VAE decoding, direct target-dtype weight loading, and nearest-neighbor upsampling using JAX broadcasting. It also adds support for multiple inference repetitions, profiling, and fallback configurations when local files are missing. The review feedback highlights two critical issues: first, removing the return statement in partition_prompts causes the function to return None when prompt truncation is triggered; second, both the try and except blocks for snapshot downloading use local_files_only=True, which will fail completely if the model is not already cached locally instead of falling back to an online download.
1369c52 to
2bd0d37
Compare
2bd0d37 to
d39315d
Compare
c23dc32 to
56a1119
Compare
bbe0772 to
bb40f15
Compare
…ormer, fast weight loading, and concurrent AOT compilation - Implement NNXFlux2KleinTransformer2DModel, NNXFluxDoubleTransformerBlock, NNXFluxSingleTransformerBlock, and NNX attention blocks - Implement direct-to-state zero-copy weight loader (load_and_convert_flux_klein_nnx_weights) from HuggingFace PyTorch safetensors - Add dual Flax Linen / Flax NNX pipeline support in FlaxFlux2KleinPipeline with concurrent AOT XLA graph pre-compilation - Add CLI generation driver generate_flux2klein.py with SPMD TP/FSDP device mesh sharding - Add automated unit test suite (nnx_flux2klein_test.py) and end-to-end smoke test suite (generate_flux2klein_smoke_test.py)
a5d0e8c to
c31cf5d
Compare
Summary
Significantly improving the model loading and compilation times for the Flux Klein models. This enables faster start-up and lets us iterate faster on model improvements. We also include bug fixes in Flux2.Klein implementation (handling of batch-sizes, making sure all code goes through NNX modules rather than linen).
Optimizations
the model in fp32, but this is not needed anymore.
None of these changes affect the model implementation, so the outputs are still identical.
Results
On a v6-4, we see the following speedups (BS=8, fsdp) for the 9B model:
On a v7-8:
-Warm model loading: 540s -> 140s (for cold loads, use a Hyperdisk mount)
-XLA compilation/warmup-pass: 82s -> 49s
-Inference unchanged
So, on the v6-4: We bring the loading+compilation time down to <30s. On a v7-8, <4 min. The main bottleneck is the hardware constraint of how fast it can load the model.
Correctness
Visual results are high-quality, SSIM changes from 1.00 to 0.9 (still passing all parity checks against PyTorch). Slight changes to SSIM are expected given that using NNX will lead to different graphs than linen, and thus cause different handling of floats. Reference images updated to maintain 1.00 sim on the repo.