Skip to content

feat(kv): Gemma 4 shared-row KV in the prompt cache and the KV budget (#645 P3) - #656

Merged
goodmai merged 1 commit into
mainfrom
feat/645-p3-prompt-cache
Nov 22, 2026
Merged

goodmai merged 1 commit into
mainfrom
feat/645-p3-prompt-cache

Conversation

@goodmai

@goodmai goodmai commented Nov 21, 2026

Copy link
Copy Markdown
Owner

What (#645 P3)

Follow-up to #655: --experimental-kv-shared-row now also covers requests with a prompt-cache namespace, and the server's KV budget counts the smaller rows.

  • Prompt cache: PromptCache.isSharable, isHybrid and lookup's replayable-hybrid check treat KEqVRowKVCache as a whole-context layer (like KVCacheSimple). PromptCacheTree keeps 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.
  • Disk prompt cache: skipped while rows are active. The fork serializes an unknown cache class as 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.
  • Budget: KVCacheGeometry.sharingKEqVRows(_:) and InferenceEngine.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 check do 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:

Request Prompt tokens Cached tokens Output
turn 1, cache_key: p3 7,341 0 —
turn 2, cache_key: p3 (reuses rows from the tree) 7,446 7,332 identical to the next row
turn 2, no cache key (fresh rows) 7,446 0 —

Turn 2 from the leased rows and from a fresh row cache produce identical greedy text (96 tokens).

Tests

  • FeatureMatrixTests gains kvSharedRow: 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 for gemma4KEqV (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-errors and -c release clean; 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

…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
goodmai merged commit 9cf27ac into main Nov 22, 2026
8 of 9 checks passed
@goodmai
goodmai deleted the feat/645-p3-prompt-cache branch November 22, 2026 06:51
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant