Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 5 additions & 8 deletions python/sglang/srt/elastic_ep/expert_backup_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
from sglang.srt.environ import envs
from sglang.srt.eplb.expert_location import get_global_expert_location_metadata
from sglang.srt.managers.io_struct import UpdateExpertBackupReq, sock_recv, sock_send
from sglang.srt.runtime_context import get_exec
from sglang.srt.server_args import ServerArgs
from sglang.srt.runtime_context import get_exec, get_parallel
from sglang.srt.utils.network import get_local_ip_auto

PORT_BASE = envs.SGLANG_BACKUP_PORT_BASE.get()
Expand All @@ -34,16 +33,14 @@ class ExpertBackupClient:
def __init__(
self,
*,
server_args: ServerArgs,
model_config,
moe_ep_size: int,
moe_ep_rank: int,
get_model: Callable[[], Any],
):
context = zmq.Context(2)
self.server_args = server_args
self.engine_num = server_args.nnodes
self.engine_rank = server_args.node_rank
self.engine_num = get_parallel().nnodes
self.engine_rank = get_parallel().node_rank
self.recv_list = [None] * self.engine_num
self.ready_sockets = [None] * self.engine_num
self._get_model = get_model
Expand All @@ -66,14 +63,14 @@ def __init__(
for i in range(self.engine_num):
self.recv_list[i] = context.socket(zmq.SUB)
self.recv_list[i].connect(
f"tcp://{all_ips[i * get_world_size() // server_args.nnodes]}:{PORT_BASE + i * 2 + 1}"
f"tcp://{all_ips[i * get_world_size() // get_parallel().nnodes]}:{PORT_BASE + i * 2 + 1}"
)
self.recv_list[i].setsockopt(zmq.SUBSCRIBE, b"")

# Synchronization channel to notify the manager when this client is ready.
self.ready_sockets[i] = context.socket(zmq.PUSH)
self.ready_sockets[i].connect(
f"tcp://{all_ips[i * get_world_size() // server_args.nnodes]}:{PORT_BASE + i * 2}"
f"tcp://{all_ips[i * get_world_size() // get_parallel().nnodes]}:{PORT_BASE + i * 2}"
)
sock_send(self.ready_sockets[i], UpdateExpertBackupReq())

Expand Down
41 changes: 12 additions & 29 deletions python/sglang/srt/eplb/expert_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import torch
import torch.distributed

from sglang.srt.arg_groups.overrides import should_report_expert_balancedness
from sglang.srt.environ import envs
from sglang.srt.model_executor.forward_batch_info import ForwardBatch
from sglang.srt.observability.metrics_collector import (
Expand All @@ -50,8 +49,9 @@
from sglang.srt.runtime_context import (
get_exec,
get_schedule,
logs_expert_balancedness_to_server_log,
reports_expert_balancedness,
)
from sglang.srt.server_args import ServerArgs
from sglang.srt.utils import Withable, get_device, get_int_env_var

if TYPE_CHECKING:
Expand Down Expand Up @@ -85,7 +85,6 @@ class ExpertDistributionRecorder(ABC):

@staticmethod
def init_new(
server_args: ServerArgs,
expert_location_metadata: ExpertLocationMetadata,
rank: int,
):
Expand All @@ -95,9 +94,7 @@ def init_new(
), "ExpertLocationMetadata is required for expert distribution recording. One possible"
"reason is that you are using a model that does not support expert distribution"
"recording. Try setting `get_model_config_for_expert_location` in your model."
return _ExpertDistributionRecorderReal(
server_args, expert_location_metadata, rank
)
return _ExpertDistributionRecorderReal(expert_location_metadata, rank)
else:
return _ExpertDistributionRecorderNoop()

Expand Down Expand Up @@ -160,27 +157,23 @@ class _ExpertDistributionRecorderNoop(ExpertDistributionRecorder):
class _ExpertDistributionRecorderReal(ExpertDistributionRecorder):
def __init__(
self,
server_args: ServerArgs,
expert_location_metadata: ExpertLocationMetadata,
rank: int,
):
self._server_args = server_args
self._expert_location_metadata = expert_location_metadata

self._recording = False
self._disable_all = False
self._current_forward_pass_id = Withable()
self._current_layer_idx = Withable()
self._current_debug_name = Withable()
self._accumulator = _Accumulator.init_new(
server_args, expert_location_metadata, rank
)
self._accumulator = _Accumulator.init_new(expert_location_metadata, rank)
self._single_pass_gatherers = {
k: _SinglePassGatherer.init_new(server_args, expert_location_metadata, rank)
k: _SinglePassGatherer.init_new(expert_location_metadata, rank)
for k in self._accumulator.get_single_pass_gatherer_keys()
}

if should_report_expert_balancedness(server_args):
if reports_expert_balancedness():
logger.info(
"ExpertDistributionRecorder auto start record since "
f"expert_balancedness_report_mode={get_exec().moe.expert_balancedness_report_mode}"
Expand Down Expand Up @@ -329,14 +322,11 @@ def set_global_expert_distribution_recorder(value):
class _SinglePassGatherer(ABC):
@staticmethod
def init_new(
server_args: ServerArgs,
expert_location_metadata: ExpertLocationMetadata,
rank: int,
) -> _SinglePassGatherer:
if get_exec().moe.expert_distribution_recorder_mode == "per_token":
return _DetailSinglePassGatherer(
server_args, expert_location_metadata, rank
)
return _DetailSinglePassGatherer(expert_location_metadata, rank)

if get_exec().moe.moe_a2a_backend == "mori":
return _DeepepLowLatencySinglePassGatherer(expert_location_metadata, rank)
Expand Down Expand Up @@ -403,7 +393,6 @@ class _DetailSinglePassGatherer(_SinglePassGatherer):

def __init__(
self,
server_args: ServerArgs,
expert_location_metadata: ExpertLocationMetadata,
rank: int,
):
Expand Down Expand Up @@ -668,11 +657,10 @@ def _convert_local_to_global_physical_count(
class _Accumulator(ABC):
@staticmethod
def init_new(
server_args: ServerArgs,
expert_location_metadata: ExpertLocationMetadata,
rank: int,
) -> _Accumulator:
return _Accumulator.get_class()(server_args, expert_location_metadata, rank)
return _Accumulator.get_class()(expert_location_metadata, rank)

@staticmethod
def get_class() -> Type[_Accumulator]:
Expand All @@ -685,11 +673,9 @@ def get_class() -> Type[_Accumulator]:

def __init__(
self,
server_args: ServerArgs,
expert_location_metadata: ExpertLocationMetadata,
rank: int,
):
self._server_args = server_args
self._expert_location_metadata = expert_location_metadata
self._rank = rank

Expand Down Expand Up @@ -719,15 +705,14 @@ class _UtilizationRateAccumulatorMixin(_Accumulator):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self._enable = should_report_expert_balancedness(self._server_args)
self._enable = reports_expert_balancedness()

if self._enable:
self.window_sizes = EPLB_BALANCEDNESS_WINDOW_SIZES
self._history = _DequeCollection(maxlens=self.window_sizes)
self._reset_server_log_history = True
self._rank = torch.distributed.get_rank()
expert_dispatch_cls = resolve_collector_class(
self._server_args,
STAT_LOGGER_ROLE_EXPERT_DISPATCH,
ExpertDispatchCollector,
)
Expand Down Expand Up @@ -777,12 +762,10 @@ def _append_utilization_rate(
compute_utilization_rate(gpu_physical_count)
)
should_track_history = not math.isclose(
self._server_args.eplb_min_rebalancing_utilization_threshold, 1.0
get_exec().moe.eplb_min_rebalancing_utilization_threshold, 1.0
)

should_log = (
self._server_args.should_log_expert_balancedness_to_server_log()
)
should_log = logs_expert_balancedness_to_server_log()
outputs["metrics"] = ExpertDistributionMetrics(
forward_pass_id=forward_pass_id,
eplb_balancedness=utilization_rate_gpu,
Expand Down Expand Up @@ -954,7 +937,7 @@ def dump(self, output_mode: _OutputMode):

def _get_global_average_utilization_rate(self):
if not self._enable or math.isclose(
self._server_args.eplb_min_rebalancing_utilization_threshold, 1.0
get_exec().moe.eplb_min_rebalancing_utilization_threshold, 1.0
):
return None

Expand Down
15 changes: 8 additions & 7 deletions python/sglang/srt/layers/moe/kt_ep_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from sglang.srt.layers.quantization.base_config import FusedMoEMethodBase
from sglang.srt.runtime_context import (
get_exec,
get_parallel,
get_schedule,
)
Expand Down Expand Up @@ -73,7 +74,7 @@ def create_kt_config_from_server_args(
Returns:
KTConfig if KT is configured, None otherwise
"""
if server_args.kt_weight_path is None:
if get_exec().moe.kt_weight_path is None:
return None

from sglang.srt.arg_groups.overrides import model_config_of
Expand All @@ -84,13 +85,13 @@ def create_kt_config_from_server_args(

return KTConfig(
layer_idx=layer_idx,
num_gpu_experts=server_args.kt_num_gpu_experts,
cpuinfer_threads=server_args.kt_cpuinfer,
threadpool_count=server_args.kt_threadpool_count,
weight_path=server_args.kt_weight_path,
num_gpu_experts=get_exec().moe.kt_num_gpu_experts,
cpuinfer_threads=get_exec().moe.kt_cpuinfer,
threadpool_count=get_exec().moe.kt_threadpool_count,
weight_path=get_exec().moe.kt_weight_path,
chunked_prefill_size=get_schedule().chunked_prefill_size,
method=server_args.kt_method,
max_deferred_experts_per_token=server_args.kt_max_deferred_experts_per_token,
method=get_exec().moe.kt_method,
max_deferred_experts_per_token=get_exec().moe.kt_max_deferred_experts_per_token,
num_layers=num_layers,
)

Expand Down
7 changes: 3 additions & 4 deletions python/sglang/srt/managers/prefill_delayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def __init__(
dp_size: int,
attn_tp_size: int,
cpu_group,
server_args,
max_delay_passes: int,
token_usage_low_watermark: Optional[float],
metrics_collector: Optional["SchedulerMetricsCollector"] = None,
Expand All @@ -91,14 +90,14 @@ def __init__(
self._debug_log_enabled = _DEBUG_LOG and debug_log_enabled
# Queue-based trigger is opt-in: activates only when queue_min_ratio
# is explicitly set. Additive with the slot-based trigger.
self._queue_min_ratio = server_args.prefill_delayer_queue_min_ratio
self._queue_min_ratio = get_schedule().prefill_delayer_queue_min_ratio
# Fall back to 5000ms if unset; this is a local safety cap, not a
# semantic default, so we don't surface it via ServerArgs.
self._max_delay_ms = server_args.prefill_delayer_max_delay_ms
self._max_delay_ms = get_schedule().prefill_delayer_max_delay_ms
if self._max_delay_ms is None:
self._max_delay_ms = 5000.0
self._queue_trigger_enabled = self._queue_min_ratio is not None
self._prefill_max_requests = server_args.prefill_max_requests
self._prefill_max_requests = get_schedule().prefill_max_requests
logger.info(
f"PrefillDelayer initialized with "
f"max_delay_passes={self._max_delay_passes} "
Expand Down
7 changes: 3 additions & 4 deletions python/sglang/srt/managers/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,6 @@ def init_schedule_policy(self):
attn_tp_size=self.ps.attn_tp_size,
cpu_group=self.tp_cpu_group,
device_group=self.tp_group.device_group,
server_args=self.server_args,
metrics_collector=(
self.metrics_collector
if self.metrics_reporter.enable_metrics
Expand Down Expand Up @@ -2820,7 +2819,7 @@ def _prefetch_kvcache(self, req: Req):
if self.enable_hicache_storage:
req.init_next_round_input(self.tree_cache, cow_mamba=False)
tree_cache = self.tree_cache
buffer_mode = self.server_args.hicache_host_memory_mode == "buffer_only"
buffer_mode = get_memory().hicache_host_memory_mode == "buffer_only"
last_host_node = req.last_host_node
# Buffer mode host-backups nothing, so match_prefix anchors at
# root; re-anchor on the deepest device node. The anchor is only
Expand Down Expand Up @@ -3544,7 +3543,7 @@ def _get_new_batch_prefill_raw(
req.init_next_round_input(self.tree_cache)
if (
self.enable_hicache_storage
and self.server_args.hicache_host_memory_mode == "buffer_only"
and get_memory().hicache_host_memory_mode == "buffer_only"
):
# Buffer mode: surface a staged prefetch as the request's host
# hit (consumed through init_load_back) plus its SWA window,
Expand Down Expand Up @@ -4452,7 +4451,7 @@ def is_fully_idle(self, for_health_check=False) -> bool:
if tc.enable_storage:
idle &= len(tc.ongoing_prefetch) == 0
idle &= len(tc.ongoing_backup) == 0
if self.server_args.hicache_host_memory_mode == "buffer_only":
if get_memory().hicache_host_memory_mode == "buffer_only":
# Queued writes, staged prefetches, and in-flight
# storage writes still hold host staging
# (buffer-mode unified tree only).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
compute_routing_key_stats,
)
from sglang.srt.runtime_context import (
exports_expert_balancedness_to_prometheus,
get_context,
get_disagg,
get_observability,
get_parallel,
get_spec,
logs_expert_balancedness_to_server_log,
)
from sglang.srt.utils.device_timer import DeviceTimer
from sglang.srt.utils.scheduler_status_logger import SchedulerStatusLogger
Expand Down Expand Up @@ -1009,9 +1011,7 @@ def log_batch_result_stats(
if (m := result.expert_distribution_metrics) is not None:
balancedness = m.eplb_balancedness.item()

if (
self.scheduler.server_args.should_log_expert_balancedness_to_server_log()
):
if logs_expert_balancedness_to_server_log():
if m.reset_server_log_history:
for history in self._eplb_balancedness_history:
history.clear()
Expand All @@ -1033,10 +1033,7 @@ def log_batch_result_stats(
f"gpu_physical_count_sum={gpu_physical_count_sum}"
)

if (
self.enable_metrics
and self.scheduler.server_args.should_export_expert_balancedness_to_prometheus()
):
if self.enable_metrics and exports_expert_balancedness_to_prometheus():
assert self.metrics_collector is not None
self.metrics_collector.increment_eplb_balancedness(
forward_mode=batch.forward_mode.name.lower(),
Expand Down
1 change: 0 additions & 1 deletion python/sglang/srt/managers/tokenizer_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,6 @@ def init_metric_collector_watchdog(self):
if get_observability().extra_metric_labels:
labels.update(get_observability().extra_metric_labels)
tokenizer_collector_cls = resolve_collector_class(
self.server_args,
STAT_LOGGER_ROLE_TOKENIZER,
TokenizerMetricsCollector,
)
Expand Down
4 changes: 0 additions & 4 deletions python/sglang/srt/mem_cache/base_prefix_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,10 @@ class BasePrefixCache(ABC, PrefixCacheTrait):
kv_events: Optional[KVCacheEventRecorder] = None

def init_metrics_collector(self):
from sglang.srt.runtime_context import get_server_args

server_args = get_server_args()
labels = {"cache_type": self.__class__.__name__}
if get_observability().extra_metric_labels:
labels.update(get_observability().extra_metric_labels)
radix_cache_cls = resolve_collector_class(
server_args,
STAT_LOGGER_ROLE_RADIX_CACHE,
RadixCacheMetricsCollector,
)
Expand Down
2 changes: 0 additions & 2 deletions python/sglang/srt/mem_cache/hiradix_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,8 @@ def _apply_storage_runtime_config(
labels.update(extra_metric_labels)
existing_collector = getattr(self, "storage_metrics_collector", None)
if existing_collector is None:
from sglang.srt.runtime_context import get_server_args

storage_cls = resolve_collector_class(
get_server_args(),
STAT_LOGGER_ROLE_STORAGE,
StorageMetricsCollector,
)
Expand Down
Loading
Loading