Repository: pytorch
Issue: cudaMallocAsync cause too much fragmentation.
Labels: module: cuda,module: memory usage,triaged,module: CUDACachingAllocator

### 🐛 Describe the bug

I try to use [CUDA’s built-in asynchronous allocator](https://developer.nvidia.com/blog/using-cuda-stream-ordered-memory-allocator-part-1/) by setting `PYTORCH_CUDA_ALLOC_CONF=backend:cudaMallocAsync`. Then I encounter OOM with over 20GB memory 'missing':
```
torch.cuda.OutOfMemoryError: Allocation on device 5 would exceed allowed memory. (out of memory)
Currently allocated     : 54.14 GiB
Requested               : 686.41 MiB
Device limit            : 79.35 GiB
Free (according to CUDA): 27.19 MiB
PyTorch limit (set by user-supplied memory fraction)
                        : 17179869184.00 GiB
```
This dose not like what the [post](https://developer.nvidia.com/blog/using-cuda-stream-ordered-memory-allocator-part-1/) saying:

If a memory allocation request made using cudaMallocAsync can’t be serviced due to fragmentation of the corresponding memory pool, the CUDA driver defragments the pool by remapping unused memory in the pool to a contiguous portion of the GPU’s virtual address space. Remapping existing pool memory instead of allocating new memory from the OS also helps keep the application’s memory footprint low.

This task can barely run with PyTorch's allocator, yet, still with significant fragmentation:
```
(after 3 iterations) memory (MB) | allocated: 50900.0244140625 | max allocated: 68742.84326171875 | reserved: 76064.0 | max reserved: 76064.0
```



### Versions

Collecting environment information...
PyTorch version: 2.0.1+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Alibaba Group Enterprise Linux Server 7.2 (Paladin) (x86_64)
GCC version: (GCC) 8.3.1 20190311 (Red Hat 8.3.1-3)
Clang version: Could not collect
CMake version: version 3.27.4
Libc version: glibc-2.32

Python version: 3.8.13 (default, Mar 28 2022, 11:38:47)  [GCC 7.5.0] (64-bit runtime)
Python platform: 
Is CUDA available: True
CUDA runtime version: 11.7.64
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration:
GPU 0: NVIDIA A100-SXM4-80GB
GPU 1: NVIDIA A100-SXM4-80GB
GPU 2: NVIDIA A100-SXM4-80GB
GPU 3: NVIDIA A100-SXM4-80GB
GPU 4: NVIDIA A100-SXM4-80GB
GPU 5: NVIDIA A100-SXM4-80GB
GPU 6: NVIDIA A100-SXM4-80GB
GPU 7: NVIDIA A100-SXM4-80GB

Nvidia driver version: 470.154
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.8.9.1
/usr/lib64/libcudnn_adv_infer.so.8.9.1
/usr/lib64/libcudnn_adv_train.so.8.9.1
/usr/lib64/libcudnn_cnn_infer.so.8.9.1
/usr/lib64/libcudnn_cnn_train.so.8.9.1
/usr/lib64/libcudnn_ops_infer.so.8.9.1
/usr/lib64/libcudnn_ops_train.so.8.9.1
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):                128
On-line CPU(s) list:   0-75,124-127
Off-line CPU(s) list:  76-123
Thread(s) per core:    1
Core(s) per socket:    32
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 106
Model name:            Intel(R) Xeon(R) Platinum 8369B CPU @ 2.90GHz
Stepping:              6
CPU MHz:               3440.970
CPU max MHz:           3500.0000
CPU min MHz:           800.0000
BogoMIPS:              5800.00
Virtualization:        VT-x
L1d cache:             48K
L1i cache:             32K
L2 cache:              1280K
L3 cache:              49152K
NUMA node0 CPU(s):     0-31,64-95
NUMA node1 CPU(s):     32-63,96-127
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 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq rdpid fsrm md_clear pconfig flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.19.5
[pip3] torch==2.0.1+cu117
[pip3] torchaudio==2.0.2+cu117
[pip3] torchmetrics==1.1.2
[pip3] torchvision==0.15.2+cu117
[pip3] triton==2.0.0
[conda] magma-cuda117             2.6.1                         1    pytorch
[conda] mkl                       2023.2.0                 pypi_0    pypi
[conda] mkl-include               2023.2.0                 pypi_0    pypi
[conda] numpy                     1.19.5                   pypi_0    pypi
[conda] torch                     2.0.1+cu117              pypi_0    pypi
[conda] torchaudio                2.0.2+cu117              pypi_0    pypi
[conda] torchmetrics              1.1.2                    pypi_0    pypi
[conda] torchvision               0.15.2+cu117             pypi_0    pypi
[conda] triton                    2.0.0                    pypi_0    pypi

cc @ptrblck