How to instantiate torchmetrics instances properly so Lightning sends them to device #19859
Unanswered
arzaatri
asked this question in
Lightning Trainer API: Trainer, LightningModule, LightningDataModule
Replies: 1 comment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The lightning documentation says this about torchmetrics:
"Modular metrics are automatically placed on the correct device when properly defined inside a LightningModule. This means that your data will always be placed on the same device as your metrics. No need to call .to(device) anymore!"
Can anyone expand on what "properly defined" entails? As an example of what may not be proper, I initialized and call my metrics like so:
And got this error when running my code:
Encountered different devices in metric calculation (see stacktrace for details). This could be due to the metric class not being on the same device as input. Instead of `metric=BinaryAccuracy(...)` try to do `metric=BinaryAccuracy(...).to(device)` where device corresponds to the device of the input.
Is the way that I define my metrics within the LightningModule improper? If so, what are the requirements to define torchmetrics instance in such a way that Lightning automatically moves them to the correct device?
Beta Was this translation helpful? Give feedback.
All reactions