# How to train my model on multiple GPU

**URL:** <https://discuss.huggingface.co/t/how-to-train-my-model-on-multiple-gpu/75775>\
**Category:** 🤗Transformers\
**Created:** [March 4, 2024, 7:49am UTC](https://discuss.huggingface.co/t/how-to-train-my-model-on-multiple-gpu/75775 "2024-03-04T07:49:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shrijayan](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/shrijayan/32/28178_2.png) [@shrijayan](https://discuss.huggingface.co/u/shrijayan)\
**Post date:** [March 4, 2024, 7:49am UTC](https://discuss.huggingface.co/t/how-to-train-my-model-on-multiple-gpu/75775/1 "2024-03-04T07:49:17Z")

</div>

I am trying to train the Sentence Transformer Model named cross-encoder/ms-marco-MiniLM-L-12-v2 where When I try to train it utilizes only one GPU, where in my machine I have two GPUs. I tried DataParallel and DistributedDataParallel, but didn’t Work.

```auto
from sentence_transformers import SentenceTransformer, losses
from torch.utils.data import DataLoader

# Replace 'model_name' and 'max_seq_length' with your actual model name and max sequence length
model_name = 'your_model_name'
max_seq_length = your_max_seq_length

# Load SentenceTransformer model
model = SentenceTransformer(model_name)
model.max_seq_length = max_seq_length

# Replace 'train_examples' with your actual training examples
train_examples = your_train_examples

# Create DataLoader for training
train_dataloader = DataLoader(train_examples, batch_size=16, drop_last=True, shuffle=True)

# Define the loss function
train_loss = losses.MarginMSELoss(model)

# Tune the model
model.fit(train_objectives=[(train_dataloader, train_loss)], epochs=500, warmup_steps=int(len(train_dataloader) * 0.1))

# Replace 'output_path' with the desired path for saving the trained model
output_path = 'your_output_path'

# Save the model after training
model.save(output_path)

```

---

<div class="post-metadata">

**Author:** ![giuseppe-trimigno](https://avatars.discourse-cdn.com/v4/letter/g/3e96dc/32.png) [@giuseppe-trimigno](https://discuss.huggingface.co/u/giuseppe-trimigno)\
**Post date:** [March 5, 2024, 8:36am UTC](https://discuss.huggingface.co/t/how-to-train-my-model-on-multiple-gpu/75775/2 "2024-03-05T08:36:01Z")

</div>

Consider taking a look at [Accelerate library](https://huggingface.co/docs/accelerate/index), you can train on multiple GPUs with few changes in your code.

---

<div class="post-metadata">

**Author:** ![shrijayan](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/shrijayan/32/28178_2.png) [@shrijayan](https://discuss.huggingface.co/u/shrijayan)\
**Post date:** [March 6, 2024, 9:12am UTC](https://discuss.huggingface.co/t/how-to-train-my-model-on-multiple-gpu/75775/3 "2024-03-06T09:12:50Z")

</div>

Will checkout this.
