nemo_automodel.components.models.deepseek_v41.dfly2
nemo_automodel.components.models.deepseek_v41.dfly2
DFly2 draft for DeepSeek-V4.1-Flash (arXiv:2611.34776).
Each draft layer is a V4.1 block (latent MQA with an attention sink, routed MoE and single-pass mHC) paired with one of the target’s ratio-1 sparse-attention layers. It owns no context projection: its queries read the keys that target layer attends over, its own sliding-window latents followed by the shared 1:1 global latents, at the slots that layer attends from the row before the anchor (the last 128 tokens plus the indexer’s top-512 picks), together with the draft keys of its own block. The released checkpoint pairs layers 28, 32 and 36.
Around the backbone, the target’s final hidden state at the row before the anchor is fused
into the anchor slot (MTP fusion), and a teacher-forced hidden correction, a SwiGLU plus
an attentive correction over the last layer’s context and the block’s own prefix, adjusts
every slot before the frozen LM head. Slot 0 holds the anchor and is not supervised, so a
block of block_size slots drafts block_size - 1 tokens.
Module Contents
Classes
Functions
Data
API
Validated declarative configuration of the V4.1 DFly2 draft.
Draft slots per block, the anchor slot included.
Feed the acceptance head detached states, so its loss trains only the head.
Build the acceptance head, absent from the released draft.
Token filling the non-anchor slots.
Routed experts per draft layer.
Blocks sampled per sequence.
Experts each token is routed to.
Target layers paired with the draft layers, in draft-layer order. Each must be a ratio-1 sparse-attention layer, whose context rows are raw tokens.
Target text configuration; the draft reuses its attention, mHC and MoE shapes and its compressed-layer rotary embedding.
Validate the target pairing and build a freshly initialized draft.
Parameters:
Kernel selection for the draft; defaults to default_dfly2_backend.
Returns: DeepseekV41DFly2Model
The draft, ready for device placement.
Raises:
ValueError: A paired layer is not a ratio-1 target layer, or a size is invalid.
Bases: Module
Model-owned DFly2 draft emitting the shared DSpark loss inputs over its drafted slots.
Run the draft layers on the block inputs.
Parameters:
Tensor of shape [batch, draft_sequence, hidden].
One tensor of shape [batch, context_keys, head_dim] per layer.
One integer tensor of shape [batch, draft_sequence, slots] per layer.
Integer tensor of shape [batch, draft_sequence].
Returns: torch.Tensor
The collapsed, not yet normalized output of shape [batch, draft_sequence, hidden].
Project hidden states through the frozen vocabulary head in FP32.
Parameters:
Tensor of shape […, hidden], with arbitrary leading dimensions.
Returns: torch.Tensor
FP32 tensor of shape […, vocab].
Run DFly2 training for sampled anchors.
Slot k of the block at anchor a predicts token a + k; slot 0 holds the
anchor and is dropped from the outputs, so every output tensor covers the
block_size - 1 drafted slots and its teacher is the target’s distribution at
the token before the label.
Parameters:
Target-token tensor of shape [batch, sequence].
Supervision tensor of shape [batch, sequence].
One tensor per draft layer of shape [batch, context_keys,
head_dim], as in DFly2TargetFeatures.
One integer tensor per draft layer of shape [batch, sequence,
slots], as in DFly2TargetFeatures.
Frozen target tensor of shape [batch, sequence, hidden], the input of the target’s LM head.
Returns: DSparkForwardOutput
Shared DSpark loss inputs. Draft and teacher logits have shape [batch,
Start the attentive correction from the last draft layer’s attention, with its output closed.
Its query and key input projections read [hidden; previous embedding]: the hidden
columns take the layer’s wq_a / wkv and the embedding columns start at zero. The
norms, wq_b, wo_a and both sinks take the layer’s values; wo_b, the gate and
the per-slot biases start at zero, so the correction contributes nothing until trained.
Copy the target’s embedding and vocabulary projection.
Parameters:
Target embedding with weight of shape [vocab, hidden].
Target output projection with weight of shape [vocab, hidden].
Must be true: the draft shares the target’s frozen embedding and head.
Start every draft layer’s attention, norms, mHC mixes and shared expert from its paired target layer.
Collective when the target is sharded: every rank must call it, in the same order.
The routed experts and the router keep their fresh initialization; warm-start them
from an existing draft through draft_weights_path instead.
Parameters:
The frozen target DeepseekV41ForCausalLM; its parameters may be
DTensors, which are gathered one at a time.
Initialize every draft parameter for checkpoint-free training.
The fusion gate, the hidden correction’s down projection and the attentive correction’s output start at zero, so a fresh draft is its backbone alone.
Parameters:
Device used by grouped expert initialization. Defaults to the first attention projection’s device.
Bases: DeepseekV41Attention
Latent MQA over a paired target layer’s keys and the draft’s own block.
Attend draft queries to their listed target keys and their own block.
Parameters:
Normalized draft tensor of shape [batch, draft_sequence, hidden].
Paired target layer’s keys of shape [batch, context_keys, head_dim].
Integer tensor of shape [batch, draft_sequence, slots] of rows of
cat(context_keys, draft_keys), -1 unused.
Integer tensor of shape [batch, draft_sequence].
Returns: torch.Tensor
Tensor of shape [batch, draft_sequence, hidden]. Inputs are not mutated.
Bases: Module
Per-slot latent MQA over the last draft layer’s context plus a gated read of the block prefix.
The query comes from the slot’s normalized hidden state and the normalized embedding of
the token before it. Slot k reads the context keys the last draft layer read, then,
in a softmax of its own with a sink and a learned bias by distance, the keys of slots
0..k of its block, added through a per-head tanh gate. wo_b and the gate
start at zero, so a fresh correction is an exact no-op.
Gated read of each slot’s own block prefix (slots 0..k for slot k).
The prefix has a softmax of its own: a per-head learned bias indexed by how many slots
back the key is, plus the block_sink logit in the denominator as an extra column.
Each head scales its read by tanh of a gate computed from the slot’s inputs.
Parameters:
Tensor of shape [batch, slots, heads, head_dim], whole blocks of block_size.
Tensor of shape [batch, slots, head_dim], each slot’s key, also its value.
Tensor of shape [batch, slots, 2 * hidden], the query inputs.
Slots per block.
Returns: torch.Tensor
FP32 tensor of shape [batch, slots, heads, head_dim].
Compute the correction of every slot.
Parameters:
Tensor of shape [batch, slots, 2 * hidden], the concatenated normalized hidden state and previous-token embedding of each slot; whole blocks.
Last draft layer’s context keys of shape [batch, context_keys, head_dim].
Integer tensor of shape [batch, slots, key_slots] of
context_keys rows, -1 unused.
Integer tensor of shape [batch, slots].
Slots per block.
Returns: torch.Tensor
Tensor of shape [batch, slots, hidden].
Bases: Module
One draft layer: latent MQA, routed MoE and single-pass mHC, as in the target’s blocks.
Expose the shared parallelizer’s MoE interface without duplicate registration.
Apply one draft layer.
The attention input collapses the streams with the previous layer’s FFN pre-mix and the FFN input with this layer’s attention pre-mix, as the target’s blocks do.
Parameters:
Tensor of shape [batch, draft_sequence, streams, hidden].
FP32 tensor of shape [batch, draft_sequence, streams].
Tensor of shape [batch, context_keys, head_dim].
Integer tensor of shape [batch, draft_sequence, slots].
Integer tensor of shape [batch, draft_sequence].
Returns: torch.Tensor
Updated streams of shape [batch, draft_sequence, streams, hidden] and the FP32
Bases: Module
FP32 acceptance head on the drafted slots’ final-normalized states (absent from the released draft).
Predict an acceptance logit per slot.
Parameters:
Tensor of shape […, hidden], with arbitrary leading dimensions.
Returns: torch.Tensor
FP32 tensor of shape […].
Bases: Module
Teacher-forced residual from each slot’s hidden state and the token before it.
Return the corrected hidden states.
Parameters:
Final-normalized draft tensor of shape [batch, slots, hidden].
Tensor of shape [batch, slots, hidden], the embedding of the token before each slot (the anchor for slots 0 and 1).
Tensor of shape [batch, context_keys, head_dim].
Integer tensor of shape [batch, slots, key_slots].
Integer tensor of shape [batch, slots].
Slots per block.
Returns: torch.Tensor
Tensor of shape [batch, slots, hidden].
Bases: Module
Fuse the target’s last hidden state into the anchor slot, as the target’s MTP layer opens.
Compute the residual added to every anchor slot.
Parameters:
Tensor of shape [batch, num_anchors, hidden], the anchor tokens’ embeddings.
Tensor of shape [batch, num_anchors, hidden], the target’s final normalized hidden state at the row before each anchor.
Returns: torch.Tensor
Tensor of shape [batch, num_anchors, hidden].
Attend every query over its listed keys, one shared latent serving as key and value.
Parameters:
Tensor of shape [batch, queries, heads, head_dim].
Tensor of shape [batch, keys, head_dim].
Integer tensor of shape [batch, queries, slots] of kv rows, -1
unused. The listed keys of a row must be distinct.
FP32 tensor of shape [heads], learned logits added to every softmax denominator only.
Query/key score multiplier.
"tilelang" or "cudnn" run the shared sparse kernel; "sdpa" and
"eager" scatter the lists into a dense mask.
Returns: torch.Tensor
Tensor of shape [batch, queries, heads, head_dim] in the dtype of query; zero
Return the draft’s kernel selection.
Parameters:
"sdpa" or "eager" scatter the key lists into a dense mask, which suits
short rows; "tilelang" or "cudnn" run the shared sparse kernel, which long
rows need.
Returns: BackendConfig
Backend with torch linears and FP32 RMSNorm. Like BackendConfig’s own default, the