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

Automatic device transfer converts namedtuples into regular tuples #199

Closed
lakshmi-speak opened this issue Nov 27, 2023 · 2 comments
Closed
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@lakshmi-speak
Copy link

lakshmi-speak commented Nov 27, 2023

🐛 Bug

In the latest version of the utilities library, automatic device transfer converts namedtuples into regular tuples, causing loss of attribute access provided by the namedtuple.

To Reproduce

from collections import namedtuple
import torch
from pytorch_lightning import LightningModule, Trainer

Batch = namedtuple("Batch", ["features", "targets"])

class SimpleModule(LightningModule):
    def __init__(self):
        super().__init__()
        self.linear = torch.nn.Linear(5, 1)
        
    def forward(self, batch):
        print(type(batch))
        print(batch.features)
        return self.linear(batch.features)

    def training_step(self, batch, batch_idx):
        return self(batch)

    def train_dataloader(self):
        features = torch.randn(10, 5)
        targets = torch.randint(0, 1, (10,))
        dataset = [Batch(features[i], targets[i]) for i in range(10)]
        return torch.utils.data.DataLoader(dataset, batch_size=1)
    
    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=1e-3)

model = SimpleModule()
trainer = Trainer(fast_dev_run=True)
trainer.fit(model)

This script fails with attribute error AttributeError: 'tuple' object has no attribute 'features', when the library version is 0.10.0 and runs without errors on 0.9.0. The Pytorch lightning version was unchanged, 2.1.2 for both.

Additional context

Environment details
  • PyTorch Version (e.g., 1.0): 2.1.1+cu121
  • OS (e.g., Linux): Ubuntu
  • How you installed PyTorch (conda, pip, source): pip
  • Python version: 3.10.13
  • CUDA/cuDNN version: 12.2

This happened for both single GPU and multi GPU run with DDP. This was a bit of work to track down since the pinned pytorch_lightning version was unchanged.
This is the exact same as a previous lightning issue, although that one seems to have been due to PyTorch?

@lakshmi-speak lakshmi-speak added bug Something isn't working help wanted Extra attention is needed labels Nov 27, 2023
@carmocca
Copy link
Contributor

carmocca commented Dec 13, 2023

Hi @lakshmi-speak. Sorry for this.

I think it's best that we revert #160.

Would you like to open a PR doing that and adding your namedtuple sample as a test to https://github.com/Lightning-AI/utilities/blob/main/tests/unittests/core/test_apply_func.py?

@Borda
Copy link
Member

Borda commented Dec 22, 2023

@carmocca was this resolved in #210 too?

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 Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants