Skip to content

Adding Image Editing to Flux2.Klein Models - #461

Draft
amepas wants to merge 9 commits into
mainfrom
onboarding-imageedit-flux2klein
Draft

Adding Image Editing to Flux2.Klein Models#461
amepas wants to merge 9 commits into
mainfrom
onboarding-imageedit-flux2klein

Conversation

@amepas

@amepas amepas commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Summary:

Adds Image Editing Support to the Flux2.Klein 4B and 9B models. Supports both single-image and multi-reference image editing (conditioning image generation on up to 4 reference images alongside textual prompts).

Key Additions

  • Updated VAE: Added support for VAE Encoder to encode input images.
  • Multi-Reference Conditioning: Accepts up to 4 input images via CLI.
  • Zero T2I Regression: Verified zero accuracy loss or performance regressions on existing pipelines.
  • Kernel Stability: Explicitly set use_base_exp2=True to eliminate mathematical precision loss when running the custom 2D Ring Ulysses attention kernel (ulysses_ring_custom_fixed_m).
  • Test Coverage: Added comprehensive smoke and parity tests for single/multi-image editing workflows.

Usage

Code is configured to re-use all possible pipeline components and existing entry points. Calling image editing only requires a flag pointing to the image file locations.

python generate_flux2klein.py \
  prompt="add a bunch of animals swimming in the water in front of the castle and get rid of all birds" \
  image_paths="['/path/to/img1.png', '/path/to/img2.png']"

Runtime:

Editing single image of size 1024 by 1024 with the Flux2.Klein 9B using the Optimal TPU-v7 T2I settings from PR #458.

Pipeline Stage TPU v7-2 (Optimal T2I settings) TPU v6-4 (TP=4)
VAE Encoding 24.28 ms 26.47 ms
Qwen3 Encoding 8.60 ms 7.35 ms
Denoising Loop 640.19 ms 1204.24 ms
VAE Decoding 31.11 ms 34.02 ms
Image Saving 107.76 ms 113.21 ms
Total Inference (Excl. Saving) 707.75 ms 1276.39 ms
Total E2E Pipeline 815.51 ms 1389.50 ms

Correctness:

E2E Parity Test against Diffusers pytorch baseline shows SSIM of 0.80 (both running bfloat16). Test included!

Visual Verification

Example 1:

Prompt: "add a bunch of animals swimming in the water in front of the castle and get rid of all birds"

Original:
image

Edited:
image

Example 2:

Prompt: "change the painting so she is facing forward instead of looking over her shoulder"

Original:
image

Edited:
image

Future Work

KV Cache not supported in this PR.

amepas added 8 commits August 14, 2026 01:44
…weight loading, causal splash attention, and end-to-end optimizations

- Implemented Flax NNX Transformer architecture (NNXFlux2KleinTransformer2DModel) with support for both 4B (5 double / 20 single layers) and 9B (8 double / 24 single layers) configurations.
- Integrated Flax Qwen3 text encoder with 3-layer intermediate hidden states extraction (layers 9, 18, 27), custom causal splash attention, and proper sharding constraints.
- Implemented FlaxAutoencoderKL VAE decoder with fused batch normalization unscaling and channel re-layout.
- Added fused end-to-end denoising loop scan with Flow Match Euler scheduler.
- Added concurrent AOT XLA compilation across Qwen3, Flux transformer, and VAE.
- Implemented fast host-memory streaming weight converter for safetensors shards directly into NNX State PyTree.
- Optimized splash attention block sizes and Ulysses context parallelism sharding.
- Added comprehensive unit tests (nnx_flux2klein_test.py) and end-to-end smoke test suite (generate_flux2klein_smoke_test.py).
… & 9B)

- Implement NNXAutoencoderKLFlux2 (pure Flax NNX VAE encoder and decoder) with zero-copy weight loading and verified float32 parity
- Implement multi-image conditioning helpers (prepare_multi_image_ids, prepare_image_latents, patchify/unpatchify)
- Implement FlaxFlux2KleinImageEditPipeline for end-to-end multi-image editing inference
- Implement generate_flux2klein_image_edit.py CLI runner and default YAML configs for 4B and 9B
- Add comprehensive unit tests and parity test suite across all 7 implementation phases
…o single pipeline and CLI

- Refactored FlaxFlux2KleinPipeline to conditionally handle multi-image editing when images are provided, eliminating duplicate pipeline definitions.
- Unified generate_flux2klein.py and base configs (base_flux2klein.yml, base_flux2klein_9B.yml) to accept image_paths for multi-image conditioning.
- Added test_flux2klein_4b_image_edit_smoke to generate_flux2klein_smoke_test.py.
- Added test_flux2klein_image_edit_e2e_parity.py for end-to-end parity verification against PyTorch Diffusers reference.
… obsolete dev tests

- Deleted separate YAML configs (base_flux2klein_image_edit.yml, base_flux2klein_image_edit_9B.yml).
- Deleted separate CLI runner (generate_flux2klein_image_edit.py) and separate pipeline (flux2klein_image_edit_pipeline.py).
- Removed obsolete intermediate parity/scratch scripts in favor of test_flux2klein_image_edit_e2e_parity.py and generate_flux2klein_smoke_test.py.
- Cleaned up export in maxdiffusion.pipelines.flux.__init__.
- Renamed test_flux2klein_image_edit_e2e_parity.py to edit_flux2klein_e2e_test.py.
- Added test_flux2klein_4b_image_edit_smoke and test_flux2klein_9b_image_edit_smoke in generate_flux2klein_smoke_test.py.
- Added golden reference images ref_flux2klein_4b_image_edit.png and ref_flux2klein_9b_image_edit.png conditioned on ref_flux2klein_4b.png with prompt 'change the lighting to evening'.
- Verified SSIM >= 0.95 assertion on both 4B and 9B image edit smoke tests on TPU VM.
…e loading

- Replace img.max() > 1.0 heuristic with dtype-aware normalization in FlaxFlux2KleinPipeline
- Handle integer uint8 and floating point ranges explicitly
- Add try-except error handling for reference image loading in generate_flux2klein
- Remove duplicate unpatchify_latents in models/flux/util.py
- Apply pyink formatting and resolve ruff linter issues
@github-actions

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces support for the FLUX.2-Klein model in MaxDiffusion, adding the top-level NNXFlux2KleinTransformer2DModel and NNXAutoencoderKLFlux2 VAE models, along with image editing capabilities, concurrent AOT compilation, and end-to-end parity tests. The review feedback highlights several critical runtime issues that must be addressed, including missing imports (snapshot_download, Image, and time) and the use of non-existent Flax NNX API functions (nnx.to_flat_state and nnx.from_flat_state), which should be replaced with .flat() and nnx.State.from_flat() respectively.

Comment thread src/maxdiffusion/generate_flux2klein.py
Comment thread src/maxdiffusion/models/flux/util.py
Comment thread src/maxdiffusion/models/flux/util.py
Comment thread src/maxdiffusion/models/flux/vae/autoencoder_kl_flux2_nnx.py
Comment thread src/maxdiffusion/models/flux/vae/autoencoder_kl_flux2_nnx.py
Comment thread src/maxdiffusion/pipelines/flux/flux2klein_pipeline.py
Comment thread src/maxdiffusion/pipelines/flux/flux2klein_pipeline.py
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