Ground CP shared KV collective choices in production-shaped evidence

Add a production-style CP shared KV collective benchmark and keep the page-aligned cache contract ledger current with the collective, unpack, multi-extend prefix, P2P, and CUDA IPC findings. The benchmark makes the final consumer layout explicit so dense all-reduce and owner-packed all-gather are compared after producing the same logical-dense result.\n\nConstraint: Prefixes may be assembled from multiple radix/cache extents, so single-run zigzag ordering is only a gated fast path.\nRejected: Treat rank-major all-gather output as the final product | consumers require logical-dense ordering.\nRejected: Switch index materialization based on noisy microbenchmarks | current evidence is not strong enough for production.\nConfidence: medium\nScope-risk: narrow\nDirective: Do not replace generic logical-dense materialization with zigzag ordered gather unless a run descriptor proves the prefix shape.\nTested: python -m py_compile benchmark/hicache/bench_cp_shared_kv_production_collective.py\nTested: Remote production-style benchmark logs under /mnt/beegfs/cjy/log/cp_shared_kv_collective_bench_*_20260531_*.log\nNot-tested: Production SGLang ETE with a CUDA IPC/P2P materialization path; this commit only adds benchmark/documentation surfaces.
This commit is contained in:
laoyao0822
2026-05-31 17:04:00 +08:00
parent 71872bb851
commit a149289554
2 changed files with 949 additions and 0 deletions
@@ -3613,3 +3613,279 @@ Verification:
`PYTHONPATH=python python -m pytest -q test/registered/unit/mem_cache/test_cp_hicache_metadata.py::TestHiCacheEvictLoggingLevels::test_evict_hot_path_success_logs_are_debug_only`
→ `1 passed`.
- Local and remote `py_compile` passed for touched Python files.
### C87 — 2026-05-31 all-gather owner-packed path must unpack to logical dense final product
Decision:
- Rank-major/owner-major packed pages are only a communication intermediate for
the proposed all-gather replacement of dense all-reduce.
- The consumer-facing final product remains a dense buffer in request
logical/page-table order. Dense slot `i` corresponds to
`logical_pages.reshape(-1)[i]`.
- The no-dummy final contract maps invalid/sentinel pages and invalid locs to
`-1`; page 0 is not used as a silent dummy destination in the new final
remap contract.
Implication for benchmarking:
- Measuring only `pack -> all_gather -> rank-major buffer` is incomplete and
overestimates the all-gather approach.
- The production-style benchmark must include the reorder/unpack cost:
`pack owner pages -> all_gather rank-major pages -> unpack to logical dense`.
- MLA remap must also be measured against the final no-dummy logical dense
layout, not against the rank-major intermediate.
Implemented TAI support:
- `build_logical_dense_page_inverse()` builds a no-dummy zero-based
logical-page-to-dense-slot inverse.
- `unpack_rank_major_pages_to_logical_dense()` copies the rank-major gathered
intermediate back to logical dense order and zero-fills invalid/sentinel slots.
Verification:
- RED before implementation: the two new TAI CUDA tests failed with missing
imports for `build_logical_dense_page_inverse` and
`unpack_rank_major_pages_to_logical_dense`.
- GREEN after implementation on g0034 container:
`test_build_logical_dense_page_inverse_remaps_without_dummy_page` and
`test_unpack_rank_major_pages_to_logical_dense_matches_logical_order_reference`
passed.
### C88 — 2026-05-31 owner-packed all-gather benchmark after logical-dense unpack
Remote benchmark environment:
- Host/container: g0034 / `sglang-glm5-dev-2`.
- Pattern: `--logical-pattern zigzag-owners`, 8 CP ranks, correctness checks enabled.
- Logs:
- `/mnt/beegfs/cjy/log/cp_shared_kv_collective_bench_20260531_154608.log`
- `/mnt/beegfs/cjy/log/cp_shared_kv_collective_bench_mla_focus_20260531_154720.log`
- `/mnt/beegfs/cjy/log/cp_shared_kv_collective_bench_index_focus_20260531_154804.log`
MLA focused run (`repeat=30`, p50 GPU ms):
| tokens | dense all-reduce | pack+all-gather+unpack | dense+remap | gather+unpack+remap |
| ---: | ---: | ---: | ---: | ---: |
| 4096 | 0.148 | 0.153 | 0.150 | 0.184 |
| 8192 | 0.167 | 0.151 | 0.169 | 0.182 |
| 16384 | 0.211 | 0.171 | 0.216 | 0.182 |
| 40320 | 0.330 | 0.275 | 0.334 | 0.284 |
MLA breakdown, avg GPU ms:
- rank-major metadata: ~0.059–0.060 ms.
- rank-major unpack to logical dense: ~0.056 ms up to 16k, ~0.070 ms at 40k.
- cached logical-dense loc remap: ~0.041 ms.
- pack+all-gather intermediate grows with bytes: ~0.132 ms at 4k, ~0.249 ms at 40k.
Conclusion for MLA:
- After including the required logical-dense unpack, owner-packed all-gather is
not a clear win for very short prefixes. At 4k it is roughly equal/slower,
especially when remap is included.
- It becomes beneficial around 16k+ in this microbenchmark and remains beneficial
at 40k.
- The fixed overheads (metadata, unpack, remap) are large enough that this path
should be gated by payload/size and should remain per-request, not per-layer.
Index focused run (`repeat=30`, p50 GPU ms):
| tokens | dense all-reduce | pack+all-gather+unpack |
| ---: | ---: | ---: |
| 4096 | 0.111 | 0.144 |
| 8192 | 0.110 | 0.142 |
| 16384 | 0.119 | 0.142 |
| 40320 | 0.125 | 0.140 |
Conclusion for index:
- The index payload is too small for owner-packed all-gather plus unpack to win
in this benchmark. Dense all-reduce p50 remains faster.
- Do not switch index prefetch/materialize collective to owner-packed all-gather
based on the current evidence.
### C89 — 2026-05-31 extended prefix sweep 4k–120k
Remote benchmark logs:
- MLA: `/mnt/beegfs/cjy/log/cp_shared_kv_collective_bench_mla_4k_120k_20260531_160744.log`
- Index: `/mnt/beegfs/cjy/log/cp_shared_kv_collective_bench_index_4k_120k_20260531_160839.log`
Configuration:
- 8 CP ranks, `--logical-pattern zigzag-owners`, `--warmup 8`, `--repeat 30`.
- Prefix tokens: `4096 8192 16384 32768 65536 98304 122880`.
- Final product for owner-packed path includes the required
rank-major-to-logical-dense unpack.
MLA p50 GPU ms:
| tokens | pages | dense all-reduce | gather+unpack | ratio | dense+remap | gather+unpack+remap | ratio |
| ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
| 4096 | 64 | 0.142 | 0.154 | 1.09 | 0.147 | 0.183 | 1.25 |
| 8192 | 128 | 0.166 | 0.151 | 0.91 | 0.170 | 0.182 | 1.07 |
| 16384 | 256 | 0.211 | 0.171 | 0.81 | 0.215 | 0.184 | 0.86 |
| 32768 | 512 | 0.288 | 0.243 | 0.84 | 0.290 | 0.250 | 0.86 |
| 65536 | 1024 | 0.446 | 0.378 | 0.85 | 0.450 | 0.385 | 0.85 |
| 98304 | 1536 | 0.607 | 0.519 | 0.86 | 0.611 | 0.525 | 0.86 |
| 122880 | 1920 | 0.726 | 0.627 | 0.86 | 0.729 | 0.633 | 0.87 |
MLA p50 breakdown:
- Metadata is flat at ~0.058 ms.
- Cached remap is flat at ~0.040 ms.
- Pack+all-gather grows with transferred bytes: 0.124 ms at 4k, 0.542 ms at 120k.
- Unpack grows with final dense bytes: 0.055 ms at 4k, 0.125 ms at 120k.
MLA conclusion:
- With final logical-dense unpack included, owner-packed all-gather is still not
worth it at 4k.
- It starts becoming useful around 8k for transfer-only, and around 16k once
remap is included.
- From 16k to 120k it is consistently about 13–19% faster by p50 in this
microbenchmark.
Index p50 GPU ms from this run:
| tokens | dense all-reduce | gather+unpack | ratio |
| ---: | ---: | ---: | ---: |
| 4096 | 0.196 | 0.230 | 1.18 |
| 8192 | 0.197 | 0.233 | 1.18 |
| 16384 | 0.204 | 0.228 | 1.12 |
| 32768 | 0.210 | 0.136 | 0.65 |
| 65536 | 0.138 | 0.138 | 1.00 |
| 98304 | 0.154 | 0.140 | 0.91 |
| 122880 | 0.176 | 0.156 | 0.88 |
Index caveat:
- Index timings are small and showed more run-to-run inconsistency than MLA.
- Keep the earlier conclusion conservative: do not switch index to
owner-packed all-gather based only on this microbenchmark until the production
critical path and warmup/order effects are isolated.
### C90 — 2026-05-31 multi-extend prefixes break single-zigzag ordered all-gather assumption
Finding:
- The two-pass ordered all-gather idea only directly applies when the requested
prefix is one contiguous in-seq CP split extent whose logical pages follow a
single zigzag owner run:
`0,1,...,cp_size-1,cp_size-1,...,1,0`.
- Production prefixes can be assembled from multiple historical extend/cache
extents through radix/HiCache reuse. The global logical prefix can therefore
look like a concatenation of several independently created zigzag runs, not one
single zigzag over the full prefix length.
- A global two-pass all-gather keyed only by CP rank would mis-order pages for
these multi-extent prefixes unless it also carries extent/run metadata and
performs the ordered gather per run, or falls back to the generic rank-major
gather plus logical-dense unpack.
Implication:
- Treat single-zigzag two-pass ordered all-gather as a fast path gated by an
explicit single-run/regular-run descriptor check.
- Keep the generic rank-major + unpack path as the correctness baseline for
multi-extend prefixes until segmented ordered-gather is implemented and
benchmarked.
### C91 — 2026-05-31 custom P2P allgatherv candidate to replace dense all-reduce
Finding:
- Dense all-reduce is the wrong semantic primitive for CP shared KV materialize:
each rank owns a sparse subset of pages, but all-reduce communicates the full
dense buffer and performs a reduction even though non-owner entries are zeros.
- Padded owner-packed all-gather removes the reduction and most zero traffic, but
still requires equal per-rank payload size (`max_owned_pages`) and an unpack to
logical dense order.
- Production prefixes may be composed from multiple extend/cache runs, so a
single ordered zigzag all-gather is only a gated fast path, not the generic
replacement.
Candidate generic replacement:
- Implement a custom P2P ring allgatherv for owner page payloads:
1. All ranks derive per-owner page counts and compact offsets from the same
`logical_pages`/layout metadata; no runtime size collective is needed in the
common deterministic case.
2. Each rank packs only its owned pages into a compact contiguous segment.
3. Ring P2P forwards variable-size segments for `cp_size - 1` steps using
`batch_isend_irecv`/NCCL P2P.
4. Receives write directly into a compact owner-concat buffer at known offsets.
5. Existing logical-dense unpack can consume a compact page inverse
(`logical_slot -> owner_offset + local_owner_ordinal`) instead of padded
`rank * max_owned_pages + ordinal`.
Risks / benchmark requirements:
- Naive direct send-to-all is not acceptable; it increases per-rank injection
versus ring all-gather. The P2P implementation must use a ring or another
bandwidth-efficient schedule.
- P2P has more launch/control overhead than one NCCL collective. It must be
benchmarked against both dense all-reduce and padded all-gather+unpack for
4k--120k prefixes and both MLA/index payloads.
- NCCL P2P is not guaranteed to be zero-SM; do not claim compute-overlap benefits
without profiling.
### C92 — 2026-05-31 CUDA IPC P2P ops are viable but should reuse existing SGLang IPC infrastructure
Finding:
- The repo already has CUDA IPC support in several places:
- `python/sglang/srt/distributed/device_communicators/cuda_wrapper.py`
exposes `cudaIpcGetMemHandle` / `cudaIpcOpenMemHandle`.
- `python/sglang/srt/distributed/device_communicators/custom_all_reduce.py`
and `custom_all_reduce_utils.py` already register/open IPC buffers and test
real P2P access.
- `python/sglang/jit_kernel/include/sgl_kernel/distributed/custom_all_reduce.cuh`
already caches opened IPC handles and maintains peer pointer tables.
- Therefore a CP shared KV CUDA-IPC transport should not start from scratch. It
should reuse the existing handle exchange, peer-access validation, and handle
lifetime patterns, then add a narrower owner-page copy/gather primitive.
Design direction:
- Build an intra-node CUDA IPC owner-page transport for CP ranks on the same host.
- Register long-lived staging/output buffers once per scheduler/rank, exchange IPC
handles during CP group initialization, and keep opened peer pointers cached.
- Launch a custom copy kernel that pulls remote owner page segments from peer
pointers into the local compact owner-concat or logical-dense destination.
- Keep NCCL/PyTorch collective fallback only for unsupported topology or failed
IPC/P2P validation, and log it explicitly.
Constraints:
- CUDA IPC is intra-node only. It does not replace RDMA or cross-node transfer.
- Correctness requires explicit stream/event ordering: a rank must not read a
peer staging segment before the peer pack kernel has completed.
- Lifetime must be explicit: registered peer buffers cannot be freed/reallocated
while another rank holds an IPC pointer.
- This path may still use SMs for the copy kernel unless implemented with copy
engine primitives; do not assume it is 0-SM without profiling.
### C93 — 2026-05-31 tai-kernel is the right boundary for CUDA IPC owner-page gather
Finding:
- `tai-kernel` already has a JIT C++/CUDA extension path for NSA prefill KV IO:
`python/tai_kernel/nsa_prefill/_kvcacheio_extension_loader.py` builds
`csrc/kvcacheio_ops.cc` and `csrc/kvcacheio_lf_pf.cu` with
`torch.utils.cpp_extension.load` and registers `tai_kernel_ops` via
`TORCH_LIBRARY_FRAGMENT`.
- CUDA IPC cannot be implemented in Triton alone because handle creation/opening,
peer pointer lifetime, and optional interprocess event handling require CUDA
runtime APIs. The right shape is a small C++/CUDA extension in `tai-kernel`
plus CUDA kernels for peer-pointer copy/gather.
Design implication:
- Add IPC primitives to the existing tai-kernel extension boundary rather than
adding another SGLang-local extension or relying on Python/ctypes in the hot
path.
- Expose handles as fixed-size uint8 tensors or keep opened peer pointers in a
C++ registry keyed by an integer transport id. Avoid Python object handles in
production hot paths.