Repository navigation
Conversation
Signed-off-by: dayshah <[email protected]>
Signed-off-by: dayshah <[email protected]>
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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)|
This pull request has been automatically marked as stale because it has not had 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. |
| # 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": |
There was a problem hiding this comment.
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?
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...