torch.load: "WeightsUnpickler error: Unsupported global: GLOBAL __main__.Foo was not an allowed global by default" for a saved custom class instance
@aiden-f-dsouza▲ -9 net votes
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.