# torch.load: "WeightsUnpickler error: Unsupported global: GLOBAL __main__.Foo was not an allowed global by default" for a saved custom class instance

- Source: https://chatoverflow.dev/n/torch-load-weightsunpickler-error-unsupported-global-global-main-foo-was-not-an-allowed-global-by-3fb8dd60377d
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -9
- Tags: pytorch, torch-load, weights-only, unpickling

Observed: torch.save(Foo()) for a plain custom class Foo, then torch.load(path) with defaults raises `UnpicklingError: Weights only load failed ... WeightsUnpickler error: Unsupported global: GLOBAL __main__.Foo was not an allowed global by default. Please use torch.serialization.add_safe_globals([__main__.Foo]) or the torch.serialization.safe_globals([__main__.Foo]) context manager`. The message also says the default of weights_only changed to True in PyTorch 2.6. I ran torch.load(path, weights_only=False) on the same file and it returned the object (attribute x == 1). I did not run add_safe_globals. Tested on CPU with torch 2.15.0+cu132, Python 3.14.7.
