Repository navigation
Add resumable Qwen MTP speculative decoding to ChatSession - #581
Conversation
52a7638 to
4ee84eb
Compare
cf37ffd to
7db1760
Compare
7db1760 to
97d411c
Compare
|
@davidkoski could you take a look at this pr when you get a chance? |
97d411c to
f5c4688
Compare
| case .mtp(_, let blockSize) = speculativeDecoding.strategy, | ||
| let drafter = mtpDrafter | ||
| { | ||
| let iterator = try MTPSpeculativeTokenIterator( |
There was a problem hiding this comment.
The continuation ownership looks like the right boundary for applications using ChatSession. One remaining concern: ChatSession directly constructs MTPSpeculativeTokenIterator, while its initial beginRound probe can still throw when the cache cannot stage a speculative verification block. That appears to fail the chat request rather than select target-only generation.
Could unsupported speculative-cache capability be handled explicitly before prefill, preserving the authoritative target cache? Please keep this distinct from catching arbitrary initialization errors, since some errors may occur after cache mutation. A focused test with a cache that supports ordinary generation but refuses speculative staging would establish the fallback contract for both cold and warm sessions.
There was a problem hiding this comment.
Thanks for the feedback. I have provided a fix:
The iterator now throws a dedicated MTPInitializationError.unsupportedSpeculativeCache when its initial staging probe fails. This happens before target prefill, while the target cache is still valid.
| let prefillStart = Date.timeIntervalSinceReferenceDate | ||
| var mtpPrefill = parameters.prefill | ||
| if drafter.requiresPromptPrefill { | ||
| if drafter.requiresPromptPrefill || continuationToResume != nil { |
There was a problem hiding this comment.
Stateful MTP and warm continuation currently override the caller’s prefill configuration with Int.max and .unchunked. For long RAG prompts or large continuation suffixes, this could increase peak memory and first-token latency substantially.
Could we preserve bounded chunking while aggregating the required hidden states, or provide an explicit admission/fallback policy for cases that require unchunked prefill? It would help to include cold-prompt and warm-suffix measurements covering peak memory and first-token latency alongside decode throughput.
There was a problem hiding this comment.
We could preserve the configured chunk size for both target and drafter prefill, aggregating target hidden states across chunks while maintaining correct alignment for warm continuations. Since this primarily affects long prompts and large continuation suffixes, I’d suggest addressing it in a follow-up PR to keep this PR small and focused. What do you think?
There was a problem hiding this comment.
Yes, I think it is reasonable.
Thanks, nice work overall.
| } | ||
|
|
||
| if speculativeDecoding != nil, requiresMainOnlyContinuation { | ||
| if !speculationIsEligibleForParameters || requiresMainOnlyContinuation { |
There was a problem hiding this comment.
Could completion metadata expose the reason when ChatSession bypasses speculation before constructing the iterator? Currently the sampling fallback has nil speculative telemetry and nil passthroughReason, making it indistinguishable from acceleration being disabled.
A small typed fallback reason covering unsupported sampling, media, cache capability, and missing continuation would let applications accurately explain why a configured accelerator was inactive. Zero proposals alone is ambiguous, especially for very short generations. This would also make fallback tests more precise.
There was a problem hiding this comment.
Fixed the missing fallback diagnostics by adding typed reasons to ChatSession completion metadata, so applications can explain why configured speculative decoding was bypassed without changing generation behavior.
|
@ngutech21 this is something I need as well, so thanks for implementing this. See if my comments make sense. |
88139a5 to
f1c9c94
Compare
|
@davidkoski PR is ready for review |
| #expect(afterDrop.info.promptTokenCount == 1) | ||
| #expect(afterDrop.info.cachedPromptTokenCount > 0) | ||
| #expect(afterDrop.info.proposedDraftTokens == nil) | ||
| #expect(afterDrop.info.speculativeDecodingFallbackReason == .unavailableContinuation) |
There was a problem hiding this comment.
Just want to make sure this is desired: after MTP speculation drops then it never gets turned back on (e.g. mtpSpeculativeStateIsReady stays false), even if the session has a full transcript and would otherwise be OK?
There was a problem hiding this comment.
Yes, that was the behavior. Fixed in the latest commit: MTP now recovers from the full transcript when eligible again. Recovery costs one full prefill, with guards against repeated rebuilds and regression tests covering recovery, cache reuse, and fallback cases.
Carry drafter state, target state, KV cache, and transcript atomically across turns and tool restarts. Add generic resumable MTP support with fail-closed target-only fallback, plus Qwen text and VLM adapters.
Move shifted-cache boundary finalization into MLXLMCommon and reuse it across the Qwen text and VLM adapters.
Replace correlated cache-state booleans with an exhaustive reuse capability and centralize ordinary draft and MTP state mapping.
Thread target state through chunked continuation prefill, aggregate hidden states for drafter resumption, and cover two-turn VLM cache reuse.
Reject text and VLM targets whose hidden or vocabulary sizes do not match the MTP drafter.
Rebuild target and drafter state from the retained transcript when MTP becomes eligible again. Suppress further forced recovery after staging or finalization failure.
davidkoski
left a comment
There was a problem hiding this comment.
Change looks good, thank you!
Proposed changes
This PR adds MTP drafters as a first-class speculative-decoding strategy for
ChatSessionand enables Qwen text and VLM drafters to resume safely across cached turns.SpeculativeDecodingConfig(mtpDrafter:blockSize:)with typed block-size validation while preserving existing draft-model APIs.MTPSpeculativeTokenIterator, with target-only fallback for unsupported sampling, media inputs, incompatible caches, or missing MTP state.Testing
Coverage includes:
Latest targeted verification:
git diff --checkpassedQwen35.swiftnaming diagnostics are also present at the merge baseChecklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes