Download tests/test_published_checkpoints.py from Celsia/torch-dimensions: direct link, hf CLI and curl.
- Browser
- Download file 4.72 kB
-
https://huggingface.co/Celsia/torch-dimensions/resolve/main/tests/test_published_checkpoints.py
- Command line
-
hf download hf://Celsia/torch-dimensions/tests/test_published_checkpoints.py
-
curl -L -o test_published_checkpoints.py https://huggingface.co/Celsia/torch-dimensions/resolve/main/tests/test_published_checkpoints.py
4.72 kB
| """The published benchmark checkpoints load, and load into the right model. | |
| `MPS bench/` and `CUDA bench/` are shipped in the repository and uploaded to the | |
| Hugging Face repo, so they are part of the published surface: someone will | |
| `torch.load` one and expect it to go into the model the manifest names. That | |
| makes them a compatibility contract, and an untested contract is a promise. | |
| The failure this guards against is quiet. Rename a submodule, change a mixer's | |
| internal layout, add a buffer — the library's own tests all still pass, and the | |
| only thing that breaks is every checkpoint published before the change. Loading | |
| with `strict=True` here turns that into a failing test on the commit that | |
| causes it rather than a bug report from someone who downloaded the weights. | |
| Skipped rather than failed when the directories are absent, since a source | |
| checkout without the artifacts is a legitimate way to work on the library. | |
| """ | |
| from __future__ import annotations | |
| import importlib.util | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import pytest | |
| import torch | |
| import torch.nn as nn | |
| ROOT = Path(__file__).resolve().parent.parent | |
| sys.path.insert(0, str(ROOT / "benchmarks")) | |
| spec = importlib.util.spec_from_file_location("_td_zoo", ROOT / "benchmarks" / "pretrain.py") | |
| pretrain = importlib.util.module_from_spec(spec) | |
| sys.modules["_td_zoo"] = pretrain | |
| spec.loader.exec_module(pretrain) | |
| RUNS = [d for d in ("MPS bench", "CUDA bench") if (ROOT / d / "manifest.json").exists()] | |
| def entries(run: str): | |
| manifest = json.loads((ROOT / run / "manifest.json").read_text()) | |
| return [e for e in manifest["models"] if "error" not in e] | |
| CASES = [(run, e["name"]) for run in RUNS for e in entries(run)] | |
| def test_the_checkpoint_loads_into_the_model_it_names(run, name): | |
| """`strict=True` on purpose: a checkpoint that only loads with | |
| `strict=False` has already silently stopped matching the architecture, and | |
| the missing tensors are whatever the model then leaves at its init.""" | |
| cfg = pretrain.ZOO[name] | |
| lat = cfg["lat"]() | |
| blob = torch.load(ROOT / run / name / "weights.pt", map_location="cpu", weights_only=True) | |
| torch.manual_seed(0) | |
| model = cfg["build"](lat) | |
| head = nn.Linear(model.config["d_model"], 1) | |
| model.load_state_dict(blob["model"]) | |
| head.load_state_dict(blob["head"]) | |
| # The manifest counts *parameters*; a state dict also carries buffers, and | |
| # S4's DPLR kernel has four elements of them. Comparing the two totals | |
| # directly reports a mismatch that is only bookkeeping. | |
| recorded = next(e for e in entries(run) if e["name"] == name)["n_params"] | |
| assert sum(p.numel() for p in model.parameters()) == recorded | |
| def test_the_loaded_checkpoint_runs(run, name): | |
| """Loading is not running: a state dict can be accepted and still leave a | |
| model that produces NaN on the machine doing the loading, which is the | |
| whole point of publishing weights trained on other hardware.""" | |
| cfg = pretrain.ZOO[name] | |
| lat = cfg["lat"]() | |
| blob = torch.load(ROOT / run / name / "weights.pt", map_location="cpu", weights_only=True) | |
| torch.manual_seed(0) | |
| model = cfg["build"](lat) | |
| head = nn.Linear(model.config["d_model"], 1) | |
| model.load_state_dict(blob["model"]) | |
| head.load_state_dict(blob["head"]) | |
| model, head = model.eval(), head.eval() | |
| x = torch.randn(2, 3, *lat.shape, 1) | |
| with torch.no_grad(): | |
| y = head(model(x)) | |
| assert y.shape == (2, 3, *lat.shape, 1) | |
| assert torch.isfinite(y).all() | |
| def test_the_two_runs_cover_the_same_models_and_say_what_they_ran_at(): | |
| """A comparison of two runs is only meaningful if they contain the same | |
| models, and only interpretable if each records the precision it used — | |
| CUDA's cuDNN TF32 default is on, and a run that does not say so cannot be | |
| compared with one that had it off.""" | |
| left, right = ({e["name"] for e in entries(r)} for r in RUNS) | |
| assert left == right, f"the runs differ: {sorted(left ^ right)}" | |
| for run in RUNS: | |
| manifest = json.loads((ROOT / run / "manifest.json").read_text()) | |
| assert "precision" in manifest, f"{run} does not record what precision it ran at" | |
| assert "weights_from" in entries(run)[0], ( | |
| f"{run} does not record where its weights came from" | |
| ) | |