Skip to content

cuda-core: restore DeviceBuffer async methods - #204

Merged
nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:upstream-devicebuffer-async
Jun 16, 2026
Merged

nihalpasham merged 2 commits into
NVIDIA:mainfrom
niklebedenko:upstream-devicebuffer-async

Conversation

@niklebedenko

Copy link
Copy Markdown
Contributor

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, and drop_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 existing T: DeviceCopy impl 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::memory async helpers and keep the current buffer ownership model. Zero-byte buffers return the same null-pointer/zero-length representation used by the rest of DeviceBuffer and treat zero-byte copy, memset, and free as no-ops.

Diligence

The branch keeps async behavior in DeviceBuffer instead 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_async consumes self through into_raw_parts, preventing the ordinary Drop path from freeing the same allocation a second time.

Tests

Extended crates/cuda-core/tests/device_buffer.rs with device_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/main without the methods and passes on this branch.

Validation

cargo fmt --all -- --check
cargo test -p cuda-core device_buffer_async_compat_methods_roundtrip
cargo test -p cuda-core --all-targets
cargo clippy -p cuda-core --all-targets -- -D warnings

@nihalpasham nihalpasham added host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) enhancement New feature or request safety Memory safety, soundness, or undefined behavior labels Jun 16, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

The methods themselves are good, but the implicit Drop wasn't correct and compute-sanitizer caught it:

  • uninitialized_async allocates from the async pool (cuMemAllocAsync), but normal drop frees with the sync cuMemFree - sanitizer flags free-before-alloc when a copy is still in flight
  • your roundtrip test never hits it because to_host_vec synchronizes first

Layered a fix:

  • the buffer now co-owns its stream and frees with cuMemFreeAsync on drop, so dropping it without an explicit drop_async is safe too
  • added a drop-without-sync stress test (clean under compute-sanitizer, 0 errors)
  • also fixed a small unrelated context refcount leak in into_raw_parts

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
nihalpasham force-pushed the upstream-devicebuffer-async branch from fe21602 to 79feade Compare June 16, 2026 09:42
@nihalpasham
nihalpasham merged commit acd5718 into NVIDIA:main Jun 16, 2026
24 checks passed
@niklebedenko
niklebedenko deleted the upstream-devicebuffer-async branch June 24, 2026 11:40
meefs pushed a commit to meefs/cuda-oxide that referenced this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request host-apis Host-side runtime APIs (cuda-host, cuda-core, cuda-async) safety Memory safety, soundness, or undefined behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Restore async DeviceBuffer compatibility methods

2 participants