Abstract
Large Language Models (LLMs) have demonstrated exceptional performance across diverse tasks, yet their training remains highly resource-intensive and susceptible to critical challenges such as training instability. A predominant source of this instability stems from gradient and loss spikes, which disrupt the learning process, often leading to costly interventions like checkpoint recovery and experiment restarts. This paper presents a comprehensive investigation into gradient spikes observed during LLM training, revealing their prevalence across multiple architectures and datasets. Our analysis shows that these spikes can be up to $1000\times$ larger than typical gradients, substantially deteriorating model performance. To address this issue, we propose Spike-Aware Adam with Momentum Reset (SPAM), a novel optimizer designed to counteract gradient spikes through momentum reset and spike-aware gradient clipping. Extensive experiments demonstrate that SPAM consistently surpasses Adam and its variants across various tasks, including LLM pre-training from 60M to 1B and memory-efficient fine-tuning.
Introduction
Training Large Language Models (LLMs) is a notoriously finicky process. Even with massive compute budgets, researchers often find their training runs derailed by sudden "loss spikes"—moments where the model's error shoots up, potentially ruining weeks of work. Traditionally, the industry standard fix has been manual: researchers monitor logs and manually restart training from an earlier checkpoint if a spike occurs. This process is both expensive and exhausting. This paper introduces SPAM (Spike-Aware Adam with Momentum Reset), a new optimizer designed to make LLM training more stable, efficient, and resilient to these sudden shocks.
The Problem: Gradient Spikes and Momentum Poisoning
The "Edge of Stability" in LLM training is a precarious place. During training, gradients occasionally explode, reaching magnitudes up to 1000x larger than normal. These "gradient spikes" are not just transient errors; they have a lasting impact due to the momentum mechanisms in popular optimizers like Adam.
Adam keeps a running average of past gradients (the "first moment") and their squared values (the "second moment"). When a massive spike occurs, it "poisons" these averages. Because the second moment typically uses a very slow decay rate (0.999), the ghost of a single bad gradient can haunt the optimizer for hundreds of steps, leading to prolonged instability or total divergence.
Methods: The SPAM Optimizer
To address these challenges, SPAM integrates two primary mechanisms into the Adam framework:
- Momentum Reset: Every few hundred steps, the optimizer wipes its memory of the first and second moments. This "reboot" clears out any poisoned state. To prevent the reset itself from shocking the system, a short cosine warmup period is implemented after each reboot.
- Spike-Aware Clipping: Instead of traditional global norm clipping, SPAM identifies specific "spiked" parameters on-the-fly using a Gradient Spike Score (GSS). If a gradient is too large relative to its historical average, it is scaled down, preserving its directional information rather than simply zeroing it out.
- Sparse Momentum: By resetting momentum periodically, the authors realized they didn't need to track every parameter all the time. SPAM introduces a version that only updates a random subset of momentum terms, significantly reducing the memory footprint—a critical feature for training massive models on limited hardware.
Key Results and Insights
Experimental evaluations across LLaMA, Pythia, and OPT architectures show that spikes are universal, occurring regardless of dataset quality or architectural tricks like LayerNorm scaling.
In pre-training LLaMA models (from 60M to 1B parameters), SPAM consistently achieved lower perplexity than Adam, Adafactor, and Adam-mini. For a 60M model, SPAM reached a perplexity of 30.46 compared to Adam's 34.09.
Furthermore, SPAM demonstrated exceptional memory efficiency. When operating under strict memory constraints, it outperformed GaLore, a leading memory-efficient optimizer. In fine-tuning LLaMA2-7B, SPAM achieved an average score of 66.7 across downstream tasks, beating Adam's 63.4. Importantly, the computational overhead is negligible; the wall-clock time per iteration remains nearly identical to standard Adam.
Conclusion
SPAM offers a robust solution to the pervasive problem of training instability in LLMs. By combining periodic momentum resets with intelligent, spike-aware gradient clipping, it provides a more stable and memory-efficient path to scaling billion-parameter models. Future work will focus on bridging the theoretical gap for non-convex optimization and investigating the ultimate root causes of these gradient spikes.
Summary updated