fixed the issue of DPO trainer that using one node and mutiple GPUs and set the device_map='auto'
Author: pengwei715
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes 
https://github.com/huggingface/trl/issues/958
https://github.com/huggingface/trl/issues/1399

It's a very strong assumption that `tr_loss` and `tr_loss_step` are on the same device. `arg.device` may not be as same as the current device. 

For example, the `dpo_trainer` is child class of `trainer`. If you are running a DPO training job with one node multiple GPUs. If you set the `device_map='auto'`. The `tr_loss_step` could be on different device after computing the `dpo_loss`.  


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?
@lvwerra 
@kashif 
@younesbelkada 
@guy1992l

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @pacman100

Integrations:

- deepspeed: HF Trainer/Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc and @younesbelkada

Documentation: @stevhliu and @MKhalusova

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
Hi @younesbelkada 

Thanks for your reply! I completely understand the logic of "it's related to `trl` then the fix should be in `trl`", but just to be clear: It should be the case if and only if there is no chance that `tr_loss` and `tr_loss_step` can be on separate devices as it is the responsibility of the one who calculate the loss to put it on the right device. If so, maybe we should add an assert:

```python
assert tr_loss.device == tr_loss_step.device, f"Calculated loss must be on the original device: {tr_loss.device} but device in use is {tr_loss_step.device}"
``` 

Regarding the fix, maybe just to spare our time you know how to get the original `tr_loss.device` back in `DPOTrainer` where you point out the fix should be? I saw it is taken from `args.device`, is `args` available there?

Hi @guy1992l !
Yes having an assert could work ! You can retrieve `args` from `self` directly: https://github.com/huggingface/transformers/blob/c852d4fba6a46f8b71c608041a10691b66f174e5/src/transformers/trainer.py#L363 let me know if that works !


Hi @younesbelkada, @amyeroberts, @guy1992l 

Thank you very much for the feedback. I will test it out. And Let you know. 

Hi @younesbelkada , @amyeroberts , @guy1992l ,

This PR is doing the assertion checking to make sure the `tr_loss` and `tr_loss_step` are in the same device before updating. 

For the `dpo_trainer`, I have opened another PR here to address the issue inside of the `DPOTrainer` class. The PR is here. https://github.com/huggingface/trl/pull/1439. 

Both are tested. 
Please let me know your thoughts. Thanks. 

For the next reveiwers - the core issue should be fixed with https://github.com/huggingface/trl/pull/1439 - this PR simply checks if the losses are on the correct device, this should be safe IMO as only sub-classed trainers would potentially face this issue and the training would fail anyway if the devices are not the same

> Thanks! I agree that the fix should be over in `trl`, but it makes sense that we ensure that the devices are the same in this case otherwise raise a better err for users. Thanks!
> 
> (You may need to run `make style; make quality`

@muellerzr , Thank you very much. Let me do that. 

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_29695). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

> Reference in new

Silly me. Thanks. @amyeroberts 

Hi @amyeroberts , It seems this PR is ready to merge. Please let me know if more changes are still needed. Thanks.