Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move_data_to_device fails with dataclasses with field(init=False) fields #9930

Closed
ronif opened this issue Oct 14, 2021 · 2 comments · Fixed by #9963
Closed

move_data_to_device fails with dataclasses with field(init=False) fields #9930

ronif opened this issue Oct 14, 2021 · 2 comments · Fixed by #9963
Labels
bug Something isn't working help wanted Open to be worked on

Comments

@ronif
Copy link
Contributor

ronif commented Oct 14, 2021

🐛 Bug

move_data_to_device has special handling of dataclasses, but in case the dataclass has a non-init field, elem_type(**out) in apply_to_collection will fail

To Reproduce

from pytorch_lightning.utilities import move_data_to_device
import torch
from dataclasses import dataclass, field
from typing import NamedTuple, List

@dataclass
class Data:
    example_id: List[str]
    x: torch.Tensor
    y: int = field(init=False)

    def __post_init__(self):
        self.y = 3

batch_size = 5

batch = Data(
    example_id=[f"e-{i}" for i in range(batch_size)],
    x = torch.rand(batch_size),
)

assert batch.y == 3
device = torch.device("cuda:0")

move_data_to_device(batch=batch, device=device)

Expected behavior

Should run without exceptions

Environment

  • Packages:
    - numpy: 1.21.2
    - pyTorch_debug: False
    - pyTorch_version: 1.9.1
    - pytorch-lightning: 20211014
    - tqdm: 4.62.3
  • System:
    - OS: Linux
    - architecture:
    - 64bit
    -
    - processor: x86_64
    - python: 3.9.7
@ronif ronif added bug Something isn't working help wanted Open to be worked on labels Oct 14, 2021
@ronif
Copy link
Contributor Author

ronif commented Oct 14, 2021

I think this is an easy fix. Can submit a PR if needed.

@justusschock
Copy link
Member

@ronif that would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Open to be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants