From d9a008e0a7d091cd8d18cb297ddcf57dac75cf92 Mon Sep 17 00:00:00 2001 From: AUTHENSOR Date: Wed, 14 Oct 2026 22:16:41 -0700 Subject: [PATCH] fix(infra): rank checkpoint names without crashing on stepless or .pt files fetch_latest_checkpoint_name promised .pth support but raised ValueError on int('model') whenever a stepless name shared the list with numbered checkpoints, and again on .pt files (int('100.pt')) that the W&B caller's own (".pth", ".pt") filter feeds it. Rank stepless names as -1 so numbered checkpoints win, resolve all-stepless sets lexicographically, and parse both suffixes. --- param_decomp/infra/wandb.py | 21 ++++++-- .../test_fetch_latest_checkpoint_name.py | 51 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 param_decomp/tests/infra/test_fetch_latest_checkpoint_name.py diff --git a/param_decomp/infra/wandb.py b/param_decomp/infra/wandb.py index b7ea4bf8f..e4a140762 100644 --- a/param_decomp/infra/wandb.py +++ b/param_decomp/infra/wandb.py @@ -98,10 +98,25 @@ 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 `_`; `-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 _.pth or .pth. + Assumes format is `_.pth` (or `.pt`) or `.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)] @@ -109,7 +124,7 @@ def fetch_latest_checkpoint_name(filenames: list[str], prefix: str | None = None 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: diff --git a/param_decomp/tests/infra/test_fetch_latest_checkpoint_name.py b/param_decomp/tests/infra/test_fetch_latest_checkpoint_name.py new file mode 100644 index 000000000..fb566655e --- /dev/null +++ b/param_decomp/tests/infra/test_fetch_latest_checkpoint_name.py @@ -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_")