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.

Video: make MiniMax-H3's Diffusers path fast by default (#8320)

Daniel Hanf776ce30a5bf
diff --git a/scripts/video_quality.py b/scripts/video_quality.py
index b39fbb66ed1..118cfaccd41 100644
--- a/scripts/video_quality.py
+++ b/scripts/video_quality.py
@@ -295,6 +295,9 @@ def run_config(
"attention_backend": spec.get("attention_backend"),
"transformer_cache": spec.get("transformer_cache"),
"transformer_quant": spec.get("transformer_quant"),
+ # On MiniMax-H3 the conditioner precision is a backend default, so "the released bfloat16
+ # encoder" is a spec value rather than the absence of one.
+ "text_encoder_quant": spec.get("text_encoder_quant"),
}
t0 = time.monotonic()
status = backend.load_pipeline(args.model, **load_kwargs)
@@ -331,6 +334,8 @@ def run_config(
"attention_backend",
"transformer_cache",
"transformer_quant",
+ "text_encoder_quant",
+ "speed_optims",
"offload_policy",
"model_kind",
)
diff --git a/studio/backend/core/inference/diffusion_precision.py b/studio/backend/core/inference/diffusion_precision.py
index 52bb1cc6b1c..de8b4808877 100644
--- a/studio/backend/core/inference/diffusion_precision.py
+++ b/studio/backend/core/inference/diffusion_precision.py
@@ -60,13 +60,19 @@ _TE_INT8_SKIP: dict[str, tuple[int, int]] = {
def normalize_te_quant(value: Optional[str]) -> Optional[str]:
- """Lower/strip a requested text-encoder quant; None / "" / "none" -> None.
+ """Lower/strip a requested text-encoder quant; None / "" / "none" / "off" / "auto" -> None.
+
+ The three no-scheme spellings collapse here because no family quantises its encoder without
+ a named scheme. They stay distinct to the caller that cares: MiniMax-H3 reads the RAW request
+ as a tri-state (unset picks the hosted conditioner, "none"/"off" pin the released bf16 one)
+ BEFORE normalising, so folding them is what lets an opt-out reach that branch at all instead
+ of being rejected here.
Raises ValueError for an unsupported value so a bad request is rejected cheaply."""
if value is None:
return None
normalized = str(value).strip().lower().replace("-", "_")
- if not normalized or normalized == "none":
+ if not normalized or normalized in ("none", "off", "auto"):
return None
if normalized not in TE_QUANT_MODES:
raise ValueError(
diff --git a/studio/backend/core/inference/diffusion_prequant.py b/studio/backend/core/inference/diffusion_prequant.py
index 891cc0f4cbc..149cb9f2cf9 100644
--- a/studio/backend/core/inference/diffusion_prequant.py
+++ b/studio/backend/core/inference/diffusion_prequant.py
@@ -826,8 +826,9 @@ def pin_prequantized_module(
device: Any,
*,
logger: Any = None,
+ label: str = "pre-quantized denoiser",
) -> bool:
- """Keep a pre-quantized module resident on ``device``, out of a ComponentsManager's rotation.
+ """Keep a module resident on ``device``, out of a ComponentsManager's rotation.
``ComponentsManager.enable_auto_cpu_offload`` parks every component on the CPU and moves each
one onto the accelerator inside its own ``pre_forward``, i.e. from within the block that is
@@ -843,6 +844,11 @@ def pin_prequantized_module(
their hooks, and the strategy sizes its decisions from live free memory, so the encoder and
the VAEs still offload around it.
+ For a torchao module that placement is REQUIRED, for the reason above. A caller may also pin a
+ plain dense module, where it is an optimisation instead: a module that moves per forward cannot
+ be regionally compiled either, since the onload hooks wrap the forward the graph would replace.
+ That caller owns the fit check (this function sizes nothing) and passes its own ``label``.
+
Returns True when the module was pinned. Best-effort on the hook surgery: if the manager does
not look the way this expects, the module is still placed on ``device`` and False is returned,
which is the behaviour before pinning existed.
@@ -867,7 +873,8 @@ def pin_prequantized_module(
module.to(device)
if logger is not None:
logger.info(
- "diffusion.prequant: pre-quantized denoiser pinned on %s (offload rotation: %s)",
+ "diffusion.prequant: %s pinned on %s (offload rotation: %s)",
+ label,
device,
"removed" if pinned else "unchanged",
)
diff --git a/studio/backend/core/inference/diffusion_speed.py b/studio/backend/core/inference/diffusion_speed.py
index 9f0bfad3426..11f1d2d172e 100644
--- a/studio/backend/core/inference/diffusion_speed.py
+++ b/studio/backend/core/inference/diffusion_speed.py
@@ -33,6 +33,9 @@ load bit-identical. torch imported lazily.
from __future__ import annotations
+import os
+import sys
+from functools import lru_cache
from typing import Any, Optional
from . import diffusion_gguf_compile as gguf_compile
@@ -138,12 +141,38 @@ def resolve_speed_mode(
return normalize_speed_mode(value)
+@lru_cache(maxsize = 1)
+def torch_compile_runtime_available() -> bool:
+ """Whether THIS process can actually run an inductor compile.
+
+ Inductor needs Triton, and Windows is the one supported platform whose normal install has no
+ Triton wheel. The three Studio workers (inference / training / export) already gate on this
+ import and set ``TORCHDYNAMO_DISABLE=1`` when it fails, but the diffusion and video backends
+ run in the SERVER process, which those gates never reach, so ask it once here.
+
+ ``TORCHDYNAMO_DISABLE`` is honored on every platform: a compile under it is a silent no-op that
+ would otherwise be recorded as an engaged optimisation. Cached, since neither answer can change
+ inside a process and this runs on every load."""
+ if os.environ.get("TORCHDYNAMO_DISABLE", "").strip() not in ("", "0"):
+ return False
+ if sys.platform != "win32":
+ return True
+ try:
+ import triton # noqa: F401, PLC0415
+ except Exception: # noqa: BLE001 -- absent or broken Triton means eager, never a failed load
+ return False
+ return True
+
+
def compile_eligible(target: Any, *, is_gguf: bool, family: Any) -> bool:
"""Whether the denoiser's repeated block should be regionally compiled.
- Only on CUDA (incl. ROCm), for a bf16 transformer, on a compile-friendly family. ``is_gguf``
- no longer disqualifies (GGUF compiles fine and ~2.3x faster); the param is kept for compat."""
+ Only on CUDA (incl. ROCm), for a bf16 transformer, on a compile-friendly family, in a process
+ that can run inductor. ``is_gguf`` no longer disqualifies (GGUF compiles fine and ~2.3x
+ faster); the param is kept for compat."""
del is_gguf # GGUF is compile-eligible now; param kept for call-site compat.
+ if not torch_compile_runtime_available():
+ return False
if not bool(getattr(target, "supports_default_torch_compile", False)):
return False
if not bool(getattr(family, "supports_torch_compile", True)):
diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.py
index 9b9a2df5d03..59742d3c61d 100644
--- a/studio/backend/core/inference/video.py
+++ b/studio/backend/core/inference/video.py
@@ -76,6 +76,7 @@ from .diffusion_memory import (
)
from .diffusion_speed import (
SPEED_DEFAULT,
+ SPEED_EAGER,
SPEED_MAX,
SPEED_OFF,
apply_speed_optims,
@@ -140,6 +141,7 @@ from .video_minimax_h3 import (
h3_transformer_task,
)
from .video_minimax_h3_te import (
+ H3_TE_QUANT_DEFAULT,
H3_TE_QUANT_REPO,
h3_te_quant_scheme,
h3_te_resident_gb,
@@ -509,6 +511,117 @@ def _h3_te_canonical(repo_id: Optional[str]) -> str:
return canonical_base(repo_id).strip().lower()
+# ── MiniMax-H3 modular precision tri-state ────────────────────────────────────
+# The modular workflow builds each component through its own from_pretrained, so nothing dense can
+# be quantised in place: a hosted checkpoint is the only way to run a component small, which makes
+# "unset" a real choice rather than a no-op.
+#
+# unset / "auto" -> whatever the backend decided for that component
+# "none" / "off" -> the released bfloat16 component, pinned
+# a scheme -> that scheme, or a recorded fallback
+#
+# The two components decided it differently on measured quality (see each site): the CONDITIONER
+# defaults to the hosted quantized artifact, which preserves the sample; the DENOISER keeps the
+# released weights, because both hosted checkpoints re-roll it. These helpers are the one place
+# the tri-state is read, so the plan, the estimate, the pull and the load cannot disagree.
+
+
+def _h3_precision_unset(value: Optional[str]) -> bool:
+ """True when the caller left this precision to the backend."""
+ return value is None or str(value).strip().lower() in ("", "auto")
+
+
+def _h3_precision_pinned_dense(value: Optional[str]) -> bool:
+ """True when the caller explicitly asked for the released bfloat16 component."""
+ return value is not None and str(value).strip().lower() in ("none", "off")
+
+
+def _h3_auto_precision_target(target: Any = None) -> Any:
+ """The device target the auto choice is made against. Never raises."""
+ if target is not None:
+ return target
+ try:
+ return resolve_diffusion_device_target()
+ except Exception: # noqa: BLE001 -- an unanswerable probe keeps the dense components
+ return None
+
+
+def _h3_auto_precision_ok(target: Any = None) -> bool:
+ """Whether an UNSET MiniMax-H3 precision may resolve to the hosted quantized components.
+
+ CUDA (which covers ROCm) with a bfloat16 compute dtype, and nothing else. Both artifacts are
+ portable in principle, so the limit is what has been MEASURED: MPS cannot reach this path at
+ all (``ComponentsManager.enable_auto_cpu_offload`` needs ``mem_get_info``, which ``torch.mps``
+ lacks, so the modular loader raises first), and CPU-only hosts run H3 through sd.cpp instead.
+
+ An EXPLICIT request is unaffected by this gate and still works wherever it worked before."""
+ resolved = _h3_auto_precision_target(target)
+ if resolved is None:
+ return False
+ try:
+ import torch
+ return (
+ getattr(resolved, "device", None) == "cuda"
+ and getattr(resolved, "dtype", None) is torch.bfloat16
+ )
+ except Exception: # noqa: BLE001 -- no torch means no diffusers path to optimise
+ return False
+
+
+def _h3_dense_denoiser_resident_bytes(
+ fam: Any, *, denoiser: Any, te_scheme: Optional[str], dtype: Any
+) -> Optional[tuple[int, int]]:
+ """``(denoiser_bytes, everything_else_bytes)`` for a MiniMax-H3 modular pipeline, or None.
+
+ ``everything_else`` is the conditioner at the precision this load ENGAGED, plus the VAEs, plus
+ a frames-aware activation headroom: what must still fit alongside a denoiser taken out of the
+ rotation. The denoiser is measured off the built module, not the family table, because the
+ table holds the released dense size and the module in hand may not be it."""
+ components = getattr(fam, "bf16_components_gb", None)
+ if not components or denoiser is None:
+ return None
+ try:
+ from itertools import chain
+
+ import torch
+
+ denoiser_bytes = sum(
+ t.numel() * t.element_size()
+ for t in chain(denoiser.parameters(), denoiser.buffers())
+ if not getattr(t, "is_meta", False)
+ )
+ if denoiser_bytes <= 0:
+ return None
+ scale = 2.0 if dtype is torch.float32 else 1.0
+ text_encoder_gb = h3_te_resident_gb(te_scheme, bf16_gb = components[1])
+ headroom_bytes = (
+ estimate_video_runtime_mib(
+ width = fam.resolution_presets[0][0],
+ height = fam.resolution_presets[0][1],
+ num_frames = fam.default_num_frames,
+ )
+ * 1024
+ * 1024
+ )
+ others = int((text_encoder_gb + components[2]) * scale * 1000.0**3) + headroom_bytes
+ return int(denoiser_bytes), others
+ except Exception: # noqa: BLE001 -- an unanswerable estimate keeps the rotation as it is
+ return None
+
+
+def _h3_dense_denoiser_fits(sizes: Optional[tuple[int, int]], free_bytes: Optional[int]) -> bool:
+ """Whether the released denoiser can come OUT of the offload rotation and stay resident.
+
+ Its own bytes plus everything that still has to run beside it, against the live free reading.
+ Split out from the sizing helper so the comparison can be tested without standing up a modular
+ load: it is what authorises a pin, and a wrong pin is an OOM rather than a slow generation. No
+ reading means no pin, which is today's behaviour."""
+ if sizes is None or free_bytes is None:
+ return False
+ denoiser_bytes, others_bytes = sizes
+ return int(free_bytes) >= int(denoiser_bytes) + int(others_bytes)
+
+
def _progress(phase: Optional[str], **extra: Any) -> dict[str, Any]:
return {"phase": phase, **extra}
@@ -1029,8 +1142,8 @@ class VideoBackend:
kwargs["gguf_filename"],
kwargs.get("hf_token"),
cancel_event = cancel_event,
- # The plan counts a file cached under EITHER root and stages neither, so the
- # load has to resolve through both or it re-pulls what the planner skipped.
+ # The plan counts a file cached under EITHER root, so the load has to
+ # resolve through both or it re-pulls what the planner skipped.
reuse_other_cache_root = True,
)
)
@@ -1359,6 +1472,7 @@ class VideoBackend:
fam: Any,
text_encoder_quant: Optional[str],
base: Optional[str] = None,
+ target: Any = None,
) -> Optional[str]:
"""The hosted QUANTIZED conditioner scheme this pick would load, or None.
@@ -1377,7 +1491,11 @@ class VideoBackend:
encoder path uses: the same base, or one verified to ship byte-identical component weights.
Anything else keeps the pipeline's own dense encoder.
- Pure, never raises, never touches the network."""
+ An UNSET request resolves to the hosted artifact rather than to the dense encoder: see
+ ``H3_TE_QUANT_DEFAULT``. ``"none"`` still pins the released bfloat16 encoder.
+
+ Never raises, never touches the network (the device probe behind the auto default reads
+ torch, not the Hub)."""
try:
if not getattr(fam, "modular_workflow", None):
return None
@@ -1386,6 +1504,15 @@ class VideoBackend:
canonical = getattr(fam, "base_repo", None)
if not canonical or not base or not te_base_equivalent(canonical, base):
return None
+ if _h3_precision_pinned_dense(text_encoder_quant):
+ return None
+ if _h3_precision_unset(text_encoder_quant):
+ return (
+ H3_TE_QUANT_DEFAULT
+ if h3_te_quant_scheme(H3_TE_QUANT_DEFAULT) is not None
+ and _h3_auto_precision_ok(target)
+ else None
+ )
return h3_te_quant_scheme(normalize_te_quant(text_encoder_quant))
except Exception: # noqa: BLE001 -- an unanswerable probe keeps the dense encoder
return None
@@ -1565,8 +1692,10 @@ class VideoBackend:
if not getattr(fam, "modular_workflow", None):
return False
scheme = normalize_transformer_quant(transformer_quant)
- # "auto" leaves the choice to the backend, which keeps the released bfloat16 components
- # for a modular workflow, so it must NOT drop the dense shards that load then wants.
+ # "auto" leaves the choice to the backend, which keeps the released bfloat16 DENOISER
+ # for a modular workflow (the conditioner default is a separate decision, made in
+ # _h3_te_quant_scheme), so it must NOT drop the dense shards that load then wants. The
+ # hosted checkpoints were measured and left opt-in: see _load_h3_modular_pipeline.
if scheme is None or scheme == TQ_AUTO:
return False
return video_family_prequant_repo(fam, scheme, base) is not None
@@ -1685,10 +1814,9 @@ class VideoBackend:
encoders keep theirs: the pre-quant loader meta-inits the DiT from it,
so dropping it would break the very load that made the skip safe.
- ``h3_task`` picks the H3 denoiser partition. The base repo ships two, and a load only ever
- brings up one: ``transformer/`` for fl2va (which also covers text-only) and
- ``transformer_ref/`` for ref2va. They are 66.28 GB each, so the scoped list carries exactly
- one of them, never both."""
+ ``h3_task`` picks the H3 denoiser partition. The base repo ships two 66.28 GB partitions and
+ a load brings up one: ``transformer/`` for fl2va (which also covers text-only),
+ ``transformer_ref/`` for ref2va. The scoped list carries exactly one, never both."""
from .diffusion_te_prequant import is_prequant_covered_weight
from .video_minimax_h3 import H3_TASK_REFERENCES
@@ -1818,7 +1946,7 @@ class VideoBackend:
``h3_task`` picks which MiniMax-H3 denoiser partition is staged. It was swallowed by
``**load_kwargs`` too, so a ref2va plan staged the fl2va ``transformer/`` and left the
- 66.28 GB ``transformer_ref/`` the load then needs to be pulled inline."""
+ 66.28 GB ``transformer_ref/`` the load needs to be pulled inline."""
from huggingface_hub import HfApi
fam = _detect_load_family(repo_id, gguf_filename, family_override)
@@ -1848,13 +1976,14 @@ class VideoBackend:
checkpoint: bool = False,
) -> int:
"""Count every required file and record, per root, where the loader would find it.
- Which of them to stage is decided per REPO once every add is in (see below), not here:
- the 2.3 path adds one repo across several calls, and a call that happens to hold only
- other-root files cannot see that the repo straddles both. Returns the full size.
+
+ Returns the full size. WHICH files to stage is decided per REPO once every add is in
+ (see below), not here: the 2.3 path adds one repo across several calls, and a call
+ holding only other-root files cannot see that the repo straddles both.
``checkpoint`` marks the entry holding the SELECTED model so the panel can label it
- without re-deriving the answer from the repo id, which the plan itself may have
- rewritten. Same envelope key the image planners emit, so one page-side map serves both."""
+ without re-deriving it from a repo id the plan may have rewritten. Same envelope key
+ the image planners emit, so one page-side map serves both."""
from core.inference.diffusion import DiffusionBackend
def _where(name: str, size: int) -> Optional[str]:
@@ -1865,9 +1994,9 @@ class VideoBackend:
repo, name, revision, size, roots = (None,)
):
return None
- # A stale live copy under the right name shadows the good one, because
- # reuse_other_cache_root switches roots only when the live lookup finds NOTHING.
- # Presence alone is what that switch tests, so ask without the size.
+ # A stale live copy under the right name shadows the good one: reuse_other_cache_root
+ # switches roots only when the live lookup finds NOTHING, and presence alone is what
+ # it tests, so ask without the size.
if DiffusionBackend._hub_file_is_cached(repo, name, revision, None, roots = (live,)):
return None
return "other"
@@ -1878,8 +2007,7 @@ class VideoBackend:
if gguf:
label["gguf"] = gguf
# Only ever raised, never cleared: a repo holding both the checkpoint and companion
- # files (a 2.3 pick whose extras live in the checkpoint repo) is keyed once, so a later
- # companion add would otherwise unflag what the checkpoint add marked.
+ # files is keyed once, so a later companion add would unflag what the checkpoint set.
label["checkpoint"] = label["checkpoint"] or checkpoint
required = 0
for name, size in files:
@@ -1974,13 +2102,12 @@ class VideoBackend:
return {"entries": [], "total_bytes": 0, "required_bytes": 0, "checkpoint_bytes": 0}
for repo, where in located.items():
# Files STRADDLING the two roots cannot be handed to from_pretrained as one snapshot:
- # _predownload_base returns no directory in that state and the assembly is pinned to
- # hub_cache_dir(), so the other-root subset is invisible to it and gets re-pulled
- # inline, past this plan's progress, cancel and disk preflight, failing offline after a
- # plan that reported nothing left to do. A repo living wholly in the other root is fine,
- # which is what reuse_other_cache_root is for. A file missing everywhere downloads INTO
- # the live root, so it counts as live: dropping it would read a mixed repo as unsplit
- # and stage only the missing part, manufacturing the very split this avoids.
+ # _predownload_base returns no directory then and the assembly is pinned to
+ # hub_cache_dir(), so the other-root subset is re-pulled inline, past this plan's
+ # progress, cancel and disk preflight. A repo living WHOLLY in the other root is fine
+ # (that is what reuse_other_cache_root is for). A file missing everywhere downloads
+ # into the live root, so it counts as live: dropping it would read a mixed repo as
+ # unsplit and stage only the missing part, manufacturing the split this avoids.
split = {w or "live" for w, _size in where.values()} == {"live", "other"}
staged = [
(name, size)
@@ -2000,10 +2127,9 @@ class VideoBackend:
"files": names,
"bytes": sum(size for _name, size in staged),
"gguf_filename": gguf,
- # The flag describes the ENTRY, not the pick: a 2.3 repo holding both the cached
- # checkpoint and its missing extras stages companion files only, and labelling that
- # "Model file" misdescribes what is downloading. A pipeline pick has no single file,
- # so the repo itself is the model and anything staged from it is part of it.
+ # Describes the ENTRY, not the pick: a 2.3 repo whose checkpoint is already cached
+ # stages companion files only, and calling that "Model file" misdescribes what is
+ # downloading. A pipeline pick has no single file, so the repo itself is the model.
"checkpoint": labels[repo]["checkpoint"]
and (gguf is None or gguf in missing_names),
}
@@ -2245,10 +2371,10 @@ class VideoBackend:
roots.add(root)
if name == "model_index.json":
snapshot_root = root or None
- # Only hand back a snapshot the WHOLE set lives in. reuse_other_cache_root resolves each
- # file through whichever root holds it, so a moved cache can serve the manifest from the
- # old root while the rest land in the live one, and from_pretrained would then be pointed
- # at a tree missing the others. Mirrors the image prefetch path.
+ # Only hand back a snapshot the WHOLE set lives in. reuse_other_cache_root resolves per
+ # file, so a moved cache can serve the manifest from the old root while the rest land
+ # in the live one, pointing from_pretrained at a tree missing the others. Mirrors the
+ # image prefetch path.
if snapshot_root is not None and roots != {snapshot_root}:
return None
return snapshot_root
@@ -2450,14 +2576,18 @@ class VideoBackend:
device = device,
hf_token = hf_token,
memory_mode = memory_mode,
- # transformer_quant is only normalised BELOW this dispatch, and the modular branch
- # returns before reaching that, so normalise here with the same function rather
- # than moving the dispatch past a block written for the conventional path (its
- # speed_mode/auto-ladder defaulting means nothing to a modular workflow).
- transformer_quant = normalize_transformer_quant(transformer_quant),
- # Same reason as transformer_quant above: the conventional path normalises the
- # encoder request below this dispatch, which the modular branch never reaches.
- text_encoder_quant = normalize_te_quant(text_encoder_quant),
+ # RAW, not normalised. Both normalisers fold "none"/"off" into the same None an
+ # omitted request produces, and for a modular workflow those are opposite answers:
+ # unset means "pick the hosted quantized components", "none" means "keep the
+ # released bfloat16 ones". validate_load_request above already rejected malformed
+ # values with both normalisers; the modular loader normalises after reading the
+ # tri-state.
+ transformer_quant = transformer_quant,
+ text_encoder_quant = text_encoder_quant,
+ # The speed layer lives BELOW this dispatch, which the modular branch never reached:
+ # no channels_last VAE, no cudnn.benchmark, no attention backend, no compile.
+ speed_mode = speed_mode,
+ attention_backend = attention_backend,
h3_task = h3_task,
_load_token = _load_token,
_base_local_dir = _base_local_dir,
@@ -3136,6 +3266,8 @@ class VideoBackend:
memory_mode: Optional[str],
transformer_quant: Optional[str] = None,
text_encoder_quant: Optional[str] = None,
+ speed_mode: Optional[str] = None,
+ attention_backend: Optional[str] = None,
h3_task: Optional[str] = None,
target: Any = None,
_load_token: Optional[int] = None,
@@ -3143,14 +3275,23 @@ class VideoBackend:
) -> dict[str, Any]:
"""Load MiniMax-H3 through its official Modular Diffusers workflow.
- ``transformer_quant`` and ``text_encoder_quant`` are ALREADY-normalised schemes (or None /
- "auto"). A scheme with a hosted pre-quantized checkpoint is built here and pre-seeded into
- the pipeline; anything else keeps the released bfloat16 components."""
+ ``transformer_quant`` and ``text_encoder_quant`` are RAW requests, read as the tri-state
+ ``_h3_precision_unset`` / ``_h3_precision_pinned_dense`` describe. Unset takes the hosted
+ quantized CONDITIONER and the released bfloat16 DENOISER; ``"none"`` pins the released
+ component either way; an explicit scheme is honored or falls back with the reason
+ recorded."""
# Defence in depth: validate_load_request now refuses a single_file modular pick outright,
# so this is unreachable from the routes. It stays because this method is also reachable
# directly, and by the time it runs the resident pipeline has already been torn down.
if kind != "pipeline":
raise ValueError("MiniMax-H3 Diffusers loading requires the pipeline artifact.")
+ umem_target = target if target is not None else resolve_diffusion_device_target()
+ # What the CALLER asked for, kept for the resolved record, before the tri-state below
+ # rewrites it. An unset request must not read back as a pin.
+ transformer_quant_requested = transformer_quant
+ text_encoder_quant_requested = text_encoder_quant
+ transformer_quant_is_auto = _h3_precision_unset(transformer_quant)
+ text_encoder_quant_is_auto = _h3_precision_unset(text_encoder_quant)
# Load one denoiser partition. fl2va also covers text-only generation.
workflow = h3_task or fam.modular_workflow
manager = diffusers.ComponentsManager()
@@ -3165,9 +3306,20 @@ class VideoBackend:
transformer_quant_engaged: Optional[str] = None
transformer_quant_reason = "released bfloat16 components"
# "auto" asks the backend to choose, and the auto ladder quantises a DENSE module on device
- # -- something this workflow never materialises -- so it stays on the released components.
- # Only an explicitly requested scheme takes the hosted path.
- scheme = transformer_quant if transformer_quant not in (None, TQ_AUTO) else None
+ # -- something this workflow never materialises -- so it stays on the released denoiser.
+ # Only an explicit scheme takes the hosted path. Those checkpoints ARE fast (the same
+ # 8-step job runs 23.7 s against 194 s) and were measured before being considered as a
+ # default: at the model's own 30-step schedule they show no NaN, no black frames and no
+ # visible degradation, but they RE-ROLL the sample -- mean SSIM 0.49 (int8) / 0.43 (fp8)
+ # against the released denoiser, where that config against itself scores 0.99. Different
+ # is not worse, but nothing here can tell the two apart, so they stay opt-in and the
+ # default takes its speed from the resident placement + regional compile below, which
+ # change no weights at all.
+ scheme = (
+ None if transformer_quant_is_auto else normalize_transformer_quant(transformer_quant)
+ )
+ if scheme == TQ_AUTO: # defence in depth; _h3_precision_unset already caught "auto"
+ scheme = None
# The hosted checkpoints are keyframe (fl2va) denoisers. validate_load_request refuses this
# pairing on the route; a direct call lands here, and seeding one would silently generate
# the wrong partition rather than fail, so drop to the released components instead.
@@ -3183,7 +3335,6 @@ class VideoBackend:
# component set is an OS kill with no torch OOM to catch. Placed after `scheme` settles and
# before ANY weight is opened -- the hosted pre-quantized denoiser below is materialised on
# the CPU, which is the same memory.
- umem_target = target if target is not None else resolve_diffusion_device_target()
self._raise_on_modular_unified_shortfall(
fam,
target = umem_target,
@@ -3265,7 +3416,14 @@ class VideoBackend:
text_encoder_quant_reason = "released bfloat16 components"
# Base-gated, through the same resolver the plan and the pre-fetch use, so a derivative
# base that keeps its own conditioner can never be handed this one.
- te_scheme = self._h3_te_quant_scheme(fam, text_encoder_quant, base)
+ te_scheme = self._h3_te_quant_scheme(fam, text_encoder_quant, base, umem_target)
+ # From here down the request is the NORMALISED scheme (None once "none" has pinned dense),
+ # which is what the reason strings and the fail-closed check below were written against.
+ text_encoder_quant = (
+ None if text_encoder_quant_is_auto else normalize_te_quant(text_encoder_quant)
+ )
+ if text_encoder_quant_is_auto and te_scheme is not None:
+ text_encoder_quant_reason = f"auto: hosted quantized {te_scheme} conditioner"
# And gated a second time, on the identity the base NAME cannot establish. That resolver
# compares repo ids, so a derivative stored under a directory (or repo) whose last segment
# is also MiniMax-H3 passes it. This pipeline's own modular index names the repo its
@@ -3309,6 +3467,16 @@ class VideoBackend:
f"no hosted quantized {text_encoder_quant} conditioner for {fam.name}; "
f"loaded the released bfloat16 encoder instead"
)
+ elif text_encoder_quant_is_auto and te_scheme is None:
+ # An UNSET request never fails a load, but it must still say why the fast default did
+ # not engage, or a 194-second generation looks like the intended behaviour.
+ text_encoder_quant_reason = (
+ f"auto declined: this pipeline's conditioner comes from "
+ f"{te_index_source or 'a source that could not be read'}, not {fam.base_repo}"
+ if te_index_declined
+ else "auto: no hosted quantized conditioner applies here; loaded the released "
+ "bfloat16 encoder"
+ )
if te_scheme is not None:
from .video_minimax_h3_te import load_h3_quantized_text_encoder
text_encoder = load_h3_quantized_text_encoder(
@@ -3388,10 +3556,10 @@ class VideoBackend:
memory_mode = memory_mode,
scheme = None,
)
- # cache_dir is passed here too, for the same reason the token is: load_components forwards
- # its extra kwargs through ComponentSpec.load into each component's from_pretrained, and
- # without it those ~145 GB of Hub-pinned components resolve against the HF_HUB_CACHE snapshot
- # taken at import time rather than Studio's live cache folder, which the user can move.
+ # cache_dir for the same reason as the token: load_components forwards extra kwargs through
+ # ComponentSpec.load into each from_pretrained, and without it those ~145 GB of Hub-pinned
+ # components resolve against the import-time HF_HUB_CACHE snapshot rather than Studio's
+ # live cache folder, which the user can move.
pipe.load_components(
workflow = workflow,
dtype = dtype,
@@ -3419,6 +3587,28 @@ class VideoBackend:
)
offload_policy = "none"
denoiser_pinned = False
+ # load_components just spent minutes building ~145 GB of components, and everything below
+ # this line either moves weights onto the card or mutates process-wide backend flags. The
+ # conventional placement path fences on the token for exactly that reason; without the
+ # same fence here a cancelled or superseded worker resumes into a GPU a replacement load
+ # already owns and puts a 66.3 GB denoiser next to it. The next check was the state commit,
+ # which is after the placement it is meant to prevent.
+ if _load_token is not None and _load_token != self._load_token:
+ del pipe
+ clear_gpu_cache()
+ raise RuntimeError("Video load was cancelled or superseded.")
+ # Resolved BEFORE the placement below, not after it: the dense pin is a speed optimisation
+ # by its own reasoning, so "off" has to be known in time to decline it.
+ effective_speed = resolve_speed_mode(speed_mode, is_gguf = False, dense_default = SPEED_DEFAULT)
+ if effective_speed == SPEED_MAX:
+ # SPEED_MAX compiles with dynamic=False. H3's packed sequence length carries the
+ # caption's token rows, so a static graph recompiles per prompt: measured 0.957-1.000
+ # s/step static against 1.000-1.040 dynamic, and two recompiles paid for it.
+ logger.info(
+ "video.speed_mode: MiniMax-H3 runs the 'default' regional profile under max "
+ "(a static graph retraces on the caption's contribution to the packed length)"
+ )
+ effective_speed = SPEED_DEFAULT
if device != "cpu":
manager.enable_auto_cpu_offload(
# Measured H3 activations need substantially more than the
@@ -3429,6 +3619,7 @@ class VideoBackend:
memory_reserve_margin = "40GB",
)
offload_policy = "model"
+ denoiser = getattr(pipe, "transformer", None)
if transformer_quant_engaged:
# enable_auto_cpu_offload just parked every component on the CPU and will move
# each one back inside its own pre_forward. A torchao pre-quantized denoiser does
@@ -3436,9 +3627,103 @@ class VideoBackend:
# and take it out of the rotation. Nothing else changes: the encoder and the VAEs
# keep their hooks and still offload around it.
from .diffusion_prequant import pin_prequantized_module
- denoiser_pinned = pin_prequantized_module(
- manager, getattr(pipe, "transformer", None), device, logger = logger
+ denoiser_pinned = pin_prequantized_module(manager, denoiser, device, logger = logger)
+ elif denoiser is not None and effective_speed != SPEED_OFF:
+ # The RELEASED bfloat16 denoiser, for a different reason: not correctness, speed.
+ # Which is why speed=off skips this branch and keeps the rotation: taking a module
+ # out of the offload rotation is not free, it trades the ability to budget the
+ # denoiser against the requested frame count for throughput, and an explicit "off"
+ # is the one request that says do not make that trade. The pre-quantized pin above
+ # is NOT gated the same way: there it is correctness, not speed.
+ # Every denoise step moves the same 66.3 GB module in and out, and a module that
+ # moves cannot be compiled either (the onload hooks fight the graph). Quantizing
+ # the conditioner is what makes pinning affordable -- 66.3 GB denoiser + 27.2 GB
+ # encoder resident rather than two 66 GB components. Sized against LIVE free
+ # memory, after every component is built and parked: the only reading that
+ # describes the card this load actually got.
+ sizes = _h3_dense_denoiser_resident_bytes(
+ fam, denoiser = denoiser, te_scheme = text_encoder_quant_engaged, dtype = dtype
)
+ free_bytes = None
+ if sizes is not None:
+ try:
+ free_bytes = torch.cuda.mem_get_info()[0] if device == "cuda" else None
+ except Exception: # noqa: BLE001 -- unreadable free memory keeps the rotation
+ free_bytes = None
+ if sizes is not None and free_bytes is not None:
+ denoiser_bytes, others_bytes = sizes
+ if _h3_dense_denoiser_fits(sizes, free_bytes):
+ from .diffusion_prequant import pin_prequantized_module
+ denoiser_pinned = pin_prequantized_module(
+ manager,
+ denoiser,
+ device,
+ logger = logger,
+ label = "released bfloat16 denoiser",
+ )
+ else:
+ logger.info(
+ "video.h3_denoiser_placement: %.1f GB free is under the %.1f GB the "
+ "denoiser plus the other components need, so it stays in the offload "
+ "rotation (and is not compiled)",
+ free_bytes / 1e9,
+ (denoiser_bytes + others_bytes) / 1e9,
+ )
+
+ # ── the speed layer this workflow used to skip entirely.
+ # apply_speed_optims / select_attention_backend live below the modular dispatch in
+ # load_pipeline, which returns first, so every H3 load reported speed_optims [] and
+ # attention_backend null. Called here rather than by moving the dispatch, because the
+ # compile decision below needs ``denoiser_pinned``, which only exists after the offload.
+ # ``effective_speed`` itself was resolved above the offload, because the pin reads it.
+ if effective_speed in (SPEED_DEFAULT, SPEED_MAX) and not denoiser_pinned:
+ # Compile ONLY over a resident denoiser: inside the rotation the compiled graph fights
+ # the onload hooks, measured 69-85 s against 30-115 s eager on the same job at a
+ # 135.7 GB peak. The rest of the tier (channels_last VAE, cudnn.benchmark, attention
+ # backend) is kept.
+ logger.info(
+ "video.speed_mode: MiniMax-H3 denoiser is in the CPU-offload rotation, so the "
+ "regional compile is held off (measured slower than eager there); keeping the "
+ "lossless optimisations"
+ )
+ effective_speed = SPEED_EAGER
+ backend_flags = snapshot_backend_flags()
+ # Until the state commit hands ownership to _teardown_state_locked, a failure has to restore
+ # these process-wide flags itself. Registered BEFORE the first mutation, as above.
+ self._precommit_globals = (_load_token, backend_flags)
+ attention_engaged = None
+ speed_optims: tuple = ()
+ try:
+ attention_engaged = apply_attention_backend(
+ pipe,
+ select_attention_backend(
+ umem_target, attention_backend, speed_active = effective_speed != SPEED_OFF
+ ),
+ logger = logger,
+ # The probe behind this must see the dtype the pipeline actually RUNS in.
+ target = types.SimpleNamespace(device = device, dtype = dtype),
+ )
+ applied = apply_speed_optims(
+ pipe,
+ types.SimpleNamespace(
+ device = device,
+ dtype = dtype,
+ supports_default_torch_compile = getattr(
+ umem_target, "supports_default_torch_compile", False
+ ),
+ ),
+ is_gguf = False,
+ family = fam,
+ speed_mode = effective_speed,
+ cache_active = False,
+ # The conditioner and the VAEs stay in the rotation even when the denoiser is
+ # pinned, so the onload hooks are live and fullgraph has to drop.
+ offload_active = offload_policy != "none",
+ logger = logger,
+ )
+ speed_optims = tuple(k for k, v in applied.items() if v)
+ except Exception as exc: # noqa: BLE001 -- optimisation only, never fail a load
+ logger.warning("video.h3_speed_optims failed, continuing unoptimised: %s", exc)
resolved = build_resolved_record(
{
@@ -3448,11 +3733,21 @@ class VideoBackend:
"MiniMax-H3 ComponentsManager auto CPU offload"
+ ("; the pre-quantized denoiser stays resident" if denoiser_pinned else ""),
),
- "speed_mode": (None, "off", "modular pipeline uses its native execution path"),
- "attention_backend": (None, "native", "Diffusers model default"),
+ "speed_mode": (
+ speed_mode,
+ effective_speed,
+ "regional compile over the resident pre-quantized denoiser"
+ if "compiled" in speed_optims
+ else "lossless optimisations only; the denoiser is in the CPU-offload rotation",
+ ),
+ "attention_backend": (
+ attention_backend,
+ attention_engaged or "native",
+ "cuDNN fused attention on NVIDIA when a speed profile is active",
+ ),
"transformer_cache": (None, "off", "not supported by this modular workflow"),
"transformer_quant": (
- transformer_quant,
+ transformer_quant_requested,
transformer_quant_engaged or "off",
transformer_quant_reason,
),
@@ -3460,7 +3755,7 @@ class VideoBackend:
# fallback in the resolved record, never disappear into a bare "off" that looks
# like the caller never asked.
"text_encoder_quant": (
- text_encoder_quant,
+ text_encoder_quant_requested,
text_encoder_quant_engaged or "off",
text_encoder_quant_reason,
),
@@ -3484,7 +3779,11 @@ class VideoBackend:
offload_policy = offload_policy,
vae_tiling = True,
memory_mode = normalize_memory_mode(memory_mode),
- speed_mode = SPEED_OFF,
+ speed_mode = effective_speed,
+ # Already filtered to the engaged optimisations (True names only).
+ speed_optims = speed_optims,
+ backend_flags = backend_flags,
+ attention_backend = attention_engaged,
# The ENGAGED scheme, not the requested one, exactly as the conventional path
# records it: status() reports this field, and a dense fallback must not claim a
# quant the resident denoiser does not have.
@@ -3496,10 +3795,15 @@ class VideoBackend:
h3_denoiser_pinned = denoiser_pinned,
resolved = resolved,
)
+ # Ownership of the globals transferred to _state / _teardown_state_locked.
+ self._precommit_globals = None
logger.info(
- "video.loaded: %s (%s, modular diffusers, transformer_quant=%s, text_encoder_quant=%s)",
+ "video.loaded: %s (%s, modular diffusers, speed=%s%s, transformer_quant=%s, "
+ "text_encoder_quant=%s)",
repo_id,
fam.name,
+ effective_speed,
+ f" [{', '.join(speed_optims)}]" if speed_optims else "",
transformer_quant_engaged or "off",
text_encoder_quant_engaged or "off",
)
@@ -3844,9 +4148,9 @@ class VideoBackend:
steps = int(steps or default_steps)
guidance = float(default_guidance if guidance is None else guidance)
if not fam.supports_cfg:
- # A CFG-free family never reads this number: diffusers gets no guidance kwarg
- # and the native path pins cfg_scale to 1.0. Clamp it here so the recipe cannot
- # record a guidance that did not run.
+ # A CFG-free family never reads this: diffusers gets no guidance kwarg and the
+ # native path pins cfg_scale to 1.0. Clamped so the recipe cannot record a
+ # guidance that did not run.
guidance = float(fam.default_guidance)
shift, audio_shift = self._resolve_flow_shifts(
fam, state.engine, flow_shift, audio_flow_shift
@@ -4495,9 +4799,9 @@ class VideoBackend:
"flow_shift": flow_shift,
# sd.cpp pins the audio schedule, so the recipe records what it actually ran.
"audio_flow_shift": state.family.default_audio_flow_shift,
- # The BUILD this clip came off, read from the ENGAGED state and never from the
- # request, exactly as the diffusers path records it. Without these six the
- # sidecar of every native GGUF clip saves a blank recipe.
+ # The BUILD this clip came off, from the ENGAGED state and never the request,
+ # as the diffusers path records it. Without these six every native GGUF clip
+ # saves a blank recipe.
"model_kind": state.kind,
"gguf_filename": state.gguf_filename,
"transformer_quant": state.transformer_quant,
diff --git a/studio/backend/core/inference/video_families.py b/studio/backend/core/inference/video_families.py
index 7bd730be772..2f2513626ec 100644
--- a/studio/backend/core/inference/video_families.py
+++ b/studio/backend/core/inference/video_families.py
@@ -153,7 +153,16 @@ _FAMILIES: tuple[VideoFamily, ...] = (
duration_presets = (5.0, 10.0, 14.4),
# Decimal GB resident estimates: transformer, Qwen3-VL conditioner, video+audio VAEs.
bf16_components_gb = (66.3, 66.8, 11.1),
- supports_torch_compile = False,
+ # Regionally compilable. The DiT declares _repeated_blocks (MiniMaxH3TransformerBlock +
+ # MiniMaxH3TokenRefinerBlock); every block sees (1, S, 5376) plus an (S,) index tensor,
+ # where S is the PACKED length (18,870 video + 207 audio rows + the caption's text rows at
+ # 960x544x124). The caption moves S by ~2% (19,096 at 19 tokens vs 19,479 at 402) and S
+ # cannot change mid-denoise, so dynamic=True traces once and holds: measured 1.298-1.342
+ # s/step eager vs 1.000-1.040 compiled (1.30x), first forward 10.2 s, zero recompiles
+ # across captions of 19/19/37/128/402 tokens. The loader engages this only when the
+ # denoiser is RESIDENT; compiling inside a full CPU-offload rotation measured slower than
+ # eager, so that case stays on the no-compile tier.
+ supports_torch_compile = True,
gguf_repo = "unsloth/MiniMax-H3-GGUF",
# Hosted pre-quantized FL2VA denoisers. The modular workflow builds each component through
# its own from_pretrained, so there is no dense module to quantise in place: these are the
diff --git a/studio/backend/core/inference/video_minimax_h3_te.py b/studio/backend/core/inference/video_minimax_h3_te.py
index 39b31a18544..49df27535d4 100644
--- a/studio/backend/core/inference/video_minimax_h3_te.py
+++ b/studio/backend/core/inference/video_minimax_h3_te.py
@@ -71,6 +71,16 @@ H3_TE_QUANT_FILES: dict[str, str] = {
"int8": "text_encoders/qwen3vl_32b_minimax_h3_int8_convrot.safetensors",
}
+# The scheme an UNSET ``text_encoder_quant`` resolves to on a device that supports it.
+#
+# The released conditioner is a 66.7 GB dense bfloat16 Qwen3-VL of 64 decoder layers and H3 reads
+# ``hidden_states[50]``; transformers has no early exit, so the default runs all 64 layers and
+# streams 66.7 GB across the CPU-offload boundary every generation. The hosted artifact is 27.1 GB
+# over 50 layers with the same read, which is why it is the default rather than an opt-in. NOT an
+# INT8 GEMM: the ConvRot forward dequantizes to the compute dtype and runs an ordinary
+# ``F.linear``, so the win is bytes moved and layers executed, not faster math.
+H3_TE_QUANT_DEFAULT = "int8"
+
# Resident bytes of each conditioner, decimal GB, measured from the safetensors headers on the Hub
# (2026-08-09) as the end of the last tensor's data. The quantized load is storage-faithful -- INT8
# stays INT8, the per-output-channel scales stay float32, the vision tower and the embedding table
diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py
index 0e60dacc717..2910fa38f4b 100644
--- a/studio/backend/models/inference.py
+++ b/studio/backend/models/inference.py
@@ -3236,7 +3236,9 @@ class VideoLoadRequest(BaseModel):
"backend's transformer_quant field.",
)
)
- text_encoder_quant: Optional[Literal["fp8", "fp8_dynamic", "int8", "nvfp4"]] = Field(
+ text_encoder_quant: Optional[
+ Literal["auto", "none", "off", "fp8", "fp8_dynamic", "int8", "nvfp4"]
+ ] = Field(
None,
description = "Quantise the dense companion text encoder (Gemma3 / UMT5 / Qwen2.5-VL), "
"which loads bf16 from the base repo regardless of how the DiT was sourced and is often "
@@ -3244,7 +3246,10 @@ class VideoLoadRequest(BaseModel):
"8.9); fp8_dynamic = torchao per-row fp8 COMPUTE on the tensor cores (cc >= 8.9); int8 = "
"torchao int8 COMPUTE with per-family keep-bf16 selection (cc >= 8.0; falls back to fp8 "
"for a family without a measured schedule); nvfp4 = torchao 4-bit weight-only (Blackwell "
- "sm_100+). null keeps the encoder dense. Mirrors the image backend's field.",
+ "sm_100+). null/auto leaves the choice to the backend, which keeps the encoder dense on "
+ "every family except MiniMax-H3, where it takes the hosted quantized conditioner; "
+ "none/off always keeps the released bf16 encoder, which on MiniMax-H3 is the only way "
+ "to ask for it. Mirrors the image backend's field.",
)
h3_task: Optional[Literal["fl2va", "ref2va"]] = Field(
diff --git a/studio/backend/tests/test_diffusion_speed.py b/studio/backend/tests/test_diffusion_speed.py
index 39dd91fd1da..30247ab83d9 100644
--- a/studio/backend/tests/test_diffusion_speed.py
+++ b/studio/backend/tests/test_diffusion_speed.py
@@ -707,3 +707,67 @@ def test_regional_compile_arms_cache_hook_inners(monkeypatch):
)
assert applied["compiled"] is True
assert armed == [pipe.transformer]
+
+
+# ── the inductor runtime gate ────────────────────────────────────────────────
+# The Studio workers already refuse torch.compile when Triton is missing on Windows; the diffusion
+# and video backends run in the SERVER process, which those gates never reach.
+
+
+def _clear_runtime_cache():
+ from core.inference.diffusion_speed import torch_compile_runtime_available
+ torch_compile_runtime_available.cache_clear()
+
+
+def test_torchdynamo_disable_is_honored_on_every_platform(monkeypatch):
+ from core.inference import diffusion_speed as ds_mod
+
+ # compile_eligible reads torch to test the dtype, and without the stub it returns False for
+ # every input -- which would make the assertions below pass whatever the gate did.
+ _stub_torch(monkeypatch)
+ _clear_runtime_cache()
+ monkeypatch.delenv("TORCHDYNAMO_DISABLE", raising = False)
+ # The positive control. Without it the two `is False` lines below prove nothing.
+ assert ds_mod.compile_eligible(_target(), is_gguf = False, family = _family()) is True
+
+ _clear_runtime_cache()
+ monkeypatch.setenv("TORCHDYNAMO_DISABLE", "1")
+ assert ds_mod.torch_compile_runtime_available() is False
+ assert ds_mod.compile_eligible(_target(), is_gguf = False, family = _family()) is False
+ _clear_runtime_cache()
+ monkeypatch.setenv("TORCHDYNAMO_DISABLE", "0")
+ assert ds_mod.torch_compile_runtime_available() is True
+ assert ds_mod.compile_eligible(_target(), is_gguf = False, family = _family()) is True
+ _clear_runtime_cache()
+
+
+def test_windows_without_triton_falls_back_to_eager(monkeypatch):
+ """A compile call on a Windows install with no Triton wheel is not an error at compile time --
+ it fails at the first forward, mid-generation. Decide it here instead."""
+ from core.inference import diffusion_speed as ds_mod
+
+ monkeypatch.delenv("TORCHDYNAMO_DISABLE", raising = False)
+ monkeypatch.setattr(ds_mod.sys, "platform", "win32")
+ monkeypatch.setitem(sys.modules, "triton", None) # `import triton` -> ImportError
+ _clear_runtime_cache()
+ assert ds_mod.torch_compile_runtime_available() is False
+ assert ds_mod.compile_eligible(_target(), is_gguf = False, family = _family()) is False
+
+ # A Windows install that DOES have the wheel is not held back.
+ monkeypatch.setitem(sys.modules, "triton", types.ModuleType("triton"))
+ _clear_runtime_cache()
+ assert ds_mod.torch_compile_runtime_available() is True
+ _clear_runtime_cache()
+
+
+def test_linux_and_mac_are_not_asked_about_triton(monkeypatch):
+ """Only Windows ships without it, and a probe import on a healthy Linux box is pure cost."""
+ from core.inference import diffusion_speed as ds_mod
+
+ monkeypatch.delenv("TORCHDYNAMO_DISABLE", raising = False)
+ monkeypatch.setitem(sys.modules, "triton", None)
+ for platform_name in ("linux", "darwin"):
+ monkeypatch.setattr(ds_mod.sys, "platform", platform_name)
+ _clear_runtime_cache()
+ assert ds_mod.torch_compile_runtime_available() is True
+ _clear_runtime_cache()
diff --git a/studio/backend/tests/test_video_h3_te_quant.py b/studio/backend/tests/test_video_h3_te_quant.py
index 3c66acf2f61..01048f76adc 100644
--- a/studio/backend/tests/test_video_h3_te_quant.py
+++ b/studio/backend/tests/test_video_h3_te_quant.py
@@ -52,6 +52,26 @@ def _fam(
return types.SimpleNamespace(name = name, modular_workflow = modular_workflow, base_repo = base_repo)
+# Device targets are passed EXPLICITLY below: the auto default reads the real device when none is
+# given, and a test whose answer depends on the runner's GPU is not a test.
+def _cuda_target():
+ return types.SimpleNamespace(
+ device = "cuda", dtype = torch.bfloat16, supports_default_torch_compile = True
+ )
+
+
+def _cpu_target():
+ return types.SimpleNamespace(
+ device = "cpu", dtype = torch.float32, supports_default_torch_compile = False
+ )
+
+
+def _mps_target():
+ return types.SimpleNamespace(
+ device = "mps", dtype = torch.bfloat16, supports_default_torch_compile = False
+ )
+
+
# ── the resolver ─────────────────────────────────────────────────────────────────
def test_only_int8_has_a_hosted_conditioner():
assert h3_te_quant_scheme("int8") == "int8"
@@ -261,7 +281,46 @@ def test_only_a_modular_family_drops_its_dense_encoder():
# A conventional family casts its own dense encoder in place and still needs those shards.
assert VideoBackend._h3_te_quant_scheme(_fam(modular_workflow = None), "int8", H3_BASE) is None
assert VideoBackend._h3_te_quant_scheme(_fam(), "fp8", H3_BASE) is None
- assert VideoBackend._h3_te_quant_scheme(_fam(), None, H3_BASE) is None
+
+
+# ── the tri-state: unset is the fast default, "none" is the escape hatch ─────────
+def test_an_unset_request_takes_the_hosted_conditioner():
+ """Unset is what the video page sends, so this is the whole point: it must resolve to the
+ hosted 27.1 GB / 50-layer conditioner, not to the released 66.7 GB / 64-layer one."""
+ assert VideoBackend._h3_te_quant_scheme(_fam(), None, H3_BASE, _cuda_target()) == "int8"
+ assert VideoBackend._h3_te_quant_scheme(_fam(), "auto", H3_BASE, _cuda_target()) == "int8"
+ assert VideoBackend._h3_te_quant_scheme(_fam(), "", H3_BASE, _cuda_target()) == "int8"
+
+
+def test_none_still_pins_the_released_encoder():
+ """The escape hatch has to exist and has to be distinguishable from unset, or a bit-exact
+ comparison against the released components becomes unexpressible."""
+ for pinned in ("none", "None", "off", " OFF "):
+ assert VideoBackend._h3_te_quant_scheme(_fam(), pinned, H3_BASE, _cuda_target()) is None
+
+
+def test_the_auto_default_is_cuda_only():
+ """MPS and CPU keep the components they load today. The ConvRot forward is plain torch and
+ would likely run there, but nobody has measured it, and the modular loader does not reach a Mac
+ at all (ComponentsManager.enable_auto_cpu_offload needs mem_get_info, which torch.mps lacks).
+ An EXPLICIT request is unaffected by this gate."""
+ for target in (_cpu_target(), _mps_target()):
+ assert VideoBackend._h3_te_quant_scheme(_fam(), None, H3_BASE, target) is None
+ assert VideoBackend._h3_te_quant_scheme(_fam(), "int8", H3_BASE, target) == "int8"
+ # A CUDA target whose compute dtype is not bf16 (a pre-Ampere card promoted to fp32) is not a
+ # device this was measured on either.
+ fp32_cuda = types.SimpleNamespace(device = "cuda", dtype = torch.float32)
+ assert VideoBackend._h3_te_quant_scheme(_fam(), None, H3_BASE, fp32_cuda) is None
+
+
+def test_an_unset_request_on_a_derivative_still_keeps_its_own_encoder():
+ """The auto default must not loosen the base gate: substituting someone else's conditioner is
+ exactly as wrong when the backend chose it as when the user asked for it."""
+ assert (
+ VideoBackend._h3_te_quant_scheme(_fam(), None, "someone/MiniMax-H3-anime", _cuda_target())
+ is None
+ )
+ assert VideoBackend._h3_te_quant_scheme(_fam(), None, None, _cuda_target()) is None
def test_only_the_base_the_artifact_was_cut_from_gets_the_hosted_conditioner():
@@ -608,6 +667,12 @@ def test_an_explicit_encoder_request_that_engages_nothing_is_refused(monkeypatch
hf_token = None,
memory_mode = None,
text_encoder_quant = "int8",
+ # This test is about the ENCODER refusal, so pin the denoiser dense: unset would
+ # resolve to the hosted int8 checkpoint and the fake diffusers module has no
+ # transformer class to build it. CPU target so the answer cannot depend on the
+ # runner's GPU.
+ transformer_quant = "none",
+ target = _cpu_target(),
diffusers = fake_diffusers,
torch = None,
)
diff --git a/studio/backend/tests/test_video_prequant.py b/studio/backend/tests/test_video_prequant.py
index 9b6bd71c02a..6683b1a0f50 100644
--- a/studio/backend/tests/test_video_prequant.py
+++ b/studio/backend/tests/test_video_prequant.py
@@ -388,3 +388,222 @@ def test_pinning_still_places_the_module_when_the_manager_is_unrecognisable():
assert pin_prequantized_module(manager, transformer, "cuda") is False
assert transformer.moved_to == ["cuda"]
assert manager.model_hooks == before
+
+
+# ── the denoiser default: measured, and deliberately NOT changed ─────────────────
+# The hosted checkpoints are the fast ones (the same 8-step job runs 23.7 s against 194 s), so the
+# question was whether to default to one. Measured against the released denoiser at H3's own
+# 30-step schedule, fixed prompt and seed, 960x544x124: no NaN, no black frames, no visible
+# degradation, but a re-rolled sample -- mean SSIM 0.49 (int8) / 0.43 (fp8) where the released
+# config against ITSELF scores 0.99. They stay opt-in.
+
+
+def _h3_fam():
+ fam = detect_video_family("MiniMaxAI/MiniMax-H3")
+ assert fam is not None and fam.modular_workflow
+ return fam
+
+
+def test_an_unset_denoiser_request_keeps_the_released_weights():
+ fam, base = _h3_fam(), "MiniMaxAI/MiniMax-H3"
+ for unset in (None, "auto"):
+ assert VideoBackend._denoiser_prequant_covered(fam, unset, base) is False
+ # An explicit scheme still takes the hosted checkpoint, and the plan still drops the dense
+ # shards for it -- the opt-in is unchanged.
+ assert VideoBackend._denoiser_prequant_covered(fam, "int8", base) is True
+ assert VideoBackend._denoiser_prequant_covered(fam, "fp8", base) is True
+
+
+def test_the_dense_denoiser_is_pinned_only_when_it_actually_fits():
+ """Pinning the released denoiser is what makes the regional compile possible (a module the
+ offload hooks move per forward cannot be compiled) and quantizing the conditioner is what makes
+ it affordable. Being an optimisation, the fit test stays conservative: the denoiser plus
+ everything that still has to run beside it."""
+ import torch
+
+ from core.inference.video import _h3_dense_denoiser_resident_bytes
+
+ fam = _h3_fam()
+
+ class _Denoiser:
+ def __init__(self, gb):
+ self._t = torch.empty(0)
+ self._gb = gb
+
+ def parameters(self):
+ return iter(())
+
+ def buffers(self):
+ # One notional tensor standing in for the module's weight bytes.
+ return iter([torch.empty(int(self._gb * 1e9), dtype = torch.uint8, device = "meta")])
+
+ # A meta tensor is skipped (it holds no memory yet), so an unbuilt module sizes to nothing
+ # rather than to a number that would wrongly authorise a pin.
+ assert (
+ _h3_dense_denoiser_resident_bytes(
+ fam, denoiser = _Denoiser(66.3), te_scheme = "int8", dtype = torch.bfloat16
+ )
+ is None
+ )
+ assert (
+ _h3_dense_denoiser_resident_bytes(
+ fam, denoiser = None, te_scheme = "int8", dtype = torch.bfloat16
+ )
+ is None
+ )
+
+ class _Real(_Denoiser):
+ def buffers(self):
+ return iter([torch.empty(1024, dtype = torch.uint8)])
+
+ sizes = _h3_dense_denoiser_resident_bytes(
+ fam, denoiser = _Real(0), te_scheme = "int8", dtype = torch.bfloat16
+ )
+ assert sizes is not None
+ denoiser_bytes, others = sizes
+ assert denoiser_bytes == 1024
+ # The conditioner is priced at the precision the load ENGAGED, which is the whole reason the
+ # dense denoiser can be resident at all: 27.2 GB hosted against 66.8 GB released.
+ dense_sizes = _h3_dense_denoiser_resident_bytes(
+ fam, denoiser = _Real(0), te_scheme = None, dtype = torch.bfloat16
+ )
+ assert dense_sizes is not None and dense_sizes[1] - others > 38 * 1000**3
+ # And it is never just the weights: the activation headroom is in there too.
+ assert others > (27.2 + fam.bf16_components_gb[2]) * 1000**3
+
+
+def test_the_pin_decision_itself_refuses_a_card_that_cannot_hold_it():
+ """The sizing above is only half of it. This is the comparison that authorises the pin, and
+ getting it wrong is an OOM rather than a slow generation, so it is asserted separately from
+ the estimate that feeds it."""
+ from core.inference.video import _h3_dense_denoiser_fits
+
+ sizes = (66_300_000_000, 33_000_000_000) # denoiser, everything else
+ need = sizes[0] + sizes[1]
+
+ assert _h3_dense_denoiser_fits(sizes, need) is True # exactly enough still fits
+ assert _h3_dense_denoiser_fits(sizes, need + 1) is True
+ assert _h3_dense_denoiser_fits(sizes, need - 1) is False # one byte short does not
+ # The denoiser alone fitting is NOT enough: the conditioner and the VAEs still have to run.
+ assert _h3_dense_denoiser_fits(sizes, sizes[0]) is False
+ # No estimate and no reading both keep the rotation, which is today's behaviour.
+ assert _h3_dense_denoiser_fits(None, need) is False
+ assert _h3_dense_denoiser_fits(sizes, None) is False
+
+
+# ── the conditioner opt-out has to be reachable ──────────────────────────────────
+
+
+def test_the_released_conditioner_is_reachable_through_the_load_api():
+ """The new default makes an omitted ``text_encoder_quant`` select the hosted INT8 conditioner,
+ so ``none``/``off`` became the only way to ask for the released bfloat16 one. That request has
+ to survive both gates it passes through -- the request model and the cheap normaliser -- or the
+ bfloat16 reference configuration is unreachable and no comparison against it can be run."""
+ from pydantic import ValidationError
+
+ from core.inference.diffusion_precision import normalize_te_quant
+ from models.inference import VideoLoadRequest
+
+ for opt_out in ("none", "off", "None", " OFF "):
+ assert normalize_te_quant(opt_out) is None
+ # "auto" is the same no-scheme answer to this normaliser; the tri-state that distinguishes it
+ # from an opt-out reads the RAW request before normalising.
+ assert normalize_te_quant("auto") is None
+ # A genuinely unsupported scheme is still refused here, cheaply, as before.
+ with pytest.raises(ValueError):
+ normalize_te_quant("int3")
+
+ def _request(value):
+ return VideoLoadRequest(model_path = "MiniMaxAI/MiniMax-H3", text_encoder_quant = value)
+
+ for accepted in (None, "auto", "none", "off", "fp8", "fp8_dynamic", "int8", "nvfp4"):
+ assert _request(accepted).text_encoder_quant == accepted
+ with pytest.raises(ValidationError):
+ _request("int3")
+
+
+def test_the_opt_out_spellings_reach_the_h3_tri_state():
+ """Reaching the model is only useful if the tri-state then reads them as the dense pin rather
+ than folding them into the unset branch that takes the hosted checkpoint."""
+ from core.inference.video import _h3_precision_pinned_dense, _h3_precision_unset
+
+ for opt_out in ("none", "off", "OFF", " none "):
+ assert _h3_precision_pinned_dense(opt_out) is True
+ assert _h3_precision_unset(opt_out) is False
+ for unset in (None, "", "auto", "AUTO"):
+ assert _h3_precision_unset(unset) is True
+ assert _h3_precision_pinned_dense(unset) is False
+
+
+def test_speed_off_declines_the_dense_pin_but_never_the_prequantized_one():
+ """The dense pin is a speed optimisation by its own reasoning, so an explicit ``speed=off``
+ has to decline it: taking the denoiser out of the rotation trades the ability to budget it
+ against the requested frame count for throughput. The pre-quantized pin is NOT the same
+ decision -- a torchao module does not survive the mid-block move -- so it stays unconditional.
+ """
+ import ast
+ import inspect
+ import textwrap
+
+ from core.inference.diffusion_speed import SPEED_DEFAULT, SPEED_OFF, resolve_speed_mode
+
+ # "off" is the only profile that reaches the gate as SPEED_OFF, so it is the only one the
+ # gate can decline. Asserted on the resolver rather than assumed.
+ assert resolve_speed_mode("off", is_gguf = False, dense_default = SPEED_DEFAULT) == SPEED_OFF
+ for on in ("default", "max"):
+ assert resolve_speed_mode(on, is_gguf = False, dense_default = SPEED_DEFAULT) != SPEED_OFF
+
+ # The gate itself, read off the loader: the dense branch is the ``elif`` beside the
+ # pre-quantized ``if``, and only the dense one may mention the speed profile. Read from the
+ # source because standing up a modular load to observe the placement is not something this
+ # network-free suite can do, and an ungated pin is exactly the regression worth catching.
+ source = textwrap.dedent(inspect.getsource(VideoBackend._load_h3_modular_pipeline))
+ pins = [
+ node
+ for node in ast.walk(ast.parse(source))
+ if isinstance(node, ast.If)
+ and "transformer_quant_engaged" == getattr(node.test, "id", None)
+ and node.orelse
+ ]
+ assert len(pins) == 1, "the two denoiser placements are no longer one if/elif pair"
+ prequantized_branch, dense_branch = pins[0], pins[0].orelse[0]
+ assert isinstance(dense_branch, ast.If)
+ dense_test = ast.dump(dense_branch.test)
+ assert "SPEED_OFF" in dense_test, "the dense pin no longer honours an explicit speed=off"
+ assert "denoiser" in dense_test
+ # And the correctness pin is still unconditional on the profile.
+ assert "SPEED_OFF" not in ast.dump(prequantized_branch.test)
+ for stmt in prequantized_branch.body:
+ assert "SPEED_OFF" not in ast.dump(stmt)
+
+
+def test_the_dense_placement_is_fenced_on_the_load_token():
+ """``load_components`` spends minutes building ~145 GB, and everything after it either moves
+ weights onto the card or mutates process-wide backend flags. A cancelled or superseded worker
+ that resumes there puts a 66.3 GB denoiser next to a model a replacement load already owns.
+ The conventional placement path fences for exactly that reason; this one has to as well, and
+ the fence has to sit BEFORE the placement rather than at the state commit after it."""
+ import ast
+ import inspect
+ import textwrap
+
+ source = textwrap.dedent(inspect.getsource(VideoBackend._load_h3_modular_pipeline))
+ tree = ast.parse(source)
+
+ def _line(predicate) -> int:
+ hits = [node.lineno for node in ast.walk(tree) if predicate(node)]
+ assert hits, "landmark not found in the modular loader"
+ return min(hits)
+
+ load_components = _line(lambda n: isinstance(n, ast.Attribute) and n.attr == "load_components")
+ fence = _line(
+ lambda n: isinstance(n, ast.Compare)
+ and "_load_token" in ast.dump(n)
+ and n.lineno > load_components
+ )
+ placement = _line(
+ lambda n: isinstance(n, ast.Attribute) and n.attr == "enable_auto_cpu_offload"
+ )
+ assert (
+ load_components < fence < placement
+ ), "the token fence must sit between load_components and the placement it guards"