From 7fb85a9d8806acfe865d998cbcc1899ea7b739c9 Mon Sep 17 00:00:00 2001 From: "Wenfei (Charles) Qi" <74716511+Manfredss@users.noreply.github.com> Date: Tue, 29 Sep 2026 21:15:56 -0700 Subject: [PATCH] [MLX] Fix startup after logical token capacity change (#41314) --- .../hardware_backend/mlx/model_runner_stub.py | 7 ++++ .../mlx/test_mlx_runner_pool_contract.py | 37 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/python/sglang/srt/hardware_backend/mlx/model_runner_stub.py b/python/sglang/srt/hardware_backend/mlx/model_runner_stub.py index 7a47ebf8fa..185e98004e 100644 --- a/python/sglang/srt/hardware_backend/mlx/model_runner_stub.py +++ b/python/sglang/srt/hardware_backend/mlx/model_runner_stub.py @@ -107,6 +107,13 @@ class MlxModelRunnerStub(ModelRunner): # that path working instead of raising AttributeError. prefill_aware_swa = False + @property + def logical_max_total_num_tokens(self) -> int: + """MLX pool slots already count logical tokens; no DCP expansion applies.""" + return self.req_to_token_pool.schedulable_token_capacity( + self.max_total_num_tokens + ) + @property def preloaded_weights_bytes(self) -> int: """Return zero for the base Torch loader accounting hook. diff --git a/test/registered/unit/hardware_backend/mlx/test_mlx_runner_pool_contract.py b/test/registered/unit/hardware_backend/mlx/test_mlx_runner_pool_contract.py index f35f4effb4..6de3fac850 100644 --- a/test/registered/unit/hardware_backend/mlx/test_mlx_runner_pool_contract.py +++ b/test/registered/unit/hardware_backend/mlx/test_mlx_runner_pool_contract.py @@ -23,7 +23,9 @@ from __future__ import annotations import importlib.util import inspect import unittest +from types import SimpleNamespace +from sglang.srt.runtime_context import get_context from sglang.test.ci.ci_register import register_mlx_ci from sglang.test.test_utils import CustomTestCase @@ -34,6 +36,8 @@ _SKIP_REASON = "requires mlx" if _HAS_MLX: from sglang.srt.hardware_backend.mlx.model_runner_stub import MlxModelRunnerStub + from sglang.srt.managers.tp_worker import TpModelWorker + from sglang.srt.mem_cache.memory_pool import ReqToTokenPool from sglang.srt.model_executor.model_runner import ModelRunner @@ -41,6 +45,39 @@ if _HAS_MLX: class TestMlxRunnerPoolContract(CustomTestCase): """Guard the stub's scheduler-facing ``ModelRunner`` contracts.""" + def test_worker_startup_uses_mlx_token_capacity(self): + """MLX startup must report its own pool capacity without a Torch configurator.""" + runner = object.__new__(MlxModelRunnerStub) + runner.max_total_num_tokens = 64 + runner.max_running_requests = 2 + runner.is_hybrid_swa = False + runner.forward_stream = None + runner.req_to_token_pool = ReqToTokenPool( + size=2, + max_context_len=128, + device="cpu", + enable_memory_saver=False, + ) + runner.token_to_kv_pool = SimpleNamespace(size=64) + runner.token_to_kv_pool_allocator = SimpleNamespace() + worker = object.__new__(TpModelWorker) + worker._model_runner = runner + worker.model_runner_list = [runner] + worker.model_config = SimpleNamespace(context_len=128) + worker.dllm_algorithm = None + worker.random_seed = 0 + worker.device = "cpu" + + with get_context().override_server_args( + max_prefill_tokens=128, max_queued_requests=None + ): + worker.alloc_memory_pool() + info = worker.get_worker_info() + + self.assertEqual(info[0], 64) + self.assertEqual(info[4], 63) + self.assertEqual(info[5], 58) + def test_stub_reports_no_preloaded_torch_weights(self): runner = object.__new__(MlxModelRunnerStub) runner.pre_model_load_memory = 1.0