Repository: pytorch
Issue: fsdp training with the seq2seqTranier module gets stuck during evaluation.
Labels: triaged,module: fsdp

### 🐛 Describe the bug

when trying to finetune flan-t5 large with the Seq2seqTrainer module, and also passing fsdp_transformer_layer_cls_to_wrap="T5Block" and fsdp="full_shard auto_wrap", I got at first the following error:
```
RuntimeError: The tensor has a non-zero number of elements, but its data is not allocated yet. Caffe2 uses a lazy al
location, so you will need to call mutable_data() or raw_mutable_data() to actually allocate memory.
```

Following #82461 , I replaced the following line:
[https://github.com/huggingface/transformers/blob/main/src/transformers/trainer_seq2seq.py#L276](link)

with this code:
```
        try:
            decoder_input_ids = inputs["decoder_input_ids"]
            inputs = {k: v for k, v in inputs.items() if k != "decoder_input_ids"}
            generated_tokens = self.model.generate(**inputs, **gen_kwargs)
        except RuntimeError as e:
            if "The tensor has a non-zero number of elements, but its data is not allocated yet" in str(e):
                inputs["decoder_input_ids"] = decoder_input_ids
                inputs_dummy = {key:value[:,:1] for key,value in inputs.items()}
                model(**inputs_dummy)
                inputs = {k: v for k, v in inputs.items() if k != "decoder_input_ids"}
                generated_tokens = self.model.generate(**inputs, **gen_kwargs)
            else:
                raise RuntimeError(str(e))
```
But now, when the model hits `model(**inputs_dummy)`, the GPUs' utility reaches 100% instantly, and the whole process gets stuck.
I traced the origin of the problem to the following function, but I couldn't quite understand what causes it:
[https://github.com/pytorch/pytorch/blob/main/torch/distributed/distributed_c10d.py#L2776](link)

I would really appreciate you help.

### Versions

PyTorch version: 1.13.1+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: CentOS Linux 7 (Core) (x86_64)
GCC version: (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Clang version: Could not collect
CMake version: version 2.8.12.2
Libc version: glibc-2.17

Python version: 3.7.16 (default, Jan 17 2023, 22:20:44)  [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-3.10.0-1160.88.1.el7.x86_64-x86_64-with-centos-7.9.2009-Core
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA GeForce GTX TITAN X
GPU 1: NVIDIA GeForce GTX TITAN X
GPU 2: NVIDIA GeForce GTX TITAN X
GPU 3: NVIDIA GeForce GTX TITAN X

Nvidia driver version: 470.42.01
cuDNN version: /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn.so.7
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                48
On-line CPU(s) list:   0-47
Thread(s) per core:    2
Core(s) per socket:    12
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 79
Model name:            Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz
Stepping:              1
CPU MHz:               2499.975
CPU max MHz:           2900.0000
CPU min MHz:           1200.0000
BogoMIPS:              4399.74
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              30720K
NUMA node0 CPU(s):     0-11,24-35
NUMA node1 CPU(s):     12-23,36-47
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 invpcid_single intel_ppin rsb_ctxsw ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap intel_pt xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts spec_ctrl intel_stibp

Versions of relevant libraries:
[pip3] numpy==1.21.5
[pip3] pytorch-lightning==1.9.5
[pip3] torch==1.13.1
[pip3] torchaudio==0.12.1
[pip3] torchmetrics==0.11.4
[pip3] torchvision==0.13.1
[conda] blas                      1.0                         mkl
[conda] cudatoolkit               11.3.1               h2bc3f7f_2
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2021.4.0           h06a4308_640
[conda] mkl-service               2.4.0            py37h7f8727e_0
[conda] mkl_fft                   1.3.1            py37hd3c417c_0
[conda] mkl_random                1.2.2            py37h51133e4_0
[conda] numpy                     1.21.5           py37h6c91a56_3
[conda] numpy-base                1.21.5           py37ha15fc14_3
[conda] pytorch-lightning         1.9.5                    pypi_0    pypi
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torch                     1.13.1                   pypi_0    pypi
[conda] torchaudio                0.12.1               py37_cu113    pytorch
[conda] torchmetrics              0.11.4                   pypi_0    pypi
[conda] torchvision               0.13.1               py37_cu113    pytorch

cc @zhaojuanmao @mrshenli @rohan-varma @awgu