Repository navigation
Agent fan-out: share a live request's prefix (KV blocks + recurrent state) while it is still decoding #5723
Linwei-Chen
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
For agent workloads, the expensive case is not "the next question after a cold prompt". It is N agents starting from the same long prefix while the first one is still decoding.
Example: Agent 1 sends a 32K system + document prefix, prefills it, and starts streaming. Agent 2 arrives a few seconds later with the exact same 32K prefix. Today oMLX makes Agent 2 prefill the whole 32K again, competing with Agent 1's decode. On a Mac Studio M5 Ultra 256GB this is the difference between a ~2 s launch and a ~15-30 s launch per additional agent, and it gets worse as N grows.
I would like oMLX to support in-flight prefix sharing: publish a request's completed prefix blocks and hybrid-state snapshots as soon as the prefill passes them, so another request can restore them while the donor is still decoding. This is the one capability InferD has and oMLX/mlx-serve currently do not.
What the source does today
At
633d6cf(0.8.4):store_cache()is called from_cleanup_finished()(request finished),_store_system_prefix()(finished), and_store_for_memory_requeue()(request paused under memory pressure). There is no prefill-time publish path.register_block_hash()is called only fromstore_cache()when it allocates new blocks.cache_full_blocks()exists inpaged_cache.pybut has no callers._boundary_cache_snapshots[request_id], but they are keyed by request id and merged into block data only at store time.fork_cache()exists but has no scheduler/HTTP caller.So the paged blocks, refcounts, COW, and boundary snapshots are already there. What is missing is publication timing and cross-request visibility.
What InferD does (existence proof)
InferD's design is documented at https://inferd.incept5.ai/how-it-works.html and in
docs/CONTEXTS.md:snapshot.snapshot_at()inserts the blocks into the trie, incrementsblock_ref, copies the recurrent state into a free slot, and publishes the node. The copy is encoded in the same Metal command buffer; afterwaitUntilCompleted, the next request can read it.admit()walks the trie, shares the blocks, restores the state, and setscomputed = cached-- it only recomputes the last <32 tokens needed for logits.wait_for_leader()/ fanout makes the leader snapshot at the end of the shared part and the follower waits. Their comment: "8 agents on one 32k document: 188 s -> one prefill".POST /v1/contexts) can prefill a prefix once, pin it, and let N agents fork from it; sessions keep per-agent turns.Measured in their docs (Qwen3.8-27B 4-bit + DFlash2, M5 Pro):
The seven optimizations on their page are useful context, but the one that matters here is optimization 4 (prefix cache + recurrent state snapshots) combined with the scheduler's publication timing; optimization 5 (sessions + SSD tier) is the explicit API surface. Continuous batching, chunked prefill, paged 8-bit KV, speculative decoding, and native Metal/int8/ANE are already partly present in oMLX; the missing piece is making a live prefix visible.
Proposed behavior (minimum viable)
store_cache()at finish.fetch_cache()/reconstruct_cache()should be able to restore a "live" entry (blocks + snapshot), not only a stored one. If the donor has not reached the requested boundary yet, optionally wait for a leader snapshot (fanout) instead of duplicating the prefill./v1/contextsand/v1/sessions.live_prefix_hits,live_prefix_tokens_saved,fanout_waits, and per-requestcached_tokensshould make the win visible.A narrower first step is fine: block-aligned prefixes, text-only, one model family (Qwen3.8-Flash-Next), then expand to GLM-5.3 / DeepSeek-V4.1. The acceptance criteria from #5400 still apply: no prefill slowdown, same skip rules as finish/cancel stores, cleanup on reject/cancel, no regression with 2+ requests, token-identical output.
Related work in this repo
Willing to help
I have a Mac Studio M5 Ultra 256GB and can test Qwen3.8-Flash-Next, GLM-5.3-Flash, and DeepSeek-V4.1-Flash with a 32K shared prefix and N concurrent agents. I can provide TTFT, prefill tokens, live-prefix hit counts, and traces. Happy to run a test matrix or review a prototype.
All reactions