Skip to content

feat(hicache): keep one MLA KV host copy per node - #72

Merged
agiping merged 7 commits into
infrawaves/dev/v0.5.21from
hicache-mla-kv-remove-redundant-copies-one-per-node
Oct 23, 2026
Merged

agiping merged 7 commits into
infrawaves/dev/v0.5.21from
hicache-mla-kv-remove-redundant-copies-one-per-node

Conversation

@weiweisss

@weiweisss weiweisss commented Oct 16, 2026 •

Copy link
Copy Markdown

Why

MLA latent KV is identical on every rank of a TP group, yet HiCache pins a separate L2 (host DRAM) copy on each rank, and each rank reads it from L3 (Mooncake) on its own. On an N-GPU node, the same data is stored N times and read N times.

What

Flag Default Effect
--enable-mla-hicache-per-node off Keep one MLA KV host copy per node
--mla-hicache-per-node-mode {broadcast,shm} broadcast How followers restore MLA KV; only read when the flag above is on
tp_cache_group split by node
├── leader (first rank on the node): owns the host copy; the only rank that writes MLA L2 and reads L3
└── followers: never write MLA L2 or read MLA L3
      broadcast: leader H2D per layer (load stream, overlapped with forward),
                 then the layer-wait hook broadcasts each layer over NCCL on the forward stream
      shm:       every rank does its own H2D from the shared host buffer
  • Shared memory: the leader creates a memfd; followers reopen it via /proc/<pid>/fd/<fd>. Each rank populates only its own share, and no rank pins before all shares are populated. Broadcast followers drop the mapping afterwards.
  • Capacity: the host pool and host memory budget scale with the ranks per node; only the leader checks available memory.
  • Correctness: writing/loading acks use all_reduce(MIN), so the leader's completion decides.
  • Unchanged: KDA/Mamba side pools, L3 keys and L3 data layout, so existing L3 data stays valid. With the flag off, no new code path runs.
  • Rejected at startup: PP>1, PD decode, DCP>1, shm + write_back, Mooncake standalone_storage, stacks other than hybrid MLA + Mamba/KDA, non-CUDA or FP4 MLA pools. With the flag on, runtime L3 attach/detach and policy updates are rejected.
  • Reuses the broadcaster and allocator-only host pool from [HiCache] Add MLA host-dedup primitives sgl-project/sglang#36800 and the per-layer load hook pattern from [Perf] hicache: MLA/DSA host dedup with rotating layer owners (opt-in) sgl-project/sglang#42009.

Results

Production traffic replay, starting from a cold L3.

  • B300 (TP8 on one node, high L2 pressure): L2 capacity ×8. L3 hits become L2 hits and TTFT drops clearly. Both modes work.
  • GB300 (TP4 on one tray, low L2 pressure): L2 capacity ×4. MLA L3 read traffic drops by about 3/4; hit rate and TTFT match baseline. Broadcast is recommended (see the known limitations).

Merge Danger

Door: two-way. The flag is off by default and the L3 format is unchanged, so turning it off restores the current behavior.

Blast Radius: prefill instances with the flag on. Known limitations:

  • Cross-Grace cost on GB300: the shared host buffer spans both Grace CPUs, so both modes access remote memory.
    • broadcast: leader D2H bandwidth is about 0.8× baseline (asynchronous, TTFT unaffected). Leader H2D also crosses Grace, but the current load_back metric includes the broadcasts and cannot isolate it.
    • shm: with several ranks reading at once, H2D effective bandwidth is about 0.6× baseline with a longer tail.
    • At the tested load the end-to-end effect is not visible; it may show up when L2 reloads dominate or prefill is saturated. On B300, the PCIe limit hides this cost.
  • Broadcast reload cost is not yet measured: the leader H2D plus the per-layer broadcast that followers wait on still need a trace.

yifei she and others added 6 commits October 19, 2026 21:23
MLA latent KV is identical on every TP rank, but each rank kept its own
HiCache host copy and read it from L3 on its own, so a 4-GPU node held
and fetched 4 copies of the same data.

With --enable-mla-hicache-per-node, only the first rank on each node
(leader) keeps a real MLA host pool and loads from L3; the other local
ranks use the allocator-only host pool from sgl-project#36800. During the forward,
the per-layer HiCache wait hook (as in sgl-project#42009) broadcasts each loaded
MLA layer from the leader to the local GPUs over NCCL/NVLink, so the
broadcast overlaps with compute. KDA/Mamba state, L3 keys and L3 layout
are unchanged, so existing L3 data stays valid.

The leader's host pool and budget are sized for the whole node; all
ranks size the allocator the same so the radix tree stays consistent.
PD decode, DCP, FP4 MLA and single-rank nodes fall back to per-rank
copies with a warning. The flag is off by default.

Co-Authored-By: Claude <[email protected]>
Three bugs in the --enable-mla-hicache-per-node path.

1. Chunked-prefix prefill read prefix KV before the leader's broadcast

   A follower's device KV comes only from the leader's broadcast, and that
   broadcast is sent from the layer-wait hook (LayerDoneCounter.wait_until).
   The trtllm_mla eager prefill takes MHA_CHUNKED_KV and reads prefix KV
   through HybridLinearKVPool.get_mla_kv_buffer, which, unlike the other
   reads on that pool, did not wait. The inner MLA pool does not wait
   either: the hybrid pool clears its counter to None. So a follower read
   stale rows for that layer and gave wrong output with no error.

   get_mla_kv_buffer now waits on the model layer id before swapping in
   the compact id, as get_key_buffer does. This also fixes the same
   missing wait on H2D for the non-dedup path.

2. Explicit --hicache-ratio/--hicache-size could pin past host memory

   Followers build an allocator-only MLA pool with no memory check and
   go straight on to the Mamba host pool, whose check reads live free
   memory while the leader is still pinning. With an explicit size there
   is no budget snapshot, so an oversized config could fail while pinning
   (or be killed) instead of with "Not enough host memory available".

   Every rank now waits on a gloo barrier over the TP CPU group before
   the Mamba pool is built. Followers block until all leaders have
   pinned, so the check sees real free memory. Gloo, not NCCL, since
   pinning can outlast the NCCL watchdog. No new process group.

3. GB200/GB300: the leader's pool was sized for two Graces

   A tray has two Graces, each its own NUMA node, and the scheduler is
   membind-ed to its GPU's node. The leader's budget is the whole tray's
   free memory, but it can only pin from one Grace, so the default
   auto-sized pool asked for about twice what that node can give.

   auto_size_hicache halves the per-rank budget when the flag is on and
   is_mnnvl_fabric_device() is true; the MIN all-reduce keeps every
   rank's ratio equal. Limits: Mamba host capacity is halved too, and an
   explicit ratio/size is not covered.

Not run: no GPU available, only checked with py_compile.

Co-Authored-By: Claude <[email protected]>
The previous commit fixed GB200/GB300 by halving the auto-size budget.
That was wrong in two ways:

- auto_size_hicache overrides the global hicache_ratio, and the Mamba
  host pool reads the same value (MambaPoolHost in
  build_hybrid_mamba_stack), so Mamba host capacity was halved too.
- An explicit --hicache-ratio/--hicache-size never reaches
  auto_size_hicache, so the leader still pinned the whole tray's share
  on one Grace.

The real constraint is per NUMA node, not per host. With the same
ratio, the tray's total is unchanged by this feature; what changed is
that the leader, membind-ed to its GPU's node, pins every follower's
share there. A GB300 tray has two Graces, each its own node, so the
leader's node took twice its share.

Spread the leader's MLA host pool over the nodes its ranks are bound
to instead:

- build_for_node all-gathers each rank's NUMA node and cpuset-allowed
  nodes over the TP CPU group. The node comes from
  get_numa_node_if_available, the same query the launcher binds with.
- leader_host_shares derives the multiplier from that layout, not a
  constant. If the group spans several nodes, every rank is bound and
  the leader may allocate on all of them, the pool is interleaved over
  them and the multiplier is (fewest ranks on a node) x (node count):
  2 x 2 = 4 on a GB300 tray. Even interleave puts the same amount on
  every node, so this keeps each node at or below its own ranks'
  share. A group on one node, or with NUMA binding off, keeps the
  group size and is not interleaved. If the leader cannot reach every
  node, or binding is mixed, it falls back to the ranks on its own
  node. The minimum over all groups is used so every rank sizes its
  allocator the same.
- numa_utils.interleave_memory sets MPOL_INTERLEAVE on the calling
  thread for the leader's pool build only, then restores the previous
  policy, mode flags included. It must wrap the allocation: the HiCache
  mmap is pre-faulted, so mbind afterwards would not move the pages. It
  raises if the policy cannot be set, since the pool was sized for it.
- leader_budget_scope uses the same multiplier and enters the
  interleave scope.

With the same ratio, no NUMA node holds more host memory than without
the flag. Mamba is unchanged, explicit ratio/size and auto-sizing are
both covered, and KV cache capacity stays 4x on GB300. L3 reads stay
at one per tray.

Trade-off: the leader's H2D now reads half its pages from the other
node (Grace-Grace link on GB300, the socket link on dual-socket x86).
Dual-socket x86 hosts with binding on are interleaved too, with the
multiplier going from 4 to 8. Neither link's bandwidth was measured.
Not checked: whether the Mooncake/UMBP host allocators fault pages in
this process, which the policy needs.

The halving in hicache_auto_size.py is reverted.

Not run: no GPU available, only checked with py_compile; the multiplier
and nodemask logic were checked on GB300, single-NUMA, dual-socket,
uneven and restricted-cpuset layouts in isolation.

Co-Authored-By: Claude <[email protected]>
--enable-mla-hicache-per-node keeps one MLA KV HiCache host copy per node.
SGLANG_MLA_HICACHE_PER_NODE_MODE now selects how followers get the KV:

- broadcast (default): unchanged #72 behaviour. Followers keep an
  allocator-only host pool and the leader broadcasts each loaded layer
  over NCCL from the layer-wait hook.
- shm: the leader allocates the MLA host pool in a memfd (interleaved over
  the node's NUMA nodes by the existing leader_budget_scope) and sends the
  fd to same-host followers over an abstract Unix socket (SCM_RIGHTS).
  Every rank maps and pins the same pages and does its own H2D, so the
  forward path is the same as without the flag.

In both modes only the leader writes the shared copy: followers skip the
anchor D2H, use a page-get stub for L3 prefetch (the leader's count wins
through the existing all_reduce(MIN)), and do not register the buffer with
Mooncake. shm rejects --hicache-write-policy write_back at startup and on
attach-storage: write-back eviction syncs locally, so a follower could load
shared pages before the leader's D2H into them lands.

Also adds benchmark/hicache/bench_h2d_numa.py, which compares HiCache-style
page-gather H2D from one interleaved shared buffer against per-GPU private
local buffers, one process per GPU.

Not run: no GPU here; checked with py_compile, ruff and isort only.
Reject pipeline parallelism for per-node MLA HiCache and check mode agreement
on the WORLD CPU group before entering mode-specific collectives.

Close shared-buffer descriptors and bootstrap sockets on success and failure.
Derive the controller's follower role from the MLA host pool, skip unused
follower D2H staging, and avoid follower MLA existence queries while retaining
rank-sharded Mamba checks.

Add focused regression coverage, including cold/preloaded module identity
handling in the Mooncake fixtures.

Validation: Python compilation, formatting/import-order checks, no new Ruff
diagnostics, and isolated before/after source probes. Registered tests could
not collect because torch/msgspec are unavailable; GPU/distributed/Mooncake
integration remains untested.
Three gaps in the per-node MLA HiCache startup path:

- A follower waited for the leader's shared host buffer with no timeout,
  so a stuck leader hung it forever. The wait now uses --dist-timeout,
  or 120 minutes (the gloo group default) when it is unset, and raises
  a RuntimeError that points at the leader's log.
- NodeSharedHostTensorAllocator closes its sockets after the first
  exchange, so a second allocate() raised IndexError on a follower and
  returned an unshared buffer on the leader. It now rejects a second
  call explicitly.
- In broadcast mode, the one-rank-per-node early return leaked the NCCL
  group it had just created. Every rank takes that return together, so
  each now destroys its own handle before returning.

Adds unit tests for all three.

Not run: torch is unavailable locally; checked with py_compile only.

Co-Authored-By: Claude <[email protected]>
@luke396
luke396 force-pushed the hicache-mla-kv-remove-redundant-copies-one-per-node branch from 8d9126e to b9391c9 Compare October 20, 2026 07:06
@luke396
luke396 changed the base branch from infrawaves/v0.5.21 to infrawaves/dev/v0.5.21 October 20, 2026 07:11
Share one initialization path between broadcast and shm, replace the socket
handoff, fix L3 configuration at startup, and trim the feature to its
contracts.

- Both modes: the leader sizes a memfd without prefaulting it, followers
  reopen it via /proc/<pid>/fd/<fd>, each rank prefaults a contiguous
  page-aligned share with MADV_POPULATE_WRITE, and no rank registers before
  every share is populated. Broadcast followers then drop the mapping and
  keep dummy slot metadata. The leader's FD stays open until that fence;
  open and mapped-size failures abort every rank.
- Leave NUMA placement to deployment: remove the leader interleave, the NUMA
  benchmark and their helpers.
- With the flag on, reject runtime L3 attach/detach and policy updates.
  Reject at startup Mooncake standalone_storage, PD decode, DCP, stacks
  other than hybrid MLA + Mamba/KDA, and non-CUDA or FP4 MLA pools.
- Replace SGLANG_MLA_HICACHE_PER_NODE_MODE with
  --mla-hicache-per-node-mode {broadcast,shm}; drop the WORLD mode agreement.
- NodeSharedHostTensorAllocator takes this node's ranks and the cache group
  and derives its index, size and leader.
- Make host-pool parameters explicit and keep allocation fences in the
  assembler.
- Cover the hybrid MLA read waiting on its layer load.
- Trim tests to contract-level guards and comments to unstated contracts.
@luke396
luke396 force-pushed the hicache-mla-kv-remove-redundant-copies-one-per-node branch from 35adf0d to 2dc9858 Compare October 23, 2026 06:26
@agiping
agiping merged commit a98898f into infrawaves/dev/v0.5.21 Oct 23, 2026
Nozom1ff pushed a commit that referenced this pull request Oct 29, 2026
--enable-mla-hicache-per-node keeps one MLA KV HiCache host copy per node.
SGLANG_MLA_HICACHE_PER_NODE_MODE now selects how followers get the KV:

- broadcast (default): unchanged #72 behaviour. Followers keep an
  allocator-only host pool and the leader broadcasts each loaded layer
  over NCCL from the layer-wait hook.
- shm: the leader allocates the MLA host pool in a memfd (interleaved over
  the node's NUMA nodes by the existing leader_budget_scope) and sends the
  fd to same-host followers over an abstract Unix socket (SCM_RIGHTS).
  Every rank maps and pins the same pages and does its own H2D, so the
  forward path is the same as without the flag.

In both modes only the leader writes the shared copy: followers skip the
anchor D2H, use a page-get stub for L3 prefetch (the leader's count wins
through the existing all_reduce(MIN)), and do not register the buffer with
Mooncake. shm rejects --hicache-write-policy write_back at startup and on
attach-storage: write-back eviction syncs locally, so a follower could load
shared pages before the leader's D2H into them lands.

Also adds benchmark/hicache/bench_h2d_numa.py, which compares HiCache-style
page-gather H2D from one interleaved shared buffer against per-GPU private
local buffers, one process per GPU.

Not run: no GPU here; checked with py_compile, ruff and isort only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants