Skip to content

Adopt any_resource in BufferResource and RmmResourceAdaptor#772

Open
bdice wants to merge 6 commits intorapidsai:mainfrom
bdice:any-resource
Open

Adopt any_resource in BufferResource and RmmResourceAdaptor#772
bdice wants to merge 6 commits intorapidsai:mainfrom
bdice:any-resource

Conversation

@bdice
Copy link
Contributor

@bdice bdice commented Jan 8, 2026

Summary

  • Adopt cuda::mr::any_resource<cuda::mr::device_accessible> in BufferResource and RmmResourceAdaptor to own upstream memory resources
  • Update accessor methods to return refs from owned any_resource members (requires non-const this)
  • Simplify do_is_equal comparison by comparing owned resources directly

Part of RMM issue 2011.

Depends on rapidsai/rmm#2200.

anon added 2 commits January 8, 2026 15:05
Change BufferResource::device_mr_ member from rmm::device_async_resource_ref
to cuda::mr::any_resource<cuda::mr::device_accessible> so that the buffer
resource owns the memory resource it wraps.

This ensures the memory resource remains valid for the lifetime of the
BufferResource, matching the ownership semantics now used by
rmm::device_buffer.

Changes:
- Add #include <cuda/memory_resource>
- Change device_mr_ type to any_resource
- Make device_mr() accessor non-const (required because CCCL's resource_ref
  can only be constructed from non-const any_resource&)
Change RmmResourceAdaptor's memory resource members from
rmm::device_async_resource_ref to cuda::mr::any_resource so that the
adaptor owns the memory resources it wraps.

Changes:
- Add #include <cuda/memory_resource>
- Change primary_mr_ to any_resource
- Change fallback_mr_ to std::optional<any_resource>
- Make get_upstream_resource() and get_fallback_resource() non-const
  (required because CCCL's resource_ref can only be constructed from
  non-const any_resource&)
- Update get_fallback_resource() to construct resource_ref from optional
- Update do_is_equal() to compare any_resource members directly, with
  explicit optional value extraction to avoid CCCL's recursive constraint
  satisfaction issue with std::optional::operator==
@copy-pr-bot
Copy link

copy-pr-bot bot commented Jan 8, 2026

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Comment on lines +94 to 96
[[nodiscard]] rmm::device_async_resource_ref device_mr() noexcept {
return device_mr_;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this "return device_mr_ as resource_ref cannot be const?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s discuss on this RMM PR thread, it is the same question: rapidsai/rmm#2201 (comment)

* - Memory usage tracking.
* - Fallback memory resource support upon out-of-memory in the primary resource.
*/
class RmmResourceAdaptor final : public rmm::mr::device_memory_resource {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we're not yet reading to drop the inheritance like we have in RMM?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am planning to drop the inheritance and remove the device_memory_resource class as a later step. For now, my immediate goals are to drop pointer-based resources device_memory_resource* (in favor of refs), and, if a ref is used as a member (e.g. of an adaptor or container), switch to any_resource so it is owning. This prevents a class of use-after-free bugs that have plagued RMM.

Comment on lines +133 to +140
bool fallbacks_equal;
if (fallback_mr_.has_value() != cast->fallback_mr_.has_value()) {
fallbacks_equal = false;
} else if (!fallback_mr_.has_value()) {
fallbacks_equal = true;
} else {
fallbacks_equal = (*fallback_mr_ == *cast->fallback_mr_);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the old logic that initialises fallbacks_equal immediately, is there a functional reason for this change? I think no.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll investigate but I think it was failing that way.

@bdice bdice added breaking Introduces a breaking change feature request New feature or request labels Jan 10, 2026
@bdice bdice marked this pull request as ready for review January 10, 2026 15:20
@bdice bdice requested a review from a team as a code owner January 10, 2026 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Introduces a breaking change feature request New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants