Repository navigation
feat(kv): Gemma 4 shared-row KV in the prompt cache and the KV budget (#645 P3) - #656
Merged
Merged
Conversation
…s KV budget (#645 P3) - PromptCache, hybrid snapshots and lookup treat KEqVRowKVCache as a whole-context layer like KVCacheSimple; the prompt-cache tree remembers an empty row template per layer and leases row layers back as rows. - The prompt-cache launch path builds rows when the plan asks for them, so namespaced requests keep and reuse rows too. - The disk prompt cache neither writes rows (the fork would reload them as plain KVCache) nor restores into a tree that holds rows. - KVCacheGeometry.sharingKEqVRows and InferenceEngine.sharedKVRowLayout: the orchestrator's KV budget counts 640 values per global KV head. - Tests: feature-matrix kvSharedRow (three turns, cached reuse, greedy equal to stock, reports on only for gemma4KEqV), tree round trip, disk skip, geometry bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012cicguZMN3jhNGmf5327oh
goodmai
added a commit
that referenced
this pull request
Nov 22, 2026
… (#659) OnPolicyRun sampled student rollouts at the teacher temperature from MLX's global random state, which parallel tests and other work advance, so the held-out set behind reverseKLBefore/After and every training batch changed from run to run; the OFT on-policy test failed klAfter < klBefore about once per few CI runs (#654, #656). Each rollout now samples inside withRandomState(seed:) with a seed hashed (FNV-1a) from the prompt ids, the round (0 = held-out, step + 1 = training) and the batch slot, as LabTrain already does. The OFT on-policy test passed 6 of 6 runs alone and the OFT + distill suites (29 tests) in parallel. Closes #658 Claude-Session: https://claude.ai/code/session_012cicguZMN3jhNGmf5327oh Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 23, 2026
Open
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.
What (#645 P3)
Follow-up to #655:
--experimental-kv-shared-rownow also covers requests with a prompt-cache namespace, and the server's KV budget counts the smaller rows.PromptCache.isSharable,isHybridand lookup's replayable-hybrid check treatKEqVRowKVCacheas a whole-context layer (likeKVCacheSimple).PromptCacheTreekeeps an empty row template per layer when it stores rows and leases those layers back as rows (state parts are no longer hard-coded to[keys, values]). The prompt-cache launch path builds row caches when the plan asks for them.KVCache, so a row would reload as a stock cache with the wrong state; restoring stock entries into a row tree would mix layouts. Both directions are refused, and the load log says so.KVCacheGeometry.sharingKEqVRows(_:)andInferenceEngine.sharedKVRowLayout; after a load, the orchestrator's KV planner uses 640 values per global KV head (Gemma 4 12B: 6,144 fewer bytes per token at fp16).Not here:
basalt fit/models checkdo not take the flag yet (MemoryEvaluator threads KV options through several entry points; a follow-up).Measured
mlx-community/gemma-4-26B-A4B-it-qat-4bit, release, flag on, REST:cache_key: p3cache_key: p3(reuses rows from the tree)Turn 2 from the leased rows and from a fresh row cache produce identical greedy text (96 tokens).
Tests
FeatureMatrixTestsgainskvSharedRow: on every tiny family, a plain first turn, a cached first turn and a second turn reusing it decode the same greedy text as stock; cached prompt tokens equal stock's; the feature reports on only forgemma4KEqV(engine applies the layout and the tree holds row templates).KEqVRowPromptCacheTests: tree store → lease returns row layers with the stored prefix and the same next-token logits (≤ 1e-5); the disk store refuses to write rows.KEqVRowLayoutTests.geometry: −6,144 bytes per token on a 12B-shaped config, sliding layers unchanged, proxy geometries untouched.Verified
swift build -Xswiftc -warnings-as-errorsand-c releaseclean; no new comments; no size limit exceeded; feature matrix, prompt-cache, hybrid-anchor, orchestrator, memory and K=V suites pass (193 tests).🤖 Generated with Claude Code
https://claude.ai/code/session_012cicguZMN3jhNGmf5327oh