# PyTorch runtime error "expected all tensors to be on the same device" when all tensors are on the same device

**URL:** <https://discuss.pytorch.org/t/pytorch-runtime-error-expected-all-tensors-to-be-on-the-same-device-when-all-tensors-are-on-the-same-device/149739>\
**Category:** Uncategorized\
**Created:** [April 21, 2022, 2:08pm UTC](https://discuss.pytorch.org/t/pytorch-runtime-error-expected-all-tensors-to-be-on-the-same-device-when-all-tensors-are-on-the-same-device/149739 "2022-04-21T14:08:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GraphitePearl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/graphitepearl/32/48858_2.png) [@GraphitePearl](https://discuss.pytorch.org/u/GraphitePearl)\
**Post date:** [April 21, 2022, 2:08pm UTC](https://discuss.pytorch.org/t/pytorch-runtime-error-expected-all-tensors-to-be-on-the-same-device-when-all-tensors-are-on-the-same-device/149739/1 "2022-04-21T14:08:39Z")

</div>

I’m working on an inference script to quickly make predictions and get useful statistics. I decided it would be a good idea to get the predictions in batches and use CUDA to make the process much faster (inference on cpu takes more that 5 seconds so that’s not an option).

When I use the CPU to run my model, there is no problem, I get the output and no errors are thrown. However, when I transfer the model and its inputs to CUDA, I get the message that tensors are on different devices, which makes no sense to me.

```auto
device = torch.device('cuda:0')
sample = next(iter(dataloader))

img, _ = sample
output = model(img.unsqueeze(0))
# The line above works fine

model = model.to(device)
img_cuda = img.to(device)
output = model(img_cuda.unsqueeze(0))
# The line above causes the error

```

I have a feeling, something is not right with transfering the model to CUDA.

---

<div class="post-metadata">

**Author:** ![InnovArul](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/innovarul/32/5282_2.png) [@InnovArul](https://discuss.pytorch.org/u/InnovArul)\
**Post date:** [April 21, 2022, 2:35pm UTC](https://discuss.pytorch.org/t/pytorch-runtime-error-expected-all-tensors-to-be-on-the-same-device-when-all-tensors-are-on-the-same-device/149739/2 "2022-04-21T14:35:26Z")

</div>

> [@GraphitePearl](#):
>
> `model = model.to(device)`

Are you explicitly handling movement of data inside `model` with `.to(), .cuda(), .cpu()` calls?

---

<div class="post-metadata">

**Author:** ![GraphitePearl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/graphitepearl/32/48858_2.png) [@GraphitePearl](https://discuss.pytorch.org/u/GraphitePearl)\
**Post date:** [April 22, 2022, 6:59am UTC](https://discuss.pytorch.org/t/pytorch-runtime-error-expected-all-tensors-to-be-on-the-same-device-when-all-tensors-are-on-the-same-device/149739/3 "2022-04-22T06:59:30Z")

</div>

Thank you. That’s very clever idea, but there are no such statements inside the model. However, a simpler model causes no such errors and works amazingly quick on a GPU. I’ll have to carefully examine the more sophisticated model.

---

<div class="post-metadata">

**Author:** ![GraphitePearl](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/graphitepearl/32/48858_2.png) [@GraphitePearl](https://discuss.pytorch.org/u/GraphitePearl)\
**Post date:** [April 22, 2022, 1:30pm UTC](https://discuss.pytorch.org/t/pytorch-runtime-error-expected-all-tensors-to-be-on-the-same-device-when-all-tensors-are-on-the-same-device/149739/4 "2022-04-22T13:30:51Z")

</div>

I found the fix, although I’m still not sure why the problem occurred. The model creates an instance of an ConvLSTM cell, which initializes a tensor that needed to be manually transferred to CUDA.
