nemo_automodel.components.speculative.dflash.dflash2_core
nemo_automodel.components.speculative.dflash.dflash2_core
DFlash 2 online training wrapper.
DFlash 2 (https://inco.ai/blog/dflash2/) keeps DFlash’s parallel block draft and
adds a two-tap in-block convolution to the backbone plus a pairwise path selector
over each position’s top-k candidates (see
nemo_automodel.components.speculative.dflash.draft_qwen3_dflash2). The
convolution needs no new supervision — it is part of the backbone and is trained
by the ordinary DFlash objective — so this wrapper differs from
DFlashTrainerModule in exactly one place: it adds a second term that trains
the selector::
loss = base_loss + selector_loss_weight * selector_loss
base_loss is DFlash’s decay-weighted block CE over the full vocabulary, i.e.
what makes each position’s candidate list good. selector_loss is a CE over
the selector_top_k candidates of that same position, scored against the
ground-truth predecessor (the token the walk would have committed had every
earlier position been right) and supervised with the index of the true token
inside the candidate list. Positions whose true token missed the candidate list
carry no selector signal — there is nothing there to select — and are excluded
from the selector term; candidate_recall reports how often that happens.
Both terms use the same block-position decay weights, so a position’s importance is identical in the two objectives.
Module Contents
Classes
API
Per-step training outputs for the DFlash 2 draft.
loss / accuracy / valid_tokens mirror DFlashStepMetrics so the
shared DFlash training loop consumes them unchanged. The primary accuracy and
acceptance-length fields describe the selector path — what the draft
actually emits at decode time — and the base_* fields the backbone’s own
top-1 picks, so the two are directly comparable on the same denominator.
Bases: DFlashTrainerModule
DFlash 2 online training wrapper: DFlash block CE + candidate-selection CE.
Block-position decay weights for the block_size - 1 predicted positions.
Parameters:
Tensor of shape [batch, blocks, depth]; the supervised-position mask the weights are multiplied into.
Returns: torch.Tensor
Tensor of shape [batch, blocks, depth] equal to mask scaled by
Score each supervised position’s top-k candidates against its true predecessor.
Teacher-forces the predecessor: position k is scored after the token at
anchor + k - 1, which is what the decode-time walk would have committed
if every earlier position had been accepted. Position 1’s predecessor is the
anchor token itself, exactly as at decode time.
Parameters:
Tensor of shape [batch, blocks, depth, hidden]; the draft hidden states of the predicted (non-anchor) block positions.
Tensor of shape [batch, blocks, depth, vocab]; the backbone logits at those positions.
Long tensor of shape [batch, blocks, block_size]; the
ground-truth token at anchor + k for block position k, so
depth == block_size - 1.
Returns: torch.Tensor
Tuple (scores, candidate_ids, target_index, has_target): scores
Parallel block-wise training forward with the DFlash 2 path selector.
Sequence packing (position_ids [B, S] per-document reset positions,
seq_lens [B, max_docs] document lengths, doc_remaining [B, S])
is handled by the shared DFlash prologue, which keeps every block inside one
document.
Parameters:
Long tensor of shape [batch, sequence]; the context tokens.
Tensor of shape [batch, sequence, layers * hidden]; the captured target-model context features.
Tensor of shape [batch, sequence]; the supervised-token mask.
Long tensor of shape [batch, sequence] with per-document
reset positions under packing, or None.
Long tensor of shape [batch, max_docs] with packed document
lengths, or None when unpacked.
Long tensor of shape [batch, sequence]; real tokens left
in each position’s document, or None when unpacked.
Returns: DFlash2StepMetrics
DFlash2StepMetrics for this micro-batch.