Follow the input tensor's device instead of forcing CUDA - #4
Open
itzzdev09 wants to merge 1 commit into
Open
Conversation
All four model files move the adjacency matrix with:
self.A = self.A.cuda(x.get_device())
torch.Tensor.get_device() returns -1 for a CPU tensor, and .cuda() requests CUDA
regardless of where x actually lives. On a machine without a GPU the call is
therefore .cuda(-1), which raises:
RuntimeError: Device index must not be negative
Reproduced on torch 2.14.0+cpu:
>>> x = torch.zeros(2, 3)
>>> x.get_device()
-1
>>> torch.zeros(3, 3).cuda(x.get_device())
RuntimeError: Device index must not be negative
>>> torch.zeros(3, 3).to(x.device)
tensor(..., device='cpu')
Using .to(x.device) follows whichever device x is already on: the same GPU when
the model runs on CUDA, and CPU otherwise. This is also what the StackOverflow
answer linked from the issue recommends.
Applied to all four copies of the model:
DK/WeatherGCNet, DK/WeatherGCNet with gamma, NL/WeatherGCNet, NL/WeatherGCNet
with gamma.
Fixes tstanczyk95#1.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1, open since May 2022.
All four model files move the adjacency matrix with:
torch.Tensor.get_device()returns -1 for a CPU tensor, and.cuda()requests CUDA regardless of wherexactually lives. On a machine without a GPU the call becomes.cuda(-1).Reproduced
On
torch 2.14.0+cpu, this reproduces the reporter's error message exactly:The change
.to(x.device)follows whichever devicexis already on — the same GPU when the model runs on CUDA, and CPU otherwise. It's also what the StackOverflow answer the reporter linked recommends.Applied to all four copies of the model, which carry the identical line:
DK wind speed forecasting/WeatherGCNet/model_dk.pyDK wind speed forecasting/WeatherGCNet with gamma/model_dk.pyNL wind speed forecasting/WeatherGCNet/model_nl.pyNL wind speed forecasting/WeatherGCNet with gamma/model_nl.py.cuda(appears exactly once per file and nowhere else in the repo, so this is the complete set.What I could not verify
I don't have the wind-speed datasets or a GPU here, so I verified the device semantics in isolation rather than by training a model end to end. The change is confined to that one line per file and doesn't touch the GPU path's behaviour — on CUDA,
.to(x.device)and.cuda(x.get_device())both land onx's device.🤖 Generated with Claude Code