Skip to content

feat: wire paged KV block budget to a --kv-cache-budget knob - #176

Merged
inureyes merged 1 commit into
mainfrom
feat/122-block-budget-activation
Jun 7, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/122-block-budget-activation

Conversation

@inureyes

@inureyes inureyes commented Jun 7, 2026

Copy link
Copy Markdown
Member

Summary

Sub-step b3 of #122 activates the paged KV cache block-budget admission gate (built dormant in b1/b2) by deriving and installing a budget at server startup. A new --kv-cache-budget serve knob (env MLXCEL_KV_CACHE_BUDGET) takes either a raw byte cap or the literal auto; absent, the pool stays unbounded so the default path is byte-identical to before.

mlxcel serve -m qwen3 --decode-storage-backend paged --kv-cache-budget auto
mlxcel serve -m qwen3 --decode-storage-backend paged --kv-cache-budget 8589934592   # 8 GiB

What changed

Testable core (execution::memory_estimate, +7 unit tests):

  • PagedBudgetDirective { Bytes(u64), Auto } + FromStr (auto or a raw byte count).
  • paged_block_bytes() — the real cost of one paged block (block_size tokens of one layer's K+V) from the architecture-aware KV geometry. Exact for the pure-attention Fp16 models that are pool-backed today.
  • auto_kv_budget_bytes() — inverts the estimator's fit inequality total = factor×(weights+kv) + activation ≤ available for the KV term: kv ≤ (available − activation)/factor − weights.
  • resolve_paged_block_budget() — directive → concrete block count.

Wiring — the directive threads through the standard config chain (mirroring --max-kv-size): ServeArgs / mlx_server → ServerStartupInput → ServerStartupConfig → ServerConfig → WorkerSchedulerConfig. It is resolved to a block count on the worker thread (where model_path and model.num_layers() are both known) and installed via BatchScheduler::with_paged_block_budget → CachePool::set_paged_block_budget. A budget that rounds below one block is downgraded to unbounded with a warning rather than wedging every request.

Scope / behavior

  • Opt-in, default-unchanged. No flag ⇒ None ⇒ unbounded pool ⇒ byte-identical to pre-b3.
  • The gate is only meaningful for pool-backed (Fp16, dense-natural-backend) sequences under --decode-storage-backend paged; model-owned and quantized families keep dense caches and ignore it (documented in --help).
  • The worker derivation is thin glue over the unit-tested core; BatchScheduler stays pub(crate) (same testing boundary as b2).

Test plan

  • cargo build (lib + both binaries, metal,accelerate) — clean
  • cold cargo clippy -D warnings — clean
  • cargo fmt --all --check — clean
  • cargo test — memory_estimate 29 (7 new), cli_input 93, startup 45, model_provider 26, serve 13, scheduler_prompt_cache 21, server::batch 181 — all green
  • Real binary: --kv-cache-budget auto parses; 8GiB rejected with a clear error; --help documents the knob

Sub-step c (GET /v1/cache/stats block fields + E2E admit-under-pressure / no-leak) remains to finish #122.

Part of #122.

Sub-step b3 of #122 activates the paged KV cache block-budget admission gate (built dormant in b1/b2) by deriving and installing a budget at server startup. The new --kv-cache-budget serve knob (env MLXCEL_KV_CACHE_BUDGET) takes either a raw byte cap or the literal `auto`; absent, the pool stays unbounded so the default path is byte-identical to before.

The testable core lives in execution::memory_estimate: PagedBudgetDirective{Bytes,Auto} with FromStr, paged_block_bytes() computes one block's real cost from the architecture-aware KV geometry (block_size × per-token-per-layer K+V), and auto_kv_budget_bytes() inverts the estimator's fit inequality total = factor×(weights+kv)+activation ≤ available to kv ≤ (available−activation)/factor − weights. resolve_paged_block_budget() turns a directive into a block count.

The directive threads through the standard config chain (ServeArgs / mlx_server → ServerStartupInput → ServerStartupConfig → ServerConfig → WorkerSchedulerConfig) and is resolved to a concrete block count on the worker thread, where model_path and model.num_layers() are both known, then installed via BatchScheduler::with_paged_block_budget → CachePool::set_paged_block_budget. A budget that rounds below one block is downgraded to unbounded with a warning rather than wedging every request.

The gate is only meaningful for pool-backed (Fp16, dense-natural-backend) sequences under --decode-storage-backend paged; model-owned and quantized families keep dense caches and ignore it. Adds 7 unit tests covering the resolution core. Sub-step c (GET /v1/cache/stats block fields + E2E) remains.

Part of #122.
@inureyes inureyes added the type:enhancement New features, capabilities, or significant additions label Jun 7, 2026
@inureyes
inureyes merged commit 8378d94 into main Jun 7, 2026
4 checks passed
@inureyes
inureyes deleted the feat/122-block-budget-activation branch June 7, 2026 14:42
@inureyes inureyes self-assigned this Jun 10, 2026
@inureyes inureyes added status:done Completed priority:medium Medium priority labels Jun 10, 2026
@inureyes inureyes added this to the 0.2 milestone Jun 21, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
)

Sub-step b3 of lablup#122 activates the paged KV cache block-budget admission gate (built dormant in b1/b2) by deriving and installing a budget at server startup. The new --kv-cache-budget serve knob (env MLXCEL_KV_CACHE_BUDGET) takes either a raw byte cap or the literal `auto`; absent, the pool stays unbounded so the default path is byte-identical to before.

The testable core lives in execution::memory_estimate: PagedBudgetDirective{Bytes,Auto} with FromStr, paged_block_bytes() computes one block's real cost from the architecture-aware KV geometry (block_size × per-token-per-layer K+V), and auto_kv_budget_bytes() inverts the estimator's fit inequality total = factor×(weights+kv)+activation ≤ available to kv ≤ (available−activation)/factor − weights. resolve_paged_block_budget() turns a directive into a block count.

The directive threads through the standard config chain (ServeArgs / mlx_server → ServerStartupInput → ServerStartupConfig → ServerConfig → WorkerSchedulerConfig) and is resolved to a concrete block count on the worker thread, where model_path and model.num_layers() are both known, then installed via BatchScheduler::with_paged_block_budget → CachePool::set_paged_block_budget. A budget that rounds below one block is downgraded to unbounded with a warning rather than wedging every request.

The gate is only meaningful for pool-backed (Fp16, dense-natural-backend) sequences under --decode-storage-backend paged; model-owned and quantized families keep dense caches and ignore it. Adds 7 unit tests covering the resolution core. Sub-step c (GET /v1/cache/stats block fields + E2E) remains.

Part of lablup#122.
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:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant