Skip to content

fix(inkling2): DFlash2 DraftContextWindow prefix cache, flat SWA I/O, and StateStore RAM cap - #408

Merged
Brooooooklyn merged 1 commit into
mainfrom
fix/inkling2-dflash2-prefix-cache
Nov 25, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
fix/inkling2-dflash2-prefix-cache

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Contributor

Why

In long multi-turn Inkling-2-Mini coding-agent sessions with DFlash2 enabled, two root bugs compounded:

  1. Follow-up turns lost DFlash2 speculation or re-prefilled the entire prompt — neither scheduled nor exclusive prefix-cache hits saved/restored the 2.53 MiB 4-bit DraftContextWindow (sliding_window = 2048), while target SWA blocks older than N - 1024 are pruned.
  2. Unbounded engine RAM growth (~58+ GB footprint) — Inkling2StateStore had no RAM cap, never dropped RAM after SSD write-behind, stored each parked anchor as 2,560 separate Vec<u8>s (~45% mimalloc bloat) + 192 separate 16 KB Metal ShortConv buffers, and was invisible to Governor::charge().

Architecture

flowchart LR
    subgraph Turn["Inkling-2-Mini Turn (Scheduled & Exclusive)"]
        LiveWin["Live DraftContextWindow (2.53 MiB 4-bit GPU ring)"]
        Snap["Lazy Boundary Snapshot (detach_snapshot, 0 host stall)"]
        LiveWin --> Snap
    end

    subgraph Store["Inkling2StateStore (RAM-capped + Governor-charged)"]
        Anchor["Flat Inkling2StateAnchor\n• Arc<Vec<u8>> SWA (0-copy)\n• Arc<Vec<u8>> ShortConv\n• Span 2 4-bit DraftContextWindow"]
        HostBudget["HostPoolCacheLimitGuard\n→ Governor stats.pools"]
        Anchor --> HostBudget
    end

    subgraph Disk["ColdCacheManager SSD Tier (v2 fingerprint)"]
        Blob["State Blob (Spans 0, 1, 2)\n+ 8-layer Global KV chain"]
        Hold["ColdKeyHold on in-place upgrades\n+ quota-floor eviction guard"]
        Blob --- Hold
    end

    Snap -->|"ClaimClass::Optional\ntry_reserve + publish"| Anchor
    Anchor -->|"4-tier RAM trim &\nwrite-behind / repair"| Blob
    Anchor -->|"Prefix hit\n(0 target re-prefill)"| LiveWin
    Blob -->|"SSD restore\n(0 target re-prefill)"| LiveWin
Loading

Summary of Changes

Area Before After
SWA raw block I/O 2,560 separate Vec<u8>s per anchor Single flat Vec<u8> moved zero-copy into Arc<Vec<u8>> (read_block_all_layers_into, read/restore_sliding_raw_blocks_flat_for)
ShortConv parked state 192 live 16 KB Metal buffers per anchor Packed host Arc<Vec<u8>> (~1.9 MiB)
DFlash2 prefix cache Missing on RAM/SSD hits (fell back to AR or cold re-prefill) Live DraftContextWindow capture (detach_snapshot), Span 2 persistence (v2 fingerprint hashing loaded fc + hidden_norm weights), and 0-target-reprefill restore across both scheduled & exclusive lanes
StateStore RAM & Governor Uncapped, invisible to Governor, never dropped after SSD offload Hard ram_cap ((lanes * 4).clamp(8, 32)), 4-tier RAM trim, HostPoolCacheLimitGuard charged into Governor::charge(), lifetime-tied TrackedPackedBytes, ClaimClass::Optional admission, UnitKey::Extent + UnitKey::DraftContext + allocator freed_by_marks reclaim
SSD reliability In-place upgrades could evict fallback or drop repairs ColdKeyHold fallback pinning, ColdCacheManager quota-floor guard, MakeRoom::Refuse in-place overwrites, independent Repair tracking + spill_backoff, unwritable-writer cleanup
Cache policy & hygiene reuse_cache = false leaked checkpoints/taps; missing push_decode_limit & embedding weights Gated checkpoints, preemption/replay, and draft snapshots on reuse_cache; wired DecodeCacheLimitGuard; added Embedding::resident_weight_arrays(); lazy-allocated LookupIndex and reset on park

Verification

  • cargo fmt --all -- --check: pass
  • cargo clippy -j 2 -p mlx-paged-attn -p mlx-core --lib --tests -- -D warnings: pass (0 warnings)
  • cargo test -j 2 -p mlx-paged-attn --lib: 234 passed, 0 failed
  • cargo test -j 2 -p mlx-core --lib models::inkling2: 152 passed, 0 failed (re-verified state_disk 34/34 post-rebase onto origin/main)
  • cargo test -j 2 -p mlx-core --lib disk_tier: 36 passed, 0 failed
  • cargo test -j 2 -p mlx-core --lib cache_limit: 33 passed, 0 failed
  • cargo test -j 2 -p mlx-core --lib engine::governor: 42 passed, 0 failed
  • cargo test -j 2 -p mlx-core --lib models::qwen3_5::dflash2_state: 66 passed, 0 failed

… and StateStore RAM cap

- Add single-allocation flat SWA raw block I/O (read_block_all_layers_into, read/restore_sliding_raw_blocks_flat_for) eliminating 2,560 per-layer Vec<u8> allocations per parked anchor
- Flatten Inkling2StateAnchor to zero-copy Arc<Vec<u8>> SWA + packed ShortConv host bytes + optional Span 2 4-bit DraftContextWindow (v2 fingerprint with in-memory DFlash2 context fusion weight hash)
- Add hard StateStore RAM cap, 4-tier RAM trim, spill backoff, ColdKeyHold-protected in-place SSD upgrades & Global KV chain repair, unwritable-writer cleanup, and HostPoolCacheLimitGuard Governor budget registration
- Wire live DraftContextWindow capture (lazy detach_snapshot, best-effort) and zero-target-reprefill prefix-cache restore across both scheduled and exclusive DFlash2 lanes, plus parked draft-ring cap & Governor reclaim (UnitKey::DraftContext, UnitKey::Extent, allocator freed_by_marks)
- Gate cache-opted-out turns across prefill, decode, preemption, replay, and final-token materialization; include embedding weights in resident accounting and lazy-allocate LookupIndex
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 529bfc61-3889-46e7-9076-1f21c7d248c5

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-25T00:58:27.945315Z 7929aa5 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Brooooooklyn
Brooooooklyn merged commit 26a7bbe into main Nov 25, 2026
12 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/inkling2-dflash2-prefix-cache branch November 25, 2026 02:10
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