From 156d54e56cf29d6871912ee1efeec6db2d5255c0 Mon Sep 17 00:00:00 2001 From: LoCinder <61399298+LoCinder@users.noreply.github.com> Date: Sun, 11 Oct 2026 09:50:13 +0800 Subject: [PATCH] fix: wait for HiCache load-back before hybrid MLA KV reads (#38217) Co-authored-by: zhouzechun Co-authored-by: alphabetc1 <2508695655@qq.com> --- python/sglang/srt/mem_cache/memory_pool.py | 2 ++ .../unit/mem_cache/test_full_loc_fast_path.py | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/python/sglang/srt/mem_cache/memory_pool.py b/python/sglang/srt/mem_cache/memory_pool.py index 80f50cc56b..9025fedfa7 100644 --- a/python/sglang/srt/mem_cache/memory_pool.py +++ b/python/sglang/srt/mem_cache/memory_pool.py @@ -4502,6 +4502,8 @@ class HybridLinearKVPool(KVCache): dst_dtype: Optional[torch.dtype] = None, ): assert self.use_mla, "get_mla_kv_buffer called when use_mla is False" + # Wait for HiCache load-back here; the inner pool's counter is disabled. + self._wait_for_layer(layer.layer_id) # Read door -- same physical-id contract as the write door: `loc` is # a read-index tensor already translated at its production site # (fetch_mha_one_shot_kv_indices / prepare_chunked_kv_indices); the diff --git a/test/registered/unit/mem_cache/test_full_loc_fast_path.py b/test/registered/unit/mem_cache/test_full_loc_fast_path.py index 6efbda76e0..fd2ab2c95c 100644 --- a/test/registered/unit/mem_cache/test_full_loc_fast_path.py +++ b/test/registered/unit/mem_cache/test_full_loc_fast_path.py @@ -26,6 +26,7 @@ import unittest import torch from sglang.test.ci.ci_register import register_cpu_ci +from sglang.test.test_utils import CustomTestCase register_cpu_ci(est_time=10, suite="base-a-test-cpu") @@ -265,7 +266,7 @@ class _RecordingMLAPool(_RecordingPool): self.mla_set_calls.append(loc) -class TestHybridLinearMLARouting(unittest.TestCase): +class TestHybridLinearMLARouting(CustomTestCase): """MLA-side door contract of `HybridLinearKVPool`: `set_mla_kv_buffer` forwards `loc` untouched -- writes are physical since the ForwardBatch rebind.""" @@ -292,6 +293,23 @@ class TestHybridLinearMLARouting(unittest.TestCase): self.assertEqual(len(pool.full_kv_pool.mla_set_calls), 1) self.assertIs(pool.full_kv_pool.mla_set_calls[0], loc) + def test_get_mla_kv_buffer_waits_before_layer_remap(self): + pool = self._make_bare_pool() + pool.start_layer = 4 + pool.full_attention_layer_id_mapping = {6: 0} + calls = [] + pool.layer_transfer_counter = types.SimpleNamespace( + wait_until=lambda layer_id: calls.append(("wait", layer_id)) + ) + + def read(layer, loc, dst_dtype=None): + calls.append(("read", layer.layer_id)) + + pool.full_kv_pool.get_mla_kv_buffer = read + layer = types.SimpleNamespace(layer_id=6) + pool.get_mla_kv_buffer(layer, None) + self.assertEqual(calls, [("wait", 2), ("read", 0)]) + class TestMlaWriteDoorsUnderDcp(unittest.TestCase): """Which MLA write door is DCP-aware, and which refuses.