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 Wan fused blocks: one Triton binary per kernel for every sequence length (#17101)

Daniel Han29b1bed935df
diff --git a/studio/backend/core/inference/video_wan_fused.py b/studio/backend/core/inference/video_wan_fused.py
index c15110a59c0..1cb3fac0e09 100644
--- a/studio/backend/core/inference/video_wan_fused.py
+++ b/studio/backend/core/inference/video_wan_fused.py
@@ -17,6 +17,7 @@ from .rocm_bf16 import is_rocm_torch
WAN_FUSED_ENV = "UNSLOTH_DIFFUSION_WAN_FUSED_ADALN"
# rotary only (the block fusion stays): UNSLOTH_DIFFUSION_WAN_FUSED_ROPE=0
WAN_FUSED_ROPE_ENV = "UNSLOTH_DIFFUSION_WAN_FUSED_ROPE"
+WAN_FUSED_ANY_LENGTH_ENV = "UNSLOTH_DIFFUSION_WAN_FUSED_ANY_LENGTH"
_MODULE = "diffusers.models.transformers.transformer_wan"
_CLASS = "WanTransformerBlock"
# ``_digest(WanTransformerBlock.forward)``: identical in diffusers 0.36.0, 0.37.0, 0.40.0 and main (80c7ed26).
@@ -62,6 +63,15 @@ def _kernels() -> Optional[dict]:
except Exception: # noqa: BLE001
return None
+ # L only splits the row id into (sample, token): no alignment depends on it.
+ any_length = (os.environ.get(WAN_FUSED_ANY_LENGTH_ENV) or "").strip().lower() not in (
+ "0",
+ "off",
+ "false",
+ "no",
+ )
+ unspec = ["L"] if any_length else []
+
@triton.jit
def _mul_rn(a, b):
# enable_fp_fusion=False is not enough (ptxas forms FFMA2 on sm_100), hence inline PTX.
@@ -69,7 +79,7 @@ def _kernels() -> Optional[dict]:
"mul.rn.f32 $0, $1, $2;", "=r,r,r", [a, b], dtype = tl.float32, is_pure = True, pack = 1
)
- @triton.jit
+ @triton.jit(do_not_specialize = unspec)
def _modnorm(
X,
MEAN,
@@ -108,7 +118,7 @@ def _kernels() -> Optional[dict]:
y = y + shift
tl.store(OUT + row * D + col, y.to(OUT.dtype.element_ty), mask = mask)
- @triton.jit
+ @triton.jit(do_not_specialize = unspec)
def _gate_residual(
X,
A,
@@ -157,7 +167,7 @@ def _kernels() -> Optional[dict]:
y = tl.fma(w, _mul_rn(rstd, x - mean), bb)
tl.store(OUT + row * D + col, y.to(OUT.dtype.element_ty), mask = mask)
- @triton.jit
+ @triton.jit(do_not_specialize = unspec)
def _rope(
X,
COS,
diff --git a/studio/backend/tests/test_video_wan_fused_any_length.py b/studio/backend/tests/test_video_wan_fused_any_length.py
new file mode 100644
index 00000000000..1f93a1067f7
--- /dev/null
+++ b/studio/backend/tests/test_video_wan_fused_any_length.py
@@ -0,0 +1,56 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The fused Wan kernels take the sequence length L unspecialized: L only splits the row id into (sample, token), so
+a new resolution or clip length whose L % 16 differs reuses the binary instead of compiling another. CPU only (the
+kernels are built, not launched)."""
+
+from __future__ import annotations
+
+import inspect
+
+import pytest
+
+pytest.importorskip("triton")
+
+from core.inference import video_wan_fused as wf # noqa: E402
+
+
+def _jit(name: str):
+ fn = wf._kernels()[name]
+ for v in inspect.getclosurevars(fn).nonlocals.values():
+ if type(v).__name__ == "JITFunction" and v.__name__ == "_" + name:
+ return v
+ raise AssertionError(name)
+
+
+def kernels(monkeypatch):
+ def build(env = None):
+ if env is None:
+ monkeypatch.delenv(wf.WAN_FUSED_ANY_LENGTH_ENV, raising = False)
+ else:
+ monkeypatch.setenv(wf.WAN_FUSED_ANY_LENGTH_ENV, env)
+ wf._kernels.cache_clear()
+ return {n: _jit(n) for n in ("modnorm", "gate_residual", "affine_norm", "rope")}
+
+ yield build
+ wf._kernels.cache_clear()
+
+
+def _unspecialized(fn) -> list:
+ return [p.name for p in fn.params if p.do_not_specialize]
+
+
+def test_length_is_not_specialized(kernels):
+ ks = kernels()
+ assert _unspecialized(ks["modnorm"]) == ["L"]
+ assert _unspecialized(ks["gate_residual"]) == ["L"]
+ assert _unspecialized(ks["rope"]) == ["L"]
+ # D bounds the vectorized hidden axis and the strides set load alignment: those stay specialized
+ assert _unspecialized(ks["affine_norm"]) == []
+
+
+def test_kill_switch_specializes_again(kernels):
+ ks = kernels("0")
+ assert all(_unspecialized(k) == [] for k in ks.values())