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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio video: keep the share of MiniMax-H3's streamed conditioner that fits in host RAM (#16325)
Daniel Han2f93b7303c1c
diff --git a/studio/backend/core/inference/diffusion_small_host.py b/studio/backend/core/inference/diffusion_small_host.pyindex a618b4354e3..73b909da39e 100644--- a/studio/backend/core/inference/diffusion_small_host.py+++ b/studio/backend/core/inference/diffusion_small_host.py@@ -910,6 +910,41 @@ def encoder_resident_mode() -> Optional[bool]:return None+def encoder_resident_keep_mib(host_total_mib: Optional[int]) -> int:+ return host_reserve_mib(host_total_mib) + _ENCODER_RESIDENT_RUNTIME_MIB+++def encoder_resident_share_mib(+ streamed_mib: int,+ *,+ host_total_mib: Optional[int],+ host_available_mib: Optional[int],+ host_commit_mib: Optional[int] = None,+ min_share_mib: int = 0,+) -> tuple[int, str]:+ """MiB of ``streamed_mib`` that fit under ``decide_encoder_resident``'s reserve; ``host_available_mib`` read after placement."""+ if streamed_mib <= 0:+ return 0, "no memory-mapped encoder weights"+ if host_available_mib is None:+ return 0, "host RAM unreadable"+ keep = encoder_resident_keep_mib(host_total_mib)+ room = int(host_available_mib) - keep+ if host_commit_mib is not None:+ room = min(room, int(host_commit_mib) - keep)+ share = max(0, min(int(streamed_mib), room))+ if share <= 0 or share < int(min_share_mib):+ return (+ 0,+ f"{host_available_mib} MiB available host RAM leaves {max(0, room)} MiB above the "+ f"{keep} MiB the load keeps",+ )+ return (+ share,+ f"{share} of {streamed_mib} MiB leaves {int(host_available_mib) - share} MiB of "+ f"{host_available_mib} MiB available host RAM (keeps {keep} MiB)",+ )++def decide_encoder_resident(streamed_mib: int,*,@@ -930,7 +965,7 @@ def decide_encoder_resident(return False, "the int8 denoiser already spills to disk"if host_available_mib is None:return False, "host RAM unreadable"- keep = host_reserve_mib(host_total_mib) + _ENCODER_RESIDENT_RUNTIME_MIB+ keep = encoder_resident_keep_mib(host_total_mib)left = int(host_available_mib) - int(streamed_mib)if left < keep:return (diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.pyindex a44d3802893..2af396d18dc 100644--- a/studio/backend/core/inference/video.py+++ b/studio/backend/core/inference/video.py@@ -11010,6 +11010,7 @@ class VideoBackend:denoiser_streamed: Optional[str] = Nonedenoiser_single_host_copy = Falsete_streamed: Optional[str] = None+ block_pinner = Noneresidency = None# Token fence before placement, or a superseded worker loads 66.3 GB onto another load's GPU.if _load_token is not None and _load_token != self._load_token:@@ -11495,6 +11496,13 @@ class VideoBackend:)except Exception as exc: # noqa: BLE001 -- stock moveslogger.warning("video.h3_pinned_swap: %s: %s", vae_name, exc)+ if te_streamed == "stream_lazy":+ # Last host copy, so available RAM is final.+ from .video_minimax_h3_te import h3_te_resident_enabled, keep_h3_te_resident+ if h3_te_resident_enabled():+ if block_pinner is not None:+ block_pinner.join()+ keep_h3_te_resident(getattr(pipe, "text_encoder", None), logger = logger)resolved = build_resolved_record({"family_override": _family_override_resolved(family_override, fam),diff --git a/studio/backend/core/inference/video_minimax_h3_te.py b/studio/backend/core/inference/video_minimax_h3_te.pyindex f2a364e7688..bcaa59f3ca7 100644--- a/studio/backend/core/inference/video_minimax_h3_te.py+++ b/studio/backend/core/inference/video_minimax_h3_te.py@@ -645,3 +645,143 @@ def stream_h3_text_encoder("pinned" if pin else "pageable",)return mode+++# A mapped conditioner larger than the page cache is re-read from disk every prompt (L4: +78-84 s); anon pages are not.+H3_TE_RESIDENT_ENV = "UNSLOTH_H3_TE_RESIDENT"+_H3_TE_RESIDENT_MIN_MIB = 1024+_H3_TE_COPY_THREADS = 4+++def h3_te_resident_enabled() -> bool:+ """``UNSLOTH_H3_TE_RESIDENT=0`` leaves the streamed conditioner on its memory mapping."""+ import os+ return str(os.environ.get(H3_TE_RESIDENT_ENV, "")).strip().lower() not in (+ "0",+ "off",+ "false",+ "no",+ )+++def _streamed_host_tensors(target: Any, groups: Optional[list] = None) -> list:+ """(group, tensor, host) pageable CPU tensors, language model first: vision is only read for image prompts."""+ import torch++ if groups is None:+ from .diffusion_memory import _offload_groups+ groups = _offload_groups(target)+ names = {id(m): n for n, m in target.named_modules()}++ def _late(group: Any) -> bool:+ mods = list(getattr(group, "modules", None) or ())+ leader = getattr(group, "onload_leader", None)+ name = names.get(id(mods[0] if mods else leader), "")+ return "visual" in name.split(".")++ out: list = []+ seen: set = set()+ for group in sorted(groups, key = _late):+ cpu = getattr(group, "cpu_param_dict", None) or {}+ tensors = (+ [t for m in getattr(group, "modules", None) or () for t in m.parameters()]+ + [t for m in getattr(group, "modules", None) or () for t in m.buffers()]+ + list(getattr(group, "parameters", None) or ())+ + list(getattr(group, "buffers", None) or ())+ )+ for tensor in tensors:+ host = cpu.get(tensor, tensor)+ if id(tensor) in seen or host is None or host.device.type != "cpu":+ continue+ seen.add(id(tensor))+ if type(host) is not torch.Tensor and type(host) is not torch.nn.Parameter:+ continue # tensor subclasses keep their own storage layout+ if host.numel() == 0 or host.is_pinned():+ continue+ out.append((group, tensor, host))+ return out+++def keep_h3_te_resident(+ text_encoder: Any,+ *,+ logger: Any = None,+ groups: Optional[list] = None,+ host_ram: Any = None,+) -> int:+ """Copy the share of the streamed conditioner that fits beside the reserve into anonymous RAM; run after every+ load-time host allocation. ``host_ram``: ``(total, available, commit)`` MiB. Returns MiB copied. Never raises."""+ try:+ target = getattr(text_encoder, "model", None)+ if target is None or not h3_te_resident_enabled():+ return 0+ entries = _streamed_host_tensors(target, groups)+ need = -(-sum(h.numel() * h.element_size() for _g, _t, h in entries) // (1 << 20))+ if need <= 0:+ return 0+ from .diffusion_small_host import (+ encoder_resident_share_mib,+ host_commit_mib,+ host_ram_mib,+ )++ if host_ram is None:+ total, available = host_ram_mib()+ commit = host_commit_mib()+ else:+ total, available, commit = host_ram+ share, why = encoder_resident_share_mib(+ need,+ host_total_mib = total,+ host_available_mib = available,+ host_commit_mib = commit,+ min_share_mib = min(need, _H3_TE_RESIDENT_MIN_MIB),+ )+ if share <= 0:+ if logger is not None:+ logger.info("video.h3_te_resident: the conditioner stays memory-mapped (%s)", why)+ return 0+ budget = share << 20+ chosen = []+ for entry in entries:+ nbytes = entry[2].numel() * entry[2].element_size()+ if nbytes <= budget:+ chosen.append(entry)+ budget -= nbytes+ copied = _copy_host_tensors_(chosen)+ mib = copied >> 20+ if logger is not None:+ logger.info(+ "video.h3_te_resident: %d of %d MiB of the streamed conditioner copied into host RAM (%s)",+ mib,+ need,+ why,+ )+ return mib+ except Exception as exc: # noqa: BLE001 -- the mapping still works+ if logger is not None:+ logger.warning("video.h3_te_resident: kept the mapping: %s", exc)+ return 0+++def _copy_host_tensors_(entries: list) -> int:+ import torch+ from concurrent.futures import ThreadPoolExecutor++ def _fresh(host: Any) -> Any:+ out = torch.empty_like(host, memory_format = torch.contiguous_format)+ out.copy_(host) # releases the GIL: parallel page faults keep the disk queue full+ return out++ with torch.no_grad(), ThreadPoolExecutor(_H3_TE_COPY_THREADS) as pool:+ fresh = list(pool.map(lambda e: _fresh(e[2]), entries))+ copied = 0+ with torch.no_grad():+ for (group, tensor, host), new in zip(entries, fresh):+ cpu = getattr(group, "cpu_param_dict", None)+ if cpu is not None and tensor in cpu:+ cpu[tensor] = new+ if tensor.device.type == "cpu" and tensor.data_ptr() == host.data_ptr():+ tensor.data = new+ copied += new.numel() * new.element_size()+ return copieddiff --git a/studio/backend/tests/test_video_h3_te_resident.py b/studio/backend/tests/test_video_h3_te_resident.pynew file mode 100644index 00000000000..d94143b7b64--- /dev/null+++ b/studio/backend/tests/test_video_h3_te_resident.py@@ -0,0 +1,261 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""MiniMax-H3: the pageable streamed conditioner keeps the share that fits in anonymous host RAM. CPU only."""++from __future__ import annotations++import ast+import sys+from pathlib import Path+from types import SimpleNamespace++import pytest+import torch++import core.inference.diffusion_small_host as sh+import core.inference.video_minimax_h3_te as te++# Colab L4 high-RAM: 53 GiB host, 20.2 GiB available after load, 23.5 GiB streamed conditioner.+L4_TOTAL = 53 * 1024+L4_AFTER_LOAD = 20664+H3_TE_STREAMED = 24064++[email protected](autouse = True)+def _clean_env(monkeypatch):+ for name in (te.H3_TE_RESIDENT_ENV, sh.ENCODER_RESIDENT_ENV, sh.HOST_RAM_LIMIT_ENV):+ monkeypatch.delenv(name, raising = False)+++def _keep(total = L4_TOTAL):+ return sh.encoder_resident_keep_mib(total)+++def test_keep_matches_the_small_host_reserve():+ assert _keep() == sh.host_reserve_mib(L4_TOTAL) + sh._ENCODER_RESIDENT_RUNTIME_MIB+ # decide_encoder_resident refuses exactly where the share stops being the whole encoder+ need = 6912+ edge = need + _keep(31 * 1024)+ for available in (edge - 1, edge, edge + 1):+ ok, _ = sh.decide_encoder_resident(+ need, host_total_mib = 31 * 1024, host_available_mib = available+ )+ share, _ = sh.encoder_resident_share_mib(+ need, host_total_mib = 31 * 1024, host_available_mib = available+ )+ assert ok == (share == need)+++def test_l4_copies_the_share_beside_the_reserve():+ share, why = sh.encoder_resident_share_mib(+ H3_TE_STREAMED, host_total_mib = L4_TOTAL, host_available_mib = L4_AFTER_LOAD+ )+ assert share == L4_AFTER_LOAD - _keep(), why+ assert 0 < share < H3_TE_STREAMED+++def test_whole_encoder_when_it_fits():+ share, _ = sh.encoder_resident_share_mib(+ H3_TE_STREAMED, host_total_mib = 177 * 1024, host_available_mib = 120_000+ )+ assert share == H3_TE_STREAMED++[email protected]("available", [None, 0, 5000, "keep"])+def test_no_room_copies_nothing(available):+ if available == "keep":+ available = _keep()+ share, why = sh.encoder_resident_share_mib(+ H3_TE_STREAMED, host_total_mib = L4_TOTAL, host_available_mib = available+ )+ assert share == 0, why+++def test_small_share_is_not_worth_it():+ share, _ = sh.encoder_resident_share_mib(+ H3_TE_STREAMED,+ host_total_mib = L4_TOTAL,+ host_available_mib = _keep() + 500,+ min_share_mib = 1024,+ )+ assert share == 0+++def test_commit_headroom_caps_the_share():+ share, _ = sh.encoder_resident_share_mib(+ H3_TE_STREAMED,+ host_total_mib = L4_TOTAL,+ host_available_mib = 40_000,+ host_commit_mib = _keep() + 3000,+ )+ assert share == 3000+++def _mmap_tensors(tmp_path, specs):+ from safetensors import safe_open+ from safetensors.torch import save_file++ gen = torch.Generator().manual_seed(0)+ path = tmp_path / "te.safetensors"+ save_file(+ {k: torch.randint(-127, 127, shape, generator = gen, dtype = torch.int8) for k, shape in specs},+ str(path),+ )+ with safe_open(str(path), framework = "pt", device = "cpu") as handle:+ return path, {k: handle.get_tensor(k) for k, _ in specs}+++def _file_mapped(ptr: int, path: Path) -> bool:+ for line in Path("/proc/self/maps").read_text().splitlines():+ parts = line.split()+ if len(parts) >= 6 and parts[-1] == str(path):+ lo, hi = (int(x, 16) for x in parts[0].split("-"))+ if lo <= ptr < hi:+ return True+ return False+++class _Leaf(torch.nn.Module):+ def __init__(self, weight):+ super().__init__()+ self.register_buffer("weight", weight)+++def _fake_encoder(tensors):+ """text_encoder.model with language and vision leaves, one stream-style offload group per leaf."""+ model = torch.nn.Module()+ model.language_model = torch.nn.ModuleList([_Leaf(tensors["l0"]), _Leaf(tensors["l1"])])+ model.visual = torch.nn.ModuleList([_Leaf(tensors["v0"])])+ groups = []+ for leaf in (model.visual[0], model.language_model[0], model.language_model[1]):+ groups.append(+ SimpleNamespace(+ modules = [leaf],+ parameters = [],+ buffers = [],+ onload_leader = leaf,+ cpu_param_dict = {leaf.weight: leaf.weight.data},+ )+ )+ return SimpleNamespace(model = model), groups+++SPECS = [("l0", (1024, 1024)), ("l1", (1024, 1024)), ("v0", (1024, 1024))]+LEAF_MIB = 1++[email protected](not sys.platform.startswith("linux"), reason = "reads /proc/self/maps")+def test_copy_leaves_the_mapping_and_keeps_values(tmp_path, monkeypatch):+ monkeypatch.setattr(te, "_H3_TE_RESIDENT_MIN_MIB", 1)+ path, tensors = _mmap_tensors(tmp_path, SPECS)+ ref = {k: v.clone() for k, v in tensors.items()}+ assert all(_file_mapped(t.data_ptr(), path) for t in tensors.values())+ encoder, groups = _fake_encoder(tensors)+ mib = te.keep_h3_te_resident(encoder, groups = groups, host_ram = (L4_TOTAL, 10**6, None))+ assert mib == 3 * LEAF_MIB+ for group in groups:+ ((tensor, host),) = group.cpu_param_dict.items()+ assert host is not None and not _file_mapped(host.data_ptr(), path)+ assert tensor.data_ptr() == host.data_ptr() # the offloaded buffer reads the copy too+ m = encoder.model+ for name, leaf in (+ ("l0", m.language_model[0]),+ ("l1", m.language_model[1]),+ ("v0", m.visual[0]),+ ):+ assert torch.equal(leaf.weight, ref[name])+++def test_share_fills_language_layers_before_the_vision_tower(tmp_path, monkeypatch):+ monkeypatch.setattr(te, "_H3_TE_RESIDENT_MIN_MIB", 1)+ _path, tensors = _mmap_tensors(tmp_path, SPECS)+ encoder, groups = _fake_encoder(tensors)+ before = {id(g): next(iter(g.cpu_param_dict.values())).data_ptr() for g in groups}+ # room for two of the three 1 MiB leaves above the reserve+ mib = te.keep_h3_te_resident(encoder, groups = groups, host_ram = (L4_TOTAL, _keep() + 2, None))+ assert mib == 2 * LEAF_MIB+ moved = [next(iter(g.cpu_param_dict.values())).data_ptr() != before[id(g)] for g in groups]+ assert moved == [False, True, True] # visual group first in the list, kept on the mapping+++def test_kill_switch_changes_nothing(tmp_path, monkeypatch):+ monkeypatch.setenv(te.H3_TE_RESIDENT_ENV, "0")+ _path, tensors = _mmap_tensors(tmp_path, SPECS)+ encoder, groups = _fake_encoder(tensors)+ before = [next(iter(g.cpu_param_dict.values())).data_ptr() for g in groups]+ assert te.keep_h3_te_resident(encoder, groups = groups, host_ram = (L4_TOTAL, 10**6, None)) == 0+ assert [next(iter(g.cpu_param_dict.values())).data_ptr() for g in groups] == before+++def test_no_room_changes_nothing(tmp_path):+ _path, tensors = _mmap_tensors(tmp_path, SPECS)+ encoder, groups = _fake_encoder(tensors)+ before = [next(iter(g.cpu_param_dict.values())).data_ptr() for g in groups]+ assert te.keep_h3_te_resident(encoder, groups = groups, host_ram = (L4_TOTAL, 5000, None)) == 0+ assert [next(iter(g.cpu_param_dict.values())).data_ptr() for g in groups] == before+++def test_unreadable_host_ram_and_odd_input_never_raise():+ assert te.keep_h3_te_resident(None) == 0+ assert (+ te.keep_h3_te_resident(+ SimpleNamespace(model = torch.nn.Linear(2, 2)), host_ram = (None, None, None)+ )+ == 0+ )+ assert te.keep_h3_te_resident(SimpleNamespace(model = object()), groups = [object()]) == 0+++def test_pinned_and_subclass_hosts_are_skipped():+ class _Sub(torch.Tensor):+ pass++ leaf = _Leaf(torch.zeros(4))+ sub = torch.zeros(4).as_subclass(_Sub)+ group = SimpleNamespace(+ modules = [leaf], parameters = [], buffers = [], cpu_param_dict = {leaf.weight: sub}+ )+ model = torch.nn.Module()+ model.leaf = leaf+ assert te._streamed_host_tensors(model, [group]) == []+++def test_real_group_offload_forward_unchanged(tmp_path, monkeypatch):+ pytest.importorskip("diffusers")+ from diffusers.hooks import apply_group_offloading+ from safetensors.torch import load_file, save_file++ monkeypatch.setattr(te, "_H3_TE_RESIDENT_MIN_MIB", 0)+ torch.manual_seed(0)+ model = torch.nn.Sequential(torch.nn.Linear(64, 256), torch.nn.GELU(), torch.nn.Linear(256, 64))+ path = tmp_path / "m.safetensors"+ save_file(model.state_dict(), str(path))+ model.load_state_dict(load_file(str(path)), assign = True)+ x = torch.randn(3, 64)+ ref = model(x)+ apply_group_offloading(+ model,+ onload_device = torch.device("cpu"),+ offload_device = torch.device("cpu"),+ offload_type = "leaf_level",+ )+ mib = te.keep_h3_te_resident(SimpleNamespace(model = model), host_ram = (L4_TOTAL, 10**6, None))+ assert mib >= 0+ assert torch.equal(model(x), ref)+ if sys.platform.startswith("linux"):+ assert not any(_file_mapped(p.data_ptr(), path) for p in model.parameters())+++def test_load_copies_only_for_the_pageable_stream_after_the_vae_copies():+ src = (Path(te.__file__).parent / "video.py").read_text(encoding = "utf-8")+ tree = ast.parse(src)+ guards = [+ node+ for node in ast.walk(tree)+ if isinstance(node, ast.If)+ and "keep_h3_te_resident" in ast.unparse(node)+ and ast.unparse(node.test) == "te_streamed == 'stream_lazy'"+ ]+ assert len(guards) == 1+ assert src.index("keep_h3_te_resident(") > src.index("install_pinned_swap(")