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

How to determine the device from within a LightningDataModule #3341

Closed
nathanpainchaud opened this issue Sep 3, 2020 · 2 comments
Closed
Labels
question Further information is requested

Comments

@nathanpainchaud
Copy link
Contributor

nathanpainchaud commented Sep 3, 2020

❓ Questions and Help

What is your question?

Is there a recommended way (or is it even at all possible) to determine which device is used from within the new LightningDataModule?

I ask the question because right now I decide how to set pin_memory when initializing the dataloaders based on the device. Prior to 0.9, the _dataloaders() methods were part of the LightningModule and I could simply access self.device to check the device the data would be transferred to.

Code

Example of what I did before 0.9:

def train_dataloader(self) -> DataLoader:
    return DataLoader(..., pin_memory=self.device.type == "cuda")

What have you tried?

I've checked the doc and API of the LightningDataModule, but I don't see any indications of how to get the device.

What's your environment?

  • Version: 0.9.0
@nathanpainchaud nathanpainchaud added the question Further information is requested label Sep 3, 2020
@ananyahjha93
Copy link
Contributor

you can check the trainer object for on_gpu variable and override your datamodule method to take this as a param.

@nathanpainchaud
Copy link
Contributor Author

For anyone wondering, the recent PR #3684, which adds a reference to the Trainer in the LightningDataModule, fixes my issue and allows us to do something like this:

def train_dataloader(self) -> DataLoader:
    return DataLoader(..., pin_memory=self.trainer.on_gpu)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants