# What is the difference between using tensor.cuda() and tensor.to(torch.device(“cuda:0”))

**URL:** <https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259>\
**Category:** distributed\
**Created:** [July 15, 2020, 5:32am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259 "2020-07-15T05:32:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leockl](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@Leockl](https://discuss.pytorch.org/u/Leockl)\
**Post date:** [July 15, 2020, 5:32am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259/1 "2020-07-15T05:32:05Z")

</div>

Using PyTorch, what is the difference between the following two methods in sending a tensor to GPU:

Method 1:

```auto
X = np.array([[1, 3, 2, 3], [2, 3, 5, 6], [1, 2, 3, 4]])
X = torch.DoubleTensor(X).cuda()

```

Method 2:

```auto
X = np.array([[1, 3, 2, 3], [2, 3, 5, 6], [1, 2, 3, 4]])
X = torch.DoubleTensor(X)

device = torch.device("cuda:0")
X = X.to(device)

```

Similarly, is there any difference in the same two methods above when applied to sending a model to GPU:

Method A:

```auto
gpumodel = model.cuda()

```

Method B:

```auto
device = torch.device("cuda:0")
gpumodel = model.to(device)

```

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![iffiX](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/iffix/32/24443_2.png) [@iffiX](https://discuss.pytorch.org/u/iffiX)\
**Post date:** [July 15, 2020, 5:47am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259/2 "2020-07-15T05:47:15Z")

</div>

there is no difference

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [July 15, 2020, 5:53am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259/3 "2020-07-15T05:53:16Z")

</div>

There might be a difference, if you were resetting the default CUDA device via `torch.cuda.set_device()` as seen in this code snippet:

```python
torch.cuda.set_device('cuda:1')
x = torch.randn(1).cuda()
print(x)
> tensor([0.9038], device='cuda:1') # uses the default device now

y = torch.randn(1).to('cuda:0')
print(y)
> tensor([-0.7296], device='cuda:0') # explicitly specify cuda:0

```

---

<div class="post-metadata">

**Author:** ![Leockl](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@Leockl](https://discuss.pytorch.org/u/Leockl)\
**Post date:** [July 15, 2020, 5:55am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259/4 "2020-07-15T05:55:32Z")

</div>

Ok thanks @iffiX for confirming they are both essentially doing the same thing.

---

<div class="post-metadata">

**Author:** ![iffiX](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/iffix/32/24443_2.png) [@iffiX](https://discuss.pytorch.org/u/iffiX)\
**Post date:** [July 15, 2020, 5:56am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259/5 "2020-07-15T05:56:11Z")

</div>

Ah yes, thats important, I forgot this 🙂

---

<div class="post-metadata">

**Author:** ![Leockl](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/l/e9a140/32.png) [@Leockl](https://discuss.pytorch.org/u/Leockl)\
**Post date:** [July 15, 2020, 5:57am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259/6 "2020-07-15T05:57:17Z")

</div>

Ok many thanks @ptrblck for the more detailed answer where the 2nd method is specifying which GPU device to use and the 1st method is just using the default GPU device.

---

<div class="post-metadata">

**Author:** ![iacob](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/8dc957/32.png) [@iacob](https://discuss.pytorch.org/u/iacob)\
**Post date:** [April 15, 2021, 7:04am UTC](https://discuss.pytorch.org/t/what-is-the-difference-between-using-tensor-cuda-and-tensor-to-torch-device-cuda-0/89259/7 "2021-04-15T07:04:15Z")

</div>

Their syntax varies slightly, but [they are equivalent](https://stackoverflow.com/questions/62907815/pytorch-what-is-the-difference-between-using-tensor-cuda-and-tensor-totorch/66568863#66568863):

| ⠀ | .to(name) | .to(device) | .cuda() |
| --- | --- | --- | --- |
| **CPU** | `to('cpu')` | `to(torch.device('cpu'))` | `cpu()` |
| **Current GPU** | `to('cuda')` | `to(torch.device('cuda'))` | `cuda()` |
| **Specific GPU** | `to('cuda:1')` | `to(torch.device('cuda:1'))` | `cuda(device=1)` |

Note: the current cuda device is `0` by default, but this can be set with `torch.cuda.set_device()`.
