Shrink the symm compose region to a compact current-page staging

Peers only ever read the CURRENT pages of a rank's compose output — the
prefix comes straight from the IPC-registered KV pool — so the symm
region does not need to hold the whole dense buffer (pool-bound ~2.5 GB
double-buffered slab).  It now holds one round of current pages in
merged-span order (extend-cap-bound, ~58-100 MB), and dense buffers
become purely rank-local (plain allocations or the optional local arena;
COMPOSE_SYMM no longer requires COMPOSE_ARENA).

Exchange per compose call: publish my written current pages
dense[page] -> staging[slot i] (slot = the page's batch current index,
identical on every rank, so peers address each other's staging with no
per-batch handshake), cp_symm_barrier, gather peers'
staging[writer][slot] -> dense[page] via the existing src!=dst page
gather.  Reuse safety keeps the parity-half distance-2 argument, now on
the staging.  Capacity sizing comes from the admission caps
(max_total_extend_tokens / max_batch_requests) with a pool-derived
fallback and the SYMM_HEAP_MB override; overflow fails fast
(batch-logical, hence rank-uniform).

Idea credit: laoyao0822's touched-pages-proportional staging
(906ecbe5d4), rebound onto our barrier-gated, group-agreed transport.

Validated on g0033 8xH200: 151 unit tests; 8-rank GPU byte-exactness
vs compose_v2 across 8 layers (arena on and off, parity halves
exercised); benchmark path e (real protocol) byte-exact, current-page
exchange 0.196 ms vs 0.354 ms compact-AR isolated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 21:50:34 +00:00
co-authored by Claude Fable 5
parent f74f0a557f
commit e8e5edb230
4 changed files with 422 additions and 188 deletions
@@ -6,7 +6,7 @@ every rank with real NCCL collectives and (for v2) real tai-kernel CUDA-IPC
gathers, and asserts the composed dense buffers and locs are byte-identical
between the legacy per-span path and compose_v2.
Run inside the g0034 cjy-glm5-new container:
Run inside the g0033 syh-dev-new container:
cd /mnt/beegfs/syh/sglang-stable && \
SGLANG_CP_SHARED_KV_USE_TAI_MATERIALIZE=1 \
PYTHONPATH=python:/mnt/beegfs/syh/tai-kernel/python \
@@ -267,38 +267,52 @@ def main() -> None:
flush=True,
)
# ---- Step B: symm exchange (arena + barrier + peer gather, zero NCCL
# in the current-page phase). Multiple layers exercise parity halves. ----
# ---- Step B: compact symm staging (publish + barrier + peer gather,
# zero NCCL in the current-page phase). Multiple layers exercise the
# staging parity halves; the arena is exercised in a second pass to
# prove the two knobs are independent. ----
def _check_symm(layer_id: int, tag: str) -> None:
symm_kv, symm_locs = _compose(
s, layer_id=layer_id, writers=s["current_page_writer_ranks"]
)
torch.cuda.synchronize()
assert torch.equal(ref_locs, symm_locs), (
f"rank{rank} layer{layer_id} [{tag}]: symm locs mismatch"
)
if not torch.equal(ref_kv, symm_kv):
diff = (ref_kv != symm_kv).any(dim=-1).any(dim=-1)
bad = torch.nonzero(diff).reshape(-1)[:8].cpu().tolist()
raise AssertionError(
f"rank{rank} layer{layer_id} [{tag}]: symm dense kv mismatch "
f"at rows {bad} (of {int(diff.sum())})"
)
with envs.SGLANG_CP_SHARED_KV_COMPOSE_V2.override(
True
), envs.SGLANG_CP_SHARED_KV_COMPOSE_ARENA.override(
True
), envs.SGLANG_CP_SHARED_KV_COMPOSE_SYMM.override(True):
for layer_id in range(4):
symm_kv, symm_locs = _compose(
s, layer_id=layer_id, writers=s["current_page_writer_ranks"]
)
torch.cuda.synchronize()
assert torch.equal(ref_locs, symm_locs), (
f"rank{rank} layer{layer_id}: symm locs mismatch"
)
if not torch.equal(ref_kv, symm_kv):
diff = (ref_kv != symm_kv).any(dim=-1).any(dim=-1)
bad = torch.nonzero(diff).reshape(-1)[:8].cpu().tolist()
raise AssertionError(
f"rank{rank} layer{layer_id}: symm dense kv mismatch at "
f"rows {bad} (of {int(diff.sum())})"
)
_check_symm(layer_id, "no-arena")
with envs.SGLANG_CP_SHARED_KV_COMPOSE_ARENA.override(True):
for layer_id in range(4, 8):
_check_symm(layer_id, "arena")
dist.barrier()
from sglang.srt.layers.attention.nsa.cp_shared_kv_compose import (
get_compose_arena,
get_compose_staging,
)
assert get_compose_arena(device).symm_ready, "symm slab was not registered"
staging = get_compose_staging(device)
assert staging.registered, "symm staging was not registered"
if rank == 0:
staged_mb = (
2
* staging.capacity_pages
* (staging.page_nbytes("token_kv") + staging.page_nbytes("index"))
/ (1 << 20)
)
print(
"PASS: symm compose byte-identical to v2 across 4 layers "
"(arena registered, barrier + peer gather engaged)",
"PASS: compact-staging symm compose byte-identical to v2 across "
f"8 layers (staging {staging.capacity_pages} pages ~{staged_mb:.1f} "
"MiB, publish + barrier + peer gather engaged; arena on and off)",
flush=True,
)
dist.destroy_process_group()