nemo_automodel.components.loss.mtp
nemo_automodel.components.loss.mtp
Module Contents
Classes
Functions
API
Typed config for the Multi-Token-Prediction auxiliary loss.
MTP is gated on the model emitting per-depth outputs; this config only
carries its hyperparameters. scaling_factor=None keeps the
model-provided value (out.mtp_loss_scaling_factor /
get_mtp_loss_scaling_factor); set it to override. ignore_index=None
inherits the configured loss’s sentinel; an explicit value must match it.
Build the pipeline-schedule, MTP-aware loss for loss_fn/model.
Aggregate MTP loss and its per-depth components.
Bases: Module
Pipeline schedule loss that can add MTP auxiliary CE on the last stage.
Per-microbatch seq_idx is read from a trailing element of the
last-stage output tuple — the model appends an [B, S] int32 tail
when MTP is enabled. This binds each microbatch’s seq_idx to its loss
call via the PP runtime’s output→loss contract, so the wiring is
schedule-agnostic. Legacy cu_seqlens (THD path) is a fallback for
models that don’t emit a seq_idx tail.
Detect and strip a trailing per-microbatch seq_idx from output.
Convention: with MTP enabled the last-stage output is
(logits, *mtp_per_depth_h, seq_idx) with an [B, S] int32
tail — dtype alone discriminates.
Last-stage pipeline loss (main CE plus optional MTP aux CE).
B=microbatch, S=seq, H=hidden, V=vocab.
Parameters:
bare hidden states [B, S, H] (FusedLinearCrossEntropy
path), a HF output with logits [B, S, V], or an MTP tuple
(logits, *mtp_per_depth_h[, seq_idx]) with seq_idx
[B, S] int32. A tuple with FusedLinearCrossEntropy raises.
target token ids [B, S] int64.
Returns: torch.Tensor
Scalar loss tensor.
Compute the DeepSeek-V3 Multi-Token Prediction auxiliary loss.
Each depth’s CE is dispatched through :func:calculate_loss with the
same loss class as the main path, so MTP inherits FusedLinearCrossEntropy
/ MaskedCrossEntropy memory and numerical characteristics.
Parameters:
Configured per-token loss class (same instance the main path uses).
Per-depth hidden-state tensors of shape
[batch, sequence, hidden], or [1, tokens, hidden] for a
flattened THD-packed stream.
Per-depth logit tensors of shape
[batch, sequence, vocab], or [1, tokens, vocab] for a
flattened THD-packed stream.
Optional precomputed target tensors, one per
MTP depth, with the same local shape and token layout as labels.
Context-parallel callers must shift and boundary-mask them in global
sequence order before applying the model input shard. These targets
are authoritative: this function cannot infer or repair global
packed-sequence boundaries from a rank-local CP shard.
Original unshifted label tensor of shape [batch, sequence]
or [tokens] for a flattened THD-packed stream.
The wrapped model; used to fetch the shared LM head when the loss class needs materialized logits (non-FusedLinearCE path).
Coefficient applied to the summed per-depth CE.
Total non-ignore label-token count used for sum-reduction normalization.
Label value masked out of the CE loss for the trailing
k+1 rolled positions at depth k. Defaults to the configured
loss’s ignore index and must match it when provided.
Optional cumulative sequence lengths [num_seqs+1]
(THD-pack layout). When supplied and seq_idx is not, builds
a per-token sub-sequence index via searchsorted. Without packing
this can be omitted.
Optional per-token sub-sequence index [B, S] (or [S]).
Equality classes are what matter; absolute values can be any
ints. Takes precedence over cu_seqlens. Used to mask label
rolls whose source position lies in a different sub-sequence.
Optional LM-head weight tensor of shape [vocab, hidden].
Supplying it lets the main loss and all MTP depths share one DTensor
full_tensor() gather on the FusedLinearCrossEntropy path.
Group that contributes independent loss shards when the shared LM-head weight is a DTensor.
Optional per-token objective multipliers matching
labels.shape.
Return the aggregate loss together with the unscaled
loss for each MTP depth. Defaults to False to preserve the
scalar return expected by existing callers.
Returns: torch.Tensor | MTPLossOutput
Scalar MTP loss tensor. When return_per_depth=True, returns an