Repository navigation
feat: wire paged KV block budget to a --kv-cache-budget knob - #176
Merged
Merged
Conversation
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.
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.
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
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-budgetserve knob (envMLXCEL_KV_CACHE_BUDGET) takes either a raw byte cap or the literalauto; absent, the pool stays unbounded so the default path is byte-identical to before.What changed
Testable core (
execution::memory_estimate, +7 unit tests):PagedBudgetDirective { Bytes(u64), Auto }+FromStr(autoor a raw byte count).paged_block_bytes()— the real cost of one paged block (block_sizetokens 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 inequalitytotal = factor×(weights+kv) + activation ≤ availablefor 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 (wheremodel_pathandmodel.num_layers()are both known) and installed viaBatchScheduler::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
None⇒ unbounded pool ⇒ byte-identical to pre-b3.--decode-storage-backend paged; model-owned and quantized families keep dense caches and ignore it (documented in--help).BatchSchedulerstayspub(crate)(same testing boundary as b2).Test plan
cargo build(lib + both binaries,metal,accelerate) — cleancargo clippy -D warnings— cleancargo fmt --all --check— cleancargo test—memory_estimate29 (7 new),cli_input93,startup45,model_provider26,serve13,scheduler_prompt_cache21,server::batch181 — all green--kv-cache-budget autoparses;8GiBrejected with a clear error;--helpdocuments the knobSub-step c (
GET /v1/cache/statsblock fields + E2E admit-under-pressure / no-leak) remains to finish #122.Part of #122.