Skip to content

Issue: UnpicklingError when loading model checkpoint with torch.load in PyTorch 2.6 #61

@Vans-Lee

Description

@Vans-Lee

Description: I encountered an UnpicklingError while loading a model checkpoint with the following code:
checkpoint = torch.load('model_checkpoint.pth') model.load_state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
The error suggests that torch.load has changed its default behavior in PyTorch 2.6. Specifically, the weights_only parameter is now set to True by default, which prevents loading the full checkpoint file (including the model and optimizer state dictionaries).

Proposed Solution:
To resolve this issue, you can explicitly set the weights_only parameter to False when loading the checkpoint, which will load the full checkpoint including both the model and optimizer state dictionaries. However, please note that this could potentially allow execution of arbitrary code, so make sure to trust the source of the checkpoint file.

checkpoint = torch.load('model_checkpoint.pth', weights_only=False) model.load_state_dict(checkpoint['model_state_dict']) optimizer.load_state_dict(checkpoint['optimizer_state_dict'])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions