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 a hosted checkpoint's header across processes, so an 8 GiB Qwen-Image-2.1 load skips its FP8 header read (L4 load -1.8 s) (#17353)

Daniel Han0a9c409b0995
diff --git a/studio/backend/core/inference/prequant_header_probe.py b/studio/backend/core/inference/prequant_header_probe.py
index bed1fca9560..3be1803f77d 100644
--- a/studio/backend/core/inference/prequant_header_probe.py
+++ b/studio/backend/core/inference/prequant_header_probe.py
@@ -214,6 +214,12 @@ def read_hub_checkpoint_header(
size = getattr(meta, "size", None)
if not size:
return None, True
+ etag = getattr(meta, "etag", None)
+ cached = _header_cache_get(etag, filename, int(size))
+ if cached is not None:
+ cached["size"] = int(size)
+ cached["commit"] = getattr(meta, "commit_hash", None)
+ return cached, True
url = hf_hub_url(repo_id, filename, revision = getattr(meta, "commit_hash", None) or revision)
try:
headers = dict(build_hf_headers(token = token))
@@ -221,6 +227,85 @@ def read_hub_checkpoint_header(
return None, True
out = read_checkpoint_header(filename, _hub_range_reader(url, headers, deadline), int(size))
if out is not None:
+ _header_cache_put(etag, filename, int(size), out)
out["size"] = int(size)
out["commit"] = getattr(meta, "commit_hash", None)
return out, True
+
+
+# "0": every fresh process reads a hosted checkpoint's header over the network again (two ranged GETs, ~1-2 s).
+HEADER_CACHE_ENV = "UNSLOTH_DIFFUSION_HEADER_CACHE"
+_HEADER_CACHE_NAME = "prequant_header_cache.json"
+_HEADER_CACHE_MAX = 64
+_HEADER_CACHE_ENTRY_MAX = 512 * 1024
+_HEADER_CACHE_KEYS = ("format", "metadata", "container")
+_HEADER_CACHE_LOCK = threading.Lock()
+
+
+def _header_cache_file() -> Optional[Any]:
+ import os
+ if (os.environ.get(HEADER_CACHE_ENV) or "").strip().lower() in ("0", "off", "false", "no"):
+ return None
+ try:
+ from pathlib import Path
+
+ from utils.paths.storage_roots import cache_root
+ return Path(cache_root()) / _HEADER_CACHE_NAME
+ except Exception: # noqa: BLE001 - no resolvable root: read over the network as before
+ return None
+
+
+def _header_cache_key(etag: Any, filename: str, size: int) -> Optional[str]:
+ """The file's content digest (the Hub ETag of an LFS / Xet file is its sha256) plus name and size: one answer
+ per content, whatever repo or revision serves it. Read only after this request's own HEAD returned it."""
+ if not isinstance(etag, str) or not etag.strip('"'):
+ return None
+ return f"{etag.strip(chr(34))}|{filename.rsplit('/', 1)[-1]}|{int(size)}"
+
+
+def _header_cache_get(etag: Any, filename: str, size: int) -> Optional[dict]:
+ key = _header_cache_key(etag, filename, size)
+ path = _header_cache_file() if key else None
+ if path is None:
+ return None
+ try:
+ with _HEADER_CACHE_LOCK:
+ table = json.loads(path.read_text(encoding = "utf-8")) if path.is_file() else {}
+ hit = table.get(key) if isinstance(table, dict) else None
+ except Exception: # noqa: BLE001 - unreadable or corrupt: a miss
+ return None
+ if not isinstance(hit, dict) or not isinstance(hit.get("format"), str):
+ return None
+ if not all(k in hit for k in _HEADER_CACHE_KEYS):
+ return None
+ # nothing read this time: the skip log's byte count stays true
+ return {**{k: hit[k] for k in _HEADER_CACHE_KEYS}, "bytes_read": 0}
+
+
+def _header_cache_put(etag: Any, filename: str, size: int, header: dict) -> None:
+ import os
+
+ key = _header_cache_key(etag, filename, size)
+ path = _header_cache_file() if key else None
+ if path is None:
+ return
+ try:
+ entry = {k: header.get(k) for k in _HEADER_CACHE_KEYS}
+ if len(json.dumps(entry, default = str)) > _HEADER_CACHE_ENTRY_MAX:
+ return
+ with _HEADER_CACHE_LOCK:
+ try:
+ table = json.loads(path.read_text(encoding = "utf-8")) if path.is_file() else {}
+ except Exception: # noqa: BLE001
+ table = {}
+ if not isinstance(table, dict):
+ table = {}
+ table.pop(key, None)
+ table = dict(list(table.items())[-(_HEADER_CACHE_MAX - 1) :])
+ table[key] = entry
+ path.parent.mkdir(parents = True, exist_ok = True)
+ tmp = path.with_name(f"{path.name}.{os.getpid()}.{threading.get_ident()}.tmp")
+ tmp.write_text(json.dumps(table, default = str), encoding = "utf-8")
+ os.replace(tmp, path)
+ except Exception: # noqa: BLE001 - persistence is best-effort
+ pass
diff --git a/studio/backend/tests/test_prequant_header_cache.py b/studio/backend/tests/test_prequant_header_cache.py
new file mode 100644
index 00000000000..23ca335cfe9
--- /dev/null
+++ b/studio/backend/tests/test_prequant_header_cache.py
@@ -0,0 +1,190 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Hosted headers are cached per Hub ETag: a fresh process does one HEAD, not HEAD + two ranged GETs."""
+
+from __future__ import annotations
+
+import http.server
+import json
+import os
+import struct
+import threading
+import types
+
+import pytest
+
+import core.inference.prequant_header_probe as probe
+
+
+def _write_safetensors(
+ path,
+ meta,
+ payload = 4 << 20,
+):
+ from core.inference.prequant_safetensors import UNSLOTH_FORMAT_KEY, UNSLOTH_METADATA_KEY
+
+ header = {
+ "__metadata__": {
+ UNSLOTH_FORMAT_KEY: "unsloth-prequant-v1",
+ UNSLOTH_METADATA_KEY: json.dumps(meta),
+ },
+ "w": {"dtype": "I8", "shape": [payload], "data_offsets": [0, payload]},
+ }
+ raw = json.dumps(header).encode("utf-8")
+ with open(path, "wb") as handle:
+ handle.write(struct.pack("<Q", len(raw)))
+ handle.write(raw)
+ handle.write(b"\0" * payload)
+ return path
+
+
+class _RangeServer:
+ def __init__(self, root):
+ self.root = root
+ self.ranged_gets = 0
+ owner = self
+
+ class Handler(http.server.BaseHTTPRequestHandler):
+ def log_message(self, *args):
+ pass
+
+ def do_GET(self):
+ path = os.path.join(owner.root, self.path.lstrip("/").split("?")[0])
+ size = os.path.getsize(path)
+ a, _, b = self.headers.get("Range", "bytes=0-").replace("bytes=", "").partition("-")
+ start, end = int(a), min(size - 1, int(b) if b else size - 1)
+ owner.ranged_gets += 1
+ with open(path, "rb") as handle:
+ handle.seek(start)
+ body = handle.read(end - start + 1)
+ self.send_response(206)
+ self.send_header("Content-Length", str(len(body)))
+ self.send_header("Content-Range", f"bytes {start}-{end}/{size}")
+ self.end_headers()
+ self.wfile.write(body)
+
+ self.httpd = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)
+ self.thread = threading.Thread(target = self.httpd.serve_forever, daemon = True)
+ self.thread.start()
+ self.url = f"http://127.0.0.1:{self.httpd.server_address[1]}"
+
+ def close(self):
+ self.httpd.shutdown()
+ self.httpd.server_close()
+
+
+def hub(tmp_path, monkeypatch):
+ import huggingface_hub
+ import utils.paths.storage_roots as roots
+
+ root = tmp_path / "hub"
+ (root / "org" / "repo").mkdir(parents = True)
+ server = _RangeServer(str(root))
+ state = types.SimpleNamespace(root = root, server = server, etag = "a" * 64, heads = 0)
+
+ def hf_hub_url(
+ repo_id,
+ filename,
+ revision = None,
+ **_,
+ ):
+ return f"{server.url}/{repo_id}/{filename}"
+
+ def get_hf_file_metadata(
+ url,
+ token = None,
+ timeout = None,
+ **_,
+ ):
+ state.heads += 1
+ path = os.path.join(str(root), url[len(server.url) + 1 :])
+ if not os.path.isfile(path):
+ err = Exception("404")
+ err.response = types.SimpleNamespace(status_code = 404)
+ raise err
+ return types.SimpleNamespace(
+ size = os.path.getsize(path), commit_hash = "c0ffee", etag = state.etag
+ )
+
+ monkeypatch.setattr(huggingface_hub, "hf_hub_url", hf_hub_url)
+ monkeypatch.setattr(huggingface_hub, "get_hf_file_metadata", get_hf_file_metadata)
+ cache_dir = tmp_path / "studio_cache"
+ monkeypatch.setattr(roots, "cache_root", lambda: cache_dir)
+ monkeypatch.delenv(
+ probe.__dict__.get("HEADER_CACHE_ENV", "UNSLOTH_DIFFUSION_HEADER_CACHE"), raising = False
+ )
+ _write_safetensors(
+ root / "org" / "repo" / "model.safetensors", {"scheme": "fp8", "min_features": 512}
+ )
+ yield state
+ server.close()
+
+
+def _read(name = "model.safetensors"):
+ return probe.read_hub_checkpoint_header("org/repo", name, timeout_seconds = 10.0)
+
+
+def test_a_second_read_of_the_same_content_skips_the_ranged_gets(hub):
+ first, exists = _read()
+ assert exists is True and first is not None
+ assert first["format"] == "unsloth-prequant-v1"
+ reads = hub.server.ranged_gets
+ assert reads >= 1 and first["bytes_read"] > 0
+
+ second, exists = _read()
+ assert exists is True
+ # same header, one more HEAD, no range read
+ assert hub.server.ranged_gets == reads
+ assert hub.heads == 2
+ assert second["format"] == first["format"]
+ assert second["metadata"] == first["metadata"]
+ assert second["container"] == first["container"]
+ assert second["size"] == first["size"] and second["commit"] == "c0ffee"
+ assert second["bytes_read"] == 0
+
+
+def test_a_reupload_with_a_new_etag_reads_the_new_header(hub):
+ _read()
+ reads = hub.server.ranged_gets
+ _write_safetensors(
+ hub.root / "org" / "repo" / "model.safetensors", {"scheme": "fp8", "min_features": 1024}
+ )
+ hub.etag = "b" * 64
+ header, _ = _read()
+ assert hub.server.ranged_gets > reads
+ assert header["metadata"]["min_features"] == 1024
+
+
+def test_a_missing_file_is_still_a_definite_404(hub):
+ _read()
+ header, exists = _read("gone.safetensors")
+ assert header is None and exists is False
+
+
+def test_no_etag_never_caches(hub):
+ hub.etag = None
+ _read()
+ reads = hub.server.ranged_gets
+ _read()
+ assert hub.server.ranged_gets > reads
+
+
+def test_a_corrupt_cache_file_is_a_miss(hub, tmp_path):
+ _read()
+ (tmp_path / "studio_cache" / "prequant_header_cache.json").write_text(
+ "{not json", encoding = "utf-8"
+ )
+ reads = hub.server.ranged_gets
+ header, _ = _read()
+ assert header is not None and hub.server.ranged_gets > reads
+
+
+def test_the_kill_switch_reads_every_time(hub, monkeypatch):
+ monkeypatch.setenv("UNSLOTH_DIFFUSION_HEADER_CACHE", "0")
+ _read()
+ reads = hub.server.ranged_gets
+ _read()
+ assert hub.server.ranged_gets > reads
+ assert not (hub.root.parent / "studio_cache" / "prequant_header_cache.json").exists()