Skip to content

fix(flux2): Fix FLUX.2 Klein image generation quality#8838

Merged
lstein merged 7 commits intoinvoke-ai:mainfrom
Pfannkuchensack:fix/flux2-klein-high-resolution-quality
Feb 6, 2026
Merged

fix(flux2): Fix FLUX.2 Klein image generation quality#8838
lstein merged 7 commits intoinvoke-ai:mainfrom
Pfannkuchensack:fix/flux2-klein-high-resolution-quality

Conversation

@Pfannkuchensack
Copy link
Collaborator

@Pfannkuchensack Pfannkuchensack commented Feb 3, 2026

Summary

This commit addresses severe quality degradation and artifacts when generating images with FLUX.2 Klein models.

Root causes fixed:

  1. BN Normalization on noise input (flux2_denoise.py)

    • Random noise was incorrectly normalized before denoising
    • Diffusers only normalizes image latents from VAE.encode(), not random noise
    • Random noise is already N(0,1) distributed - normalization corrupted it
  2. Missing BN Denormalization on output (flux2_denoise.py)

    • Output latents must be denormalized before VAE decode
    • Converts from transformer space to VAE-expected space
    • Without this, images appear washed-out with lost colors
  3. Dynamic mu calculation causing over-shifted schedules (sampling_utils.py)

    • mu was resolution-dependent, causing extreme values at high resolutions
    • At 2048x2048: mu=3.23 compressed almost all sigmas above 0.9
    • Fixed to constant 2.02 (matching ComfyUI's proven configuration)

Impact:

  • All resolutions: Proper color reproduction, no washed-out images
  • High resolutions (>1024x1024): No grid/diamond artifacts
  • FLUX.2 Klein 4B/9B: Both models now produce correct output
  • All generation modes: T2I and reference images

Related Issues / Discussions

#8832

Community-reported quality degradation issues on Discord:

QA Instructions

Test Setup

Use FLUX.2 Klein models (both 4B and 9B variants) with the following configurations:

Critical Test Cases (Priority)

1. Baseline - Standard Resolution:

  • Generate at 1024×1024 with both Klein 4B and 9B
  • Use 4 steps for distilled models, 28 steps for base model
  • Expected: Full color, proper detail, no artifacts

2. High Resolution Test:

  • Test resolutions: 1536×1536, 2048×2048
  • Klein 9B was heavily affected here before fix
  • Expected: Consistent quality, no grid/diamond patterns

3. Color Reproduction:

  • Generate colorful subjects (e.g., "orange tiger in green forest")
  • Verify colors are vibrant, not washed-out or desaturated
  • Expected: Full color saturation throughout denoising

4. Both Generation Modes:

  • Text-to-Image (T2I) generation
  • Reference image editing (Kontext)

What to Look For

Improvements:

  • ✅ No grid/diamond artifacts at any resolution
  • ✅ Full color reproduction (no washed-out images)
  • ✅ Proper detail preservation and coherence
  • ✅ Consistent quality scaling with resolution
  • ✅ Stable denoising process (colors maintained throughout)

Technical Verification:

  • BN stats should be found from VAE (check logs if issues)
  • Latent values should be properly scaled after denormalization

Merge Plan

No special merge considerations needed.

This is a targeted bug fix with:

  • No breaking changes
  • No API changes
  • No database schema changes
  • No migration required

Safe to merge after QA verification.

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable) - Not applicable (algorithmic fix)
  • ❗Changes to a redux slice have a corresponding migration - Not applicable (backend-only changes)
  • Documentation added / updated (if applicable) - Not applicable (internal algorithm fix, code comments added)
  • Updated What's New copy (if doing a release after this PR) - Recommended: "Fixed FLUX.2 Klein image quality issues (color loss, artifacts)"

This commit addresses severe quality degradation and artifacts when
generating images larger than 1024x1024 with FLUX.2 Klein models.

Root causes fixed:

1. Dynamic max_image_seq_len in scheduler (flux2_denoise.py)
   - Previously hardcoded to 4096 (1024x1024 only)
   - Now dynamically calculated based on actual resolution
   - Allows proper schedule shifting at all resolutions

2. Smoothed mu calculation discontinuity (sampling_utils.py)
   - Eliminated 40-50% mu value drop at seq_len 4300 threshold
   - Implemented smooth cosine interpolation (4096-4500 transition zone)
   - Gradual blend between low-res and high-res formulas

Impact:
- FLUX.2 Klein 9B: Major quality improvement at high resolutions
- FLUX.2 Klein 4B: Improved quality at high resolutions
- Baseline 1024x1024: Unchanged (no regression)
- All generation modes: T2I and Kontext (reference images)

Fixes: Community-reported quality degradation issue
See: Discord discussions in #garbage-bin and #devchat

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@github-actions github-actions bot added python PRs that change python files invocations PRs that change invocations backend PRs that change backend files labels Feb 3, 2026
@Pfannkuchensack Pfannkuchensack changed the title fix(flux2): Fix image quality degradation at resolutions > 1024x1024 fix(flux2klein): Fix image quality degradation at resolutions > 1024x1024 Feb 3, 2026
@lstein lstein self-assigned this Feb 4, 2026
@lstein
Copy link
Collaborator

lstein commented Feb 4, 2026

I can confirm that the PR does not degrade image quality or performance, but I'm having a difficult time reproducing the image quality degradation on the main branch for comparison. Is it possible that the issue only appears with certain video cards?

I am going to ask other developers on Discord to do some testing.

@lstein
Copy link
Collaborator

lstein commented Feb 5, 2026

Just updating the comment thread to match conclusions on Discord.

This PR does not fix the underlying issue. At higher step counts, larger image sizes, and/or shorter prompts, both flux.2 klein models produce marked image artifacts. The effect is easily seen at 1024x1024 by generating without a prompt, which produces images with prominent diagonal elements.

JPPhoto and others added 4 commits February 5, 2026 14:46
  Fixes grid/diamond artifacts and color loss at resolutions > 1024x1024.

  Root causes identified and fixed:
  - BN normalization was incorrectly applied to random noise input
    (diffusers only normalizes image latents from VAE.encode)
  - BN denormalization must be applied to output before VAE decode
  - mu parameter was resolution-dependent causing over-shifted schedules
    at high resolutions (now fixed to 2.02, matching ComfyUI)

  Changes:
  - Remove BN normalization on noise input (not needed for N(0,1) noise)
  - Preserve BN denormalization on denoised output (required for VAE)
  - Fix mu to constant 2.02 for all resolutions (matches ComfyUI)

  Tested at 2048x2048 with FLUX.2 Klein 4B
@Pfannkuchensack Pfannkuchensack changed the title fix(flux2klein): Fix image quality degradation at resolutions > 1024x1024 fix(flux2): Fix FLUX.2 Klein image generation quality Feb 6, 2026
Copy link
Collaborator

@JPPhoto JPPhoto left a comment

Choose a reason for hiding this comment

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

Results are worlds better than they were, great job!

@lstein lstein merged commit a42fdb0 into invoke-ai:main Feb 6, 2026
13 checks passed
lstein pushed a commit that referenced this pull request Feb 6, 2026
* fix(flux2): Fix image quality degradation at resolutions > 1024x1024

This commit addresses severe quality degradation and artifacts when
generating images larger than 1024x1024 with FLUX.2 Klein models.

Root causes fixed:

1. Dynamic max_image_seq_len in scheduler (flux2_denoise.py)
   - Previously hardcoded to 4096 (1024x1024 only)
   - Now dynamically calculated based on actual resolution
   - Allows proper schedule shifting at all resolutions

2. Smoothed mu calculation discontinuity (sampling_utils.py)
   - Eliminated 40-50% mu value drop at seq_len 4300 threshold
   - Implemented smooth cosine interpolation (4096-4500 transition zone)
   - Gradual blend between low-res and high-res formulas

Impact:
- FLUX.2 Klein 9B: Major quality improvement at high resolutions
- FLUX.2 Klein 4B: Improved quality at high resolutions
- Baseline 1024x1024: Unchanged (no regression)
- All generation modes: T2I and Kontext (reference images)

Fixes: Community-reported quality degradation issue
See: Discord discussions in #garbage-bin and #devchat

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

* fix(flux2): Fix high-resolution quality degradation for FLUX.2 Klein

  Fixes grid/diamond artifacts and color loss at resolutions > 1024x1024.

  Root causes identified and fixed:
  - BN normalization was incorrectly applied to random noise input
    (diffusers only normalizes image latents from VAE.encode)
  - BN denormalization must be applied to output before VAE decode
  - mu parameter was resolution-dependent causing over-shifted schedules
    at high resolutions (now fixed to 2.02, matching ComfyUI)

  Changes:
  - Remove BN normalization on noise input (not needed for N(0,1) noise)
  - Preserve BN denormalization on denoised output (required for VAE)
  - Fix mu to constant 2.02 for all resolutions (matches ComfyUI)

  Tested at 2048x2048 with FLUX.2 Klein 4B

* Chore Ruff

---------

Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Co-authored-by: Jonathan <34005131+JPPhoto@users.noreply.github.com>
@Pfannkuchensack Pfannkuchensack deleted the fix/flux2-klein-high-resolution-quality branch February 7, 2026 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.11.1 backend PRs that change backend files invocations PRs that change invocations python PRs that change python files

Projects

Status: 6.11.1

Development

Successfully merging this pull request may close these issues.

3 participants