Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio diffusion: keep the streamed text encoder in host RAM on the small-host route when it fits (#16304)

Daniel Hanb0adf541d36f
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index 19686466634..183266390dd 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -12236,10 +12236,59 @@ class DiffusionBackend:
)
if load_token is not None:
self._raise_if_load_cancelled(load_token)
+ if not resident:
+ self._small_host_encoders_resident(pipe, decision, info, logger, load_token)
mark(pipe, info)
logger.info("diffusion.small_host: %s", info["components"])
return self._small_host_plan(pipe, plan, fam, logger)
+ def _small_host_encoders_resident(
+ self,
+ pipe: Any,
+ decision: Any,
+ info: dict,
+ logger: Any,
+ load_token: Any = None,
+ ) -> None:
+ """Copy routed encoders' mmapped weights into host RAM if it fits; must run after the DiT is placed."""
+ from .diffusion_small_host import (
+ decide_encoder_resident,
+ materialize_streamed_encoder_,
+ streamed_encoder_mib,
+ )
+
+ cancel = None if load_token is None else (lambda: self._raise_if_load_cancelled(load_token))
+ for name in decision.storage_dtypes:
+ if not name.startswith("text_encoder"):
+ continue
+ module = getattr(pipe, name, None)
+ if module is None:
+ continue
+ try:
+ need = streamed_encoder_mib(module)
+ except Exception: # noqa: BLE001 - an unsizeable encoder stays memory-mapped
+ continue
+ if need <= 0:
+ continue
+ total, available = host_ram_mib()
+ ok, why = decide_encoder_resident(
+ need,
+ host_total_mib = total,
+ host_available_mib = available,
+ host_commit_mib = host_commit_mib(),
+ int8_spill = bool(getattr(decision, "int8_spill", False)),
+ )
+ logger.info(
+ "diffusion.small_host: %s weights %s in host RAM (%s)",
+ name,
+ "copied" if ok else "stay memory-mapped",
+ why,
+ )
+ if not ok:
+ continue
+ mib = materialize_streamed_encoder_(module, cancel)
+ info["components"][name] = f"anonymous host RAM, layerwise cast ({mib} MiB)"
+
def _small_host_plan(self, pipe: Any, plan: Any, fam: Any, logger: Any) -> Any:
"""Group offload, routed encoders streamed; the denoiser keeps what fits after the runtime reserve resident."""
info = small_host_engaged_on(pipe)
diff --git a/studio/backend/core/inference/diffusion_small_host.py b/studio/backend/core/inference/diffusion_small_host.py
index 5025bef1410..a618b4354e3 100644
--- a/studio/backend/core/inference/diffusion_small_host.py
+++ b/studio/backend/core/inference/diffusion_small_host.py
@@ -862,11 +862,113 @@ def prepare_streamed_encoder_(module: Any, compute_dtype: Any) -> int:
skip_modules_pattern = None,
skip_modules_classes = None,
)
+ sub._unsloth_mmap_streamed = True
continue
_convert(sub, want)
return streamed // _MIB
+# Small hosts drop mmapped encoder pages between prompts (FLUX.1 T5 on a 31 GB T4: ~7 GB re-read, 18-24 s/prompt).
+ENCODER_RESIDENT_ENV = "UNSLOTH_DIFFUSION_SMALL_HOST_TE_RESIDENT"
+# Host RAM still taken after the route: pinned DiT staging (~2 GB on FLUX.1) + encoder prefetch ring.
+_ENCODER_RESIDENT_RUNTIME_MIB = 2560
+
+
+def _mmap_streamed_tensors(module: Any) -> list:
+ """(owner, name, tensor) of every CPU parameter / buffer left in storage dtype by ``prepare_streamed_encoder_``."""
+ out: list = []
+ seen: set = set()
+ for sub in module.modules():
+ if not getattr(sub, "_unsloth_mmap_streamed", False):
+ continue
+ for kind, items in (
+ ("p", sub.named_parameters(recurse = False)),
+ ("b", sub.named_buffers(recurse = False)),
+ ):
+ for name, t in items:
+ if t is None or t.device.type != "cpu" or t.numel() == 0:
+ continue
+ key = t.untyped_storage().data_ptr()
+ if key in seen:
+ continue
+ seen.add(key)
+ out.append((sub, kind, name, t))
+ return out
+
+
+def streamed_encoder_mib(module: Any) -> int:
+ total = sum(int(t.numel()) * int(t.element_size()) for *_x, t in _mmap_streamed_tensors(module))
+ return -(-total // _MIB)
+
+
+def encoder_resident_mode() -> Optional[bool]:
+ value = _env(ENCODER_RESIDENT_ENV)
+ if value in ("0", "off", "false", "no"):
+ return False
+ if value in ("1", "on", "true", "yes"):
+ return True
+ return None
+
+
+def decide_encoder_resident(
+ streamed_mib: int,
+ *,
+ host_total_mib: Optional[int],
+ host_available_mib: Optional[int],
+ host_commit_mib: Optional[int] = None,
+ int8_spill: bool = False,
+) -> tuple[bool, str]:
+ """``host_available_mib`` is read after placement, so it already excludes the int8 DiT."""
+ mode = encoder_resident_mode()
+ if streamed_mib <= 0:
+ return False, "no memory-mapped encoder weights"
+ if mode is False:
+ return False, f"{ENCODER_RESIDENT_ENV}=0"
+ if mode is True:
+ return True, f"{ENCODER_RESIDENT_ENV}=1"
+ if int8_spill:
+ 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
+ left = int(host_available_mib) - int(streamed_mib)
+ if left < keep:
+ return (
+ False,
+ f"{streamed_mib} MiB would leave {left} MiB of {host_available_mib} MiB available host RAM, "
+ f"under the {keep} MiB the load keeps",
+ )
+ if host_commit_mib is not None and int(host_commit_mib) - int(streamed_mib) < keep:
+ return (
+ False,
+ f"{streamed_mib} MiB would leave {int(host_commit_mib) - int(streamed_mib)} MiB of commit headroom, "
+ f"under the {keep} MiB the load keeps",
+ )
+ return (
+ True,
+ f"{streamed_mib} MiB leaves {left} MiB of {host_available_mib} MiB available host RAM",
+ )
+
+
+def materialize_streamed_encoder_(module: Any, cancel_check: Any = None) -> int:
+ """Must run before group offload records the host tensors. Returns MiB copied."""
+ import torch
+
+ copied = 0
+ with torch.no_grad():
+ for sub, kind, name, t in _mmap_streamed_tensors(module):
+ fresh = torch.empty_like(t, memory_format = torch.contiguous_format)
+ fresh.copy_(t)
+ if kind == "p":
+ getattr(sub, name).data = fresh
+ else:
+ sub._buffers[name] = fresh
+ copied += int(t.numel()) * int(t.element_size())
+ if cancel_check is not None:
+ cancel_check()
+ return -(-copied // _MIB)
+
+
def module_dtype_ok(module: Any, compute_dtype: Any) -> bool:
try:
return getattr(module, "dtype", compute_dtype) == compute_dtype
diff --git a/studio/backend/tests/test_diffusion_small_host_te_resident.py b/studio/backend/tests/test_diffusion_small_host_te_resident.py
new file mode 100644
index 00000000000..fcf8f73f91b
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_small_host_te_resident.py
@@ -0,0 +1,231 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Small-host route: mmapped encoder weights move into host RAM when they fit. CPU only."""
+
+from __future__ import annotations
+
+import sys
+import types
+
+import pytest
+import torch
+
+import core.inference.diffusion_memory as dm
+import core.inference.diffusion_small_host as sh
+
+KAGGLE_TOTAL = 31 * 1024
+# FLUX.1-dev on a Kaggle T4: T5-XXL leaves 6912 MiB memory-mapped; ~13.3 GB is available once the int8 DiT is placed
+T5_STREAMED = 6912
+KAGGLE_AFTER_PLACEMENT = 13300
+
+
[email protected](autouse = True)
+def _clean_env(monkeypatch):
+ for name in (sh.ENCODER_RESIDENT_ENV, sh.SMALL_HOST_ENV, sh.HOST_RAM_LIMIT_ENV):
+ monkeypatch.delenv(name, raising = False)
+
+
+def _decide(**kw):
+ args = dict(host_total_mib = KAGGLE_TOTAL, host_available_mib = KAGGLE_AFTER_PLACEMENT)
+ args.update(kw)
+ return sh.decide_encoder_resident(T5_STREAMED, **args)
+
+
+def test_kaggle_t4_copies_t5_into_host_ram():
+ ok, why = _decide()
+ assert ok, why
+
+
[email protected]("available", [None, 9000, T5_STREAMED + 3072 + 2560 - 1])
+def test_short_host_keeps_encoder_memory_mapped(available):
+ ok, _why = _decide(host_available_mib = available)
+ assert not ok
+
+
+def test_boundary_keeps_reserve_and_runtime():
+ keep = sh.host_reserve_mib(KAGGLE_TOTAL) + sh._ENCODER_RESIDENT_RUNTIME_MIB
+ assert _decide(host_available_mib = T5_STREAMED + keep)[0]
+ assert not _decide(host_available_mib = T5_STREAMED + keep - 1)[0]
+
+
+def test_large_host_reserve_scales_with_total():
+ # 10% of a 128 GB host is kept: 20 GB available after placement covers T5 on a 31 GB host but not here
+ assert _decide(host_total_mib = 31 * 1024, host_available_mib = 20000)[0]
+ assert not _decide(host_total_mib = 160 * 1024, host_available_mib = 20000)[0]
+
+
+def test_spill_and_commit_keep_encoder_memory_mapped():
+ assert not _decide(int8_spill = True)[0]
+ assert not _decide(host_commit_mib = 8000)[0]
+ assert _decide(host_commit_mib = 20000)[0]
+
+
+def test_kill_switch(monkeypatch):
+ monkeypatch.setenv(sh.ENCODER_RESIDENT_ENV, "0")
+ assert not _decide()[0]
+ monkeypatch.setenv(sh.ENCODER_RESIDENT_ENV, "1")
+ assert _decide(host_available_mib = 100)[0]
+ assert not sh.decide_encoder_resident(0, host_total_mib = 1, host_available_mib = 1)[0]
+
+
+class _Block(torch.nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.wi = torch.nn.Linear(64, 128, bias = False)
+ self.wo = torch.nn.Linear(128, 64, bias = False)
+
+ def forward(self, h):
+ return self.wo(torch.relu(self.wi(h)).to(self.wo.weight.dtype))
+
+
+class _Enc(torch.nn.Module):
+ _keep_in_fp32_modules = ["wo"]
+
+ def __init__(self):
+ super().__init__()
+ self.embed = torch.nn.Embedding(100, 64)
+ self.block = _Block()
+ self.proj = torch.nn.Linear(64, 64)
+ self.norm = torch.nn.LayerNorm(64)
+
+ @property
+ def dtype(self):
+ return next(p.dtype for p in self.parameters() if p.is_floating_point())
+
+ def forward(self, ids):
+ return self.norm(self.proj(self.block(self.embed(ids))))
+
+
+def _file_ranges(path) -> list:
+ out = []
+ with open("/proc/self/maps", encoding = "utf-8") as f:
+ for line in f:
+ if line.rstrip().endswith(str(path)):
+ lo, hi = line.split()[0].split("-")
+ out.append((int(lo, 16), int(hi, 16)))
+ return out
+
+
+def _mapped(t, ranges) -> bool:
+ ptr = t.untyped_storage().data_ptr()
+ return any(lo <= ptr < hi for lo, hi in ranges)
+
+
+def _mmap_encoder(tmp_path):
+ from safetensors import safe_open
+ from safetensors.torch import save_file
+
+ torch.manual_seed(0)
+ src = _Enc()
+ state = {k: v.to(torch.bfloat16).contiguous() for k, v in src.state_dict().items()}
+ path = tmp_path / "model.safetensors"
+ save_file(state, str(path))
+ enc = _Enc()
+ with safe_open(str(path), framework = "pt") as f:
+ for k in f.keys():
+ owner, _, pname = k.rpartition(".")
+ sub = enc.get_submodule(owner)
+ setattr(sub, pname, torch.nn.Parameter(f.get_tensor(k), requires_grad = False))
+ return enc, path
+
+
[email protected](not sys.platform.startswith("linux"), reason = "reads /proc/self/maps")
+def test_materialize_moves_streamed_weights_off_the_file(tmp_path):
+ pytest.importorskip("diffusers")
+ enc, path = _mmap_encoder(tmp_path)
+ ranges = _file_ranges(path)
+ if not ranges or not _mapped(enc.proj.weight, ranges):
+ pytest.skip("safetensors did not memory-map this file")
+ ids = torch.randint(0, 100, (2, 7))
+ sh.prepare_streamed_encoder_(enc, torch.float32)
+ # a forward outside group offload re-stores layerwise-cast weights, so the reference is a second copy
+ (tmp_path / "ref").mkdir()
+ ref, _ref_path = _mmap_encoder(tmp_path / "ref")
+ sh.prepare_streamed_encoder_(ref, torch.float32)
+ with torch.no_grad():
+ before = ref(ids)
+ assert _mapped(enc.proj.weight, _file_ranges(path))
+ mib = sh.materialize_streamed_encoder_(enc)
+ assert mib == sh.streamed_encoder_mib(enc) and mib > 0
+ for sub in enc.modules():
+ if getattr(sub, "_unsloth_mmap_streamed", False):
+ assert sub.weight.dtype == torch.bfloat16
+ assert not _mapped(sub.weight, _file_ranges(path))
+ with torch.no_grad():
+ assert torch.equal(enc(ids), before)
+
+
+def test_materialize_keeps_ties():
+ pytest.importorskip("diffusers")
+ enc = _Enc()
+ for p in enc.parameters():
+ p.data = p.data.to(torch.bfloat16)
+ sh.prepare_streamed_encoder_(enc, torch.float32)
+ tied = torch.nn.Linear(64, 128, bias = False)
+ tied.weight = enc.block.wi.weight
+ tied._unsloth_mmap_streamed = True
+ enc.tied = tied
+ sh.materialize_streamed_encoder_(enc)
+ assert enc.tied.weight is enc.block.wi.weight
+
+
+def _route(monkeypatch, tmp_path, available):
+ from core.inference import diffusion as d
+ from core.inference.diffusion import DiffusionBackend
+
+ enc, _path = _mmap_encoder(tmp_path)
+ monkeypatch.setattr(d, "host_ram_mib", lambda: (KAGGLE_TOTAL, available))
+ monkeypatch.setattr(d, "host_commit_mib", lambda: None)
+ monkeypatch.setattr(
+ dm, "_loaded_component_mib", lambda pipe: {"text_encoder_2": (1, "text_encoder")}
+ )
+ pipe = types.SimpleNamespace(text_encoder_2 = enc)
+ decision = sh.SmallHostDecision(True, "test", storage_dtypes = {"text_encoder_2": "bfloat16"})
+ lines: list = []
+ log = types.SimpleNamespace(
+ info = lambda *a, **k: lines.append(a[0] % a[1:] if len(a) > 1 else a[0])
+ )
+ plan = dm.MemoryPlan(
+ requested_mode = dm.MEMORY_MODE_AUTO,
+ offload_policy = dm.OFFLOAD_GROUP,
+ vae_tiling = False,
+ vae_slicing = False,
+ device_memory = dm.DeviceMemory("cuda", "cuda", "discrete_vram", 14647, 15095),
+ estimates = {"safe_device_budget_mib": 12599},
+ )
+ target = types.SimpleNamespace(torch_device = torch.device("cpu"))
+ backend = types.SimpleNamespace(
+ _raise_if_load_cancelled = lambda token: None,
+ _small_host_plan = lambda pipe, plan, fam, logger: plan,
+ )
+ backend._small_host_encoders_resident = types.MethodType(
+ DiffusionBackend._small_host_encoders_resident, backend
+ )
+ DiffusionBackend._apply_small_host_route(
+ backend, pipe, plan, decision, target, torch.float32, None, log, None
+ )
+ return pipe, lines
+
+
+def test_route_copies_encoder_when_host_covers_it(monkeypatch, tmp_path):
+ pytest.importorskip("diffusers")
+ pipe, lines = _route(monkeypatch, tmp_path, 1 << 20)
+ info = sh.engaged_on(pipe)
+ assert info["components"]["text_encoder_2"].startswith("anonymous host RAM")
+ assert any("copied in host RAM" in line for line in lines)
+
+
+def test_route_keeps_encoder_mapped_on_a_short_host(monkeypatch, tmp_path):
+ pytest.importorskip("diffusers")
+ pipe, lines = _route(monkeypatch, tmp_path, 4000)
+ info = sh.engaged_on(pipe)
+ assert info["components"]["text_encoder_2"].startswith("memory-mapped")
+ assert any("stay memory-mapped" in line for line in lines)
+
+
+def test_route_kill_switch(monkeypatch, tmp_path):
+ pytest.importorskip("diffusers")
+ monkeypatch.setenv(sh.ENCODER_RESIDENT_ENV, "0")
+ pipe, _lines = _route(monkeypatch, tmp_path, 1 << 20)
+ assert sh.engaged_on(pipe)["components"]["text_encoder_2"].startswith("memory-mapped")