Repository navigation
feat(kv-int8): the model keeps one wired residency; --idle-release frees it - #359
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 119f8a146d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
48e9916 to
f4f001e
Compare
a1287cb to
65bdcb8
Compare
…ees it qwen3_5, qwen3_5_moe and qwen4_exp hold one MLX wired lease for the model's life instead of one per turn. The paged pool keeps its K/V buffers in an MTLResidencySet of its own, never attached to a queue. One process clock (Splash metal::Residency): the drive loop marks it before GPU work, a beat thread requests residency every 500 ms, and after --idle-release without a command it ends the lease and the pool sets and dispatches residency_kick. The addon sets MLX_METAL_RESIDENCY_REFRESH_INTERVAL_MS=500 when unset. mlx serve and mlx agent take --idle-release (default 10m, off = never; Splash parse_idle_release). After that time without a chat request the server unloads the chat model and dispose() frees its weights; the next request loads it again. POST /v1/memory/release does it now (200 / 409 engine_busy / 503 server_shutdown, Splash fieldless body rules). /health gains weights and work.releasePending. dispose() on Qwen35Model, Qwen35MoeModel and Qwen4ExpModel; setResidencyKeepAlive. Flash Next banks stay non-volatile between turns (Splash ExpertStore has no purgeable state and no timed purge). Deleted: the 14 per-turn WiredLimitContext sites of the three families, their wired_limit_bytes estimates and estimate_memory_bytes, scheduler_wired_bytes and ScheduledTurn::_wired_context; SLOT_RECLAIM_FALLBACK and the 30 s purge, bank suspend/activate, activation-refusal retry, slow-source detection, close_turn_if_idle, DIRECT_STAGING_BYTES; Qwen3_5Model::shutdown_for_test (now shutdown). Gaps: the release unloads the whole model (KV, prefix cache, sessions go too); restore is a full load; training drops the lease. Tests: 10 new Rust tests, 3 new and 8 changed TS test files; 18 named mutations, each red under its mutation and green without it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ounds the idle timer - The weights' release drains MLX's free pool (clearCache) after the disposals settle, still in the writer slot, with or without the 30 s sweeper: Splash MetalAllocation::detach hands the buffers back. - A /v1/responses persist that still retains a chat binding counts as busy, so a release answers 409 before it unregisters anything (Splash WeightResidency::release refuses unless the engine is idle). - The idle timer keeps an idleSince stamp and arms at most 2^31 - 1 ms at a time, re-arming until the deadline (Splash polls idleSince_), so a limit past ~24.8 days no longer fires after 1 ms. Co-Authored-By: Claude Opus 5.5 <[email protected]>
f4f001e to
7dad53b
Compare
Stage S5 of the Splash int8 KV alignment (stack #344). Base: S21 (#359). The paged pool gets an Int8 kind with per-row scales and a GPU store kernel that matches Splash `splash_store_kv_row`. Only tests select it. Every model still builds a bf16 pool (S6 adds the reads, S7 makes the pool follow `kv_format`). ``` per layer, Int8 pool (outside the MLX allocator, in the S21 pool residency set) K int8 [nb, H, D/16, bs, 16] (x=16 packed, as the bf16 pool) V int8 [nb, H, D, bs] (dimension-major) K scale f32 [nb, H, bs] V scale f32 [nb, H, bs] bytes / block = 2·L·H·bs·(D+4) 27B, bs16: 532,480 store (one threadgroup per row, D threads): scale = max|v|/127, q = clamp(rint(v·127/max), ±127) ``` ## Changes - `Int8 = 3` in `MetalDtype`, both C++ `KvDtype` enums (tied by a `static_assert`) and `KvDtypeC`. `is_fp8` stays false. Every C ABI entry range-checks the dtype byte. - `LayerKVPool`: 4 planes for Int8, none extra for bf16. `grow_to`, `shrink_to`, `move_blocks`, host IO and the residency set carry every plane. - Host IO and the cold tier: each side's blob is data followed by scales (Splash span order). The cold-tier format does not change. - New `cache/kv_int8_store.metal` (built with `-fno-fast-math`). `PagedKVWrite` always has 4 outputs; the scales come back in place. - Adapter: scale views cached per pool generation. Dtype checks compare against `io_dtype()` (bf16 for Int8). Every read entry refuses Int8 by name until S6. - Sizing: one formula in `profile::bytes_per_block`, `calculate_num_blocks` and `memory_mb_for_one_full_sequence`. A test ties it to S4's `KvLayout::page_bytes()`. ## Fast math The shipped store (safe math) matches the flat quantizer exactly: 0 of 196,608 q bytes, 0 of 768 scales. A `-ffast-math` build differs in 20 q bytes (16 of 768 rows). Each one is an exact .5 quotient, for example −63.5 → −63 instead of −64. ## Gaps vs Splash - K keeps the x=16 layout; Splash K is token-major. Blocks are 16 tokens; Splash pages are 32. No 64 KiB plane alignment. - A reused block is not cleared (Splash `KvPool::clearPage`). - Safe math here, fast math in Splash (the 20 q bytes above). ## Tests - 17 new tests and 16 named mutations, each red when on and green when off. - `cargo fmt --check`, `cargo clippy --all-targets -D warnings`, `yarn typecheck`: clean. - `mlx-paged-attn --lib` 209, `paged_int8_store` 2. mlx-core (`RUST_TEST_THREADS=1`): adapter 187, `paged_int8_tests` 3, memory_plan 32, qwen4_exp 231, gemma4 121, nemotron_h 94, qwen3_5 689, lfm2 76, qwen3 45, k2_horizon 20, qwen3_asr 21, `paged` 470: all pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <[email protected]>
Stage S21 of the Splash int8 KV alignment (stack #344). Base: S22 (#358).
The model holds one wired residency for its life, as Splash 1.6
metal::Residencydoes.--idle-releasefrees the weights after an idle time.Changes
HybridScheduler::drivecovers every lane, DFlash2 included. The 14 per-turnWiredLimitContextsites andscheduler_wired_bytesare deleted.mlx-paged-attn:residency.rs(one process clock, 500 ms beat, pool residency set) and theresidency_kickkernel.MLX_METAL_RESIDENCY_REFRESH_INTERVAL_MS=500when unset. New napi:setResidencyKeepAlive,dispose()on the three model classes.mlx serve/mlx agent:--idle-release./healthgainsweightsandwork.releasePending. The 30 s cache clear stays.ExpertStorehas no timed purge).SLOT_RECLAIM_FALLBACK, the 30 s purge, bank suspend/activate and slow-source detection are deleted.Gaps vs Splash
--allow-idle-sleepis not ported.Tests
cargo fmt --check,cargo clippy --all-targets -D warnings,yarn typecheck,vp lint,vp fmt --check: clean.RUST_TEST_THREADS=1): stream, engine::, qwen3_5, qwen4_exp, memory_plan, model_thread, cache_limit, grpo::engine, qwen3_asr, qwen3::model: all pass.mlx-paged-attn --lib: 200 pass.__test__/server,__test__/server/host, cli serve / agent-args / agent-update / launch-claude / delegate: 1725 pass.Not in this PR
--idle-release 2m: wired bytes stay up between turns, release and restore log lines,/health, 409 while streaming, and an A/B of the 500 ms refresh against 0.🤖 Generated with Claude Code