Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions param_decomp/infra/wandb.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,33 @@ def parse_wandb_run_path(input_path: str) -> tuple[str, str, str]:
)


def _checkpoint_step(filename: str) -> int:
"""Rank a checkpoint filename by its trailing `_<step>`; `-1` when there is none.

Handles both `.pth` and `.pt` (the W&B caller accepts either) and treats a
non-integer trailing segment (e.g. `model.pth`, `model_final.pth`) as stepless.
"""
stem = (
filename.rsplit(".pth", 1)[0] if filename.endswith(".pth") else filename.rsplit(".pt", 1)[0]
)
tail = stem.rsplit("_", 1)[-1]
return int(tail) if tail.isdigit() else -1


def fetch_latest_checkpoint_name(filenames: list[str], prefix: str | None = None) -> str:
"""Fetch the latest checkpoint name from a list of .pth files.
"""Fetch the latest checkpoint name from a list of `.pth`/`.pt` files.

Assumes format is <name>_<step>.pth or <name>.pth.
Assumes format is `<name>_<step>.pth` (or `.pt`) or `<name>.pth`. Files whose
trailing segment is not an integer rank as stepless (`-1`), so any numbered
checkpoint outranks them and an all-stepless set resolves lexicographically.
"""
if prefix:
filenames = [filename for filename in filenames if filename.startswith(prefix)]
if not filenames:
raise ValueError(f"No files found with prefix {prefix}")
if len(filenames) == 1:
return filenames[0]
return sorted(filenames, key=lambda x: int(x.split(".pth")[0].split("_")[-1]))[-1]
return sorted(filenames, key=lambda x: (_checkpoint_step(x), x))[-1]


def fetch_latest_wandb_checkpoint(run: Run, prefix: str | None = None) -> File:
Expand Down
51 changes: 51 additions & 0 deletions param_decomp/tests/infra/test_fetch_latest_checkpoint_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest

from param_decomp.infra.wandb import fetch_latest_checkpoint_name


@pytest.mark.parametrize(
"filenames, expected",
[
(["model_10.pth", "model_2.pth", "model_100.pth"], "model_100.pth"),
(["model_2.pth", "model_10.pth"], "model_10.pth"), # numeric, not lexicographic
# The docstring's second supported format among numbered files used to
# raise ValueError: int("model").
(["model.pth", "model_100.pth"], "model_100.pth"),
(["model.pth", "model_5.pth", "model_50.pth"], "model_50.pth"),
# `.pt` files come through the W&B caller's (".pth", ".pt") filter;
# int("100.pt") used to raise.
(["model_10.pt", "model_100.pt"], "model_100.pt"),
(["model_100.pth", "model_200.pt"], "model_200.pt"),
# Word suffixes are stepless, never a crash.
(["model_final.pth", "model_7.pth"], "model_7.pth"),
# All stepless: deterministic lexicographic resolution.
(["beta.pth", "alpha.pth"], "beta.pth"),
# A lone file is returned as-is whatever it looks like.
(["model.pth"], "model.pth"),
(["model_final.pth"], "model_final.pth"),
],
ids=[
"max-step",
"numeric-not-lexicographic",
"bare-name-among-numbered",
"bare-name-lowest-rank",
"pt-suffixes",
"mixed-pth-pt",
"word-suffix-stepless",
"all-stepless-lexicographic",
"single-bare",
"single-word-suffix",
],
)
def test_fetch_latest_checkpoint_name(filenames: list[str], expected: str) -> None:
assert fetch_latest_checkpoint_name(filenames) == expected


def test_prefix_filters_before_ranking() -> None:
filenames = ["other_999.pth", "model_10.pth", "model_100.pth"]
assert fetch_latest_checkpoint_name(filenames, prefix="model_") == "model_100.pth"


def test_no_matches_raises() -> None:
with pytest.raises(ValueError, match="No files found with prefix"):
fetch_latest_checkpoint_name(["model_1.pth"], prefix="absent_")
Loading