Repository: pytorch
Issue: Memory usage increase post compilation for torch.compile ViT-H-14-quickgelu_dfn5b
Labels: triaged,oncall: pt2,module: inductor

### 🐛 Describe the bug

I'm running inference with torch compile for OpenCLIP model ViT-H-14-quickgelu_dfn5b, with half precision and on "cuda" device in "max-autotune" mode. My CPU memory usage has increased by 900MB-1GB as compared to the same model without torch compile. The increase persists during runtime after compilation and doesn't seem to reduce, see the plot below. Hypothesis is that the increase is due to the compilation matadata and alternate graphs.

I have a resource constrained setup, how do we solve this? What are the compilation settings we can use or steps we can add post compile to remove the built up CPU memory. Please refer to the code below and the logs attached. 


Code:
```
self.model = torch.load(model_weight_path)
self.model.to(device)
if use_torch_compile and device == "cuda":
    try:
        self.model = torch.compile(self.model, mode="max-autotune")
    except Exception as e:
        logging.warning(
            f"Failed to use torch compile: {e}, "
            "falling back to no torch compile"
        )

if fp16:
    self.model.half()
self.model.eval()
```

Following I have already tried with no luck:

1. `self.model = torch.compile(self.model, mode="reduced-overhead", fullgraph=True)`
2. `self.model = torch.compile(self.model, fullgraph=True)`
3. Post compile cleanup using:
```
# Cleanup compilation artifacts
import gc
import torch._dynamo

torch._dynamo.reset()
gc.collect()
if torch.cuda.is_available():
    torch.cuda.empty_cache()

```

<img width="1316" alt="image" src="https://github.com/user-attachments/assets/06c5ca4a-7cc7-44cc-b008-10cd8a1ee83d">


### Error logs

```
nvert: [INFO] Step 1: torchdynamo start tracing forward
[2024-11-15 09:01:05,575] torch._dynamo.symbolic_convert: [INFO] Step 1: torchdynamo done tracing forward (RETURN_VALUE)
[2024-11-15 09:01:05,596] torch._dynamo.output_graph: [INFO] Step 2: calling compiler function debug_wrapper
[2024-11-15 09:01:09,743] torch._inductor.compile_fx: [INFO] Step 3: torchinductor compiling FORWARDS graph 0
[2024-11-15 09:01:09,810] torch._inductor.utils: [WARNING] not enough cuda cores to use max_autotune mode
[2024-11-15 09:01:09,815] torch._inductor.graph: [INFO] Creating implicit fallback for:
  target: aten._scaled_dot_product_efficient_attention.default
  args[0]: TensorBox(
    View(
      PermuteView(data=View(
        View(
          SliceView(
            StorageBox(
              Pointwise(
                'cuda',
                torch.float16,
                tmp0 = load(buf13, i3 + 1280 * i0 + 3840 * i1)
                return tmp0
                ,
                ranges=[3, 257, 1, 1280],
                origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, arg1_1, view_3, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
              )
            ),
            size=[1, 257, 1, 1280],
            reindex=lambda i0, i1, i2, i3: [i0, i1, i2, i3],
            origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, view_3, arg1_1, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, select, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
          ),
          size=(257, 1, 1280),
          reindex=lambda i0, i1, i2: [0, i0, 0, i2],
          origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, view_3, arg1_1, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, select, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
        ),
        size=(257, 16, 80),
        reindex=lambda i0, i1, i2: [i0, 0, 80*i1 + i2],
        origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, arg1_1, view_3, view_2, convert_element_type_7, view_5, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, select, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
      ), dims=[1, 0, 2]),
      size=(1, 16, 257, 80),
      reindex=lambda i0, i1, i2, i3: [i1, i2, i3],
      origins={permute_4, convert_element_type_1, addmm, mul_2, convert_element_type_6, add, arg1_1, view_3, view_2, convert_element_type_7, view_5, view_8, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, select, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
    )
  )
  args[1]: TensorBox(
    View(
      PermuteView(data=View(
        View(
          SliceView(
            StorageBox(
              Pointwise(
                'cuda',
                torch.float16,
                tmp0 = load(buf13, i3 + 1280 * i0 + 3840 * i1)
                return tmp0
                ,
                ranges=[3, 257, 1, 1280],
                origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, arg1_1, view_3, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
              )
            ),
            size=[1, 257, 1, 1280],
            reindex=lambda i0, i1, i2, i3: [i0 + 1, i1, i2, i3],
            origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, view_3, arg1_1, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, select_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
          ),
          size=(257, 1, 1280),
          reindex=lambda i0, i1, i2: [0, i0, 0, i2],
          origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, view_3, arg1_1, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, select_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
        ),
        size=(257, 16, 80),
        reindex=lambda i0, i1, i2: [i0, 0, 80*i1 + i2],
        origins={convert_element_type_1, view_6, addmm, mul_2, convert_element_type_6, add, arg1_1, view_3, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, select_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
      ), dims=[1, 0, 2]),
      size=(1, 16, 257, 80),
      reindex=lambda i0, i1, i2, i3: [i1, i2, i3],
      origins={convert_element_type_1, view_6, addmm, mul_2, convert_element_type_6, permute_5, add, arg1_1, view_3, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, view_9, arg3_1, convert_element_type_3, arg2_1, select_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
    )
  )
  args[2]: TensorBox(
    View(
      PermuteView(data=View(
        View(
          SliceView(
            StorageBox(
              Pointwise(
                'cuda',
                torch.float16,
                tmp0 = load(buf13, i3 + 1280 * i0 + 3840 * i1)
                return tmp0
                ,
                ranges=[3, 257, 1, 1280],
                origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, arg1_1, view_3, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, squeeze}
              )
            ),
            size=[1, 257, 1, 1280],
            reindex=lambda i0, i1, i2, i3: [i0 + 2, i1, i2, i3],
            origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, view_3, arg1_1, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, select_2, squeeze}
          ),
          size=(257, 1, 1280),
          reindex=lambda i0, i1, i2: [0, i0, 0, i2],
          origins={convert_element_type_1, addmm, mul_2, convert_element_type_6, add, view_3, arg1_1, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, select_2, squeeze}
        ),
        size=(257, 16, 80),
        reindex=lambda i0, i1, i2: [i0, 0, 80*i1 + i2],
        origins={convert_element_type_1, addmm, mul_2, view_7, convert_element_type_6, add, arg1_1, view_3, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, select_2, squeeze}
      ), dims=[1, 0, 2]),
      size=(1, 16, 257, 80),
      reindex=lambda i0, i1, i2, i3: [i1, i2, i3],
      origins={convert_element_type_1, view_10, addmm, mul_2, view_7, convert_element_type_6, add, arg1_1, view_3, view_2, convert_element_type_7, permute_3, rsqrt_1, arg5_1, add_3, convert_element_type_2, sub_1, var_mean, arg4_1, convert_element_type_5, arg3_1, convert_element_type_3, arg2_1, permute_1, permute_6, convert_element_type_4, var_mean_1, view_4, clone, mul_1, add_2, unsqueeze, arg136_1, convert_element_type_8, rsqrt, permute_2, add_4, arg137_1, mul_3, cat, sub, mul, add_1, select_2, squeeze}
    )
  )
  args[3]: False
[2024-11-15 09:01:09,820] torch._inductor.graph: [INFO] Using FallbackKernel: torch.ops.aten._scaled_dot_product_efficient_attention.default
[2024-11-15 09:01:16,529] torch._inductor.compile_fx: [INFO] Step 3: torchinductor done compiling FORWARDS graph 0
[2024-11-15 09:01:16,530] torch._dynamo.output_graph: [INFO] Step 2: done compiler function debug_wrapper

```

### Versions

```
python3 collect_env.py
Collecting environment information...

PyTorch version: 2.0.0a0+gitc263bd4
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.5 LTS (x86_64)
GCC version: (Ubuntu 8.4.0-3ubuntu2) 8.4.0
Clang version: 10.0.0-4ubuntu1 
CMake version: version 3.30.2
Libc version: glibc-2.31

Python version: 3.10.0 (default, Nov  1 2024, 22:46:53) [GCC 8.4.0] (64-bit runtime)
Python platform: Linux-6.5.0-28-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA RTX A2000 12GB
Nvidia driver version: 535.161.07
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.6.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.6.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Byte Order:                         Little Endian
Address sizes:                      46 bits physical, 48 bits virtual
CPU(s):                             24
On-line CPU(s) list:                0-23
Thread(s) per core:                 1
Core(s) per socket:                 16
Socket(s):                          1
NUMA node(s):                       1
Vendor ID:                          GenuineIntel
CPU family:                         6
Model:                              183
Model name:                         13th Gen Intel(R) Core(TM) i7-13700
Stepping:                           1
CPU MHz:                            1786.068
CPU max MHz:                        5200.0000
CPU min MHz:                        800.0000
BogoMIPS:                           4224.00
Virtualization:                     VT-x
L1d cache:                          384 KiB
L1i cache:                          256 KiB
L2 cache:                           16 MiB
NUMA node0 CPU(s):                  0-23
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: Not affected
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; Enhanced / Automatic IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected
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 art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq tme rdpid movdiri movdir64b fsrm md_clear serialize pconfig arch_lbr ibt flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] flake8==4.0.1
[pip3] mypy==1.1.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.2
[pip3] onnx==1.16.0
[pip3] onnxruntime-gpu==1.17.1
[pip3] open-clip-torch==2.24.0
[pip3] torch==2.0.0a0+gitc263bd4
[pip3] torchvision==0.15.1a0+42759b1
[pip3] triton==2.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 @aakhundov