Skip to content

torch.load planned default flip for weights_only  #7799

@mikaylagawarecki

Description

@mikaylagawarecki

TL;DR

PyTorch is planning a BC-breaking change in torch.load to flip the default for weights_only from None (i.e. False) to True (and have added a warning to this effect in torch 2.4 :) ) that will break loading of tensor serialized when on XLA.

Context

Instead of using the default Unpickler provided by pickle, torch.load(weights_only=True) uses a custom Unpickler that restricts the allowed GLOBALs in the checkpoint (classes and functions) to those here (that required to build state_dicts).

The purpose of this is towards addressing the issue of remote code execution when using torch.load.

Another feature of this is that users can allowlist certain globals using add_safe_globals (in torch 2.4) or the safe_globals context manager (in torch nightly), a simple example being

import torch
from torch.serialization import safe_globals

class MyTensor(...):
     pass
 
 t = MyTensor(torch.randn(2, 3))    
 torch.save(t, "ckpt.pt")
 
 # This fails saying that MyTensor is not an allowed GLOBAL
 # t1 = torch.load("ckpt.pt", weights_only=True)
 
 # This succeeds
 with safe_globals([MyTensor]):
     torch.load("ckpt.pt", weights_only=True)

How this affects XLA

Notably, XLA uses a special path that uses numpy for serialization/deserialization see here. However, we have made a decision not to include the numpy GLOBALS required for unpickling in the defaut list as we do not control the codepaths numpy implements for pickling (see relevant GLOBALs here)

Ask

Opening this issue to figure out the best way to move forward re above to make the flip as smooth as possible!

Ideally, it would be good if the path for serializing XLA tensors could be refactored to not use numpy and we would definitely accept a PR that implements this!

Separately, for existing checkpoints I imagine there will be something that needs to be done there.

cc @JackCaoG

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