Repository navigation
cuda-core: restore DeviceBuffer async methods - #204
Merged
nihalpasham merged 2 commits intoJun 16, 2026
Merged
Conversation
Signed-off-by: nik <[email protected]>
Collaborator
|
The methods themselves are good, but the implicit
Layered a fix:
Good to merge now. |
uninitialized_async allocates from the stream-ordered pool
(cuMemAllocAsync), but the implicit Drop freed with the synchronous
cuMemFree. compute-sanitizer flags that as a real use-after-free
("free-before-alloc") when stream work is still pending: the sync free
does not order against the async pool.
Have the buffer co-own its stream (Arc<CudaStream>, since new_stream
already hands out an Arc) and record it as the dealloc stream. Drop now
frees stream-ordered with cuMemFreeAsync on that stream for async-pool
allocations and keeps the synchronous cuMemFree for synchronous
(cuMemAlloc) allocations. uninitialized_async takes &Arc<CudaStream>;
callers already hold one, so call sites are unchanged. into_raw_parts
moves ctx out via ManuallyDrop (also dropping the dealloc-stream Arc)
instead of clone+forget, which additionally fixes a latent context
strong-count leak.
Adds uninitialized_async_implicit_drop_is_stream_ordered, a
drop-without-sync stress test that is clean under compute-sanitizer
memcheck (0 errors) and reproduced the free-before-alloc before the fix.
nihalpasham
force-pushed
the
upstream-devicebuffer-async
branch
from
June 16, 2026 09:42
fe21602 to
79feade
Compare
meefs
pushed a commit
to meefs/cuda-oxide
that referenced
this pull request
Sep 30, 2026
Signed-off-by: Heltion <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #203.
Problem
DeviceBuffer<T>already carries an explicit stream-oriented API, but it lacked several named async compatibility methods:uninitialized_async,copy_from_host_async,copy_from_device_async,zero_async, anddrop_async.Callers that need to express stream-ordered allocation, copies, memset, or explicit free had to either use less direct API names or drop to raw allocation/copy helpers.
Fix
Add the missing
DeviceBuffer<T>compatibility methods on the existingT: DeviceCopyimpl block:unsafe fn uninitialized_async(stream, len);copy_from_host_async;copy_from_device_async;zero_async;drop_async.The methods delegate to the existing
cuda-core::memoryasync helpers and keep the current buffer ownership model. Zero-byte buffers return the same null-pointer/zero-length representation used by the rest ofDeviceBufferand treat zero-byte copy, memset, and free as no-ops.Diligence
The branch keeps async behavior in
DeviceBufferinstead of exposing new raw driver calls. Length checks mirror the existing synchronous copy helpers, so mismatched host/device or device/device copies fail before any CUDA call is issued.The explicit
drop_asyncconsumesselfthroughinto_raw_parts, preventing the ordinaryDroppath from freeing the same allocation a second time.Tests
Extended
crates/cuda-core/tests/device_buffer.rswithdevice_buffer_async_compat_methods_roundtrip.The test allocates uninitialized device buffers, copies host data into one buffer, copies device-to-device into a second buffer, reads exact host results back, zeroes the clone, checks the zeroed result, and then frees both buffers through
drop_async. It also covers zero-length async allocation/free.The regression fails on
upstream/mainwithout the methods and passes on this branch.Validation