Skip to content

perf: chunked slab storage for the paged pool - #237

Merged
inureyes merged 3 commits into
mainfrom
perf/issue-235-chunked-pool-slabs
Jun 12, 2026
Merged

inureyes merged 3 commits into
mainfrom
perf/issue-235-chunked-pool-slabs

Conversation

@inureyes

Copy link
Copy Markdown
Member

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.

  • Each layer now stores 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 pair, no ladder; the allocator high-water tracks live capacity.
  • A physical row maps to (slab, row-in-slab) via one helper. write_block, copy_on_write_block, and read_block_contents address the row's slab with the same slice/donation discipline; pool_tensor_bytes sums slabs.
  • gather_visible splits the block-row list into maximal same-slab runs and concatenates the per-run take results 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.
  • The gated fused kernel (MLXCEL_PAGED_ATTENTION_NATIVE, off by default) reads one contiguous buffer per side, so paged_decode_fused declines to the gather fallback on layers grown past one slab. Documented in ADR 0001 and turbo-kv-cache.md.
  • pool_grow_events now 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_burst harness scenario: 8 concurrent UNIQUE ~4.2k-token prompts)

peak footprint vs dense backend
before (main) 5013 MiB 1.52x
after (this PR) 3739 MiB 1.13x

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

Closes #235

inureyes added 3 commits June 12, 2026 17:46
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.
@inureyes
inureyes merged commit 4e17ef6 into main Jun 12, 2026
4 checks passed
@inureyes
inureyes deleted the perf/issue-235-chunked-pool-slabs branch June 12, 2026 09:01
@inureyes inureyes added this to the 0.2 milestone Jun 21, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added status:done Completed type:performance Performance improvements priority:medium Medium priority labels Sep 6, 2026
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:done Completed type:performance Performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf(core): chunked slab storage for the paged pool to eliminate growth reallocation entirely

1 participant