Skip to content

cuda.core: raise CUDAOutOfMemoryError, a MemoryError, on out-of-memory - #3235

Open
srimanachanta wants to merge 1 commit into
NVIDIA:mainfrom
srimanachanta:core-out-of-memory-error
Open

srimanachanta wants to merge 1 commit into
NVIDIA:mainfrom
srimanachanta:core-out-of-memory-error

Conversation

@srimanachanta

@srimanachanta srimanachanta commented Oct 27, 2026 •

Copy link
Copy Markdown

Closes #3260

Summary

Add cuda.core.CUDAOutOfMemoryError(CUDAError, MemoryError). CUDA_ERROR_OUT_OF_MEMORY and cudaErrorMemoryAllocation now raise it instead of a plain CUDAError.

Motivation

Allocating too much from a DeviceMemoryResource raises CUDAError, which derives only from Exception:

>>> mr.allocate(1 << 46, stream=dev.default_stream)
CUDAError: CUDA_ERROR_OUT_OF_MEMORY: ...
>>> type(e).__mro__
(CUDAError, Exception, BaseException, object)

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 own CudaOutOfMemory for this reason (common.hpp, registered as a MemoryError in bindings.cpp), and its sweep halves the chunk on except MemoryError (simulation.py). That retry does not see out-of-memory errors from cuda.core.

Change

  • _check_driver_error and _check_runtime_error pick CUDAOutOfMemoryError for the two out-of-memory codes. Every driver and runtime failure in cuda.core, including the C++ handle layer through HANDLE_RETURN(get_last_error()), goes through these two functions.
  • Message, explanation and notes are unchanged. Code that catches CUDAError is unaffected.
  • Exported from cuda.core, added to api.rst, the error handling page and the 1.3.0 release notes.

NVRTC's NVRTC_ERROR_OUT_OF_MEMORY still raises NVRTCError. I left it out to keep the change to driver and runtime errors.

Tests

  • Driver and runtime OOM codes raise CUDAOutOfMemoryError, which is both a CUDAError and a MemoryError.
  • No other driver or runtime code raises a MemoryError.
  • End to end: a 64 TiB request from a DeviceMemoryResource raises MemoryError, and the pool stays usable.
  • test_cuda_utils.py, test_error_handling.py, test_api_docs_consistency.py and test_memory.py pass locally (RTX 5070 Ti, driver 615, CUDA 13.4).

@copy-pr-bot

copy-pr-bot Bot commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Oct 27, 2026
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

📝 Summary

Summary by CodeRabbit

  • New Features
    • CUDA out-of-memory failures now raise CUDAOutOfMemoryError, which can be caught as either a CUDAError or a standard MemoryError.
    • The exception is available from the top-level cuda.core namespace. Other CUDA errors continue to raise `CUDAError.
  • Documentation
    • Updated the error-handling guide, API reference, and release notes with details about the new exception.

The change adds and exports CUDAOutOfMemoryError. Driver and runtime out-of-memory errors now raise this class, which inherits from CUDAError and MemoryError. Tests and documentation cover the new exception.

Changes: CUDA out-of-memory exception

Layer / File(s) Summary
Exception definition and error classification
cuda_core/cuda/core/_utils/cuda_utils.pyi, cuda_core/cuda/core/_utils/cuda_utils.pyx, cuda_core/tests/test_cuda_utils.py
Defines CUDAOutOfMemoryError as a CUDAError and MemoryError subclass. Driver and runtime out-of-memory codes raise it. Tests check its inheritance, error messages, and classification of other errors.
Public API and allocation validation
cuda_core/cuda/core/__init__.py, cuda_core/tests/test_memory.py, cuda_core/docs/source/api.rst, cuda_core/docs/source/error_handling.rst, cuda_core/docs/source/release/1.3.0-notes.rst
Exports and documents CUDAOutOfMemoryError. A memory-pool-gated test checks an oversized allocation failure and a subsequent successful allocation.

Assessment against linked issues

Objective Addressed Explanation
Add cuda.core.CUDAOutOfMemoryError(CUDAError, MemoryError) and raise it for CUDA_ERROR_OUT_OF_MEMORY and cudaErrorMemoryAllocation [#3260] ✅
Preserve error messages and allow except CUDAError to catch these errors [#3260] ✅
Keep NVRTC out-of-memory errors as NVRTCError [#3260] ✅

Priority: Normal

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: Low · up to 6b759

The documentation may lead readers to attempt an invalid import. The change is mergeable with a documentation clarification.

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 590a8bf and b7b0f23.

Files selected for processing: 8
  • cuda_core/cuda/core/__init__.py
  • cuda_core/cuda/core/_utils/cuda_utils.pyi
  • cuda_core/cuda/core/_utils/cuda_utils.pyx
  • cuda_core/docs/source/api.rst
  • cuda_core/docs/source/error_handling.rst
  • cuda_core/docs/source/release/1.3.0-notes.rst
  • cuda_core/tests/test_cuda_utils.py
  • cuda_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.

Comment on lines +23 to +24
:class:`CUDAError` and :class:`MemoryError`. All ``cuda.core`` classes are
importable from the top-level ``cuda.core`` namespace. Invalid arguments and misuse raise the

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

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 Andy-Jost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Rebase onto main. Since #3255, _RUNTIME_SUCCESS and _NVRTC_SUCCESS are declared in cuda_utils.pxd and assigned as plain globals in the .pyx, and the check functions are cpdef inline in the .pxd. The only conflict is the constant block in cuda_utils.pyx: keep main's two assignments and add _RUNTIME_OUT_OF_MEMORY beside them. Your exc_type selection merges cleanly into the renamed _raise_driver_error and _raise_runtime_error. No CI has run on this PR yet; after the rebase the workflows are approved and it runs.
  2. The release note (and the commit message) say except MemoryError works "as it does in CuPy, PyTorch and NumPy". PyTorch raises torch.OutOfMemoryError, a RuntimeError, as #3260 itself notes; please name CuPy, RMM, and NumPy.
  3. error_handling.rst: "All cuda.core classes are importable from the top-level namespace" reads as every class, and graph classes, for one, live in cuda.core.graph. "These exception classes are importable from the top-level cuda.core namespace" says what you mean; please rewrap the paragraph too.

@srimanachanta
srimanachanta force-pushed the core-out-of-memory-error branch from b7b0f23 to 90abbed Compare October 29, 2026 04:06
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.
@srimanachanta
srimanachanta force-pushed the core-out-of-memory-error branch from 90abbed to 6b7591a Compare October 29, 2026 04:07
@srimanachanta

Copy link
Copy Markdown
Author

@Andy-Jost I have incorporated your feedback into 6b7591a

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between b7b0f23 and 6b7591a.

Files selected for processing: 4
  • cuda_core/cuda/core/_utils/cuda_utils.pyi
  • cuda_core/cuda/core/_utils/cuda_utils.pyx
  • cuda_core/docs/source/error_handling.rst
  • cuda_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.

@Andy-Jost

Copy link
Copy Markdown
Contributor

/ok to test 6b7591a

@github-actions

Copy link
Copy Markdown
Contributor

@Andy-Jost Andy-Jost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.core Everything related to the cuda.core module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEA]: Raise a MemoryError subclass on CUDA out-of-memory errors

2 participants