Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: send an explicit CFG for every FLUX family on the sd.cpp engine (#12755)
Daniel Han7825a6ecec8e
diff --git a/studio/backend/core/inference/sd_cpp_backend.py b/studio/backend/core/inference/sd_cpp_backend.pyindex 48ef88f4a50..85b99238146 100644--- a/studio/backend/core/inference/sd_cpp_backend.py+++ b/studio/backend/core/inference/sd_cpp_backend.py@@ -2232,11 +2232,10 @@ def _estimate_eta(total_steps: int, step: int, first_step_at: float, now: float)def _map_guidance(fam: DiffusionFamily, guidance: Optional[float]) -> tuple[Optional[float], Optional[float]]:- """(cfg_scale, guidance) for sd-cli from the single diffusers ``guidance`` value. FLUX families- take a distilled embedded ``--guidance``; everyone else uses real classifier-free- ``--cfg-scale``. A distilled 0/1 means CFG off (sd-cli's 1.0); a value > 1 is real CFG."""- if fam.name in ("flux.1", "flux.2-klein", "flux.2-dev"):- return None, (float(guidance) if guidance is not None else None)+ """(cfg_scale, guidance) for sd-cli. Guidance-distilled FLUX runs cfg 1.0 plus the embedded guidance; the rest+ (FLUX.2-klein included: no guidance embedder) use real CFG, 1.0 when <= 1. Always explicit: sd.cpp defaults to 7.0."""+ if fam.name in ("flux.1", "flux.1-kontext", "flux.2-dev"):+ return 1.0, (float(guidance) if guidance is not None else None)if fam.name == "z-image":# diffusers Z-Image computes pos + g * (pos - neg), so its g is standard CFG minus 1 (sd.cpp's cfg 4 == g 3).return (float(guidance) + 1.0 if guidance is not None and guidance > 0.0 else 1.0), Nonediff --git a/studio/backend/tests/test_sd_cpp_backend.py b/studio/backend/tests/test_sd_cpp_backend.pyindex 866c2fded01..ecd40814a30 100644--- a/studio/backend/tests/test_sd_cpp_backend.py+++ b/studio/backend/tests/test_sd_cpp_backend.py@@ -744,8 +744,65 @@ def test_the_moved_sd_cpp_assets_keep_their_upstream_relative_paths():def test_map_guidance_flux_uses_distilled_guidance():- cfg, g = _map_guidance(detect_family("flux.1"), 3.5)- assert cfg is None and g == 3.5+ # cfg must be explicit: unset, sd.cpp applies its default 7.0 and FLUX.1 renders dark or burnt.+ assert _map_guidance(detect_family("flux.1"), 3.5) == (1.0, 3.5)+ assert _map_guidance(detect_family("flux.1"), 0.0) == (1.0, 0.0)+ assert _map_guidance(detect_family("flux.1-kontext"), 2.5) == (1.0, 2.5)+ assert _map_guidance(detect_family("flux.2-dev"), 4.0) == (1.0, 4.0)+ assert _map_guidance(detect_family("flux.1"), None) == (1.0, None)+++def test_map_guidance_flux2_klein_distilled_off_base_real_cfg():+ assert _map_guidance(detect_family("flux.2-klein"), 1.0) == (1.0, None)+ assert _map_guidance(detect_family("flux.2-klein"), 5.0) == (5.0, None)+ assert _map_guidance(detect_family("flux.2-klein"), None) == (1.0, None)+++# (repo, family, cfg, embedded guidance) at Studio's per-model default guidance.+_FLUX_DEFAULT_GUIDANCE_CASES = [+ ("unsloth/FLUX.1-dev-GGUF", "flux.1", 1.0, 3.5),+ ("unsloth/FLUX.1-schnell-GGUF", "flux.1", 1.0, 0.0),+ ("unsloth/FLUX.1-Kontext-dev-GGUF", "flux.1-kontext", 1.0, 2.5),+ ("unsloth/FLUX.2-dev-GGUF", "flux.2-dev", 1.0, 4.0),+ ("unsloth/FLUX.2-klein-4B-GGUF", "flux.2-klein", 1.0, None),+ ("unsloth/FLUX.2-klein-9B-GGUF", "flux.2-klein", 1.0, None),+ ("unsloth/FLUX.2-klein-base-4B-GGUF", "flux.2-klein", 5.0, None),+ ("unsloth/FLUX.2-klein-base-9B-GGUF", "flux.2-klein", 5.0, None),+]++[email protected]("repo_id,fam_name,cfg,distilled", _FLUX_DEFAULT_GUIDANCE_CASES)+def test_flux_oneshot_argv_sends_explicit_cfg(repo_id, fam_name, cfg, distilled):+ from core.inference.diffusion_families import default_generation_params+ from core.inference.sd_cpp_args import build_sd_cpp_command++ steps, guidance = default_generation_params(repo_id)+ eng = _FakeEngine()+ b = _loaded_backend(fam_name, engine = eng)+ b.generate(prompt = "a fox", steps = steps, guidance = guidance, seed = 1)+ files, params, out, _kw = eng.calls[-1]+ argv = build_sd_cpp_command("/bin/sd-cli", files, params, output_path = str(out))+ assert float(argv[argv.index("--cfg-scale") + 1]) == cfg+ if distilled is None:+ assert "--guidance" not in argv+ else:+ assert float(argv[argv.index("--guidance") + 1]) == distilled++[email protected]("repo_id,fam_name,cfg,distilled", _FLUX_DEFAULT_GUIDANCE_CASES)+def test_flux_server_request_sends_txt_cfg(repo_id, fam_name, cfg, distilled):+ import dataclasses++ from core.inference.diffusion_families import default_generation_params++ steps, guidance = default_generation_params(repo_id)+ b = _loaded_backend(fam_name)+ server = _FakeServer("/bin/sd-server")+ b._state = dataclasses.replace(b._state, mode = "server", server = server)+ b.generate(prompt = "a fox", steps = steps, guidance = guidance, seed = 1)+ g = server.payloads[-1]["sample_params"]["guidance"]+ assert g["txt_cfg"] == cfg+ assert g.get("distilled_guidance") == distilleddef test_map_guidance_cfg_family_off_when_distilled():