Migrate Python/Cython bindings from device_memory_resource to device_async_resource_ref#2300
Merged
bdice merged 1 commit intorapidsai:stagingfrom Mar 17, 2026
Merged
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
Collaborator
Author
|
/ok to test |
This was referenced Mar 14, 2026
…async_resource_ref Replace shared_ptr[device_memory_resource] with per-subclass unique_ptr[ConcreteType] for ownership and optional[device_async_resource_ref] for the non-owning reference used by allocate/deallocate. Key changes: - librmm .pxd declarations: remove device_memory_resource class, declare device_async_resource_ref, add make_device_async_resource_ref() inline C++ template returning optional to avoid Cython default-constructor temporaries, update all adaptor constructors to take resource_ref - pylibrmm .pxd declarations: DeviceMemoryResource holds optional[device_async_resource_ref] c_ref instead of shared_ptr[device_memory_resource] c_obj; each concrete subclass holds unique_ptr[ConcreteType] c_obj - pylibrmm .pyx implementations: all __cinit__ methods construct via unique_ptr then set c_ref via make_device_async_resource_ref; typed accessors (pool_size, flush, etc.) use deref(self.c_obj); per-device functions use set_per_device_resource_ref - device_buffer.pyx: pass self.mr.c_ref.value() instead of self.mr.get_mr() Closes rapidsai#2294
bddb97a to
20f2b6a
Compare
TomAugspurger
approved these changes
Mar 17, 2026
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.
Summary
Replaces
shared_ptr[device_memory_resource]with per-subclassunique_ptr[ConcreteType](owning) andoptional[device_async_resource_ref](non-owning reference) across all Python/Cython bindings. This is a part of #2011.There are significant opportunities to make this Cython code better over time but I have to get something that removes
device_memory_resourcefrom the Python/Cython side before I can finish migration on the C++ side (#2296). I welcome critique of this design, and ideas for how it can be improved, particularly from @vyasr @wence-. I would like to address any suggested improvements in follow-up PRs, because this changeset is necessary to unblock #2301.The changes in
cdef class DeviceMemoryResourceare perhaps the most significant changes here from a design perspective.The solution I'm going with for now is to keep the
DeviceMemoryResourceclass around, as a base class for the Cython MRs, and let it handle allocate/deallocate. It owns aoptional[device_async_resource_ref]which is used for allocation/deallocation. It'soptionalso that the class can be default-constructed (Cython requires nullary constructors), but it should never benulloptexcept during initialization.Then, each MR class owns a
c_objlikeunique_ptr[cuda_memory_resource]. This isunique_ptrso it can be default-constructed for Cython's requirements. I choseunique_ptroveroptionalhere to emphasize that this member is the thing that actually owns the resource. As with thec_ref, this should never benullptrexcept during initialization. When an MR class is created, it initializes itsc_objand then constructs ac_ref(a member inherited from theDeviceMemoryResourcebase class)."Special" methods for an MR like getting the statistics counts go through
deref(self.c_obj), and "common" methods like allocate/deallocate go throughself.c_ref.value().Changes
.pxddeclarations: Removedevice_memory_resourceclass. Declaredevice_async_resource_refand amake_device_async_resource_ref()inline C++ template that returnsoptionalto work around Cython generating default-constructed temporaries for non-default-constructible types. All adaptor constructors takedevice_async_resource_refinstead ofdevice_memory_resource*..pxdclass definitions:DeviceMemoryResourcebase holdsoptional[device_async_resource_ref] c_ref; each concrete subclass holdsunique_ptr[ConcreteType] c_obj..pyximplementations: All__cinit__methods construct viaunique_ptrthen setc_refviamake_device_async_resource_ref. Typed accessors (pool_size,flush, etc.) usederef(self.c_obj). Per-device functions useset_per_device_resource_ref.device_buffer.pyx: Passesself.mr.c_ref.value()instead ofself.mr.get_mr().Closes #2294