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.

MiniMax-H3: build the ConvRot Linear class once, not once per projection (#8357)

Daniel Han186e6cb5c1b0
diff --git a/studio/backend/core/inference/diffusion_convrot.py b/studio/backend/core/inference/diffusion_convrot.py
index e09466ddce1..a139cd1dcbb 100644
--- a/studio/backend/core/inference/diffusion_convrot.py
+++ b/studio/backend/core/inference/diffusion_convrot.py
@@ -52,6 +52,7 @@ reading the metadata contract costs no import.
from __future__ import annotations
+from functools import lru_cache
from typing import Any, Iterable, Optional
# ── the metadata contract, carried in the prequant checkpoint's own ``metadata`` dict ──────────
@@ -163,9 +164,18 @@ def rotate_convrot_weight_(module: Any, group_size: int) -> None:
module.weight.data = rotated.to(weight.dtype)
+@lru_cache(maxsize = None)
def convrot_linear_class() -> Any:
"""The ``nn.Linear`` subclass that rotates its input, built lazily so importing this module
- never imports torch.
+ never imports torch, and built exactly ONCE.
+
+ The cache is not a micro-optimisation, it is the difference between one compiled graph and
+ dozens. A class defined inside a function is a NEW class object on every call, and
+ ``torch.compile`` guards each frame on ``___check_type_id`` of the modules it closes over. So
+ handing every rotated projection its own ConvRotLinear made each one look like a different
+ type and retraced the block it lives in: measured 23 recompiles against bfloat16's 1 on the
+ same job, 178 s of first-call compile against 14 s, on a denoiser with 350 rotated
+ projections. Sharing one class collapses that back to a single trace.
A CLASS SWAP rather than a wrapper module, and that is load-bearing twice over: the module
stays an ``nn.Linear``, so torchao's filter and ``quantize_`` treat it exactly as they treat
diff --git a/studio/backend/core/inference/video_minimax_h3_te.py b/studio/backend/core/inference/video_minimax_h3_te.py
index 49df27535d4..505b834fdf3 100644
--- a/studio/backend/core/inference/video_minimax_h3_te.py
+++ b/studio/backend/core/inference/video_minimax_h3_te.py
@@ -47,6 +47,7 @@ nothing configured.
from __future__ import annotations
+from functools import lru_cache
from typing import Any, Optional
# The ConvRot primitives, shared with the denoiser's hosted INT8 checkpoint. Re-exported below
@@ -155,8 +156,15 @@ def h3_te_resident_gb(scheme: Optional[str], *, bf16_gb: float) -> float:
# exactly how they would stop agreeing. Both stay importable from here.
+@lru_cache(maxsize = None)
def _int8_convrot_linear_class() -> Any:
- """The ConvRot INT8 ``nn.Linear`` stand-in, built lazily so importing this module never imports torch."""
+ """The ConvRot INT8 ``nn.Linear`` stand-in, built lazily so importing this module never imports
+ torch, and built exactly ONCE.
+
+ One load already shares a single class across every projection, so the cache is about the
+ SECOND load in a process: a fresh class there is a fresh ``___check_type_id`` guard, which
+ retraces every compiled block that survived the first one. Same reason the denoiser's
+ ``convrot_linear_class`` is cached."""
import torch
from torch import nn
diff --git a/studio/backend/tests/test_diffusion_convrot.py b/studio/backend/tests/test_diffusion_convrot.py
index 71c7935e944..a8a3b1b7cae 100644
--- a/studio/backend/tests/test_diffusion_convrot.py
+++ b/studio/backend/tests/test_diffusion_convrot.py
@@ -398,3 +398,32 @@ def test_loader_refuses_a_rotation_it_cannot_apply(monkeypatch, tmp_path, fmt, m
# None means "fall back to the dense download": slower and bigger, but never wrong. The
# alternative -- loading it anyway -- has no symptom at all.
assert _load_rotated(monkeypatch, tmp_path, _ckpt(fmt, metadata)) is None
+
+
+def test_every_rotated_projection_shares_one_class():
+ """The rotation is a class SWAP, so the class has to be a singleton.
+
+ ``torch.compile`` guards each frame on ``___check_type_id`` of the modules it closes over. A
+ class defined inside a function is a new class object per call, so giving each of the 350
+ rotated projections its own ConvRotLinear made every one of them look like a different type
+ and retraced the block it lives in: 23 recompiles against bfloat16's 1, and 178 s of
+ first-call compile against 14 s. Identity, not equality: two classes with identical bodies
+ still fail the guard."""
+ import torch
+ from torch import nn
+
+ from core.inference.diffusion_convrot import _install_rotation, convrot_linear_class
+
+ first, second = nn.Linear(256, 8, bias = False), nn.Linear(256, 8, bias = False)
+ _install_rotation(first, 256)
+ _install_rotation(second, 256)
+ assert type(first) is type(second)
+ assert type(first) is convrot_linear_class()
+ # Still an nn.Linear, which is what keeps torchao's filter and the checkpoint keys working.
+ assert isinstance(first, nn.Linear)
+ assert is_rotated_linear(first) and is_rotated_linear(second)
+ # And the swap is per instance, so a shared class must not leak one module's group to another.
+ third = nn.Linear(512, 8, bias = False)
+ _install_rotation(third, 128)
+ assert (first.convrot_groupsize, third.convrot_groupsize) == (256, 128)
+ assert torch.is_tensor(first.weight)