Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/BackendGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ overridden before passing it to `createBackend` in C++ or `create_backend` in Py
| Option key | Default | Description |
| ---------- | ------- | ----------- |
| `ucx_error_handling_mode` | `peer` | UCX endpoint error handling policy. `peer` requests peer failure reporting; `none` disables it. |
| `ucx_vram_memtype_hint` | `auto` | Controls how NIXL hints the UCX memory type during VRAM registration. Supported values are exactly: `auto`, `none`, `cuda`, `cuda-managed`, `rocm`, `ze-device`. |

The two options are independent; you do not need to set one to use the other.

`ucx_error_handling_mode` affects UCP transport lane selection, not only error reporting.
NIXL creates endpoints with `err_mode` set from this option, and UCP only selects lanes whose
Expand All @@ -193,6 +196,17 @@ can go undetected instead of surfacing as an error.
eligibility in the transport over changing this option in deployments that need remote failure
reporting.

`ucx_vram_memtype_hint` controls the memory type NIXL passes to `ucp_mem_map` when registering
VRAM. `auto` is the recommended default: it hints `ze-device` when the UCX context supports
`ze-device` and none of `cuda`, `cuda-managed` or `rocm`, and otherwise leaves detection to UCX.
`none` never hints, so the two differ only on those contexts, where `none` turns the `ze-device`
hint off. The explicit values
(`cuda`, `cuda-managed`, `rocm`, `ze-device`) force the corresponding memory type.

Value matching is case-sensitive, so `CUDA` is rejected and backend creation fails with an
invalid-argument error. The same happens when an explicit hint is not supported by the UCX context
the backend was created with.

### Make connections (optional):

When a connection is requested to an remote agent, which is possible if the remote agent’s metadata is already loaded, the local Agent would look for common backend plugins between itself and the remote agent, and for each of them initiate a connect by using the **connect** API in SB API of such backends.
Expand Down
9 changes: 9 additions & 0 deletions docs/python_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@ agent = nixl_agent("example_agent", nixl_agent_config(backends=[]))

params = agent.get_plugin_params("UCX")
params["ucx_error_handling_mode"] = "peer" # or "none"
params["ucx_vram_memtype_hint"] = "auto" # recommended default
agent.create_backend("UCX", params)
```

See [UCX backend initialization options](BackendGuide.md#ucx-backend-initialization-options)
for the supported UCX keys and their semantics. Note that `ucx_error_handling_mode` influences
UCP transport lane selection in addition to error reporting.

For `ucx_vram_memtype_hint`:

- `auto` is the recommended default. It hints `ze-device` when the UCX context supports `ze-device` and none of `cuda`, `cuda-managed` or `rocm`, and otherwise leaves detection to UCX.
- `none` never hints and always leaves detection to UCX. It differs from `auto` only on those contexts, where it turns the `ze-device` hint off.
- Explicit accelerator hints are also supported for advanced tuning: `cuda`, `cuda-managed`, `rocm`, `ze-device`.
- Values are case-sensitive, so `CUDA` is rejected.
- An explicit hint fails backend creation when the UCX context does not support the requested memory type.

## Examples

See the [Python examples](../examples/python/) directory for complete working examples including:
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/ucx/ucx_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ nixlUcxEngine::nixlUcxEngine(const nixlBackendInitParams &init_params, size_t nu
err_handling_mode = ucx_err_mode_from_string(*opt);
}

nixl_ucx_vram_memtype_hint_t vram_memtype_hint_policy = nixl_ucx_vram_memtype_hint_t::AUTO;
if (const auto opt = nixl::getBackendParamOptional<std::string>(
custom_params, std::string(nixl_ucx_vram_memtype_hint_param_name))) {
vram_memtype_hint_policy = ucx_vram_memtype_hint_from_string(*opt);
}

const auto engine_config =
nixl::getBackendParamDefaulted(custom_params, "engine_config", std::string());

Expand All @@ -105,7 +111,8 @@ nixlUcxEngine::nixlUcxEngine(const nixlBackendInitParams &init_params, size_t nu
init_params.syncMode,
num_device_channels,
engine_config,
localAgent);
localAgent,
vram_memtype_hint_policy);

uc->warnAboutHardwareSupportMismatch();

Expand Down
175 changes: 172 additions & 3 deletions src/plugins/ucx/ucx_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "ucx_utils.h"

#include <algorithm>
#include <array>
#include <cstring>
#include <exception>
#include <stdexcept>
Expand All @@ -37,6 +38,8 @@ get_ucx_backend_common_options() {

params.emplace(nixl_ucx_err_handling_param_name,
ucx_err_mode_to_string(UCP_ERR_HANDLING_MODE_PEER));
params.emplace(nixl_ucx_vram_memtype_hint_param_name,
ucx_vram_memtype_hint_to_string(nixl_ucx_vram_memtype_hint_t::AUTO));
return params;
}

Expand Down Expand Up @@ -78,6 +81,56 @@ ucx_err_mode_from_string(std::string_view s) {
throw std::invalid_argument(err_msg.str());
}

[[nodiscard]] std::string_view
Comment thread
yafshar marked this conversation as resolved.
Resolved
ucx_vram_memtype_hint_to_string(nixl_ucx_vram_memtype_hint_t t) {
switch (t) {
case nixl_ucx_vram_memtype_hint_t::AUTO:
return "auto";
case nixl_ucx_vram_memtype_hint_t::NONE:
return "none";
case nixl_ucx_vram_memtype_hint_t::CUDA:
return ucs_memory_type_names[UCS_MEMORY_TYPE_CUDA];
case nixl_ucx_vram_memtype_hint_t::CUDA_MANAGED:
return ucs_memory_type_names[UCS_MEMORY_TYPE_CUDA_MANAGED];
case nixl_ucx_vram_memtype_hint_t::ROCM:
return ucs_memory_type_names[UCS_MEMORY_TYPE_ROCM];
case nixl_ucx_vram_memtype_hint_t::ZE_DEVICE:
return ucs_memory_type_names[UCS_MEMORY_TYPE_ZE_DEVICE];
}
throw std::invalid_argument(std::to_string(static_cast<int>(t)));
}

[[nodiscard]] nixl_ucx_vram_memtype_hint_t
ucx_vram_memtype_hint_from_string(std::string_view s) {
constexpr std::array<nixl_ucx_vram_memtype_hint_t, 6> hint_modes = {
nixl_ucx_vram_memtype_hint_t::AUTO,
nixl_ucx_vram_memtype_hint_t::NONE,
nixl_ucx_vram_memtype_hint_t::CUDA,
nixl_ucx_vram_memtype_hint_t::CUDA_MANAGED,
nixl_ucx_vram_memtype_hint_t::ROCM,
nixl_ucx_vram_memtype_hint_t::ZE_DEVICE,
};

for (const auto hint_mode : hint_modes) {
const auto mode_name = ucx_vram_memtype_hint_to_string(hint_mode);
if (mode_name == s) {
return hint_mode;
}
}

std::stringstream err_msg;
err_msg << "Invalid VRAM memtype hint mode: " << s
<< ". Matching is case-sensitive; use exact values. Valid values are: <";
for (size_t i = 0; i < hint_modes.size(); ++i) {
err_msg << ucx_vram_memtype_hint_to_string(hint_modes[i]);
if (i < hint_modes.size() - 1) {
err_msg << "|";
}
}
err_msg << ">";
throw std::invalid_argument(err_msg.str());
}

static void
err_cb_wrapper(void *arg, ucp_ep_h ucp_ep, ucs_status_t status) {
auto ep = static_cast<nixlUcxEp *>(arg);
Expand Down Expand Up @@ -439,6 +492,53 @@ makeMtType(const bool prog_thread, const nixl_thread_sync_t sync_mode) noexcept
nixl::ucx::mt_mode_t::SINGLE;
}

[[nodiscard]] std::string_view
ucx_memory_type_to_string(const ucs_memory_type_t mem_type) {
const auto mem_type_index = static_cast<unsigned>(mem_type);
if (mem_type_index >= static_cast<unsigned>(UCS_MEMORY_TYPE_LAST)) {
return "invalid";
}

return ucs_memory_type_names[mem_type_index];
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

[[nodiscard]] ucs_memory_type_t
ucx_auto_detect_vram_hint(const uint64_t memory_types_mask) {
// AUTO is intentionally conservative: only auto-hint ZE on ZE-only accelerator stacks.
// CUDA is left to UCX auto-detection by default because it is reliable with shared
// primary context, and mixed accelerator environments are treated as ambiguous.
const bool has_ze = UCS_BIT_GET(memory_types_mask, UCS_MEMORY_TYPE_ZE_DEVICE);
const bool has_cuda = UCS_BIT_GET(memory_types_mask, UCS_MEMORY_TYPE_CUDA) ||
UCS_BIT_GET(memory_types_mask, UCS_MEMORY_TYPE_CUDA_MANAGED);
const bool has_rocm = UCS_BIT_GET(memory_types_mask, UCS_MEMORY_TYPE_ROCM);

if (has_ze && !has_cuda && !has_rocm) {
return UCS_MEMORY_TYPE_ZE_DEVICE;
}

return UCS_MEMORY_TYPE_UNKNOWN;
}

[[nodiscard]] ucs_memory_type_t
ucx_policy_to_mem_type(const nixl_ucx_vram_memtype_hint_t policy) {
switch (policy) {
case nixl_ucx_vram_memtype_hint_t::AUTO:
case nixl_ucx_vram_memtype_hint_t::NONE:
return UCS_MEMORY_TYPE_UNKNOWN;
case nixl_ucx_vram_memtype_hint_t::CUDA:
return UCS_MEMORY_TYPE_CUDA;
case nixl_ucx_vram_memtype_hint_t::CUDA_MANAGED:
return UCS_MEMORY_TYPE_CUDA_MANAGED;
case nixl_ucx_vram_memtype_hint_t::ROCM:
return UCS_MEMORY_TYPE_ROCM;
case nixl_ucx_vram_memtype_hint_t::ZE_DEVICE:
return UCS_MEMORY_TYPE_ZE_DEVICE;
}

NIXL_FATAL << "Invalid VRAM memtype hint mode: " << static_cast<int>(policy);
std::terminate();
}

} // namespace

nixlUcxContext::nixlUcxContext(const std::vector<std::string> &devs,
Expand All @@ -447,10 +547,12 @@ nixlUcxContext::nixlUcxContext(const std::vector<std::string> &devs,
nixl_thread_sync_t sync_mode,
size_t num_device_channels,
const std::string &engine_config,
const std::string &name)
const std::string &name,
nixl_ucx_vram_memtype_hint_t vram_mem_type_hint_policy)
: mtType_(makeMtType(prog_thread, sync_mode)),
ucpVersion_(makeUcpVersion()),
name_(name) {
name_(name),
vramMemTypeHintPolicy_(vram_mem_type_hint_policy) {

ucp_params_t ucp_params;
ucp_params.field_mask = UCP_PARAM_FIELD_FEATURES | UCP_PARAM_FIELD_MT_WORKERS_SHARED;
Expand Down Expand Up @@ -532,6 +634,9 @@ nixlUcxContext::nixlUcxContext(const std::vector<std::string> &devs,
}

ctx.reset(new_ctx);

// ctx is released by its deleter if this throws, so no manual cleanup is needed here.
Comment thread
yafshar marked this conversation as resolved.
Resolved
resolveMemoryTypeConfig();
}

std::ostream &
Expand All @@ -553,6 +658,63 @@ nixlUcxContext::supportsMemoryType(ucs_memory_type_t mem_type) const {
return UCS_BIT_GET(attr.memory_types, mem_type) != 0;
}

void
nixlUcxContext::resolveMemoryTypeConfig() {
ucp_context_attr_t context_attr = {
.field_mask = UCP_ATTR_FIELD_MEMORY_TYPES,
};

const auto status = ucp_context_query(ctx.get(), &context_attr);
Comment thread
yafshar marked this conversation as resolved.
Resolved
if (status != UCS_OK) {
if (vramMemTypeHintPolicy_ != nixl_ucx_vram_memtype_hint_t::AUTO &&
vramMemTypeHintPolicy_ != nixl_ucx_vram_memtype_hint_t::NONE) {
throw std::runtime_error(
"Failed to query UCX context memory types: " +
std::string(ucs_status_string(status)) +
". This query is required for explicit ucx_vram_memtype_hint='" +
std::string(ucx_vram_memtype_hint_to_string(vramMemTypeHintPolicy_)) + "'");
}

NIXL_WARN << "Failed to query UCX context memory types: " << ucs_status_string(status)
<< ", VRAM memory type hinting will be disabled";
return;
}

supportedMemoryTypesMask_ = context_attr.memory_types;

if (vramMemTypeHintPolicy_ == nixl_ucx_vram_memtype_hint_t::AUTO) {
vramMemTypeHint_ = ucx_auto_detect_vram_hint(supportedMemoryTypesMask_);
if (vramMemTypeHint_ != UCS_MEMORY_TYPE_UNKNOWN) {
NIXL_INFO << "VRAM memtype hint mode is auto, selected "
<< ucx_memory_type_to_string(vramMemTypeHint_) << " based on UCX context";
} else {
NIXL_DEBUG << "VRAM memtype hint mode is auto, no unambiguous accelerator memory type"
" found; UCX auto-detection will be used";
}
return;
}

if (vramMemTypeHintPolicy_ == nixl_ucx_vram_memtype_hint_t::NONE) {
NIXL_DEBUG << "VRAM memtype hint mode is none, UCX auto-detection will be used";
return;
}

const auto configured_hint = ucx_policy_to_mem_type(vramMemTypeHintPolicy_);
NIXL_ASSERT(configured_hint != UCS_MEMORY_TYPE_UNKNOWN);

if (!UCS_BIT_GET(supportedMemoryTypesMask_, configured_hint)) {
throw std::invalid_argument(
"Configured VRAM memtype hint '" +
std::string(ucx_vram_memtype_hint_to_string(vramMemTypeHintPolicy_)) +
"' is not supported by current UCX context");
}

vramMemTypeHint_ = configured_hint;
NIXL_INFO << "VRAM memtype hint mode is "
<< ucx_vram_memtype_hint_to_string(vramMemTypeHintPolicy_) << ", using "
<< ucx_memory_type_to_string(vramMemTypeHint_);
}

namespace {
[[nodiscard]] ucs_thread_mode_t
toUcsThreadModeChecked(const nixl::ucx::mt_mode_t t) {
Expand Down Expand Up @@ -655,6 +817,13 @@ nixlUcxContext::memReg(void *addr, size_t size, nixlUcxMem &mem, nixl_mem_t nixl
.length = mem.size,
};

if (nixl_mem_type == nixl_mem_t::VRAM_SEG && vramMemTypeHint_ != UCS_MEMORY_TYPE_UNKNOWN) {
mem_params.field_mask |= UCP_MEM_MAP_PARAM_FIELD_MEMORY_TYPE;
mem_params.memory_type = vramMemTypeHint_;
NIXL_DEBUG << "memReg: hinting " << ucx_memory_type_to_string(mem_params.memory_type)
<< " for VRAM_SEG";
}

ucs_status_t status = ucp_mem_map(ctx.get(), &mem_params, &mem.memh);
if (status != UCS_OK) {
NIXL_ERROR << *this << ": failed to ucp_mem_map: " << ucs_status_string(status);
Expand All @@ -674,7 +843,7 @@ nixlUcxContext::memReg(void *addr, size_t size, nixlUcxMem &mem, nixl_mem_t nixl
if (attr.mem_type == UCS_MEMORY_TYPE_HOST) {
NIXL_ERROR << *this
<< ": VRAM memory is detected as host by UCX. "
"UCX is likely not configured with CUDA/ROCm support. "
"UCX memory type detection/configuration is likely mismatched. "
"VRAM registration cannot proceed.";
ucp_mem_unmap(ctx.get(), mem.memh);
return -1;
Expand Down
27 changes: 26 additions & 1 deletion src/plugins/ucx/ucx_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <atomic>
#include <memory>
#include <string_view>
#include <type_traits>

extern "C" {
Expand All @@ -32,7 +33,17 @@ extern "C" {

#include "absl/strings/numbers.h"

enum class nixl_ucx_vram_memtype_hint_t {
AUTO,
NONE,
CUDA,
CUDA_MANAGED,
ROCM,
ZE_DEVICE,
};

inline constexpr std::string_view nixl_ucx_err_handling_param_name = "ucx_error_handling_mode";
inline constexpr std::string_view nixl_ucx_vram_memtype_hint_param_name = "ucx_vram_memtype_hint";

// The API `ucp_context_query(ctx, &attr)` sets `UCS_MEMORY_TYPE_RDMA` in `attr.memory_types`
// field only from UCX 1.22
Expand Down Expand Up @@ -159,6 +170,12 @@ class nixlUcxContext {
const nixl::ucx::mt_mode_t mtType_;
const unsigned ucpVersion_;
const std::string name_;
const nixl_ucx_vram_memtype_hint_t vramMemTypeHintPolicy_;
uint64_t supportedMemoryTypesMask_{0};
ucs_memory_type_t vramMemTypeHint_{UCS_MEMORY_TYPE_UNKNOWN};

void
resolveMemoryTypeConfig();

[[nodiscard]] bool
supportsMemoryType(ucs_memory_type_t mem_type) const;
Expand All @@ -170,7 +187,9 @@ class nixlUcxContext {
nixl_thread_sync_t sync_mode,
size_t num_device_channels,
const std::string &engine_conf = "",
const std::string &name = "");
const std::string &name = "",
nixl_ucx_vram_memtype_hint_t vram_mem_type_hint_policy =
nixl_ucx_vram_memtype_hint_t::AUTO);
nixlUcxContext(nixlUcxContext &&) = delete;
nixlUcxContext(const nixlUcxContext &) = delete;

Expand Down Expand Up @@ -290,4 +309,10 @@ ucx_err_mode_to_string(ucp_err_handling_mode_t t);
[[nodiscard]] ucp_err_handling_mode_t
ucx_err_mode_from_string(std::string_view s);

[[nodiscard]] std::string_view
ucx_vram_memtype_hint_to_string(nixl_ucx_vram_memtype_hint_t t);

[[nodiscard]] nixl_ucx_vram_memtype_hint_t
ucx_vram_memtype_hint_from_string(std::string_view s);

#endif
Loading
Loading