From f06149945f934b7be925f2ad2db100767eaec25f Mon Sep 17 00:00:00 2001 From: Vyas Ramasubramani Date: Wed, 14 Oct 2026 22:12:16 -0700 Subject: [PATCH 1/3] Handle empty RMM buffers without an upstream stream Signed-off-by: Vyas Ramasubramani --- cpp/src/memory/buffer_resource.cpp | 8 +++++++- cpp/tests/test_buffer_resource.cpp | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cpp/src/memory/buffer_resource.cpp b/cpp/src/memory/buffer_resource.cpp index bc474fbba..5963689fb 100644 --- a/cpp/src/memory/buffer_resource.cpp +++ b/cpp/src/memory/buffer_resource.cpp @@ -335,7 +335,13 @@ std::unique_ptr BufferResource::move( ); cuda::stream_ref upstream = data->stream(); if (upstream.get() != stream.get()) { - cuda_stream_join(stream, upstream); + // Default-constructed and moved-from RMM buffers have no stream or work to join. + bool const empty_without_stream = + data->capacity() == 0 + && upstream.get() == cuda::stream_ref{cuda::invalid_stream_t{}}.get(); + if (!empty_without_stream) { + cuda_stream_join(stream, upstream); + } data->set_stream(stream); } diff --git a/cpp/tests/test_buffer_resource.cpp b/cpp/tests/test_buffer_resource.cpp index 7f6b8a43f..dca11f142 100644 --- a/cpp/tests/test_buffer_resource.cpp +++ b/cpp/tests/test_buffer_resource.cpp @@ -67,6 +67,18 @@ std::int64_t reserved_bytes(BufferResource const& br, MemoryType mem_type) { return br.memory_available(mem_type) - br.memory_available_for_reservation(mem_type); } +TEST(BufferResource, MoveDefaultConstructedDeviceBuffer) { + StreamPool stream_pool{1}; + auto stream = stream_pool.get_stream(); + auto br = BufferResource::create(rmm::mr::get_current_device_resource_ref()); + + auto buffer = br->move(std::make_unique(), stream); + + EXPECT_EQ(buffer->size, 0U); + EXPECT_EQ(buffer->stream().get(), stream.get()); + EXPECT_NO_THROW(buffer->latest_write_event().host_wait()); +} + TEST(BufferResource, ReservationOverbooking) { // Create a buffer resource that always reports 10 KiB of available device memory. auto br = BufferResource::create( From 98b968dc440c68594353276e052715c664fc2acc Mon Sep 17 00:00:00 2001 From: Peter Andreas Entschev Date: Thu, 15 Oct 2026 08:23:35 +0000 Subject: [PATCH 2/3] Address review feedback for empty RMM buffer handling Follow-up to the fix for empty `rmm::device_buffer`s reporting an invalid stream after rapidsai/rmm#2546: - Document in `BufferResource::move()` that an empty buffer without a stream (default-constructed or moved-from `rmm::device_buffer`) is not synchronized and simply has its stream set to the target stream. - Simplify the invalid-stream check to `upstream == cuda::invalid_stream`, using the comparison operator provided by CCCL. - Add a regression test for moving a moved-from `rmm::device_buffer`, which rapidsai/rmm#2546 documents as a separate case that now reports an invalid stream. Signed-off-by: Peter Andreas Entschev --- cpp/include/rapidsmpf/memory/buffer_resource.hpp | 4 ++++ cpp/src/memory/buffer_resource.cpp | 3 +-- cpp/tests/test_buffer_resource.cpp | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/cpp/include/rapidsmpf/memory/buffer_resource.hpp b/cpp/include/rapidsmpf/memory/buffer_resource.hpp index 5da70c216..1ea504d31 100644 --- a/cpp/include/rapidsmpf/memory/buffer_resource.hpp +++ b/cpp/include/rapidsmpf/memory/buffer_resource.hpp @@ -516,6 +516,10 @@ class BufferResource : public std::enable_shared_from_this { * - @p stream is synchronized with the device buffer's current stream, and * - the device buffer's current stream is updated to @p stream. * + * If @p data is empty and has no stream (e.g. a default-constructed or moved-from + * `rmm::device_buffer`), no synchronization is performed and its stream is set to + * @p stream. + * * @param data Unique pointer to the device or pinned host buffer. * @param stream CUDA stream associated with the new Buffer. Use or synchronize with * this stream when operating on the Buffer. diff --git a/cpp/src/memory/buffer_resource.cpp b/cpp/src/memory/buffer_resource.cpp index 5963689fb..564f0eccd 100644 --- a/cpp/src/memory/buffer_resource.cpp +++ b/cpp/src/memory/buffer_resource.cpp @@ -337,8 +337,7 @@ std::unique_ptr BufferResource::move( if (upstream.get() != stream.get()) { // Default-constructed and moved-from RMM buffers have no stream or work to join. bool const empty_without_stream = - data->capacity() == 0 - && upstream.get() == cuda::stream_ref{cuda::invalid_stream_t{}}.get(); + data->capacity() == 0 && upstream == cuda::invalid_stream; if (!empty_without_stream) { cuda_stream_join(stream, upstream); } diff --git a/cpp/tests/test_buffer_resource.cpp b/cpp/tests/test_buffer_resource.cpp index dca11f142..1dc5bfe12 100644 --- a/cpp/tests/test_buffer_resource.cpp +++ b/cpp/tests/test_buffer_resource.cpp @@ -79,6 +79,22 @@ TEST(BufferResource, MoveDefaultConstructedDeviceBuffer) { EXPECT_NO_THROW(buffer->latest_write_event().host_wait()); } +TEST(BufferResource, MoveMovedFromDeviceBuffer) { + StreamPool stream_pool{1}; + auto stream = stream_pool.get_stream(); + auto br = BufferResource::create(rmm::mr::get_current_device_resource_ref()); + + auto source = std::make_unique(16, stream); + auto destination = std::move(*source); + + auto buffer = br->move(std::move(source), stream); + + EXPECT_EQ(buffer->size, 0U); + EXPECT_EQ(buffer->stream().get(), stream.get()); + EXPECT_NO_THROW(buffer->latest_write_event().host_wait()); + EXPECT_EQ(destination.size(), 16U); +} + TEST(BufferResource, ReservationOverbooking) { // Create a buffer resource that always reports 10 KiB of available device memory. auto br = BufferResource::create( From f8e809f9523491eaead83fa90edee200afd11d32 Mon Sep 17 00:00:00 2001 From: Peter Andreas Entschev Date: Thu, 15 Oct 2026 08:46:39 +0000 Subject: [PATCH 3/3] Remove invalid moved-from device_buffer test `BufferResource.MoveMovedFromDeviceBuffer` segfaults in `BufferResource::move()`. Moving from an `rmm::device_buffer` also moves out its memory resource (`_mr`), leaving an empty `any_resource`, so the subsequent `is_host_accessible(data->memory_resource())` call dereferences an empty resource. This is unrelated to the invalid-stream change in rapidsai/rmm#2546: moved-from buffers were never valid input to `BufferResource::move()`. Remove the test and drop the "moved-from" mentions from the `move()` docstring and the code comment, which only apply to default-constructed buffers. Signed-off-by: Peter Andreas Entschev --- cpp/include/rapidsmpf/memory/buffer_resource.hpp | 2 +- cpp/src/memory/buffer_resource.cpp | 2 +- cpp/tests/test_buffer_resource.cpp | 16 ---------------- 3 files changed, 2 insertions(+), 18 deletions(-) diff --git a/cpp/include/rapidsmpf/memory/buffer_resource.hpp b/cpp/include/rapidsmpf/memory/buffer_resource.hpp index 1ea504d31..d49b1f5f2 100644 --- a/cpp/include/rapidsmpf/memory/buffer_resource.hpp +++ b/cpp/include/rapidsmpf/memory/buffer_resource.hpp @@ -516,7 +516,7 @@ class BufferResource : public std::enable_shared_from_this { * - @p stream is synchronized with the device buffer's current stream, and * - the device buffer's current stream is updated to @p stream. * - * If @p data is empty and has no stream (e.g. a default-constructed or moved-from + * If @p data is empty and has no stream (e.g. a default-constructed * `rmm::device_buffer`), no synchronization is performed and its stream is set to * @p stream. * diff --git a/cpp/src/memory/buffer_resource.cpp b/cpp/src/memory/buffer_resource.cpp index 564f0eccd..42bc75a08 100644 --- a/cpp/src/memory/buffer_resource.cpp +++ b/cpp/src/memory/buffer_resource.cpp @@ -335,7 +335,7 @@ std::unique_ptr BufferResource::move( ); cuda::stream_ref upstream = data->stream(); if (upstream.get() != stream.get()) { - // Default-constructed and moved-from RMM buffers have no stream or work to join. + // Default-constructed RMM buffers have no stream or work to join. bool const empty_without_stream = data->capacity() == 0 && upstream == cuda::invalid_stream; if (!empty_without_stream) { diff --git a/cpp/tests/test_buffer_resource.cpp b/cpp/tests/test_buffer_resource.cpp index 1dc5bfe12..dca11f142 100644 --- a/cpp/tests/test_buffer_resource.cpp +++ b/cpp/tests/test_buffer_resource.cpp @@ -79,22 +79,6 @@ TEST(BufferResource, MoveDefaultConstructedDeviceBuffer) { EXPECT_NO_THROW(buffer->latest_write_event().host_wait()); } -TEST(BufferResource, MoveMovedFromDeviceBuffer) { - StreamPool stream_pool{1}; - auto stream = stream_pool.get_stream(); - auto br = BufferResource::create(rmm::mr::get_current_device_resource_ref()); - - auto source = std::make_unique(16, stream); - auto destination = std::move(*source); - - auto buffer = br->move(std::move(source), stream); - - EXPECT_EQ(buffer->size, 0U); - EXPECT_EQ(buffer->stream().get(), stream.get()); - EXPECT_NO_THROW(buffer->latest_write_event().host_wait()); - EXPECT_EQ(destination.size(), 16U); -} - TEST(BufferResource, ReservationOverbooking) { // Create a buffer resource that always reports 10 KiB of available device memory. auto br = BufferResource::create(