[Trainer.py] when --load_best_model_at_end is set in Distributed Training
Author: logoutAgain
Pull request: no

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->

- `transformers` version: 4.4, not very sure
- Platform: linux
- Python version: 3.7
- PyTorch version (GPU?): 1.8
- Tensorflow version (GPU?): ////
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: Distributed training in a single node with multi-gpus

### Who can help
<!-- Your issue 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:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @Rocketknight1

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

Model hub:

- for issues with a model report at https://discuss.huggingface.co/ and tag the model's creator.

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 --> @sgugger 

## Information

Model I am using (Bert, XLNet ...): Bart

The problem arises when using:
trainer.py

The tasks I am working on is:
my own task or dataset

## To reproduce
I'm not sure if it's a bug or I misunderstood. So I am here for help. 

Steps to reproduce the behavior:

1.  set `--load_best_model_at_end` and use distributed training mode with multi-gpus.
2.  When the best model appears in the last step, the main process needs to save the model in `self._maybe_log_save_evaluate(tr_loss, model, trial, epoch)` after training. However, the subprocess may run `self.model = self.model.from_pretrained(self.state.best_model_checkpoint)` before the best model is completely saved.
3. So the subprocess will occur `OSError: Error no file named ['pytorch_model.bin', 'tf_model.h5', 'model.ckpt.index'] found in directory <best model dir>......`

Evaluate:
1. I print the time after `self._maybe_log_save_evaluate(tr_loss, model, trial, epoch)`  and time before `self.model = self.model.from_pretrained(self.state.best_model_checkpoint)`.  It seems that the result has confirmed my guess.
2. I add `dist.barrier()` after `self._maybe_log_save_evaluate(tr_loss, model, trial, epoch)` and the error doesn't appear anymore.

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->



Comments:
Could you try again on the latest version? This bug has normally been fixed (see [this comment](https://github.com/huggingface/transformers/blob/9a7545943dd611b0d4887a5ab3a7ef7f53b8e76f/src/transformers/trainer.py#L1351) and the four line below).

> Could you try again on the latest version? This bug has normally been fixed (see [this comment](https://github.com/huggingface/transformers/blob/9a7545943dd611b0d4887a5ab3a7ef7f53b8e76f/src/transformers/trainer.py#L1351) and the four line below).

Yes, it looks good. I download the source code to the project, so I can't update it in time. I'm sorry to take up your time。