Sharpness-Aware Minimization in Large Language Models: How Adversarial Weight Perturbations and Flat Minima Boost Generalization

In overparameterized deep neural networks, minimizing empirical training loss is insufficient to guarantee optimal generalization on unseen distributions. Modern deep architectures, including vision models and autoregressive Large Language Models (LLMs), operate in regimes where parameter counts far exceed training token counts, producing highly non-convex loss surfaces populated by infinite global minima. Standard optimization via Stochastic Gradient Descent (SGD) or AdamW often converges to sh

6 min
Sharpness-Aware Minimization in Large Language Models: How Adversarial Weight Perturbations and Flat Minima Boost Generalization

In overparameterized deep neural networks, minimizing empirical training loss is insufficient to guarantee optimal generalization on unseen distributions. Modern deep architectures, including vision models and autoregressive Large Language Models (LLMs), operate in regimes where parameter counts far exceed training token counts, producing highly non-convex loss surfaces populated by infinite global minima. Standard optimization via Stochastic Gradient Descent (SGD) or AdamW often converges to sharp, narrow valleys in the parameter space. While these sharp minima exhibit near-zero training loss, minor distribution shifts between training and test sets cause steep error spikes.

Sharpness-Aware Minimization (SAM), introduced by Foret et al. (ICLR 2021), reformulates gradient optimization as a min-max game. Rather than minimizing the loss value at a single point in parameter space, SAM searches for parameters situated within entire neighborhoods of uniformly low loss. By computing an adversarial weight perturbation that maximizes loss within a bounded Euclidean ball and updating weights based on the gradient at that perturbed point, SAM explicitly penalizes loss landscape sharpness. Applied to language models by Bahri, Mobahi, and Tay (ACL 2022), SAM substantially improves generalization across pre-training and downstream fine-tuning.


1. The Geometry of Generalization: Flat vs. Sharp Minima

The link between loss landscape geometry and generalization was formalized empirically by Keskar et al. (ICLR 2017), who observed that large-batch training tends to land in sharp minima with poor out-of-sample performance, whereas small-batch training benefits from stochastic noise that pushes parameters toward flatter basins.

Two-Stage Optimization Flowchart of Sharpness-Aware Minimization

Mathematically, the local geometry of the empirical loss LS(w)L_S(\mathbf{w}) around a local minimum w\mathbf{w}^* is governed by its Taylor expansion:

LS(w+δ)LS(w<em>)+LS(w</em>)Tδ+12δT2LS(w)δL_S(\mathbf{w}^* + \boldsymbol{\delta}) \approx L_S(\mathbf{w}^<em>) + \nabla L_S(\mathbf{w}^</em>)^T \boldsymbol{\delta} + \frac{1}{2} \boldsymbol{\delta}^T \nabla^2 L_S(\mathbf{w}^*) \boldsymbol{\delta}

At a local minimum, the first-order gradient $\nabla L_S(\mathbf{w}^)$ vanishes. The curvature is therefore determined entirely by the Hessian matrix $\mathbf{H} = \nabla^2 L_S(\mathbf{w}^)$:

  • Sharp Minima: Characterized by large eigenvalues λmax(H)\lambda_{\max}(\mathbf{H}) and high spectral trace Tr(H)\operatorname{Tr}(\mathbf{H}). A small parameter shift δ\boldsymbol{\delta} induced by test set distribution divergence results in a quadratic explosion of the loss 12δTHδ\frac{1}{2} \boldsymbol{\delta}^T \mathbf{H} \boldsymbol{\delta}.
  • Flat Minima: Characterized by small spectral norm H2\|\mathbf{H}\|_2 and low trace. Even if the evaluation distribution slightly shifts the optimal parameters, the loss remains stable within the wide basin.

2. PAC-Bayesian Generalization Bounds and the SAM Objective

Foret et al. grounded SAM in PAC-Bayesian generalization bounds. For any parameter distribution Q\mathcal{Q} and prior P\mathcal{P}, the expected population risk LD(w)L_{\mathcal{D}}(\mathbf{w}) is bounded with probability 1δ1 - \delta over the training set SDnS \sim \mathcal{D}^n by:

EuN(0,σ2I)[LD(w+u)]maxϵ2ρLS(w+ϵ)+h(w22ρ2)\mathbb{E}_{\mathbf{u} \sim \mathcal{N}(0, \sigma^2 \mathbf{I})}[L_{\mathcal{D}}(\mathbf{w} + \mathbf{u})] \le \max_{\|\boldsymbol{\epsilon}\|_2 \le \rho} L_S(\mathbf{w} + \boldsymbol{\epsilon}) + h\left(\frac{\|\mathbf{w}\|_2^2}{\rho^2}\right)

where ρ>0\rho > 0 is the perturbation radius and h()h(\cdot) is a strictly monotonic regularization function.

To minimize this upper bound, SAM optimizes the perturbed loss objective:

minwLSSAM(w)minw[maxϵ2ρLS(w+ϵ)]\min_{\mathbf{w}} L_S^{\text{SAM}}(\mathbf{w}) \triangleq \min_{\mathbf{w}} \left[ \max_{\|\boldsymbol{\epsilon}\|_2 \le \rho} L_S(\mathbf{w} + \boldsymbol{\epsilon}) \right]

This objective decomposes directly into standard empirical risk plus a sharpness penalty term:

LSSAM(w)=LS(w)+[maxϵ2ρLS(w+ϵ)LS(w)]Sharpnessρ(w)L_S^{\text{SAM}}(\mathbf{w}) = L_S(\mathbf{w}) + \underbrace{\left[ \max_{\|\boldsymbol{\epsilon}\|_2 \le \rho} L_S(\mathbf{w} + \boldsymbol{\epsilon}) - L_S(\mathbf{w}) \right]}_{\text{Sharpness}_\rho(\mathbf{w})}


3. The SAM Algorithm: Dual-Phase Min-Max Step

Directly computing the exact inner maximization $\max_{\|\boldsymbol{\epsilon}\|_2 \le \rho} L_S(\mathbf{w} + \boldsymbol{\epsilon})$ at every step is intractable. SAM solves this via a first-order Taylor approximation around w\mathbf{w}:

LS(w+ϵ)LS(w)+ϵTwLS(w)L_S(\mathbf{w} + \boldsymbol{\epsilon}) \approx L_S(\mathbf{w}) + \boldsymbol{\epsilon}^T \nabla_{\mathbf{w}} L_S(\mathbf{w})

Subject to the norm constraint ϵ2ρ\|\boldsymbol{\epsilon}\|_2 \le \rho, the linear program achieves its maximum when ϵ\boldsymbol{\epsilon} aligns parallel to the gradient vector wLS(w)\nabla_{\mathbf{w}} L_S(\mathbf{w}):

ϵ(w)=argmaxϵ2ρ{ϵTwLS(w)}=ρwLS(w)wLS(w)2\boldsymbol{\epsilon}^*(\mathbf{w}) = \arg\max_{\|\boldsymbol{\epsilon}\|_2 \le \rho} \left\{ \boldsymbol{\epsilon}^T \nabla_{\mathbf{w}} L_S(\mathbf{w}) \right\} = \rho \frac{\nabla_{\mathbf{w}} L_S(\mathbf{w})}{\|\nabla_{\mathbf{w}} L_S(\mathbf{w})\|_2}

Once the adversarial perturbation ϵ(w)\boldsymbol{\epsilon}^*(\mathbf{w}) is determined, SAM updates the original weights w\mathbf{w} using the gradient evaluated at the perturbed position:

gSAM=wLS(w+ϵ(w))\mathbf{g}_{\text{SAM}} = \nabla_{\mathbf{w}} L_S(\mathbf{w} + \boldsymbol{\epsilon}^*(\mathbf{w}))

wt+1=wtη[gSAM+λwt]\mathbf{w}_{t+1} = \mathbf{w}_t - \eta \left[ \mathbf{g}_{\text{SAM}} + \lambda \mathbf{w}_t \right]

where η\eta is the learning rate and λ\lambda is the weight decay coefficient.

Sharpness-Aware Minimization Step Execution:
1. Sample mini-batch B = {(x_i, y_i)}_{i=1}^m
2. Compute base gradient: g = (1/m) * sum(grad_w L(x_i, y_i; w_t))
3. Calculate worst-case perturbation: epsilon = rho * (g / ||g||_2)
4. Compute sharpness-penalized gradient: g_sam = (1/m) * sum(grad_w L(x_i, y_i; w_t + epsilon))
5. Apply weight update to base parameters: w_{t+1} = Optimizer_Step(w_t, g_sam)

4. Scale Invariance and Adaptive SAM (ASAM)

A theoretical limitation of standard SAM is its lack of scale invariance. In architectures containing Layer Normalization or RMSNorm, multiplying a layer's weight tensor Wl\mathbf{W}_l by a constant α>0\alpha > 0 and dividing the subsequent layer by α\alpha leaves the model's forward function identical. However, the Euclidean radius ϵ2ρ\|\boldsymbol{\epsilon}\|_2 \le \rho imposes an isotropic sphere that treats all parameters equally regardless of their magnitude.

To eliminate this artifact, Kwon et al. (ICML 2021) proposed Adaptive SAM (ASAM). ASAM defines the perturbation constraint in a parameter-scaled norm:

ϵi(w)=ρwigiwg2\boldsymbol{\epsilon}^*_i(\mathbf{w}) = \rho \frac{|w_i| g_i}{\| |\mathbf{w}| \odot \mathbf{g} \|_2}

where \odot denotes element-wise multiplication. ASAM scales the perturbation proportionally to the absolute magnitude of each individual weight, ensuring that layers with large parameter values receive proportionately larger perturbations, preserving scale invariance across Transformer blocks.


5. SAM in Transformers and Large Language Models

In natural language processing, Transformer architectures exhibit distinct loss landscape properties compared to convolutional networks. Bahri et al. (ACL 2022) conducted extensive empirical evaluations applying SAM and ASAM to T5, BERT, and RoBERTa across pre-training and downstream GLUE/SuperGLUE benchmarks.

Their findings revealed critical operational dynamics:

  • Pre-Training Loss vs. Downstream Generalization: While SAM models often show slightly higher training loss during early pre-training epochs due to the adversarial perturbation penalty, they achieve consistently lower validation perplexity and higher zero-shot/few-shot downstream transfer scores.
  • Perturbation Radius Tuning: For language models trained with AdamW, optimal perturbation radii ρ\rho are typically an order of magnitude smaller than those used in vision models (for instance, ρ[0.01,0.05]\rho \in [0.01, 0.05] for Transformers compared to ρ[0.05,0.20]\rho \in [0.05, 0.20] for ResNets).
  • Robustness to Spurious Correlations: SAM acts as an implicit regularizer against memorization, preventing attention heads from overfitting to high-frequency token co-occurrences.

6. Computational Overhead and Acceleration Techniques

The principal drawback of standard SAM is its computational cost: evaluating both LS(w)\nabla L_S(\mathbf{w}) and LS(w+ϵ)\nabla L_S(\mathbf{w} + \boldsymbol{\epsilon}) requires two sequential forward-backward passes per optimization step, effectively halving training throughput (100% compute overhead).

To mitigate this bottleneck, several accelerated variants have been developed:

  • LookSAM (Du et al., ICML 2022): Computes the full adversarial perturbation ϵ\boldsymbol{\epsilon}^* only once every kk steps (typically k=5k=5 or 1010). In intermediate steps, LookSAM reuses the gradient direction from the projection step, reducing the compute overhead from 100% down to under 15% with minimal generalization loss.
  • Surrogate Gap Minimization / GSAM (Zhuang et al., ICLR 2022): Decomposes the gradient update into two orthogonal vectors: one minimizing the empirical loss and the other minimizing the surrogate gap (LS(w+ϵ)LS(w)L_S(\mathbf{w} + \boldsymbol{\epsilon}) - L_S(\mathbf{w})), preventing gradient cancellation and speeding up convergence.
  • Micro-Batch SAM in Distributed Sharding: In FSDP and Megatron-LM tensor parallel setups, computing the global gradient norm g2\|\mathbf{g}\|_2 requires an All-Reduce across all ranks before constructing ϵ\boldsymbol{\epsilon}. Sharded implementations compute layer-local norms to avoid synchronization overhead during the ascent step.

7. Comparative Architectural Trade-Offs

  • Standard SGD / AdamW: Operates with 1 forward-backward pass per step (1x overhead). Relies solely on stochastic batch noise for implicit regularization without explicit bounds on Hessian eigenvalues. Lacks scale invariance.
  • Weight Decay (L2L_2 Regularization): Adds parameter norm penalty with 1x overhead. Provides weak indirect shrinkage without directly controlling loss surface curvature.
  • Stochastic Weight Averaging (SWA): Averages checkpoint weights along the trajectory with periodic evaluation overhead. Traverses toward the center of flat loss basins without modifying inner gradient steps. Scale invariant.
  • Sharpness-Aware Minimization (SAM): Employs adversarial minimax weight perturbation with 2x overhead. Explicitly bounds the maximum eigenvalue λmax(H)\lambda_{\max}(\mathbf{H}) of the loss Hessian. Lacks scale invariance under layer scaling.
  • Adaptive SAM (ASAM): Uses weight-magnitude-scaled perturbations with 2x overhead. Normalizes curvature bounds across layers, restoring scale invariance in normalized Transformer blocks.
  • LookSAM: Amortizes minimax perturbations every kk iterations, reducing compute overhead to approximately 1.15x while maintaining curvature regularization.

Sources

Written by

More to read

  • Function Calling Evaluation in Production: AST Matching, Executable Sandboxes, and Multi-Turn Benchmark Architecture

    Production AI systems increasingly rely on Large Language Models not merely as conversational generators, but as deterministic execution routers that select and invoke external software tools. While general-purpose LLM evaluations such as MMLU or Chatbot Arena measure semantic fluency and broad reasoning, they provide little insight into whether a model can reliably format API parameters, adhere to strict JSON schemas, or maintain consistency across multi-step execution graphs. In real-world ag

    1 min
  • Prefix-Tree KV Cache Management in Production: RadixAttention, Tree-Structured LRU Eviction, and Token-Level Sharing in SGLang and vLLM

    Prefix-Tree KV Cache Management in Production: RadixAttention, Tree-Structured LRU Eviction, and Token-Level Sharing in SGLang and vLLM Autoregressive large language model inference is heavily constrained by memory bandwidth and the computational overhead of the prefill phase. For workloads such as multi-turn conversations, autonomous agent tool loops, few-shot prompt evaluations, and tree-search decoding, consecutive requests often share substantial token prefixes. In a standard multi-turn ses

    1 min
  • Neural Collapse: How Simplex Equiangular Tight Frames Emerge at the Terminal Phase of Training

    In classification tasks, deep neural networks exhibit an unexpected geometric simplicity during late-stage optimization. While the internal activations of early training appear high-dimensional and complex, the penultimate layer representations and linear classifiers converge toward an exact, symmetrical geometric structure known as Neural Collapse (NC). First identified empirically by Papyan, Han, and Donoho (2020), Neural Collapse emerges during the Terminal Phase of Training (TPT). This regi

    1 min