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: stop image loads exhausting Windows commit on Intel Arc iGPUs (#17378)
Daniel Han808917945633
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.pyindex 8eca55c060b..700cf1d7c4f 100644--- a/studio/backend/core/inference/diffusion.py+++ b/studio/backend/core/inference/diffusion.py@@ -4218,11 +4218,14 @@ def _image_cast_cached(cands: list, variant: Optional[str]) -> list:def _converted_shards_pread(dtype: Any, small_host: Any) -> Any:"""Windows: transformers components converted to ``dtype`` read through ``pread`` (no copy-on-write commit for the- stored bytes, #14513); components the small-host route loads at their stored dtype stay memory-mapped."""- from .safetensors_pread import pread_safetensors+ stored bytes, #14513); components the small-host route loads at their stored dtype stay memory-mapped. An Intel+ iGPU reads every shard through ``pread`` (#17137)."""+ from .host_pinning import xpu_is_integrated+ from .safetensors_pread import pread_safetensors, pread_wantedkeep = tuple(small_host.storage_dtypes) if small_host is not None and small_host.engaged else ()- return pread_safetensors(convert_to = dtype, keep = keep)+ every = pread_wanted() and xpu_is_integrated()+ return pread_safetensors(convert_to = dtype, keep = keep, every_shard = every)class DiffusionBackend:diff --git a/studio/backend/core/inference/host_pinning.py b/studio/backend/core/inference/host_pinning.pyindex 891952e593c..d25de52ae22 100644--- a/studio/backend/core/inference/host_pinning.py+++ b/studio/backend/core/inference/host_pinning.py@@ -57,6 +57,22 @@ def device_is_integrated(device: Any = None) -> bool:return False+def xpu_is_integrated() -> bool:+ """Whether the current XPU is a Core Ultra Arc iGPU, by PCI id. Never raises."""+ try:+ import torch++ xpu = getattr(torch, "xpu", None)+ if xpu is None or not xpu.is_available():+ return False+ device_id = getattr(xpu.get_device_properties(), "device_id", None)+ from utils.hardware.hardware import _INTEL_XPU_IGPU_PCI_IDS++ return isinstance(device_id, int) and device_id in _INTEL_XPU_IGPU_PCI_IDS+ except Exception: # noqa: BLE001 - unclassifiable: keep the discrete path+ return False++def host_pinning_allowed(device: Any = None) -> bool:return not device_is_integrated(device)diff --git a/studio/backend/core/inference/safetensors_pread.py b/studio/backend/core/inference/safetensors_pread.pyindex bd9ad9bef01..1acad8eba2d 100644--- a/studio/backend/core/inference/safetensors_pread.py+++ b/studio/backend/core/inference/safetensors_pread.py@@ -17,7 +17,8 @@ PREAD_ENV = "UNSLOTH_SAFETENSORS_PREAD"_lock = threading.Lock()_depth = 0_original: Any = None-# per load thread, so concurrent media loads never share rules: (target dtype or None = fp32-only video rule, kept dirs)+# per load thread, so concurrent media loads never share rules: (target dtype or None = fp32-only video rule, kept dirs,+# every shard)_local = threading.local()@@ -64,13 +65,15 @@ def _backend_for(filename: Any) -> Optional[str]:path = Path(filename)rules = _active_rules()- if any(path.parent.name in keep for _, keep in rules):+ if any(path.parent.name in keep for _, keep, _ in rules):return "keep"stored = {k: v for k, v in _safetensors_bytes_by_dtype(path).items() if k != "kept"}if not stored:return Nonedominant = max(stored.items(), key = lambda kv: kv[1])[0]- for to, _ in rules:+ for to, _, every in rules:+ if every:+ return "pread"if to is None and stored.get("float32", 0) * 2 > sum(stored.values()):return "pread"if to is not None and dominant in ("bfloat16", "float16", "float32") and dominant != to:@@ -111,10 +114,16 @@ def _with_pread(safe_open: Any) -> Any:@contextlib.contextmanager-def pread_safetensors(convert_to: Any = None, keep: Iterable[str] = ()) -> Iterator[bool]:+def pread_safetensors(+ convert_to: Any = None,+ keep: Iterable[str] = (),+ every_shard: bool = False,+) -> Iterator[bool]:"""Route ``transformers.modeling_utils.safe_open`` of fp32 shards, and with ``convert_to`` of every shard stored in- another dtype outside the ``keep`` component dirs, through ``pread``; yields whether it did. Nests; a concurrent- load in another thread only gets the same bytes through another backend."""+ another dtype outside the ``keep`` component dirs, through ``pread``; yields whether it did. ``every_shard`` reads+ same-dtype shards through ``pread`` too: on an integrated GPU the device copy is host commit, and a mapped shard+ stays charged until its last tensor has moved. Nests; a concurrent load in another thread only gets the same bytes+ through another backend."""global _depth, _originalinstalled = Falseprevious_rules = None@@ -136,7 +145,7 @@ def pread_safetensors(convert_to: Any = None, keep: Iterable[str] = ()) -> Iteraif installed:to = None if convert_to is None else str(convert_to).replace("torch.", "")previous_rules = _active_rules()- _local.rules = previous_rules + ((to, frozenset(keep)),)+ _local.rules = previous_rules + ((to, frozenset(keep), bool(every_shard)),)try:yield installedfinally:diff --git a/studio/backend/tests/test_safetensors_pread_igpu.py b/studio/backend/tests/test_safetensors_pread_igpu.pynew file mode 100644index 00000000000..f50836465bc--- /dev/null+++ b/studio/backend/tests/test_safetensors_pread_igpu.py@@ -0,0 +1,228 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""#17137: on Windows an image load onto an Intel Arc iGPU reads same-dtype shards through ``pread`` too, so a mapped+shard is not charged to commit while its tensors are copied into the GPU's share of host RAM. CPU only."""++from __future__ import annotations++import sys+import types++import pytest+import torch++import core.inference.safetensors_pread as sp+from core.inference import host_pinning++transformers = pytest.importorskip("transformers")+import transformers.modeling_utils as modeling_utils # noqa: E402++requires_backend = pytest.mark.skipif(+ not sp._supports_backend(modeling_utils.safe_open),+ reason = "installed safetensors has no backend keyword",+)++[email protected](autouse = True)+def _clean(monkeypatch):+ monkeypatch.delenv(sp.PREAD_ENV, raising = False)+ yield+ assert sp._depth == 0 and sp._original is None and not sp._active_rules()+++def _fake_xpu(+ monkeypatch,+ device_id,+ available = True,+):+ props = types.SimpleNamespace(device_id = device_id, name = "Intel(R) Arc(TM) GPU")+ xpu = types.SimpleNamespace(+ is_available = lambda: available, get_device_properties = lambda *a: props+ )+ monkeypatch.setattr(torch, "xpu", xpu, raising = False)+++ "device_id, available, expected",+ [+ (0x64A0, True, True), # Lunar Lake Arc 130V / 140V (the report)+ (0x7D55, True, True), # Meteor Lake Arc+ (0xB080, True, True), # Panther Lake Arc+ (0xE20B, True, False), # Arc B580 (discrete)+ (0x56A0, True, False), # Arc A770 (discrete)+ (0x0BD5, True, False), # Data Center GPU Max+ (0x64A0, False, False),+ (None, True, False), # torch without device_id+ ],+)+def test_xpu_is_integrated(monkeypatch, device_id, available, expected):+ _fake_xpu(monkeypatch, device_id, available)+ assert host_pinning.xpu_is_integrated() is expected+++def test_xpu_is_integrated_never_raises(monkeypatch):+ def boom(*a):+ raise RuntimeError("no level zero")++ monkeypatch.setattr(+ torch,+ "xpu",+ types.SimpleNamespace(is_available = lambda: True, get_device_properties = boom),+ raising = False,+ )+ assert host_pinning.xpu_is_integrated() is False+++def _record_backends(monkeypatch):+ seen = []+ real = modeling_utils.safe_open++ def spy(*args, **kwargs):+ seen.append(kwargs.get("backend"))+ return real(*args, **kwargs)++ spy.__text_signature__ = real.__text_signature__+ monkeypatch.setattr(modeling_utils, "safe_open", spy)+ return seen+++@requires_backend+ "stored, keep, every, expected",+ [+ (torch.bfloat16, (), False, None), # unchanged off an iGPU: a same-dtype shard stays mapped+ (torch.bfloat16, (), True, "pread"),+ (torch.float16, (), True, "pread"),+ (torch.float32, (), True, "pread"),+ (torch.bfloat16, ("text_encoder",), True, None), # a kept component still stays mapped+ ],+)+def test_every_shard_backend_choice(monkeypatch, tmp_path, stored, keep, every, expected):+ from safetensors.torch import save_file++ comp = tmp_path / "text_encoder"+ comp.mkdir()+ path = comp / "model.safetensors"+ save_file({"w": torch.zeros(64, 64, dtype = stored)}, str(path))+ monkeypatch.setattr(sys, "platform", "win32")+ seen = _record_backends(monkeypatch)+ with sp.pread_safetensors(convert_to = stored, keep = keep, every_shard = every):+ with modeling_utils.safe_open(str(path), framework = "pt", device = "cpu") as handle:+ handle.get_slice("w")[...]+ assert seen == [expected]+++ "platform, igpu, every",+ [("win32", True, True), ("win32", False, False), ("linux", True, False)],+)+def test_image_load_scope_engages_on_windows_igpu_only(monkeypatch, platform, igpu, every):+ from core.inference import diffusion++ monkeypatch.setattr(sys, "platform", platform)+ monkeypatch.setattr(host_pinning, "xpu_is_integrated", lambda: igpu)+ got = {}++ def fake_scope(+ convert_to = None,+ keep = (),+ every_shard = False,+ ):+ got.update(convert_to = convert_to, keep = keep, every_shard = every_shard)+ return "scope"++ monkeypatch.setattr(sp, "pread_safetensors", fake_scope)+ assert diffusion._converted_shards_pread(torch.bfloat16, None) == "scope"+ assert got == {"convert_to": torch.bfloat16, "keep": (), "every_shard": every}+++_COMMIT_ARM = r"""+import ctypes, sys, torch+sys.path.insert(0, sys.argv[1])+import core.inference.safetensors_pread as sp+from transformers import LlamaForCausalLM++class PMC(ctypes.Structure):+ _fields_ = [("cb", ctypes.c_ulong), ("PageFaultCount", ctypes.c_ulong)] + [+ (n, ctypes.c_size_t) for n in ("PeakWorkingSetSize", "WorkingSetSize", "QuotaPeakPagedPoolUsage",+ "QuotaPagedPoolUsage", "QuotaPeakNonPagedPoolUsage", "QuotaNonPagedPoolUsage", "PagefileUsage",+ "PeakPagefileUsage")]++k = ctypes.WinDLL("kernel32", use_last_error = True)+k.GetCurrentProcess.restype = ctypes.c_void_p+k.K32GetProcessMemoryInfo.argtypes = [ctypes.c_void_p, ctypes.POINTER(PMC), ctypes.c_ulong]++def counters():+ c = PMC(); c.cb = ctypes.sizeof(PMC)+ assert k.K32GetProcessMemoryInfo(k.GetCurrentProcess(), ctypes.byref(c), c.cb), ctypes.get_last_error()+ return c++import transformers.modeling_utils as mu++base = counters().PagefileUsage+with sp.pread_safetensors(convert_to = torch.bfloat16, every_shard = sys.argv[3] == "pread"):+ scoped = mu.safe_open++ # Studio pins transformers 5.5, which passes no backend; 5.17+ asks for pread on Windows itself+ def as_studio_pin(*args, **kwargs):+ kwargs.pop("backend", None)+ return scoped(*args, **kwargs)++ mu.safe_open = as_studio_pin+ model = LlamaForCausalLM.from_pretrained(sys.argv[2], dtype = torch.bfloat16)+# what .to("xpu") does on an iGPU: each weight gets a fresh allocation in the same RAM, one at a time+with torch.no_grad():+ for p in model.parameters():+ p.data = p.data.clone()+print("PEAK_MIB", (counters().PeakPagefileUsage - base) >> 20)+"""++[email protected](sys.platform != "win32", reason = "process commit is a Windows counter")+@requires_backend+def test_windows_igpu_load_commits_about_one_copy(tmp_path):+ """bf16 load at bf16 then moved onto memory the host shares: a mapped shard stays charged until its last tensor+ has moved (stored + copy); pread frees each tensor as it moves."""+ import subprocess+ import warnings+ from pathlib import Path++ from transformers import LlamaConfig, LlamaForCausalLM++ config = LlamaConfig(+ vocab_size = 32000,+ hidden_size = 2048,+ intermediate_size = 5632,+ num_hidden_layers = 12,+ num_attention_heads = 16,+ num_key_value_heads = 16,+ )+ with torch.device("meta"):+ model = LlamaForCausalLM(config)+ model = model.to_empty(device = "cpu").to(torch.bfloat16)+ with torch.no_grad():+ for p in model.parameters():+ p.zero_()+ # one shard, like Qwen-Image-2.1's 16.3 GB text_encoder/model.safetensors+ model.save_pretrained(tmp_path, max_shard_size = "4GB")+ stored_mib = sum(f.stat().st_size for f in tmp_path.glob("*.safetensors")) >> 20+ del model++ backend = str(Path(sp.__file__).resolve().parents[2])+ peaks = {}+ for arm in ("mmap", "pread"):+ out = subprocess.run(+ [sys.executable, "-c", _COMMIT_ARM, backend, str(tmp_path), arm],+ capture_output = True,+ text = True,+ timeout = 900,+ )+ assert out.returncode == 0, out.stderr[-3000:]+ peaks[arm] = int(+ next(l for l in out.stdout.splitlines() if l.startswith("PEAK_MIB")).split()[1]+ )+ warnings.warn(+ f"windows commit for a {stored_mib} MiB bf16 load moved onto shared memory: {peaks}"+ )+ assert peaks["mmap"] - peaks["pread"] >= stored_mib * 0.7, peaksdiff --git a/studio/backend/utils/hardware/hardware.py b/studio/backend/utils/hardware/hardware.pyindex b72bd86c86a..a4be489cd7d 100644--- a/studio/backend/utils/hardware/hardware.py+++ b/studio/backend/utils/hardware/hardware.py@@ -653,6 +653,8 @@ _INTEL_XPU_PCI_IDS = frozenset((0xB084, 0xB085, 0xB086, 0xB087, # PTL Arc Pro (Mesa iris_pci_ids.h)0x0BD0, 0x0BD4, 0x0BD5, 0x0BD6, 0x0BD7, 0x0BD8, 0x0BD9, 0x0BDA, 0x0BDB, 0x0B69, 0x0B6E, # PVC)) # fmt: skip+# The iGPUs above: their device memory is host RAM. Kept literal, installer tests parse _INTEL_XPU_PCI_IDS.+_INTEL_XPU_IGPU_PCI_IDS = frozenset((0x7D55, 0x7D51, 0x64A0, *range(0xB080, 0xB088)))def _is_intel_xpu_pci_id(device_id: int) -> bool: