Skip to content

[core][rdt] Let torch create pinned receiving buffers if possible - #59398

Open
dayshah wants to merge 2 commits into
ray-project:masterfrom
dayshah:pin-recv-if-possible
Open

dayshah wants to merge 2 commits into
ray-project:masterfrom
dayshah:pin-recv-if-possible

Conversation

@dayshah

@dayshah dayshah commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

Description

Torch has its own memory caching allocator for pinned (page-locked / not swappable) memory. Currently we create the receiving buffers on cpu memory without pinning them and rely on the rdma memory registration to actually pin the memory (e.g. nixl's memory registration function will actually call mlock). Torch keeps the pinned memory around though and on repeated transfers, it's less likely that we'll pay the cost of allocating, faulting in, and locking pages on the recv. Two of the more expensive parts of rdma memory registration right now are faulting in the pages and locking them. Basically torch provides us with this kind of half-registered memory pool that seems like a decent alternative to building a registered memory pool ourselves.

On the latency tests on 1GB tensors in rdt_single_node_microbenchmark.py, we can get 30% faster overall transfers using nixl on cpu. Note that these are just single node ipc's so the actual transfer time isn't reality but basically eliminating receving buffer creation + registration a lot of the time should still help a lot in other cases and are usually always a big part of the recv.

The gotcha with this is that torch actually uses cuda's cudaHostAlloc to allocate the memory on the cpu, so a user can't actually use this on a no gpu machine. To make this even worse, torch doesn't just check for the existence of the library if you set pin memory, it actually checks for the existence of a gpu so if you're running this on a ray actor with num_gpus=0, you won't be able to pin the memory even if the node does actually have gpus and cuda libraries because Ray will set cuda_visible_devices in the process's env...

@dayshah
dayshah requested a review from a team as a code owner December 12, 2025 01:16
@dayshah dayshah added the go add ONLY when ready to merge, run all tests label Dec 12, 2025

@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 introduces a performance optimization by using Torch's pinned memory allocator for receiving buffers when possible. This is a good change that can improve data transfer efficiency. The implementation correctly checks for CUDA availability and CPU device type before enabling memory pinning. I've added one comment suggesting a small refactoring to make the code more concise and maintainable by reducing duplication.

Comment on lines +101 to +108
tensor = None
# Torch won't allow us to pin memory if cuda isn't available because it actually uses cudaHostAlloc
# Doing this through torch here is beneficial because it has a caching allocator that will keep
# and reuse old pinned memory.
if torch.cuda.is_available() and device.type == "cpu":
tensor = torch.empty(shape, dtype=dtype, device=device, pin_memory=True)
else:
tensor = torch.empty(shape, dtype=dtype, device=device)

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.

medium

This logic can be simplified to avoid code duplication and the unnecessary initialization of tensor to None. You can determine whether to pin memory with a boolean flag and then make a single call to torch.empty. The pin_memory argument is safely ignored for non-CPU tensors.

            # Torch won't allow us to pin memory if cuda isn't available because it actually uses cudaHostAlloc
            # Doing this through torch here is beneficial because it has a caching allocator that will keep
            # and reuse old pinned memory.
            pin_memory = torch.cuda.is_available() and device.type == "cpu"
            tensor = torch.empty(shape, dtype=dtype, device=device, pin_memory=pin_memory)

@ray-gardener ray-gardener Bot added the core Issues that should be addressed in Ray Core label Dec 12, 2025
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had
any activity for 14 days. It will be closed in another 14 days if no further activity occurs.
Thank you for your contributions.

You can always ask for help on our discussion forum or Ray's public slack channel.

If you'd like to keep this open, just leave any comment, and the stale label will be removed.

@github-actions github-actions Bot added the stale The issue is stale. It will be closed within 7 days unless there are further conversation label Dec 26, 2025
@dayshah dayshah added unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it. and removed stale The issue is stale. It will be closed within 7 days unless there are further conversation labels Dec 30, 2025
# Torch won't allow us to pin memory if cuda isn't available because it actually uses cudaHostAlloc
# Doing this through torch here is beneficial because it has a caching allocator that will keep
# and reuse old pinned memory.
if torch.cuda.is_available() and device.type == "cpu":

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.

is there a way to check the num_gpus as well as you mentioned in the PR description that this fails if torch can't detect a gpu?

This branch has not been deployed

No deployments
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 unstale A PR that has been marked unstale. It will not get marked stale again if this label is on it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants