# Trainer.train throws RuntimeError: Expected all tensors to be on the same device

**URL:** <https://discuss.huggingface.co/t/trainer-train-throws-runtimeerror-expected-all-tensors-to-be-on-the-same-device/16349>\
**Category:** Beginners\
**Created:** [April 1, 2022, 4:26am UTC](https://discuss.huggingface.co/t/trainer-train-throws-runtimeerror-expected-all-tensors-to-be-on-the-same-device/16349 "2022-04-01T04:26:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sssingh](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sssingh/32/19900_2.png) [@sssingh](https://discuss.huggingface.co/u/sssingh)\
**Post date:** [April 1, 2022, 4:26am UTC](https://discuss.huggingface.co/t/trainer-train-throws-runtimeerror-expected-all-tensors-to-be-on-the-same-device/16349/1 "2022-04-01T04:26:44Z")

</div>

I have moved the model to ‘cuda’ and confirmed that the TrainingArguments object does pickup ‘cuda’ as a device but when I try to train it throws this error, here is the code…

# Set device

device = torch.device(‘cuda’ if torch.cuda.is\_available() else ‘cpu’)  
torch.cuda.current\_device(), device

OUTPUT → (0, device(type=‘cuda’))

# Create model and push to cuda

model = AutoModelForSequenceClassification.from\_pretrained(MODEL\_CKPT, num\_labels=6).to(device)

# Instantiate a TrainerArguments objectbatch\_size = 64

logging\_steps = len(emotion\_encoded[‘train’]) // batch\_size  
epochs=2  
learning\_rate = 2e-5  
output\_dir = MODEL\_CKPT + ‘-finetuned-emotion-sssingh’  
args = TrainingArguments(output\_dir=output\_dir,  
per\_device\_train\_batch\_size=batch\_size,  
per\_device\_eval\_batch\_size=batch\_size,  
learning\_rate=learning\_rate,  
weight\_decay=0.01,  
num\_train\_epochs=epochs,  
evaluation\_strategy=‘epoch’,  
disable\_tqdm=False,  
logging\_steps=logging\_steps,  
log\_level=‘error’)

args.device  
OUTPUT → device(type=‘cuda’, index=0)

# Instantiate a Trainer object and train model end-to-end

trainer = Trainer(model=model,  
tokenizer=tokenizer,  
args=args,  
compute\_metrics=performance\_metric,  
train\_dataset=emotion\_encoded[‘train’],  
eval\_dataset=emotion\_encoded[‘validation’])

# Train

trainer.train()

This throws this error …  
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument index in method wrapper\_\_index\_select)

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](https://discuss.huggingface.co/u/BramVanroy)\
**Post date:** [April 1, 2022, 9:49am UTC](https://discuss.huggingface.co/t/trainer-train-throws-runtimeerror-expected-all-tensors-to-be-on-the-same-device/16349/2 "2022-04-01T09:49:20Z")

</div>

TrainingArguments automatically sets the device to a GPU (`cuda:0`) if it is available.

> <https://github.com/huggingface/transformers/blob/9de70f213eb234522095cc9af7b2fac53afc2d87/src/transformers/training_args.py#L1069-L1079>

You should not manually move your model to GPU. So remove all the `.to()` calls. After initializing you can verify that the GPU is being used by checking `args.device`.

If that is correct, and you are still experiencing an issue, it is possible that your custom function `performance_metric` does something with mixed tensors. In that case, please post the full error trace and the custom function.

---

<div class="post-metadata">

**Author:** ![Francal](https://avatars.discourse-cdn.com/v4/letter/f/5daacb/32.png) [@Francal](https://discuss.huggingface.co/u/Francal)\
**Post date:** [April 29, 2022, 10:16am UTC](https://discuss.huggingface.co/t/trainer-train-throws-runtimeerror-expected-all-tensors-to-be-on-the-same-device/16349/3 "2022-04-29T10:16:46Z")

</div>

Hi Bram,

I get the same error message for the Trainer.train() command when I run the below code in Colab using GPU as the run type. However, when I use CPU as the run type it runs successfully without any errors. Do you have any advise as to how I can run the below in GPU without getting the “Expected all tensors to be on the same device error”

[Colab Code](https://colab.research.google.com/drive/1An1VNpKKMRVrwcdQQNSe7Omh_fl2Gj-2?usp=sharing)

 ![error message](https://us1.discourse-cdn.com/hellohellohello/original/2X/8/8ab5a20612f25d6c6d1d6abeeed53d2e669b2f31.png)

---

<div class="post-metadata">

**Author:** ![BramVanroy](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/bramvanroy/32/47360_2.png) [@BramVanroy](https://discuss.huggingface.co/u/BramVanroy)\
**Post date:** [April 29, 2022, 4:10pm UTC](https://discuss.huggingface.co/t/trainer-train-throws-runtimeerror-expected-all-tensors-to-be-on-the-same-device/16349/4 "2022-04-29T16:10:38Z")

</div>

This may be the same issue as discussed [here](https://github.com/nlp-with-transformers/notebooks/issues/31).

---

<div class="post-metadata">

**Author:** ![Francal](https://avatars.discourse-cdn.com/v4/letter/f/5daacb/32.png) [@Francal](https://discuss.huggingface.co/u/Francal)\
**Post date:** [April 30, 2022, 1:48am UTC](https://discuss.huggingface.co/t/trainer-train-throws-runtimeerror-expected-all-tensors-to-be-on-the-same-device/16349/5 "2022-04-30T01:48:11Z")

</div>

Thank you very much. The solution proposed in the link you provided worked perfectly.

---

<div class="post-metadata">

**Author:** ![LidorPrototype](https://avatars.discourse-cdn.com/v4/letter/l/91b2a8/32.png) [@LidorPrototype](https://discuss.huggingface.co/u/LidorPrototype)\
**Post date:** [May 17, 2023, 4:14am UTC](https://discuss.huggingface.co/t/trainer-train-throws-runtimeerror-expected-all-tensors-to-be-on-the-same-device/16349/6 "2023-05-17T04:14:32Z")

</div>

I tried that, but it did not solve the issue for me at all ☹ any other ideas?
