Skip to content

Studio: send an explicit CFG for every FLUX family on the sd.cpp engine - #12755

Merged
danielhanchen merged 3 commits into
mainfrom
studio/sdcpp-flux-cfg
Oct 6, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio/sdcpp-flux-cfg

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Problem

On the native sd.cpp engine, every FLUX family renders at the wrong CFG. FLUX.1-schnell comes out almost black, FLUX.1-dev looks like a burnt cartoon, FLUX.2-dev turns the subject solid red, and FLUX.2-klein is oversharpened. This happens in both server and one-shot mode.

_map_guidance returned cfg_scale = None for flux.1, flux.2-klein and flux.2-dev:

  • the one-shot argv only adds --cfg-scale when it is not None, so it had none;
  • build_img_gen_request only writes guidance.txt_cfg when it is not None, so the server request had none.

The docstring assumed an unset cfg means sd-cli's 1.0. It does not. At the pinned build (master-813-bfbef5b-u1d02858):

  • sd_sample_params_init sets txt_cfg = 7.0 (src/stable-diffusion.cpp:446). The CLI options and the server defaults both start from it (examples/common/common.cpp:1024, --cfg-scale help "default: 7.0" at 1206).
  • The server copies those defaults into every request (examples/server/routes_sdcpp.cpp:358), and the JSON only overrides txt_cfg when the key is present (examples/common/common.cpp:2148).
  • resolve_guidance has no FLUX case (src/pipeline/request.cpp:276-329). The only model whose cfg it forces is MiniMax-H3. With img_cfg defaulting to 1, a cfg of 7 also turns on the unconditional pass (line 313), so every distilled FLUX step ran the model twice.

So all FLUX families got real CFG 7. FLUX.1 breaks visibly because it is guidance-distilled. FLUX.2 degrades more quietly (oversaturated, oversharpened), which is why it was easy to miss.

Change

_map_guidance (sd_cpp_backend.py) always returns an explicit cfg for FLUX:

  • Guidance-distilled FLUX.1 dev / schnell / Kontext and FLUX.2-dev: cfg 1.0 (one pass) plus the embedded guidance, as before. These are the reference workflow defaults: dev cfg 1 / guidance 3.5, schnell cfg 1, Kontext cfg 1 / guidance 2.5, FLUX.2-dev guidance 4 with no CFG.
  • FLUX.2-klein: this model has no guidance embedder (guidance_embeds: false; sd.cpp only enables the embedder when guidance_in weights exist, src/model/diffusion/flux.hpp:125). It now uses the same real-CFG mapping as the other CFG families: 1.0 for 0 or 1, and the value itself above 1.
    • With Studio's per-model defaults, the distilled 4B / 9B models (guidance 1.0) get cfg 1.
    • The undistilled klein-base (guidance 5.0, the same value the diffusers path passes to Flux2KleinPipeline as real CFG) gets cfg 5. These match the reference workflows.
    • The --guidance value it used to send was ignored by this model, so it is no longer sent.

No signature changes. The request and argv builders are untouched: None still means unset for the families that rely on it.

Results

B200, Studio's own SdCppDiffusionBackend on the pinned sd.cpp build, main (6f4d9974dc) against this branch. Same Q4 GGUF, prompt, seed 42, 1024x1024, steps and guidance per model default. Each arm ran in server mode and in forced one-shot mode (sd-server hidden). The control is main's own one-shot sd-cli argv, replayed with only the cfg (and the embedded guidance) set to the reference workflow's values.

model main sends this PR sends LPIPS vs control, main LPIPS vs control, this PR mean luminance / saturation, main this PR reference workflow render
FLUX.1-dev distilled_guidance 3.5 txt_cfg 1.0, distilled_guidance 3.5 0.598 0.000 0.465 / 0.484 0.378 / 0.328 0.321 / 0.410
FLUX.1-schnell distilled_guidance 0.0 txt_cfg 1.0, distilled_guidance 0.0 0.775 0.000 0.064 / 0.804 0.456 / 0.257 0.325 / 0.340
FLUX.2-klein-4B distilled_guidance 1.0 txt_cfg 1.0 0.484 0.000 0.363 / 0.432 0.463 / 0.329 0.425 / 0.277
FLUX.2-klein-base-4B distilled_guidance 5.0 txt_cfg 5.0 0.246 0.000 0.424 / 0.442 0.411 / 0.432 0.413 / 0.413
FLUX.2-dev distilled_guidance 4.0 txt_cfg 1.0, distilled_guidance 4.0 0.434 0.000 0.370 / 0.415 0.353 / 0.377 0.373 / 0.314
  • LPIPS is vgg; alex agrees within 0.07 in every cell.
  • The sent columns are the captured server request bodies (sample_params.guidance).
    • The one-shot argv matches them: main sends only --guidance <g>; this PR sends --cfg-scale 1 --guidance <g>, --cfg-scale 1 (klein) and --cfg-scale 5 (klein-base).
    • On main, server and one-shot give identical scores. On this branch, the server image, the one-shot image and the control are the same picture (LPIPS 0.000), and for FLUX.1-dev they are byte-identical files.
  • The reference workflow renders the same GGUF with its own sampler noise, so the composition differs. LPIPS against it is 0.57 to 0.87 on both arms and does not separate them. The colour statistics do: schnell goes from near-black (luminance 0.064) to the reference's range, and the saturation of every distilled model drops toward the reference.
  • Render time also drops, since the distilled models run one model pass per step instead of two (server, FLUX.1-dev 87 s to 23 s, FLUX.2-dev 116 s to 57 s). These are shared GPUs, so treat the times as indicative only.

FLUX cfg before and after

Columns: reference workflow render of the same GGUF, the sd.cpp control, main, this PR (server mode).

Tests

  • test_map_guidance_flux_uses_distilled_guidance now expects (1.0, g) for FLUX.1, Kontext and FLUX.2-dev. It previously asserted cfg is None.
  • test_map_guidance_flux2_klein_distilled_off_base_real_cfg: klein at 1.0 gives cfg 1, at 5.0 gives cfg 5, and no embedded guidance is sent.
  • test_flux_oneshot_argv_sends_explicit_cfg and test_flux_server_request_sends_txt_cfg, each parametrized over FLUX.1-dev, schnell, Kontext, FLUX.2-dev, klein 4B / 9B and klein-base 4B / 9B at Studio's own default guidance for that repo. They run generate and check the built sd-cli argv (--cfg-scale, --guidance) and the server body (txt_cfg, distilled_guidance).
  • On main the 18 new or updated cases fail. Here they pass: test_sd_cpp_*.py + test_h3_native_server.py, 1040 passed, 1 skipped.

Not covered

  • Kontext and FLUX.2-klein-9B / klein-base-9B were not rendered. Kontext has no native sd.cpp route today; its mapping is unit-tested only.
  • On the diffusers path, a distilled klein ignores a guidance above 1. On this engine, a guidance above 1 set by hand on a distilled klein now runs real CFG at that value. The per-model defaults never do this.
  • One seed and one prompt per model.

_map_guidance returned no cfg for flux.1, flux.2-klein and flux.2-dev, so
the one-shot argv had no --cfg-scale and the server request had no
guidance.txt_cfg. sd.cpp then applied its own default cfg of 7.0, which
renders FLUX.1-dev and schnell dark or burnt and oversaturates FLUX.2.

Guidance-distilled FLUX (FLUX.1 dev / schnell / Kontext, FLUX.2-dev) now
runs one pass at cfg 1.0 with the embedded guidance. FLUX.2-klein has no
guidance embedder, so it takes the real-CFG mapping: the 4-step models
default to 1.0, the undistilled klein-base to its real CFG of 5.0.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-06T03:30:44.110274Z ebc3a57 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-05T17:06:34.259325Z a129a06 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Oct 6, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: e9308592d1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 7825a6e into main Oct 6, 2026
@danielhanchen
danielhanchen deleted the studio/sdcpp-flux-cfg branch October 6, 2026 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant