nemo_automodel.components.speculative.dfly2
nemo_automodel.components.speculative.dfly2
DFly2 speculative-decoding draft training (arXiv:2611.34776).
A block-parallel drafter for sparse-attention targets: each draft layer is paired with one target sparse-attention layer and reads that layer’s keys (KV sharing) at the positions it attends from the row before the anchor (index sharing), so the served drafter keeps no KV cache and runs no indexer of its own.
Submodules
nemo_automodel.components.speculative.dfly2.commonnemo_automodel.components.speculative.dfly2.corenemo_automodel.components.speculative.dfly2.lossnemo_automodel.components.speculative.dfly2.target
Package Contents
Classes
Functions
API
Target model that exposes the attention state its paired DFly2 draft layers read.
Run the frozen forward and return the paired layers’ keys and key lists.
Parameters:
Integer tensor of shape [batch, sequence].
Binary right-padding tensor of shape [batch, sequence].
Target layers paired with the draft layers, in draft-layer order.
Frozen target state one DFly2 training step reads.
One integer tensor per draft layer, of shape [batch, sequence,
slots]. Row r lists the context_keys rows the paired target layer
attended from token r, with -1 marking unused slots. Every listed key
lies at or before r in r’s document.
One tensor per draft layer, of shape [batch, context_keys, key_dim]: the keys and values the paired target layer attended over, in that layer’s own row order (for example a sliding-window segment followed by a shared global segment).
Tensor of shape [batch, sequence, hidden], the target’s final normalized hidden state (the input of its LM head).
Run a frozen target that implements DFly2TargetFeatureProvider.
Run the target once and collect the DFly2 features.
Parameters:
Integer tensor of shape [batch, sequence].
Binary right-padding tensor of shape [batch, sequence].
Tensor of shape [batch, sequence] selecting supervised tokens.
Returns: DFly2TargetBatch
The batch with the features of DFly2TargetFeatureProvider.dfly2_target_features.
Return the target’s input embedding.
Return the target’s LM head.
Bases: Module
DFly2 online training wrapper computing the D-PACE-weighted LK objective.
Run the draft on the target features and compute the DFly2 loss.
Parameters:
Target-token tensor of shape [batch, sequence].
Supervision tensor of shape [batch, sequence].
One tensor per draft layer of shape [batch, context_keys,
key_dim], as in DFly2TargetFeatures.
One integer tensor per draft layer of shape [batch,
sequence, slots], as in DFly2TargetFeatures.
Tensor of shape [batch, sequence, hidden].
Returns: DSparkStepMetrics
Step metrics. The per-position acceptance sums have block_size entries,
Target config that owns construction of its architecture-specific DFly2 draft.
Build a draft from the recipe’s declarative options.
Build every draft query’s key list: the target’s picks for its block, then its block.
Each block reads the key list of the target row just before its anchor, the last token
the target has verified when the block is drafted. Every query of a block shares that
list and then sees its own block’s draft keys, which follow the num_context_keys
context rows in the draft attention’s key tensor. A block never sees another block. An
anchor at the sequence start has no verified row before it and sees only its own block;
a dropped block (block_keep_mask false) sees no key at all, so its output is zero.
Parameters:
Integer tensor of shape [batch, sequence, slots] with -1
unused, as in DFly2TargetFeatures.
Integer tensor of shape [batch, num_anchors].
Boolean tensor of shape [batch, num_anchors].
Number of context rows preceding the draft keys.
Draft slots per block, the anchor slot included.
Whether a draft query sees only the earlier slots of its block.
Returns: torch.Tensor
Int32 tensor of shape [batch, num_anchors * block_size, padded_slots], the context