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: list a crashed or cancelled run's saved checkpoints on the Export page (#11600)

Nilay7236e91a399d
diff --git a/studio/backend/tests/test_checkpoints_scan.py b/studio/backend/tests/test_checkpoints_scan.py
index 6d473146f5d..530dea620ee 100644
--- a/studio/backend/tests/test_checkpoints_scan.py
+++ b/studio/backend/tests/test_checkpoints_scan.py
@@ -254,3 +254,98 @@ def test_scan_checkpoints_prefers_exact_history_match_over_newer_suffix(tmp_path
models = checkpoints_module.scan_checkpoints(outputs_dir = str(outputs_dir))
assert models[0][2]["base_model"] == "correct/base"
+
+
+def test_scan_checkpoints_lists_run_with_only_intermediate_checkpoints(tmp_path, monkeypatch):
+ outputs_dir = _make_outputs_dir(tmp_path, monkeypatch)
+ run_dir = outputs_dir / "run_x"
+ for step, loss, adapter_config in (
+ (50, 0.75, {}),
+ (100, 0.5, {"base_model_name_or_path": "unsloth/X", "peft_type": "LORA", "r": 16}),
+ ):
+ checkpoint_dir = run_dir / f"checkpoint-{step}"
+ checkpoint_dir.mkdir(parents = True)
+ (checkpoint_dir / "adapter_config.json").write_text(json.dumps(adapter_config))
+ (checkpoint_dir / "trainer_state.json").write_text(
+ json.dumps({"log_history": [{"loss": loss}]})
+ )
+ partial_dir = outputs_dir / "run_partial" / "tmp-checkpoint-5"
+ partial_dir.mkdir(parents = True)
+ (partial_dir / "adapter_config.json").write_text("{}")
+
+ models = checkpoints_module.scan_checkpoints(outputs_dir = str(outputs_dir))
+
+ assert models == [
+ (
+ "run_x",
+ [
+ ("checkpoint-100", str(run_dir / "checkpoint-100"), 0.5),
+ ("checkpoint-50", str(run_dir / "checkpoint-50"), 0.75),
+ ],
+ {"base_model": "unsloth/X", "peft_type": "LORA", "lora_rank": 16},
+ )
+ ]
+
+ targets = checkpoints_module.list_preview_targets(str(outputs_dir))
+
+ assert [(t["ref"], t["is_latest"]) for t in targets] == [
+ ("run_x/checkpoint-100", False),
+ ("run_x/checkpoint-50", False),
+ ]
+
+
+def test_scan_checkpoints_skips_unreadable_folder(tmp_path, monkeypatch):
+ outputs_dir = _make_outputs_dir(tmp_path, monkeypatch)
+ run_dir = outputs_dir / "run_done"
+ run_dir.mkdir()
+ (run_dir / "adapter_config.json").write_text("{}")
+ (outputs_dir / "locked").mkdir()
+ (outputs_dir / "locked_checkpoint" / "checkpoint-100").mkdir(parents = True)
+ real_iterdir, real_exists = Path.iterdir, Path.exists
+
+ def iterdir(self):
+ if self.name == "locked":
+ raise PermissionError(13, "Permission denied", str(self))
+ return real_iterdir(self)
+
+ def exists(self, *args, **kwargs):
+ if self.parent.name in ("locked", "checkpoint-100"):
+ raise PermissionError(13, "Permission denied", str(self))
+ return real_exists(self, *args, **kwargs)
+
+ monkeypatch.setattr(Path, "iterdir", iterdir)
+ monkeypatch.setattr(Path, "exists", exists)
+
+ models = checkpoints_module.scan_checkpoints(outputs_dir = str(outputs_dir))
+
+ assert [name for name, _checkpoints, _metadata in models] == ["run_done"]
+
+
+def test_scan_checkpoints_keeps_root_adapter_first(tmp_path, monkeypatch):
+ outputs_dir = _make_outputs_dir(tmp_path, monkeypatch)
+ run_dir = outputs_dir / "run_root"
+ run_dir.mkdir()
+ (run_dir / "adapter_config.json").write_text(
+ json.dumps({"base_model_name_or_path": "unsloth/X", "peft_type": "LORA", "r": 16})
+ )
+ for step, loss in ((50, 0.25), (100, 0.125)):
+ checkpoint_dir = run_dir / f"checkpoint-{step}"
+ checkpoint_dir.mkdir()
+ (checkpoint_dir / "adapter_config.json").write_text("{}")
+ (checkpoint_dir / "trainer_state.json").write_text(
+ json.dumps({"log_history": [{"loss": loss}]})
+ )
+
+ models = checkpoints_module.scan_checkpoints(outputs_dir = str(outputs_dir))
+
+ assert models == [
+ (
+ "run_root",
+ [
+ ("run_root", str(run_dir), 0.125),
+ ("checkpoint-100", str(run_dir / "checkpoint-100"), 0.125),
+ ("checkpoint-50", str(run_dir / "checkpoint-50"), 0.25),
+ ],
+ {"base_model": "unsloth/X", "peft_type": "LORA", "lora_rank": 16},
+ )
+ ]
diff --git a/studio/backend/utils/models/checkpoints.py b/studio/backend/utils/models/checkpoints.py
index 8c608277368..94184122515 100644
--- a/studio/backend/utils/models/checkpoints.py
+++ b/studio/backend/utils/models/checkpoints.py
@@ -143,6 +143,31 @@ def _read_checkpoint_loss(checkpoint_path: Path) -> Optional[float]:
return None
+# Both probe every outputs folder, so an unreadable one is skipped, not fatal to the scan.
+def _has_own_model(path: Path) -> bool:
+ try:
+ return own_entry(path / "config.json") or own_entry(path / "adapter_config.json")
+ except OSError:
+ return False
+
+
+def _checkpoint_dirs(run_dir: Path) -> List[Path]:
+ try:
+ return sorted(
+ (
+ sub
+ for sub in run_dir.iterdir()
+ if sub.is_dir()
+ and sub.name.startswith("checkpoint-")
+ and within_account(sub)
+ and _has_own_model(sub)
+ ),
+ key = _checkpoint_sort_key,
+ )
+ except OSError:
+ return []
+
+
def scan_checkpoints(
outputs_dir: str | None = None,
) -> List[Tuple[str, List[Tuple[str, str, Optional[float]]], dict]]:
@@ -151,8 +176,8 @@ def scan_checkpoints(
Returns:
[(model_name, [(display_name, checkpoint_path, loss), ...], metadata), ...]
metadata keys (optional): base_model, peft_type, lora_rank.
- First checkpoint entry is the main adapter; its loss mirrors the latest
- (highest-step) intermediate checkpoint. Numbered checkpoints are sorted
+ First entry is the main adapter (loss mirrors the highest-step checkpoint)
+ only when the run has a final save. Numbered checkpoints are sorted
by numeric step descending; non-numbered checkpoint-* dirs keep the
previous lexicographic directory order.
"""
@@ -172,12 +197,16 @@ def scan_checkpoints(
if not within_account(item):
continue
- config_file = item / "config.json"
- adapter_config = item / "adapter_config.json"
-
- if not (own_entry(config_file) or own_entry(adapter_config)):
+ has_root_model = _has_own_model(item)
+ valid_checkpoints = _checkpoint_dirs(item)
+ # A cancelled or crashed run has no final save but can still have checkpoints.
+ if not has_root_model and not valid_checkpoints:
continue
+ meta_dir = item if has_root_model else valid_checkpoints[0]
+ config_file = meta_dir / "config.json"
+ adapter_config = meta_dir / "adapter_config.json"
+
# Training metadata from adapter_config.json / config.json
metadata: dict = {}
try:
@@ -218,39 +247,12 @@ def scan_checkpoints(
else:
metadata["base_model"] = name_part
- # Valid training run.
- checkpoints = []
-
- # Main adapter placeholder — loss filled from the last checkpoint below.
- checkpoints.append((item.name, str(item), None))
-
- # Scan for intermediate checkpoints (checkpoint-N subdirs).
- valid_checkpoints = []
- for sub in item.iterdir():
- if not sub.is_dir() or not sub.name.startswith("checkpoint-"):
- continue
- if not within_account(sub):
- continue
- sub_config = sub / "config.json"
- sub_adapter = sub / "adapter_config.json"
- if own_entry(sub_config) or own_entry(sub_adapter):
- valid_checkpoints.append(sub)
-
- intermediate_checkpoints = []
- for sub in sorted(valid_checkpoints, key = _checkpoint_sort_key):
- loss = _read_checkpoint_loss(sub)
- intermediate_checkpoints.append((sub.name, str(sub), loss))
-
- checkpoints.extend(intermediate_checkpoints)
-
- # Assign the latest checkpoint's loss to the main adapter entry.
- if intermediate_checkpoints:
- last_checkpoint_loss = intermediate_checkpoints[0][2]
- checkpoints[0] = (
- checkpoints[0][0],
- checkpoints[0][1],
- last_checkpoint_loss,
- )
+ checkpoints = [
+ (sub.name, str(sub), _read_checkpoint_loss(sub)) for sub in valid_checkpoints
+ ]
+ if has_root_model:
+ latest_loss = checkpoints[0][2] if checkpoints else None
+ checkpoints.insert(0, (item.name, str(item), latest_loss))
models.append((item.name, checkpoints, metadata))
logger.debug(f"Found model: {item.name} with {len(checkpoints)} checkpoint(s)")