Files
leleka/docs/adaptive-spec.md
T

5.9 KiB

Adaptive speculative window (claude/adaptive-spec)

Status: implemented, flag-gated (LELEKA_SPEC_ADAPTIVE=on), model-free tests pass; device results below.

Why

The Original verifies a fixed window: MTP chain of spec - 1 = 3 drafts cut at --spec-min-p 0.5, plus an optional suffix (prompt lookup) window up to spec + 2 = 6 rows chosen by DraftPolicy (a yes/no on the suffix, with the MTP window as the alternative). Whether a wider window pays depends on two things that differ between workloads and between moments of one run:

  • how often the next draft is right (code edit, a file re-emitted from the context, tool-call JSON: high; prose: low), and
  • what one more verified row costs (expert tier hit rate, CPU pool and PCIe load: with a converged tier the extra rows are nearly free; with a cold tier each row touches more distinct experts and the window gets slower).

At about 50 ms a round, one more accepted token per round is worth 30-40 %, so the choice of the width is worth more than most kernel work, and a fixed value is wrong for part of every session.

Policy (crates/leleka-runtime/src/qwen_forward/original_spec_adaptive.rs)

Per round the policy sees everything the drafters offer: the MTP chain (tokens and their own probabilities) and the suffix continuation (up to max_t - 1 tokens, the match length, whether its first token equals the MTP's first draft). It evaluates every prefix of each as a candidate window and takes the one with the best

rate(t) = E[tokens committed by the round](t) / (T(t) + chain ms)
E(t)    = 1 + sum_{j<t} prod_{i<=j} a_i
  • a_i is the calibrated probability that draft i is accepted given drafts 1..i-1 were: for the MTP a table indexed by (depth, bucket of the draft's own probability), shrunk toward a depth-free table per probability bucket, shrunk toward a prior from the bucket centre; for the suffix the first row and the continuation rows by match-length bucket, separately when the suffix agrees with the MTP and when it does not. Counts are decayed (0.995 per sample). A scalar logit bias per source moves fast (rate 0.06 per tested row, fading) so a change of content (code to prose, JSON, a pasted file) is followed in tens of rounds without waiting for the tables.
  • The learning signal is exact: of t-1 offered rows, rows 1..min(accepted+1, t-1) were tested (row j counts as a hit when j <= accepted), the rest were censored by the first rejection.
  • T(t) is the measured round time of a t-row window minus the draft chain (0.45 ms per chain step), kept as a decayed mean per width (winsorised) blended with a fit a + c (t-1) over the recent rounds (forgetting 0.97 per round, slope pulled toward 15 % of the level per row). The fit is what predicts widths not run lately: it carries the current tier / CPU / PCIe state. A width's own mean is trusted less as it ages. Every observation also moves the other widths a part of the way (a state change is mostly common to all widths).
  • Hysteresis: the previous round's (source, width) is kept unless another candidate is better by LELEKA_SPEC_MARGIN (2 %).
  • Exploration is deterministic (no RNG, traces are reproducible): every LELEKA_SPEC_EXPLORE rounds (32; 8 for the first 24 rounds) a neighbouring width (+-1) whose model rate is within 12 % of the best and which has not been measured for 48 rounds is run instead. Verification keeps the result exact; only one round's speed is at stake.
  • Next chain plan (draft_plan): chain length = peak-held recent width (decays 8 % per round) capped by max_t - 1; confidence floor = the smallest draft probability whose calibrated acceptance clears rate * (row slope + chain step ms) / survival, i.e. a lower floor when rows are cheap, clamped to [LELEKA_SPEC_MIN_P_LO, request spec_min_p] (the operator's value stays the upper bound; 0 means 0.9). The plan is applied to the MTP actor per round (set_draft_plan; the actor's max_drafts is set from it).

Verification is untouched. The controller still checks every offered row against the target's own output, commits the accepted prefix and the bonus token. Sampled requests use the existing sampled verification path unchanged (acceptance is equality with the sampled target token; the policy only chooses how many rows to offer).

Knobs

variable default meaning
LELEKA_SPEC_ADAPTIVE off on: adaptive window (8-row verifier, MTP chain up to 7 drafts, suffix 3)
LELEKA_SPEC_ADAPTIVE_MAX_T 8 widest window (rows); the Original's constant kVerifyMaxT is 8
LELEKA_SPEC / _MTP_MAX_T / _SUFFIX 4 / spec / 3 the Original's --spec, --mtp-max-t, --suffix-draft for the fixed policy
LELEKA_SPEC_ARMS - fixed3,fixed4,fixed6,adaptive: policies one process switches between per request (A/B in one load)
LELEKA_SPEC_EXPLORE, _MARGIN, _MIN_P_LO, _DRAFT_STEP_MS, _COST_ALPHA, _DECAY, _BIAS_RATE 32, 0.02, 0.10, 0.45, 0.12, 0.995, 0.06 policy constants
LELEKA_SPEC_STATS=1 off one DBG spec-stats line per request: rounds, tokens per round, width and accepted histograms, mean ms per width
LELEKA_SPEC_TRACE=1 off one DBG spec-trace line per timed round (width, source, accepted, round ms, expected tokens, next chain plan)
smoke: @file:PATH lines in LELEKA_SMOKE_PROMPTS - a whole file as one prompt (code with its newlines); with LELEKA_SPEC_ARMS pass p of prompt i uses arm (p+i) mod arms

Wider windows than 6

kVerifyMaxT = 8 is compiled into the window kernels (acc[kVerifyMaxT] register arrays, the grouped-expert GMAX >= kVerifyMaxT static_assert, head and MoE group tables), so 8 rows is the ceiling without kernel changes and a re-seal of the PTX (sm_120 + sm_86); 12 or 16 rows are not available from this branch. With --spec 6 equivalent (LELEKA_SPEC=6, adaptive) the verifier records windows 1..8 (one more capture + instantiate than 1..6, about +20 ms per request), the MTP is loaded at 8.