8000 Optimizer classes not `dill` picklable after using `torch.compile` · Issue #126154 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content
Optimizer classes not dill picklable after using torch.compile #126154
Open
@ringohoffman

Description

@ringohoffman

🐛 Describe the bug

To get started:

$ pip install torch dill

After you use torch.compile, Optimizer subclasses stop being dill picklable:

import dill
import torch

torch.save(torch.optim.AdamW, "a.pth", pickle_module=dill)  # OK

@torch.compile
def bias_gelu_fused_(inp: torch.Tensor, bias: torch.Tensor) -> torch.Tensor:
    """Bias-GeLU fused"""
    x = inp + bias
    return x * 0.5 * (1.0 + torch.tanh(0.79788456 * x * (1 + 0.044715 * x * x)))

torch.save(torch.optim.AdamW, "fake.txt", pickle_module=dill)  # PicklingError: Can't pickle <built-in function reset_code>: it's not found as torch._C._dynamo.eval_frame.reset_code

This seems to be partly the reason:

opt._init_group = disable(opt._init_group)

I think it boils down to torch._C._dynamo.eval_frame not being importable as a package, but as an attribute of torch._C._dynamo (like an imported module):

import dill
import torch._C._dynamo

print(torch._C._dynamo.eval_frame)  # <module 'torch._C._dynamo.eval_frame'>
print(torch._C._dynamo.eval_frame._CacheEntry)  # <class 'torch._C._dynamo.eval_frame._CacheEntry'>

torch.save(torch._C._dynamo.eval_frame._CacheEntry, "a.pth", pickle_module=dill)  # PicklingError: Can't pickle <class 'torch._C._dynamo.eval_frame._CacheEntry'>: it's not found as torch._C._dynamo.eval_frame._CacheEntry

import torch._C._dynamo.eval_frame  # ModuleNotFoundError: No module named 'torch._C._dynamo.eval_frame'; 'torch._C._dynamo' is not a package

eval_frame is defined here:

auto m = py::handle(eval_frame).cast<py::module>();

Is there some change to dill or C++-generated modules that solve this?

Versions

[conda] torch                     2.3.0                    pypi_0    pypi

cc @mruberry @mikaylagawarecki @msaroufim @ezyang @bdhirsh @anijain2305 @chauhang

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

      0