Skip to content

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.

Studio diffusion: fused FLUX AdaLN on by default on NVIDIA Linux (2.4% per eager step, fp32 error unchanged) (#16760)

Daniel Han8033ef3ff22a
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index d3b1a174a3f..a045e158cfd 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -10608,6 +10608,8 @@ class DiffusionBackend:
backend = "image",
audit = load_audit,
logger = logger,
+ explicit_off = speed_mode is not None
+ and normalize_speed_mode(speed_mode) == SPEED_OFF,
)
if speed_deferred:
from .diffusion_qwenimage21_eager import install_for_pipe as q21_eager
diff --git a/studio/backend/core/inference/diffusion_autoopt/apply.py b/studio/backend/core/inference/diffusion_autoopt/apply.py
index 24a8f13ee04..44378cf0b21 100644
--- a/studio/backend/core/inference/diffusion_autoopt/apply.py
+++ b/studio/backend/core/inference/diffusion_autoopt/apply.py
@@ -188,6 +188,7 @@ def install_patches(
backend: str = "image",
audit: Optional[Audit] = None,
logger: Any = None,
+ explicit_off: Optional[bool] = None,
) -> dict:
"""Run one load-time patch stage (``load_eager`` / ``load``) for ``pipe``."""
ctx = patches.PatchContext(
@@ -197,6 +198,7 @@ def install_patches(
dtype = dtype,
device = device,
speed_mode = speed_mode,
+ explicit_off = explicit_off,
backend = backend,
logger = logger,
)
diff --git a/studio/backend/core/inference/diffusion_autoopt/patches.py b/studio/backend/core/inference/diffusion_autoopt/patches.py
index 0fa50db4063..30197c20145 100644
--- a/studio/backend/core/inference/diffusion_autoopt/patches.py
+++ b/studio/backend/core/inference/diffusion_autoopt/patches.py
@@ -186,6 +186,8 @@ class PatchContext:
dtype: Any = None
device: Any = None
speed_mode: Optional[str] = None
+ # The user asked for speed "off" (an unset request also resolves to off on dense loads); None = speed_mode == "off".
+ explicit_off: Optional[bool] = None
offload_active: bool = False
denoiser_offloaded: Optional[bool] = None
backend: str = "image"
@@ -422,7 +424,11 @@ def _builtin_specs() -> list[PatchSpec]:
def rocm_fused(ctx: PatchContext) -> dict:
from ..diffusion_rocm_fused import install_for_pipe
- return install_for_pipe(ctx.pipe, ctx.dtype, ctx.device, ctx.logger)
+
+ exact = ctx.explicit_off
+ if exact is None:
+ exact = str(ctx.speed_mode or "").strip().lower() == "off"
+ return install_for_pipe(ctx.pipe, ctx.dtype, ctx.device, ctx.logger, adaln = not exact)
def rocm_fused_uninstall() -> None:
from ..diffusion_rocm_fused import uninstall
diff --git a/studio/backend/core/inference/diffusion_rocm_fused.py b/studio/backend/core/inference/diffusion_rocm_fused.py
index a61767d5509..2cc4e168363 100644
--- a/studio/backend/core/inference/diffusion_rocm_fused.py
+++ b/studio/backend/core/inference/diffusion_rocm_fused.py
@@ -2,8 +2,8 @@
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Triton RoPE (auto on ROCm and NVIDIA CUDA Linux) and AdaLN kernels for FLUX; inference only,
-never while compiling; AdaLN off by default (not bit-identical); UNSLOTH_DIFFUSION_FUSED_ROPE,
-UNSLOTH_DIFFUSION_FUSED_ADALN"""
+never while compiling; AdaLN auto on NVIDIA CUDA Linux off the ``off`` speed tier (one rounding instead of three, not
+bit-identical); UNSLOTH_DIFFUSION_FUSED_ROPE, UNSLOTH_DIFFUSION_FUSED_ADALN"""
from __future__ import annotations
@@ -80,7 +80,7 @@ def _is_nvidia_linux() -> bool:
# gfx1151 klein 1024^2: RoPE 8.76 -> 8.09 s pixel-identical; AdaLN adds no speed and moves pixels (LPIPS 0.004).
_AUTO_ON = {FUSED_ROPE_ENV: True, FUSED_ADALN_ENV: False}
-_AUTO_ON_NVIDIA = {FUSED_ROPE_ENV: True, FUSED_ADALN_ENV: False}
+_AUTO_ON_NVIDIA = {FUSED_ROPE_ENV: True, FUSED_ADALN_ENV: True}
def _switch_on(env: str) -> bool:
@@ -511,8 +511,10 @@ def install_for_pipe(
dtype: Any,
device: Any = "cuda",
logger: Any = None,
+ *,
+ adaln: bool = True,
) -> dict:
- """Install for a FLUX.1 / FLUX.2 denoiser, otherwise restore stock."""
+ """Install for a FLUX.1 / FLUX.2 denoiser, otherwise restore stock; ``adaln=False`` (speed off) keeps stock AdaLN."""
transformer = getattr(pipe, "transformer", None)
module_name = type(transformer).__module__ if transformer is not None else ""
if module_name not in _MODULES:
@@ -520,7 +522,7 @@ def install_for_pipe(
return {"rope": False, "adaln": 0}
uninstall()
rope = install_rope(module_name, dtype, device)
- adaln = install_adaln(dtype, device)
+ adaln = install_adaln(dtype, device) if adaln else 0
if logger is not None and (rope or adaln):
try:
logger.info(
diff --git a/studio/backend/tests/test_diffusion_rocm_fused.py b/studio/backend/tests/test_diffusion_rocm_fused.py
index 76dc8e4971d..60b42cf3547 100644
--- a/studio/backend/tests/test_diffusion_rocm_fused.py
+++ b/studio/backend/tests/test_diffusion_rocm_fused.py
@@ -69,10 +69,29 @@ def _nvidia(monkeypatch, platform = "linux"):
@pytest.mark.parametrize("dtype", [torch.bfloat16, torch.float16])
-def test_auto_engages_rope_only_on_nvidia_linux(fake_kernels, monkeypatch, dtype):
+def test_auto_engages_rope_and_adaln_on_nvidia_linux(fake_kernels, monkeypatch, dtype):
_nvidia(monkeypatch)
- assert rf.install_for_pipe(_PipeLike(), dtype, "cuda") == {"rope": True, "adaln": 0}
+ assert rf.install_for_pipe(_PipeLike(), dtype, "cuda") == {"rope": True, "adaln": 3}
assert fmod.apply_rotary_emb is rf._ROPE_FNS[rf._MODULES[0]]
+ assert nm.AdaLayerNormZero.forward is rf._adaln_zero_forward
+
+
[email protected]("dtype", [torch.bfloat16, torch.float16])
+def test_the_exact_tier_keeps_stock_adaln_on_nvidia(fake_kernels, monkeypatch, dtype):
+ _nvidia(monkeypatch)
+ rf.install_for_pipe(_PipeLike(), dtype, "cuda")
+ assert rf.install_for_pipe(_PipeLike(), dtype, "cuda", adaln = False) == {
+ "rope": True,
+ "adaln": 0,
+ }
+ assert fmod.apply_rotary_emb is rf._ROPE_FNS[rf._MODULES[0]]
+ assert nm.AdaLayerNormZero.forward is _STOCK_FWD["AdaLayerNormZero"]
+
+
+def test_the_adaln_kill_switch_on_nvidia(fake_kernels, monkeypatch):
+ _nvidia(monkeypatch)
+ monkeypatch.setenv(rf.FUSED_ADALN_ENV, "0")
+ assert rf.install_for_pipe(_PipeLike(), torch.bfloat16, "cuda") == {"rope": True, "adaln": 0}
assert nm.AdaLayerNormZero.forward is _STOCK_FWD["AdaLayerNormZero"]
@@ -97,6 +116,7 @@ def test_auto_is_inert_without_a_gpu_torch(fake_kernels, monkeypatch):
def test_kill_switch_on_nvidia(fake_kernels, monkeypatch, value):
_nvidia(monkeypatch)
monkeypatch.setenv(rf.FUSED_ROPE_ENV, value)
+ monkeypatch.setenv(rf.FUSED_ADALN_ENV, value)
assert rf.install_for_pipe(_PipeLike(), torch.bfloat16, "cuda") == {"rope": False, "adaln": 0}
assert fmod.apply_rotary_emb is stock_rope
@@ -110,7 +130,7 @@ def test_flux2_fp16_keeps_its_own_patch_on_nvidia(fake_kernels, monkeypatch):
pipe = _PipeLike("diffusers.models.transformers.transformer_flux2")
try:
assert fr.install_for_pipe(pipe, torch.float16, "cuda")
- assert rf.install_for_pipe(pipe, torch.float16, "cuda") == {"rope": False, "adaln": 0}
+ assert rf.install_for_pipe(pipe, torch.float16, "cuda") == {"rope": False, "adaln": 3}
assert f2mod.apply_rotary_emb is fr._fused_apply_rotary_emb
finally:
fr.uninstall()
@@ -230,7 +250,7 @@ def test_load_and_teardown_wiring():
diffusion_rocm_fused.uninstall,
)
try:
- diffusion_rocm_fused.install_for_pipe = lambda *a: seen.append(("install", a)) or {
+ diffusion_rocm_fused.install_for_pipe = lambda *a, **k: seen.append(("install", a, k)) or {
"rope": False,
"adaln": 0,
}
@@ -241,10 +261,41 @@ def test_load_and_teardown_wiring():
ap.PatchContext(pipe = pipe, dtype = "bf16", device = "cuda", logger = "log", speed_mode = "off"),
)
diffusion._uninstall_fused_dit_patches()
+ for mode in (None, "eager", "default"):
+ ap.install_stage(
+ ap.STAGE_LOAD,
+ ap.PatchContext(
+ pipe = pipe, dtype = "bf16", device = "cuda", logger = "log", speed_mode = mode
+ ),
+ )
+ diffusion._uninstall_fused_dit_patches()
+ # An unset dense request resolves to "off" but is not an explicit off.
+ ap.install_stage(
+ ap.STAGE_LOAD,
+ ap.PatchContext(
+ pipe = pipe,
+ dtype = "bf16",
+ device = "cuda",
+ logger = "log",
+ speed_mode = "off",
+ explicit_off = False,
+ ),
+ )
+ diffusion._uninstall_fused_dit_patches()
finally:
diffusion_rocm_fused.install_for_pipe = orig_install
diffusion_rocm_fused.uninstall = orig_uninstall
- assert ("install", (pipe, "bf16", "cuda", "log")) in seen and ("uninstall",) in seen
+ assert ("install", (pipe, "bf16", "cuda", "log"), {"adaln": False}) in seen
+ assert seen.count(("install", (pipe, "bf16", "cuda", "log"), {"adaln": True})) == 4
+ stage_load = next(
+ c
+ for c in ast.walk(load)
+ if isinstance(c, ast.Call)
+ and any(isinstance(a, ast.Attribute) and a.attr == "STAGE_LOAD" for a in c.args)
+ )
+ explicit = next(k.value for k in stage_load.keywords if k.arg == "explicit_off")
+ assert "speed_mode is not None" in ast.unparse(explicit)
+ assert ("uninstall",) in seen
def test_stock_lines_still_present_in_diffusers():