Repository navigation
cuda.core: raise CUDAOutOfMemoryError, a MemoryError, on out-of-memory - #3235
srimanachanta wants to merge 1 commit into
Conversation
CUDA_ERROR_OUT_OF_MEMORY and cudaErrorMemoryAllocation now raise CUDAOutOfMemoryError, a subclass of both CUDAError and MemoryError, so `except MemoryError` works for cuda.core allocations as it does for CuPy and PyTorch. Code that catches CUDAError is unaffected.
📝 Summary
Priority: Change: Feature Merge Risk:
|
There was a problem hiding this comment.
Actionable comments posted: 1

Review info
-
Run configuration-
- Source mode: Auto
- Repository: NVIDIA/cuda-python
- Branch: main
- File: .coderabbit.yaml
- Commit: 590a8bf
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Enterprise
- Run ID:
deb66916-c5af-44de-827c-e6ebbf1830a5
Files selected for processing: 8-
cuda_core/cuda/core/__init__.pycuda_core/cuda/core/_utils/cuda_utils.pyicuda_core/cuda/core/_utils/cuda_utils.pyxcuda_core/docs/source/api.rstcuda_core/docs/source/error_handling.rstcuda_core/docs/source/release/1.3.0-notes.rstcuda_core/tests/test_cuda_utils.pycuda_core/tests/test_memory.py
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 8 remain after this review.
-
| :class:`CUDAError` and :class:`MemoryError`. All ``cuda.core`` classes are | ||
| importable from the top-level ``cuda.core`` namespace. Invalid arguments and misuse raise the |
There was a problem hiding this comment.
suggestion: Limit the importability claim to these exception classes. The sentence says all cuda.core classes are available at the top level. Classes such as Graph instead live in cuda.core.graph, so a reader following this claim can use an invalid import. Replace “All cuda.core classes” with “These exception classes.” (raw.githubusercontent.com)
Summary
Add
cuda.core.CUDAOutOfMemoryError(CUDAError, MemoryError).CUDA_ERROR_OUT_OF_MEMORYandcudaErrorMemoryAllocationnow raise it instead of a plainCUDAError.Motivation
Allocating too much from a
DeviceMemoryResourceraisesCUDAError, which derives only fromException:Other libraries raise a
MemoryErrorsubclass: CuPy'sOutOfMemoryError, PyTorch'storch.cuda.OutOfMemoryError, and NumPy'sMemoryError. Code that recovers from running out of memory by asking for less catchesMemoryError. A downstream CUDA extension library defines its ownCudaOutOfMemoryfor this reason (common.hpp, registered as aMemoryErrorin bindings.cpp), and its sweep halves the chunk onexcept MemoryError(simulation.py). That retry does not see out-of-memory errors fromcuda.core.Change
_check_driver_errorand_check_runtime_errorpickCUDAOutOfMemoryErrorfor the two out-of-memory codes. Every driver and runtime failure incuda.core, including the C++ handle layer throughHANDLE_RETURN(get_last_error()), goes through these two functions.CUDAErroris unaffected.cuda.core, added toapi.rst, the error handling page and the 1.3.0 release notes.NVRTC's
NVRTC_ERROR_OUT_OF_MEMORYstill raisesNVRTCError. I left it out to keep the change to driver and runtime errors.Tests
CUDAOutOfMemoryError, which is both aCUDAErrorand aMemoryError.MemoryError.DeviceMemoryResourceraisesMemoryError, and the pool stays usable.test_cuda_utils.py,test_error_handling.py,test_api_docs_consistency.pyandtest_memory.pypass locally (RTX 5070 Ti, driver 615, CUDA 13.4).