Stalled loop during prediction with deepspeed
Author: avivbrokman
Pull request: no

### System Info

- `transformers` version: 4.31.0.dev0
- Platform: Linux-5.10.173-154.642.amzn2.x86_64-x86_64-with-glibc2.26
- Python version: 3.10.10
- Huggingface_hub version: 0.14.1
- Safetensors version: 0.3.1
- PyTorch version (GPU?): 2.0.0 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: Yes
- Using distributed or parallel set-up in script?: Yes

### Who can help?

@pacman100 (b/c deepspeed-only problem) @sgugger (b/c this is a documentation example script)

### Information

- [X] The official example scripts
- [ ] My own modified scripts

### Tasks

- [X] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

I am running `examples/pytorch/translation/run_translation.py` on a machine with 4 V100's.   To replicate my issue, run 

deepspeed examples/pytorch/translation/run_translation.py --deepspeed tests/deepspeed/ds_config_zero3.json /
--model_name_or_path t5-small /
--per_device_train_batch_size 1 /
--output_dir output_dir /
--overwrite_output_dir /
--fp16 /
--do_train /
--max_train_samples 64 /
--num_train_epochs 1 /
--dataset_name wmt16 /
--dataset_config "ro-en" /
--source_lang en /
--target_lang ro /
--do_predict  /
--max_predict_samples 64 /
--predict_with_generate

### Expected behavior

I would expect the script to fully run using `deepspeed`, not just without it.  Right now, it outputs an warning message 

```
Invalidate trace cache @ step 0: expected module 2, but got module 0
Invalidate trace cache @ step 1: expected module 116, but got module 2
```
and gets stuck during the `.evaluation_loop()` method.  I added some printing steps to the code, and it appeared that the code was stalling after the first `.prediction_step()`. 

Comments:
Not really sure, but let me tag @pacman100 (?) and see if he knows this is more an issue in `transformers/accelerate` or should go to DeepSpeed repo issue page.

I wasn't sure which repo it belonged in either.  I couldn't seem to locate the source of the bug.  Based on all the print statements I added, it looked like it had to be in the `dataloader`, but then I added some code just to iterate through the `dataloader` without doing anything, and that worked without issue.

Hello @avivbrokman, so you mean the issue is not with dataloaders?


@pacman100 I couldn't figure it out—I think this is beyond my coding skill level.  I spent a few days trying to locate the source of the bug, but failed.  Normally, when I get an error, the traceback helps me find the problem.  Here, there's no error message, so my (probably highly inadvisable) solution was to add print statements in between every single line in the source code so I can see the last line that was executed.   

When I add `print(f'finished step {step}')` at line 3179 of `trainer.py` with one less level of indentation than line 3178, it prints, but then the `print(f'beginning step {step}')` at line 3114 doesn't execute for a second batch.  This led me to believe that the issue was with the `dataloader`.  So I inserted the following code at line 3112:

```
for step, inputs in enumerate(dataloader):
    print(step)
```

This fully executed, which led me to to believe the problem is not the `dataloader`.  At this point, I reached the limits of my understanding, and submitted my bug report.

Thank you. This isn't a deepspeed issue as this also happens on just using DDP

> for step, inputs in enumerate(dataloader):
> print(step)
> This fully executed, which led me to to believe the problem is not the dataloader. At this point, I reached the limits of my understanding, and submitted my bug report.

This doesn't print for 2nd step for me

Seems to be related to dataloader, cc @muellerzr, post completion of 1 step, it hangs when using DDP:

<img width="1502" alt="Screenshot 2023-07-12 at 5 02 43 PM" src="https://github.com/huggingface/transformers/assets/13534540/dd55966a-0bd0-4004-b72e-be9069a9535b">


Command:
```
accelerate launch --num_processes 2 examples/pytorch/translation/run_translation.py --model_name_or_path t5-small --per_device_train_batch_size 1 --output_dir output_dir --overwrite_output_dir --fp16 --do_train --max_train_samples 64 --num_train_epochs 1 --dataset_name wmt16 --dataset_config "ro-en" --source_lang en --target_lang ro --do_predict --max_predict_samples 64 --predict_with_generate
```

Main branch of transformers and accelerate. @muellerzr, any ideas about what might be going wrong?


Hmmm @pacman100 can you grab the absolutely latest version of main on Accelerate and try again? (Like within the last 5 minutes)

Hello, just updated the accelerate to main and still the issue persists

@muellerzr Is the #24775 PR just for the Trainer? I am hitting this issue in my eval loop, but with a custom loop, not the trainer. I am using zero-3.

@init-random yes, we'd need a reproducer to know what's going on with your custom loop, but in general that's the correct solution to do if you're mimicking what the trainer should be doing. (And it's not directly deepspeed related)

@muellerzr OK, thank you! I'll look into it and open a new issue, if need be.

@muellerzr @pacman100 It seems like this issue still persists.

See [this](https://github.com/microsoft/DeepSpeed/issues/5006) issue. I've updated to the latest ```accelerate == 0.29.3``` and was hoping #24775 would fix it, but after the first epoch is completed, I get the message ```Invalidate trace cache @ step 0: expected module 0, but got module 456``` and its just stuck post that.

I also think this is an Accelerate issue because @pacman100 has said this also happens with DDP.

Can we please look into this?

@vikram71198 thats a slightly different problem. Can you please open a new issue (rather than commenting on an issue last posted a year ago) for us to look at please! (And a full reproducer included in the issue)

Well, I think they're both identical issues. But sure, I can open a new issue.