Skip to content

Fix .safetensors detection for local ResNet50 checkpoints (str weights_path) - #238

Merged
guillaumejaume merged 1 commit into
mahmoodlab:mainfrom
SharoonSharif:fix/safetensors-str-weights-path
Sep 15, 2026
Merged

guillaumejaume merged 1 commit into
mahmoodlab:mainfrom
SharoonSharif:fix/safetensors-str-weights-path

Conversation

@SharoonSharif

Copy link
Copy Markdown
Contributor

Fixes #172.

Problem. ResNet50InferenceEncoder._build checks weights_path.suffix == ".safetensors", but _get_weights_path() returns a str (from local_ckpts.json or the weights_path kwarg), so any local ResNet50 checkpoint raises AttributeError: 'str' object has no attribute 'suffix' inside the bare except. The user then sees the misleading "Failed to create ResNet50 model from local checkpoint … download pytorch_model.bin" error even though the checkpoint is valid. Reported and diagnosed by @haydenych and @PingjunChen in #172.

Fix. One line: str(weights_path).endswith(".safetensors"). Works for str and pathlib.Path inputs and leaves the torch.load branch unchanged. Also removes a stray leading space in the adjacent error message (` model.safetensors`).

Test. tests/test_resnet50_local_checkpoint.py (fast tier: CPU only, no network, ~2 s). It builds an untrained timm resnet50 with the same kwargs as _build, stamps conv1.weight with a probe value, saves the state dict as model.safetensors and as pytorch_model.bin, loads each through encoder_factory("resnet50", weights_path=<str>), and asserts the probe tensor is present in the encoder, so the test proves the file was actually loaded rather than silently skipped by strict=False.

Without the fix both cases error with the AttributeError above; with the fix both pass.

Verification. Windows 11, Python 3.12, torch 2.14 (CPU), timm 1.0.29, safetensors 0.8.0:

python -m unittest tests.test_resnet50_local_checkpoint -v
Ran 2 tests in 1.727s
OK

python -m unittest discover -s tests -p "test_*.py"
Ran 150 tests — 149 pass, 74 skipped (gated integration tests), 1 failure

The single failure (test_wsi_states_v2.TestWSIStatesV2.test_output_stats_added_for_paths) reproduces identically on a clean main checkout on Windows and is unrelated to this change.

Out of scope on purpose (happy to follow up separately): the bare except: clauses in load.py, and the other encoders that call torch.load directly.

_get_weights_path() returns a str, so weights_path.suffix raised
AttributeError inside the bare except and surfaced the misleading
"download pytorch_model.bin" error for valid .safetensors files.
Use str(weights_path).endswith(".safetensors") and add a fast-tier
regression test that loads .safetensors and .bin state dicts from a
string path.

Fixes mahmoodlab#172
@guillaumejaume
guillaumejaume merged commit e4c98b2 into mahmoodlab:main Sep 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] torch.load fails on .safetensors checkpoints in PyTorch 2.6+

2 participants