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 tests: keep the multi-GB fake checkpoints sparse on Windows (#17119)
Daniel Hana4323ea18062
diff --git a/studio/backend/tests/_sparse_file.py b/studio/backend/tests/_sparse_file.pynew file mode 100644index 00000000000..363eede6d00--- /dev/null+++ b/studio/backend/tests/_sparse_file.py@@ -0,0 +1,64 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Extend a test file to a large size without paying for it on disk.++``truncate`` past the end leaves a hole on Linux and macOS. On Windows it does not: NTFS allocates the+extension unless the file is sparse, and Python's ``truncate`` extends through the CRT's ``_chsize_s``,+which writes the new bytes as zeros, so even a sparse file gets them allocated. A suite of multi-GB fake+checkpoints then fills a Windows runner's disk.+"""++from __future__ import annotations++import os+++def _extend_sparse_windows(handle, size: int) -> None:+ import ctypes+ import msvcrt+ from ctypes import wintypes++ kernel32 = ctypes.WinDLL("kernel32", use_last_error = True)+ kernel32.DeviceIoControl.argtypes = [+ wintypes.HANDLE,+ wintypes.DWORD,+ ctypes.c_void_p,+ wintypes.DWORD,+ ctypes.c_void_p,+ wintypes.DWORD,+ ctypes.POINTER(wintypes.DWORD),+ ctypes.c_void_p,+ ]+ kernel32.SetFilePointerEx.argtypes = [+ wintypes.HANDLE,+ ctypes.c_longlong,+ ctypes.c_void_p,+ wintypes.DWORD,+ ]+ kernel32.SetEndOfFile.argtypes = [wintypes.HANDLE]+ fsctl_set_sparse = 0x000900C4+ file_begin = 0++ handle.flush()+ position = handle.tell()+ native = wintypes.HANDLE(msvcrt.get_osfhandle(handle.fileno()))+ returned = wintypes.DWORD(0)+ # Sparse first, then move the end of file: SetEndOfFile on a sparse file allocates nothing.+ if not kernel32.DeviceIoControl(+ native, fsctl_set_sparse, None, 0, None, 0, ctypes.byref(returned), None+ ):+ raise ctypes.WinError(ctypes.get_last_error())+ if not kernel32.SetFilePointerEx(native, size, None, file_begin) or not kernel32.SetEndOfFile(+ native+ ):+ raise ctypes.WinError(ctypes.get_last_error())+ handle.seek(position)+++def extend_sparse(handle, size: int) -> None:+ """``handle.truncate(size)`` that stays sparse on Windows too."""+ if os.name == "nt":+ _extend_sparse_windows(handle, size)+ else:+ handle.truncate(size)diff --git a/studio/backend/tests/test_diffusion_backend.py b/studio/backend/tests/test_diffusion_backend.pyindex 9c849b81090..3f792908b7d 100644--- a/studio/backend/tests/test_diffusion_backend.py+++ b/studio/backend/tests/test_diffusion_backend.py@@ -17,6 +17,8 @@ from pathlib import Pathimport pytest+from ._sparse_file import extend_sparse+from ._torchao_installed import needs_torchaofrom core.inference.diffusion import (@@ -7015,7 +7017,7 @@ def _hub_blob(root, repo_id, name, mib):blob = root / f"models--{repo_id.replace('/', '--')}" / "blobs" / nameblob.parent.mkdir(parents = True, exist_ok = True)with open(blob, "wb") as fh:- fh.truncate(mib * 1024 * 1024)+ extend_sparse(fh, mib * 1024 * 1024)return blob@@ -7043,7 +7045,7 @@ def _sparse_snapshot_file(root, repo_id, rev, rel, mib):path = root / f"models--{repo_id.replace('/', '--')}" / "snapshots" / rev / relpath.parent.mkdir(parents = True, exist_ok = True)with open(path, "wb") as fh:- fh.truncate(mib * 1024 * 1024)+ extend_sparse(fh, mib * 1024 * 1024)return path@@ -7078,7 +7080,7 @@ def _other_root_base_snapshot(path = snapshot / relpath.parent.mkdir(parents = True, exist_ok = True)with open(path, "wb") as fh:- fh.truncate(mib * 1024 * 1024)+ extend_sparse(fh, mib * 1024 * 1024)return snapshot@@ -10264,7 +10266,7 @@ def _seed_cache_file(target.write_bytes(b"x")if size is not None:with target.open("r+b") as handle:- handle.truncate(size)+ extend_sparse(handle, size)return target@@ -10836,7 +10838,7 @@ def _base_snapshot_with_sizes(tmp_path, monkeypatch, sizes):path = snapshot / relpath.parent.mkdir(parents = True, exist_ok = True)with open(path, "wb") as fh:- fh.truncate(mib * 1024 * 1024)+ extend_sparse(fh, mib * 1024 * 1024)return snapshot@@ -13092,7 +13094,7 @@ def _q21_precast_cache(tmp_path, monkeypatch, *, mib):snap.mkdir(parents = True)if mib:with open(snap / "Qwen-Image-2.1-text_encoder-FP8.safetensors", "wb") as fh:- fh.truncate(mib * 1024 * 1024)+ extend_sparse(fh, mib * 1024 * 1024)return snap@@ -13165,7 +13167,7 @@ def test_precast_text_encoder_mib_prices_hidreams_standalone_fourth_encoder(monksnap = repo / "snapshots" / revsnap.mkdir(parents = True)with open(snap / te_candidate_filenames(source)[0], "wb") as fh:- fh.truncate(7700 * 1024 * 1024)+ extend_sparse(fh, 7700 * 1024 * 1024)# text_encoder_3 is FLUX.1's T5-XXL, served from FLUX.1's hosted text_encoder_2 file; cache it too.t5 = te_prequant_sources(fam, te_quant_mode = "fp8", target = target, components = ("text_encoder_3",)@@ -13175,7 +13177,7 @@ def test_precast_text_encoder_mib_prices_hidreams_standalone_fourth_encoder(monk(t5_repo / "refs" / "main").write_text(rev)(t5_repo / "snapshots" / rev).mkdir(parents = True)with open(t5_repo / "snapshots" / rev / te_candidate_filenames(t5)[0], "wb") as fh:- fh.truncate(5000 * 1024 * 1024)+ extend_sparse(fh, 5000 * 1024 * 1024)assert DiffusionBackend._precast_text_encoder_mib(fam, "HiDream-ai/HiDream-I1-Full", target, "fp8") == (12700, ("text_encoder_3", "text_encoder_4"), True)diff --git a/studio/backend/tests/test_diffusion_plan_load_dtype.py b/studio/backend/tests/test_diffusion_plan_load_dtype.pyindex 6177766220e..43a2681b3e3 100644--- a/studio/backend/tests/test_diffusion_plan_load_dtype.py+++ b/studio/backend/tests/test_diffusion_plan_load_dtype.py@@ -9,6 +9,8 @@ import typesimport pytest+from ._sparse_file import extend_sparse+torch = pytest.importorskip("torch")from core.inference import diffusion as dmod@@ -39,7 +41,7 @@ def _safetensors(path, tensors):with open(path, "wb") as fh:fh.write(len(raw).to_bytes(8, "little"))fh.write(raw)- fh.truncate(8 + len(raw) + offset)+ extend_sparse(fh, 8 + len(raw) + offset)return path@@ -257,7 +259,7 @@ def test_gguf_companions_are_priced_at_the_load_dtype(tmp_path, monkeypatch):)gguf = tmp_path / "t-Q8_0.gguf"with open(gguf, "wb") as fh:- fh.truncate(1000 * MIB)+ extend_sparse(fh, 1000 * MIB)_card(monkeypatch)plan = _plan(torch.bfloat16, kind = "gguf", single_file = str(gguf))assert plan.estimates["companion_dense_mib"] == 1100diff --git a/studio/backend/tests/test_sparse_file_helper.py b/studio/backend/tests/test_sparse_file_helper.pynew file mode 100644index 00000000000..054d9541b92--- /dev/null+++ b/studio/backend/tests/test_sparse_file_helper.py@@ -0,0 +1,40 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""The fake multi-GB checkpoints in the diffusion and video suites must cost (almost) nothing on disk."""++from __future__ import annotations++import os++from ._sparse_file import extend_sparse++_GIB = 1 << 30+++def _allocated_bytes(path) -> int:+ if os.name == "nt":+ import ctypes+ from ctypes import wintypes++ kernel32 = ctypes.WinDLL("kernel32", use_last_error = True)+ kernel32.GetCompressedFileSizeW.argtypes = [+ wintypes.LPCWSTR,+ ctypes.POINTER(wintypes.DWORD),+ ]+ kernel32.GetCompressedFileSizeW.restype = wintypes.DWORD+ high = wintypes.DWORD(0)+ low = kernel32.GetCompressedFileSizeW(str(path), ctypes.byref(high))+ return (high.value << 32) | low+ return os.stat(path).st_blocks * 512+++def test_a_gigabyte_fake_file_allocates_almost_nothing(tmp_path):+ path = tmp_path / "fake.safetensors"+ with open(path, "wb") as fh:+ fh.write(b"header")+ extend_sparse(fh, _GIB)+ assert os.path.getsize(path) == _GIB+ with open(path, "rb") as fh:+ assert fh.read(6) == b"header"+ assert _allocated_bytes(path) < 64 << 20, _allocated_bytes(path)diff --git a/studio/backend/tests/test_video_backend.py b/studio/backend/tests/test_video_backend.pyindex 0f58ab0f91c..ccac2a8a42d 100644--- a/studio/backend/tests/test_video_backend.py+++ b/studio/backend/tests/test_video_backend.py@@ -18,6 +18,18 @@ from pathlib import Pathimport pytest+try:+ from ._sparse_file import extend_sparse+except ImportError: # loaded by path, with no parent package (test_h3_native_resident.py)+ import importlib.util as _importlib_util++ _sparse_spec = _importlib_util.spec_from_file_location(+ "_studio_tests_sparse_file", Path(__file__).with_name("_sparse_file.py")+ )+ _sparse_module = _importlib_util.module_from_spec(_sparse_spec)+ _sparse_spec.loader.exec_module(_sparse_module)+ extend_sparse = _sparse_module.extend_sparse+import core.inference.video as video_modulefrom core.inference.diffusion_device import DiffusionDeviceTargetfrom core.inference.video import (@@ -12875,7 +12887,7 @@ def test_ltx23_hub_pick_is_substituted_only_from_the_official_repo(tmp_path, monmonkeypatch.setattr(hf_cache_state, "hf_cache_roots", lambda *a, **k: [tmp_path])good = blobs / video_ltx2.LTX23_PREQUANT_SOURCE_SHA256with open(good, "wb") as fh:- fh.truncate(video_ltx2.LTX23_PREQUANT_SOURCE_SIZE) # sparse+ extend_sparse(fh, video_ltx2.LTX23_PREQUANT_SOURCE_SIZE) # sparsesnap = tmp_path / "snap"snap.mkdir()(snap / name).symlink_to(good)@@ -12887,7 +12899,7 @@ def test_ltx23_hub_pick_is_substituted_only_from_the_official_repo(tmp_path, monelsewhere.mkdir(parents = True)fake = elsewhere / video_ltx2.LTX23_PREQUANT_SOURCE_SHA256with open(fake, "wb") as fh:- fh.truncate(video_ltx2.LTX23_PREQUANT_SOURCE_SIZE)+ extend_sparse(fh, video_ltx2.LTX23_PREQUANT_SOURCE_SIZE)(snap / name).unlink()(snap / name).symlink_to(fake)monkeypatch.setattr(@@ -12899,7 +12911,9 @@ def test_ltx23_hub_pick_is_substituted_only_from_the_official_repo(tmp_path, mon(snap / name).symlink_to(good)bad = blobs / ("0" * 64)with open(bad, "wb") as fh:- fh.truncate(video_ltx2.LTX23_PREQUANT_SOURCE_SIZE) # right size, wrong content (zeros)+ extend_sparse(+ fh, video_ltx2.LTX23_PREQUANT_SOURCE_SIZE+ ) # right size, wrong content (zeros)(snap / name).unlink()(snap / name).symlink_to(bad)monkeypatch.setattr(video_ltx2, "ltx23_source_sha256", lambda path: "0" * 64)