Skip to content
Merged
5 changes: 4 additions & 1 deletion .typos.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[default]
extend-ignore-words = ["CANN", "ASO", "fre", "wqs"]
extend-ignore-words = ["CANN", "ASO", "fre", "wqs", "hsa"]

[default.extend-words]
CANN = "CANN"
ASO = "ASO"
fre = "fre"
wqs = "wqs"
# AMD HSA runtime symbol prefix (hsa_*, hsaRes, hsaErr, etc.) — used by the
# ROCm dmabuf MR registration path.
hsa = "hsa"

[files]
extend-exclude = [
Expand Down
10 changes: 9 additions & 1 deletion mooncake-transfer-engine/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ endif()

if(USE_HIP)
target_include_directories(transfer_engine PRIVATE ${HIP_INCLUDE_DIRS})
target_link_libraries(transfer_engine PUBLIC hip::host rt)
# hsa-runtime64 provides hsa_amd_portable_export_dmabuf, used in the
# RDMA transport's HIP dmabuf MR registration path. find_library
# locates it inside the ROCm install (typically /opt/rocm/lib) so the
# linker doesn't depend on the system default search path.
find_library(HSA_RUNTIME_LIBRARY
NAMES hsa-runtime64
PATHS /opt/rocm/lib /opt/rocm-${ROCM_VERSION}/lib
REQUIRED)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does rocm installer force to use this path?

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.

Good catch — /opt/rocm/lib was a hard-coded fallback that doesn't work for HPC installs / DKMS layouts / custom prefixes.

Pushed 40f6f97 to switch to:

find_package(hsa-runtime64 CONFIG REQUIRED)
target_link_libraries(transfer_engine PUBLIC hip::host hsa-runtime64::hsa-runtime64 rt)

ROCm ships hsa-runtime64-config.cmake (confirmed at /opt/rocm/lib/cmake/hsa-runtime64/ on ROCm 7.0.2 and 7.2.2 in our two test clusters), and CMAKE_PREFIX_PATH already includes ROCm's cmake dir per common.cmake:279. No paths hard-coded now.

target_link_libraries(transfer_engine PUBLIC hip::host ${HSA_RUNTIME_LIBRARY} rt)
endif()

if(USE_MLU)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
#include "config.h"
#include "cuda_alike.h"
#include "environ.h"
#if defined(USE_HIP)
// hsa_amd_portable_export_dmabuf is the ROCm equivalent of
// cuMemGetHandleForAddressRange(...CU_MEM_RANGE_HANDLE_TYPE_DMA_BUF_FD...).
// Used in the dmabuf branch of registerMemoryRegionInternal below.
#include <hsa/hsa.h>
#include <hsa/hsa_ext_amd.h>
#endif
#include "transport/rdma_transport/endpoint_store.h"
#include "transport/rdma_transport/rdma_endpoint.h"
#include "transport/rdma_transport/rdma_transport.h"
Expand Down Expand Up @@ -338,6 +345,98 @@ int RdmaContext::registerMemoryRegionInternal(void *addr, size_t length,
cuDevicePrimaryCtxRelease(cuDev);
#endif
}
#elif defined(USE_HIP)
// ROCm/HIP equivalent of the CUDA dmabuf path above. Allows AMD GPU
// memory to be registered for RDMA without nvidia-peermem (which has
// no AMD equivalent in most distros). Mirrors the same host/device
// split: ibv_reg_mr() for host memory; ibv_reg_dmabuf_mr() for device
// memory, with the dmabuf fd obtained from
// hsa_amd_portable_export_dmabuf().
//
// Tested on:
// - AMD MI355X (gfx950) + Pensando ionic, ROCm 7.2.2
// - AMD MI300X (gfx942) + Broadcom Thor2 (bnxt_re), ROCm 7.0.2

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not sure that such details are needed in the code. Please consider compressing comments

hipPointerAttribute_t hipAttr{};
hipError_t hipRes = hipPointerGetAttributes(&hipAttr, addr);

if (hipRes != hipSuccess || hipAttr.type == hipMemoryTypeHost ||
hipAttr.type == hipMemoryTypeUnregistered) {
// Host memory — standard ibv_reg_mr() path.
mrMeta.addr = addr;
mrMeta.mr = ibv_reg_mr(pd_, addr, length, access);
} else if (hipAttr.type == hipMemoryTypeDevice ||
hipAttr.type == hipMemoryTypeManaged) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

is it safe to use dmabuf for managed memory? Managed pages can migrate between host and device, and the exported dmabuf fd captures the device-side handle only at export time

// Device memory — use dmabuf fd export + ibv_reg_dmabuf_mr().
// Worker threads (RDMA polling, etc.) may not have the device that
// owns `addr` set as their current HIP device. Mirror the CUDA path's
// ctx-retain/ctx-release dance with a small RAII guard: pin the
// active device to `hipAttr.device` for the duration of the
// hipMemGetAddressRange / hsa_amd_portable_export_dmabuf calls,
// then restore whatever device the caller had.
struct HipDeviceGuard {
int prev_device = 0;
bool need_restore = false;
bool set_ok = false;
explicit HipDeviceGuard(int target_device) {
if (hipGetDevice(&prev_device) == hipSuccess) {
need_restore = (prev_device != target_device);
}
set_ok = (hipSetDevice(target_device) == hipSuccess);
}
~HipDeviceGuard() {
if (need_restore) {
(void)hipSetDevice(prev_device);
}
}
} dev_guard(hipAttr.device);
if (!dev_guard.set_ok) {
LOG(ERROR) << "Failed to set HIP device to " << hipAttr.device
<< " for dmabuf export of " << (uintptr_t)addr;
return ERR_CONTEXT;
}

// Get the allocation base + size, since `addr` may sit at an offset
// within a larger hipMalloc block (caching allocators pack tensors).
hipDeviceptr_t allocBase = nullptr;
size_t allocSize = 0;
hipRes = hipMemGetAddressRange(&allocBase, &allocSize,
reinterpret_cast<hipDeviceptr_t>(addr));
if (hipRes != hipSuccess) {
LOG(ERROR) << "Failed to call hipMemGetAddressRange for "
<< (uintptr_t)addr
<< " hip error=" << hipGetErrorString(hipRes);
return ERR_CONTEXT;
}

int dmabuf_fd = -1;
uint64_t hsa_dmabuf_offset = 0;
hsa_status_t hsaRes = hsa_amd_portable_export_dmabuf(
allocBase, allocSize, &dmabuf_fd, &hsa_dmabuf_offset);
if (hsaRes != HSA_STATUS_SUCCESS) {
const char *hsaErr = nullptr;
hsa_status_string(hsaRes, &hsaErr);
LOG(ERROR) << "Failed to retrieve dmabuf for " << (uintptr_t)addr
<< " base=" << (uintptr_t)allocBase
<< " size=" << allocSize
<< " hsa error=" << (hsaErr ? hsaErr : "unknown");
return ERR_CONTEXT;
}

mrMeta.addr = addr;
// Offset within the dmabuf-backed region: distance from the
// allocation base, plus any offset hsa returned for the export.
uint64_t reg_offset =
(uintptr_t)addr - (uintptr_t)allocBase + hsa_dmabuf_offset;
mrMeta.mr = ibv_reg_dmabuf_mr(pd_, reg_offset, length, (uintptr_t)addr,
Comment thread
alogfans marked this conversation as resolved.
Resolved
dmabuf_fd, access);
const int regErrno = errno;
if (close(dmabuf_fd) != 0) {
PLOG(WARNING) << "Failed to close dmabuf fd";
}
if (!mrMeta.mr) {
errno = regErrno;
}
}
#else
mrMeta.addr = addr;
mrMeta.mr = ibv_reg_mr(pd_, addr, length, access);
Expand Down
Loading