Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Should be a way to unpickle an object with a torch cuda tensor on a CPU-only machine when using plain "pickle"Β #16797

@petered

Description

@petered

πŸš€ Feature

The ability to pickle.load a Python object containing a torch cuda tensor on a CPU only machine.

Motivation

Currently, trying to do this gives RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location='cpu' to map your storages to the CPU. Even though you are loading with pickle.load, not torch.load.

When the "loading" code is pytorch agnostic (exists in a repo that does not use pytorch), you can't just change a pickle.load(f) into a torch.load(f, map_location='cpu'). This may be the case when the saved data takes a particular structure and loading/unloading is handled by some code that does not depend on pytorch.

Pitch

A context manager could take care of this:

with torch.loading_context(map_location='cpu'):
    obj = pickle.load(f)  # In my case this call is buried deeper in torch-agnostic code

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureA request for a proper, new feature.module: serializationIssues related to serialization (e.g., via pickle, or otherwise) of PyTorch objectstodoNot as important as medium or high priority tasks, but we will work on these.triagedThis 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