Adding KV-Cache Support to Flux2.Klein Image Editing - #465
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for the Flux2Klein model, including new configuration files, NNX-based model implementations for the Transformer and VAE, and a dedicated pipeline. It also adds end-to-end parity and smoke tests. The review identified several critical issues: missing imports in flux2klein_pipeline.py and generate_flux2klein.py that will cause runtime errors, and the incorrect use of nnx.silu instead of jax.nn.silu across multiple model files.
f04bf0e to
af7a73f
Compare
11ca39d to
1a0cfc5
Compare
1a0cfc5 to
6190941
Compare
3850b6b to
6121acd
Compare
| return float(ssim(arr1, arr2, channel_axis=-1)) | ||
|
|
||
|
|
||
| def find_model_path(): |
There was a problem hiding this comment.
Nit: find_model_path() contains hardcoded machine paths (/mnt/hyperdisk_weights/..., /mnt/data/...). We can use get_model_snapshot_dir() (as in edit_flux2klein_e2e_test.py) for clean HF_HOME resolution.
|
|
||
| import jax | ||
| import jax.numpy as jnp | ||
| import flax |
There was a problem hiding this comment.
Nit: import flax, import flax.linen as nn, and nn_partitioning appear to be unused
…age editing with E2E parity and smoke tests - Implement prefix extraction phase (step 0) and cached denoising scan loop in FlaxFlux2KleinPipeline - Support KV cache slicing and concatenation across Flax NNX Double and Single transformer blocks - Safely bound Splash/Flash attention block sizes for asymmetric cross-attention sequences with opt-in flag - Add CLI and config support for use_kv with dynamic FLUX.2-klein-9b-kv repository resolution - Add cross-framework E2E parity test achieving 0.8881 SSIM / 22.58 dB PSNR - Add 9B KV-cache image editing smoke test with verified golden reference image
6121acd to
889bf70
Compare
Summary
Speeding up Image Editing on Flux2.Klein models by onboarding Flux2.Klein-9B-KV model variant. This is a separate model (same architecture, different weights) that was trained to support caching KV for reference images during image editing.
Builds on PR 461 so some overlapping code changes are included in this PR and its listed as two commits until 461 is merged.
Usage
Just adding the use_kv=True flag to the 9B model call will switch to the KV-cache model support.
Speed-Ups
Block-sizes can be tuned further for each setting. Using default settings still gives substantial improvements (using CP=2 on a TPU v7-2)
Visual Verification
Prompt: change the painting so she is facing forward instead of looking over her shoulder
Original:

Edited:

Correctness
KV-Cache implementation does not cause any regression or change in behavior on other flux2.klein pipelines.
Below numbers generated on v6-4.
Note: KV-Cache requires a separate model checkpoint/training setup and is only supported on the 9B model.