ultralytics YOLO: Weights only load failed (fix)
On torch 2.6 and newer, YOLO("yolov8n.pt") with an old ultralytics release fails with _pickle.UnpicklingError: Weights only load failed and Unsupported global: GLOBAL ultralytics.nn.tasks.DetectionModel. Tested fixes: upgrade ultralytics to 8.2.103 or later, or set TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1.
1. The error
ultralytics 8.0.196 (a common pinned version in older YOLOv8 projects) with torch 2.14.1, loading the stock yolov8n.pt:
import torch, ultralytics
print("torch", torch.__version__, "| ultralytics", ultralytics.__version__)
from ultralytics import YOLO
try:
model = YOLO("yolov8n.pt")
except Exception as e:
msg = str(e).splitlines()
print(type(e).__module__ + "." + type(e).__name__ + ":", msg[0][:60] + "...")
print([l.strip() for l in msg if "Unsupported global" in l][0][:110] + "...")
torch 2.14.1+cpu | ultralytics 8.0.196
_pickle.UnpicklingError: Weights only load failed. This file can still be loaded, to ...
WeightsUnpickler error: Unsupported global: GLOBAL ultralytics.nn.tasks.DetectionModel was not an allowed glob...
Old ultralytics calls torch.load(file, map_location='cpu') without weights_only. Since torch 2.6 that argument defaults to True, and a YOLO .pt file is a pickled DetectionModel object, not a plain state_dict, so the safe unpickler refuses it.
2. Fix: upgrade ultralytics
Newer releases wrap torch.load and pass weights_only=False themselves. In our test the same file loaded on 8.2.103 (2024-09-28) and on the current release:
pip install -U ultralytics
import torch, ultralytics
from ultralytics import YOLO
model = YOLO("yolov8n.pt") # same file, no env var
r = model("bus.jpg", verbose=False)[0]
print("torch", torch.__version__, "| ultralytics", ultralytics.__version__, "| detections:", len(r.boxes))
torch 2.14.1+cpu | ultralytics 8.4.185 | detections: 6
If your project pins ultralytics==8.0.* or 8.1.*, raise the pin to at least 8.2.103, the oldest release we confirmed loads the file.
3. Fix without upgrading: TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1
If you cannot touch the ultralytics version (a frozen repo, a tool that bundles it), torch honours an environment variable that restores the old default when weights_only is not passed. Set it before the load, in code or in the shell (export TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1):
import os
os.environ["TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD"] = "1" # must be set before the load
import ultralytics
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
r = model("bus.jpg", verbose=False)[0]
print("ultralytics", ultralytics.__version__, "| detections:", len(r.boxes))
.../site-packages/ultralytics/nn/tasks.py:567: UserWarning: Environment variable TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD detected, since the`weights_only` argument was not explicitly passed to `torch.load`, forcing weights_only=False.
return torch.load(file, map_location='cpu'), file # load
ultralytics 8.0.196 | detections: 6
Same 8.0.196 install, same file: it loads and detects with a UserWarning. A third option is pinning torch<2.6, where the default was still False.
Only for files you trust
Both fixes turn full unpickling back on, which can run arbitrary code from the file. That is fine for official ultralytics weights or your own training runs; for .pt files from unknown sources, don't. The general torch background and the add_safe_globals route are in torch.load: Weights only load failed; the same error after fixing pkg_resources in the old yolov5 package is covered in yolov5: No module named pkg_resources.
Frequently asked questions
Which ultralytics version fixed it?
In our test 8.0.196 failed and 8.2.103 (2024-09-28) and 8.4.185 loaded the same yolov8n.pt on torch 2.14.1.
Is TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD=1 safe?
Only for weight files you trust. It turns full unpickling back on, which can run code stored in the file.
Written with AI assistance. Measured on 2026-10-24 on Linux with torch 2.14.1+cpu, CPython 3.11.17, ultralytics 8.0.196, 8.2.103 and 8.4.185. Output pasted unedited except shortened site-packages paths. PlainTables is our extension; ultralytics and PyTorch are free.