nemo_automodel.components.speculative.dfly2.loss
nemo_automodel.components.speculative.dfly2.loss
DFly2 training objective: D-PACE-weighted LK distillation plus an optional confidence head.
Per drafted slot, with p the target distribution and q the draft’s:
TV = 1/2 sum|p - q|andKL = sum p (log p - log q)over the full vocabulary;- the LK hybrid
lambda * KL + (1 - lambda) * TVwithlambda = exp(-eta * sg(1 - TV)), which distills while the draft is far from the target and turns to the acceptance probability1 - TVas it gets close; - D-PACE position weights (arXiv:2605.18810), computed detached from the draft’s cross entropy on the corpus token, weight both terms;
- the confidence head regresses the slot’s acceptance probability
sum min(p, q).
Every term is a weighted sum over the local slots divided by the weight sum of all data-parallel ranks, and the backward loss is scaled by the world size, so the averaged gradient equals the global weighted mean regardless of how slots split across ranks (the DSpark objective’s convention).
Module Contents
Functions
Data
API
Compute per-slot TV and KL without full-vocabulary FP32 temporaries for every slot.
Each chunk is recomputed in the backward pass, so only one chunk’s probabilities are alive at a time.
Parameters:
Tensor of shape […, vocab].
Tensor of shape […, vocab], detached.
Slots per chunk.
Returns: tuple[torch.Tensor, torch.Tensor]
(tv, kl), each of shape […].
Compute FP32 total variation and forward KL for a chunk of slots.
Parameters:
Tensor of shape [tokens, vocab].
Tensor of shape [tokens, vocab].
Returns: tuple[torch.Tensor, torch.Tensor]
(tv, kl), each of shape [tokens].
Compute the draft’s FP32 cross entropy on the corpus tokens, chunked and without autograd.
It only sets the detached D-PACE weights and the logged ce, so no full-vocabulary
log-softmax is kept for the backward pass.
Parameters:
Tensor of shape […, vocab].
Integer tensor of shape […].
Slots per chunk.
Returns: torch.Tensor
Tensor of shape […].
Compute the DFly2 objective and its logged terms for one micro-batch.
Parameters:
Draft outputs over the drafted slots only: draft_logits and
aligned_target_logits of shape [batch, num_anchors, drafted, vocab],
target_ids and eval_mask of shape [batch, num_anchors, drafted],
block_keep_mask of shape [batch, num_anchors], and an optional
confidence_pred of shape [batch, num_anchors, drafted].
D-PACE smoothing in [0, 1].
Sharpness of the LK mixing weight; larger values switch to TV sooner.
Weight of the confidence loss; ignored without a head.
Returns: torch.Tensor
The backward loss (scaled by the data-parallel world size, see the module
Raises:
ValueError:aligned_target_logitsis missing.