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 video: park MiniMax-H3's rotating VAEs by re-pointing at their pageable host copy when pinning is not allowed (#15715)

Daniel Hanc432bc3393d7
diff --git a/studio/backend/core/inference/video_minimax_h3_residency.py b/studio/backend/core/inference/video_minimax_h3_residency.py
index 47d220e78ab..3a4d7ebff41 100644
--- a/studio/backend/core/inference/video_minimax_h3_residency.py
+++ b/studio/backend/core/inference/video_minimax_h3_residency.py
@@ -886,6 +886,7 @@ def _outside_inference_mode(fn: Any) -> Any:
H3_VAE_PINNED_SWAP_ENV = "UNSLOTH_H3_VAE_PINNED_SWAP"
+H3_VAE_PAGEABLE_SWAP_ENV = "UNSLOTH_H3_VAE_PAGEABLE_SWAP"
def install_pinned_swap(
@@ -894,13 +895,11 @@ def install_pinned_swap(
logger: Any = None,
label: str = "component",
) -> bool:
- """Stock rotation uploads from pageable memory and copies back every park; device moves use pinned host copy; UNSLOTH_H3_VAE_PINNED_SWAP=0 keeps stock moves."""
- if str(os.environ.get(H3_VAE_PINNED_SWAP_ENV, "")).strip().lower() in (
- "0",
- "off",
- "false",
- "no",
- ):
+ """Stock rotation uploads from pageable memory and copies back every park; device moves use pinned host copy; UNSLOTH_H3_VAE_PINNED_SWAP=0 keeps stock moves.
+ Where pinning is not allowed the swap keeps the module's own pageable host copy, so a park is still a re-point
+ (UNSLOTH_H3_VAE_PAGEABLE_SWAP=0 keeps stock moves there)."""
+ off = ("0", "off", "false", "no")
+ if str(os.environ.get(H3_VAE_PINNED_SWAP_ENV, "")).strip().lower() in off:
return False
if module is None or getattr(module, "_unsloth_pinned_swap", None) is not None:
return False
@@ -908,22 +907,26 @@ def install_pinned_swap(
from .video_minimax_h3_te import _module_payload_bytes, h3_te_pin_allowed, pin_module_in_place
- if not torch.cuda.is_available() or not h3_te_pin_allowed(_module_payload_bytes(module)):
+ if not torch.cuda.is_available():
return False
tensors = list(module.parameters()) + list(module.buffers())
if not tensors or any(t.device.type != "cpu" for t in tensors):
return False
if any(type(t.data) is not torch.Tensor for t in tensors):
return False
- try:
- pin_module_in_place(module, repoint = True)
- except Exception as exc: # noqa: BLE001 -- stock moves
- if logger is not None:
- logger.warning("video.h3_pinned_swap: %s not pinned (%s)", label, exc)
+ pinned = h3_te_pin_allowed(_module_payload_bytes(module))
+ if not pinned and str(os.environ.get(H3_VAE_PAGEABLE_SWAP_ENV, "")).strip().lower() in off:
return False
+ if pinned:
+ try:
+ pin_module_in_place(module, repoint = True)
+ except Exception as exc: # noqa: BLE001 -- stock moves
+ if logger is not None:
+ logger.warning("video.h3_pinned_swap: %s not pinned (%s)", label, exc)
+ return False
host: dict[int, Any] = {}
for tensor in list(module.parameters()) + list(module.buffers()):
- if tensor.device.type == "cpu" and tensor.is_pinned():
+ if tensor.device.type == "cpu" and (tensor.is_pinned() or not pinned):
host[id(tensor)] = tensor.data
stock_to = module.to
@@ -953,8 +956,9 @@ def install_pinned_swap(
module._unsloth_pinned_swap = host
if logger is not None:
logger.info(
- "video.h3_pinned_swap: %s moves by re-pointing at a pinned host copy (%.2f GB)",
+ "video.h3_pinned_swap: %s moves by re-pointing at a %s host copy (%.2f GB)",
label,
+ "pinned" if pinned else "pageable",
sum(t.numel() * t.element_size() for t in host.values()) / 1e9,
)
return True
diff --git a/studio/backend/tests/test_h3_stream_compile_and_top_group.py b/studio/backend/tests/test_h3_stream_compile_and_top_group.py
index e5d936041d9..984f775a993 100644
--- a/studio/backend/tests/test_h3_stream_compile_and_top_group.py
+++ b/studio/backend/tests/test_h3_stream_compile_and_top_group.py
@@ -296,3 +296,59 @@ def test_the_pinned_swap_only_installs_from_the_parked_state(monkeypatch):
_swap_ready(monkeypatch)
vae = torch.nn.Linear(4, 4).to("meta")
assert not install_pinned_swap(vae)
+
+
+def test_a_vae_that_cannot_pin_still_parks_by_repointing_at_its_pageable_copy(monkeypatch):
+ import core.inference.video_minimax_h3_te as te
+ from core.inference.video_minimax_h3_residency import install_pinned_swap
+
+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
+ monkeypatch.setattr(te, "h3_te_pin_allowed", lambda *a: False)
+ monkeypatch.setattr(
+ te, "pin_module_in_place", lambda *a, **k: pytest.fail("pinned without a pin budget")
+ )
+ vae = torch.nn.Sequential(torch.nn.Conv2d(3, 4, 3), torch.nn.GroupNorm(2, 4))
+ expect = {k: v.clone() for k, v in vae.state_dict().items()}
+ host_ptr = vae[0].weight.data_ptr()
+ assert install_pinned_swap(vae)
+ assert not vae[0].weight.is_pinned()
+ vae[0].weight.data = vae[0].weight.data.clone()
+ vae.to("cpu")
+ assert vae[0].weight.data_ptr() == host_ptr, "the CPU move copied instead of re-pointing"
+ for k, v in vae.state_dict().items():
+ assert torch.equal(v, expect[k])
+
+
+def test_the_pageable_swap_honours_its_kill_switch(monkeypatch):
+ import core.inference.video_minimax_h3_te as te
+ from core.inference.video_minimax_h3_residency import install_pinned_swap
+
+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
+ monkeypatch.setattr(te, "h3_te_pin_allowed", lambda *a: False)
+ monkeypatch.setenv("UNSLOTH_H3_VAE_PAGEABLE_SWAP", "0")
+ vae = torch.nn.Linear(4, 4)
+ assert not install_pinned_swap(vae)
+ assert "to" not in vae.__dict__
+
+
[email protected](not torch.cuda.is_available(), reason = "needs a CUDA device")
+def test_a_pageable_swap_round_trip_on_cuda(monkeypatch):
+ import core.inference.video_minimax_h3_te as te
+ from core.inference.video_minimax_h3_residency import install_pinned_swap
+
+ monkeypatch.setattr(te, "h3_te_pin_allowed", lambda *a: False)
+ vae = torch.nn.Sequential(torch.nn.Conv2d(3, 4, 3), torch.nn.GroupNorm(2, 4))
+ expect = {k: v.clone() for k, v in vae.state_dict().items()}
+ host_ptr = vae[0].weight.data_ptr()
+ assert install_pinned_swap(vae)
+ x = torch.randn(1, 3, 8, 8)
+ ref = vae(x)
+ for _ in range(2):
+ vae.to("cuda")
+ assert all(t.is_cuda for t in vae.parameters())
+ y = vae(x.cuda()).cpu()
+ vae.to("cpu")
+ assert vae[0].weight.data_ptr() == host_ptr
+ for k, v in vae.state_dict().items():
+ assert torch.equal(v, expect[k])
+ assert torch.allclose(y, ref, atol = 1e-5)