Skip to content

[core][rdt] Application-level exclusive NIC assignment for NIXL/UCX t… - #64542

Open
AyushKashyapII wants to merge 27 commits into
ray-project:masterfrom
AyushKashyapII:rdt-nic-allocator
Open

AyushKashyapII wants to merge 27 commits into
ray-project:masterfrom
AyushKashyapII:rdt-nic-allocator

Conversation

@AyushKashyapII

Copy link
Copy Markdown
Contributor

Why

Ray actors using RDT/NIXL over UCX don't control which RDMA NIC they use —UCX picks devices on its own. On multi-NIC nodes this means several actors can silently pile onto the same NIC while others sit idle, causing unpredictable tensor-transfer performance with no visible error.

Closes/Addresses #64426.

What

Per discussion on the issue (thanks @Sparks0219 @stephanie-wang), this implements NIC pinning entirely at the application level inside ray.experimental.rdt — no Ray Core / scheduler / resource-model changes.This keeps the change small, fully opt-in, and iterable, while leaving the door open to promote parts of it into core later if the design holds up.

ray/experimental/rdt/nic_allocator.py (new)

  • discover_rdma_nics() — enumerates port-qualified RDMA device names (e.g. mlx5_0:1) from /sys/class/infiniband, the same sysfs surface
    _is_efa_available() already relies on (works inside containers/pods where netdevs are namespaced away). Returns [] on non-RDMA/non-Linux hosts.
  • NICAllocator — a small @ray.remote actor acting as a cluster-wide registry of (node, NIC) -> owning actor. Runs as a single detached singleton; single-threaded actor execution gives us race-free acquire/release for free. Supports idempotent node registration, re-entrant acquisition (an actor re-acquiring gets the same NIC back instead of leaking a second one), and a snapshot() for observability.
  • acquire_nic_for_current_actor() / release_nic_for_current_actor() — the public entry points. Both are no-ops unless
    RAY_RDT_NIC_PINNING=1 is set, and fail open on any error (discovery failure, allocator unreachable, timeout) by returning None / logging-and-returning, so a broken allocator degrades to today's behavior rather than breaking transfers.

ray/experimental/rdt/nixl_tensor_transport.py

_init_nixl_agent() now calls acquire_nic_for_current_actor() and, if it gets a NIC back, sets UCX_NET_DEVICES before constructing the NIXL agent (UCX reads this at context-init time). Scoped to the UCX backend only — LIBFABRIC/EFA is untouched. A user-supplied UCX_NET_DEVICES
always takes precedence and is never overwritten.

ray/experimental/rdt/rdt_manager.py

RDTManager.shutdown() now calls release_nic_for_current_actor() to return the actor's NIC to the pool on clean shutdown. (Known gap: a SIGKILLed actor leaks its NIC until release_all_for_node is called — snapshot() gives operators visibility into this; a reaper is a natural follow-up.)

ray/tests/test_rdt_nic_allocator.py (new)

  • discover_rdma_nics against a fake sysfs tree (parametrized: no devices / single device / multi-port / multi-device, verifying sorted determinism).
  • NICAllocator logic exercised directly as a plain class (no cluster): acquire/release cycle, exhaustion, re-entrancy, idempotent registration, unknown node, release_all_for_node.
  • Fail-open checks: flag unset, and flag set but no NICs discovered —both return None before any Ray call is made.
  • One end-to-end test with ray_start_regular: 4 actors contend for 2 fake NICs, exactly 2 succeed, and releasing one frees a slot for a waiter.

Design notes / open questions carried from the issue discussion

  • Opt-in and fail-open by design — with RAY_RDT_NIC_PINNING unset, every code path here is a no-op; existing RDT/NIXL users see zero behavior change.
  • Not scheduler-aware — the allocator can refuse a NIC when the pool is exhausted, but it does not (and cannot, from application code) prevent Ray from placing another actor on an already-full node. That trade-off
    is what keeps this out of Ray Core for v1, per @stephanie-wang's suggestion.
  • GPU/NIC PCIe affinity (e.g. matching topology via nvidia-smi topo -m) is intentionally out of scope here — this PR only does exclusive assignment. Happy to iterate on affinity-aware allocation as a follow-up once the basic mechanism is validated.
  • NIC granularity: one allocatable unit = one (device, port) pair, not one HCA. Flagging this in case multi-port HCAs should be treated differently.

Test plan

  • New unit tests in test_rdt_nic_allocator.py (sysfs discovery, allocator logic, fail-open paths, end-to-end multi-actor contention)
  • Manual validation on real RDMA hardware (multi-NIC node) — checking UCX_NET_DEVICES is set correctly per actor and transfers no longer collide on one NIC
  • Confirmed default behavior (flag unset) is unchanged for existing NIXL tests

@AyushKashyapII
AyushKashyapII requested a review from a team as a code owner July 6, 2026 11:36

@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 an application-level exclusive NIC assignment mechanism for RDT/NIXL transfers to prevent network device contention among actors on the same node. It adds a cluster-wide NICAllocator actor, RDMA NIC discovery utilities, and integration hooks for acquiring and releasing NICs. The review feedback focuses on performance optimizations, suggesting the use of a process-local variable to track acquired NICs and avoid redundant remote calls during shutdown, as well as checking the environment variable before importing and invoking allocator functions when NIC pinning is disabled.

Comment on lines +29 to +30
_INFINIBAND_SYSFS_ROOT = "/sys/class/infiniband"

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

To avoid unnecessary and potentially blocking GCS/remote calls during actor shutdown, we should track whether the current process actually successfully acquired a NIC. Let's define a process-local variable _acquired_nic to store the name of the acquired NIC.

_INFINIBAND_SYSFS_ROOT = "/sys/class/infiniband"

# Track the currently acquired NIC for this process to avoid unnecessary release calls on shutdown.
_acquired_nic: Optional[str] = None

Comment on lines +154 to +162
nic = ray.get(allocator.acquire.remote(node_id, actor_id), timeout=timeout_s)
if nic is None:
logger.warning(
"All %d RDMA NICs on node %s are already assigned; "
"falling back to unpinned UCX device selection.",
len(nics),
node_id,
)
return nic

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

Update acquire_nic_for_current_actor to record the successfully acquired NIC in the process-local _acquired_nic variable.

        nic = ray.get(allocator.acquire.remote(node_id, actor_id), timeout=timeout_s)
        if nic is None:
            logger.warning(
                "All %d RDMA NICs on node %s are already assigned; "
                "falling back to unpinned UCX device selection.",
                len(nics),
                node_id,
            )
        else:
            global _acquired_nic
            _acquired_nic = nic
        return nic

Comment on lines +172 to +187
def release_nic_for_current_actor(timeout_s: float = 5.0) -> None:
"""Best-effort release of this actor's NIC on shutdown."""
if not _nic_pinning_enabled():
return
try:
ctx = ray.get_runtime_context()
actor_id = ctx.get_actor_id()
if actor_id is None:
return
allocator = ray.get_actor(NIC_ALLOCATOR_NAME, namespace=NIC_ALLOCATOR_NAMESPACE)
ray.get(
allocator.release.remote(ctx.get_node_id(), actor_id),
timeout=timeout_s,
)
except Exception:
logger.debug("RDT NIC release skipped.", exc_info=True)

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

Update release_nic_for_current_actor to check _acquired_nic and return early if no NIC was acquired. This avoids unnecessary blocking calls to ray.get_actor and ray.get during shutdown for actors/workers that never held a NIC.

Suggested change
def release_nic_for_current_actor(timeout_s: float = 5.0) -> None:
"""Best-effort release of this actor's NIC on shutdown."""
if not _nic_pinning_enabled():
return
try:
ctx = ray.get_runtime_context()
actor_id = ctx.get_actor_id()
if actor_id is None:
return
allocator = ray.get_actor(NIC_ALLOCATOR_NAME, namespace=NIC_ALLOCATOR_NAMESPACE)
ray.get(
allocator.release.remote(ctx.get_node_id(), actor_id),
timeout=timeout_s,
)
except Exception:
logger.debug("RDT NIC release skipped.", exc_info=True)
def release_nic_for_current_actor(timeout_s: float = 5.0) -> None:
"""Best-effort release of this actor's NIC on shutdown."""
global _acquired_nic
if not _nic_pinning_enabled() or _acquired_nic is None:
return
try:
ctx = ray.get_runtime_context()
actor_id = ctx.get_actor_id()
if actor_id is None:
return
allocator = ray.get_actor(NIC_ALLOCATOR_NAME, namespace=NIC_ALLOCATOR_NAMESPACE)
ray.get(
allocator.release.remote(ctx.get_node_id(), actor_id),
timeout=timeout_s,
)
_acquired_nic = None
except Exception:
logger.debug("RDT NIC release skipped.", exc_info=True)

def _init_nixl_agent(self):
"""Builds the NIXL agent for the selected backend."""
backend = self.select_backend()
if backend == "UCX" and "UCX_NET_DEVICES" not in os.environ:

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

Optimize the backend check to also verify if RAY_RDT_NIC_PINNING is enabled before importing and calling acquire_nic_for_current_actor. This completely avoids any import and function call overhead for the default case where NIC pinning is disabled.

Suggested change
if backend == "UCX" and "UCX_NET_DEVICES" not in os.environ:
if (
backend == "UCX"
and "UCX_NET_DEVICES" not in os.environ
and os.environ.get("RAY_RDT_NIC_PINNING", "0") == "1"
):

Comment on lines +228 to +232
# Return this actor's pinned RDMA NIC (if any) to the cluster-wide
# pool. No-op unless RAY_RDT_NIC_PINNING=1.
from ray.experimental.rdt.nic_allocator import release_nic_for_current_actor

release_nic_for_current_actor()

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

Optimize the shutdown hook to only import and call release_nic_for_current_actor if RAY_RDT_NIC_PINNING is enabled. This avoids unnecessary imports and overhead during shutdown for the default case.

Suggested change
# Return this actor's pinned RDMA NIC (if any) to the cluster-wide
# pool. No-op unless RAY_RDT_NIC_PINNING=1.
from ray.experimental.rdt.nic_allocator import release_nic_for_current_actor
release_nic_for_current_actor()
# Return this actor's pinned RDMA NIC (if any) to the cluster-wide
# pool. No-op unless RAY_RDT_NIC_PINNING=1.
if os.environ.get("RAY_RDT_NIC_PINNING", "0") == "1":
from ray.experimental.rdt.nic_allocator import release_nic_for_current_actor
release_nic_for_current_actor()

@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Jul 6, 2026
Comment thread python/ray/_private/worker.py Resolved
@AyushKashyapII

Copy link
Copy Markdown
Contributor Author

hey @Sparks0219
Pushed a follow-up commit addressing the two issues flagged in review:

  1. release_nic_for_current_actor no longer clears _acquired_nic when the release RPC fails — only on confirmed success, so a failed release doesn't silently make the leak permanent.

  2. shutdown_rdt_manager now calls release_nic_for_current_actor unconditionally instead of gating it on RAY_RDT_NIC_PINNING's current value, since that flag can change after a NIC was already acquired. To keep this cheap by default, NICAllocator's @ray.remote wrapping is now lazy (built on first real use) rather than at import time, so the import itself no longer carries any Ray actor-registration cost.

Added regression tests for both. Ready for another look whenever you get a chance.Thanks !!!

Comment thread python/ray/_private/worker.py Resolved
Comment thread python/ray/_private/worker.py Resolved
@AyushKashyapII

Copy link
Copy Markdown
Contributor Author

while working on it @edoakes @Sparks0219 , (release running after GCS teardown): I don't think patching the hook timing is the right fix which i thought of earlier. The actor calling release() on its way out is inherently unreliable -- it also doesn't cover SIGKILL, which was already a known gap. I'd rather flip the design so the allocator verifies actor liveness itself and reclaims NICs from dead owners opportunistically (checked only when the pool looks exhausted, not via a background poller), which would fix both issues with one mechanism instead of chasing hook-ordering edge cases.

Before I implement that, I want to check: is there an existing Ray-internal way to check "is this actor still alive" that you'd consider appropriate for application-level code to depend on? The two I'm aware of are ray.util.state.get_actor() (public API, but depends on the dashboard/ state API being up) or a lower-level GCS actor-info accessor (more reliable, but reaches further into internals than the rest of this PR does). Wanted your take before building on either.

@AyushKashyapII

Copy link
Copy Markdown
Contributor Author

hey @Sparks0219
Pushed two more fixes:

  1. discover_rdma_nics now filters out inactive (non-ACTIVE) ports via sysfs state, so a down port can no longer be pinned and steer UCX at an unusable device.

  2. NICAllocator state is now persisted to the internal KV store on every mutation and rehydrated on init, so a restarted/re-created allocator recovers existing assignments instead of starting empty (which could silently double-assign a NIC already held by a surviving actor). Also added max_restarts=-1 and soft head-node affinity so recovery happens in place more often.

Still open: the release-after-GCS-teardown issue. I don't think patching hook timing is the right fix -- I'd rather have the allocator verify actor liveness itself and reclaim from dead owners opportunistically (checked only when the pool looks exhausted), which also covers the existing SIGKILL gap in one mechanism instead of two.

Before I build that: is there a Ray-internal way to check "is this actor still alive" that you'd consider appropriate for application-level code to depend on? I'm weighing ray.util.state.get_actor() (public, but needs the dashboard/state API up) against a lower-level GCS actor-info accessor (more reliable, but reaches further into internals than the rest of this PR does). Wanted your take before picking one and building on it -- happy to go either way, or something else entirely if you have a preference.

@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 Aug 12, 2026
@github-actions github-actions Bot 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 Aug 12, 2026
@Sparks0219

Copy link
Copy Markdown
Contributor

Hey sorry for the delay in reviewing, will take a look in the next couple days @AyushKashyapII

@AyushKashyapII

Copy link
Copy Markdown
Contributor Author

hey @Sparks0219 thanks for the reply i have corrected the build error and merged it with the master branch .it should be up for merge .would be happy to make necessary changes which u think are important
thanks for ur time !!!

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@AyushKashyapII

Copy link
Copy Markdown
Contributor Author

hey @Sparks0219 ,pinging this again the issue has been left stale for weeks and i just wanted to get ur attention into it
when u get time it would be really helpful if u could look into it

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 4367e7a. Configure here.

@Sparks0219

Copy link
Copy Markdown
Contributor

Hey @AyushKashyapII, apologies I went on vacation and I'll be back by next week, I'll take a look at this then

@AyushKashyapII

Copy link
Copy Markdown
Contributor Author

hi @Sparks0219 , enjoy ur vacation !!! just wanted to ping this i have fixed the build errors which showed up and when u get time would love to get ur review on this .
thanks

@AyushKashyapII

Copy link
Copy Markdown
Contributor Author

hey @Sparks0219 wanted to ping this again i wanted to close this PR hopefully if the solution satisfies u and would be ready to make any changes if required . could u please take a look into it
thanks for ur time

@AyushKashyapII

Copy link
Copy Markdown
Contributor Author

hey @Sparks0219 was just hoping if u could find some time to like review this PR.this has been stale for a while and ur review would be of great help
thanks for ur time

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

community-contribution Contributed by the community core Issues that should be addressed in Ray Core 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.

2 participants