Skip to content

PyTorch 2.6.0: Weights only load failed #497

@hongbo-miao

Description

@hongbo-miao

When upgrade from PyTorch 2.5.1 to 2.6.0, for code like

from ogb.graphproppred import PygGraphPropPredDataset

dataset = PygGraphPropPredDataset(name = 'ogbg-molhiv')

I got error:

Traceback (most recent call last):
  File "/home/runner/work/hongbomiao.com/hongbomiao.com/machine-learning/graph-neural-network/src/main.py", line 103, in main
    dataset, split_idx = fetch_dataset(config)
                         ^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/hongbomiao.com/hongbomiao.com/machine-learning/graph-neural-network/src/model/data_loader.py", line 13, in fetch_dataset
    dataset = PygGraphPropPredDataset(name=config.dataset)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/hongbomiao.com/hongbomiao.com/machine-learning/graph-neural-network/.venv/lib/python3.12/site-packages/ogb/graphproppred/dataset_pyg.py", line 68, in __init__
    self.data, self.slices = torch.load(self.processed_paths[0])
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/hongbomiao.com/hongbomiao.com/machine-learning/graph-neural-network/.venv/lib/python3.12/site-packages/torch/serialization.py", line 1470, in load
    raise pickle.UnpicklingError(_get_wo_message(str(e))) from None
_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 torch_geometric.data.data.DataEdgeAttr was not an allowed global by default. Please use `torch.serialization.add_safe_globals([DataEdgeAttr])` or the `torch.serialization.safe_globals([DataEdgeAttr])` context manager to allowlist this global if you trust this class/function.

I fixed by https://github.com/hongbo-miao/hongbomiao.com/pull/23546/files

from torch_geometric.data.data import DataEdgeAttr, DataTensorAttr
from torch_geometric.data.storage import GlobalStorage

torch.serialization.add_safe_globals([DataEdgeAttr, DataTensorAttr, GlobalStorage])

I am wondering if the library should handle this issue in a future version.

What would be the recommended approach? Thank you! ☺️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions