Conversation
There was a problem hiding this comment.
Code Review
This pull request renames the Flux transformer module, updates configurations to use Ulysses attention with custom block sizes, optimizes layer normalization calculations, and adds profiling support. A critical issue was identified in attention_flax.py where a missing null check on image_rotary_emb could lead to an AttributeError when it is None.
Perseus14
force-pushed
the
flux_opt
branch
13 times, most recently
from
August 6, 2026 15:37
214a291 to
255a1d2
Compare
…nd Flux.2-klein - Implemented performance profiling, kernel fusion, instruction scheduling, and latency optimizations for the Flux model family (Flux.1-dev, Flux.1-schnell, Flux.2-klein-4B/9B) on Cloud TPU v6e (Trillium). - Enabled ulysses_custom_fixed_m attention kernel with base-2 exponential scaling (use_base2_exp: True). - Automatically routes heads_per_tile=1 for fixed-m attention dispatches (preserving heads_per_tile>1 tile batching for non-fixed-m kernels). - Enabled LP LLO instruction scheduler in Pallas (use_experimental_scheduler: True). - Fused Rotary Position Embeddings (RoPE) directly on (B, L, H, D) layout, eliminating 228 redundant transpose/swapaxes ops. - Fused LayerNorm scale and shift calculations into single multiply-add ops. - Maintained latents in native bfloat16 for VAE decoding, cutting VAE decode time by 54.2%. - Fixed null check for image_rotary_emb in FlaxFluxAttention to prevent AttributeError.
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.
Summary
This PR implements comprehensive performance profiling, kernel fusion, instruction scheduling, and latency optimizations for the Flux model family (
FLUX.1-dev,FLUX.1-schnell, andFLUX.2-klein-4B/9B) on Google Cloud TPU v6e (Trillium).Across cumulative optimization passes on TPU v6e-8 (1024×1024 resolution):
FLUX.1-dev(50 steps): End-to-end steady-state latency dropped from 8.904 s → 7.780 s (-1.124 s per image, 152.84 ms/step, 6.54 iterations/sec).FLUX.1-schnell(4 steps): End-to-end latency reduced from 0.860 s → 0.749 s (< 0.75 s total / image).FLUX.2-klein-4B(4 steps): Achieved sub-400ms generation at 0.394 s (63.92 ms/step, 15.64 iterations/sec).FLUX.2-klein-9B(4 steps): Achieved sub-700ms generation at 0.686 s (137.05 ms/step, 7.30 iterations/sec).Key Changes
Cauchy-Schwarz Fixed-$m$ Attention Kernel (
ulysses_custom_fixed_m):ulysses_custom_fixed_mutilizing Cauchy-Schwarz bound softmax and base-2 exponential scaling (use_base2_exp: True).Pallas Multi-Head Tile Batching (
heads_per_tile: 3 / 4):Pallas LP LLO Instruction Scheduler (
use_experimental_scheduler: True):XLA_TPU_FORCE_LP_LLO_SCHEDULERvia Pallas compiler parameters.Fused Direct RoPE & Transpose Elimination:
apply_rope) to run directly on(B, L, H, D)sequence-first tensors inattention_flax.pyandtransformer_flux.py.transposeandswapaxesops across 57 layers.Fused AdaLN-Zero Math:
normalization_flax.pyandtransformer_flux.py.Native BF16 VAE Latent Decode:
bfloat16duringvae_decodeingenerate_flux.pyandflux2klein_pipeline.py.Dynamic Text Truncation & Pre-Broadcasted Global Modulation:
base_flux_dev.ymlandbase_flux2klein.ymlfor standard prompts.jnp.expand_dimscalls per pass.Module Renaming:
transformer_flux_flax.py→transformer_flux.pyfor module naming consistency across MaxDiffusion and updated all imports.Performance Comparison Matrix (Cloud TPU v6e-8, 1024×1024 Resolution)
FLUX.1-dev(50 steps)FLUX.1-schnell(4 steps)FLUX.2-klein-4B(4 steps)FLUX.2-klein-9B(4 steps)Component-Level Benchmarks
FLUX.1-dev): 8.663 s → 7.642 s (-1.021 s)FLUX.1-schnell): 0.693 s → 0.611 s (-82 ms)Verification & Testing
pyink src/maxdiffusion --pyink-indentation=2 --line-length=125).ruff check src/).