Repository: pytorch
Issue: torch.cuda.empty_cache() causes extra memory usage on 'cuda:0'
Labels: module: cuda,triaged,module: CUDACachingAllocator

### 🐛 Describe the bug

# Issue Description:
When utilizing PyTorch with a specific CUDA device (in this case, 'cuda:8'), calling `torch.cuda.empty_cache()` unexpectedly results in additional memory allocation on 'cuda:0', approximately 255MB. This behavior is contrary to expectations, as the operation should ideally only affect the memory cache of the specified device ('cuda:8') and not impact other CUDA devices.

# Code
```python
import numpy as np
import torch

a = np.ones(100)
b = torch.tensor(a).to('cuda:8')
torch.cuda.empty_cache()
# The previous behavior is normal, only occupying the video memory on cuda:8.

del b
torch.cuda.empty_cache() #At this time, about 255MB of video memory on cuda:0 is occupied.
```
# Implications in Multi-GPU Clusters:
This characteristic/bug can pose significant challenges in multi-GPU clusters, especially in shared environments among multiple users. The unintended memory allocation on 'cuda:0' can lead to its memory being exhausted, thereby preventing all users from performing their tasks effectively.

# Environment Details:

- **NVIDIA-SMI Version**: 550.54.14
- **Driver Version**: 550.54.14
- **CUDA Version**: 12.4
- **Hardware**: NVIDIA GeForce RTX 3090

### Versions

PyTorch version: 2.4.1.post303
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.4 LTS (x86_64)
GCC version: (Anaconda gcc) 11.2.0
Clang version: Could not collect
CMake version: version 3.30.0-rc4
Libc version: glibc-2.35

Python version: 3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:36:39) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-6.5.0-41-generic-x86_64-with-glibc2.35
Is CUDA available: True
CUDA runtime version: 12.4.99
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA GeForce RTX 3090
GPU 1: NVIDIA GeForce RTX 3090
GPU 2: NVIDIA GeForce RTX 3090
GPU 3: NVIDIA GeForce RTX 3090
GPU 4: NVIDIA GeForce RTX 3090
GPU 5: NVIDIA GeForce RTX 3090
GPU 6: NVIDIA GeForce RTX 3090
GPU 7: NVIDIA GeForce RTX 3090
GPU 8: NVIDIA GeForce RTX 3090
GPU 9: NVIDIA GeForce RTX 3090

Nvidia driver version: 550.54.14
cuDNN version: Could not collect
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):                             104
On-line CPU(s) list:                0-103
Vendor ID:                          GenuineIntel
Model name:                         Intel(R) Xeon(R) Platinum 8269CY CPU @ 2.50GHz
CPU family:                         6
Model:                              85
Thread(s) per core:                 2
Core(s) per socket:                 26
Socket(s):                          2
Stepping:                           7
CPU max MHz:                        3800.0000
CPU min MHz:                        1200.0000
BogoMIPS:                           5000.00
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 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 intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts vnmi pku ospke avx512_vnni md_clear flush_l1d arch_capabilities
Virtualization:                     VT-x
L1d cache:                          1.6 MiB (52 instances)
L1i cache:                          1.6 MiB (52 instances)
L2 cache:                           52 MiB (52 instances)
L3 cache:                           71.5 MiB (2 instances)
NUMA node(s):                       2
NUMA node0 CPU(s):                  0-25,52-77
NUMA node1 CPU(s):                  26-51,78-103
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit:        KVM: Mitigation: VMX disabled
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:             Mitigation; Enhanced IBRS
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; BHI Syscall hardening, KVM SW loop
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Mitigation; TSX disabled

Versions of relevant libraries:
[pip3] gpytorch==1.13
[pip3] numpy==1.26.4
[pip3] numpy-groupies==0.10.2
[pip3] numpyro==0.13.2
[pip3] nvidia-cublas-cu12==12.1.3.1
[pip3] nvidia-cuda-cupti-cu12==12.1.105
[pip3] nvidia-cuda-nvrtc-cu12==12.1.105
[pip3] nvidia-cuda-runtime-cu12==12.1.105
[pip3] nvidia-cudnn-cu12==8.9.2.26
[pip3] nvidia-cufft-cu12==11.0.2.54
[pip3] nvidia-curand-cu12==10.3.2.106
[pip3] nvidia-cusolver-cu12==11.4.5.107
[pip3] nvidia-cusparse-cu12==12.1.0.106
[pip3] nvidia-nccl-cu12==2.19.3
[pip3] nvidia-nvjitlink-cu12==12.3.101
[pip3] nvidia-nvtx-cu12==12.1.105
[pip3] pytorch-ignite==0.5.0.post2
[pip3] pytorch-lightning==2.2.0.post0
[pip3] torch==2.4.1.post303
[pip3] torch-geometric==2.6.1
[pip3] torchaudio==2.4.1
[pip3] torchmetrics==1.4.0.post0
[pip3] torchvision==0.19.1
[pip3] triton==2.2.0
[conda] cuda-cudart               12.1.105                      0    nvidia
[conda] cuda-cupti                12.1.105                      0    nvidia
[conda] cuda-libraries            12.1.0                        0    nvidia
[conda] cuda-nvrtc                12.1.105                      0    nvidia
[conda] cuda-nvtx                 12.1.105                      0    nvidia
[conda] cuda-opencl               12.3.101                      0    nvidia
[conda] cuda-runtime              12.1.0                        0    nvidia
[conda] cudatoolkit               11.8.0               h6a678d5_0  
[conda] cudnn                     9.3.0.75               cuda11.8    nvidia
[conda] gpytorch                  1.13                     pypi_0    pypi
[conda] libcublas                 12.1.0.26                     0    nvidia
[conda] libcufft                  11.0.2.4                      0    nvidia
[conda] libcurand                 10.3.4.107                    0    nvidia
[conda] libcusolver               11.4.4.55                     0    nvidia
[conda] libcusparse               12.0.2.55                     0    nvidia
[conda] libmagma                  2.8.0                hfdb99dd_0    conda-forge
[conda] libmagma_sparse           2.8.0                h9ddd185_0    conda-forge
[conda] libnvjitlink              12.1.105                      0    nvidia
[conda] libopenvino-pytorch-frontend 2024.4.0             h5888daf_2    conda-forge
[conda] libtorch                  2.4.1           cuda118_h232d35b_303    conda-forge
[conda] mkl                       2024.1.0                 pypi_0    pypi
[conda] mkl-service               2.4.0           py310h5eee18b_1  
[conda] mkl_fft                   1.3.8           py310ha3dbc2a_1    conda-forge
[conda] mkl_random                1.2.5           py310hbd113e2_1    conda-forge
[conda] nccl                      2.23.4.1             h03a54cd_2    conda-forge
[conda] numpy                     1.25.2                   pypi_0    pypi
[conda] numpy-base                1.26.4          py310h8a23956_0  
[conda] numpy-groupies            0.10.2                   pypi_0    pypi
[conda] numpyro                   0.13.2             pyhd8ed1ab_0    conda-forge
[conda] nvidia-cublas-cu12        12.1.3.1                 pypi_0    pypi
[conda] nvidia-cuda-cupti-cu12    12.1.105                 pypi_0    pypi
[conda] nvidia-cuda-nvrtc-cu12    12.1.105                 pypi_0    pypi
[conda] nvidia-cuda-runtime-cu12  12.1.105                 pypi_0    pypi
[conda] nvidia-cudnn-cu12         8.9.2.26                 pypi_0    pypi
[conda] nvidia-cufft-cu12         11.0.2.54                pypi_0    pypi
[conda] nvidia-curand-cu12        10.3.2.106               pypi_0    pypi
[conda] nvidia-cusolver-cu12      11.4.5.107               pypi_0    pypi
[conda] nvidia-cusparse-cu12      12.1.0.106               pypi_0    pypi
[conda] nvidia-nccl-cu12          2.19.3                   pypi_0    pypi
[conda] nvidia-nvjitlink-cu12     12.3.101                 pypi_0    pypi
[conda] nvidia-nvtx-cu12          12.1.105                 pypi_0    pypi
[conda] pyg                       2.6.1           py310_torch_2.4.0_cu121    pyg
[conda] pytorch                   2.4.1           cuda118_py310h8b36b8a_303    conda-forge
[conda] pytorch-cuda              12.1                 ha16c6d3_5    pytorch
[conda] pytorch-ignite            0.5.0.post2              pypi_0    pypi
[conda] pytorch-lightning         2.2.0.post0              pypi_0    pypi
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.4.1               py310_cu121    pytorch
[conda] torchmetrics              1.3.1                    pypi_0    pypi
[conda] torchtriton               2.2.0                     py310    pytorch
[conda] torchvision               0.19.1              py310_cu121    pytorch
[conda] triton                    2.2.0                    pypi_0    pypi

cc @ptrblck @msaroufim @eqy