Enable batched CP shared-KV current reuse correctness

Batch-size > 1 cache-hit traffic must not lose the current/partial-current reuse fast path. This change extends the target MLA/index sync-correct path to validate batched current suffix rows, compose page-aligned prefix spans, and route batched index top-k through current-only or partial-current reuse without falling back to scalar guards.\n\nThe implementation keeps page as the minimum cache unit: prefix cache coverage is page-aligned, while current suffix rows are sliced by valid extend lengths so padded tail rows are not exposed to attention. The index top-k batch path now mirrors the single-request contract: current-only reuses current index KV directly, partial-current materializes once and shares the dense buffer across request segments.\n\nConstraint: CP shared-KV supports in-seq zigzag only; round-robin remains fail-fast for shared-KV.\nConstraint: No new collectives are introduced; this is a sync correctness path, not a new communication scheme.\nRejected: Keep bs>1 current_index_kv fail-fast | disables the cache-hit path that W4 is meant to restore.\nRejected: Pad requests to a common batch length | violates the page-granular contract and exposes padded tails to consumers.\nConfidence: medium\nScope-risk: moderate\nDirective: Do not reintroduce batch_size_not_one or batch_gt1 current-reuse guards without proving an equivalent batched fast path exists.\nTested: Local py_compile for touched runtime/test files.\nTested: Remote g0034 pytest test_nsa_cp_utils.py test_cp_shared_kv_runtime.py => 157 passed, 5 warnings, 2 subtests passed.\nTested: Remote g0034 pytest test_cp_shared_kv_layout.py => 27 passed, 3 warnings.\nNot-tested: Full ETE with live traffic; CUDA kernel-backed batched descriptors; L1 prefetch, HiCache load/backup, and draft/EAGLE bs>1 current reuse.
This commit is contained in:
laoyao0822
2026-06-03 07:06:21 +08:00
parent a7472c415f
commit 50d0008705
9 changed files with 1195 additions and 152 deletions
@@ -84,17 +84,18 @@ Implication: Phase 2 must add batch-aware owner plans and allocation. Legacy all
Implication: Phase 3 must split `out_cache_loc` per request, then concatenate this rank's local locs in the same local compute order.
### C7. Current/partial-current reuse is single-request
### C7. Current/partial-current reuse has been extended to target bs>1
- `python/sglang/srt/layers/attention/nsa/cp_shared_kv_runtime.py:1764-1904`
- `_current_extend_kv_reuse_miss_reason()` returns `batch_size_not_one`.
- `current_extend_kv_rows_for_reuse()` requires one extend length.
- `_current_extend_kv_reuse_miss_reason()` no longer rejects `batch_size > 1`; it validates batch size, prefix/extend/seq lengths, and `out_cache_loc`.
- `current_extend_kv_rows_for_reuse()` returns flattened valid current rows from `sum(extend_seq_lens_cpu)`, so padded tails are not exposed to attention.
- `python/sglang/srt/layers/attention/nsa/nsa_indexer.py:309-380`
- Index partial-current compose requires one positive page-aligned prefix.
- Index partial-current compose uses batch prefix slot spans for bs>1.
- Index top-k current-only bs>1 avoids materialize; partial-current bs>1 materializes once and reuses the dense buffer for all request segments.
- `python/sglang/srt/layers/attention/nsa_backend.py:1940-2020`
- MLA partial-current compose has the same one-prefix fail-fast contract.
- MLA partial-current compose uses batch prefix slot spans for bs>1.
Implication: Phase 4/5 must first keep sync full materialize correct, then add batched current/partial-current reuse.
Implication: target MLA/index sync-correct current-only and partial-current reuse are covered. Remaining work is kernel-backed variable-length descriptors, L1 prefetch, HiCache load/backup, and draft/EAGLE.
### C8. L1 prefix prefetch is single-request
@@ -187,19 +187,20 @@ target sync correctness
结论:Phase 3 必须以 request 为单位 split `out_cache_loc`,再拼接本 rank local loc。
### C7. current / partial-current reuse 是单请求结构
### C7. current / partial-current reuse 已从单请求结构扩展到 target bs>1
相关代码:
- `python/sglang/srt/layers/attention/nsa/cp_shared_kv_runtime.py:1764-1904`
- `_current_extend_kv_reuse_miss_reason()` 返回 `batch_size_not_one`;
- `current_extend_kv_rows_for_reuse()` 只接受一个 extend length。
- `_current_extend_kv_reuse_miss_reason()` 已移除 `batch_size_not_one`,改成 batch size、prefix/extend/seq len、`out_cache_loc` 的一致性校验;
- `current_extend_kv_rows_for_reuse()` 按 `sum(extend_seq_lens_cpu)` 返回 flattened valid current rows,避免 padded tail 暴露给 attention。
- `python/sglang/srt/layers/attention/nsa/nsa_indexer.py:309-380`
- index partial-current compose 要求单个 positive page-aligned prefix。
- index partial-current compose 对 bs>1 使用 batch prefix slot span;
- index top-k current-only bs>1 不 materialize,partial-current bs>1 只 materialize 一次并复用 dense buffer。
- `python/sglang/srt/layers/attention/nsa_backend.py:1940-2020`
- MLA partial-current compose 同样是单 prefix 合同。
- MLA partial-current compose 对 bs>1 使用 batch prefix slot span。
结论:Phase 5 需要做 batched current suffix slicing 和 remap。W4-1 可先只做 target index/top-k sync correctness。
结论:target MLA/index 的同步正确路径已支持 bs>1 current-only 与 partial-current。剩余工作是把这套合同下沉到 variable-length TAI/SGL kernel、L1 prefetch、HiCache load/back up、draft/EAGLE。
### C8. L1 shared-KV prefetch 是单请求结构
@@ -3,9 +3,9 @@
> 日期:2026-06-03
> 分支:`cjy-cp-refactor`
> 当前基线:`e4cf8d18b`
> 范围:W3 `local out_cache_loc + direct write`,W4-1 `target index/top-k sync correctness`。
> 范围:W3 `local out_cache_loc + direct write`,W4-1 `target index/top-k sync correctness`,W4-2 `target current/partial-current reuse`。
> **命名说明:** 本文的 **W4-1** 指顺序实现计划中的 **target index/top-k sync correctness**。并行派工文档/原定 W4 是 **current/partial-current reuse**;那部分在本文中仍视为后续阶段,不在本轮实现范围内。
> **命名说明:** 本文的 **W4-1** 指顺序实现计划中的 **target index/top-k sync correctness**。并行派工文档/原定 W4 是 **current/partial-current reuse**;本轮已继续补齐 target MLA/index 的 bs>1 current-only 与 partial-current 同步正确性,记为 **W4-2**。
## 0. 目标和非目标
@@ -14,12 +14,13 @@
1. bs>1 的 CP shared-KV direct write 使用每个 request 独立的 page-aligned split,再按 request order 拼接本 rank local rows。
2. MLA KV direct write 和 index KV direct write 共享同一个 ForwardBatch 级 page/local-loc plan。
3. target index/top-k sync path 使用 per-request metadata,不能把 batch flatten 成一条长序列。
4. W4-1 第一版只做 sync correctness,不启用 bs>1 current reuse、partial-current reuse、L1 prefetch、draft/EAGLE。
4. W4-1 第一版只做 sync correctness;W4-2 打开 target MLA/index 的 bs>1 current reuse、partial-current reuse。
5. L1 prefetch、draft/EAGLE、kernel-backed batched descriptor 仍不在本文实现范围内。
非目标:
- 不实现 W2 owner-lane allocator;W3 单测使用 synthetic owner-valid `out_cache_loc`。
- 不实现 bs>1 current/partial-current reuse。
- 不实现 L1 prefetch、draft/EAGLE 的 bs>1 current/partial-current reuse。
- 不实现 batched MLA/index prefetch。
- 不实现 draft/EAGLE bs>1。
- 不新增 collective 来同步 batch plan。
@@ -218,26 +219,34 @@ for req_id:
后续性能优化可以再把多个 segment 合并成 batched top-k descriptor。
### C10. 原定 W4 current/partial-current reuse 仍是单请求合同
### C10. W4-2 已将 target current/partial-current reuse 扩展到 bs>1
`_maybe_materialize_shared_index_buffer()` 在 `nsa_indexer.py:309-517`。
当 `current_index_kv is not None` 时,它要求:
```python
len(prefix_lens_cpu) == 1
positive page-aligned prefix
```
见 `nsa_indexer.py:341-355`。
因此 W4-1 第一版遇到 bs>1 + `current_index_kv is not None` 必须 fail-fast:
已修正的合同:
```text
[CP_SHARED_KV_FAIL_FAST][batch_gt1_index_current_reuse_unsupported]
current-only bs>1:
不 materialize prefix,直接把 current_index_kv 传给每个 request segment 的 top-k。
partial-current bs>1:
先用 batch prefix slot span 一次 materialize page-aligned prefix,
再 splice flattened valid current rows。
```
不要在 W4-1 里临时拼 current reuse,否则会和 W5 的 partial/current reuse 工作混在一起。
实现点:
- `cp_shared_kv_runtime.current_extend_kv_rows_for_reuse()` 不再拒绝 `batch_size > 1`,按 `sum(extend_seq_lens_cpu)` 返回 valid current rows。
- `cp_shared_kv_runtime.build_batch_prefix_slot_span()` 用 page-table row-major slot span 表达多个 request 的 prefix pages。
- `materialize_prefix_and_reuse_current_kv_page_slots()` / `materialize_prefix_and_reuse_current_index_page_slots()` 支持 `prefix_slot_span`。
- `nsa_backend.py` 的 MLA partial-current sync 对 bs>1 使用 batch prefix slot span。
- `nsa_indexer.py` 的 index partial-current sync 同样使用 batch prefix slot span。
- `_get_topk_in_seq_cp_pair_batch()` 不再对 `current_index_kv` fail-fast:current-only 直接透传,partial-current 先 materialize 后所有 segment 复用同一个 dense index buffer。
仍然不支持/未验证:
- 这只是同步正确路径,不代表底层 TAI/SGL variable-length batched kernel 已完成。
- L1 prefetch、draft/EAGLE 的 bs>1 current/partial-current reuse 尚未接入。
### C11. `_build_batch_metadata_from_plan()` 的 batch cu tensor 不能直接传给单 segment top-k
@@ -420,7 +429,7 @@ W4-1 依赖:
- W1 batch metadata;
- W3 local q/weights 已按 request boundary split;
- `metadata.get_page_table_64()` 可返回 batch page table;
- 不启用 bs>1 current reuse / partial-current reuse。
- W4-2 后允许 target bs>1 current reuse / partial-current reuse。
### 4.2 输出合同
@@ -449,19 +458,26 @@ if getattr(metadata, "batch_size", 1) > 1:
保留原 scalar path。
#### W4-1-S2:新增 `_get_topk_in_seq_cp_pair_batch()`
#### W4-1-S2/W4-2-S1:新增并扩展 `_get_topk_in_seq_cp_pair_batch()`
伪代码:
```python
plan = get_cp_shared_kv_batch_plan(forward_batch)
if current_index_kv is not None:
fail-fast batch_gt1_index_current_reuse_unsupported
shared_block_tables = metadata.get_page_table_64()
shared_index_buffer, shared_block_tables = self._maybe_materialize_shared_index_buffer(
forward_batch, layer_id, shared_block_tables
)
shared_index_buffer = None
shared_block_tables = None
current_index_kv_for_topk = current_index_kv
if current_index_kv is not None and not is_current_only_extend_batch(forward_batch):
current_index_kv_for_topk = None
shared_block_tables = metadata.get_page_table_64()
shared_index_buffer, shared_block_tables = self._maybe_materialize_shared_index_buffer(
forward_batch, layer_id, shared_block_tables, current_index_kv=current_index_kv
)
elif current_index_kv is None:
shared_block_tables = metadata.get_page_table_64()
shared_index_buffer, shared_block_tables = self._maybe_materialize_shared_index_buffer(
forward_batch, layer_id, shared_block_tables
)
cursor = 0
outputs = []
@@ -551,12 +567,24 @@ mock `_maybe_materialize_shared_index_buffer()`,断言 bs>1 top-k 只调用一
构造某个 request 的 `actual_seq_q_next=0`,断言 `_get_topk_ragged_with_cp()` 不被调用,输出中该 segment 是 empty。
#### T4:bs>1 current_index_kv fail-fast
#### T4:bs>1 partial-current index reuse 只 materialize 一次
传入 `current_index_kv`,断言:
```text
[CP_SHARED_KV_FAIL_FAST][batch_gt1_index_current_reuse_unsupported]
_maybe_materialize_shared_index_buffer(..., current_index_kv=...) 调用一次;
所有 request segment 的 top-k 复用同一个 shared_index_buffer/shared_block_tables;
segment 调用里 current_index_kv=None。
```
#### T5:bs>1 current-only index reuse 不 materialize
构造 `extend_prefix_lens_cpu=[0, 0]` 且 `seq_lens_cpu == extend_seq_lens_cpu`,传入 `current_index_kv`,断言:
```text
不读取 page table;
不调用 _maybe_materialize_shared_index_buffer;
每个 request segment 直接收到 current_index_kv。
```
#### T5:scalar tests 保持