Repository: pytorch
Issue: Pluggable Allocator Does not raise torch.cuda.OutOfMemoryError
Labels: module: cuda,module: error checking,module: memory usage,triaged

### 🐛 Describe the bug

When using a pluggable allocator in PyTorch (e.g., RMM), the torch.cuda.OutOfMemoryError is not raised upon GPU memory exhaustion. Instead, a generic RuntimeError is thrown, which means the error handling in downstream code bases needs to depend on catching the generic error instead of a more precise error. 

**Current Behavior:**

```python3
import torch
import gc

ENABLE_RMM =True
if ENABLE_RMM:
    import rmm
    from rmm.allocators.torch import rmm_torch_allocator
    torch.cuda.memory.change_current_allocator(rmm_torch_allocator)

try:
    large_tensor = torch.randn(100000, 100000, device='cuda')
    print("Tensor created successfully.")
except torch.cuda.OutOfMemoryError as e:
    print("Caught Torch Cuda OutOfMemoryError")
except RuntimeError as e:
    print("Caught generic RuntimeError")
    print(e)
finally:
    gc.collect()
    torch.cuda.empty_cache()

```

```python3
Caught generic RuntimeError
std::bad_alloc: out_of_memory: CUDA error at: /tmp/pip-build-env-80cmzetq/overlay/lib/python3.11/site-packages/librmm/include/rmm/mr/device/cuda_memory_resource.hpp:60: cudaErrorMemoryAllocation out of memory
```

Expected Behavior:

```python3
import torch
import gc

ENABLE_RMM=False
if ENABLE_RMM:
    import rmm
    from rmm.allocators.torch import rmm_torch_allocator
    torch.cuda.memory.change_current_allocator(rmm_torch_allocator)

try:
    # Attempt to create a large tensor on the GPU
    large_tensor = torch.randn(100000, 100000, device='cuda')
    print("Tensor created successfully.")
except torch.cuda.OutOfMemoryError as e:
    print("Caught Torch Cuda OutOfMemoryError")
except RuntimeError as e:
    print("Caught generic RuntimeError ERROR")
    print(e)
finally:
    # Clean up by freeing GPU memory if necessary
    gc.collect()
    torch.cuda.empty_cache()
```

```
Caught Torch Cuda OutOfMemoryError
```



### Versions

```python3

Collecting environment information...
PyTorch version: 2.3.1.post300
Is debug build: False
CUDA used to build PyTorch: 12.0
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: version 3.16.3
Libc version: glibc-2.31

Python version: 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0] (64-bit runtime)
Python platform: Linux-5.4.0-189-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 12.0.76
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: Tesla V100-SXM3-32GB
GPU 1: Tesla V100-SXM3-32GB
GPU 2: Tesla V100-SXM3-32GB
GPU 3: Tesla V100-SXM3-32GB
GPU 4: Tesla V100-SXM3-32GB
GPU 5: Tesla V100-SXM3-32GB
GPU 6: Tesla V100-SXM3-32GB
GPU 7: Tesla V100-SXM3-32GB
GPU 8: Tesla V100-SXM3-32GB
GPU 9: Tesla V100-SXM3-32GB
GPU 10: Tesla V100-SXM3-32GB
GPU 11: Tesla V100-SXM3-32GB
GPU 12: Tesla V100-SXM3-32GB
GPU 13: Tesla V100-SXM3-32GB
GPU 14: Tesla V100-SXM3-32GB
GPU 15: Tesla V100-SXM3-32GB

Nvidia driver version: Could not collect
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
Byte Order:                         Little Endian
Address sizes:                      46 bits physical, 48 bits virtual
CPU(s):                             96
On-line CPU(s) list:                0-95
Thread(s) per core:                 2
Core(s) per socket:                 24
Socket(s):                          2
NUMA node(s):                       2
Vendor ID:                          GenuineIntel
CPU family:                         6
Model:                              85
Model name:                         Intel(R) Xeon(R) Platinum 8168 CPU @ 2.70GHz
Stepping:                           4
CPU MHz:                            3304.656
CPU max MHz:                        3700.0000
CPU min MHz:                        1200.0000
BogoMIPS:                           5400.00
Virtualization:                     VT-x
L1d cache:                          1.5 MiB
L1i cache:                          1.5 MiB
L2 cache:                           48 MiB
L3 cache:                           66 MiB
NUMA node0 CPU(s):                  0-23,48-71
NUMA node1 CPU(s):                  24-47,72-95
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit:        KVM: Mitigation: Split huge pages
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:             Mitigation; IBRS
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; IBRS; IBPB conditional; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Mitigation; Clear CPU buffers; SMT vulnerable
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 pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm 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 hwp hwp_act_window hwp_epp hwp_pkg_req pku ospke md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] flake8==7.1.1
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.26.4
[pip3] torch==2.3.1.post300
[pip3] torchvision==0.18.0a0+c6f1f0d
[conda] libmagma                  2.7.2                h173bb3b_2    conda-forge
[conda] libmagma_sparse           2.7.2                h173bb3b_3    conda-forge
[conda] libtorch                  2.3.1           cuda120_h2b0da52_300    conda-forge
[conda] mkl                       2023.2.0         h84fe81f_50496    conda-forge
[conda] numpy                     1.26.4          py311h64a7726_0    conda-forge
[conda] pytorch                   2.3.1           cuda120_py311hf6aebf0_300    conda-forge
[conda] torchvision               0.18.0          cuda120py311hdb12af6_0    conda-forge
```

cc @ptrblck @msaroufim @malfet