kempnerforge.training.loss

Loss function registry for KempnerForge.

Registers loss functions and provides build_loss_fn() to compose them with config-driven options (chunk size, z-loss). Follows the same builder pattern as build_optimizer.

Functions

build_loss_fn(config)

Build a composed loss function from training config.

chunked_cross_entropy_loss(logits, labels[, ...])

Cross-entropy computed in token-dimension chunks.

cross_entropy_loss(logits, labels[, fp32])

Standard cross-entropy loss for language modeling.

z_loss(logits, weight[, chunk_size])

Logit magnitude regularizer (PaLM / Gemini).

kempnerforge.training.loss.cross_entropy_loss(logits, labels, fp32=False)[source]

Standard cross-entropy loss for language modeling.

Uses ignore_index=-100 so packed-sequence boundary tokens (labeled -100 by the dataset) are excluded from the loss. When no -100 labels are present, this has zero overhead. A batch with every label -100 returns an exact float32 zero that stays on the autograd graph of logits.

With fp32=True the loss and its gradient are computed in fp32 even when the logits are bf16 (train.fp32_cross_entropy).

Parameters:
Return type:

torch.Tensor

kempnerforge.training.loss.chunked_cross_entropy_loss(logits, labels, chunk_size=4096, fp32=False)[source]

Cross-entropy computed in token-dimension chunks.

Chunks along the token dimension and uses PyTorch’s fused CE kernel per chunk, avoiding an explicit float32 materialization of the full logit tensor. For Llama-3 7B (vocab=128K, batch=4, seq=4096), the manual logsumexp path would create a ~8 GB float32 copy; this implementation avoids that entirely.

Uses ignore_index=-100 so packed-sequence boundary tokens are excluded. When no -100 labels are present, this has zero overhead. A batch with every label -100, or with no rows at all ((0, vocab) or (batch, 0, vocab) logits, as a packed micro-batch with no text gives), returns an exact float32 zero that stays on the autograd graph of logits.

Note: the input logit tensor (B*S, V) is still fully materialized by the model’s output head before reaching this function. For deeper savings (never materializing the full logit tensor), the output projection itself must be chunked in the model forward pass — a future enhancement.

Parameters:
  • logits (torch.Tensor) – (batch, seq, vocab) or (tokens, vocab).

  • labels (torch.Tensor) – (batch, seq) or (tokens,).

  • chunk_size (int) – Number of tokens per chunk.

  • fp32 (bool) – Compute the loss and its gradient in fp32 from bf16 logits, still without an fp32 copy of the full logits (train.fp32_cross_entropy).

Return type:

torch.Tensor

kempnerforge.training.loss.z_loss(logits, weight, chunk_size=None)[source]

Logit magnitude regularizer (PaLM / Gemini).

Penalizes large logit magnitudes to prevent logit drift that causes NaN/divergence in long training runs. Negligible compute cost.

Formula: weight * mean(logsumexp(logits, dim=-1) ** 2)

The log-sum-exp runs in float32, chunk_size tokens at a time, and keeps only its per-token result for backward, which recomputes the softmax one chunk at a time instead of keeping a float32 copy of the logits.

Parameters:
  • logits (torch.Tensor) – Model output logits, shape (batch, seq, vocab) or (tokens, vocab). A plain tensor, not a DTensor: under TP the output head returns a local tensor (use_local_output), so a DTensor never reaches the loss.

  • weight (float) – Regularization weight (PaLM uses 1e-4).

  • chunk_size (int | None) – Tokens per float32 chunk. None takes every token at once.

Returns:

Scalar z-loss term to add to the main loss; zero on the graph for logits with no rows (a packed micro-batch without text).

Return type:

torch.Tensor

kempnerforge.training.loss.build_loss_fn(config)[source]

Build a composed loss function from training config.

Follows the build_optimizer pattern: config in, callable out. Binds chunk_size for chunked CE and fp32 when train.fp32_cross_entropy is set, and composes z-loss with the same chunk size (every token at once for unchunked CE), so the caller gets a clean (logits, labels) -> Tensor interface.

Return type:

Callable[[torch.Tensor, torch.Tensor], torch.Tensor]