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
30 changes: 30 additions & 0 deletions docs/docs/advanced_features/pd_disaggregation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,36 @@ Serving-side capability enforcement is tracked in [#39122](https://github.com/sg
uv pip install mooncake-transfer-engine
```

### AWS EFA

On an x86_64 AWS EFA instance, replace the standard CUDA 13 Mooncake package with the EFA build. The two packages install the same `mooncake` Python module, so do not install them side by side.

The following commands replace the package in an SGLang CUDA 13 environment while preserving its existing Python dependencies:

```bash
python3 -m pip uninstall -y \
mooncake-transfer-engine-cuda13 \
mooncake-transfer-engine-efa-cuda13
python3 -m pip install --no-deps \
mooncake-transfer-engine-efa-cuda13==0.3.13.post1

export LD_LIBRARY_PATH="/opt/amazon/efa/lib:/opt/amazon/efa/lib64:${LD_LIBRARY_PATH:-}"
export MOONCAKE_PROTOCOL=efa

python3 -c 'from mooncake import engine; assert engine.SUPPORT_EFA'
fi_info -p efa -t FI_EP_RDM
```

The EFA wheel expects a compatible AWS EFA userspace installation on the host and does not bundle `libfabric` or `libefa`. The current wheel is available only for x86_64. It is built without Mooncake EP/PG and the NVLink/MNNVL components, so use the standard Mooncake CUDA package for those features.

The current libfabric EFA provider cannot transfer CUDA VMM-backed buffers. Until [libfabric issue #12853](https://github.com/ofiwg/libfabric/issues/12853) is resolved, apply these settings to both prefill and decode workers:

- Do not set `SGLANG_MOONCAKE_CUSTOM_MEM_POOL`.
- Do not enable `expandable_segments:True` in `PYTORCH_CUDA_ALLOC_CONF` or `PYTORCH_ALLOC_CONF`.

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.

Third VMM source that slips through: SGLANG_ENABLE_POST_CAPTURE_KV_SIZING=1 backs the KV cache with KvVmmBufferOwner (memory_pool.py:2364, cuMemCreate/cuMemMap). post_capture_kv_sizing_planned already opts out when SGLANG_MOONCAKE_CUSTOM_MEM_POOL is set (arg_groups/overrides.py:1822) but knows nothing about MOONCAKE_PROTOCOL, so EFA + post-capture sizing with neither flagged var set still registers VMM buffers and fails the same way.

Either add a bullet here, or add the protocol check to post_capture_kv_sizing_planned.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[by Codex] Addressed in f9b3372: post_capture_kv_sizing_planned now returns false for the Mooncake EFA backend, with unit-test coverage and documentation noting the automatic fallback.

- Do not set `MC_FORCE_MNNVL` or `NCCL_MNNVL_ENABLE`; EFA is not an MNNVL transport.

SGLang validates the first two constraints at startup to avoid a later `fi_read` or `fi_write` failure from libfabric.

### IB Device Configuration

`--disaggregation-ib-device` supports the following formats when using the Mooncake backend:
Expand Down
38 changes: 38 additions & 0 deletions python/sglang/srt/disaggregation/mooncake/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""Mooncake-specific utilities for custom memory pool management."""

import logging
import os
from typing import Any, Optional, Tuple

import torch
Expand All @@ -26,6 +27,41 @@
SUPPORTED_MOONCAKE_CUSTOM_MEM_POOL_TYPES = ["NVLINK", "BAREX", "INTRA_NODE_NVLINK"]


def _cuda_expandable_segments_enabled() -> Optional[str]:
"""Return the allocator env var that enables CUDA expandable segments."""
for var in ("PYTORCH_CUDA_ALLOC_CONF", "PYTORCH_ALLOC_CONF"):
for field in os.environ.get(var, "").split(","):
key, _, value = field.partition(":")
if key.strip() == "expandable_segments" and value.strip().lower() == "true":
return var
return None


def _validate_efa_allocator_compatibility(
enable_custom_mem_pool: bool, custom_mem_pool_type: Optional[str]
) -> None:
"""Reject CUDA VMM allocators unsupported by the current libfabric EFA path."""
if envs.MOONCAKE_PROTOCOL.get().lower() != "efa":
return

if enable_custom_mem_pool:

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.

enable_custom_mem_pool is True for INTRA_NODE_NVLINK, but that type never allocates a VMM pool: init_mooncake_custom_mem_pool bails out at L58 with (False, None, None), and disaggregation/utils.py:355 puts aux metadata on cuda through the default allocator precisely because custom_mem_pool is None. Those buffers are cudaMalloc-backed, EFA handles them fine.

We recommend that mode for A100/H20/H100 in pd_disaggregation.mdx:170 paired with MC_INTRANODE_NVLINK=true, which is exactly the p4d/p5 shape: NVLink inside the node, EFA across. So this turns a working config into a startup failure, with a reason that isn't true for it.

Gate on the type instead:

Suggested change
if enable_custom_mem_pool:
if enable_custom_mem_pool and custom_mem_pool_type in ("NVLINK", "BAREX"):

and drop INTRA_NODE_NVLINK from the loop in test_efa_rejects_custom_memory_pools.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[by Codex] Addressed in f9b3372: EFA validation now rejects only NVLINK and BAREX, while INTRA_NODE_NVLINK remains allowed. The unit test and EFA documentation were updated accordingly.

raise ValueError(
f"SGLANG_MOONCAKE_CUSTOM_MEM_POOL={custom_mem_pool_type} is "
"incompatible with MOONCAKE_PROTOCOL=efa. Mooncake custom memory "
"pools use CUDA VMM allocations, which the current libfabric EFA "
"provider cannot transfer. Unset SGLANG_MOONCAKE_CUSTOM_MEM_POOL."
)

expandable_segments_var = _cuda_expandable_segments_enabled()
if expandable_segments_var is not None:
raise ValueError(
f"{expandable_segments_var} enables expandable_segments, which is "
"incompatible with MOONCAKE_PROTOCOL=efa because the current "
"libfabric EFA provider cannot transfer CUDA VMM allocations. "
"Disable expandable_segments."
)


def init_mooncake_custom_mem_pool(
device: str,
) -> Tuple[bool, Optional[Any], Optional[str]]:
Expand Down Expand Up @@ -109,4 +145,6 @@ def check_mooncake_custom_mem_pool_enabled() -> Tuple[bool, Optional[str]]:
enable_custom_mem_pool = False
custom_mem_pool_type = None

_validate_efa_allocator_compatibility(enable_custom_mem_pool, custom_mem_pool_type)

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.

This only reaches the decode side when staging is on. conn.py:256 sits inside the PREFILL branch, and decode gets here only via _init_staging_allocator -> init_staging_allocator (staging_handler.py:756), with SGLANG_DISAGG_STAGING_BUFFER defaulting to False. The other decode path, maybe_init_custom_mem_pool, is gated on SGLANG_MOONCAKE_CUSTOM_MEM_POOL being set, which is the var EFA users are told here not to set.

So a default decode worker with expandable_segments:True gets no check, and the failure stays quiet: register_buffer_to_engine ignores the return of batch_register, which swallows the exception and logs at debug (mooncake_transfer_engine.py:166-181). That's the opaque fi_read/fi_write path this PR is meant to remove, still live on half the deployment.

Call it from somewhere both roles hit (CommonKVManager.__init__, or engine init) rather than from check_mooncake_custom_mem_pool_enabled.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[by Codex] Addressed in f9b3372: allocator compatibility validation now runs at the start of MooncakeKVManager.init, which is shared by both prefill and decode roles, before engine initialization and buffer registration.


return enable_custom_mem_pool, custom_mem_pool_type
67 changes: 67 additions & 0 deletions test/registered/unit/disaggregation/test_mooncake_efa_allocator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import unittest
from unittest.mock import patch

from sglang.srt.disaggregation.mooncake.utils import (
check_mooncake_custom_mem_pool_enabled,
)
from sglang.srt.environ import envs
from sglang.test.ci.ci_register import register_cpu_ci

register_cpu_ci(est_time=1, suite="base-a-test-cpu")


class TestMooncakeEfaAllocator(unittest.TestCase):
def _check(self, protocol, custom_mem_pool=None, allocator_env=None):
allocator_env = allocator_env or {}
with (
patch.object(envs.MOONCAKE_PROTOCOL, "get", return_value=protocol),
patch.object(
envs.SGLANG_MOONCAKE_CUSTOM_MEM_POOL,
"get",
return_value=custom_mem_pool,
),
patch(
"sglang.srt.disaggregation.mooncake.utils.os.environ.get",
side_effect=lambda key, default="": allocator_env.get(key, default),
),
):
return check_mooncake_custom_mem_pool_enabled()

def test_efa_uses_default_allocator(self):
self.assertEqual(self._check("efa"), (False, None))

def test_efa_rejects_custom_memory_pools(self):
for pool_type in ("true", "NVLINK", "BAREX", "INTRA_NODE_NVLINK"):
with self.subTest(pool_type=pool_type):
with self.assertRaisesRegex(
ValueError, "incompatible with MOONCAKE_PROTOCOL=efa"
):
self._check("efa", custom_mem_pool=pool_type)

def test_efa_rejects_expandable_segments(self):
for var in ("PYTORCH_CUDA_ALLOC_CONF", "PYTORCH_ALLOC_CONF"):
with self.subTest(var=var):
with self.assertRaisesRegex(ValueError, var):
self._check(
"efa",
allocator_env={
var: "garbage_collection_threshold:0.8,"
"expandable_segments:True"
},
)

def test_efa_allows_disabled_expandable_segments(self):
self.assertEqual(
self._check(
"EFA",
allocator_env={"PYTORCH_CUDA_ALLOC_CONF": "expandable_segments:False"},
),
(False, None),
)

def test_non_efa_keeps_custom_memory_pool_behavior(self):
self.assertEqual(self._check("rdma", custom_mem_pool="true"), (True, "NVLINK"))


if __name__ == "__main__":
unittest.main()
Loading