Repository: pytorch
Issue: OOM with mixed precision with OPT1.3B Decoder
Labels: triaged,module: amp (automated mixed precision)

### 🐛 Describe the bug

I am doing simple forward pass on HF OPT1.3B model. With (1, 2048) input size, FP32 forward pass works, but same model same input with mixed precision throws OOM on 40GB A100. I tried the same on 48GB A6000 and same happens with (2, 1664)  input size. Furthermore, with (1, 1024) input size 100 forward passes  FP32 took 21 seconds using 23GB memory, fp16 4.74 seconds using 27GBs memory.
Simple code to reproduce the issue.
```python
from transformers import AutoModelForCausalLM
import torch
model = AutoModelForCausalLM.from_pretrained('facebook/opt-1.3b').cuda()
inpt = torch.randint(0, 1000, (1, 2048))
with torch.autocast(device_type='cuda', dtype=torch.float16):
    out = model(inpt.cuda())  # OOM
```
But this works
```python
from transformers import AutoModelForCausalLM
import torch
model = AutoModelForCausalLM.from_pretrained('facebook/opt-1.3b').cuda()
inpt = torch.randint(0, 1000, (1, 2048))
out = model(inpt.cuda())
```
Is it expected to see more memory usage with fp16 prediction? Thanks.

### Versions

Collecting environment information...
PyTorch version: 2.2.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.27.4
Libc version: glibc-2.35

Python version: 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:08:06) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.15.0-87-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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
Address sizes:                      46 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             40
On-line CPU(s) list:                0-39
Vendor ID:                          GenuineIntel
Model name:                         Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
CPU family:                         6
Model:                              79
Thread(s) per core:                 2
Core(s) per socket:                 10
Socket(s):                          2
Stepping:                           1
CPU max MHz:                        3100,0000
CPU min MHz:                        1200,0000
BogoMIPS:                           4389.85
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 cpuid aperfmperf 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 cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad 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 md_clear flush_l1d
Virtualization:                     VT-x
L1d cache:                          640 KiB (20 instances)
L1i cache:                          640 KiB (20 instances)
L2 cache:                           5 MiB (20 instances)
L3 cache:                           50 MiB (2 instances)
NUMA node(s):                       2
NUMA node0 CPU(s):                  0-9,20-29
NUMA node1 CPU(s):                  10-19,30-39
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        KVM: Mitigation: VMX disabled
Vulnerability L1tf:                 Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:                  Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:             Mitigation; PTI
Vulnerability Mmio stale data:      Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Retpolines, IBPB conditional, IBRS_FW, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Mitigation; Clear CPU buffers; SMT vulnerable

Versions of relevant libraries:
[pip3] numpy==1.24.3
[pip3] pytorch-lightning==2.0.9
[pip3] torch==2.2.1
[pip3] torch-pruning==1.3.6
[pip3] torchmetrics==1.2.0
[pip3] torchvision==0.16.2
[pip3] triton==2.2.0
[conda] numpy                     1.24.3                   pypi_0    pypi
[conda] pytorch-lightning         2.0.9                    pypi_0    pypi
[conda] torch                     2.0.1                    pypi_0    pypi
[conda] torch-pruning             1.3.6                    pypi_0    pypi
[conda] torchmetrics              1.2.0                    pypi_0    pypi
[conda] torchvision               0.16.2                   pypi_0    pypi
[conda] triton                    2.2.0                    pypi_0    pypi


cc @mcarilli @ptrblck @leslie-fang-intel @jgong5