# Distributed Training w/ Trainer

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%3E%5C
**Category:** 🤗Transformers\
**Created:** [July 7, 2021, 4:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151 "2021-07-07T16:21:39Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![josephgatto](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@josephgatto](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjosephgatto)%5C
**Post date:** [July 7, 2021, 4:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F1 "2021-07-07T16:21:39Z")

</div>

Does anyone have an end-to-end example of how to do multi-gpu, multi-node distributed training using the trainer? I can’t seem to find one anywhere.

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [July 7, 2021, 5:52pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F2 "2021-07-07T17:52:14Z")

</div>

All the examples using the `Trainer` run in multi-gpu multi-node, you just have to use the PyTorch launcher to properly launch a multi-GPU multinode training.

---

<div class="post-metadata">

**Author:** ![josephgatto](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@josephgatto](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fjosephgatto)%5C
**Post date:** [July 7, 2021, 6:03pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F3 "2021-07-07T18:03:40Z")

</div>

So is there no code adjustments that need to be made, only how the file is launched?

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [July 7, 2021, 6:08pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F4 "2021-07-07T18:08:41Z")

</div>

Yes, the `Trainer` will deal with all the rest by itself.

---

<div class="post-metadata">

**Author:** ![SSamDav](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/ssamdav/32/29888_2.png) [@SSamDav](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FSSamDav)%5C
**Post date:** [July 23, 2021, 8:33am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F5 "2021-07-23T08:33:27Z")

</div>

Hi I’m trying to run a multi-node training using the Trainer class, for that I run my script with the `python -m torch.distributed.launch --nproc_per_node=8 --nnodes=2 --node_rank=1 --master_addr="IP" \ --master_port=1234`, however, the script doesn’t wait for the master node. Also when I run in the master node the script doesn’t wait for the child node. Should I set up any env variable? The only thing that I’m doing is passing the local\_rank to the TrainingArgs.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fsgugger)%5C
**Post date:** [July 26, 2021, 1:05pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F6 "2021-07-26T13:05:20Z")

</div>

It’s hard to know what the problem could be without seeing the script you are launching.

---

<div class="post-metadata">

**Author:** ![Lee1990](https://avatars.discourse-cdn.com/v4/letter/l/4da419/32.png) [@Lee1990](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FLee1990)%5C
**Post date:** [April 24, 2022, 9:02am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F7 "2022-04-24T09:02:37Z")

</div>

what do you mean by “use the PyTorch launcher to properly launch a multi-GPU multinode training” ?

---

<div class="post-metadata">

**Author:** ![nlp](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@nlp](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnlp)%5C
**Post date:** [May 31, 2022, 10:10am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F8 "2022-05-31T10:10:45Z")

</div>

It doesnt work with Longformer. Is this expected ?

---

<div class="post-metadata">

**Author:** ![nbroad](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/nbroad/32/26465_2.png) [@nbroad](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fnbroad)%5C
**Post date:** [May 31, 2022, 1:31pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F9 "2022-05-31T13:31:53Z")

</div>

[https://pytorch.org/docs/stable/distributed.html#launch-utility](https://pytorch.org/docs/stable/distributed.html#launch-utility)

---

<div class="post-metadata">

**Author:** ![keval-sha](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@keval-sha](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fkeval-sha)%5C
**Post date:** [April 14, 2025, 8:27pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F10 "2025-04-14T20:27:41Z")

</div>

Are there any examples of running a distributed training job using pytorch on k8s, specifically GKE?

---

<div class="post-metadata">

**Author:** ![theharshithh](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/theharshithh/32/47921_2.png) [@theharshithh](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Ftheharshithh)%5C
**Post date:** [May 20, 2025, 1:09pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F11 "2025-05-20T13:09:14Z")

</div>

Use raylib for distributed training. here is a guide put up by verl for mutli-node training in ray.

> **[Multinode Training — verl documentation](https://verl.readthedocs.io/en/latest/start/multinode.html#set-up-multinode-ray-cluster)**

---

<div class="post-metadata">

**Author:** ![dag332](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/dag332/32/48741_2.png) [@dag332](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2Fdag332)%5C
**Post date:** [June 3, 2025, 6:14pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fdistributed-training-w-trainer%2F8151%2F12 "2025-06-03T18:14:56Z")

</div>

if you want to do multi-node, multi-GPU training make sure to use the `local_process_index` instead of the `process_index`. Otherwise the device count doesn’t account for the GPUs on the separate node

```auto
from trl import SFTConfig, SFTTrainer
from accelerate import PartialState

device_string = PartialState().local_process_index
 
model = AutoModelForCausalLM.from_pretrained(
    ...
    device_map={'':device_string}
)

```
