fix(ml): KV-carrying decode cursors — O(T) speculative decode - #78
Merged
Merged
Conversation
The first SpeculativeModel re-prefilled both models from zero every block: O(T^2), measured 5.7x slower than plain decode. IMFModel now exposes a DecodeCursor (seed/feed/rewindToLen with carried KV caches; plain-graph models recompute behind the same interface) and the speculative loop runs one incremental decoder call per block per model. KV rewind slices per head - [1,H,S,D] memory is not a contiguous prefix (the first attempt corrupted caches). Benchmark (scripts/bench-speculative.mts, arm64 CPU, warm, 5 golden rows): plain 2.1 int8 21.4s, lite int4 75.7s, speculative 155.4s at 0.4614 acceptance. Two measured findings recorded in interscript-ml RESULTS.md: int4 CPU kernels (MatMulNBits) lose to int8, and dynamic-int8 activation scales are computed per fed tensor, so single-step vs batched framing yields materially different decodes - runtime acceptance 0.46 vs the probe's 0.99 uniform-framing figure, and the batched-verifier output degraded a word on a canonical row. SpeculativeModel is documented as measurement infrastructure on quantized artifacts; output preservation holds for fp-class pairs. The e2e asserts decode health and honest stats on the quantized pair; the tiny-fixture pair (drafter==verifier) still asserts exact equality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces the speculative decode implementation's cost model and records what the measurement taught.
The fix.
IMFModel.cursor(hidden)— aDecodeCursorcarrying KV caches across calls (seed/feed/rewindToLen; plain-graph models recompute behind the same interface). The speculative loop becomes one incremental decoder run per block per model, O(T) total — the first implementation re-prefilled from zero every block (O(T^2), 5.7x slower than plain decode). KV rewind slices per head:[1,H,S,D]memory is not a contiguous prefix, and the naive slice silently corrupted caches (caught by exactness on the tiny fixture).What the benchmark measured (
scripts/bench-speculative.mts, arm64 CPU, warm, 5 golden rows; interscript-ml RESULTS.md 2026-09-12):Positioning consequence (playground tier pulled in interscript.github.io#185):
SpeculativeModelis documented as measurement infrastructure on quantized artifacts; output preservation vstranslate()holds for fp-class pairs. The e2e asserts decode health and honest stats on the quantized pair; the fixture pair (drafter==verifier) still asserts exact equality.Full suite 271 passed; the module docstring carries the constraint.