torch.load: Weights only load failed (fix)

Since torch 2.6, torch.load defaults to weights_only=True, so loading a file saved with torch.save(model) fails with _pickle.UnpicklingError: Weights only load failed. Tested fixes: weights_only=False for trusted files, torch.serialization.add_safe_globals, or save a state_dict.

1. The error

A model class in net.py, saved two ways:

import torch

class Net(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.fc = torch.nn.Linear(4, 2)
import torch
from net import Net

torch.save(Net(), "model.pt")               # whole model object
torch.save(Net().state_dict(), "weights.pt")  # weights only
print("saved")

Loading the whole-model file with the default arguments on torch 2.14.1:

import torch

model = torch.load("model.pt")
Traceback (most recent call last):
...
_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`. Re-running `torch.load` with `weights_only` set to `False` will likely succeed, but it can result in arbitrary code execution. Do it only if you got the file from a trusted source.
	(2) Alternatively, to load with `weights_only=True` please check the recommended steps in the following error message.
	WeightsUnpickler error: Unsupported global: GLOBAL net.Net was not an allowed global by default. Please use `torch.serialization.add_safe_globals([net.Net])` or the `torch.serialization.safe_globals([net.Net])` context manager to allowlist this global if you trust this class/function.

Since torch 2.6, torch.load defaults to weights_only=True. That mode only unpickles tensors, plain containers and an allowlist of torch types, so a file holding a full nn.Module object (your class net.Net) is rejected. Files written before 2.6 with torch.save(model) hit this too.

2. Fix for a file you trust: weights_only=False

import torch

model = torch.load("model.pt", weights_only=False)  # only for files you trust
print(type(model).__name__)
Net

This is the old behaviour. Unpickling with weights_only=False can run arbitrary code stored in the file, so use it only for checkpoints you created or got from a source you trust. If the torch.load call is inside a library you cannot edit, the environment variable TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1 has the same effect for calls that do not pass weights_only; it printed UserWarning: Environment variable TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD detected ... forcing weights_only=False. and loaded the model.

3. Safer: allowlist the classes the error names

import torch
from net import Net

torch.serialization.add_safe_globals([Net, torch.nn.Linear])
model = torch.load("model.pt")
print(type(model).__name__)
Net

This keeps weights_only=True and trusts only the classes you list. The error names one class at a time: with only Net listed, it next reported torch.nn.modules.linear.Linear. Add each one it reports. torch.serialization.safe_globals([...]) does the same inside a with block.

4. Best long-term: save a state_dict

import torch
from net import Net

model = Net()
model.load_state_dict(torch.load("weights.pt"))
print("loaded", list(model.state_dict()))
loaded ['fc.weight', 'fc.bias']

A state_dict file contains only tensors, so the default torch.load accepts it with no flags. Load an old full-model file once with option 2, then re-save it with torch.save(model.state_dict(), ...).

Related: ultralytics YOLO (old releases) · yolov5 (hits this after the setuptools fix) · torch: Numpy is not available · Torch not compiled with CUDA enabled.

Frequently asked questions

Is weights_only=False safe?

Only for files you trust. It unpickles arbitrary objects, which can run code stored in the file.

Which torch version changed this?

torch 2.6 changed the default of weights_only in torch.load from False to True, as the error message itself says.

Written with AI assistance. Measured on 2026-10-24 on Linux with torch 2.14.1+cpu and CPython 3.11.17. Output pasted unedited except a cut traceback. PlainTables is our extension; PyTorch is free.