TorchSpec icon indicating copy to clipboard operation
TorchSpec copied to clipboard

`acc_i` for DFlash 2 measures the unary head, not the token the selector drafts

Open man2machine opened this issue 3 months ago • 3 comments

(this issue was written by an LLM per my direction)

Summary

DFlash2Model inherits DFlashModel's accuracy metric, which takes the argmax of the LM head logits. For DFlash and EAGLE 3 that is exactly right, and for DSpark it is also right. For DFlash 2 it is not: the candidate selector is the last step of the drafting decision, and it never writes back to the logits the metric reads. So acc_i — and simulated_acc_len, which is a product of it — reports DFlash 2 with its selector switched off.

This makes the headline numbers non-comparable across methods in the one place where people compare them, and it makes the metric blind to whether the selector works at all.

Why the same line of code is correct for DSpark and wrong for DFlash 2

The metric lives in torchspec/models/dflash.py:

# torchspec/models/dflash.py:503-504
pred_ids = torch.argmax(flat_logits, dim=-1)
correct = (pred_ids == flat_targets) & (binary_eval_mask > 0.5)

The question is what flat_logits holds by the time this runs, and the two families answer it differently.

DSpark rewrites the logits in place, before the metric. The Markov head is applied to base_logits_4d and the result is what flat_logits is built from:

# torchspec/models/dspark.py:170-177
logits_4d = base_logits_4d
if self.draft_model.markov_head is not None:
    logits_4d = self.draft_model.markov_head.apply_block_logits(...)
...
flat_logits = logits_4d.reshape(-1, vocab_size)

By dspark.py:252 the argmax is therefore taken over corrected logits. DSpark's acc_i is post-correction and reports the token DSpark actually drafts. That is the right behaviour.

DFlash 2's selector never touches the logits. It takes a top-K shortlist from them and re-ranks it with predecessor information (torchspec/models/draft/dflash2.py:387-416):

unary_logits, candidate_ids = torch.topk(logits, self.top_k, dim=-1, sorted=False)
...
scores = unary_logits + torch.einsum("...r,...kr->...k", predecessor, successor)
return scores, candidate_ids

The drafted token is candidate_ids[argmax(scores)]. The bilinear term is precisely what reorders the shortlist, and none of it is written back into logits. So the inherited metric argmaxes the pre-re-rank marginals and scores a token the model would not have proposed.

Consequence

The two cases where this bites:

  1. The selector promotes a candidate that was not the head's top-1. This is the selector doing its job, and it is scored as a miss. acc_i is biased low by exactly the amount the selector helps, so the better the selector, the worse the metric looks.
  2. The selector demotes the head's top-1. Scored as a hit that the model did not take, so the bias is not uniformly one-signed and cannot be reasoned away as a constant offset.

Because acc_i and simulated_acc_len are what get compared across methods, DFlash 2 reads as worse than DSpark at matched draft width for a reason that is purely instrumentation. In our own Qwen3-4B runs this is not a rounding effect — it is the difference between the drafting rule and its input.

There is a second, quieter cost: with the selector invisible to acc_i, selector_loss is the only signal that the selector is learning anything. A selector that had collapsed to a constant re-ranking would leave the headline accuracy untouched, so the failure would not surface in the numbers anyone watches.

Proposed change

Change which token pred_ids holds, and change nothing else.

Everything downstream of that line — correct, accuracy, acc_per_position, and simulated_acc_len — already keys off pred_ids, so redirecting that one binding fixes the whole family of numbers without touching the scoring code:

# torchspec/models/dflash.py:503-504, today
pred_ids = torch.argmax(flat_logits, dim=-1)
correct = (pred_ids == flat_targets) & (binary_eval_mask > 0.5)
# proposed
head_pred_ids = torch.argmax(flat_logits, dim=-1)
pred_ids = self._drafted_token_ids(
    draft_hidden=draft_hidden,
    logits=logits,
    target_ids=target_ids,
    binary_eval_mask=binary_eval_mask,
    head_pred_ids=head_pred_ids,
)
correct = (pred_ids == flat_targets) & (binary_eval_mask > 0.5)

DFlashModel._drafted_token_ids returns head_pred_ids unchanged. That is the entire default implementation, so DFlash, EAGLE 3, and DSpark are bit-identical — DSpark included, because its Markov head has already rewritten the logits by the time the argmax runs, which is exactly why it needs no override.

DFlash2Model overrides it to return the selector's pick:

scores, candidate_ids = self.draft_model.candidate_selector.score_candidates(
    hidden, unary_logits, predecessor_ids,
)
selector_pick = candidate_ids.gather(-1, scores.argmax(dim=-1, keepdim=True)).squeeze(-1)
# Slot 0 is the masked anchor: the selector conditions each slot on its predecessor, so it
# never runs there. Keep the head's own choice; the mask drops it downstream regardless.
anchor_pick = head_pred_ids.view(batch, num_blocks, block_size)[..., :1]
return torch.cat((anchor_pick, selector_pick), dim=-1).reshape(-1)

Returning ids rather than corrected logits is deliberate. A selector has no vocabulary-shaped output to write back — only scores over K candidate indices — and DFlash 2 trains its backbone on the uncorrected head, so rewriting the logits the way DSpark does would move the objective rather than just the metric.

The one detail that matters: the metric must not pass training_successor_ids. score_candidates substitutes the ground truth for the weakest candidate whenever the truth missed the top-K (draft/dflash2.py:396-411). That is the right thing for the selector loss — without it there is no valid target — but a metric computed on the injected shortlist is inflated, because the truth is present by construction. The metric path has to re-score on the shortlist the marginals actually produced.

Scope and compatibility

  • Metrics only. The call sits inside the existing torch.no_grad() block, so gradients, the objective, and every checkpoint are untouched. We verified this locally by neutralising the new path and asserting the loss is bit-identical.
  • acc_i / simulated_acc_len will shift for DFlash 2 runs, so curves are not comparable across the change. That is the point of the fix, but it is worth a note in the release description for anyone tracking a run in flight.
  • DFlash, DSpark, and EAGLE 3 are unaffected, by construction rather than by inspection: the default hook is an identity on the tensor the metric used before.
  • Cost is one extra score_candidates call per step, under no_grad, for DFlash 2 only.

Happy to send a PR if this framing looks right to you.

Aside (not part of the proposal)

While instrumenting this we also found it useful to log selector recall — the fraction of supervised slots whose true token is present in the un-injected top-K shortlist.

It separates "the selector re-ranked badly" from "the answer was never in the shortlist", which are very different problems and are otherwise indistinguishable from selector_loss alone — and under truth injection, training never sees the shortlists that serving will.

It rides along for free, since the un-injected shortlist the fix already computes is exactly what it needs. In our own tree the hook therefore returns (pred_ids, components) so the override can hand back that extra column; the proposal above keeps the signature to the ids alone, because the acc_i fix stands on its own and we would rather keep this issue to one change. Mentioning the recall column only in case it is of interest.

man2machine avatar Aug 24 '26 18:08 man2machine

hi @man2machine makes a lot of sense. since both fixes are already in proposed change. Do you want to create a PR? I can review it.

if not, i can implement it

yubofredwang avatar Aug 26 '26 04:08 yubofredwang

@yubofredwang I'll put out a PR soon. I already implemented the changes in a fork of mine, but it has many other experimental changes as well so I'll need to extract out the relevant stuff for the PR.

man2machine avatar Aug 27 '26 00:08 man2machine

This issue has been closed due to inactivity. Feel free to reopen it if it is still relevant.

lightseek-bot avatar Sep 22 '26 23:09 lightseek-bot