# Trainer, device error cuda:0 and cuda:1

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-device-error-cuda-0-and-cuda-1%2F69249%3E%5C
**Category:** 🤗Transformers\
**Created:** [January 15, 2024, 5:08pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-device-error-cuda-0-and-cuda-1%2F69249 "2024-01-15T17:08:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FreddyMro](https://avatars.discourse-cdn.com/v4/letter/f/a3d4f5/32.png) [@FreddyMro](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FFreddyMro)%5C
**Post date:** [January 15, 2024, 5:08pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-device-error-cuda-0-and-cuda-1%2F69249%2F1 "2024-01-15T17:08:26Z")

</div>

Hi, I want to train a model using the `Trainer`. In the `model_init` function, I instantiate a model and perform heavy calculations for experimental weight initialization. These calculations should be performed on a cuda device which is why I instantiate my model on a cuda device by manually moving parameters onto `cuda` (not `cuda:0` or `cuda:1`).

My system has two cuda cards which I both want to use. When I run the training (trainer.train()), I get the error: `RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0`.

I expected the trainer to take care of data parallelism. When I instantiate the model without moving parameters to cuda (which takes an eternity), everything works fine.

Could somebody please share any insights into how I can use cuda for model initialization without running into this error during training? Many thanks!

---

<div class="post-metadata">

**Author:** ![rachel-sunshine](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rachel-sunshine](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Frachel-sunshine)%5C
**Post date:** [January 16, 2024, 8:26pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-device-error-cuda-0-and-cuda-1%2F69249%2F2 "2024-01-16T20:26:11Z")

</div>

I faced this error once when using yolov.

Do this -  
os.environ[“CUDA\_VISIBLE\_DEVICES”]=“0”  
at the top

---

<div class="post-metadata">

**Author:** ![FreddyMro](https://avatars.discourse-cdn.com/v4/letter/f/a3d4f5/32.png) [@FreddyMro](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FFreddyMro)%5C
**Post date:** [January 17, 2024, 3:01pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-device-error-cuda-0-and-cuda-1%2F69249%2F3 "2024-01-17T15:01:27Z")

</div>

Thank you for your suggestion! That would indeed work but turn off my second GPU entirely which I do not want 😉

But I found a funny solution which I do not entirely understand but I’ll share for those having the same issue.

Somewhere in my model I have code along the lines of:

```auto
some_weight = nn.Parameter(any_random_weight).to(device)

```

Changing this to

```auto
some_weight = nn.Parameter(any_random_weight.to(device))

```

i.e. moving the tensor to cuda before making it a parameter (as supposed to the other way round) did the trick! I do not know why but I’ll gratefully accept it’s working now. `device` was set to `"cuda"` in my case.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/hellohellohello/original/2X/d/de4155eb4aa4108ecb32a1389d7cc37ae69f88b7.png) [@system](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsystem)%5C
**Post date:** [January 18, 2024, 3:02am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Ftrainer-device-error-cuda-0-and-cuda-1%2F69249%2F4 "2024-01-18T03:02:14Z")

</div>

Closed on January 18, 2024, 3:02am UTC

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.
