Skip to content
mlmentorship

Z-loss

Z-loss penalizes the squared softmax log-partition value. It limits logit growth and can improve stability during long training runs or cooldowns.

Published · 5 min read ·Core ·Foundation

Summary

Z-loss adds a term proportional to to the cross-entropy objective, where is the softmax partition function over the vocabulary and are the pre-softmax logits.

A typical coefficient is . The term encourages to stay near zero, anchoring the common offset that cross-entropy leaves unconstrained. It does not bound every logit difference.

Two reasons, in order of how often they come up:

  1. Stability. Without z-loss, large LLM training runs occasionally see the lm_head logits drift to very large magnitudes, which can cause numerical issues in the softmax (especially in BF16 or FP8). PaLM and DeepSeek both used z-loss for this.
  2. A controlled cooldown intervention. The Marin 8B retrospective reports a successful intervention in its recipe. Excluding normalization parameters from weight decay is an implementation choice, not proof that no other control exists. Z-loss anchors the common-offset direction; it does not bound every parameter norm or logit difference.

The mechanism

The cross-entropy loss for a single token with target is:

Note that is invariant to adding a constant to all logits (because shifts by the same amount that does). That invariance means cross-entropy alone provides no gradient pressure on the common offset of the logits. The model can drift the entire logit vector up or down without changing the loss.

Learning objective

A shared logit shift leaves probabilities unchanged but changes z-loss

Shift every logit: same probabilities, different normalizerAdding 2 to all logits multiplies the softmax denominator by exp(2). Softmax probabilities are unchanged, but log-normalizer-based z-loss changes.Raw mass changes; normalized sharesrepeatOriginal logits zero and oneLog normalizer 1.313Add two to both logits: multiply rawmass by exp(2)Log normalizer 3.313Both normalize to 0.269 and 0.731.
Derivation

SAME PREDICTION, FREE OFFSET

Logits: [0, 1]

Probabilities: [0.269, 0.731]

Target CE: 0.313

log Z: log(1 + e) = 1.313

SHIFT BOTH BY -1.313

Logits: [-1.313, -0.313]

Probabilities: [0.269, 0.731]

Target CE: 0.313

log Z: 0, so z-loss is 0

Read it this way: compare the rows across panels. Subtracting the same constant from every logit preserves their differences, so softmax and cross-entropy do not notice. The partition value does notice: z-loss prefers the equivalent representative whose log Z is near zero. It anchors a shared offset; it does not force all logits to be equal or erase the model's probability distribution. Original calculation checked against the PaLM training objective.
Check: Why can z-loss change while softmax probabilities and target cross-entropy stay exactly the same under a common shift?
Read it this way: Adding 2 to all logits multiplies the softmax denominator by exp(2). Softmax probabilities are unchanged, but log-normalizer-based z-loss changes.
Which quantity changes even when every softmax probability stays fixed?

Adding breaks the invariance. The gradient with respect to a logit becomes:

The second term pulls toward zero, anchoring the otherwise free common offset. The pressure is small per-step but persistent.

When to use it

Consider a measured ablation when diagnostics indicate a relevant failure mode:

  • Track log-partition values, logit differences, finite-value failures, and held-out task behavior rather than choosing from model size alone.
  • Treat Marin’s cooldown result and coefficient as a source-specific example to reproduce or test, not a universal prescription.
  • Compare the actual low-precision implementation and a recoverable control run before attributing an instability to the common-offset direction.

Short fine-tuning and large pretraining both need a workload-specific decision; neither label settles whether the extra objective helps.

How to set the coefficient

A coefficient such as is a starting point for an ablation, not a scale-free guarantee of a small penalty. For that coefficient, contributes 0.0004, while contributes one. State token/batch reduction and log base: natural-log cross-entropy is measured in nats, not bits. The cross-entropy lesson separates the probability objective from added regularizers.

If you set much larger, the model spends capacity matching instead of fitting the data and downstream perplexity worsens. If you set it much smaller, you lose the regularization effect.

Subtle effects worth knowing

Subsequent Marin experiments showed that z-loss doesn’t just prevent logit blowup. In the steady state, z-loss increases the weight norm of the lm_head and decreases the scale of the final layer norm. This makes sense in retrospect: the final layer norm has a disproportionate impact on logit scale (one parameter affecting all logits), and it’s the only player not constrained by weight decay. Z-loss redistributes the pressure.

What an interviewer expects you to say

If asked about z-loss:

  1. State that it’s a penalty on added to cross-entropy.
  2. Explain that cross-entropy is invariant to adding a constant to all logits, so it provides no gradient on absolute logit scale. Z-loss breaks that invariance.
  3. Mention both use cases: stability against logit blowup and regularization on logit scale during deep cooldowns.
  4. Inspect the actual decay groups and precision policy; explain why a source-specific intervention is not a bound on every logit or parameter.

Further reading