Skip to content

Implement pass-through state_dict and load_state_dict for dynamo OptimizedModule #113422

Description

@awaelchli

🚀 The feature, motivation and pitch

I propose that we implement .state_dict() and .load_state_dict() in OptimizedModule in such a way that they bypass the wrapper and return/load the state from the inner _orig_mod module.

Motivation:

  • User might accidentally save the state dict of a compiled model into their checkpoint, only to find out later they can't load because the keys don't match
  • The torch-compiled module may be wrapped by FSDP, DDP, etc. This would make it cumbersome for the user to save a checkpoint while also removing these keys or unwrapping the model.

The sketch of the implementation:

class OptimizedModule(torch.nn.Module):
    ...
    
    def state_dict(self, *args, **kwargs):
        return self._orig_mod.state_dict(*args, **kwargs)

    def load_state_dict(self, *args, **kwargs):
        # TODO: add some backward-compatibility code here
        return self._orig_mod.load_state_dict(*args, **kwargs)

With this implementation, we can effortlessly load the state-dict of a regular nn.Module into a torch-compiled module and vice versa!

Alternatives

Keep as is.

Additional context

This would probably also close/fix #94575
I am happy to send a PR for this.

cc @mruberry @mikaylagawarecki @ezyang @msaroufim @wconstab @bdhirsh @anijain2305 @zou3519

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: serializationIssues related to serialization (e.g., via pickle, or otherwise) of PyTorch objectsoncall: pt2triagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions