During the pre-training of modern large language models, few operational failures are as costly as loss spikes. When training clusters containing thousands of GPUs run for weeks across trillions of tokens, a sudden, discontinuous surge in cross-entropy loss can corrupt optimizer momentum buffers, induce numerical overflow in half-precision representations, and permanently degrade downstream model capabilities. In severe cases, models experience catastrophic divergence, forcing engineering teams to roll back checkpoints, alter data ordering, or discard days of compute.
Historically, frontier engineering teams treated loss spikes as transient anomalies to be mitigated with manual heuristics: rolling back model weights by 100 to 200 steps, skipping the offending batch of training tokens, and resuming with modified learning rates. However, empirical investigations across architectures such as OPT, PaLM, GLM, Gemma, and Chameleon have revealed that loss spikes are deterministic consequences of specific mathematical and architectural vulnerabilities in deep Transformers.
Understanding the root mechanisms behind training instability (including attention logit explosion, vocabulary logit drift, embedding gradient shocks, and loss landscape geometry) has enabled the development of principled architectural safeguards: z-loss regularization, QK-normalization, attention logit soft-capping, and refined normalization schedules.
Anatomy of a Loss Spike
A loss spike is characterized by a rapid, non-linear increase in the training loss over a small number of optimization steps, typically accompanied by an explosive increase in the global gradient norm:
In adaptive optimizers such as AdamW, the update rule depends on the exponentially decaying moving averages of past gradients () and squared gradients ():
When an extreme gradient enters these buffers, the first moment absorbs a massive shock that persists across hundreds of subsequent optimization steps, even if subsequent batch gradients return to normal levels. Furthermore, the second moment scales up, causing the effective step size for those parameters to contract unpredictably before decaying back to baseline.
Training Step ->
Loss Trajectory:
2.80 |----------------------\
2.60 | \
2.40 | \___________
2.20 | \
2.00 |_________/\/\________________________\__/\_ (Normal decay)
-------------------------------------------
Step 42,100: Normal loss ~ 1.95, ||g||_2 ~ 0.8
Step 42,105: Anomaly occurs, ||g||_2 jumps to 48.5
Step 42,108: Loss spikes to 2.85 (Catastrophic spike)
Step 42,200: Loss recovers to 2.15 (Permanent perplexity degradation)Loss spikes fall into two primary categories:
- Recoverable Transient Spikes: The loss jumps sharply but gradually recovers toward the baseline trajectory over dozens or hundreds of steps. While training continues, the post-recovery loss curve often settles at a slightly worse perplexity asymptote than an uncorrupted run.
- Unrecoverable Divergent Spikes: The loss jumps toward near-random initialization values (e.g., ), output activations produce
NaNorInfvalues under 16-bit precision, and gradient norms reach machine limits, destroying model weights irrecoverably.
Mathematical and Structural Root Causes
Loss spikes do not occur at random. They stem from interactions between multi-head self-attention mechanics, loss landscape geometry, vocabulary projection layers, and low-precision floating-point formats.
1. Attention Logit Growth and Entropy Collapse
In standard scaled dot-product attention, queries and keys are projected from token representations :
During training, as parameter norms and grow, the dot products can grow arbitrarily large. Although the scaling factor controls variance under unit-variance initialization, it does not constrain unbounded growth during hundreds of thousands of optimizer updates.
When maximum attention logits exceed values such as 50 or 100:
- The softmax function saturates, collapsing into a sharp, near-one-hot probability distribution where one token receives and all other tokens receive .
- The entropy of the attention distribution collapses to zero.
- The derivative of softmax with respect to its input approaches zero for saturated tokens (), causing gradient vanishing across most contextual paths, while the attended token experiences sharp gradient spikes that propagate backward into query and key projection matrices.
2. Output Vocabulary Logit Drift and the Softmax Partition Function
At the final layer of a language model, hidden states are projected to vocabulary logits via the unembedding matrix :
The cross-entropy loss for target token is defined as:
Where is the partition function.
Because cross-entropy is shift-invariant (adding a constant to all logits leaves unchanged), the optimization objective does not penalize the absolute magnitude of logits . Consequently, during long training runs, average logit values can drift toward large positive or negative magnitudes (e.g., or ).
When logits drift excessively:
- In FP16 format (maximum representable value ), overflows for unless strict subtractive normalization ( subtraction) is executed.
- Even with log-sum-exp stabilization, intermediate activations in backward passes and gradient computation buffers suffer severe numerical precision loss, causing sudden gradient spikes.
3. The Slingshot Effect and the Edge of Stability
Optimization dynamics research (Cohen et al., 2021) demonstrated that gradient descent with adaptive optimizers operates at the "Edge of Stability."
Let represent the Hessian matrix of the loss function, and denote its maximum eigenvalue (the maximum spectral curvature). In standard convex optimization, stability requires:
In deep neural networks trained with AdamW, the model parameters often traverse narrow, highly curved ravines. When the effective step size approaches or exceeds the curvature boundary (), the optimizer oscillates violently across the ravine walls. This "slingshot mechanism" propels the parameter trajectory out of the narrow valley into a high-loss region, generating an instantaneous loss spike before the optimizer can re-establish curvature equilibrium.
4. Embedding Gradient Shocks
In standard Transformer tokenizers with vocabularies ranging from 32,000 to 256,000 tokens, token frequency follows a power-law distribution. Rare tokens (e.g., specialized code syntax, multi-byte Unicode combinations, non-English scripts) appear infrequently in training batches.
When a rare token suddenly appears in a batch with a high prediction error, the gradient with respect to its specific embedding row can be several orders of magnitude larger than the average token gradient. Because embedding updates are sparse, this sudden large update creates an unnormalized perturbation that propagates through all downstream transformer blocks via residual connections.
Architectural Solutions and Stabilization Techniques

Modern foundation model architectures deploy multiple complementary techniques to bound activations, constrain logits, and stabilize gradient flow.
1. z-Loss Regularization (PaLM)
Introduced during the training of the 540-billion-parameter Pathways Language Model (PaLM, Chowdhery et al., 2022), z-loss addresses output vocabulary logit drift by adding an auxiliary loss term that directly penalizes large values of the log partition function :
The total training loss becomes:
Where is typically set to ().
Mathematical Effect of z-Loss
Taking the derivative of with respect to logit :
When logits drift positive such that (), pushes all logits downward proportional to their softmax probability . If logits become excessively negative such that (), the penalty pushes logits upward. This constrains the log partition function near zero (), preventing logit drift without requiring artificial logit clipping.
import torch
import torch.nn as nn
import torch.nn.functional as F
class CrossEntropyWithZLoss(nn.Module):
def __init__(self, z_loss_weight: float = 1e-4, eps: float = 1e-8):
super().__init__()
self.z_loss_weight = z_loss_weight
self.eps = eps
def forward(self, logits: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
# logits shape: [batch_size * seq_len, vocab_size]
# targets shape: [batch_size * seq_len]
# Log-sum-exp: ln(Z)
log_z = torch.logsumexp(logits, dim=-1)
# Standard Cross-Entropy: -z_y + ln(Z)
target_logits = logits.gather(dim=-1, index=targets.unsqueeze(-1)).squeeze(-1)
ce_loss = log_z - target_logits
# Auxiliary z-loss penalty: alpha * (ln Z)^2
z_loss = self.z_loss_weight * (log_z ** 2)
return (ce_loss + z_loss).mean()2. QK-Normalization (Scaling ViT, Chameleon, Gemma 3)
QK-normalization (Dehghani et al., 2023) directly prevents attention logit explosion by applying Layer Normalization or RMSNorm to the query () and key () tensors before calculating attention dot products.
In standard attention:
In QK-normalized attention:
Where RMSNorm scales vectors to unit root-mean-square magnitude across the head dimension :
Mathematical Bound on Attention Scores
When queries and keys are unit-normalized (without learnable gain , or with initialized gain ):
The dot product is bounded by the Cauchy-Schwarz inequality:
Therefore, the scaled attention logit is bounded:
For a standard head dimension , the maximum possible pre-softmax logit is strictly bounded to , completely preventing attention logits from reaching saturation regimes ().
class QKNormAttention(nn.Module):
def __init__(self, d_model: int, num_heads: int, head_dim: int):
super().__init__()
self.num_heads = num_heads
self.head_dim = head_dim
self.scale = 1.0 / (head_dim ** 0.5)
self.q_proj = nn.Linear(d_model, num_heads * head_dim, bias=False)
self.k_proj = nn.Linear(d_model, num_heads * head_dim, bias=False)
self.v_proj = nn.Linear(d_model, num_heads * head_dim, bias=False)
self.out_proj = nn.Linear(num_heads * head_dim, d_model, bias=False)
# Per-head RMSNorm for Q and K
self.q_norm = nn.RMSNorm(head_dim, eps=1e-6)
self.k_norm = nn.RMSNorm(head_dim, eps=1e-6)
def forward(self, x: torch.Tensor, mask: torch.Tensor = None) -> torch.Tensor:
B, N, _ = x.shape
# Project and reshape: [B, num_heads, N, head_dim]
q = self.q_proj(x).view(B, N, self.num_heads, self.head_dim).transpose(1, 2)
k = self.k_proj(x).view(B, N, self.num_heads, self.head_dim).transpose(1, 2)
v = self.v_proj(x).view(B, N, self.num_heads, self.head_dim).transpose(1, 2)
# Apply QK-Norm across head_dim
q = self.q_norm(q)
k = self.k_norm(k)
# Compute bounded attention
scores = torch.matmul(q, k.transpose(-2, -1)) * self.scale
if mask is not None:
scores = scores.masked_fill(mask == 0, float('-inf'))
attn_weights = F.softmax(scores, dim=-1)
out = torch.matmul(attn_weights, v)
out = out.transpose(1, 2).contiguous().view(B, N, -1)
return self.out_proj(out)QK-normalization is fully compatible with optimized attention kernels like FlashAttention-2 and FlashAttention-3 because normalization occurs before the fused kernel execution.
3. Logit Soft-Capping (Gemma 2, Grok)
Logit soft-capping (Gemma Team, 2024) restricts the range of pre-softmax attention logits and final vocabulary logits using a smooth hyperbolic tangent () function:
Where:
- for attention matrix logits.
- for final vocabulary prediction logits.
Derivative and Clamping Behavior
As , , ensuring .
The derivative remains strictly non-zero across the entire domain:
Unlike hard clipping (), which zeros gradients when values exceed thresholds, soft-capping preserves continuous gradient backpropagation while preventing numerical overflow.
def soft_cap(logits: torch.Tensor, cap_value: float = 30.0) -> torch.Tensor:
"""Smooth hyperbolic tangent logit soft-capping."""
return cap_value * torch.tanh(logits / cap_value)While soft-capping provides stability during pre-training, standard implementations require customized CUDA kernels to prevent materializing un-fused intermediate tensors during self-attention computation.
Normalization Placement: Pre-LN vs. Sandwich-LN vs. DeepNorm
The placement of normalization layers governs how residual variance scales with network depth .
Post-LN (Vaswani et al., 2017):
x_{l+1} = LayerNorm(x_l + SubLayer(x_l))
-> Unstable: Gradients at output layers are magnitude O(1), but vanish at early layers O(1/sqrt(L)).
Pre-LN (Xiong et al., 2020):
x_{l+1} = x_l + SubLayer(LayerNorm(x_l))
-> Stable initialization: Gradients flow cleanly via skip connections.
-> Drawback: Residual norm grows as O(sqrt(L)), making later layers contribute proportionally less.
Sandwich-LN (CogView, GLM):
x_{l+1} = x_l + LayerNorm(SubLayer(LayerNorm(x_l)))
-> Prevents activation explosions inside sublayers.
-> Risk: Can suppress gradient backpropagation in architectures deeper than 60 layers.
DeepNorm (Wang et al., 2022):
x_{l+1} = LayerNorm(x_l * alpha + SubLayer(x_l))
Where alpha = (2L)^{1/4}, and sublayer weights are scaled by beta = (8L)^{-1/4}.
-> Retains Post-LN performance while matching Pre-LN training stability up to 1,000 layers.Embedding Gradient Shrinking and Adaptive Clipping
To prevent rare-token embedding updates from destabilizing the residual stream, GLM (Zeng et al., 2022) introduced embedding gradient shrinking:
Where . This dampens updates to the embedding matrix without slowing the learning rate of internal transformer weights.
Additionally, global gradient clipping scales the collective gradient vector if its norm exceeds a threshold (typically 1.0):
Comparison of Stability Mechanisms
Comparing Key Stabilization Techniques
- z-Loss Regularization: Applied at the output unembedding layer. Targets vocabulary logit drift and partition function overflow. Adds negligible computational overhead () and is fully compatible with FlashAttention. Adopted in PaLM, Chameleon, and DeepSeek-V3.
- QK-Normalization: Applied to query and key projection vectors inside every attention head. Targets attention logit explosion and entropy collapse. Adds less than computational overhead and integrates directly with FlashAttention kernels. Adopted in ViT-22B, Gemma 3, and Chameleon.
- Attention and Output Logit Soft-Capping: Applied to pre-softmax attention matrices and final vocabulary projections. Targets logit magnitude growth and output overconfidence. Requires custom fused kernels to avoid memory overhead during attention computation. Adopted in Gemma 2 and Grok-1.
- Sandwich Normalization: Applied to sublayer outputs before residual additions. Targets intermediate activation blowups. Adds minimal overhead () but carries risks of gradient vanishing in networks exceeding 60 layers. Adopted in CogView and GLM-130B.
- DeepNorm: Applied to residual stream connections via dynamic weight scaling. Targets post-LN gradient explosion. Adds zero computational overhead at runtime and is fully compatible with all attention engines. Adopted in DeepNet and GLM-4.
- Embedding Gradient Shrinking: Applied to input embedding lookup tensors. Targets rare-token gradient shocks and sudden residual perturbations. Adds zero runtime overhead. Adopted in GLM and ChatGLM.
Operational Diagnostics and Anomaly Detection
In production training runs, automated telemetry systems track early indicators of instability 50 to 500 steps before a full loss spike occurs:
- Attention Entropy Monitoring:
A sudden drop in average attention entropy across any single head indicates logit concentration and impending saturation.
- Per-Layer Gradient Norm Ratios:
Tracking highlights whether gradient energy is concentrating anomalously in the initial embeddings or final classification heads.
- Maximum Logit Tracking:
Logging across attention heads and on the output layer catches logit drift before it triggers half-precision exponent overflow.
By combining QK-normalization on attention heads with z-loss regularization on vocabulary projections and Pre-LN/RMSNorm residual connections, modern LLM pre-training pipelines eliminate the primary architectural drivers of loss spikes, ensuring deterministic convergence across multi-trillion token runs.
Sources
- PaLM: Scaling Language Modeling with Pathways (Chowdhery et al., 2022)
- Scaling Vision Transformers to 22 Billion Parameters (Dehghani et al., 2023)
- Chameleon: Mixed-Modal Early-Fusion Foundation Models (Chameleon Team, 2024)
- Gemma 2: Improving Open Language Models at a Practical Size (Gemma Team, 2024)
- OPT: Open Pre-trained Transformer Language Models (Zhang et al., 2022)
- GLM-130B: An Open Bilingual Pre-trained Model (Zeng et al., 2022)
- DeepNet: Scaling Transformers to 1,000 Layers (Wang et al., 2022)
- Gradient Descent on Neural Networks Typically Occurs at the Edge of Stability (Cohen et al., 2021)



