Skip to content

lanes: an external drafter behind any target backend - #455

Merged
ashhart merged 8 commits into
ashhart:zig-flashnextfrom
jschmied:zig-drafter-interface
Oct 10, 2026
Merged

ashhart merged 8 commits into
ashhart:zig-flashnextfrom
jschmied:zig-drafter-interface

Conversation

@jschmied

@jschmied jschmied commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Today a drafter lives inside its family's backend: Backend.draft is that family's MTP head. Drafters that read the
target's hidden states from outside (EAGLE-3, a DFlash block, an MTP head loaded on its own) have no place to go. Two
cases I know of: Kolibri-1, which has no MTP head and which we serve on our fork with an EAGLE-3 drafter
(josch15366/Kolibri-1-EAGLE3-drafter, about +25 % single-stream decode, byte-identical), and Laguna-S-2.1 with
poolside's published DFlash drafter (#312).

This PR adds the interface in the lane core only. No family changes; Nemotron's and Flash Next's own heads are untouched.

What changes

  • backend.zig: Features and an optional Backend.features(s, taps, start, count), the tapped states of cache rows
    the target still holds: the prompt rows its pass computed (from s.cached) until the first verify, then the last
    verify's rows, and after a keep the rows it retained (a single-stream round keeps before it drafts), until the next
    verify, prefill or release. Packed rows in the drafter's taps order, space (host, or a device pointer on device,
    which the drafter must run on), dtype, and ready, a tagged fence (none, cuda_event, metal_event). Null by
    default (error.NoFeatures).
  • backend.zig: optional Backend.prepare_features(taps): the target learns the drafter's layers once, before any
    forward (Drafted.init calls it), and keeps those layers' states and no others; features() must ask for that set.
    A target without the hook is built with its taps.
  • drafter.zig (new): Drafter vtable taps, facts, open, absorb, hold, held, release. Every call takes a
    round's streams together ([]Absorb, []Hold, one held readback), so a GPU drafter runs them as one batch.
    Facts carries the drafter's own depth, step cost, prior, plain guard, batching and per-stream device bytes
    (Drafted.streamBytes(target) sums them for the native server's memory admission).
  • drafted.zig (new): Drafted, a target backend plus a drafter as one Backend, so the round loop is unchanged.
    • A stream with drafts off never reaches the drafter: "draft": false on the wrapped backend is the target alone.
    • Prefill absorbs the prompt rows the pass computed; each round absorbs the kept rows, then holds drafts; held drafts
      reach the target as host tokens.
    • The drafter is only asked to draft once it has the row before its pending token. A prompt restored whole has none:
      that one round takes fillers (the pending token repeated, which the target rejects), and the drafter starts after
      the first verify. A new draft request clears unused fillers (a copy round can take the filler round's place).
    • Lifecycle: the wrapper tracks the streams it opened (the slot is reserved before open); a failure after the
      target's prompt pass releases target and drafter there (the core releases only a cancelled pass, and then the
      drafter was never opened).
    • A steady round allocates nothing: the per-round lists are kept.
    • facts() takes the depth rule's step cost, prior, plain guard and batching from the drafter, not the target.
    • Chains only: trees and early speculation stay with heads built into a family.
  • fake.zig: the fake target exposes features (a row's state is its token).

Tests (drafted_test.zig, host only)

All drafted runs go through a strict test target: the fake with its own state snapshot, holding exactly what Features
promises (a keep overwrites the rows it drops) and refusing anything else. The fake drafter refuses to draft without
the row before its pending token.

  • Drafted rounds equal one-token rounds: greedy, sampled, and two streams sharing rounds (and those reach the drafter as
    one hold of both streams, at most one readback a verify).
  • The fake drafter's drafts land far above chance (it drafts the target's token four times in five; a row or position
    mistake would drop it to about 1 in 97). Shifting the absorbed rows by one fails.
  • Drafts off on the wrapped backend: the same tokens as the target alone, and no drafter call at all.
  • A prompt restored in part and whole, greedy and sampled: the one-token decode, the drafter starting past the restored
    rows.
  • Injected failures (drafter open, absorb, a target without features, a cancelled prompt pass): target and drafter are
    each released once, the drafter only if it was opened.
  • The drafter's facts reach the depth rule in place of the target's.
  • A whole restore, then a copy round instead of the filler round, then the first real hold: the verify reads the
    drafter, not stale fillers. The strict target refuses an unprepared or different tap set.

Removing the drafts-off bypass, the cleanup, the whole-restore check, the fillers, the filler reset or the tap
preparation each fails its test; reading the
verify's rows shifted by one fails three tests on the strict target (RowsNotHeld). zig build test passes on
zig-flashnext 7742ddd (GB10, Zig 0.17.0).

Not in this PR

  • No CUDA target exposes features yet. The first would be a Kolibri-1 adapter on the CUDA registry (RFC: zig cuda: serve through tensorfold-native, with a CUDA family registry (Nemotron first) #443) with our
    drafter behind it; a registry hook so an entry can load a drafter belongs with it.
  • The depth rule prices drafts linearly (step_ms x drafts); a block drafter (DFlash) costs one pass for any depth, so
    a cost-by-depth table in depth.zig should come with the first measured block drafter.
  • Long prompts: the contract has the target hold every tapped prompt row the pass computed until the drafter absorbs
    them (3 taps x 4096 x bf16 at 128K tokens is about 3 GiB). Streaming prefill chunks into absorb should come with a
    CUDA adapter that needs it.
  • The drafter's own state is not part of prompt reuse: after a restored prefix it drafts without those rows, and after
    a whole restore it starts one round late. Keeping the last tapped row with a kept prompt state would close that.

Questions

  1. Is Backend.features the right boundary, or would you rather the target hand states to the drafter inside verify
    (one call fewer per round, but verify grows a second job)?
  2. Held drafts go to the target as host tokens (one device-to-host copy a round). A device-side path would need
    Window.held to name the drafter's buffer; worth it now, or after a CUDA target measures the copy?

@ashhart

ashhart commented Oct 7, 2026

Copy link
Copy Markdown
Owner

Thank you, @jschmied. An external drafter in front of any target, with the target's own draft left unused, is the right split. Drafts off never reaching the drafter is the property we will keep. The host tests you describe, drafted rounds equal to one-token rounds, a shifted row failing, and a whole restore starting one round late, are the gate for the interface.

Two answers.

features on the backend is the boundary we want. verify stays the target's check of tokens. The wrapper already turns held drafts into host tokens before that call. Putting the state handoff inside verify would give verify a second job.

Host tokens are the right first path. One device-to-host copy a round is acceptable until a CUDA target measures it. A device buffer on the window can wait for that measurement.

We will re-run the host tests on the current tip. Each comment has to be one line before we land the interface under your name, and that includes the file headers. backend.zig, drafter.zig, drafted.zig, and drafted_test.zig have comment runs of two or more lines. The check reports 30 places where a comment continues on the next line. The files are under 600 lines. drafted_test.zig is 509, so more tests belong in a second file.

Nothing in the served families calls it yet. A Kolibri or Laguna drafter is a later change, and it will use this hook rather than a private copy of the round loop. Chains only, as you scoped it. Trees stay with a head built into a family.

deinit frees the scratch lists and does not release a stream still in the map. Callers release first. Worth one line in the header so the next adapter does not miss it.

@jschmied
jschmied force-pushed the zig-drafter-interface branch from 4ed2640 to a419512 Compare October 7, 2026 07:44
@jschmied

jschmied commented Oct 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — done in a419512: every comment in the four files is one line (lean_check reports none there), the deinit note is in drafted.zig's header, and the branch is rebased on 7ae6df7 with zig build test passing.

A drafter that reads the target's tapped states (EAGLE-3, a DFlash block, an MTP head
loaded on its own) now sits in front of any backend that exposes `features`:

- backend.Features and an optional Backend.features: the tapped states of cache rows
  the target still holds (the prompt's, the last verify's).
- drafter.Drafter: taps, depth, open, absorb (rows from `start`, the token after each;
  a rollback is the next absorb's start), hold, held, release.
- drafted.Drafted: target + drafter as one Backend. Prefill absorbs the prompt's rows,
  each round's kept rows are absorbed before holding drafts, held drafts reach the
  target as host tokens. Chains only; trees and early speculation stay with heads
  built into a family. facts() derives the round loop's Model from the target's.

No family changes: the target's own draft is never called, Nemotron's MTP path is
untouched. Tests on the fake target: drafted == one-token rounds (greedy, sampled,
shared), and the drafter's drafts land far above chance; shifting the absorbed rows
by one fails both.
With prompt reuse a restored prefix never runs through the target, so it has no
tapped states: Drafted absorbs the prompt from s.cached, and a drafter's first
absorb may start past 0 (it drafts without the restored rows). Test: a prompt
restored from a kept state, greedy and sampled, equals the one-token decode, and
the drafter's first row is the restored one.
- Drafts off: a stream with drafts off never reaches the drafter, so the wrapped
  backend's "draft": false is the target alone.
- Lifecycle: the wrapper tracks the streams it opened; a failure after the
  target's prompt pass releases target and drafter there (the core releases
  only a cancelled pass, and then the drafter was never opened).
- Drafter facts: depth, step cost, prior, plain guard and batching come from the
  drafter, not the target (whose own head may not exist).
- A prompt restored whole: no feature row is asked for; the drafter still holds
  drafts after the pending token, and the target rejects what does not land.
- Batched calls: one absorb, one hold and one readback for a round's streams.
- Features: space, device, dtype, a readiness handle and the validity interval
  are part of the contract.
- keep() matches windows by position and checks the streams; draft storage
  grows with the windows instead of a fixed width.

Tests: drafts off makes no drafter call; injected failures (open, absorb,
missing features, a cancelled pass) clean up once; a prompt restored in part
and whole decodes exactly; the drafter's facts reach the depth rule; a shared
round is one hold of both streams. Removing the drafts-off bypass, the cleanup
or the whole-restore check fails its test.
- Features lifetime matches the round loop: a single-stream round keeps before it
  drafts, so the rows a keep retains stay readable until the next verify,
  prefill or release. A strict test target holds its own state snapshot,
  overwrites what a keep drops and refuses anything outside the contract; all
  drafted tests run on it.
- A prompt restored whole: the drafter is never asked to draft without the row
  before its pending token; that one round takes fillers (the pending token,
  which the target rejects) and the drafter starts after the first verify.
- The stream's slot is reserved before the drafter opens it, so a failed insert
  cannot leak an opened stream.
- A steady round allocates nothing: the per-round lists are kept.
- Features.ready is a tagged fence (none, cuda_event, metal_event).

Reading the verify's rows shifted by one fails three tests on the strict
target (RowsNotHeld); dropping the fillers fails the restore test.
- A new draft request clears the stream's bootstrap fillers: after a whole
  restore a copy round can take the place of the filler round, and the stale
  fillers then replaced the drafter's first real drafts. Regression test drives
  that sequence through the wrapper.
- Backend.prepare_features(taps): the target learns the drafter's layers once,
  before any forward (Drafted.init); features() must ask for that set. A target
  without the hook is built with its taps. The strict test target refuses an
  unprepared or different tap set.

Dropping the filler reset fails the regression test; skipping the preparation
fails four drafted tests.
Every comment in backend.zig, drafter.zig, drafted.zig and drafted_test.zig is
one line (tools/zig/lean_check.py reports none there); the Features contract
moves to a one-line struct doc and one line a field. drafted.zig's header says
deinit releases no stream still in the map, so callers release first.
Rebased on zig-flashnext 7ae6df7; zig build test passes.
Drafter.Facts.stream_bytes: device bytes each opened stream holds (an EAGLE
drafter's context cache); Drafted.streamBytes(target) gives what an admitted
stream takes in all, for a family adapter to report as its stream_bytes so the
native server's fail-closed admission does not undercount. Test: the facts test
checks the sum; returning the target's bytes alone fails it.
@jschmied
jschmied force-pushed the zig-drafter-interface branch from fcef055 to 0bdaff5 Compare October 10, 2026 00:42
@ashhart
ashhart merged commit 41d4b5c into ashhart:zig-flashnext Oct 10, 2026
1 of 2 checks passed
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you @jschmied. Merged into main (41d4b5c) as reviewed: the external drafter interface in the lane core, where a stream with drafts off never reaches the drafter. Your host tests pass on the current tip on macOS and Linux. Kolibri and Laguna drafters can plug in through it next, rather than through a private copy of the round loop.

@ashhart ashhart mentioned this pull request Oct 10, 2026
chaog992 pushed a commit to chaog992/TensorFold that referenced this pull request Oct 10, 2026
@jschmied
jschmied deleted the zig-drafter-interface branch October 11, 2026 17:36
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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.

2 participants