Repository navigation
cuda.core: raise CUDAOutOfMemoryError, a MemoryError, on out-of-memory - #3235
srimanachanta wants to merge 1 commit into
Conversation
📝 Summary
Priority: Change: Bug fix · Severity of issue fixed: Medium 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)
Andy-Jost
left a comment
There was a problem hiding this comment.
Thanks, the design holds up. CUDAOutOfMemoryError(CUDAError, MemoryError) keeps every existing except CUDAError working, the mapping happens once in the driver and runtime raise paths that HANDLE_RETURN, handle_return, and _check_error all funnel through, cuda.core itself never catches MemoryError, and the end-to-end test asks a capped pool for 64 TiB, so it fails at once without touching real device memory. On the question in #3260: raising a MemoryError subclass for CUDA out-of-memory is fine with me, and it matches what CuPy and RMM users expect.
Before merge:
- Rebase onto
main. Since #3255,_RUNTIME_SUCCESSand_NVRTC_SUCCESSare declared incuda_utils.pxdand assigned as plain globals in the.pyx, and the check functions arecpdef inlinein the.pxd. The only conflict is the constant block incuda_utils.pyx: keep main's two assignments and add_RUNTIME_OUT_OF_MEMORYbeside them. Yourexc_typeselection merges cleanly into the renamed_raise_driver_errorand_raise_runtime_error. No CI has run on this PR yet; after the rebase the workflows are approved and it runs. - The release note (and the commit message) say
except MemoryErrorworks "as it does in CuPy, PyTorch and NumPy". PyTorch raisestorch.OutOfMemoryError, aRuntimeError, as #3260 itself notes; please name CuPy, RMM, and NumPy. error_handling.rst: "All cuda.core classes are importable from the top-level namespace" reads as every class, and graph classes, for one, live incuda.core.graph. "These exception classes are importable from the top-levelcuda.corenamespace" says what you mean; please rewrap the paragraph too.
b7b0f23 to
90abbed
Compare
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, RMM and NumPy. Code that catches CUDAError is unaffected.
90abbed to
6b7591a
Compare
|
@Andy-Jost I have incorporated your feedback into 6b7591a |
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: d4c71ef
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Enterprise
- Run ID:
2d96acaa-9c23-4910-9e5f-f5beb2bd335b
Files selected for processing: 4-
cuda_core/cuda/core/_utils/cuda_utils.pyicuda_core/cuda/core/_utils/cuda_utils.pyxcuda_core/docs/source/error_handling.rstcuda_core/docs/source/release/1.3.0-notes.rst
Files skipped from review as they are similar to previous changes: 1-
- cuda_core/docs/source/release/1.3.0-notes.rst
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.
-
|
/ok to test 6b7591a |
|
Andy-Jost
left a comment
There was a problem hiding this comment.
Thanks, all three points are addressed: the rebase keeps main's plain _RUNTIME_SUCCESS and _NVRTC_SUCCESS assignments with the new sentinel beside them and the exc_type selection intact on every raise branch, the release note names CuPy, RMM, and NumPy, and the sentence about importable classes is fixed. CI is now running. Approving.
A last wording nit, take it or leave it: "These exception classes are importable from the top-level cuda.core namespace" now follows the MemoryError sentence, so it reads as including the built-in; naming the classes ("CUDAError, NVRTCError, and CUDAOutOfMemoryError are importable ...") avoids that.
Closes #3260
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:CuPy (cupy.cuda.memory.OutOfMemoryError) and RMM raise a MemoryError subclass when device memory runs out, and NumPy raises one for host allocations. Code that recovers from running out of memory by asking for less catches
MemoryError. 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).