nemo_automodel.components.loss.chunked_ce
nemo_automodel.components.loss.chunked_ce
Module Contents
Classes
Functions
API
Bases: LinearCrossEntropy
Chunk the LM-head projection and CE, recomputing each chunk in backward.
Unlike a logits-based loss, this consumes final hidden states and the projection weight. Neither full logits nor a full logits gradient is allocated. Only one chunk’s vocabulary activations are live at a time. Higher-order gradients are not supported.
Compute CE directly from hidden states, with no full-logit allocation.
Parameters:
Rank-local states shaped […, hidden], with arbitrary leading token dimensions, including packed [tokens, hidden].
Target indices shaped […] matching the token dimensions, or [tokens] for packed hidden states shaped [1, tokens, hidden].
Weight shaped [vocab, hidden]; FSDP DTensors use the LinearCrossEntropy.materialize_lm_weight contract. TP-sharded vocabulary/hidden states are not supported by this loss.
Global count normalizing a sum-reduced loss.
DP/CP group contributing independent token losses.
Optional constant multipliers shaped […], sum only.
Optional mask shaped […]; zero positions are ignored.
Optional output cast after projection, matching the owning model. Projection uses lm_weight.dtype; mixed-dtype hidden states are converted per chunk, never the full weight.
Returns: torch.Tensor
FP32 scalar, or losses shaped […] for reduction=“none”. Inputs
Materialize one head in its effective projection dtype for all losses.
Parameters:
Plain linear head with weight of global shape [vocab, hidden]. Its FSDP policy, when present, owns the compute dtype.
Owning model config, used to reject post-projection transforms.
Group contributing independent token losses.
Returns: torch.Tensor
Dense [vocab, hidden] weight under materialize_lm_weight’s
Validate the head and the owning model’s output-logit configuration.
A linear module alone does not rule out post-projection transformations. Read the text configuration through the HF config contract, including multimodal models whose output settings live in a nested text config.
Parameters:
Output head with weight of global shape [vocab, hidden].
Owning model’s configuration, when available.
Returns: nn.Linear
The validated linear head, unchanged.
Bases: Function
Recompute one projection/CE graph at a time.
Parameters:
Context holding [tokens, hidden] states, [vocab, hidden] weight and [tokens] labels.
Upstream loss gradients shaped [tokens].
Returns: torch.Tensor | None
Hidden-state and weight gradients matching the original shapes and
Compute chunk losses without retaining logits.
Parameters:
Autograd context.
Local hidden states shaped [tokens, hidden].
Dense projection weight shaped [vocab, hidden].
Target indices shaped [tokens].
Maximum token rows per projection.
Ignored target value.
Optional output cast matching the model projection.
Eager or compiled per-chunk loss.
Returns: torch.Tensor
FP32 losses shaped [tokens]; inputs are not mutated.
Project one token chunk and compute CE.
Parameters:
Local hidden states shaped [chunk_tokens, hidden].
Dense LM-head weight shaped [vocab, hidden].
Target indices shaped [chunk_tokens].
Ignored target value.
Optional model-owned output dtype after projection.
Returns: torch.Tensor
FP32 losses shaped [chunk_tokens].
Validate that chunk_len is positive.
Compute fp32 CE.
Parameters:
Scores shaped [tokens, vocab].
Class indices shaped [tokens].
Ignored target value.
PyTorch CE reduction.
Returns: torch.Tensor
Scalar loss, or [tokens] losses for reduction=“none”.