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 kernels: keep per-resolution sizes out of the Triton compile key (#16582)

Daniel Han525e4ab02230
diff --git a/studio/backend/core/inference/diffusion_convrot_quant.py b/studio/backend/core/inference/diffusion_convrot_quant.py
index ce33b138b29..8f59e662f29 100644
--- a/studio/backend/core/inference/diffusion_convrot_quant.py
+++ b/studio/backend/core/inference/diffusion_convrot_quant.py
@@ -151,7 +151,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
silu = _rbf16(gf * tl.sigmoid(gf))
return (silu * v.to(tl.float32)).to(tl.bfloat16)
- @triton.jit
+ @triton.jit(do_not_specialize = ["M"])
def rotq_i8(
x_ptr,
h_ptr,
@@ -302,7 +302,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
z = _h16_tile(x_ptr, v_ptr, gidx, live, h, h32, ROWS * CG, SWIGLU, TF32X3)
_h16_quant_store(z, inv_t, q_ptr, gidx, live, QMIN, QMAX, FP32_SCALE)
- @triton.jit
+ @triton.jit(do_not_specialize = ["M"])
def rotq_h16(
x_ptr,
v_ptr,
diff --git a/studio/backend/core/inference/diffusion_flux2_rope.py b/studio/backend/core/inference/diffusion_flux2_rope.py
index e6e09ea81a0..81d15e345c8 100644
--- a/studio/backend/core/inference/diffusion_flux2_rope.py
+++ b/studio/backend/core/inference/diffusion_flux2_rope.py
@@ -55,7 +55,7 @@ def _kernel() -> Optional[Callable]:
except Exception: # noqa: BLE001
return None
- @triton.jit
+ @triton.jit(do_not_specialize = ["S"])
def _rope_fwd(
X,
COS,
diff --git a/studio/backend/core/inference/diffusion_int8_gemm.py b/studio/backend/core/inference/diffusion_int8_gemm.py
index 5de7eaf3159..813b78278c9 100644
--- a/studio/backend/core/inference/diffusion_int8_gemm.py
+++ b/studio/backend/core/inference/diffusion_int8_gemm.py
@@ -183,7 +183,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
def _rbf16(x):
return x.to(tl.bfloat16).to(tl.float32)
- @triton.jit
+ @triton.jit(do_not_specialize = ["M"])
def i8mm_dq(
a_ptr,
w_ptr,
@@ -256,7 +256,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
mask = (offs_m[:, None] < M) & (offs_n[None, :] < N)
tl.store(c_ptrs, y.to(tl.bfloat16), mask = mask)
- @triton.jit
+ @triton.jit(do_not_specialize = ["M"])
def i8mm_dq_tma(
a_desc,
w_desc,
diff --git a/studio/backend/core/inference/diffusion_moss_fused.py b/studio/backend/core/inference/diffusion_moss_fused.py
index f1ca2795bec..4f450114e05 100644
--- a/studio/backend/core/inference/diffusion_moss_fused.py
+++ b/studio/backend/core/inference/diffusion_moss_fused.py
@@ -190,7 +190,7 @@ def _qk_rope_kernel() -> Any:
tl.store(o_ptr + out + j, o1.to(tl.bfloat16), mask = mask)
tl.store(o_ptr + out + D // 2 + j, o2.to(tl.bfloat16), mask = mask)
- @triton.jit
+ @triton.jit(do_not_specialize = ["S"])
def kernel(
q_ptr,
k_ptr,
diff --git a/studio/backend/core/inference/diffusion_qwenimage21_eager.py b/studio/backend/core/inference/diffusion_qwenimage21_eager.py
index 43094ef5a56..d8a2b6e50f6 100644
--- a/studio/backend/core/inference/diffusion_qwenimage21_eager.py
+++ b/studio/backend/core/inference/diffusion_qwenimage21_eager.py
@@ -87,7 +87,8 @@ def _kernel() -> Any:
global _rsqrt, _mul, _add
_rsqrt, _mul, _add = libdevice.rsqrt, libdevice.mul_rn, libdevice.add_rn
- @triton.jit
+ # n_rows keeps its % 16 hint: runtime it was 2.5% slower at even token counts on A100.
+ @triton.jit(do_not_specialize = ["n_seq"])
def _norm_rope_kernel(
x_ptr,
w_ptr,
diff --git a/studio/backend/core/inference/diffusion_rocm_fused.py b/studio/backend/core/inference/diffusion_rocm_fused.py
index efafd4bf875..a61767d5509 100644
--- a/studio/backend/core/inference/diffusion_rocm_fused.py
+++ b/studio/backend/core/inference/diffusion_rocm_fused.py
@@ -237,7 +237,7 @@ def _adaln_kernel() -> Optional[Callable]:
except Exception: # noqa: BLE001
return None
- @triton.jit
+ @triton.jit(do_not_specialize = ["S"])
def _adaln_fwd(
X,
SCALE,
diff --git a/studio/backend/core/inference/diffusion_vae_fused.py b/studio/backend/core/inference/diffusion_vae_fused.py
index 6d25a121270..27fded25219 100644
--- a/studio/backend/core/inference/diffusion_vae_fused.py
+++ b/studio/backend/core/inference/diffusion_vae_fused.py
@@ -55,6 +55,11 @@ _SHAPE_ARGS = {
"_bias_residual": ("P", "T", "HW", "W"),
"_dup_up_add": ("T_o", "H_o", "W_o", "t_off"),
"_up_nearest2x": ("T_o", "H", "W"),
+ "_gn_partials": ("T", "HW", "W", "n_chunks"),
+ "_gn_combine": ("n_chunks",),
+ "_gn_apply": ("T", "HW", "W"),
+ "_softmax_rows": ("S",),
+ "_dcae_up_add": ("F", "H", "W", "Fo"),
}
@@ -67,7 +72,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
return None
globals().update(triton = triton, tl = tl)
- @triton.jit
+ @triton.jit(do_not_specialize = _SHAPE_ARGS["_gn_partials"])
def _gn_partials(
x_ptr, bias_ptr, pn_ptr, pmean_ptr, pm2_ptr,
T, HW, W, n_chunks, sb, sc, st, sh, sw,
@@ -104,7 +109,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
tl.store(pm2_ptr + slot * G + g, m2)
tl.store(pn_ptr + slot, n_pix * CPG)
- @triton.jit
+ @triton.jit(do_not_specialize = _SHAPE_ARGS["_gn_combine"])
def _gn_combine(
pn_ptr,
pmean_ptr,
@@ -142,7 +147,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
tl.store(mean_ptr + row, mean)
tl.store(rstd_ptr + row, 1.0 / tl.sqrt(acc_m2 / acc_n + eps))
- @triton.jit
+ @triton.jit(do_not_specialize = _SHAPE_ARGS["_gn_apply"])
def _gn_apply(
x_ptr, out_ptr, mean_ptr, rstd_ptr, w_ptr, b_ptr, ib_ptr,
C, T, HW, W, G, sb, sc, st, sh, sw,
@@ -288,7 +293,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
o = o * inv_scale
tl.store(o_ptr + o_off, o.to(o_ptr.dtype.element_ty), mask = m)
- @triton.jit
+ @triton.jit(do_not_specialize = _SHAPE_ARGS["_dcae_up_add"])
def _dcae_up_add(
h_ptr, hb_ptr, x_ptr, out_ptr,
c, Cin, F, H, W, Fo, rep,
@@ -345,7 +350,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
o_off = (bf.to(tl.int64) * (4 * H * W) + p.to(tl.int64))[:, None] * c + ci[None, :]
tl.store(out_ptr + o_off, (v + xv).to(out_ptr.dtype.element_ty), mask = m)
- @triton.jit
+ @triton.jit(do_not_specialize = _SHAPE_ARGS["_softmax_rows"])
def _softmax_rows(s_ptr, p_ptr, S, P_STRIDE, scale, BLOCK: tl.constexpr): # block size
row = tl.program_id(0).to(tl.int64)
base = s_ptr + row * P_STRIDE
diff --git a/studio/backend/core/inference/video_minimax_h3_qknorm.py b/studio/backend/core/inference/video_minimax_h3_qknorm.py
index 0b39f02c640..472ffcc721e 100644
--- a/studio/backend/core/inference/video_minimax_h3_qknorm.py
+++ b/studio/backend/core/inference/video_minimax_h3_qknorm.py
@@ -51,7 +51,7 @@ def _kernel() -> Any:
_mul = libdevice.mul_rn
_add = libdevice.add_rn
- @triton.jit
+ @triton.jit(do_not_specialize = ["n_rows"])
def _qk_norm_rope_kernel(
x_ptr,
w_ptr,
diff --git a/studio/backend/core/inference/video_minimax_h3_vae.py b/studio/backend/core/inference/video_minimax_h3_vae.py
index d44a778ab7b..a8d37c4957c 100644
--- a/studio/backend/core/inference/video_minimax_h3_vae.py
+++ b/studio/backend/core/inference/video_minimax_h3_vae.py
@@ -86,7 +86,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
# Triton <= 3.2 resolves string annotations against module globals, not this function's locals
globals().update(triton = triton, tl = tl, libdevice = libdevice)
- @triton.jit
+ @triton.jit(do_not_specialize = ["T", "HW", "W", "n_chunks"])
def _gn_partials(
x_ptr,
bias_ptr,
@@ -139,7 +139,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
tl.store(pm2_ptr + slot * G + g, m2)
tl.store(pn_ptr + slot, n_pix * CPG)
- @triton.jit
+ @triton.jit(do_not_specialize = ["n_chunks"])
def _gn_combine(
pn_ptr,
pmean_ptr,
@@ -177,7 +177,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
tl.store(mean_ptr + row, mean)
tl.store(rstd_ptr + row, 1.0 / tl.sqrt(acc_m2 / acc_n + eps))
- @triton.jit
+ @triton.jit(do_not_specialize = ["T", "H", "W", "To", "Ho", "Wo"])
def _gn_silu_pad(
x_ptr,
out_ptr,
@@ -248,7 +248,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
v = tl.where(t_ok, v, 0.0)
tl.store(out_base + (wo[:, None] * C + c[None, :]), v.to(out_ptr.dtype.element_ty), mask = m)
- @triton.jit
+ @triton.jit(do_not_specialize = ["P", "T", "H", "W"])
def _add_residual(
o_ptr,
ob_ptr,
@@ -327,7 +327,7 @@ def _kernels() -> Optional[types.SimpleNamespace]:
y = h * (1.0 / tl.sqrt(ms + eps)) * w
tl.store(n_ptr + row * N + cols, y.to(n_ptr.dtype.element_ty), mask = mask)
- @triton.jit
+ @triton.jit(do_not_specialize = ["n_head_rows"])
def _qk_norm_rope(
x_ptr,
cos_ptr,
diff --git a/studio/backend/tests/test_diffusion_triton_resolution_args.py b/studio/backend/tests/test_diffusion_triton_resolution_args.py
new file mode 100644
index 00000000000..25418e5e860
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_triton_resolution_args.py
@@ -0,0 +1,79 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Token counts and spatial sizes that change with the resolution / frame count stay out of the Triton compile key
+(do_not_specialize), so a render at a resolution whose sizes are not multiples of 16 does not recompile every kernel."""
+
+import ast
+import pathlib
+
+import pytest
+
+INF = pathlib.Path(__file__).resolve().parents[1] / "core" / "inference"
+
+CASES = {
+ "diffusion_int8_gemm.py": {"i8mm_dq": ("M",), "i8mm_dq_tma": ("M",)},
+ "diffusion_convrot_quant.py": {"rotq_i8": ("M",), "rotq_h16": ("M",)},
+ "diffusion_flux2_rope.py": {"_rope_fwd": ("S",)},
+ "diffusion_rocm_fused.py": {"_adaln_fwd": ("S",)},
+ "diffusion_moss_fused.py": {"kernel": ("S",)},
+ "diffusion_qwenimage21_eager.py": {"_norm_rope_kernel": ("n_seq",)},
+ "video_minimax_h3_vae.py": {
+ "_gn_partials": ("T", "HW", "W", "n_chunks"),
+ "_gn_combine": ("n_chunks",),
+ "_gn_silu_pad": ("T", "H", "W", "To", "Ho", "Wo"),
+ "_add_residual": ("P", "T", "H", "W"),
+ "_qk_norm_rope": ("n_head_rows",),
+ },
+ "video_minimax_h3_qknorm.py": {"_qk_norm_rope_kernel": ("n_rows",)},
+}
+VAE_SHAPE_ARGS = {
+ "_gn_partials": ("T", "HW", "W", "n_chunks"),
+ "_gn_combine": ("n_chunks",),
+ "_gn_apply": ("T", "HW", "W"),
+ "_softmax_rows": ("S",),
+ "_dcae_up_add": ("F", "H", "W", "Fo"),
+}
+
+
+def _defs(path):
+ return {
+ n.name: n
+ for n in ast.walk(ast.parse(path.read_text(encoding = "utf-8")))
+ if isinstance(n, ast.FunctionDef)
+ }
+
+
+def _dns(fn):
+ for dec in fn.decorator_list:
+ if isinstance(dec, ast.Call) and ast.unparse(dec.func).endswith("jit"):
+ for kw in dec.keywords:
+ if kw.arg == "do_not_specialize":
+ return kw.value
+ return None
+
+
[email protected]("rel", sorted(CASES))
+def test_resolution_sizes_not_specialized(rel):
+ defs = _defs(INF / rel)
+ for name, args in CASES[rel].items():
+ node = _dns(defs[name])
+ assert node is not None, name
+ listed = {e.value for e in node.elts}
+ params = {a.arg for a in defs[name].args.args}
+ for a in args:
+ assert a in params and a in listed, (rel, name, a)
+
+
+def test_vae_shape_args_cover_group_norm_and_attention():
+ src = (INF / "diffusion_vae_fused.py").read_text(encoding = "utf-8")
+ tree = ast.parse(src)
+ table = next(
+ ast.literal_eval(n.value)
+ for n in tree.body
+ if isinstance(n, ast.Assign) and getattr(n.targets[0], "id", "") == "_SHAPE_ARGS"
+ )
+ defs = _defs(INF / "diffusion_vae_fused.py")
+ for name, args in VAE_SHAPE_ARGS.items():
+ assert tuple(table[name]) == args, name
+ assert ast.unparse(_dns(defs[name])) == f"_SHAPE_ARGS['{name}']", name