Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions components/src/dynamo/sglang/capacity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,35 @@ def local_dp_rank_bounds(server_args: Any) -> tuple[int, int]:
return 0, 1


def publishes_kv_events(server_args: Any) -> bool:
"""Whether this node should advertise a KV-event source.

The router keys KV sources by ``(worker_id, dp_rank)``, and non-leader nodes
publish under the leader's worker ID so the router-visible trees stay keyed
to one logical worker. That only yields a unique key per node while DP
attention gives each node a distinct rank slice.

Without DP attention, ``local_dp_rank_bounds`` returns ``[0, 1)`` on every
node. Every node of a multinode gang would therefore advertise the same
``(leader_worker_id, 0)`` source. The frontend marks that key ambiguous and
never activates the direct-ZMQ ingress.

Only the leader owns the single logical rank in TP-only mode. SGLang emits
radix-cache events from the rank-0 scheduler, so non-leader sockets have
nothing distinct to contribute.
"""
dp_size = getattr(server_args, "dp_size", 1) or 1
enable_dp_attention = getattr(server_args, "enable_dp_attention", False)
nnodes = getattr(server_args, "nnodes", 1) or 1
node_rank = getattr(server_args, "node_rank", 0) or 0

# Mirrors the branch in local_dp_rank_bounds: per-node distinct slices.
if enable_dp_attention and dp_size > 1:
return True

return not (nnodes > 1 and node_rank > 0)


def model_card_dp_rank_bounds(server_args: Any) -> tuple[int, int]:
dp_size = getattr(server_args, "dp_size", 1) or 1
return 0, dp_size
Expand Down
15 changes: 13 additions & 2 deletions components/src/dynamo/sglang/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
from dynamo.runtime import Endpoint
from dynamo.sglang._disagg import SGLANG_WORKER_GROUP_ID_KEY, get_sglang_worker_group_id
from dynamo.sglang.args import Config
from dynamo.sglang.capacity import kv_metrics_block_values, local_dp_rank_bounds
from dynamo.sglang.capacity import (
kv_metrics_block_values,
local_dp_rank_bounds,
publishes_kv_events,
)


def get_local_dp_rank_range(server_args) -> range:
Expand Down Expand Up @@ -296,7 +300,14 @@ def init_kv_event_publish(self) -> List[KvEventPublisher]:
List of KvEventPublisher instances if KV event publishing is enabled,
empty list otherwise.
"""
if self.dynamo_args.use_kv_events:
if self.dynamo_args.use_kv_events and not publishes_kv_events(self.server_args):
Comment thread
rmccorm4 marked this conversation as resolved.
Resolved
logging.info(
"Non-leader node (node_rank=%s) shares the leader's single KV "
"rank slice; skipping KV event publishing so the router sees "
"exactly one source per (worker_id, dp_rank).",
getattr(self.server_args, "node_rank", 0) or 0,
)
elif self.dynamo_args.use_kv_events:
kv_events = json.loads(self.server_args.kv_events_config)
base_ep = kv_events.get("endpoint")
if not base_ep:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

import pytest

from dynamo.sglang.capacity import (
local_dp_rank_bounds,
model_card_dp_rank_bounds,
publishes_kv_events,
)

pytestmark = [
pytest.mark.unit,
pytest.mark.sglang,
Comment thread
rmccorm4 marked this conversation as resolved.
Resolved
Expand All @@ -22,8 +28,6 @@


def test_model_card_registration_keeps_global_dp_range():
from dynamo.sglang.capacity import model_card_dp_rank_bounds

server_args = SimpleNamespace(
dp_size=16,
enable_dp_attention=True,
Expand All @@ -32,3 +36,43 @@ def test_model_card_registration_keeps_global_dp_range():
)

assert model_card_dp_rank_bounds(server_args) == (0, 16)


def _args(**kwargs) -> SimpleNamespace:
base = dict(dp_size=1, enable_dp_attention=False, nnodes=1, node_rank=0)
base.update(kwargs)
return SimpleNamespace(**base)


def test_single_node_publishes_kv_events():
assert publishes_kv_events(_args()) is True


def test_multinode_without_dp_attention_publishes_only_from_leader():
Comment thread
rmccorm4 marked this conversation as resolved.
Resolved
"""TP-only multinode must advertise one source per logical worker."""
leader = _args(nnodes=2, node_rank=0)
follower = _args(nnodes=2, node_rank=1)

# Precondition for the collision this guards against.
assert local_dp_rank_bounds(leader) == local_dp_rank_bounds(follower) == (0, 1)

assert publishes_kv_events(leader) is True
assert publishes_kv_events(follower) is False


def test_dp_attention_publishes_from_every_node():
"""Each node owns a distinct slice when DP attention is enabled."""
nodes = [
_args(dp_size=4, enable_dp_attention=True, nnodes=2, node_rank=rank)
for rank in (0, 1)
]
assert local_dp_rank_bounds(nodes[0]) != local_dp_rank_bounds(nodes[1])
assert all(publishes_kv_events(node) is True for node in nodes)


def test_dp_size_one_with_dp_attention_still_leader_only():
"""DP size one keeps the shared [0, 1) slice even with the flag set."""
assert (
publishes_kv_events(_args(enable_dp_attention=True, nnodes=2, node_rank=1))
is False
)
35 changes: 30 additions & 5 deletions lib/runtime/src/transports/event_plane/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,11 @@ impl Stream for DeduplicatingStream {
}
}

/// Keep the shared wire, channel, and source publisher ID representable in Kubernetes metadata.
const PUBLISHER_ID_BITS: u32 = 53;

/// Keep publisher IDs exactly representable in float64-backed JSON metadata.
fn discovery_safe_publisher_id(random_id: u64) -> u64 {
random_id & (i64::MAX as u64)
random_id >> (u64::BITS - PUBLISHER_ID_BITS)
Comment thread
rmccorm4 marked this conversation as resolved.
Resolved
}

/// Event publisher for a specific topic.
Expand Down Expand Up @@ -923,9 +925,26 @@ mod tests {
use crate::config::environment_names::zmq_broker as broker_env;

#[test]
fn publisher_ids_fit_kubernetes_discovery_integer_range() {
assert_eq!(discovery_safe_publisher_id(42), 42);
assert!(i64::try_from(discovery_safe_publisher_id(u64::MAX)).is_ok());
fn publisher_ids_survive_a_json_number_round_trip() {
const MAX: u64 = 1 << PUBLISHER_ID_BITS;

// This historical full-range ID is not JSON-safe. It was observed
// rounding to 13584172880116488000 after a discovery round trip.
let unsafe_id: u64 = 13_584_172_880_116_487_724;
assert!(unsafe_id > MAX);
assert_ne!(unsafe_id as f64 as u64, unsafe_id);

for random_id in [0, 1, u64::MAX, unsafe_id, 6_633_287_539_119_378] {
let publisher_id = discovery_safe_publisher_id(random_id);
assert!(
publisher_id < MAX,
"publisher ID {publisher_id} must stay below 2^53"
);
assert_eq!(
publisher_id as f64 as u64, publisher_id,
"publisher ID {publisher_id} must survive an f64 round trip"
);
}
}

#[test]
Expand Down Expand Up @@ -1119,6 +1138,12 @@ mod tests {
publisher_b.publisher_id(),
])
);
for publisher_id in [publisher_a.publisher_id(), publisher_b.publisher_id()] {
assert!(
publisher_id < (1u64 << PUBLISHER_ID_BITS),
"publisher ID {publisher_id} must stay below 2^53"
);
}
};
tokio::time::timeout(std::time::Duration::from_secs(5), receive)
.await
Expand Down
Loading