Skip to content

Fix callback resource alignment forwarding - #2485

Open
nethum529 wants to merge 7 commits into
rapidsai:mainfrom
nethum529:fix-2397-callback-alignment
Open

Fix callback resource alignment forwarding#2485
nethum529 wants to merge 7 commits into
rapidsai:mainfrom
nethum529:fix-2397-callback-alignment

Conversation

@nethum529

@nethum529 nethum529 commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Match callback memory resource argument order with CCCL.
  • Forward and validate allocation alignment before calling user callbacks.
  • Update Cython, Python docs, type hints, tests, and the Cython line-length hook for the required SPDX header.

Testing

  • Focused C++ callback and CCCL tests: 7 passed.
  • Focused Python callback tests: 8 passed.
  • pre-commit run --all-files

Breaking change: allocate_callback_t and deallocate_callback_t reorder parameters (stream first) and add an alignment parameter, so existing C++ and Python callbacks must update their signatures; the break is loud (old callbacks fail to compile / raise TypeError) rather than silent. Needs the breaking label.

Closes #2397

Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
@copy-pr-bot

copy-pr-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

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.

@nethum529
nethum529 requested a review from ttnghia July 18, 2026 04:04
@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: a1fb3a58-655a-431f-a015-935849a19d31

📥 Commits

Reviewing files that changed from the base of the PR and between d887588 and e1f169b.

📒 Files selected for processing (3)
  • cpp/tests/mr/callback_mr_tests.cpp
  • python/rmm/rmm/pylibrmm/memory_resource/_memory_resource.pyx
  • python/rmm/rmm/tests/test_callback_memory_resource.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • python/rmm/rmm/pylibrmm/memory_resource/_memory_resource.pyx
  • cpp/tests/mr/callback_mr_tests.cpp

📝 Walkthrough

Summary by CodeRabbit

  • New Features
    • Callback-based memory resources now provide CUDA stream, allocation size, and alignment to allocation and deallocation callbacks.
    • Python callbacks receive the same expanded metadata, including alignment.
  • Bug Fixes
    • Invalid allocation alignments are rejected before callbacks run, with a clear error.
    • Alignment is consistently preserved during allocation and deallocation.
  • Documentation
    • Updated callback usage guidance and examples to reflect the revised signatures.
  • Tests
    • Added coverage for alignment forwarding, invalid alignments, callback errors, and metadata consistency.

Walkthrough

The callback memory resource API now passes stream and alignment through C++ and Python callbacks, validates allocation alignment before invocation, updates documentation and type declarations, and adjusts C++ and Python tests and lint configuration.

Changes

Callback alignment API

Layer / File(s) Summary
C++ callback contract and forwarding
cpp/include/rmm/mr/callback_memory_resource.hpp, cpp/include/rmm/mr/detail/..., cpp/src/mr/detail/...
Callback signatures now place the stream first, include alignment, validate supported allocation alignment, and forward alignment during allocation and deallocation.
C++ callback integration tests
cpp/tests/mr/callback_mr_tests.cpp, cpp/tests/mr/cccl_adaptor_tests.cpp, cpp/tests/mr/mr_ref_callback_tests.cpp
Tests verify callback metadata forwarding, invalid-alignment rejection, logging, and adaptor compatibility.
Python callback bridge and validation
python/rmm/rmm/librmm/..., python/rmm/rmm/pylibrmm/..., python/rmm/rmm/tests/..., .pre-commit-config.yaml
Cython wrappers, Python type declarations, documentation, and tests use alignment-aware callbacks; the Cython lint line-length limit is set to 104.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related PRs

Suggested labels: breaking, improvement

Suggested reviewers: wence-, ttnghia, kylefromnvidia

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.90% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: forwarding callback resource alignment.
Description check ✅ Passed The description matches the changeset and correctly describes the API break, tests, and header update.
Linked Issues check ✅ Passed The PR matches #2397 by adding alignment to callback signatures, validating alignment, forwarding it, and updating C++/Python APIs.
Out of Scope Changes check ✅ Passed No clearly unrelated changes stand out; the hook and SPDX updates support the API and header requirements.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
python/rmm/rmm/pylibrmm/memory_resource/_memory_resource.pyx (1)

662-675: 🎯 Functional Correctness | 🔴 Critical | 🏗️ Heavy lift

Preserve the prior Python callback contract through a deprecation cycle.

Existing (size, stream) / (ptr, size, stream) callbacks now fail at their first invocation because this public constructor unconditionally calls the new arities. Provide a temporary legacy adapter with a deprecation warning before making the stream-first, alignment-aware signatures mandatory.

As per coding guidelines, “Treat Python API changes that break backward compatibility … without a deprecation cycle, as CRITICAL.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@python/rmm/rmm/pylibrmm/memory_resource/_memory_resource.pyx` around lines
662 - 675, The callback_memory_resource constructor now invokes only the new
stream-first, alignment-aware callback signatures, breaking existing callbacks
using (size, stream) and (ptr, size, stream). Update __init__ to detect and
adapt legacy callback arities through a temporary compatibility wrapper, emit a
deprecation warning when that adapter is used, and preserve the existing
behavior for new signatures while retaining the callback references safely.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@cpp/include/rmm/mr/callback_memory_resource.hpp`:
- Around line 27-61: Preserve backward compatibility for the public
allocate_callback_t and deallocate_callback_t callback declarations by retaining
deprecated legacy aliases or constructor overloads with the previous signatures.
Adapt legacy callbacks to the new alignment-aware callbacks internally, emit
deprecation warnings, and add migration guidance documenting the new signatures
and removal plan.

---

Outside diff comments:
In `@python/rmm/rmm/pylibrmm/memory_resource/_memory_resource.pyx`:
- Around line 662-675: The callback_memory_resource constructor now invokes only
the new stream-first, alignment-aware callback signatures, breaking existing
callbacks using (size, stream) and (ptr, size, stream). Update __init__ to
detect and adapt legacy callback arities through a temporary compatibility
wrapper, emit a deprecation warning when that adapter is used, and preserve the
existing behavior for new signatures while retaining the callback references
safely.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: d804c145-8cb1-469a-a1df-1ad6db2d9733

📥 Commits

Reviewing files that changed from the base of the PR and between f91e13d and c82950d.

📒 Files selected for processing (12)
  • .pre-commit-config.yaml
  • cpp/include/rmm/mr/callback_memory_resource.hpp
  • cpp/include/rmm/mr/detail/callback_memory_resource_impl.hpp
  • cpp/src/mr/detail/callback_memory_resource_impl.cpp
  • cpp/tests/mr/callback_mr_tests.cpp
  • cpp/tests/mr/cccl_adaptor_tests.cpp
  • cpp/tests/mr/mr_ref_callback_tests.cpp
  • python/rmm/rmm/librmm/memory_resource.pxd
  • python/rmm/rmm/pylibrmm/memory_resource/_memory_resource.pyi
  • python/rmm/rmm/pylibrmm/memory_resource/_memory_resource.pyx
  • python/rmm/rmm/tests/test_callback_memory_resource.py
  • python/rmm/rmm/tests/test_failure_callback_resource_adaptor.py

Comment on lines +27 to +61
* `void* allocate_callback_t(cuda_stream_view stream, std::size_t bytes, std::size_t alignment,
* void* arg);`
*
* * The callback receives only valid power-of-two alignment values. An invalid alignment raises
* `rmm::logic_error` before callback invocation.
*
* * Returns a pointer to an allocation of at least `bytes` usable immediately on
* `stream`. The stream-ordered behavior requirements are identical to
* `allocate`.
* `stream` with the requested `alignment`. The stream-ordered behavior requirements are
* identical to `allocate`.
*
* * The `arg` is provided to the constructor of the `callback_memory_resource`
* and will be forwarded along to every invocation of the callback function.
*/
using allocate_callback_t = std::function<void*(std::size_t, cuda_stream_view, void*)>;
using allocate_callback_t = std::function<void*(cuda_stream_view, std::size_t, std::size_t, void*)>;

/**
* @brief Callback function type used by callback_memory_resource for deallocation.
*
* The signature of the callback function is:
* `void deallocate_callback_t(void* ptr, std::size_t bytes, cuda_stream_view stream, void* arg);`
* `void deallocate_callback_t(cuda_stream_view stream, void* ptr, std::size_t bytes,
* std::size_t alignment, void* arg);`
*
* * Deallocates memory pointed to by `ptr`. `bytes` specifies the size of the allocation
* in bytes, and must equal the value of `bytes` that was passed to the allocate callback
* function. The stream-ordered behavior requirements are identical to
* `deallocate`.
* function. `alignment` must equal the value of `alignment` that was passed to the allocate
* callback function. The stream-ordered behavior requirements are identical to `deallocate`.
*
* * The callback must not throw. An exception escaping the callback causes termination because
* deallocation is `noexcept`.
*
* * The `arg` is provided to the constructor of the `callback_memory_resource`
* and will be forwarded along to every invocation of the callback function.
*/
using deallocate_callback_t = std::function<void(void*, std::size_t, cuda_stream_view, void*)>;
using deallocate_callback_t =
std::function<void(cuda_stream_view, void*, std::size_t, std::size_t, void*)>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🔴 Critical | 🏗️ Heavy lift

Preserve the previous callback API through a deprecation cycle.

Replacing these public aliases immediately breaks existing callback declarations and constructor call sites. Retain a deprecated legacy overload/alias and provide migration guidance before removal.

As per path instructions, “Breaking changes require deprecation warnings and migration guide updates.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cpp/include/rmm/mr/callback_memory_resource.hpp` around lines 27 - 61,
Preserve backward compatibility for the public allocate_callback_t and
deallocate_callback_t callback declarations by retaining deprecated legacy
aliases or constructor overloads with the previous signatures. Adapt legacy
callbacks to the new alignment-aware callbacks internally, emit deprecation
warnings, and add migration guidance documenting the new signatures and removal
plan.

Sources: Coding guidelines, Path instructions

Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
Signed-off-by: nethum529 <nethumweerasinghe.nw@gmail.com>
@wence- wence- added the breaking Breaking change label Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Breaking change

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Fix callback memory resource allocation API to match CCCL and support alignment

2 participants