Skip to content

Provider: RAM-only prefix cache for memory_only (zero data retention) requests - #2573

Closed
jonathan308 wants to merge 14 commits into
feat/zdr-cache-policyfrom
feat/zdr-prefix-cache-ram
Closed

jonathan308 wants to merge 14 commits into
feat/zdr-cache-policyfrom
feat/zdr-prefix-cache-ram

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

A RAM-only prefix cache for memory_only (zero data retention) requests

Stacked on #2575 (the memory_only cache policy). With #2575 alone, a request whose sealed _darkbloom_cache carries cache_policy: memory_only never touches the SSD tier. On every complete-checkpoint model (gpt-oss-20b, Gemma 4, Qwen 3.5, Qwen 3.6, …) that means it prefills cold on every repeat, because these slots have no RAM prefix tier at all: the engine takes one complete-checkpoint store, and its recurrent RAM bank only runs without one.

This PR gives those requests a RAM-only tier with the SSD tier's lookup and restore semantics. The provider writes nothing to a file, the retained pages are wired so they never reach the swap file, and the tier is released before the Mac sleeps, so no hibernation image holds it.

Before / After (observable behaviour)

flowchart LR
  subgraph Before
    A1[memory_only request, repeated prefix] --> B1[SSD tier refused, cache off]
    B1 --> C1[cold prefill every time]
    D1[ordinary request] --> E1[encrypted SSD tier]
  end
  subgraph After
    A2[memory_only request, repeated prefix] --> B2[RAM-only tier: wired, TTL 30 min, wiped on pressure/sleep/load/unload/exit]
    B2 --> C2[restored from RAM: cached tokens reported, tier memory]
    D2[ordinary request] --> E2[encrypted SSD tier, unchanged]
  end
Loading

Before / After (code flow)

flowchart TD
  subgraph Before
    S1[EngineV2Bridge submission] -->|memory_only| X1[no SSD stage]
    G1[engine takeStaged / donate] --> H1[SSDHybridCheckpointStore]
    H1 -->|memory-only receipt| R1[refused, released unwritten]
  end
  subgraph After
    S2[EngineV2Bridge submission] -->|memory_only| T2[store.memoryOnlyTier.stage]
    G2[engine takeStaged / donate] --> H2[SSDHybridCheckpointStore]
    H2 -->|memory-only receipt| T3[MemoryOnlyCheckpointTier]
    H2 -->|ordinary receipt| F2[SSD files, unchanged]
    T3 --> P2[MemoryOnlyCheckpointPool: mlock'ed regions, budget, LRU, TTL]
    Q2[pressure / sleep / load gate / unload / shutdown: release all] --> P2
    Y2[refused charge the tier covers: release LRU entries, retry once] --> P2
  end
Loading

What changes

  • KVCacheRAM/ (new):
    • MemoryOnlyCheckpointTier: one slot's tier. A donation copies the engine's export (readSegment) and its encoded manifest into one wired region. A restore replays them through the engine's own import (planCompleteCheckpointImport → appendSegment → finish → takeStaged). It uses the same scope-salted chain tags (SSDLookupKeys, under a per-process key), position floor and identity/salt/exact-prefix checks as the SSD tier. It refuses any ordinary receipt (counted; stays 0).
    • MemoryOnlyCheckpointPool: the process-wide budget, LRU, TTL (the SSD tier's, sliding or DARKBLOOM_SSD_KV_TTL_MODE=fixed), release points and counters.
    • LockedHostRegion: mmap + MADV_ZERO_WIRED_PAGES + minherit(VM_INHERIT_NONE) + mlock. Release zeroes, unlocks and unmaps. If wiring fails, nothing is stored.
    • MemoryOnlyPrefixCachePolicy: the switch and budget.
  • Seams:
    • SSDHybridCheckpointStore forwards memory-only receipts to its memoryOnlyTier (takeStaged, donate, completeStaging, abandonStaging, close).
    • acceptsCheckpoint is unchanged, so ordinary captures are exactly as before.
    • The bridge's memory-only branch stages from the tier. Usage reports tier memory, with no chain anchors.
    • The pool is reached through GlobalKVCacheBudget.memoryOnlyPool (.shared in production), so both servers and the ledger use one pool and tests use private ones.
  • Attached by the generic slot factory to single-Mac complete-checkpoint slots (MemoryOnlyPrefixCachePolicy.attachesTier). It is not attached to GLM-5.3-Flash, DiffusionGemma, native MiMo, Nemotron Super (its builder passes allowsMemoryOnlyTier: false), pairs, or a slot built with DARKBLOOM_PREFIX_CACHE_MEMORY=1.
  • Budget: min(8 GiB, fleet KV headroom / 4), off below 1 GiB, recomputed at each load and unload.
    • Each entry is charged to the process ledger (reserveHostBuffers) and needs a whole budget of ledger headroom beside it.
    • When free system memory, not the cap, bounds the ledger's headroom, an entry's wired pages lower that too, so an entry can cost up to twice its size in admission headroom while held. To be measured on a real Mac.
    • Nothing comes out of any KV grant: today a slot's grant is all the headroom, so a carve would cost concurrency.
    • With a budget of 0 the tier isn't offered: marked requests run with their cache off and capture nothing. An export larger than the budget is refused before it is copied.
  • Release everything on:
    • an OS memory-pressure warning or critical (both servers);
    • system sleep: the pool registers for power notifications and releases before it lets the Mac sleep;
    • a model load that needs room, before any model is evicted for it, also with both reclaim rollbacks set (the tier is counted as reclaimable);
    • unload (store close, which also waits for a restore still filling an engine import) and shutdown (before the wait for native owners).
  • A refused memory charge that the tier's entries can cover releases the least recently used ones until the shortfall is covered and retries once, so an ordinary request is admitted on its first call. An engine's own refused charge releases the shortfall off the engine's admission lock; the engine retries at its next step. A charge that can't fit even without the tier leaves it held.
  • A re-donation of a prefix already held copies nothing (counted duplicate), so under DARKBLOOM_SSD_KV_TTL_MODE=fixed an entry still expires 30 minutes after its first store.
  • Also: on a slot with the DARKBLOOM_PREFIX_CACHE_MEMORY=1 resident tiers, which have no TTL, marked requests run with the prefix cache off. They skip the prompt-token piece cache as in Provider: a memory_only cache policy per request, and SSD cache records without token IDs #2575.
  • Default change for every request: the prompt-token piece cache gets a sliding 30-minute TTL. PromptPieceTokenStore drops a piece unused for DARKBLOOM_TTFT_PROMPT_CACHE_TTL_SECONDS (default 1800, lower only).
    • Lookups stop returning it at once, and a sweep at least once a minute removes it, also on an idle Mac.
    • 0 restores the earlier behaviour, where a piece stayed until displaced, the model unloaded or the process exited. That is the kill switch.
    • The setting is forwarded by the LaunchAgent.
  • Non-cache memory residue is documented. MLX's freed-buffer pool, the paged pools' freed KV pages and retained free segments, and freed Swift heap buffers keep stale bytes of finished requests until they are reused or returned, but no lookup reaches them (docs/architecture/prefix-cache.md).
  • Settings (both forwarded by the LaunchAgent; documented in docs/reference/configuration.md):
    • DARKBLOOM_ZDR_PREFIX_CACHE_RAM=auto|off, default auto. The tier holds nothing until a marked request donates, and the coordinator's marking is off by default; that marking is the real gate. off is the kill switch.
    • DARKBLOOM_ZDR_PREFIX_CACHE_RAM_MAX_MB replaces the cap.
  • Metrics: darkbloom_zdr_prefix_ram_* on local /metrics (lookups, hit tokens, stores by outcome including duplicate and refused_busy, evictions by reason including sleep and shortfall, bytes, entries, budget). Counts only.

Sizing (one model resident, default 4 GiB reserve; from UnifiedMemoryCap.kvBudgetBytes)

RAM gpt-oss-20b gemma-4-26b-qat-4bit Qwen 3.6 35B-A3B Qwen3.5-9B
16 GB — — — —
24 GB 1.3 GiB — — 2.2 GiB
32 GB 3.3 GiB 2.3 GiB — 4.2 GiB
48 GB 7.1 GiB 6.1 GiB 4.5 GiB 8 GiB
64–512 GB 8 GiB 8 GiB 8 GiB 8 GiB

"—" means the model doesn't load at that size, or the budget is under 1 GiB. 8 GiB holds about 42 / 22 / 30 / 22 native-layout checkpoints of an 8,192-token prefix.

What is and isn't wired

  • The tier's retained bytes are wired (mlock): every payload segment and the manifest, which carries the prefix token IDs and the scope. The heap holds only HMAC tags, sizes and times.

  • Data of a request in flight is ordinary process memory, as for every request today. That covers:

    • its live KV in MLX buffers, which the slot does not wire;
    • each ≤ 4 MiB segment the engine hands over while it is copied in;
    • the short-lived JSON encode and decode buffers of an entry's manifest;
    • a restored manifest (with the prefix token IDs) until the engine takes or drops the staged checkpoint.

    Under heavy pressure macOS could page it to its swap file, which is always encrypted with a key that is never stored, and a Mac that hibernates writes it to its sleep image. Wiring the KV pool is a possible later step.

One documented exception, and a proposal

The operator opt-in DARKBLOOM_PREFIX_CACHE_MEMORY=1 (off by default, forwarded by the LaunchAgent) enables the engine-resident tiers: paged resident blocks and the recurrent bank, and Nemotron Super's bank instead of its SSD checkpoints. Those tiers have no TTL and no API to clear them.

  • memory_only requests never use them: on such a slot they run with the prefix cache off.
  • Ordinary requests on such a Mac keep entries until eviction, unload or exit.
  • That is the one prompt-derived cache in the provider that does not expire 30 minutes after its last use.

Proposal, not implemented here: release builds ignore DARKBLOOM_PREFIX_CACHE_MEMORY and fail closed to the SSD and RAM-only tiers, so the 30-minute bound holds on every provider. Debug and local qualification builds keep the opt-in. To be decided by the release owners at merge.

Pairs and clusters

Unchanged from #2575: a pair serves a memory_only request with its prefix cache off. A resident-only pair partition is a later step.

Validation

  • Tests (provider-swift/Tests/ProviderCoreTests/KVCacheRAM/, plus 4 TTL cases in PromptTokenCacheTests):
    • the tier on a real complete-checkpoint codec and admission: store and restore, ordinary receipts refused, scope separation, no file, LRU, budget, TTL (sliding and fixed), duplicates, the headroom guard, the ledger charge, a full queue, wiring refused, a release during a copy, close waiting for a restore, abandon then cold retry, availability;
    • the release points in both servers (pressure, load, shutdown) and a refused charge the tier covers, each on a private pool;
    • the attach rules, the submission policy and the per-chip budgets;
    • end to end through handleInferenceRequest on a real EngineV2 with the paged backend: the second memory_only request reports 512 cached tokens from tier memory, the SSD store writes, stages and keeps nothing, no SSD-tier evidence is sent, and an ordinary request after them gets nothing from RAM.
  • Red control: Red control for #2573: tests without the change (do not merge) #2577 runs these tests on Provider: a memory_only cache policy per request, and SSD cache records without token IDs #2575 (6009e53) with test-only adapters and no production change. With the final set (run 45146664913) they failed, and only they did: 33 test functions, all in these five suites, in two shards; every other suite passed. The first set (run 45139205005) failed the same way.
  • CI: CI (dev) run 45290548786 on 0ef9c33 (after the merge of Provider: a memory_only cache policy per request, and SSD cache records without token IDs #2575 at d7510a0): all 18 jobs green; the five suites above ran and passed, and so did Provider: a memory_only cache policy per request, and SSD cache records without token IDs #2575's new SSD suites. Earlier heads were green too (a003d6e: run 45149461513; b073324: run 45143884916). This branch is merged with dev again when it retargets there after Provider: a memory_only cache policy per request, and SSD cache records without token IDs #2575 lands.
  • Refactor pass (a separate helper, working tree only):
  • Independent check (a separate read-only helper over the whole diff): no memory_only byte reaches a file or an SSD read, write, index, epoch or evidence path; every retained byte is wired and zeroed on release; a wiring failure stores nothing; ordinary and marked traffic are separated both ways. Its findings are fixed here: Nemotron Super excluded, close joins restores, the covered-charge release and retry, budget 0 not offered, duplicates, release before sleep, the load release with rollbacks set, private pools in tests, and the added test cases.
  • Not measured yet: the up-to-2× admission cost and the release time of a full 8 GiB tier on a real Mac.

A memory_only request (zero data retention) never touches the SSD tier, so
on complete-checkpoint slots it prefilled cold on every repeat. Its
checkpoints now go to a RAM-only tier with the SSD tier's lookup and restore
semantics and no file:

- MemoryOnlyCheckpointTier copies the engine's export into one wired region
  per checkpoint (mmap + MADV_ZERO_WIRED_PAGES + minherit(NONE) + mlock;
  nothing is kept if wiring fails) and restores it through the engine's own
  import. Same scope-salted chain tags (per-process key), position rules and
  identity, salt and exact-prefix checks. Ordinary receipts are refused and
  counted.
- MemoryOnlyCheckpointPool: process-wide budget min(cap, fleet KV headroom /
  4), off below 1 GiB; each entry charged to the process ledger and admitted
  only with a whole budget of headroom beside it; LRU; the SSD tier's TTL and
  TTL mode; released on memory pressure, before a model load (and counted as
  reclaimable), on any refused charge, on unload and at shutdown, in both the
  provider loop and the standalone server.
- The SSD store forwards memory-only receipts to its attached tier at PR 1's
  gates; the bridge stages from it and keeps the prefix cache on for
  memory_only text requests only on single-Mac slots with a tier. The tier is
  not attached to GLM-5.3-Flash, DiffusionGemma, pairs or slots built with
  DARKBLOOM_PREFIX_CACHE_MEMORY=1.
- DARKBLOOM_ZDR_PREFIX_CACHE_RAM=auto|off, DARKBLOOM_ZDR_PREFIX_CACHE_RAM_MAX_MB,
  darkbloom_zdr_prefix_ram_* counters on local /metrics.
- The prompt-token piece cache drops a piece 30 minutes after its last use
  (DARKBLOOM_TTFT_PROMPT_CACHE_TTL_SECONDS, default 1800, 0 = no TTL), for
  every request.
@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
d-inference-landing Ready Ready Preview Nov 24, 2026 7:33pm UTC
darkbloom-status Ready Ready Preview Nov 24, 2026 7:33pm UTC
eigen-homepages-darkbloom Ready Ready Preview Nov 24, 2026 7:33pm UTC

Request Review

@jonathan308 jonathan308 added the run-ci Trigger one-shot PR CI run (auto-removed when run starts) label Nov 23, 2026
@github-actions github-actions Bot removed the run-ci Trigger one-shot PR CI run (auto-removed when run starts) label Nov 23, 2026
A RAM entry is not a durable commit, so the tier now completes every
donation with no positions and the engine publishes no ready evidence for a
memory_only receipt. Tests read the stored count instead, use a budget below
one page for the oversize case, and check the SSD store's own state rather
than the process-wide counter other suites also move.
…ull queue refuses

- The tier settles a donation its queue refuses (full or shut down): the
  pipeline hands it back without `onDropped`, so the engine's completion,
  which closes the donor's publication batch, never ran.
- The bridge's RAM stage is its own branch before the base's memory-only
  barrier, which is unchanged.
- The pool removes committed entries in one place, refreshes its gauges
  under the same lock as a lookup, and names the headroom it requires.
- The factory's switch is `attachesMemoryOnlyTier`; helper visibility,
  comments and two docs sentences tightened; one loop helper shared by the
  release and handler suites.
…the MiMo guards

The RAM-only tier's budget is host memory, so both servers update it at the
top of the survivors' re-slice, whatever native MiMo ownership allows.
# Conflicts:
#	provider-swift/Sources/ProviderCore/KVCacheSSD/SSDHybridCheckpointStore.swift
…d, more tests

- A refused charge the tier's entries cover now releases the least recently used ones and retries once, so an ordinary request is admitted on its first call; a charge that can't fit either way leaves the tier held. Engine-owned refusals release only the shortfall, off the admission lock.
- closeAndWait now also waits for a restore still filling an engine import.
- A re-donation of a held prefix copies nothing (counted as duplicate), so a fixed TTL keeps counting from the first store.
- The tier releases its entries before the Mac sleeps, so no hibernation image holds them.
- With a budget of 0 the tier is not offered, so marked requests don't capture checkpoints that can't be kept; an export larger than the budget is refused before it is copied.
- Nemotron Super gets no tier, as documented. The attach rule is one policy function.
- A load releases the tier before evicting a model even with both reclaim rollbacks set; shutdown releases it before the native-owner wait.
- The pool is reached through the memory budget, so tests use a private pool.
- refused_busy for a full queue; the sweep interval follows a TTL under 60 s; a budget shrunk during a copy still holds; admission refuses before evicting when in-flight copies leave no room; the read scratch covers the manifest decode.
# Conflicts:
#	docs/provider/direct-mode.md
#	provider-swift/Sources/ProviderCore/Inference/PrefixCache/RequestCachePolicy.swift
Release before sleep and the partial release on a covered refused charge; the up-to-2x admission cost when free memory bounds headroom; budget 0 and oversize exports; duplicates and the fixed TTL; transient heap copies; Nemotron Super and native MiMo in the configuration row; the changelog no longer repeats #2575's piece-cache change.
@jonathan308 jonathan308 added the run-ci Trigger one-shot PR CI run (auto-removed when run starts) label Nov 23, 2026
@github-actions github-actions Bot removed the run-ci Trigger one-shot PR CI run (auto-removed when run starts) label Nov 23, 2026
jonathan308 added a commit that referenced this pull request Nov 23, 2026
…ed control)

The tests of #2573 at b073324 on its base, with test-only adapters that give the base's behaviour: a memory_only request's prefix is never kept, the prompt-token piece cache has no TTL, nothing is counted, and no release point or attach rule exists. Production code is unchanged.
# Conflicts:
#	docs/provider/direct-mode.md
#	docs/reference/ssd-kv-cache.md
@jonathan308 jonathan308 added the run-ci Trigger one-shot PR CI run (auto-removed when run starts) label Nov 23, 2026
@github-actions github-actions Bot removed the run-ci Trigger one-shot PR CI run (auto-removed when run starts) label Nov 23, 2026
@jonathan308
jonathan308 marked this pull request as ready for review November 23, 2026 10:01
@jonathan308

Copy link
Copy Markdown
Collaborator Author

Closing: this branch's head (8592233) is already contained in dev.

@jonathan308 jonathan308 closed this Dec 2, 2026

This branch was successfully deployed

3 active deployments
Preview – d-inference-landing — 85922333 Deployed Nov 24, 2026 by vercel[bot]
Preview – eigen-homepages-darkbloom — 85922333 Deployed Nov 24, 2026 by vercel[bot]
Preview – darkbloom-status — 85922333 Deployed Nov 24, 2026 by vercel[bot]
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