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
Share the setups and the child imports two test files were paying for twice (#10893)
Daniel Hand2b256a3c2df
diff --git a/tests/python/test_docker_nb_refresh_state_durability.py b/tests/python/test_docker_nb_refresh_state_durability.pyindex efc2ff59277..8a7781670f8 100644--- a/tests/python/test_docker_nb_refresh_state_durability.py+++ b/tests/python/test_docker_nb_refresh_state_durability.py@@ -27,32 +27,88 @@ from pathlib import Pathimport pytest-def _build_shared_setup_1(root: Path):+# Ten of the eleven tests below start from one of three identical setups, each of+# which writes 500 notebooks, commits them and then drives one or two real sync+# runs over the lot. Build each one once per process and `copytree` it in, which is+# ~0.04s: nothing any of them writes embeds its own absolute path (the clone lives+# in the script's own $TMP and is gone by the time the run returns, and `up`'s git+# repo has no remote), so the copy is the same tree at a different place. Each test+# still gets a private directory under its own `tmp_path` that it is free to+# mutate, so the isolation is unchanged -- and the corpus is still 500 notebooks,+# which is what opens the failure window these tests aim at.+_PROTOTYPES: dict[str, Path] = {}+++def _prototype(key: str, build) -> Path:+ prototype = _PROTOTYPES.get(key)+ if prototype is None:+ prototype = Path(tempfile.mkdtemp(prefix = "unsloth-nbsync-"))+ atexit.register(shutil.rmtree, prototype, True)+ build(prototype)+ _PROTOTYPES[key] = prototype+ return prototype+++def _copy_of(key: str, tmp_path: Path, build):+ root = tmp_path / key+ shutil.copytree(_prototype(key, build), root, symlinks = True)+ return root / "dest", root / "tpl", root / "up"+++def _build_seed_synced(root: Path):+ """Seed-only template, 500 one-byte notebooks upstream, initial no-refresh sync."""tpl, dest, up = _template(root), root / "dest", _upstream(root)dest.mkdir()_run(tpl, dest, up, refresh = False)- _run(tpl, dest, up, refresh = True)- return dest, tpl, up-# Five tests start from the same two sync runs over 500 notebooks, which is-# ~7.4s each and ~37s of the file. Build it once per process and copy it in,-# which is ~0.04s: nothing it writes embeds its own absolute path, so the copy-# is the same tree at a different place. Each test still gets a private-# directory it is free to mutate, so the isolation is unchanged.-_SHARED_1: Path | None = None+def _build_refreshed(root: Path):+ """`_build_seed_synced` plus one uncapped refresh: everything published and recorded."""+ _build_seed_synced(root)+ _run(root / "tpl", root / "dest", root / "up", refresh = True)+++def _build_big_synced(root: Path):+ """500-notebook baked template, same upstream, initial no-refresh sync."""+ tpl, dest, up = _big_template(root, NOTEBOOKS), root / "dest", _upstream(root)+ dest.mkdir()+ _run(tpl, dest, up, refresh = False)+++# The capped refresh is itself shared, not just the tree it ran against: the two+# tests below drive the SAME capped run over the SAME tree and then ask different+# questions of it -- whether anything was published without a record, and whether+# the next refresh restores what was rolled back. Neither mutates anything before+# it reads the result, so one run answers both, and the file size limit is what+# makes the window either way: RLIMIT_FSIZE caps a file's SIZE and the state+# records RELATIVE names, so running it under the prototype's directory rather+# than the test's changes nothing about where the appends start failing.+_CAPPED_FIRST_REFRESH: subprocess.CompletedProcess | None = None+++def _build_capped_first_refresh(root: Path):+ global _CAPPED_FIRST_REFRESH+ _build_seed_synced(root)+ _CAPPED_FIRST_REFRESH = _run(+ root / "tpl",+ root / "dest",+ root / "up",+ cap_kib = CAP_KIB,+ refresh = True,+ )+++def _shared_capped_first_refresh(tmp_path):+ dest, tpl, up = _copy_of("capped", tmp_path, _build_capped_first_refresh)+ return dest, tpl, up, _CAPPED_FIRST_REFRESHdef _shared_setup_1(tmp_path):- global _SHARED_1- if _SHARED_1 is None:- prototype = Path(tempfile.mkdtemp(prefix = "unsloth-nbsync-"))- atexit.register(shutil.rmtree, prototype, True)- _build_shared_setup_1(prototype)- _SHARED_1 = prototype- root = tmp_path / "s1"- shutil.copytree(_SHARED_1, root, symlinks = True)- return root / "dest", root / "tpl", root / "up"+ return _copy_of("s1", tmp_path, _build_refreshed)+++def _shared_big_synced(tmp_path):+ return _copy_of("big", tmp_path, _build_big_synced)REPO_ROOT = Path(__file__).resolve().parents[2]@@ -155,11 +211,7 @@ def _published(dest: Path) -> set[str]:def test_no_notebook_is_published_without_a_record(tmp_path: Path):"""The invariant. A file we wrote but could not record is the unrecoverablestate, so it must be rolled back rather than left behind."""- tpl, dest, up = _template(tmp_path), tmp_path / "dest", _upstream(tmp_path)- dest.mkdir()- _run(tpl, dest, up, refresh = False)-- run = _run(tpl, dest, up, cap_kib = CAP_KIB, refresh = True)+ dest, tpl, up, run = _shared_capped_first_refresh(tmp_path)assert "could not be written" in run.stdout, ("the cap did not bite; this test proves nothing unless some append failed\n"+ run.stdout@@ -176,11 +228,7 @@ def test_no_notebook_is_published_without_a_record(tmp_path: Path):@needs_gitdef test_the_next_refresh_recovers_everything_that_was_rolled_back(tmp_path: Path):"""Rollback is only correct if the retry actually restores them."""- tpl, dest, up = _template(tmp_path), tmp_path / "dest", _upstream(tmp_path)- dest.mkdir()- _run(tpl, dest, up, refresh = False)-- first = _run(tpl, dest, up, cap_kib = CAP_KIB, refresh = True)+ dest, tpl, up, first = _shared_capped_first_refresh(tmp_path)assert "could not be written" in first.stdout, first.stdout + first.stderr# strictly fewer than the full set: `< NOTEBOOKS + 1` was vacuous, since the# run yields NOTEBOOKS either way and seed.ipynb is dropped as deleted upstream@@ -223,10 +271,7 @@ def test_a_user_edited_notebook_is_never_rolled_back(tmp_path: Path):version of this test passed without executing the code it named. The assertionthat some pristine notebook WAS removed is what keeps it honest."""- tpl, dest, up = _template(tmp_path), tmp_path / "dest", _upstream(tmp_path)- dest.mkdir()- _run(tpl, dest, up, refresh = False)- _run(tpl, dest, up, refresh = True) # uncapped: everything published and recorded+ dest, tpl, up = _shared_setup_1(tmp_path) # uncapped: everything published and recordedassert len(_published(dest)) == NOTEBOOKSedited = {f"nb{i:09d}.ipynb" for i in range(2, NOTEBOOKS + 1, 2)}@@ -329,6 +374,11 @@ def test_a_failed_record_on_an_unchanged_notebook_rolls_it_back(tmp_path: Path):missing one (early return), so the suite could call it seven times and roll backzero. The `unchanged` branch is the reachable one that must actually remove."""+ # Its own build, not a shared prototype: this is the only test upstream of+ # `_json_upstream`, whose notebooks are valid notebook JSON rather than the one+ # byte the others use. That difference is the whole point -- the body-aware+ # comparison can only report SAME, and so only reach the `unchanged` branch,+ # against real JSON.tpl, dest = _template(tmp_path), tmp_path / "dest"up = _json_upstream(tmp_path, NOTEBOOKS)dest.mkdir()@@ -515,9 +565,7 @@ def test_an_abandoned_restore_puts_the_tree_back(tmp_path: Path):was BEFORE the restores -- so the restored files have to go back too. Leaving themmeans the next refresh sees baked content where the state holds post-refreshhashes and reads every one as a user edit."""- tpl, dest, up = _big_template(tmp_path, NOTEBOOKS), tmp_path / "dest", _upstream(tmp_path)- dest.mkdir()- _run(tpl, dest, up, refresh = False)+ dest, tpl, up = _shared_big_synced(tmp_path)before = _recorded(dest)assert len(before) == NOTEBOOKS@@ -559,9 +607,7 @@ def test_a_lost_merge_record_does_not_publish_the_short_state(tmp_path: Path):abandon the staged state rather than publish it and merely withhold the marker.A failed COPY is different and still publishes, because the next boot re-walks thetemplate."""- tpl, dest, up = _big_template(tmp_path, NOTEBOOKS), tmp_path / "dest", _upstream(tmp_path)- dest.mkdir()- _run(tpl, dest, up, refresh = False)+ dest, tpl, up = _shared_big_synced(tmp_path)# notebooks the refresh had added: present in $DEST and in the state, absent from# the baked template, so only the merge loop can carry their records forwarddiff --git a/tests/test_model_registry.py b/tests/test_model_registry.pyindex 1376c2b7119..8552157620f 100644--- a/tests/test_model_registry.py+++ b/tests/test_model_registry.py@@ -137,7 +137,40 @@ def _run_registry_child(body: str) -> subprocess.CompletedProcess:)-def test_importing_registry_does_not_register_models():+_REGISTRY_LIFECYCLE = (+ "import unsloth.registry\n"+ "from unsloth.registry import register_models\n"+ "from unsloth.registry.registry import MODEL_REGISTRY\n"+ "print('REGISTRY_SIZE', len(MODEL_REGISTRY))\n"+ "register_models()\n"+ "orgs = sorted({m.org for m in MODEL_REGISTRY.values()})\n"+ "deepseek = [k for k in MODEL_REGISTRY if 'deepseek' in k.lower()]\n"+ "print('ORGS', orgs)\n"+ "print('NUM_DEEPSEEK', len(deepseek))"+)++[email protected](scope = "module")+def registry_lifecycle():+ """One child interpreter for both questions below, shared at module scope.++ They ran two children with the same argv, the same inherited environment and+ the same prelude, differing only in what they did after the import: one read+ ``MODEL_REGISTRY`` straight away, the other called ``register_models()`` first.+ That is one interpreter's worth of work, because the second child's own body+ already begins from a bare import, so reading the size BEFORE it calls+ ``register_models()`` observes exactly what the first child observed. Each was+ ~15s, almost all of it ``import unsloth``, or three quarters of this file.++ Still a fresh interpreter, which is the property both tests need: it is+ independent of any ``register_models()`` the in-process tests above ran+ against the shared registry. Module-scoped, not session-scoped, because+ nothing outside this file wants it.+ """+ return _run_registry_child(_REGISTRY_LIFECYCLE)+++def test_importing_registry_does_not_register_models(registry_lifecycle):"""Importing the registry must not populate MODEL_REGISTRY on its own.``_deepseek`` used to call ``register_deepseek_models(...)`` at module@@ -145,11 +178,7 @@ def test_importing_registry_does_not_register_models():import side effect, unlike every other family which only registers ondemand."""- result = _run_registry_child(- "import unsloth.registry\n"- "from unsloth.registry.registry import MODEL_REGISTRY\n"- "print('REGISTRY_SIZE', len(MODEL_REGISTRY))"- )+ result = registry_lifecycleassert result.returncode == 0, (f"registry import subprocess exited {result.returncode}\n"f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"@@ -158,7 +187,7 @@ def test_importing_registry_does_not_register_models():assert size_lines == ["REGISTRY_SIZE 0"], result.stdout + result.stderr-def test_register_models_registers_no_upstream_originals():+def test_register_models_registers_no_upstream_originals(registry_lifecycle):"""``register_models()`` must register each family's ``unsloth``-org modelsand must NOT leak upstream vendor "original" models.@@ -171,16 +200,7 @@ def test_register_models_registers_no_upstream_originals():registered via the normal path. Runs in a fresh interpreter so it isindependent of other tests' registry mutations."""- result = _run_registry_child(- "import unsloth.registry\n"- "from unsloth.registry import register_models\n"- "from unsloth.registry.registry import MODEL_REGISTRY\n"- "register_models()\n"- "orgs = sorted({m.org for m in MODEL_REGISTRY.values()})\n"- "deepseek = [k for k in MODEL_REGISTRY if 'deepseek' in k.lower()]\n"- "print('ORGS', orgs)\n"- "print('NUM_DEEPSEEK', len(deepseek))"- )+ result = registry_lifecycleassert result.returncode == 0, (f"register_models subprocess exited {result.returncode}\n"f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}"