Encode branch histories in masks and positions
Flatten candidate trees without leaking sibling information, distinguish logical depth from storage order, and separate correct target scoring from a proved sampling algorithm.
On this page
The question for today
What you’ll understand
- Define ancestor-only attention for candidate node representations.
- Assign positions by logical path depth rather than flattened index.
- Explain why a correct tree forward pass alone does not prove stochastic exactness.
A candidate tree proposes two possible first tokens and several children. Flattening the nodes into one tensor can let the target evaluate many histories at once, but an ordinary left-to-right mask would allow later nodes to see earlier siblings. Those are not valid histories of either candidate path. Tree verification therefore needs a topology-aware attention relation, appropriate positions, and a mapping back to branch identity. It also needs a separately justified rule for choosing which scored candidates become output.
Make each node represent one valid path
Let a committed prefix have length L and a candidate node v have depth d(v), where the first proposed tokens have depth one. Its representation can attend to the shared prefix and its own ancestor path including itself, but not sibling branches. For a decoder-only model, that representation supplies next-token logits after the path through v; the probability of v's token is obtained from its parent history. Preserve that prediction offset when gathering verification scores. Otherwise even a correctly shaped attention tensor can associate a token with the wrong conditional distribution.
A flattened storage order is an implementation choice. Siblings at the same logical depth receive the same sequence-position index for their respective alternative paths, even if their tensor offsets differ. Rotary or other positional features must follow the model's supported position semantics rather than simply count every flattened node as a new chronological token. Store parent and depth mappings and compare small tree outputs against separate ordinary target runs on each root-to-node path. Shape equality alone does not show that the target saw the same histories.
Separate scoring layout from output selection
SpecInfer and Medusa describe tree-based verification mechanisms with particular proposal and selection designs. Their tree attention shares prefix work and evaluates branch histories, but the attention mask itself is not a rejection-sampling proof. Greedy verification can follow the target's chosen token from the current history if that child exists, then emit the target choice when no matching child remains. Stochastic verification must account for how candidates were proposed, their probabilities, the order of tests, and any residual updates. Taking the highest-scoring available branch generally defines a search policy rather than sampling the original target law.
More branches increase candidate coverage but also target arithmetic, mask metadata, logits, and temporary KV storage. A tree with many nodes can be expensive even if its maximum depth is small. Duplicate tokens at different histories remain different nodes because their context differs. If an implementation merges nodes or reuses state, check that the represented histories and required probabilities remain equivalent. Approximate acceptance modes can be useful empirical tradeoffs, but they should be named separately from exact stochastic verification. Report the actual mode and its evidence rather than infer exactness from a high branch acceptance count.
Test topology, score gathering, and resource costs together
Construct a tiny tree with unequal branch lengths and repeated token labels, then compare every node representation's next-token distribution with an independent forward pass on its exact path. Check positions, parent-to-child probability gathering, cache selection, EOS, and branch pruning. Validate numerical tolerances under the actual attention kernel. Dense and specialized tree kernels can have different work and storage behavior, so count actual candidates and profile the supported implementation. A logical mask with many forbidden pairs does not guarantee that a dense kernel skips all corresponding arithmetic.
The function lab models only the quadratic size of a dense candidate-to-candidate mask block. For each separate view, Reset, set both a=1 and dimensionless size ratio x to 1 or 2, predict y=ax², then inspect. Doubling the size ratio changes the value from one to four. This is not a tree, attention kernel, or mask validator, and it omits the shared-prefix block and sparse representations. Use it for the specific N² dense-storage scaling, while real verification correctness still comes from path-level comparisons and the actual sampling procedure.
Work it through
Count valid tree attention relationships
A fictional committed prefix has ten tokens. Candidate nodes are A, B at depth one; A 1, A 2 are children of A, and B 1 is a child of B, all at depth two. Use zero-based positions and the stated ancestor-plus-self attention rule.
A and B have logical position 10. A 1, A 2, B 1 have position 11, despite distinct flattened tensor offsets.
Each depth-one node has one allowed candidate key: itself. Each depth-two node has two: its parent and itself. Therefore the candidate-to-candidate block has 1+1+2+2+2=8 allowed pairs out of 25 possible.
Every candidate can also attend to the ten committed prefix tokens, adding 5×10=50 allowed pairs. There are 58 allowed relationships in this logical query/key rectangle; this count does not prove a dense kernel executes only 58 operations or establish output-sampling exactness.
The layout has correct path positions and eight allowed candidate relationships. Prefix masking, parent score gathering, and the separate verifier still determine whether the overall decoder is correct.
The living notebook
Explore the model
Interactive lab · function
Inspect dense candidate-mask growth only
For each view: Reset, set both a=1 and x, predict, then inspect. Compare x=1 and x=2 in y=ax²: values are 1 and 4. This dimensionless analogy describes a dense N×N candidate-mask block, not a tree attention implementation, sampler, or measurement of sparse-kernel work.
- y at chosen x
- 0.608
- Tangent slope · 2ax
- 1.35
- Signed area · ax³/3
- 0.182
This lab uses only y = ax². Playback traces the curve within the fixed integration interval from 0 to x; it does not estimate numerical error or change the chosen point. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forBranch depth, flattened storage, logical positions, and actual kernel cost are distinct quantities. Validate the real path histories and output-selection rule.
Pause & reason
Check your understanding
Your turn
Try it yourself
A prefix has length six. A tree has two depth-one nodes, one depth-two child under each, and one depth-three child under one of those. Give their positions and count allowed candidate-to-candidate ancestor/self pairs.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Use positions L+depth−1 and count each node’s path length.
Compare with the solution
The two depth-one positions are 6, the two depth-two positions 7, and the depth-three position 8. Allowed candidate pairs total 1+1+2+2+3=9. This is a logical mask count; probability gathering and the verifier’s sampling proof remain separate checks.
Keep the idea
Take it with you
- Tree attention must preserve prefix and ancestor histories without sibling leakage.
- Logical depth determines positions while storage order determines tensor layout.
- Correct target scoring and exact output selection require separate arguments and tests.
Read further
Original sources and deeper explanations to follow at your own pace.
- Miao et al.: SpecInfer Tree Verification, v 4
- Cai et al.: Medusa Multiple Decoding Heads, v 3
- Leviathan et al.: Fast Inference from Transformers via Speculative Decoding, v 2
- Transformers 4.56.2: Cache and Position Semantics
- Transformers 4.56.2: Assisted Generation and Speculative Sampling Implementation
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.