Reduce CP shared-KV request-boundary stalls

CP shared KV now avoids the PyTorch sort/search remap for the single-request current-only path by deriving compact rows from page-level inverse mapping. The same change keeps sort NVTX attribution gated and splits high-frequency MoE sort markers behind a separate env var so profiling does not perturb normal runs.

Decode-side disaggregation prealloc also avoids rebuilding large token index tensors and records finer allocation timing, while compute-owner allocation/free tests cover the shared-KV page-lane behavior.

Constraint: The runtime tree used for validation is the remote /sgl-workspace/sglang-tai mount, which is not itself a Git repository, so these tracked files were synchronized into the local repo before commit.

Rejected: Keep torch.sort/searchsorted for current remap | it emits ATen/CCCL radixSortKVInPlace kernels in the attention hot path.

Rejected: Enable MoE sort NVTX under the generic sort env | the MoE preprocess sort is too frequent and can make profiling look like a hang.

Confidence: medium

Scope-risk: moderate

Directive: Do not reintroduce token-level torch.sort/searchsorted in CP shared-KV current remap without profiling the attention hot path under Nsight.

Tested: Remote container py_compile for modified runtime files; git diff --cached --check.

Not-tested: Full multi-node GLM5 PD throughput/profile rerun after the page-inverse current remap.
This commit is contained in:
laoyao0822
2026-05-05 05:18:35 +08:00
parent a638d71d53
commit 49eaf9ffde
11 changed files with 551 additions and 92 deletions
@@ -2,7 +2,7 @@
本文档定义 **Phase 3**:在 Phase 2 已经实现 CP shared/sharded persistent KV 的基础上,减少 shared KV compatibility path 引入的 **可避免重复 KV/index materialize**
Phase 3 的边界是:**复用当前 forward/chunk 中已经 CP all-gather + rerange 过的 current KV/index**,避免它们再次从 sharded persistent KV/index pool 里 materialize。后续拆分为:Phase 4 做 page-aligned CP token splitPhase 5 做 compute-owner shared KV layout更深层的 history KV shard-aware topk、selected KV exchange、distributed sparse attention 继续后移
Phase 3 的边界是:**复用当前 forward/chunk 中已经 CP all-gather + rerange 过的 current KV/index**,避免它们再次从 sharded persistent KV/index pool 里 materialize。更深层的 history KV shard-aware topk、selected KV exchange、distributed sparse attention 进入 Phase 4
---
@@ -91,7 +91,7 @@ history 仍走 Phase 2 compatibility materialize
### 1.3 非目标
以下不属于 Phase 3,进入 Phase 4/5 或后续:
以下不属于 Phase 3,进入 Phase 4 或后续:
```text
- shard-aware NSA topk
@@ -373,10 +373,13 @@ metadata.page_table_1
```text
SGLANG_CP_SHARED_KV_CURRENT_REUSE=0/1
SGLANG_CP_SHARED_KV_NVTX=0/1
SGLANG_DEBUG_SORT_NVTX=0/1
SGLANG_DEBUG_MOE_SORT_NVTX=0/1
```
建议初期默认关闭,验证稳定后再默认开启。
`SGLANG_CP_SHARED_KV_NVTX=1` 只打开 shared-KV materialize 相关 NVTX range
`SGLANG_DEBUG_SORT_NVTX=1` 只标记 CP shared KV current remap 与 KV allocator free-page merge 中可能触发 ATen sort kernel 的 `torch.sort` callsite。MoE EP preprocess sort 标记被拆到 `SGLANG_DEBUG_MOE_SORT_NVTX=1`,因为 MoE sort 调用频率很高,常规 profile 不应默认开启,避免 Nsight/CPU 侧被动态 NVTX range 拖慢。两个开关默认关闭,并在模块导入时缓存,避免热路径反复读取环境变量。
用于 Nsight Systems 定位 Phase2/Phase3 materialize、local copy、all-reduce 开销。
统计项:
@@ -486,7 +489,7 @@ history index materialize + current compact index
-> topk result maintains logical loc semantics
```
如果 topk loc semantic 不容易保证,允许保留 Phase 2 fallback,并把 mixed indexer 推迟到 Phase 5 之后的 shard-aware runtime 任务。
如果 topk loc semantic 不容易保证,允许保留 Phase 2 fallback,并把 mixed indexer 推迟到 Phase 4 前置任务。
---
@@ -615,23 +618,17 @@ MLA current-only + MLA mixed + NSA indexer current-only
---
## 9. Phase 4 / Phase 5 边界
## 9. Phase 4 边界
Phase 4 解决 CP split 与 KV page 不对齐的问题
Phase 4 继续解决 history/shared KV runtime 成本
```text
- page-aligned in-seq-split
- 保证一个 current KV page 只由一个 CP compute rank 计算
- 不改变 shared KV persistent layout
- history selected-page materialize
- shard-aware NSA topk
- local topk + global merge
- selected KV owner exchange
- distributed sparse attention
- owner-routed current write,消除写入前 current chunk all-gather 后再过滤 owner 的浪费
```
Phase 5 再解决 persistent KV layout 与 compute owner 不一致的问题:
```text
- compute-owner logical page allocation
- CP-local out_cache_loc
- local MLA KV / NSA index K direct persistent write
- 保持 materialize compatibility path 与 Mooncake PD transfer 正确
```
history selected-page materialize、shard-aware NSA topk、local topk + global merge、selected KV owner exchange、distributed sparse attention 继续后移。Phase 3 不隐藏这些问题。Phase 3 只保证:**已经 gather 过的 current chunk 不再被 shared KV compatibility path 重复 gather/materialize。**
Phase 3 不隐藏 Phase 4 问题。Phase 3 只保证:**已经 gather 过的 current chunk 不再被 shared KV compatibility path 重复 gather/materialize。**