---
title: Sharpness-Aware Minimization (SAM)
url: https://www.emergentmind.com/topics/sharpness-aware-minimization-sam
type: topic
---

# Sharpness-Aware Minimization (SAM)

Sharpness-Aware Minimization (SAM) is an optimization paradigm designed to improve generalization in deep learning by explicitly biasing training toward flatter minima in the parameter space. SAM modifies standard update rules by searching for solutions that are robust to parameter perturbations, favoring regions where the loss remains low even when the weights are altered slightly. The method is applicable across model scales and architectures and has been shown to provide consistent performance improvements in both vision and language tasks, particularly in settings with limited data.

## 1. Core Principle and Algorithmic Formulation

SAM targets solutions that minimize not only the empirical training loss $L(w)$, but also the worst-case loss in an $\ell_2$ neighborhood of the current parameters. The optimization is expressed as a minimax problem:
\[
\min_w \max_{\|\epsilon\|_2 \leq \rho} L_{\text{train}}(w + \epsilon)
\]
where $w$ are the model parameters and $\rho$ controls the neighborhood size. Computing the exact inner maximization is intractable, so a first-order approximation is used:
\[
\hat{\epsilon}(w) = \rho \frac{\nabla_w L_{\text{train}}(w)}{\|\nabla_w L_{\text{train}}(w)\|_2}
\]
The descent update is performed at $w + \hat{\epsilon}(w)$, so the update becomes:
\[
w \leftarrow w - \eta \nabla_w L_{\text{train}}(w + \hat{\epsilon}(w))
\]
This “lookahead” mechanism encourages convergence to flatter regions: parameter neighborhoods that exhibit uniformly low loss and thus improved robustness and generalization.

## 2. Integration with Language Model Optimization

The integration of SAM with the fine-tuning of language models, including T5 and mT5 variants, is characterized by a plug-and-play approach. SAM operates as a wrapper around existing first-order optimizers without altering underlying architectures or tasks. Key technical aspects include:

- The adversarial gradient is computed at $w + \hat{\epsilon}(w)$ using only a subset (∼25%) of the batch, drastically reducing additional FLOPs compared to full-batch approaches.
- Hyperparameter $\rho$ is tuned per model size and dataset regime, with smaller values (e.g., 0.05) for smaller models and larger ones (∼0.15) for bigger models and when more training data is available.
- The method is fully compatible with standard fine-tuning schedules, regularization, dropout, and task-specific preprocessing.

This lightweight integration yields a computational overhead of roughly 25% per update, since only the adversarial step requires additional gradient computation on a sub-batch.

## 3. Experimental Protocols and Scope

SAM was benchmarked by fine-tuning pre-trained T5.1.1 and mT5 models over a diverse array of tasks:

- **Benchmarks:** SuperGLUE, GLUE (natural language understanding), WebQuestions, NaturalQuestions, TriviaQA (closed-book QA), and TyDiQA-GoldP (multilingual QA).
- **Model Scales:** Small (77M), Base (250M), Large (800M), and XL (3B) parameter models.
- **Fine-tuning schedules:** For SuperGLUE and GLUE, up to 250k update steps; for QA tasks, about 20k steps each.
- **Data regime analysis:** Performance was measured not only on full data but across a range of subsampling ratios (2%–80% of the training set).

The $\rho$ hyperparameter was optimized on a per-task and per-scale basis: for example, SAM with $\rho=0.05$ for T5-Small, and $\rho=0.15$ for T5-Base/Large/XL on English-language tasks, and smaller radii for non-English/multilingual settings.

## 4. Empirical Findings

The empirical evaluation demonstrates robust and consistent improvements:

- **SuperGLUE and GLUE:** Across all model sizes, SAM improves aggregate SuperGLUE scores (e.g., T5-Base shows a relative gain of ∼4.2%) and provides consistent gains on GLUE overall metrics.
- **Closed-Book QA Tasks:** Gains in F1 and EM scores on WebQuestions, NaturalQuestions, and TriviaQA are observed, with improvements magnified in low-data regimes.
- **Data efficiency:** In scarce-data settings (e.g., 5% of training data), improvements are even more pronounced. For instance, with only 5% of SuperGLUE data, the T5-Base model gains ∼7.2% relative in aggregate score.
- **Universality:** The improvement is not contingent on multitask transfer; SAM benefits even single-task settings, indicating the bias toward flatter minima is not strictly reliant on inter-task transfer effects.

A summary table of findings:

| Model Size | SuperGLUE Relative Gain | Robustness in Low-Data | Computational Overhead |
|------------|------------------------|------------------------|-----------------------|
| Small      | Notable                | Yes                    | ~25%                  |
| Base       | ∼4.2%                  | ∼7.2% (@ 5% data)      | ~25%                  |
| Large/XL   | Consistent             | Yes                    | ~25%                  |

Detailed tables in the study further report improvements in individual metrics (F1, EM, accuracy) per task.

## 5. Theoretical and Practical Implications

SAM introduces an explicit bias toward flatter critical points, which are less sensitive to small parameter shifts and thus better generalize to unseen data. From a practical standpoint:

- **Generalization:** Consistently superior results on standard and QA benchmarks support the link between flatness (as induced by SAM) and generalization capability.
- **Computational Trade-Off:** The method increases per-batch computation by ∼25% but this is offset by improvements in accuracy, especially in low-resource or data-scarce environments.
- **Scalability:** Performance gains are largely uniform across orders of magnitude in parameter count, indicating scalability from small to very large models.
- **Integration:** SAM as a wrapper requires minimal code modifications and is nonintrusive with respect to existing training pipelines.

## 6. Limitations and Recommendations

The primary limitation of SAM is the extra computation due to the adversarial gradient step. However, experimental evidence shows that by restricting the adversarial step to a batch fragment, the overhead is modest. Practitioners are advised to:

- Tune $\rho$ carefully; overly large perturbations can destabilize learning, while too small values may blunt the flatness bias.
- Adjust training schedules in extremely low-data regimes to fully capitalize on the regularization effect of SAM.
- Consider batch-fragmented adversarial computation for efficiency.

A plausible implication is that SAM is especially beneficial for tasks where overfitting is a concern and for large parameter models fine-tuned on limited data.

## 7. Summary

Sharpness-Aware Minimization, when applied to large language model fine-tuning, provides a systematic, scalable, and efficient means for biasing optimization toward flatter minima. This leads to reliably improved generalization on both standard and challenging language understanding tasks, particularly in data-constrained scenarios. SAM’s plug-and-play nature and modest computational cost make it suitable for a broad range of real-world model optimization pipelines, and its benefits are supported by rigorous comparative and ablation studies across tasks, scales, and data regimes [2110.08529].

Source: https://www.emergentmind.com/topics/sharpness-aware-minimization-sam