Repository navigation
feat(speculative): branch the draft tree, and verify it at its own positions - #1214
Merged
Merged
Conversation
…nnot ship The drafter projects the whole vocabulary each step to pick one argmax and throws the rest away. `draft_tree` now keeps the runner-up and attaches it as a leaf sibling, which costs one verify position and no extra drafting, and targets the round that accepts nothing. Measured on gemma-4-12b-it-4bit with the 4-bit assistant at block 5, 400 tokens, temperature 0, against 162 chain rounds at 2.4630 emitted per verify with 51 zero-accept: | selection | budget | rounds | emitted/verify | zero-accept | |---|---|---|---|---| | smallest top-two margin | 1 | 158 | 2.5253 | 49 | | smallest top-two margin | 2 | 147 | 2.7143 | 31 | | earliest step | 1 | 150 | 2.6600 | 26 | | earliest step | 2 | 149 | 2.6779 | 26 | Two findings, and the second is why the budget now defaults to 0. **The drafter's confidence does not predict its errors.** Gating on the top-two logit gap at 0.3, 1.0 and 3.0 admitted 2, 2 and 5 extra nodes across 162 rounds and left emitted-per-verify at the chain's value exactly. The drafter is sharply peaked and still only about half right, so the steps it is unsure about are not the steps it is wrong about. Selecting the earliest step instead beats selecting the least certain one on every measure at once: more acceptance, fewer proposed tokens, half the zero-accept rounds. A runner-up at step `i` is only reachable if everything before it was accepted, which the margin does not describe and the step index does. **Branching is verified at the wrong RoPE positions.** A flattened tree is fed to the target as a contiguous span, and the tree mask fixes what each node can see but not where each node sits: a node's position is its index in the span, which equals its depth only while the tree is a chain. One sibling puts every later node one place too far along. That is why a linear tree is byte-identical to the chain path and a branching one is not, and speculative decoding is supposed to preserve the target's greedy output exactly, so it is a defect rather than a tradeoff. The Gemma 4 forward has no position-id parameter at all; positions come from the cache offset and the span index. So `MLXCEL_MTP_TREE_BRANCHES` defaults to 0 and warns when set. The machinery and the numbers stay because they are what makes the position-id work worth doing: half the zero-accept rounds is a real prize, and nothing else about the tree path has to change to collect it. Refs #1204
…span's The tree mask says what each node can see. Nothing said where each node sits. A flattened tree is handed to the target as a contiguous span, so a node's RoPE position is its index in that span, and index equals depth only while the tree is a chain. One sibling puts every later node a place too far along. That is why the linear tree was byte-identical to the chain and a branching one was not, and it is not a tradeoff: temperature-0 speculative decoding is supposed to reproduce the target's own greedy choice, not a shifted one. `DraftTree::depths` is the missing half of the block description, carried beside the mask on `Gemma4SpeculativeSinks` because that struct is already threaded from the MTP adapter into the forward that needs it, and consumed by two new per-position rotations in the attention layer. Both group consecutive positions into one call, so a linear tree resolves to exactly one `apply_rope` through the identical kernel the uniform path uses and cannot move the numerics on the configuration it subsumes; a tree with n leaves costs n + 1. Positions are relative to the cache offset, so a caller does not have to know which offset each layer family is at. Isolated with a probe that widened the block with a leaf the target would never pick: 400 tokens, byte-identical to the chain. So the mask, the positions, the widening and the path rollback are all correct, and what remains is confined to accepting a leaf. Accepting a leaf does not reproduce the chain's exact tokens, and cannot. The leaf's bonus is the target's argmax evaluated at a tree position, while the chain evaluates that same context at the head of the next block. Both are the target's greedy choice for the same context; they differ only where the top two sit inside f16 reduction noise. That is the jitter class MTP already carries: chain, linear tree and branching all first diverge from the non-speculative path at the same character, and the branching arm is deterministic run to run. It is a weaker guarantee than the linear tree's byte-identity, so branching now defaults to off and says why. Also removes the drafter overhead that made the first branching measurement look worse than it was. The runner-up lookup cost two device round trips per step, one of them purely to read a margin that the default policy does not use: it selects the earliest steps, because a runner-up at step i is only reachable if everything before it was accepted, which the logit gap does not describe. The margin is now read only when its gate is switched on, and the token-only lookup is skipped once the budget is filled. What is not established is the throughput case. Branching lifts acceptance (emitted-per-verify 2.4630 to 2.6600, zero-accept rounds 51 to 26 of 162) and widens every verify block to do it. Measuring which way that trades needs a quiet machine and the discard-warm-up-then-ABBA protocol; the samples taken here spread 48 to 84 tok/s within a single arm and decide nothing. Refs #1204
This was referenced Aug 18, 2026
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…sitions (lablup#1214) * feat(speculative): propose runner-up candidates, and find why they cannot ship The drafter projects the whole vocabulary each step to pick one argmax and throws the rest away. `draft_tree` now keeps the runner-up and attaches it as a leaf sibling, which costs one verify position and no extra drafting, and targets the round that accepts nothing. Measured on gemma-4-12b-it-4bit with the 4-bit assistant at block 5, 400 tokens, temperature 0, against 162 chain rounds at 2.4630 emitted per verify with 51 zero-accept: | selection | budget | rounds | emitted/verify | zero-accept | |---|---|---|---|---| | smallest top-two margin | 1 | 158 | 2.5253 | 49 | | smallest top-two margin | 2 | 147 | 2.7143 | 31 | | earliest step | 1 | 150 | 2.6600 | 26 | | earliest step | 2 | 149 | 2.6779 | 26 | Two findings, and the second is why the budget now defaults to 0. **The drafter's confidence does not predict its errors.** Gating on the top-two logit gap at 0.3, 1.0 and 3.0 admitted 2, 2 and 5 extra nodes across 162 rounds and left emitted-per-verify at the chain's value exactly. The drafter is sharply peaked and still only about half right, so the steps it is unsure about are not the steps it is wrong about. Selecting the earliest step instead beats selecting the least certain one on every measure at once: more acceptance, fewer proposed tokens, half the zero-accept rounds. A runner-up at step `i` is only reachable if everything before it was accepted, which the margin does not describe and the step index does. **Branching is verified at the wrong RoPE positions.** A flattened tree is fed to the target as a contiguous span, and the tree mask fixes what each node can see but not where each node sits: a node's position is its index in the span, which equals its depth only while the tree is a chain. One sibling puts every later node one place too far along. That is why a linear tree is byte-identical to the chain path and a branching one is not, and speculative decoding is supposed to preserve the target's greedy output exactly, so it is a defect rather than a tradeoff. The Gemma 4 forward has no position-id parameter at all; positions come from the cache offset and the span index. So `MLXCEL_MTP_TREE_BRANCHES` defaults to 0 and warns when set. The machinery and the numbers stay because they are what makes the position-id work worth doing: half the zero-accept rounds is a real prize, and nothing else about the tree path has to change to collect it. Refs lablup#1204 * feat(speculative): verify a draft tree at its own positions, not the span's The tree mask says what each node can see. Nothing said where each node sits. A flattened tree is handed to the target as a contiguous span, so a node's RoPE position is its index in that span, and index equals depth only while the tree is a chain. One sibling puts every later node a place too far along. That is why the linear tree was byte-identical to the chain and a branching one was not, and it is not a tradeoff: temperature-0 speculative decoding is supposed to reproduce the target's own greedy choice, not a shifted one. `DraftTree::depths` is the missing half of the block description, carried beside the mask on `Gemma4SpeculativeSinks` because that struct is already threaded from the MTP adapter into the forward that needs it, and consumed by two new per-position rotations in the attention layer. Both group consecutive positions into one call, so a linear tree resolves to exactly one `apply_rope` through the identical kernel the uniform path uses and cannot move the numerics on the configuration it subsumes; a tree with n leaves costs n + 1. Positions are relative to the cache offset, so a caller does not have to know which offset each layer family is at. Isolated with a probe that widened the block with a leaf the target would never pick: 400 tokens, byte-identical to the chain. So the mask, the positions, the widening and the path rollback are all correct, and what remains is confined to accepting a leaf. Accepting a leaf does not reproduce the chain's exact tokens, and cannot. The leaf's bonus is the target's argmax evaluated at a tree position, while the chain evaluates that same context at the head of the next block. Both are the target's greedy choice for the same context; they differ only where the top two sit inside f16 reduction noise. That is the jitter class MTP already carries: chain, linear tree and branching all first diverge from the non-speculative path at the same character, and the branching arm is deterministic run to run. It is a weaker guarantee than the linear tree's byte-identity, so branching now defaults to off and says why. Also removes the drafter overhead that made the first branching measurement look worse than it was. The runner-up lookup cost two device round trips per step, one of them purely to read a margin that the default policy does not use: it selects the earliest steps, because a runner-up at step i is only reachable if everything before it was accepted, which the logit gap does not describe. The margin is now read only when its gate is switched on, and the token-only lookup is skipped once the budget is filled. What is not established is the throughput case. Branching lifts acceptance (emitted-per-verify 2.4630 to 2.6600, zero-accept rounds 51 to 26 of 162) and widens every verify block to do it. Measuring which way that trades needs a quiet machine and the discard-warm-up-then-ABBA protocol; the samples taken here spread 48 to 84 tok/s within a single arm and decide nothing. Refs lablup#1204
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.
Summary
Tree drafting can now branch: the drafter keeps the runner-up it was already computing and attaches it as a leaf, and the target verifies every node at its own position instead of at the span's. Branching is opt-in and defaults to off; the tree path it sits on is itself behind
MLXCEL_MTP_TREE.Related issues
Refs #1204
Type of change
feat— new user-visible featurefix— bug fixThe defect this found
The tree mask says what each node can see. Nothing said where each node sits.
A flattened tree is handed to the target as a contiguous span, so a node's RoPE position is its index in that span. Index equals depth only while the tree is a chain, so one sibling puts every later node a place too far along and the target verifies them somewhere they do not belong. That is exactly why the linear tree merged in #1212 was byte-identical to the chain path and a branching one was not, and it is not a tradeoff: temperature-0 speculative decoding is supposed to reproduce the target's own greedy choice.
DraftTree::depthsis the missing half of the block description. It rides beside the mask onGemma4SpeculativeSinks, which is already threaded from the MTP adapter into the forward that needs it, and is consumed by two new per-position rotations in the attention layer. Both group consecutive positions into one call, so a linear tree resolves to exactly oneapply_ropethrough the identical kernel the uniform path uses and cannot move the numerics on the configuration it subsumes; a tree withnleaves costsn + 1. Positions are relative to the cache offset, so the caller does not have to know which offset each layer family is at.How it was isolated
Rather than reasoning about which of mask, positions, widening or rollback was wrong, a probe replaced the candidate with a token the target would never pick. The block still widened, the mask still ran, the positions still applied and the rollback still gathered a path, but no leaf was ever accepted.
That run is byte-identical to the chain over 400 tokens. So all four are correct and what remained was confined to accepting a leaf.
What accepting a leaf does, precisely
It does not reproduce the chain's exact tokens, and it cannot.
A leaf's bonus is the target's argmax evaluated at a tree position; the chain evaluates that same context at the head of the next block. Both are the target's greedy choice for the same context, and they differ only where the top two sit inside f16 reduction noise. That is the jitter class MTP already carries against non-speculative decoding, and the measurement says so directly: chain MTP, linear tree and branching all first diverge from the non-speculative path at the same character (161), and the branching arm is deterministic run to run.
It is still a weaker guarantee than the linear tree's byte-identity with the chain, so
MLXCEL_MTP_TREE_BRANCHESdefaults to 0 and the code says why.Two findings about the selection policy
The drafter's confidence does not predict its errors. The issue proposed spending verify positions where the drafter was least sure. Gating on the top-two logit gap at 0.3, 1.0 and 3.0 admitted 2, 2 and 5 extra nodes across 162 rounds and left emitted-per-verify at the chain's value exactly. The drafter is sharply peaked and still only about half right, so the steps it is unsure about are not the steps it is wrong about.
Selecting the earliest step beats it on every measure at once: more acceptance, fewer proposed tokens, half the zero-accept rounds. A runner-up at step
iis only reachable if everything before it was accepted, which the logit gap does not describe and the step index does. The margin survives as a documented escape hatch, off by default.Drafter overhead
The first branching measurement looked worse than the mechanism deserved because the runner-up lookup cost two device round trips per step, one of them purely to read a margin the default policy does not use. Per round that was draft time 2.87 ms to 4.77 ms, +66%, against a verify that grew only 11% for a block widening 25%. The margin is now read only when its gate is on, and the token-only lookup stops once the budget is filled.
Test plan
cargo fmt --all -- --checkcargo clippy --workspace --all-targets --features metal,accelerate -- -D warningscargo test --workspace --profile test-fast --features metal,accelerate -- --test-threads=1, failure sets unchanged frommainon this machine (13 pre-existing local reds in the root lib, 4 numeric-tolerance ones in mlxcel-core)gemma-4-12b-it-4bit+gemma-4-12b-it-assistant-4bit, block 5, 400 tokens, temperature 0depthson a chain, on a tree with a shallow sibling, and on one with a deep sibling;runner_upnaming the second-best token and its margin, and declining a chosen token outside the top twoThe throughput verdict: negative, and worse at the block the bar came from
Measured on a quiet machine, three warm-up runs discarded, then
chain b0 b1 b1 b0 chainblocks so residual drift cancels inside each block. Six samples per arm per regime, spreads under 0.7 tok/s, no overlap between arms.Block 4 (400 tokens, chain at 2.4630 emitted per round):
Block 5 (300 tokens, chain at 3.6463 emitted per round, above the 3.49 bar this issue set):
The linear tree's overhead amortizes with the block, 4.1% down to 1.9%, which is what it should do. Branching goes the other way, and the reason is structural rather than incidental: what branching fixes is the round whose chain breaks early, and at block 5 the acceptance rate is 0.78, so only 11 of 82 rounds are that round. The leaf is attached to the other 71 anyway. The gain is concentrated and the cost is unconditional, so the regime where tree drafting works best is the regime where branching hurts most.
The ceiling, for whoever considers reviving this. Give branching a perfect oracle: it knows in advance which rounds its leaf will rescue and widens only those. Block 4 goes 4743 ms to 4453 ms, +6.5%; block 5 goes 2463 ms to 2319 ms, +6.2%. That is the physical limit, a real signal collects a fraction of it and pays for its false positives, and the linear tree's own 1.9% comes off the top. Best case is around +4%, and it needs a round-failure predictor. The most natural candidate, the drafter's own top-two confidence, is measured here and does not work: the drafter is sharply peaked and about half right, so the steps it is unsure about are not the steps it is wrong about.
So branching stays off, and the acceptance result is recorded rather than shipped: emitted-per-verify 2.4630 to 2.6600 at block 4 and 3.6463 to 3.9342 at block 5, with zero-accept rounds 51 to 26 and 11 to 3.
What this PR is for, given that
The position-id work is a correctness fix and stands on its own. A flattened tree was being verified at the span's positions rather than its own, which is wrong for any branching tree and was invisible while only linear trees ran. Merging it means the tree path is correct if anyone revives branching, and means the finding is in the tree rather than in a closed branch.
Branching defaults to 0, so nothing changes for anyone who does not set it.