Beyond Average Flatness: Domain-wise Flatness for Domain Generalization

Seungjun Choi, Heeyoung KimNeurIPS 2026

TL;DR

When training neural networks to generalize across different environments, seeking "flat" regions in the average loss landscape is a popular trick—but averaging can play geometric tricks on you. Two steep, perpendicular valleys can average out to look like a gentle bowl, leaving your model brittle on individual domains. This paper exposes that geometric illusion, uncovers a sneaky optimization "shortcut" when trying to fix it, and introduces Cancellation Gap-Aware Minimization (CGM) to achieve true, balanced flatness across all domains.

What Problem Does This Paper Address?

Imagine training a vision model on photos, cartoons, and sketches, hoping it will generalize to oil paintings it has never seen. This is Domain Generalization (DG). A widely celebrated tool here is Sharpness-Aware Minimization (SAM), an optimizer that searches for parameters where the loss isn't just low, but flat—meaning small nudges to the weights don't cause the loss to spike.

Standard practice in DG is AvSAM: average the losses across all training domains first, then apply SAM to flatten that average landscape.

The authors show that AvSAM suffers from a fundamental geometric blind spot: sharpness cancellation. Imagine Domain 1’s loss surface is a steep canyon running North-South, and Domain 2’s is a steep canyon running East-West. Mathematically, their steepest curvature directions (dominant Hessian eigenvectors) are orthogonal. When you average them, the steep walls partially cancel out, creating a wider, gentler-looking bowl.

Figure 1: Illustration of sharpness cancellation under misaligned domain-wise dominant Hessian eigenvectors. Around a joint local minimizer θ⋆, the dominant eigenvectors of two source domains are orthogonal (v1 ⊥v2). Although each domain loss is sharp within the ρ-neighborhood along its dominant direction, averaging the losses can cancel these domain-specific worst-case increases, making the average loss appear relatively flat. Black bold contours indicate equal loss levels.

Because AvSAM only sees the average, it actively favors misaligned solutions where domains hide each other's sharpness. You get a model that looks flat on average, yet remains dangerously sharp—and brittle—on every single domain.

Methods

To stop domains from masking each other's steepness, the authors build their solution in two steps:

  1. Individual-domain SAM (InSAM): Instead of penalizing the sharpness of the average loss, InSAM computes the worst-case sharpness for each domain individually and averages those penalties. Mathematically, InSAM equals AvSAM plus a non-negative residual term the authors dub the Cancellation Gap—the exact amount of sharpness hidden by averaging.
  2. Cancellation Gap-Aware Minimization (CGM): Minimizing InSAM alone opens up a "bad shortcut." The optimizer can shrink the Cancellation Gap without aligning the domains simply by letting one domain stay wildly sharp while making the others ultra-flat (so the single sharp domain dominates the average). To block this degeneracy, CGM adds a squared coefficient-of-variation ($\mathrm{CV}^2$) regularizer over the domain-wise sharpness values.

During training (after a brief InSAM warm-up), CGM dynamically interpolates between standard gradients and perturbed SAM gradients for each domain: sharper-than-average domains get full SAM flattening, while flatter domains rely more on standard gradients, keeping cross-domain sharpness balanced.

Datasets

The authors evaluate their approach using the standard DomainBed suite across five image classification benchmarks (no new datasets are introduced):

  • PACS (9,991 images, 4 domains like photo/sketch)
  • VLCS (10,729 images, 4 domains)
  • OfficeHome (15,588 images, 4 domains)
  • TerraIncognita (24,788 camera-trap images, 4 domains)
  • DomainNet (586,575 images, 6 domains, 345 classes)

Key Insights and Ideas

  • AvSAM actively biases against alignment: Stronger alignment between domain curvatures creates a higher mathematical floor on average-loss sharpness. Because AvSAM tries to minimize average sharpness, it implicitly prefers misaligned domains!
  • The Cancellation Gap measures geometric harmony: Under a local quadratic view, the Cancellation Gap hits zero if and only if the steepest curvature directions across all domains point along the exact same line (up to sign).
  • Optimization finds lazy loopholes: Giving an optimizer a gap to close doesn't mean it will close it the way you want. Discovering that InSAM cheats via sharpness dominance—skewing sharpness onto one domain rather than aligning eigenvectors—is a classic lesson in loss-landscape mechanics.

Key Results

Across the five DomainBed benchmarks (using a ResNet-50 backbone and leave-one-domain-out validation), CGM achieves the highest overall average accuracy at 66.4%.

Table 1: We report the average accuracy over leave-one-domain-out splits (mean ± standard error over 3 seeds). Results marked with ∗are obtained from our own experiments, while those marked with † and ‡ are taken from [17] and [18], respectively. Other baseline results are taken from the original papers. The extended table including additional baselines is provided in Appendix C.5.
  • Step-by-step gains: Moving from AvSAM (65.1%) to InSAM (65.8%) yields a +0.7% boost across all benchmarks, and adding CGM's $\mathrm{CV}^2$ regularizer pushes it another +0.6% to 66.4%, beating strong SAM-based baselines like SAGM (66.1%) and DISAM (65.9%).
  • Geometry confirmed: Diagnostics on PACS show CGM drops average domain-wise sharpness from 0.040 (AvSAM) to 0.030, suppresses InSAM's sharpness dominance spike (from 0.713 back down to 0.619), and boosts spectral alignment from 0.366 to 0.412.
  • Free memory savings: Because CGM processes domain sub-batches sequentially rather than all at once, it takes only 1.05–1.07$\times$ the update time of AvSAM while slashing peak GPU memory (from 40.8 GiB down to 5.86 GiB when simulating 15 domains).

Limitations and Future Work

CGM's $\mathrm{CV}^2$ regularizer treats all source domains symmetrically, pushing them toward equal sharpness. While sensible when you know nothing about the domains ahead of time, some domains are naturally noisier, harder, or more important than others. Enforcing strict equality might overconstrain learning in asymmetric setups. Future work could explore priority-aware variants that allow domain-dependent sharpness targets.

Summary updated

Abstract

Sharpness-Aware Minimization (SAM) is widely used in domain generalization to promote flat minima that generalize to unseen distributions. Existing SAM-based methods typically penalize the sharpness of the *average* loss across domains. We show that this prevailing formulation—termed AvSAM—has a fundamental geometric limitation: it can favor solutions with misaligned domain-wise dominant Hessian eigenvectors. As a result, such solutions may appear flat on average while remaining sharp within individual domains. To address this limitation, we propose an alternative objective that penalizes the average *domain-wise* sharpness. We show that this objective decomposes into the AvSAM objective plus a non-negative residual term, which we call the *Cancellation Gap*. This gap is minimized if and only if the domain-wise dominant eigenvectors are aligned up to sign, indicating that it mitigates AvSAM's tendency to favor misaligned solutions. However, we further uncover a degeneracy: the Cancellation Gap can become small without improving alignment when sharpness is highly imbalanced across domains. To mitigate this failure mode, we propose Cancellation Gap Minimization (CGM), which augments the objective with a squared coefficient-of-variation ($\mathrm{CV}^2$) regularizer to discourage sharpness imbalance. Experiments show that CGM achieves the best average performance across standard domain generalization benchmarks among strong baselines. Diagnostic analyses further confirm reduced domain-wise sharpness and improved alignment of dominant Hessian eigenvectors.
BibTeX
@inproceedings{choi2026beyond,
  author = {Seungjun Choi and Heeyoung Kim},
  title = {{Beyond Average Flatness: Domain-wise Flatness for Domain Generalization}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=hlSHsj1M7I},
}
Figure