OSError:无法从缓存加载模型:'pytorch_model.bin' 文件哈希不匹配。期望 sha256:abc123... 但得到 xyz789...
OSError: Unable to load model from cache: file hash mismatch for 'pytorch_model.bin'. Expected sha256: abc123... but got xyz789...
ID: huggingface/transformers-cache-corruption
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| transformers>=4.20.0 | active | — | — | — |
| huggingface_hub>=0.12.0 | active | — | — | — |
根因分析
缓存的模型文件损坏(例如由于下载不完整、磁盘错误或并发写入),导致完整性验证期间 SHA-256 哈希不匹配。
English
The cached model file is corrupted (e.g., due to incomplete download, disk errors, or concurrent write) causing a SHA-256 hash mismatch during integrity verification.
官方文档
https://huggingface.co/docs/huggingface_hub/en/guides/manage-cache解决方案
-
清除整个缓存目录:rm -rf ~/.cache/huggingface/hub/;然后使用 from_pretrained() 重新加载模型。这会强制重新下载。
-
使用 huggingface-cli 选择性清除缓存:huggingface-cli delete-cache;或设置 HF_HUB_ENABLE_HF_TRANSFER=1 以使用稳健的下载器。
无效尝试
常见但无效的做法:
-
Delete only the specific corrupted file and re-run the script
60% 失败
The cache may still contain partial or corrupted metadata; the download might resume from a broken state.
-
Set `torch.hub.set_dir()` to a different directory and reload
75% 失败
This changes the download location but does not fix the corruption in the original cache; the error persists if the same cache is used.
-
Ignore the hash check by setting `local_files_only=False`
50% 失败
This forces a re-download but does not delete the corrupted file; the download may fail again or use the same broken file.