Expand prefill CP KV capacity by sharding persistent NSA KV
Prefill CP previously replicated NSA/MLA persistent KV on every CP rank, so CP8 consumed eight copies of KV memory while exposing only one rank of logical cache capacity. This change splits logical KV locs from per-rank physical storage, shards MLA latent KV and NSA index K/scale by deterministic page ownership, and keeps existing NSA attention kernels working through a full-view runtime materialization layer. Mooncake PD transfer now sends each prefill CP rank's owned physical pages with explicit logical page positions so non-CP decode can reconstruct full-layout KV. The implementation is guarded by an explicit server flag and startup checks, and the design documentation records the implemented scope, debug environment, and Phase 3 boundary. Constraint: Phase 2 must preserve existing NSA attention/index kernels via runtime full-view materialization Constraint: Decode side remains non-CP and receives full KV through Mooncake Rejected: Shard-aware NSA attention in this change | belongs to Phase 3 because it requires distributed topk/softmax/output contracts Rejected: Request-contiguous CP ownership | unstable under chunked prefill and tied to attention split mode Confidence: medium Scope-risk: broad Directive: Do not enable round-robin CP shared KV without wiring runtime materialization/PD transfer contracts for that split mode Directive: Keep SGLANG_DEBUG_CP_SHARED_KV disabled for perf measurements; it intentionally enables CUDA-syncing diagnostics Tested: Remote py_compile for shared-KV touched Python files in g0034 container Tested: Remote pytest selected cp_shared/shared_kv/nsa suite: 37 passed, 34 deselected Not-tested: Full GLM5 multi-node throughput/regression run after final doc update Not-tested: Phase 3 shard-aware runtime, round-robin CP mode, and non-Mooncake PD backends
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import unittest
|
||||
|
||||
import numpy as np
|
||||
|
||||
from sglang.srt.disaggregation.utils import (
|
||||
filter_kv_pages_for_cp_shared_kv,
|
||||
select_pages_by_request_positions,
|
||||
)
|
||||
from sglang.srt.mem_cache.cp_shared_kv_layout import CpSharedKVLayout
|
||||
from sglang.test.ci.ci_register import register_cpu_ci
|
||||
|
||||
register_cpu_ci(est_time=1, suite="stage-a-test-cpu")
|
||||
|
||||
|
||||
class TestCPSharedKVTransferMapping(unittest.TestCase):
|
||||
def test_filter_kv_pages_for_cp_shared_kv(self):
|
||||
layout = CpSharedKVLayout(page_size=64, cp_size=4, cp_rank=2)
|
||||
logical_pages = [5, 6, 7, 8, 9, 10, 11]
|
||||
src_pages, positions = filter_kv_pages_for_cp_shared_kv(
|
||||
layout=layout,
|
||||
logical_pages=logical_pages,
|
||||
chunk_page_start=100,
|
||||
)
|
||||
self.assertEqual(src_pages.tolist(), [2, 3])
|
||||
self.assertEqual(positions.tolist(), [102, 106])
|
||||
|
||||
def test_filter_kv_pages_for_cp_shared_kv_rejects_negative_pages(self):
|
||||
layout = CpSharedKVLayout(page_size=64, cp_size=4, cp_rank=2)
|
||||
|
||||
with self.assertRaisesRegex(
|
||||
ValueError,
|
||||
"CP shared KV transfer got negative logical_pages",
|
||||
):
|
||||
filter_kv_pages_for_cp_shared_kv(
|
||||
layout=layout,
|
||||
logical_pages=np.array([5, -1, 7], dtype=np.int32),
|
||||
chunk_page_start=100,
|
||||
)
|
||||
|
||||
def test_filter_nsa_state_pages_uses_physical_source_and_logical_destination(self):
|
||||
layout = CpSharedKVLayout(page_size=64, cp_size=4, cp_rank=2)
|
||||
logical_state_pages = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.int32)
|
||||
decode_state_pages = np.array([41, 42, 43, 44, 45, 46, 47, 48, 49], dtype=np.int32)
|
||||
|
||||
prefill_physical_pages, logical_positions = filter_kv_pages_for_cp_shared_kv(
|
||||
layout=layout,
|
||||
logical_pages=logical_state_pages,
|
||||
chunk_page_start=0,
|
||||
)
|
||||
selected_decode_pages = select_pages_by_request_positions(
|
||||
decode_state_pages,
|
||||
logical_positions,
|
||||
)
|
||||
|
||||
self.assertEqual(prefill_physical_pages.tolist(), [1, 2])
|
||||
self.assertEqual(logical_positions.tolist(), [2, 6])
|
||||
self.assertEqual(selected_decode_pages.tolist(), [43, 47])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user