Repository navigation
perf(cache,tokenizer,qwen3_5,dashboard): cross-session SSD prefix anchors, BPE drift history, and Qwen3.5 scheduled turn-end states - #459
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
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. |
…hors, BPE drift history, and Qwen3.5 scheduled turn-end states
c1f6cee to
8b31e66
Compare
Summary
Eliminates unnecessary full prefills across both new chat sessions (same model + same workspace) and same-session multi-turn follow-ups after long replies.
flowchart LR S1["Session 1 (cold)"] -->|"BP1 (sys+tools) & BP2 (workspace)<br/>+ cache_control priority hints"| SSD["Persistent SSD Tier<br/>(LRU-touched, relabel-safe)"] S1 -->|"Scheduled DFlash2 decode<br/>rolling Checkpoint -> Ordinary"| SSD S1 -->|"Emitted token IDs<br/>+ lazy cold-restart seed"| TH["TurnTokenHistory<br/>(salt-isolated, seam-safe)"] S2["New Session / Follow-up"] -->|"Matches BP1/BP2 or Turn 1 end<br/>without BPE merge drift"| SSD TH --> S2 SSD -->|"Skip ~90–99% prefill"| UI["Chat UI: '{cached} cached'"]1. Cross-Session SSD Prefix Anchors (Phases 1–3)
crates/mlx-core/src/tokenizer/prefix_breakpoints.rs,tokenizer.rs): single BPE encode pass + bounded shadow Jinja LCP extractingBP1(system+tools end) andBP2(penultimate consecutive leadingusermessage end), plus intra-messagePrefixBreakpointHint { messageIndex, byteOffset, priority? }wired through bothrender_prompt_with_breakpointsandrender_prompt_counted.disk_tier/plan.rs,dflash2_turn.rs,dflash2_state/paged.rs,model/{chat_backend,commands,paged_backend,restore}.rs):configure_draft_state_plan_with_headskeeps explicit prompt heads protected from fast superseding, suppresses pinningprompt_replaywhen an explicit head sits strictly below it, and promotes exact rolling-checkpoint hits at explicit heads.disk_tier/prefix_disk.rs,models/qwen4_exp/*,models/hy_v4_flash/*): token-deterministicBP1/BP2extraction +PrefixDisk::Point.head.cache_controlhints (packages/server/src/mappers/anthropic-request.ts,packages/core/index.d.cts,crates/mlx-core/src/tools/inkling.rs,models/inkling2/paged_forward.rs): maps Anthropiccache_controland leading-user multi-block splits into priority-tieredPrefixBreakpointHints shared by Jinja and Inkling2.crates/mlx-paged-attn/src/cold_cache*,crates/mlx-core/src/disk_tier/state_disk.rs,dflash2_state/disk.rs,inkling2/state_disk.rs):ColdKeyHoldon queued writes & chain cursors +manager.touch()on RAM/SSD hits so sharedBP1/BP2KV blocks stay hot in LRU.ColdCacheManager::verify_live_and_touchsplits confirmedmissingvs transientunverifiederrors (PermissionDenied/EIO) with per-entryverify_backoffand idle wakeups so transient stat errors never drop anchors or stall healthy writes.StateDiskEnginecheckpoint relabeling (checkpoint: true→false) guarded byPersistCommitGuard::RequireLiveSource,StateRelabelLanded.committed_identity,ColdCacheManager::remove_if_identity, andclose_cachedrain of deferred cleanups.packages/dashboard/{src/chat,ui/src/lib}/chat-*): displays{cached} cachedduringprepare/prefill.2. Multi-Turn BPE Re-Tokenization Drift Prevention
TurnTokenHistory(crates/mlx-core/src/tokenizer/turn_history.rs,tokenizer.rs,tools/inkling.rs,engine/*, allChatBackendfamilies):TurnKind::Startturns (/v1/messages&/v1/responses) for both Jinja models and Inkling2, preventing single BPE merges (e.g.'不' + '可' → '不可','.' + '\n\n' → '.\n\n') from invalidating 6K–10K cached reply tokens.cache_salt, bypassed on one-shot renders (preserve_thinking == false), cleared onResetScope::Command, guarded by RAIIPendingPromptPin, full canonical added-token parity, normalizer segment commutation, and Metaspace checks, while keepingprefix_breakpointsoffsets aligned.adopt_allacross Qwen3.5, Inkling2, Hy4-flash, and Qwen4 usingArc<[u32]>+ streaming prefix-only scoring so cold restarts recover split-BPE turn-end SSD anchors without startup decode cost.3. Qwen3.5 Scheduled DFlash2 Turn-End State Publication
StateClass::Checkpoint→StateClass::Ordinaryat finalize (models/qwen3_5/{scheduled_dflash2,dflash2_turn,dflash2_state/paged,model/commands,model/paged_backend,model/lifecycle}.rs,paged_kv_cache_adapter.rs,mlx-paged-attn/src/{block_allocator,block_table}.rs):seq_windowsthrough the end of scheduled draft prefill (plus exact-block-multiple prompts).GdnStateBlobs::snapshot_after_prefix+DraftContextWindow::detach_snapshot, incrementally registers blocks viacache_full_blocks_from(withprefix_cache_removal_epochrebuild), and maintains at most one evictable rollingCheckpointper active lane (0 extralane_state_bytes).Ordinary+ SSD write-behind (is_prompt_head = false) infinalize_paged_turn(and hooks into existing long-reply preemption promotion); adds COW detach + pending-write drain inrollback_last_tokensand fault-isolated optional window flushing.Verification
Rebased cleanly onto
main(d1efe25ba) and verified:cargo fmt --all -- --check— passedcargo clippy -p mlx-paged-attn -p mlx-core --lib --tests -- -D warnings— passed (0 warnings)cargo test -p mlx-paged-attn --lib— 249 passed, 0 failedcargo test -p mlx-core --lib -- --test-threads=1(all touched suites:disk_tier::*,tokenizer::*,tools::inkling,engine::{cmd,paged_turn,prompt_parts},qwen3_5::{dflash2_turn,dflash2_state,prefix_anchor_tests,scheduled_turn_end_tests,preempt_reply_tests},inkling2::state_disk,qwen4_exp,hy_v4_flash, rollback & DFlash2 lane regressions) — 1,291 passed, 0 failedyarn build:native && yarn build:ts && yarn typecheck && yarn --cwd packages/dashboard build— passedvp check(all 12 touched TS files) — passedvp test run(anthropic-request-mapper.test.ts,chat-service.test.ts,chat-thread-libs.test.ts,chat-thread.test.ts) — 421 passed, 0 failed