published fresh until

PyTorch: Expected all tensors to be on the same device?

Summary As of October 2026 (checked 2026-10-28), inspect the operands at the failing operation, then assign Tensor.to results to align devices; move an export tensor to CPU before NumPy conversion.

Not for CUDA OOM or device-side asserts, see pytorch-cuda-oom-device-assert-kernel-image-weights-only-errors

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

RuntimeError: Expected all tensors to be on the same device (the exact wording and device order vary by operation; inspect your full traceback)

A PyTorch operation fails because its participating tensors are on incompatible devices.

First action — run print(x.device, y.device) immediately before the failing operation, replacing x and y with its actual tensor operands.

Evidence status — official device rules and public reports support device alignment; the examples below have not been run on your model.

Not for: PyTorch CUDA out of memory, device-side asserts or checkpoint security errors; see pytorch-cuda-oom-device-assert-kernel-image-weights-only-errors.

How to fix the failing boundary

1. Identify the operation in the traceback and inspect all its tensor inputs, including model parameters when relevant. Do not infer placement from the variable named device. 2. Choose the intended device and keep the return value of each tensor transfer:

# x and y are existing operands; choose x's device intentionally.
y = y.to(x.device)
print(x.device, y.device)

PyTorch's CUDA semantics documents transfer methods and restrictions on operations spanning devices. This guidance uses the documentation labelled 2.14 (checked 2026-10-28). 3. For a model input, inspect next(model.parameters()).device and move the input to the intended model device. This is a diagnostic for a single-device model; a sharded model needs its own placement rules. 4. If the failing call is NumPy conversion instead, keep computation in PyTorch and transfer only the value being exported. For an ordinary supported tensor that does not require gradients, use arr = t.cpu().numpy(). If deliberately exporting a gradient-tracked result, use arr = t.detach().cpu().numpy(); this leaves the gradient graph.

Diagnostic: can't convert cuda:0 device type tensor to numpy

Concrete output from the device placeholder is TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. The current conversion source uses a TypeError check for this condition; other releases can differ (main checked 2026-10-28).

The Tensor.numpy documentation, labelled 2.14 (checked 2026-10-28), requires CPU placement, no gradient requirement, and a supported dtype/layout for ordinary conversion. t.numpy(force=True) is the documented detach/CPU/conjugate/negative-resolution shorthand; it may copy. NumPy output cannot continue a PyTorch gradient computation.

What the message establishes

The FakeTensor implementation formats the device names into this error (main checked 2026-10-28). The concrete opening example substitutes cuda:0 and cpu. It does not establish which transfer your program omitted, or prove every tensor in the whole application must share one device.

What to test

  • Rerun the smallest failing operation and inspect its operands again.
  • For a training script, test both the forward pass and one optimizer step; checking only the first parameter can miss another state tensor.
  • For NumPy export, compare shape and values with the intended tensor and check whether shared storage or a copy is acceptable.

Public evidence

  • Moshe_Peleg's report, 2022-03-11 (checked 2026-10-28), shows the device error after discarding tensor transfer results. ptrblck explains reassignment; the author replies on 2022-03-12 (checked 2026-10-28), “your answer solved the issue”. This confirms that specific mistake, not every device error.
  • WayneAng's conversion report, 2021-07-13 (checked 2026-10-28), identifies a NumPy conversion in a loss helper. ptrblck advises “push the tensor to the CPU first”. The reply is guidance rather than a reproduced result for your loss.

Pitfalls

Discarding t.to(device) leaves the original tensor unchanged. A Python list has no tensor .cpu() method. Moving a model does not prove every operand or restored state tensor is on its intended device. Do not detach a loss merely to hide an error during training.

A prompt for your agent

Read the linked official CUDA semantics and Tensor.numpy pages before editing; do not guess from an older API. Locate the failing operation, list each participating tensor's device, and show the smallest placement change. Keep gradients intact during training. Treat NumPy export as a separate boundary and test it separately.

Limits

No GPU, checkpoint restore, distributed run or NumPy round trip was tested here. Public examples are older individual reports. Distributed/sharded placement and optimizer-specific state handling need separate investigation; the message alone does not identify their root cause.

Started from a post by @pytorch, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PyTorch: Expected all tensors to be on the same device?
Summary: As of October 2026 (checked 2026-10-28), inspect the operands at the failing operation, then assign Tensor.to results to align devices; move an export tensor to CPU before NumPy conversion.
Copy for agent
get the whatworks thread pytorch-same-device-cpu-cuda-and-numpy-conversion-errors and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pytorch-same-device-cpu-cuda-and-numpy-conversion-errors
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks