-
Notifications
You must be signed in to change notification settings - Fork 236
Implements GraphMemoryResource #1235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
befa768
88834f7
57855a1
0375941
0b82b1f
53b1c58
1b8409b
e9422b2
3e21b9b
98ccfc7
183f7af
13e3dfb
7408fe8
c1dbc6a
2f14443
02935fd
4ac5c99
e5ea645
7042437
45db4ff
7c3ad5f
af22c81
556c6bf
1d07da1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,7 @@ from cuda.bindings cimport cydriver | |
| from cuda.core.experimental._memory._buffer cimport Buffer, MemoryResource | ||
| from cuda.core.experimental._memory cimport _ipc | ||
| from cuda.core.experimental._memory._ipc cimport IPCAllocationHandle, IPCData | ||
| from cuda.core.experimental._stream cimport default_stream, Stream | ||
| from cuda.core.experimental._stream cimport default_stream, Stream_accept, Stream | ||
| from cuda.core.experimental._utils.cuda_utils cimport ( | ||
| check_or_create_options, | ||
| HANDLE_RETURN, | ||
|
|
@@ -65,6 +65,12 @@ cdef class DeviceMemoryResourceAttributes: | |
| self._mr_weakref = mr | ||
| return self | ||
|
|
||
| def __repr__(self): | ||
| return f"{self.__class__.__name__}(%s)" % ", ".join( | ||
| f"{attr}={getattr(self, attr)}" for attr in dir(self) | ||
| if not attr.startswith("_") | ||
| ) | ||
|
|
||
| cdef int _getattribute(self, cydriver.CUmemPool_attribute attr_enum, void* value) except?-1: | ||
| cdef DeviceMemoryResource mr = <DeviceMemoryResource>(self._mr_weakref()) | ||
| if mr is None: | ||
|
|
@@ -133,7 +139,7 @@ cdef class DeviceMemoryResourceAttributes: | |
|
|
||
| cdef class DeviceMemoryResource(MemoryResource): | ||
| """ | ||
| Create a device memory resource managing a stream-ordered memory pool. | ||
| A device memory resource managing a stream-ordered memory pool. | ||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
@@ -309,14 +315,14 @@ cdef class DeviceMemoryResource(MemoryResource): | |
| raise RuntimeError("Imported memory resource cannot be exported") | ||
| return self._ipc_data._alloc_handle | ||
|
|
||
| def allocate(self, size_t size, stream: Stream = None) -> Buffer: | ||
| def allocate(self, size_t size, stream: Stream | GraphBuilder | None = None) -> Buffer: | ||
| """Allocate a buffer of the requested size. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| size : int | ||
| The size of the buffer to allocate, in bytes. | ||
| stream : Stream, optional | ||
| stream : :obj:`~_stream.Stream` | :obj:`~_graph.GraphBuilder`, optional | ||
| The stream on which to perform the allocation asynchronously. | ||
| If None, an internal stream is used. | ||
|
|
||
|
|
@@ -328,11 +334,10 @@ cdef class DeviceMemoryResource(MemoryResource): | |
| """ | ||
| if self.is_mapped: | ||
| raise TypeError("Cannot allocate from a mapped IPC-enabled memory resource") | ||
| if stream is None: | ||
| stream = default_stream() | ||
| return DMR_allocate(self, size, <Stream>stream) | ||
| stream = Stream_accept(stream) if stream is not None else default_stream() | ||
| return DMR_allocate(self, size, <Stream> stream) | ||
|
|
||
| def deallocate(self, ptr: DevicePointerT, size_t size, stream: Stream = None): | ||
| def deallocate(self, ptr: DevicePointerT, size_t size, stream: Stream | GraphBuilder | None = None): | ||
| """Deallocate a buffer previously allocated by this resource. | ||
|
|
||
| Parameters | ||
|
|
@@ -341,15 +346,17 @@ cdef class DeviceMemoryResource(MemoryResource): | |
| The pointer or handle to the buffer to deallocate. | ||
| size : int | ||
| The size of the buffer to deallocate, in bytes. | ||
| stream : Stream, optional | ||
| stream : :obj:`~_stream.Stream` | :obj:`~_graph.GraphBuilder`, optional | ||
| The stream on which to perform the deallocation asynchronously. | ||
| If the buffer is deallocated without an explicit stream, the allocation stream | ||
| is used. | ||
| """ | ||
| DMR_deallocate(self, <uintptr_t>ptr, size, <Stream>stream) | ||
| stream = Stream_accept(stream) if stream is not None else default_stream() | ||
| DMR_deallocate(self, <uintptr_t>ptr, size, <Stream> stream) | ||
|
|
||
| @property | ||
| def attributes(self) -> DeviceMemoryResourceAttributes: | ||
| """Memory pool attributes.""" | ||
| if self._attributes is None: | ||
| ref = weakref.ref(self) | ||
| self._attributes = DeviceMemoryResourceAttributes._init(ref) | ||
|
|
@@ -467,10 +474,21 @@ cdef void DMR_init_create( | |
| self._ipc_data = IPCData(alloc_handle, mapped=False) | ||
|
|
||
|
|
||
| cdef Buffer DMR_allocate(DeviceMemoryResource self, size_t size, Stream stream): | ||
| # Raise an exception if the given stream is capturing. | ||
| # A result of CU_STREAM_CAPTURE_STATUS_INVALIDATED is considered an error. | ||
| cdef inline int check_not_capturing(cydriver.CUstream s) except?-1 nogil: | ||
| cdef cydriver.CUstreamCaptureStatus capturing | ||
| HANDLE_RETURN(cydriver.cuStreamIsCapturing(s, &capturing)) | ||
| if capturing != cydriver.CUstreamCaptureStatus.CU_STREAM_CAPTURE_STATUS_NONE: | ||
| raise RuntimeError("DeviceMemoryResource cannot perform memory operations on " | ||
| "a capturing stream (consider using GraphMemoryResource).") | ||
|
|
||
|
|
||
| cdef inline Buffer DMR_allocate(DeviceMemoryResource self, size_t size, Stream stream): | ||
leofang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cdef cydriver.CUstream s = stream._handle | ||
| cdef cydriver.CUdeviceptr devptr | ||
| with nogil: | ||
| check_not_capturing(s) | ||
| HANDLE_RETURN(cydriver.cuMemAllocFromPoolAsync(&devptr, size, self._handle, s)) | ||
| cdef Buffer buf = Buffer.__new__(Buffer) | ||
| buf._ptr = <uintptr_t>(devptr) | ||
|
|
@@ -481,16 +499,19 @@ cdef Buffer DMR_allocate(DeviceMemoryResource self, size_t size, Stream stream): | |
| return buf | ||
|
|
||
|
|
||
| cdef void DMR_deallocate( | ||
| cdef inline void DMR_deallocate( | ||
| DeviceMemoryResource self, uintptr_t ptr, size_t size, Stream stream | ||
| ) noexcept: | ||
| cdef cydriver.CUstream s = stream._handle | ||
| cdef cydriver.CUdeviceptr devptr = <cydriver.CUdeviceptr>ptr | ||
| cdef cydriver.CUresult r | ||
| with nogil: | ||
| HANDLE_RETURN(cydriver.cuMemFreeAsync(devptr, s)) | ||
| r = cydriver.cuMemFreeAsync(devptr, s) | ||
| if r != cydriver.CUDA_ERROR_INVALID_CONTEXT: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: mempools are not tied to a CUDA context, so when will this happen?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a reactive fix as I was seeing many of these errors at the end of a full pytest run. I think this issue exists prior to this PR, so I could separate this if you prefer.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Andy-Jost Let's save this to a separate PR. I think this one is clean, and I'd like to merge it asap (after I read it one more time). |
||
| HANDLE_RETURN(r) | ||
|
|
||
|
|
||
| cdef DMR_close(DeviceMemoryResource self): | ||
| cdef inline DMR_close(DeviceMemoryResource self): | ||
| if self._handle == NULL: | ||
| return | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from cuda.core.experimental._memory._buffer cimport MemoryResource | ||
|
|
||
|
|
||
| cdef class cyGraphMemoryResource(MemoryResource): | ||
| cdef: | ||
| int _dev_id |
Uh oh!
There was an error while loading. Please reload this page.