diff --git a/python/sglang/srt/hardware_backend/mlx/tp_worker.py b/python/sglang/srt/hardware_backend/mlx/tp_worker.py index bd166db15b..f81bfc0b1a 100644 --- a/python/sglang/srt/hardware_backend/mlx/tp_worker.py +++ b/python/sglang/srt/hardware_backend/mlx/tp_worker.py @@ -513,7 +513,7 @@ class MlxTpModelWorker(TpModelWorker): else: # "prefill" # The allocation wrote the matched prefix into the request's row. prefix_slot_ids = ( - self.req_to_token_pool.req_to_token[ + self._model_runner.req_to_token_pool.req_to_token[ req.kv.req_pool_idx, : req.prefix_len ].tolist() if req.prefix_len diff --git a/test/registered/unit/hardware_backend/mlx/test_tp_worker_routing.py b/test/registered/unit/hardware_backend/mlx/test_tp_worker_routing.py index 81e91d6bd1..7a802eebb5 100644 --- a/test/registered/unit/hardware_backend/mlx/test_tp_worker_routing.py +++ b/test/registered/unit/hardware_backend/mlx/test_tp_worker_routing.py @@ -64,6 +64,7 @@ class _FakeRunner: # (op, rid) -> needs_logits as received; guards the worker's # chunk-finality derivation reaching the runner intact. self.logits_flags: dict[tuple[str, str], bool] = {} + self.prefix_slot_ids: dict[str, list[int]] = {} self.removed_sync_kv: dict[str, bool] = {} self._req_caches: dict[str, list] = {} self._counter = 0 @@ -128,6 +129,7 @@ class _FakeRunner: self.calls.append(("prefill_start", req_id)) self.logits_flags[("prefill_start", req_id)] = needs_logits + self.prefix_slot_ids[req_id] = list(prefix_slot_ids) return SimpleNamespace( lazy_token=mx.array([0], dtype=mx.int32), cache=[self._fake_cache_layer()], @@ -342,6 +344,22 @@ class TestMlxExtendRouting(CustomTestCase): self.assertEqual(runner.ops_for("r1"), ["extend_start"]) self.assertIs(runner.logits_flags[("extend_start", "r1")], False) + def test_async_prefix_hit_reads_slots_from_runner_pool(self): + """A radix prefix hit takes its slots from the stub runner's pool: + the worker's own req_to_token_pool is None on MLX.""" + req_to_token = torch.zeros(2, 8, dtype=torch.int32) + req_to_token[1, :5] = torch.tensor([11, 12, 13, 14, 15]) + worker = self._worker(known_rids=set()) + worker.req_to_token_pool = None + worker._model_runner = SimpleNamespace( + req_to_token_pool=SimpleNamespace(req_to_token=req_to_token) + ) + req = _FakeReq("r1", req_pool_idx=1) + req.prefix_len = 3 + batch = _FakeBatch(ForwardMode.EXTEND, [req], [2]) + worker._async_extend_batch(batch) + self.assertEqual(worker._mlx_runner.prefix_slot_ids["r1"], [11, 12, 13]) + def test_async_genuine_mixed_decode_routes_to_decode(self): p, d = _FakeReq("p1"), _FakeReq("d1") runner, launch = self._run_async([p, d], [4, 1], {"d1"}, [d], ForwardMode.MIXED)