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 runs: keep a passing full-load verdict per unchanged bundle for the run listing (#17033)

Daniel Han2a6cc7339b1b
diff --git a/studio/backend/core/training/diffusion_checkpoint.py b/studio/backend/core/training/diffusion_checkpoint.py
index 15a2e568868..2f127afc589 100644
--- a/studio/backend/core/training/diffusion_checkpoint.py
+++ b/studio/backend/core/training/diffusion_checkpoint.py
@@ -14,8 +14,10 @@ import os
import random
import re
import shutil
+import threading
import time
import uuid
+from collections import OrderedDict
from dataclasses import dataclass, replace
from pathlib import Path
from typing import Any, Callable, Optional, Iterable
@@ -1277,15 +1279,69 @@ _UNRESUMABLE_STATUS = {
}
+# Listing-only memo of passing _assert_loadable verdicts, keyed on the stat of every state file.
+# Files touched within the settle window are not kept: a coarse mtime can hide a same-size rewrite.
+_LOADABLE_PASSES: "OrderedDict[str, tuple]" = OrderedDict()
+_LOADABLE_PASSES_MAX = 512
+_LOADABLE_SETTLE_NS = 2_000_000_000
+_LOADABLE_PASSES_LOCK = threading.Lock()
+
+
+def _loadable_key(path: Path, manifest: dict[str, Any]) -> Optional[tuple]:
+ """Identity of every file _assert_loadable reads, or None when one cannot be stat'ed."""
+ files = manifest.get("files")
+ if not isinstance(files, dict):
+ return None
+ entries = []
+ try:
+ for role in sorted(files, key = str):
+ name = files[role]
+ if not isinstance(name, str) or not name or Path(name).name != name:
+ return None
+ st = os.stat(path / name)
+ entries.append(
+ (str(role), name, st.st_dev, st.st_ino, st.st_size, st.st_mtime_ns, st.st_ctime_ns)
+ )
+ except (OSError, TypeError):
+ return None
+ return tuple(entries)
+
+
+def _assert_loadable_cached(path: Path, manifest: dict[str, Any]) -> bool:
+ cache_id = str(path)
+ key = _loadable_key(path, manifest)
+ if key is not None:
+ with _LOADABLE_PASSES_LOCK:
+ if _LOADABLE_PASSES.get(cache_id) == key:
+ _LOADABLE_PASSES.move_to_end(cache_id)
+ return True
+ try:
+ _assert_loadable(path, manifest)
+ except ResumeError:
+ # Never kept: a refusal can be transient (MemoryError, EMFILE).
+ return False
+ now = time.time_ns()
+ if (
+ key is not None
+ and all(now - max(entry[5], entry[6]) > _LOADABLE_SETTLE_NS for entry in key)
+ and _loadable_key(path, manifest) == key
+ ):
+ with _LOADABLE_PASSES_LOCK:
+ _LOADABLE_PASSES[cache_id] = key
+ _LOADABLE_PASSES.move_to_end(cache_id)
+ while len(_LOADABLE_PASSES) > _LOADABLE_PASSES_MAX:
+ _LOADABLE_PASSES.popitem(last = False)
+ return True
+
+
def _fully_loadable(path: Path, manifest: dict[str, Any]) -> bool:
"""Header scan is not enough; the required state and a real torch.load must also succeed."""
try:
_assert_required_state(path, manifest)
_assert_optimizer_buildable(path, manifest)
- _assert_loadable(path, manifest)
except ResumeError:
return False
- return True
+ return _assert_loadable_cached(path, manifest)
def _source_checkpoint_bundle(
diff --git a/studio/backend/tests/test_diffusion_checkpoint_resume.py b/studio/backend/tests/test_diffusion_checkpoint_resume.py
index eaf5332ce59..4d235809345 100644
--- a/studio/backend/tests/test_diffusion_checkpoint_resume.py
+++ b/studio/backend/tests/test_diffusion_checkpoint_resume.py
@@ -10,6 +10,7 @@ import time
import os
import json
from pathlib import Path
+from collections import OrderedDict
import pytest
import torch
@@ -2721,3 +2722,70 @@ def test_the_ema_decay_is_part_of_the_identity(run_dir):
with pytest.raises(dc.ResumeError, match = "EMA decay"):
dc.preflight_resume(path, identity = off, target_steps = 500)
assert dc.preflight_resume(path, identity = slow.identity, target_steps = 500)[1] == 4
+
+
+def test_the_run_listing_reads_an_unchanged_bundle_once(run_dir, monkeypatch):
+ monkeypatch.setattr(dc, "_LOADABLE_PASSES", OrderedDict())
+ monkeypatch.setattr(dc, "_LOADABLE_SETTLE_NS", -1)
+ reads = []
+ original = dc._assert_loadable
+
+ def _counting(path, manifest):
+ reads.append(path.name)
+ return original(path, manifest)
+
+ monkeypatch.setattr(dc, "_assert_loadable", _counting)
+ run = _Run(run_dir)
+ run.save(5)
+ first = dc.describe_resume_state(str(run_dir), status = "stopped")
+ for _ in range(3):
+ assert dc.describe_resume_state(str(run_dir), status = "stopped") == first
+ assert first["can_resume"] is True and first["checkpoint_step"] == 5
+ assert reads == ["checkpoint-5"]
+
+ run.step_once(0.5)
+ run.save(5)
+ assert dc.describe_resume_state(str(run_dir), status = "stopped")["can_resume"] is True
+ assert reads == ["checkpoint-5", "checkpoint-5"]
+
+ manifest = dc.read_checkpoint(run_dir / "checkpoint-5")
+ rng = run_dir / "checkpoint-5" / manifest["files"]["rng"]
+ torch.save({"not": "rng"}, rng)
+ assert dc.describe_resume_state(str(run_dir), status = "stopped")["can_resume"] is False
+
+
+def test_a_bundle_written_inside_the_settle_window_is_read_every_time(run_dir, monkeypatch):
+ monkeypatch.setattr(dc, "_LOADABLE_PASSES", OrderedDict())
+ reads = []
+ original = dc._assert_loadable
+ monkeypatch.setattr(
+ dc,
+ "_assert_loadable",
+ lambda path, manifest: (reads.append(1), original(path, manifest))[1],
+ )
+ _Run(run_dir).save(5)
+ for _ in range(2):
+ assert dc.describe_resume_state(str(run_dir), status = "stopped")["can_resume"] is True
+ assert len(reads) == 2
+
+
+def test_a_passing_failure_to_load_is_not_remembered(run_dir, monkeypatch):
+ monkeypatch.setattr(dc, "_LOADABLE_PASSES", OrderedDict())
+ monkeypatch.setattr(dc, "_LOADABLE_SETTLE_NS", -1)
+ _Run(run_dir).save(5)
+ real = dc.LoadedCheckpoint.tensors
+ fail = {"on": True}
+
+ def _flaky(
+ self,
+ role,
+ device = "cpu",
+ ):
+ if fail["on"]:
+ raise MemoryError("transient")
+ return real(self, role, device)
+
+ monkeypatch.setattr(dc.LoadedCheckpoint, "tensors", _flaky)
+ assert dc.describe_resume_state(str(run_dir), status = "stopped")["can_resume"] is False
+ fail["on"] = False
+ assert dc.describe_resume_state(str(run_dir), status = "stopped")["can_resume"] is True