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
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import unittest
|
|
|
|
import torch
|
|
|
|
from sglang.srt.layers.attention.nsa.transform_index import (
|
|
transform_index_page_table_decode_ref,
|
|
transform_index_page_table_prefill_ref,
|
|
)
|
|
from sglang.test.ci.ci_register import register_cpu_ci
|
|
|
|
register_cpu_ci(est_time=1, suite="stage-a-test-cpu")
|
|
|
|
|
|
class TestNSATransformIndex(unittest.TestCase):
|
|
def test_decode_ref_masks_high_out_of_bounds_topk_indices(self):
|
|
page_table = torch.tensor([[10, 11, 12], [20, 21, 22]], dtype=torch.int32)
|
|
topk_indices = torch.tensor([[0, 2, 3, -1], [1, 4, 0, -1]], dtype=torch.int64)
|
|
|
|
result = transform_index_page_table_decode_ref(page_table, topk_indices)
|
|
|
|
self.assertEqual(result.tolist(), [[10, 12, -1, -1], [21, -1, 20, -1]])
|
|
|
|
def test_prefill_ref_masks_high_out_of_bounds_topk_indices(self):
|
|
page_table = torch.tensor([[10, 11, 12], [20, 21, 22]], dtype=torch.int32)
|
|
topk_indices = torch.tensor(
|
|
[[0, 3, -1], [2, 1, 9], [1, 0, -1]], dtype=torch.int64
|
|
)
|
|
|
|
result = transform_index_page_table_prefill_ref(
|
|
page_table=page_table,
|
|
topk_indices=topk_indices,
|
|
extend_lens_cpu=[2, 1],
|
|
)
|
|
|
|
self.assertEqual(result.tolist(), [[10, -1, -1], [12, 11, -1], [21, 20, -1]])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|