Skip to content

[core][rdt] implement sender side memory pool + perf improvements - #65205

Merged
stephanie-wang merged 8 commits into
ray-project:masterfrom
Sparks0219:joshlee/dont-copy-storage-tensor-into-memory-pool
Sep 1, 2026
Merged

stephanie-wang merged 8 commits into
ray-project:masterfrom
Sparks0219:joshlee/dont-copy-storage-tensor-into-memory-pool

Conversation

@Sparks0219

@Sparks0219 Sparks0219 commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Rewrite of the RDT NIXL memory pool + a couple performance improvements.
The original RDT NIXL memory pool had a couple key issues, most notably:
1.) It copied the entire storage tensor backing the views
2.) It did not collapse the transfer descriptors resulting in the transfer speed scaling with the number of tensors stored into the pool, even though they were backed by contiguous memory

The new pool solves both of these issues, and only copies the user provided views into the memory pool. It does this in two phases:

  • it calls packed_offsets on the tensor sizes + their dtypes to figure out their packed size. This packed size is because the views could have different dtypes, but to construct a view from a tensor they need to be byte aligned by a multiple of the dtype size
  • it then calls allocate_group to find a block (happy path) or multiple non contiguous blocks to copy the packed representation into.
  • we then collapse the xfer_descs in _merged_desc to minimize the number of xfer descs and reconstruct the views on the other side.

Signed-off-by: Joshua Lee <[email protected]>
Signed-off-by: Joshua Lee <[email protected]>
@Sparks0219
Sparks0219 requested a review from a team as a code owner August 4, 2026 15:23

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the NIXL memory pool management to pack tensors into contiguous pool blocks based on object IDs rather than individual storage pointers, and introduces several microbenchmarks to measure weight-syncing performance. The review feedback highlights critical bugs, including an AttributeError from accessing dtype.itemsize (which is not a valid PyTorch attribute), RuntimeErrors when calling get_device() on CPU tensors, and a potential cleanup failure when handling partially initialized tensor lists during exceptions. Additionally, performance optimizations are suggested to reduce $O(N^2)$ and $O(K^2)$ complexity in the packing and splitting algorithms to linear time.

@cursor cursor 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.

Cursor Bugbot has reviewed your changes using default effort and found 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit ea81e8b. Configure here.

@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Aug 4, 2026
Signed-off-by: Joshua Lee <[email protected]>
Signed-off-by: Joshua Lee <[email protected]>
Signed-off-by: Joshua Lee <[email protected]>
@Sparks0219 Sparks0219 added the go add ONLY when ready to merge, run all tests label Aug 18, 2026
Signed-off-by: Joshua Lee <[email protected]>
@Sparks0219 Sparks0219 changed the title [WIP] memory pool improvements [core][rdt] implement sender side memory pool + perf improvements Aug 18, 2026

@stephanie-wang stephanie-wang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good, just had some comments on simplifying it a bit. Still reviewing tests.

Comment on lines +322 to +326
Consecutive tensors that are adjacent in both the source storage and the
pool are copied together as one device copy. Weight-sync layouts, where
the tensors are ordered views of one weight, collapse to a single copy
per block. Anything else, such as interleaved order or separately
allocated tensors, simply forms chains of one and copies per tensor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is a bit of an over-optimization, not clear this is a common use case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this mainly matters if you do a ray.put on a bunch of contiguous views for the memory pool. I found that for the 10k view experiment that I did it added around 200ms to ray.put , and with this optimization it was around 2ms. I could put this in a separate PR if it's a bit easier to review.

Comment on lines +462 to +465
# NIXL requires the local and remote lists to agree on descriptor
# count and length, so build both together: one descriptor for a
# whole group when the user's buffers already match the packed
# layout, otherwise one per tensor.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the chance of this case where you are using packed tensors and the user's buffers match the packed tensors exactly isn't very high and it makes the code harder to follow, so I would remove this optimization for now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmmm yea that makes sense, I think my only hesitancy would be that in the 10k view case we would then always do 10k NIXL READ calls which would increase the latency by like 3x then doing it all in one pull. It was a bit of an engineered example though. Should I just leave it as a follow up for now?

Signed-off-by: Joshua Lee <[email protected]>
@stephanie-wang stephanie-wang self-assigned this Aug 24, 2026
@stephanie-wang

Copy link
Copy Markdown
Contributor

Yes, I think we should leave out the optimizations that I commented on earlier. I'm not sure if it makes sense to merge them at all, even in a follow-up PR. We can sync about that offline.

Signed-off-by: Joshua Lee <[email protected]>
@Sparks0219

Copy link
Copy Markdown
Contributor Author

Yes, I think we should leave out the optimizations that I commented on earlier. I'm not sure if it makes sense to merge them at all, even in a follow-up PR. We can sync about that offline.

Sounds good, removed them

@stephanie-wang
stephanie-wang merged commit 16002dd into ray-project:master Sep 1, 2026
6 checks passed
nadongjun pushed a commit to nadongjun/ray that referenced this pull request Oct 6, 2026
…y-project#65205)

Rewrite of the RDT NIXL memory pool + a couple performance improvements.
The original RDT NIXL memory pool had a couple key issues, most notably:
1.) It copied the entire storage tensor backing the views 
2.) It did not collapse the transfer descriptors resulting in the
transfer speed scaling with the number of tensors stored into the pool,
even though they were backed by contiguous memory

The new pool solves both of these issues, and only copies the user
provided views into the memory pool. It does this in two phases:
- it calls packed_offsets on the tensor sizes + their dtypes to figure
out their packed size. This packed size is because the views could have
different dtypes, but to construct a view from a tensor they need to be
byte aligned by a multiple of the dtype size
- it then calls allocate_group to find a block (happy path) or multiple
non contiguous blocks to copy the packed representation into.
- we then collapse the xfer_descs in _merged_desc to minimize the number
of xfer descs and reconstruct the views on the other side.

---------

Signed-off-by: Joshua Lee <[email protected]>
Sparks0219 added a commit that referenced this pull request Oct 8, 2026
Looks like due to the sender side pool refactor #65205 the NIXL
cross-device test was broken. This was because we weren't checking the
memory type of the receive side tensors and only using the device type
sent via the remote descriptors causing NIXL to fail because we were
saying a CPU tensor was on CUDA, vice versa. Fixing it by also taking
into account the device type on the target buffers.

---------

Signed-off-by: Joshua Lee <[email protected]>
sai-miduthuri added a commit that referenced this pull request Oct 21, 2026
…67212)

Cherry-pick of #66772 (master commit
36384df) onto `releases/2.60.0`.

## Why

`//python/ray/tests:rdt/test_rdt_nixl` fails in the `:ray: core: multi
gpu tests` and `:ray: core: multi gpu cu130 tests` jobs in all 11
`releases/2.60.0` postmerge builds so far, including build 20362 for the
current branch head. In each one, `test_nixl_cross_device_fetch` fails
with `NIXL_ERR_NOT_FOUND`.

The cause is #65205 (sender-side memory pool), which this branch has.
Since then, a fetch into target buffers labels the receiver's buffers
with the sender's device type, so a CPU buffer is described to NIXL as
CUDA, and vice versa. #66772 gives the local and remote descriptor lists
their own memory types. It also rejects target buffers that mix device
types, and tests that.

On master, the test failed in every release-automation postmerge build
that ran it, from the branch point through build 20195. It has passed in
all 34 since build 20205, the first to include #66772.

## Cherry-pick details

Applied cleanly with `git cherry-pick -x`. The change is identical to
the master commit:

- Same 2 files, 28 insertions and 3 deletions, and the same `git
patch-id --stable`.
- The diff text matches except for the three `nixl_tensor_transport.py`
hunk headers. They start 2 lines earlier here, because this branch
doesn't have the 2 lines #66777 added above them on master.

No prerequisite commits are missing:

- On master, only #66777 (raise when a cached registration is smaller
than the tensor) touched `python/ray/experimental/rdt/` or
`python/ray/tests/rdt/` between the branch cut and #66772. It changes
the NIXL registration cache and adds a docstring note in `util.py`;
#66772 neither touches nor uses either change.
- After the pick, `test_rdt_nixl.py` is identical to master at
36384df, and `nixl_tensor_transport.py` differs only by #66777's
lines.
- On this branch, no commit after the cut touched those directories.

Not a duplicate: this branch doesn't contain #66772, and no PR against
`releases/2.60.0` references it.

## Testing

| Command | Result |
|---|---|
| `pre-commit run --files
python/ray/experimental/rdt/nixl_tensor_transport.py
python/ray/tests/rdt/test_rdt_nixl.py` | Passed (ruff, black, pydoclint,
docstyle, import order, and the other applicable hooks) |

I couldn't run `test_rdt_nixl` locally because it needs GPUs and NIXL.
In CI it runs in the `:ray: core: multi gpu tests` and `:ray: core:
multi gpu cu130 tests` jobs. On premerge and microcheck, those jobs wait
on the "run multi gpu tests" block step.

AI assistance was used to prepare this cherry-pick.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Joshua Lee <[email protected]>
Signed-off-by: Sai Miduthuri <[email protected]>
Co-authored-by: Joshua Lee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants