Skip to content

fix(ml): KV-carrying decode cursors — O(T) speculative decode - #78

Merged
ronaldtse merged 2 commits into
mainfrom
fix/speculative-ot-cursors
Sep 12, 2026
Merged

ronaldtse merged 2 commits into
mainfrom
fix/speculative-ot-cursors

Conversation

@ronaldtse

Copy link
Copy Markdown
Contributor

Replaces the speculative decode implementation's cost model and records what the measurement taught.

The fix. IMFModel.cursor(hidden) — a DecodeCursor carrying 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):

  • plain 2.1 int8 21.4s · lite int4 75.7s · speculative 155.4s
  • int4 CPU kernels (MatMulNBits) lose to int8 — the "small drafter" is the expensive model on CPU
  • dynamic-int8 graphs compute activation scales per fed tensor, so single-step vs batched decode framing yields materially different decodes: runtime acceptance 0.4614 vs the probe's 0.9886 (a uniform-framing artifact), and the batched-verifier output lost a word on a canonical row

Positioning consequence (playground tier pulled in interscript.github.io#185): SpeculativeModel is documented as measurement infrastructure on quantized artifacts; output preservation vs translate() 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.

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.
@ronaldtse
ronaldtse merged commit 7f259f5 into main Sep 12, 2026
8 checks passed
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