Repository: pytorch
Issue: torch.compile makes model slower
Labels: triaged,oncall: pt2,module: inductor

### 🐛 Describe the bug

NOTE: we are only interested in compiling the decoder, the encoder is also shown in the time traces but should be ignored.

I've been trying for a long time to get `torch.compile` to work as advertised, but I've been struggling to get anything close to what others claim. In fact, `torch.compile` makes my model slower.

You can see the model source code and find instructions on how to reproduce my environment at https://github.com/alita-moore/img-to-text and you can see the torch trace logs [here](https://gist.github.com/alita-moore/b55bac79c5b1fdacbdf01e6b21f5a4f5).

See a snippet of the timing graph here (of the decoder):
<img width="974" alt="Screenshot 2024-08-31 at 10 51 24 PM" src="https://github.com/user-attachments/assets/e2ab688a-c00e-4f29-aa80-5b2cf88591ea">
You'll have to generate the timing graph yourself because the file is too large to upload.

Then here are the relevant log outputs detailing the timing between the compile and uncompiled runs:

## Decoder timing

The following are the rough timings from the forward pass of the decoder:

### Uncompiled timing (~7ms / forward pass)
```
DEBUG:root:---- Prepared inputs for generation: 0.00019s
DEBUG:root:---- Generated token: 0.00694s
DEBUG:root:---- Selected token: 0.00067s
DEBUG:root:---- Prepared inputs for generation: 0.00019s (2.11% of Next token)
DEBUG:root:---- Generated token: 0.00694s (78.24% of Next token)
DEBUG:root:---- Selected token: 0.00067s (7.55% of Next token)
DEBUG:root:-- Generated token 4: 0.01087s
DEBUG:root:---- Prepared inputs for generation: 0.00013s
DEBUG:root:---- Generated token: 0.00649s
DEBUG:root:---- Selected token: 0.00055s
DEBUG:root:---- Prepared inputs for generation: 0.00013s (1.52% of Next token)
DEBUG:root:---- Generated token: 0.00649s (76.47% of Next token)
DEBUG:root:---- Selected token: 0.00055s (6.44% of Next token)
DEBUG:root:-- Generated token 5: 0.01054s
DEBUG:root:---- Prepared inputs for generation: 0.00012s
DEBUG:root:---- Generated token: 0.00701s
DEBUG:root:---- Selected token: 0.00054s
DEBUG:root:---- Prepared inputs for generation: 0.00012s (1.34% of Next token)
DEBUG:root:---- Generated token: 0.00701s (80.23% of Next token)
DEBUG:root:---- Selected token: 0.00054s (6.18% of Next token)
DEBUG:root:-- Generated token 6: 0.01016s
DEBUG:root:---- Prepared inputs for generation: 0.00012s
DEBUG:root:---- Generated token: 0.00697s
DEBUG:root:---- Selected token: 0.00062s
DEBUG:root:---- Prepared inputs for generation: 0.00012s (1.36% of Next token)
DEBUG:root:---- Generated token: 0.00696s (78.63% of Next token)
```

### Compiled timing (~8ms / forward pass)
```
DEBUG:root:---- Prepared inputs for generation: 0.00015s
DEBUG:root:---- Generated token: 0.35622s
DEBUG:root:---- Selected token: 0.00086s
DEBUG:root:---- Prepared inputs for generation: 0.00015s (0.04% of Next token)
DEBUG:root:---- Generated token: 0.35621s (99.31% of Next token)
DEBUG:root:---- Selected token: 0.00086s (0.24% of Next token)
DEBUG:root:-- Generated token 2: 0.36042s
DEBUG:root:---- Prepared inputs for generation: 0.00023s
DEBUG:root:---- Generated token: 0.00825s
DEBUG:root:---- Selected token: 0.00071s
DEBUG:root:---- Prepared inputs for generation: 0.00023s (2.15% of Next token)
DEBUG:root:---- Generated token: 0.00825s (77.41% of Next token)
DEBUG:root:---- Selected token: 0.00071s (6.64% of Next token)
DEBUG:root:-- Generated token 3: 0.01238s
DEBUG:root:---- Prepared inputs for generation: 0.00020s
DEBUG:root:---- Generated token: 0.00818s
DEBUG:root:---- Selected token: 0.00067s
DEBUG:root:---- Prepared inputs for generation: 0.00020s (1.90% of Next token)
DEBUG:root:---- Generated token: 0.00818s (77.62% of Next token)
DEBUG:root:---- Selected token: 0.00067s (6.39% of Next token)
```


### Versions

Collecting environment information...
PyTorch version: 2.4.0+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.29.2
Libc version: glibc-2.35

Python version: 3.10.12 (main, Jul 29 2024, 16:56:48) [GCC 11.4.0] (64-bit runtime)
Python platform: Linux-6.5.0-1022-aws-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.4.131
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA L4
Nvidia driver version: 560.35.03
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.9.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.1.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.1.0
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:                      48 bits physical, 48 bits virtual
Byte Order:                         Little Endian
CPU(s):                             4
On-line CPU(s) list:                0-3
Vendor ID:                          AuthenticAMD
Model name:                         AMD EPYC 7R13 Processor
CPU family:                         25
Model:                              1
Thread(s) per core:                 2
Core(s) per socket:                 2
Socket(s):                          1
Stepping:                           1
BogoMIPS:                           5299.99
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf tsc_known_freq pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm cmp_legacy cr8_legacy abm sse4a misalignsse 3dnowprefetch topoext invpcid_single ssbd ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 clzero xsaveerptr rdpru wbnoinvd arat npt nrip_save vaes vpclmulqdq rdpid
Hypervisor vendor:                  KVM
Virtualization type:                full
L1d cache:                          64 KiB (2 instances)
L1i cache:                          64 KiB (2 instances)
L2 cache:                           1 MiB (2 instances)
L3 cache:                           8 MiB (1 instance)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-3
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Not affected
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: Vulnerable: Safe RET, no microcode
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==2.1.0
[pip3] torch==2.4.0
[pip3] torchaudio==2.4.0
[pip3] torchvision==0.19.0
[pip3] triton==3.0.0
[conda] Could not collect

cc @ezyang @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire