Fix NVLink IPC offset corruption for sub-allocated GPU tensors#1622
Fix NVLink IPC offset corruption for sub-allocated GPU tensors#1622alogfans merged 3 commits intokvcache-ai:mainfrom
Conversation
…nsport When framework caching allocators (PyTorch, etc.) sub-allocate tensors within larger cudaMalloc segments, cudaIpcGetMemHandle returns a handle for the entire segment, not the sub-allocation. The existing code stored the sub-allocation address as the buffer base, causing relocateShared- MemoryAddress to compute an incorrect offset on the target side. Small tensors (<1MB) sharing a segment would read from the wrong location. Fix: use cuMemGetAddressRange() to resolve the true cudaMalloc base address before registration. Register at segment granularity and skip duplicate registrations when multiple tensors share the same segment.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical issue in the NVLink IPC transport mechanism where sub-allocated GPU tensors, particularly those smaller than 1MB, were experiencing data corruption due to incorrect memory registration. The core problem stemmed from registering sub-pointers instead of the underlying Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the NVLink IPC offset corruption by resolving and registering the true base address of CUDA memory segments, with the registerLocalMemory implementation appearing sound. However, a critical vulnerability exists in the unregistration logic: unregisterLocalMemory fails to resolve the base address of sub-allocated tensors, leading to resource leaks and preventing correct re-registration. Additionally, a race condition was identified in the unregistration flow, which could lead to an inconsistent state if metadata registration fails. These issues require immediate attention to ensure the robustness and correctness of the transport engine.
- unregisterLocalMemory: resolve base address via cuMemGetAddressRange before erasing from tracking set and metadata (matches register path) - registerLocalMemory: insert into registered_base_addrs_ only after addLocalMemoryBuffer succeeds to avoid inconsistent state on failure
| #include <cuda.h> | ||
| #include "cuda_alike.h" |
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
alogfans
left a comment
There was a problem hiding this comment.
relocateSharedMemoryAddress should be changed accordingly.
|
Intro-node NVLink feature is contributed by @TTThanos . PTAL. Thanks |
True. @ishandhanani, could you also modify |
| // Resolve the true cudaMalloc base address. Framework caching allocators | ||
| // (PyTorch, etc.) sub-allocate tensors within larger cudaMalloc segments. | ||
| // cudaIpcGetMemHandle always returns a handle for the entire segment, so | ||
| // we must register at segment granularity for correct IPC relocation. | ||
| CUdeviceptr base_ptr = 0; | ||
| size_t alloc_size = 0; | ||
| CUresult cu_err = | ||
| cuMemGetAddressRange(&base_ptr, &alloc_size, (CUdeviceptr)addr); | ||
| if (cu_err != CUDA_SUCCESS) { | ||
| LOG(ERROR) << "IntraNodeNvlinkTransport: cuMemGetAddressRange failed " | ||
| << "for addr " << addr << " (error " << cu_err << ")"; | ||
| return -1; | ||
| } | ||
|
|
||
| // Skip if this cudaMalloc block is already registered | ||
| if (registered_base_addrs_.count((uint64_t)base_ptr)) { | ||
| return 0; | ||
| } | ||
|
|
There was a problem hiding this comment.
Learn a lot from these codes.
…ort/intranode_nvlink_transport.cpp
…he-ai#1622) * Fix IPC offset corruption for sub-allocated GPU tensors in NVLink transport When framework caching allocators (PyTorch, etc.) sub-allocate tensors within larger cudaMalloc segments, cudaIpcGetMemHandle returns a handle for the entire segment, not the sub-allocation. The existing code stored the sub-allocation address as the buffer base, causing relocateShared- MemoryAddress to compute an incorrect offset on the target side. Small tensors (<1MB) sharing a segment would read from the wrong location. Fix: use cuMemGetAddressRange() to resolve the true cudaMalloc base address before registration. Register at segment granularity and skip duplicate registrations when multiple tensors share the same segment. * Address review: fix unregister and insert-before-confirm - unregisterLocalMemory: resolve base address via cuMemGetAddressRange before erasing from tracking set and metadata (matches register path) - registerLocalMemory: insert into registered_base_addrs_ only after addLocalMemoryBuffer succeeds to avoid inconsistent state on failure * Update mooncake-transfer-engine/src/transport/intranode_nvlink_transport/intranode_nvlink_transport.cpp --------- Co-authored-by: Ishan Dhanani <ishan@dhanani.dev> Co-authored-by: Teng Ma <teng-ma@linux.alibaba.com>
Problem
registerLocalMemory()inIntraNodeNvlinkTransportstores the caller-provided pointer as the buffer base address. When frameworks like PyTorch sub-allocate tensors within largercudaMallocsegments (via caching allocators), this pointer is not thecudaMallocbase. SincecudaIpcGetMemHandlealways returns a handle for the entirecudaMallocallocation,relocateSharedMemoryAddress()computes the wrong offset on the target side -- the sub-allocation offset within the segment is lost.This corrupts all small tensors (<1MB) that share a
cudaMallocsegment. Large tensors (>1MB) are unaffected because PyTorch gives them their own segment.Fix
Use
cuMemGetAddressRange()to resolve the truecudaMallocbase address and allocation size before registration. Register at segment granularity and skip duplicates when multiple tensors share the same segment.Testing
Reproduced with a two-process test (seed on GPU 0, target on GPU 1) transferring tensors of varying sizes (64B, 16KB, 128KB, 512KB, 4MB). Before fix: 4/6 tensors corrupted. After fix: 6/6 correct.
Also validated end-to-end with SGLang remote instance weight loading (Qwen3-235B-A22B-FP8, TP=4, NVLink transport).
Note
Claude code helped me find this bug while working on sharding weights for transfer in sgl-project/sglang#19983. But I reviewed the fix and have tried it and it works