Skip to content

Follow the input tensor's device instead of forcing CUDA - #4

Open
itzzdev09 wants to merge 1 commit into
tstanczyk95:mainfrom
itzzdev09:fix/cpu-device-index
Open

Follow the input tensor's device instead of forcing CUDA#4
itzzdev09 wants to merge 1 commit into
tstanczyk95:mainfrom
itzzdev09:fix/cpu-device-index

Conversation

@itzzdev09

Copy link
Copy Markdown

Fixes #1, open since May 2022.

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 becomes .cuda(-1).

Reproduced

On torch 2.14.0+cpu, this reproduces the reporter's error message exactly:

>>> x = torch.zeros(2, 3)          # CPU tensor, as on a machine with no GPU
>>> 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')          # works

The change

.to(x.device) follows whichever device x is 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.py
  • DK wind speed forecasting/WeatherGCNet with gamma/model_dk.py
  • NL wind speed forecasting/WeatherGCNet/model_nl.py
  • NL 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 on x's device.

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Device index must not be negative

1 participant