Repository navigation
perf: chunked slab storage for the paged pool - #237
Merged
Merged
Conversation
A paged layer's physical storage was one growable tensor, so every capacity increase reallocated the whole slab (zeros + copy) and the freed old slabs formed a ladder of strictly increasing sizes in the MLX buffer cache that no later allocation could reuse. #224 bounded the transient, but successive pool-extending prefills (distinct long prompts) still paid one full-pool copy per growth and retained the ladder. The pool now stores each layer as a list of fixed-size slab tensors (POOL_SLAB_BLOCKS = 32 rows, the previous growth granularity). Growth appends fresh zeros slabs: no copy, no transient old+new pair, no ladder. A physical row maps to (slab, row_in_slab) via one helper; write_block / copy_on_write_block / read_block_contents address the row's slab, gather_visible splits the block-row list into maximal same-slab runs and concatenates the per-run takes (single-slab layers keep the one-take path), pool_tensor_bytes sums slabs, and the gated fused kernel declines to the gather fallback on multi-slab layers (documented in ADR 0001; it reads one contiguous buffer per side and is off by default). grow_events now counts append episodes; creation rounds capacity up to whole slabs as before. Measured (scripts/bench_memory_footprint.py, new distinct_burst scenario: 8 concurrent UNIQUE ~4.2k-token prompts, qwen3-0.6b, M1 Ultra): peak 5013 -> 3739 MiB (1.52x -> 1.13x of the dense backend on the same workload). Decode-heavy scenario walls within run noise (multiturn 13.3 vs 12.9 s, seqshare 14.6 vs 14.0, burst 16.0 vs 15.7) with identical cached-token counts. Gates: core 873 lib tests including a new fragmented-gather test (freed-row reuse crossing slab boundaries non-monotonically, byte-identical to the dense reference; growth pinned to exactly one appended slab per episode), server 3150, real-model parity 16/16 byte-identical across scheduler / prefix-share / real-model / handoff / serialize suites, cold clippy clean, fmt clean.
…follow-up) The 200-step fused parity test silently degraded into gather-vs-gather once the layer outgrew one slab (the pooled wrapper falls back without signal). Drive paged_decode_fused directly, assert it serves single-slab layers and declines past one slab, and compare parity only while live; the loop must now exercise both regimes. Also refresh the presize test comments that still described the retired reallocation growth, and add SAFETY and donation notes the review asked for.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
* perf: chunked slab storage for the paged pool A paged layer's physical storage was one growable tensor, so every capacity increase reallocated the whole slab (zeros + copy) and the freed old slabs formed a ladder of strictly increasing sizes in the MLX buffer cache that no later allocation could reuse. lablup#224 bounded the transient, but successive pool-extending prefills (distinct long prompts) still paid one full-pool copy per growth and retained the ladder. The pool now stores each layer as a list of fixed-size slab tensors (POOL_SLAB_BLOCKS = 32 rows, the previous growth granularity). Growth appends fresh zeros slabs: no copy, no transient old+new pair, no ladder. A physical row maps to (slab, row_in_slab) via one helper; write_block / copy_on_write_block / read_block_contents address the row's slab, gather_visible splits the block-row list into maximal same-slab runs and concatenates the per-run takes (single-slab layers keep the one-take path), pool_tensor_bytes sums slabs, and the gated fused kernel declines to the gather fallback on multi-slab layers (documented in ADR 0001; it reads one contiguous buffer per side and is off by default). grow_events now counts append episodes; creation rounds capacity up to whole slabs as before. Measured (scripts/bench_memory_footprint.py, new distinct_burst scenario: 8 concurrent UNIQUE ~4.2k-token prompts, qwen3-0.6b, M1 Ultra): peak 5013 -> 3739 MiB (1.52x -> 1.13x of the dense backend on the same workload). Decode-heavy scenario walls within run noise (multiturn 13.3 vs 12.9 s, seqshare 14.6 vs 14.0, burst 16.0 vs 15.7) with identical cached-token counts. Gates: core 873 lib tests including a new fragmented-gather test (freed-row reuse crossing slab boundaries non-monotonically, byte-identical to the dense reference; growth pinned to exactly one appended slab per episode), server 3150, real-model parity 16/16 byte-identical across scheduler / prefix-share / real-model / handoff / serialize suites, cold clippy clean, fmt clean. * test: pin the fused-kernel single-slab availability contract (review follow-up) The 200-step fused parity test silently degraded into gather-vs-gather once the layer outgrew one slab (the pooled wrapper falls back without signal). Drive paged_decode_fused directly, assert it serves single-slab layers and declines past one slab, and compare parity only while live; the loop must now exercise both regimes. Also refresh the presize test comments that still described the retired reallocation growth, and add SAFETY and donation notes the review asked for. * docs: note the donation contract on the slab write placeholder (review follow-up)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements #235. A paged layer's physical storage was one growable tensor, so every capacity increase reallocated the whole slab (zeros plus copy) and the freed old slabs formed a ladder of strictly increasing sizes in the MLX buffer cache that no later allocation could reuse. #224 bounded the transient to one layer's old+new pair, but successive pool-extending prefills (distinct long prompts, the workload cross-request sharing cannot help) still paid one full-pool copy per growth and retained the ladder.
POOL_SLAB_BLOCKS = 32rows, the previous growth granularity). Growth appends fresh zeros slabs: no copy, no transient pair, no ladder; the allocator high-water tracks live capacity.write_block,copy_on_write_block, andread_block_contentsaddress the row's slab with the same slice/donation discipline;pool_tensor_bytessums slabs.gather_visiblesplits the block-row list into maximal same-slab runs and concatenates the per-runtakeresults in block-table order; single-slab layers keep the existing one-take path. Rows are assigned mostly monotonically, so a sequence spans about one run per slab.MLXCEL_PAGED_ATTENTION_NATIVE, off by default) reads one contiguous buffer per side, sopaged_decode_fuseddeclines to the gather fallback on layers grown past one slab. Documented in ADR 0001 and turbo-kv-cache.md.pool_grow_eventsnow counts append episodes (no slab is ever copied); block ids, refcounts, free-row reuse, budgets, detach/adopt/clone, and the fix(server): prompt-cache ledger counts nominal paged bytes (32 B/block) so the capacity cap never triggers #226 real-bytes accounting are row-index concepts and stay unchanged.Measurements (M1 Ultra, qwen3-0.6b, new
distinct_burstharness scenario: 8 concurrent UNIQUE ~4.2k-token prompts)Acceptance target was within ~1.15x of dense. Decode-heavy scenario walls stay within run noise (multiturn 13.3 vs 12.9 s, seqshare 14.6 vs 14.0, shared burst 16.0 vs 15.7) with identical cached-token counts, so the run-split gather costs nothing measurable on the live path.
Validation
paged_scheduler_parity,paged_prefix_share_parity(7 cases incl. partial, clone-and-pin, unaligned whole-entry),paged_real_model_parity,paged_handoff_parity,paged_kv_serialize_parity.Closes #235