nemo_automodel.components.loss.chunked_ce
nemo_automodel.components.loss.chunked_ce
Module Contents
Classes
Functions
Data
_compiled_compute_cross_entropy
API
Bases: Module
Cross-entropy loss computed over sequence chunks.
Computes cross-entropy loss in chunks to handle long sequences more efficiently.
Parameters:
Model output tensor of shape […, vocab], with arbitrary leading token dimensions.
Target indices of shape […] matching logits.shape[:-1].
When mask is provided, ignored positions are replaced with
ignore_index in this tensor.
Optional tensor of shape […] matching labels. Nonzero
positions contribute to the loss and zero positions are ignored.
Optional global count used to normalize the sum-reduced scalar loss.
Returns: torch.Tensor
Scalar tensor containing the reduced cross-entropy loss.
Bases: Function
Sum-reduced cross-entropy with recomputed fp32 chunk activations.
Recompute the fp32 softmax chunks and return the logits gradient.
Parameters:
Autograd context containing the original-dtype tensors saved by
:meth:forward.
Scalar tensor containing the upstream loss gradient.
Returns: torch.Tensor
Tuple whose first element is the logits gradient of shape [tokens,
Compute sum-reduced cross-entropy one fp32 chunk at a time.
Parameters:
Autograd context used to save tensors for backward.
Prediction scores of shape [tokens, vocab]. The tensor stays in its original floating-point dtype and is not mutated.
Target class indices of shape [tokens]. Positions equal to
ignore_index contribute zero loss and gradient.
Target value excluded from the loss.
Maximum number of token rows upcast to fp32 at once.
Returns: torch.Tensor
Scalar fp32 tensor containing the sum-reduced loss.
Validate that chunk_len is positive.
Computes the cross-entropy loss between logits and targets.
Parameters:
Model predictions of shape (sequence_length, num_classes).
Ground-truth labels of shape (sequence_length,).
Target value that is ignored when computing the loss. Defaults to -100.
Returns:
torch.Tensor: The sum of cross-entropy losses over the sequence.