torch.load refuses an old checkpoint with Weights only load failed
Your error
_pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint.3 more lines it can showFix it fast
Most common cause, 1 of 3The file holds a whole pickled model, saved with torch.save(model)
Only for a file you made or fully trust, load it once with weights_only=False, then save its state_dict.
import torch
from model import Net # the class the checkpoint was built from
full = torch.load("model.pt", weights_only=False) # trusted file only
torch.save(full.state_dict(), "model_state.pt")
net = Net()
net.load_state_dict(torch.load("model_state.pt"))Short answer
_pickle.UnpicklingError: Weights only load failed appears because PyTorch 2.6 switched torch.load to weights_only=True, and that loader refuses pickled classes such as a whole nn.Module or NumPy types. For a file you trust, pass weights_only=False. Otherwise allowlist the reported globals with torch.serialization.safe_globals. Applies to PyTorch 2.6 through 2.15.
Which cause do you have?
Ask PyTorch which classes and functions the file wants to rebuild. It reads the pickle statically, so nothing in the file runs. Your own model class in the list means cause 1, NumPy or other plain types mean cause 2, and an error raised inside a library's code rather than yours means cause 3.
python -c "import torch; print(torch.serialization.get_unsafe_globals_in_checkpoint('model.pt'))"Then find the line you see in this list and open its fix:
- 1The file holds a whole pickled model, saved with torch.save(model)
The unsafe list names your own model class, for example __main__.Net or models.resnet.Net - 2The checkpoint also stores NumPy values or other small helper objects
The unsafe list names numpy entries rather than your model class, or the error reads Can only build Tensor, Parameter, OrderedDict and ends with a NumPy dtype class - 3A library calls torch.load for you without passing weights_only
The traceback ends inside an installed package's loading code, not in a torch.load line you wrote
What does the error look like?
_pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to do so you have two options, do those steps only if you trust the source of the checkpoint.(1) In PyTorch 2.6, we changed the default value of the `weights_only` argument in `torch.load` from `False` to `True`.WeightsUnpickler error: Unsupported global: GLOBAL __main__.Net was not an allowed global by default.WeightsUnpickler error: Can only build Tensor, Parameter, OrderedDict or types allowlisted via `add_safe_globals`, but got <class 'numpy.dtypes.Float32DType'>
Cause 1 of 3
The file holds a whole pickled model, saved with torch.save(model)
You seeThe unsafe list names your own model class, for example __main__.Net or models.resnet.Net
Check
Weights only load failed here because the checkpoint stores a complete nn.Module object, not just tensors. The weights-only unpickler builds tensors, state dicts and a few primitive types, and PyTorch's own notes recommend weights_only=False for old checkpoints that contain an nn.Module.
python -c "import torch; print(torch.serialization.get_unsafe_globals_in_checkpoint('model.pt'))"Fix
Only for a file you made or fully trust, load it once with weights_only=False, then save its state_dict. Future loads of that new file work under the default and need only your model class to receive the weights.
import torch
from model import Net # the class the checkpoint was built from
full = torch.load("model.pt", weights_only=False) # trusted file only
torch.save(full.state_dict(), "model_state.pt")
net = Net()
net.load_state_dict(torch.load("model_state.pt"))Verify
The last load runs with the default settings, and the unsafe list for model_state.pt is empty.
python -c "import torch; print(torch.serialization.get_unsafe_globals_in_checkpoint('model_state.pt'))"Confirmed by docs.pytorch.org
Cause 2 of 3
The checkpoint also stores NumPy values or other small helper objects
You seeThe unsafe list names numpy entries rather than your model class, or the error reads Can only build Tensor, Parameter, OrderedDict and ends with a NumPy dtype class
Check
Training scripts often save NumPy values, such as a best score or a dtype, beside the weights. Each one is a pickle global outside the default allowlist. The static check can miss NumPy dtypes because they are built while unpickling, so the Can only build message may show up after the first list is handled.
python -c "import torch; print(torch.serialization.get_unsafe_globals_in_checkpoint('ckpt.pt'))"Fix
Import exactly the types you trust and pass them to the safe_globals context manager, which keeps weights_only=True on for everything else. With NumPy 1.25 and later, dtype classes live under numpy.dtypes. Add each name the unsafe list reported in the same way.
import numpy as np
import torch
with torch.serialization.safe_globals([np.dtypes.Float32DType]):
ckpt = torch.load("ckpt.pt")
print(list(ckpt)[:5])Verify
The script prints the first keys of the checkpoint instead of raising UnpicklingError.
python load_ckpt.pyConfirmed by docs.pytorch.org
Cause 3 of 3
A library calls torch.load for you without passing weights_only
You seeThe traceback ends inside an installed package's loading code, not in a torch.load line you wrote
Check
Code written before PyTorch 2.6 relied on the old default and never passes the argument, so upgrading torch alone makes the library fail on its own checkpoints. Since its call site is not yours, you cannot add weights_only there.
python -c "import torch; print(torch.__version__)"Fix
Look for a newer release of the library whose own call passes weights_only. If none exists and the files are trusted, set TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1 for that one process. It changes only calls that leave weights_only unset.
TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1 python run_inference.pyVerify
The script loads its checkpoint and prints a UserWarning that the variable forced weights_only=False.
TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1 python run_inference.py 2>&1 | grep -i weights_onlyConfirmed by docs.pytorch.org
How do I stop it coming back?
- Save model.state_dict() rather than the model object, and keep extras as plain dicts, lists, numbers and tensors.
- Add a test that loads every shipped checkpoint with the default torch.load, so a pickled object fails in CI instead of in production.
- Run get_unsafe_globals_in_checkpoint on checkpoints downloaded from outside before deciding whether to trust them.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Raheeb, Loade Driver, Dhero.