AdaSAM: Boosting Sharpness-Aware Minimization with Adaptive Learning
  Rate and Momentum for Training Deep Neural Networks
Sharpness aware minimization (SAM) optimizer has been extensively explored as
it can generalize better for training deep neural networks via introducing
extra perturbation steps to flatten the landscape of deep learning models.
Integrating SAM with adaptive learning rate and momentum acceleration, dubbed
AdaSAM, has already been explored empirically to train large-scale deep neural
networks without theoretical guarantee due to the triple difficulties in
analyzing the coupled perturbation step, adaptive learning rate and momentum
step. In this paper, we try to analyze the convergence rate of AdaSAM in the
stochastic non-convex setting. We theoretically show that AdaSAM admits a
$\mathcal{O}(1/\sqrt{bT})$ convergence rate, which achieves linear speedup
property with respect to mini-batch size $b$. Specifically, to decouple the
stochastic gradient steps with the adaptive learning rate and perturbed
gradient, we introduce the delayed second-order momentum term to decompose them
to make them independent while taking an expectation during the analysis. Then
we bound them by showing the adaptive learning rate has a limited range, which
makes our analysis feasible. To the best of our knowledge, we are the first to
provide the non-trivial convergence rate of SAM with an adaptive learning rate
and momentum acceleration. At last, we conduct several experiments on several
NLP tasks, which show that AdaSAM could achieve superior performance compared
with SGD, AMSGrad, and SAM optimizers.
Hao Sun, Li Shen, Qihuang Zhong, Liang Ding, Shixiang Chen, Jingwei Sun, Jing Li, Guangzhong Sun, Dacheng Tao
AdaSAM: Boosting Sharpness-Aware Minimization with Adaptive Learning Rate and Momentum for Training Deep Neural Networks

###### Abstract

Sharpness aware minimization (SAM) optimizer has been extensively explored as it can generalize better for training deep neural networks via introducing extra perturbation steps to flatten the landscape of deep learning models. Integrating SAM with adaptive learning rate and momentum acceleration, dubbed AdaSAM, has already been explored empirically to train large-scale deep neural networks without theoretical guarantee due to the triple difficulties in analyzing the coupled perturbation step, adaptive learning rate and momentum step. In this paper, we try to analyze the convergence rate of AdaSAM in the stochastic non-convex setting. We theoretically show that AdaSAM admits a \(\mathcal{O}(1/\sqrt{bT})\) convergence rate, which achieves linear speedup property with respect to mini-batch size \(b\). Specifically, to decouple the stochastic gradient steps with the adaptive learning rate and perturbed gradient, we introduce the delayed second-order momentum term to decompose them to make them independent while taking an expectation during the analysis. Then we bound them by showing the adaptive learning rate has a limited range, which makes our analysis feasible. To the best of our knowledge, we are the first to provide the non-trivial convergence rate of SAM with an adaptive learning rate and momentum acceleration. At last, we conduct several experiments on several NLP tasks, which show that AdaSAM could achieve superior performance compared with SGD, AMSGrad, and SAM optimizers.

 Sharpness-aware minimization, Adaptive learning rate, Non-convex optimization, linear speedup.

## I Introduction

Sharpness-aware minimization (SAM) [1] is a powerful optimizer for training large-scale deep learning models by explicitly minimizing the gap between the training performance and generalization performance. It has achieved remarkable results in training various deep neural networks, including ResNet [1, 2, 3], vision transformer [4, 5], language models [6, 7, 8], on extensive benchmarks.

However, SAM-type methods suffer from several issues during training the deep neural networks, especially for huge computation costs and heavily hyper-parameter tuning procedure. In each iteration, SAM needs double gradients computation compared with classic optimizers, like SGD, Adam [9], AMSGrad [10], due to the extra perturbation step. Hence, SAM requires to forward and back propagate twice for one parameter update, resulting in one more computation cost than the classic optimizers. Moreover, as there are two steps during the training process, it needs double hyper-parameters, which makes the learning rate tuning unbearable and costly.

Adaptive learning rate optimization methods [11] scale the gradients based on the history gradient information to accelerate the convergence by tuning the learning rate automatically. These methods, such as Adagrad [12], Adam [9], and AMSGrad [10], have been proposed for solving the computer vision, natural language process, and generative neural networks tasks [11, 13, 14, 15]. Recently, several works have tried to ease the learning rate tuning in SAM by inheriting the triplet advantages of SAM, adaptive learning rate, and momentum acceleration. For example, [16] and [17] train ViT models and NLP models with adaptive learning rates and momentum acceleration, respectively. Although remarkable performance has been achieved, their convergences are still unknown since the adaptive learning rate and momentum acceleration are used in SAM. Directly analyzing its convergence is complicated and difficult due to the three coupled steps of optimization, i.e., the adaptive learning rate estimation is coupled with the momentum step and perturbation step of SAM.

In this paper, we analyze the convergence rate of SAM with an adaptive learning rate and momentum acceleration, dubbed AdaSAM, in the non-convex stochastic setting. To circumvent the difficulty in the analysis, we develop a novel technique to decouple the three-step training of SAM from the adaptive learning rate and momentum step. The analysis procedure is mainly divided into three parts. The first part is to analyze the procedure of the SAM. Then we analyze the second step that adopts the adaptive learning rate method. We introduce a second-order momentum term from the previous iteration, which is related to the adaptive learning rate and independent of SAM while taking an expectation. Then we can bound the term composed by the SAM and the previous second-order momentum due to the limited adaptive learning rate. In the last part, we analysis the momentum acceleration that is combined with the SAM and the adaptive learning rate. The momentum acceleration lead to an extra term in convergence analysis. Here, we introduce an auxiliary sequence to absorb it and show that their summation over the all iterations is controllable. We prove that AdaSAM enjoys the property of linear speedup property with respect to the batch size, i.e. \(\mathcal{O}(1/\sqrt{bT})\) where
\(b\) is the mini-batch size. Empirically, we apply AdaSAM to train RoBERTa model on the GLUE benchmark to evaluate our theoretical findings. We show that AdaSAM achieves the best performance in experiments, where it wins 6 tasks of 8 tasks, and the linear speedup can be clearly observed.

In the end, we summarize our contributions as follows:

* We present the first convergence guarantee of the adaptive SAM method with momentum acceleration under the stochastic non-convex setting. Our results suggest that a large mini-batch can help convergence due to the established linear speedup with respect to batch size.
* We conduct a series of experiments on various tasks. The results show that AdaSAM outperforms most of the state-of-art optimizers and the linear speedup is verified.

## II Preliminary and Related Work

In this section, we first describe the basic problem setup and then introduce several related works on the SAM, adaptive learning rate and momentum steps.

### _Problem Setup_

In this work, we focus on stochastic nonconvex optimization

\[\min_{x\in\mathbb{R}^{d}}f(x):=\mathbb{E}_{\xi\sim D}f_{\xi}(x), \tag{1}\]

where \(d\) is dimension of variable \(x\), \(D\) is the unknown distribution of the data samples, \(f_{\xi}(x)\) is a smooth and possibly non-convex function, and \(f_{\xi_{i}}(x)\) denotes the objective function at the sampled data point \(\xi_{i}\) according to data distribution \(D\). In machine learning, it covers empirical risk minimization as a special case and \(f\) is the loss function when the dataset \(D\) cover \(N\) data points, i.e., \(D=\{\xi_{i},i=1,2,\ldots,N\}\). Problem (1) reduces to the following finite-sum problem:

\[\min_{x\in\mathbb{R}^{d}}f(x):=\frac{1}{N}\sum_{i}f_{\xi_{i}}(x). \tag{2}\]

Notations.Without additional declaration, we represent \(f_{i}(x)\) as \(f_{\xi_{i}}(x)\) for simplification, which is the \(i\)-th loss function while \(x\in\mathbb{R}^{d}\) is the model parameter and \(d\) is the parameter dimension. We denote the \(l_{2}\) norm as \(\|\cdot\|_{2}\). A Hadamard product is denoted as \(a\odot b\) where \(a\),\(b\) are two vectors. For a vector \(a\in\mathbb{R}^{d}\), \(\sqrt{a}\) is denoted as a vector that the \(j\)-th value, \((\sqrt{a})_{(j)}\), is equal to the square root of \(a_{j}\).

### _Related Work_

Sharpness-aware minimizationMany works try to improve the generalization ability during training the deep learning model. Some methods such as dropout [18], weight decay [19], and regularization methods [20, 21] provide an explicit way to improve generalization. Previous work shows that sharp minima may lead to poor generalization whereas flat minima perform better [22, 23, 24]. Therefore, it is popular to consider sharpness to be closely related to the generalization. Sharpness-aware minimization (SAM) [1] targets to find flat minimizers explicitly by minimizing the training loss uniformly in the entire neighborhood. Specifically, SAM aims to solve the following minimax saddle point problem:

\[\min_{x}\max_{\|\delta\|\leq\rho}f(x+\delta)+\lambda\|x\|_{2}^{2}, \tag{3}\]

where \(\rho\geq 0\) and \(\lambda\geq 0\) are two hyperparameters. That is, the perturbed loss function of \(f(x)\) in a neighborhood is minimized instead of the original loss function \(f(x)\). By using Taylor expansion of \(f(x+\delta)\) with respect to \(\delta\), the inner max problem is approximately solved via

\[\delta^{*}(x) =\operatorname*{arg\,max}_{\|\delta\|\leq\rho}f(x+\delta)\] \[\approx\operatorname*{arg\,max}_{\|\delta\|\leq\rho}f(x)+\delta^ {\top}\nabla f(x)\] \[=\operatorname*{arg\,max}_{\|\delta\|\leq\rho}\delta^{\top} \nabla f(x)=\rho\frac{\nabla f(x)}{\|\nabla f(x)\|}.\]

By dropping the quadratic term, (3) is simplified as the following minimization problem

\[\min_{x}f\left(x+\rho\frac{\nabla f(x)}{\|\nabla f(x)\|}\right). \tag{4}\]

The stochastic gradient of \(f\left(x+\rho\frac{\nabla f(x)}{\|\nabla f(x)\|}\right)\) on a batch data \(b\) includes the Hessian-vector product, SAM further approximates the gradient by

\[\nabla_{x}f_{b}\left(x+\rho\frac{\nabla f_{b}(x)}{\|\nabla f_{b}(x)\|}\right) \approx\nabla_{x}f_{b}(x)\big{|}_{x+\rho\frac{\nabla f_{b}(x)}{\|\nabla f_{b }(x)\|}}.\]

Then, along the negative direction \(-\nabla_{x}f_{b}(x)\big{|}_{x+\rho\frac{\nabla f_{b}(x)}{\|\nabla f_{b}(x)\|}}\), SGD is applied to solve the surrogate minimization problem (4). It is easy to see that SAM requires twice gradient back-propagation, i.e., \(\nabla f_{b}(x)\) and \(\nabla_{x}f_{b}(x)\big{|}_{x+\rho\frac{\nabla f_{b}(x)}{\|\nabla f_{b}(x)\|}}\). Due to the existence of hyperparameter \(\rho\), one needs to carefully tune both \(\rho\) and learning rate in SAM. In practice, \(\rho\) is predefined to control the radius of the neighborhood.

Recently, Several variants of SAM are proposed to improve its performance. For example, [16, 8, 17] have empirically incorporated adaptive learning rate with SAM and shown impressive generalization accuracy, while their convergence analysis has never been studied. ESAM [25] proposes an efficient method by sparsifying the gradients to alleviate the double computation cost of backpropagation. ASAAM [17] modifies SAM by adaptively scaling the neighborhood so that the sharpness is invariant to parameters re-scaling. GSAM [16] simultaneously minimizes the perturbed function and a new defined surrogate gap function to further improve the flatness of minimizers. Liu et al. [26] also study SAM in large-batch training scenario and periodically update the perturbed gradient. Recently, [3, 8] improve the efficiency of SAM by adopting the sparse gradient perturbation technique. [27, 28] extend SAM to the federated learning setting with a significant performance gain. On the other hand, there are some works analyzing the convergence of the SAM such as [29] without considering the normalization step, i.e., the normalization in \(\frac{\nabla f_{b}(x)}{\|\nabla f_{b}(x)\|}\).
Adaptive optimizerThe adaptive optimizer can automatically adjust the learning rate based on the history gradients methods. The first adaptive method, Adagrad [12], can achieve a better result than other first-order methods under the convex setting. While training the deep neural network, Adagrad will decrease the learning rate rapidly with a degraded performance. Adadelta [30] is proposed to change this situation and introduces a learning rate based on the exponential average history gradients. Adam [9] additionally adds momentum step to stabilize the training process, and it shows great performance in many tasks. However, Reddi et al [10] give a counterexample that it cannot converge even when the objective function is convex and propose an alternative method called AMSGrad with convergence guarantee. Then, many works [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44] have been proposed to study the convergence of adaptive methods and their variants in the nonconvex setting. However, their analysis techniques can not directly extend to establish the convergence of SAM with adaptive learning rate due to the coupled perturbation step and adaptive learning rate.

Momentum accelerationMomentum methods such as Polyak's heavy ball method [45], Nestrov's accelerated gradient descent method [46] and accelerated projected method [47] are used to optimize the parameters of the deep neural network. In practice, they have been used to accelerated for federated learning tasks [48], non-negative latent factor model [49] and recommender systems [50]. There are many theoretical works [51, 52, 53] that focus on analyzing the momentum acceleration for optimizing non-convex problem. [54] shows that it is important for tuning momentum while training deep neural network. [55] first points out linear convergence results for stochastic momentum method. [56] proposes a class of accelerated zeroth-order and first-order momentum method to solve mini-optimization and minimax-optimization problem. [57] extend the momentum method by introducing an RNA scheme and a constrained formulation RNA which has non-linear updates. [58] propose a heuristic adaptive restart method and [59] propose a scheduled restart momentum accelerated SGD method named SRSGD which helps reduce the training time. [60] adds one momentum term on to the distributed gradient algorithm.

## III Methodology

In this section, we introduce SAM with adaptive learning rate and momentum acceleration, dubbed AdaSAM, to stabilize the training process of SAM and ease the learning rate tuning. Then, we present the convergence results of AdaSAM. At last, we give the proof sketch for the main theorem.

### _AdaSAM Algorithm_

AdaSAM for solving Problem (1) is described in Algorithm 1. In each iteration, a mini-batch gradient estimation \(g_{t}\) at point \(x+\epsilon(x)\) with batchsize \(b\) is computed, i.e.,

\[g_{t}=\nabla_{x}f_{b}(x)|_{x_{t}+\epsilon(x_{t})}=\frac{1}{b}\sum_{i\in B} \nabla f_{\xi_{t}}(x_{t}+\delta(x_{t})).\]

Here, \(\delta(x_{t})\) is the extra perturbed gradient step in SAM that is given as follows

\[\delta(x_{t})=\rho\frac{s_{t}}{\|s_{t}\|},\ \mathrm{where}\ s_{t}=\nabla_{x}f_{b}(x )|_{x_{t}}=\frac{1}{b}\sum_{i\in B}\nabla f_{\xi_{t}}(x_{t}).\]

Then, the momentum term of \(g_{t}\) and the second-order moment term \([g_{t}]^{2}\) is accumulatively computed as \(m_{t}\) and \(v_{t}\), respectively. AdaSAM then updates iterate along \(-m_{t}\) with the adaptive learning rate \(\gamma\eta_{t}\).

**Remark 1**.: _Below, we give several comments on AdaSAM:_

* _When_ \(\beta_{2}=1\)_, the adaptive learning rate reduces to the diminishing one as SGD. Then, AdaSAM recovers the classic SAM optimizer._
* _If we drop out the 8-th line_ \(\hat{v}_{t}=\max(\hat{v}_{t-1},v_{t}),\) _then our algorithm becomes the variant of Adam. The counterexample that Adam does not converge in the_ _[_10_]_ _also holds for the SAM variant, while AdaSAM can converge._

```
Input: Initial parameters \(x_{0}\), \(m_{-1}=0\), \(\hat{v}_{-1}=\epsilon^{2}\)(a small positive scalar to avoid the denominator diminishing), base learning rate \(\gamma\), neighborhood size \(\rho\) and momentum parameters \(\beta_{1}\), \(\beta_{2}\). Output: Optimized parameter \(x_{T+1}\)
1foriteration\(t\in\{0,1,2,...,T-1\}\)do
2 Sample mini-batch \(B=\{\xi_{t_{1}},\xi_{t_{2}},...,\xi_{t_{|B|}}\}\);
3 Compute gradient \(s_{t}=\nabla_{x}f_{B}(x)|_{x_{t}}=\frac{1}{b}\sum_{i\in B}\nabla f_{t_{i}}(x_ {t})\);
4 Compute \(\delta(x_{t})=\rho_{t}\frac{s_{t}}{\|s_{t}\|}\);
5 Compute SAM gradient \(g_{t}=\nabla_{x}f_{B}(x)|_{x_{t}+\delta(x_{t})}\);
6\(m_{t}=\beta_{1}m_{t-1}+(1-\beta_{1})g_{t}\);
7\(v_{t}=\beta_{2}v_{t-1}+(1-\beta_{2})[g_{t}]^{2}\);
8\(\hat{v}_{t}=\max(\hat{v}_{t-1},v_{t})\);
9\(\eta_{t}=1/\sqrt{\hat{v}_{t}}\);
10\(x_{t+1}=x_{t}-\gamma m_{t}\odot\eta_{t}\);
11
12 end for
```

**Algorithm 1**AdaSAM: SAM with adaptive learning rate and momentum acceleration

### _Convergence Analysis_

Before presenting the convergence results of the AdaSAM algorithm, we first introduce some necessary assumptions.

**Assumption 1** (\(L\)-smooth).: \(f_{i}\) _and \(f\) is differentiable with gradient Lipschitz property: \(\|\nabla f_{i}(x)-\nabla f_{i}(y)\|\leq L\|x-y\|,\|\nabla f(x)-\nabla f(y)\| \leq L\|x-y\|,\forall x,y\in\mathbb{R}^{d},i=1,2,...,N,\) which also implies the descent inequality, i.e., \(f_{i}(y)\leq f_{i}(x)+\langle\nabla f_{i}(x),y-x\rangle+\frac{f_{i}}{2}\|y-x\|^ {2}\)._

**Assumption 2** (Bounded variance).: _The estimator of the gradient is unbiased and the variance of the stochastic gradient is bounded. i.e.,_

\[\mathbb{E}\nabla f_{i}(x)=\nabla f(x),\quad\mathbb{E}\|\nabla f_{i}(x)-\nabla f (x)\|^{2}\leq\sigma^{2}.\]

_When the mini-batch size \(b\) is used, we have \(\mathbb{E}\|\nabla f_{b}(x)-\nabla f(x)\|^{2}\leq\frac{\sigma^{2}}{b}\)._
**Assumption 3** (**Bounded stochastic gradients**).: _The stochastic gradient is uniformly bounded, i.e.,_

\[\left\|\nabla f_{i}(x)\right\|_{\infty}\leq G,for\ any\ i=1,\ldots,N.\]

**Remark 2**.: _The above assumptions are commonly used in the proof of convergence for adaptive stochastic gradient methods such as [31, 32, 61, 62]._

Below, we briefly explain the main idea of analyzing the convergence of the AdaSAM algorithm. First, we discuss the difficulty of applying the adaptive learning rate on SAM. We notice that the main step which contains adaptive learning rate in convergence analysis is to estimate the expectation \(\mathbb{E}[x_{t+1}-x_{t}]=-\mathbb{E}m_{t}\odot\eta_{t}=-\mathbb{E}(1-\beta_{1 })g_{t}\odot\eta_{t}-\mathbb{E}\beta_{1}m_{t-1}\odot\eta_{t}\), which is conditioned on the filtration \(\sigma(x_{t})\). In this part, we consider the situation that \(\beta_{1}=0\) which does not include the momentum. Then, we apply delay technology to disentangle the dependence between \(g_{t}\) and \(\eta_{t}\), that is

\[\mathbb{E}g_{t}\odot\eta_{t} =\mathbb{E}[g_{t}\odot\eta_{t-1}]+\mathbb{E}[g_{t}\odot(\eta_{t }-\eta_{t-1})]\] \[=\nabla f(x_{t})\odot\eta_{t-1}+\mathbb{E}[g_{t}\odot(\eta_{t}- \eta_{t-1})].\]

The second term \(\mathbb{E}[g_{t}\odot(\eta_{t}-\eta_{t-1})]\) is dominated by the first term \(\nabla f(x_{t})\odot\eta_{t-1}\). Then, it is not difficult to get the convergence result of the stochastic gradient descend with the adaptive learning rate such as AMSGrad. However, when we apply the same strategy to AdaSAM, we find that \(\mathbb{E}g_{t}\odot\eta_{t-1}\) cannot be handled similarly because \(\mathbb{E}g_{t}=\mathbb{E}\nabla_{x}f_{b}\left(x+\rho\frac{\nabla f_{b}(x)}{ \left\|\nabla f_{b}(x)\right\|}\right)\neq\nabla f(x_{t})\). Inspired by [29, Lemma 16], our key observation is that

\[\mathbb{E}\nabla_{x}f_{b}\left(x+\rho\frac{\nabla f_{b}(x)}{\left\| \nabla f_{b}(x)\right\|}\right) \approx\mathbb{E}\nabla_{x}f_{b}\left(x+\rho\frac{\nabla f(x)}{ \left\|\nabla f(x)\right\|}\right)\] \[=\nabla_{x}f\left(x+\rho\frac{\nabla f(x)}{\left\|\nabla f(x) \right\|}\right)\]

and we prove the other terms such as \(\mathbb{E}\left(\nabla_{x}f_{b}\left(x+\rho\frac{\nabla f_{b}(x)}{\left\| \nabla f_{b}(x)\right\|}\right)-\nabla_{x}f_{b}\left(x+\rho\frac{\nabla f(x)}{ \left\|\nabla f(x)\right\|}\right)\right)\odot\eta_{t-1}\) have small values that do not dominate the convergence rate.

On the other hand, when we apply the momentum steps, we find that the term \(\mathbb{E}m_{t-1}\odot\eta_{t}\) cannot be ignored. By introducing an auxiliary sequence \(z_{t}=x_{t}+\frac{\beta_{1}}{1-\beta_{1}}(x_{t}-x_{t-1})\), we have \(\mathbb{E}[z_{t+1}-z_{t}]=-\mathbb{E}[\frac{\beta_{1}}{1-\beta_{1}}\gamma m_{ t-1}\odot(\eta_{t-1}-\eta_{t})-\gamma g_{t}\odot\eta_{t}]\). The first term contains the momentum term which has a small value due to the difference of the adaptive learning rate \(\eta_{t}\). Thus, it is diminishing without hurting the convergence rate.

**Theorem 1**.: _Under the assumptions 1,2,3, and \(\gamma\) is a fixed number satisfying \(\gamma\leq\frac{\epsilon}{16L}\), for the sequence \(\{x_{t}\}\) generated by Algorithm 1, we have the following convergence rate_

\[\frac{1}{T}\sum_{t=0}^{T-1}\mathbb{E}\|\nabla f(x_{t})\|_{2}^{2}\!\leq\!\frac {2G(f(x_{0})\!-\!f^{*})}{\gamma T}\!+\!\frac{8G\gamma L}{\epsilon}\frac{\sigma^ {2}}{b\epsilon}\!+\!\Phi \tag{5}\]

_where_

\[\Phi=\frac{45GL^{2}\rho_{t}^{2}}{\epsilon}+\frac{2G^{3}}{(1-\beta _{1})T}d(\frac{1}{\epsilon}-\frac{1}{G})+\frac{6\gamma^{2}L^{2}\beta_{1}^{2}}{ (1-\beta_{1})^{2}}\frac{dG^{3}}{\epsilon^{3}}\] \[+\frac{2(4+(\frac{\beta_{1}}{1-\beta_{1}})^{2})\gamma LG^{3}}{ \epsilon}d(\epsilon^{-2}-G^{-2})+\frac{8G\gamma L}{\epsilon}\frac{L\rho_{t}^{ 2}}{\epsilon}, \tag{6}\]

_in which \(T\) is the number of iteration, \(f^{*}\) is the minimal value of the function \(f\), \(\gamma\) is the base learning rate, \(b\) is the mini-batch size, \(d\) is the dimension of paramter \(x\). \(\beta_{1}\), \(G\), \(L\), \(\epsilon\), \(\sigma^{2}\), \(d\) are fixed constants._

Theorem 1 characterizes the convergence rate of the sequence \(\{x_{t}\}\) generated by AdaSAM with respect to the stochastic gradient residual. The first two terms of the right hand side of Inequality (5) are the terms that dominate the convergence rate. Compared with the first two terms, \(\Phi\) is a small value while we set neighborhood size \(\rho\) and learning rate \(\gamma\) as small values which are related to large iteration number \(T\). Then, we obtain the following corollary directly.

**Corollary 1** (**Mini-batch linear speedup**).: _Under the same conditions of Theorem 1. Furthermore, when we choose the base learning rate \(\gamma=O(\sqrt{\frac{b}{T}})\) and neighborhood size \(\rho=O(\sqrt{\frac{1}{bT}})\), the following result holds:_

\[\frac{1}{T}\sum_{t=0}^{T-1}\mathbb{E}\|\nabla f(x_{t})\|_{2}^{2} =O\left(\frac{1}{\sqrt{bT}}\right)+O\left(\frac{1}{bT}\right)+O \left(\frac{1}{T}\right)\] \[+O\left(\frac{1}{b^{\frac{1}{2}}T^{\frac{3}{2}}}\right)+O\left( \frac{b^{\frac{1}{2}}}{T^{\frac{3}{2}}}\right)+O\left(\frac{b}{T}\right).\]

_When \(T\) is sufficiently large, we achieve the linear speedup convergence rate with respect to mini-batch size \(b\), i.e.,_

\[\frac{1}{T}\sum_{t=0}^{T-1}\mathbb{E}\|\nabla f(x_{t})\|_{2}^{2}=O\left(\frac{1}{ \sqrt{bT}}\right). \tag{7}\]

**Remark 3**.: _Two comments are given about the above results:_

* _To reach a_ \(O(\delta)\) _stationary point, when the batch size is 1, it needs_ \(T=O(\frac{1}{\delta^{2}})\) _iterations. When the batch size is_ \(b\)_, we need to run_ \(T=O(\frac{1}{\delta^{2}})\) _steps. The method with batch size_ \(b\) _is_ \(b\) _times faster than batch size of 1, which means that it has the mini-batch linear speedup property._
* _According to_ _[_37, 63, 64_]__, AdaSAM can be extended to distributed version and achieves linear speedup property with respect to the number of works in the Parameter-Sever setting._

### _Proof Sketch_

In this part, we give the proof sketch of the Theorem 1. For the complete proof, please see Appendix. Below, we first introduce an auxiliary sequence \(z_{t}=x_{t}+\frac{\beta_{1}}{1-\beta_{1}}(x_{t}-x_{t-1})\). By applying \(L\)-smooth condition, we have

\[f(z_{t+1})\!\leq\!f(z_{t})\!+\!\langle\nabla f(z_{t}),z_{t+1}-z_{t}\rangle\!+\! \frac{L}{2}\|z_{t+1}-z_{t}\|^{2}. \tag{8}\]

Applying it to the sequence \(\{z_{t}\}\) and using the delay strategy yield

\[f(z_{t+1})-f(z_{t})\] \[\leq\langle\nabla f(z_{t}),\frac{\gamma\beta_{1}}{1-\beta_{1}}m_{t-1} \odot(\eta_{t-1}-\eta_{t})\rangle+\frac{L}{2}\|z_{t+1}-z_{t}\|^{2}\] \[+\langle\nabla f(z_{t}),\frac{\gamma}{b}\sum_{i\in B}\nabla f_{i}(x _{t}+\rho_{t}\frac{s_{t}}{\|s_{t}\|})\odot(\eta_{t-1}-\eta_{t})\rangle\]
\[+\langle\nabla f(z_{t})-\nabla f(x_{t}),-\frac{\gamma}{b}\sum_{i\in B} \nabla f_{i}(x_{t}+\rho_{t}\frac{s_{t}}{\|s_{t}\|})\odot\eta_{t-1}\rangle\] \[+\langle\nabla f(x_{t}),-\frac{\gamma}{b}\sum_{i\in B}\nabla f_{i} (x_{t}+\rho_{t}\frac{\nabla f(x_{t})}{\|\nabla f(x_{t})\|})\odot\eta_{t-1}\rangle\] \[+\langle\nabla f(x_{t}),\frac{\gamma}{b}\sum_{i\in B}\nabla f_{i} (x_{t}+\rho_{t}\frac{\nabla f(x_{t})}{\|\nabla f(x_{t})\|})\odot\eta_{t-1}\] \[-\frac{\gamma}{b}\sum_{i\in B}\nabla f_{i}(x_{t}+\rho_{t}\frac{s_ {t}}{\|s_{t}\|})\odot\eta_{t-1}\rangle. \tag{9}\]

From the Lemma 5, Lemma 6, Lemma 7 in appendix, we can bound the above terms in (III-A) as follows

\[\langle\nabla f(z_{t}),\frac{\gamma}{b}\sum_{i\in B}\nabla f_{i} (x_{t}+\rho_{t}\frac{s_{t}}{\|s_{t}\|})\odot(\eta_{t-1}-\eta_{t})\rangle\] \[\leq\gamma G^{2}\|\eta_{t-1}-\eta_{t}\|_{1}, \tag{10}\] \[\langle\nabla f(z_{t}),\frac{\gamma\beta_{1}}{1-\beta_{1}}m_{t-1} \odot(\eta_{t-1}-\eta_{t})\rangle\] \[\leq\frac{\gamma\beta_{1}}{1-\beta_{1}}G^{2}\|\eta_{t-1}-\eta_{t }\|_{1},\] (11) \[\langle\nabla f(x_{t}),\frac{\gamma}{b}\sum_{i\in B}\nabla f_{i} (x_{t}+\rho_{t}\frac{\nabla f(x_{t})}{\|\nabla f(x_{t})\|})\odot\eta_{t-1}\] \[-\frac{\gamma}{b}\sum_{i\in B}\nabla f_{i}(x_{t}+\rho_{t}\frac{s_ {t}}{\|s_{t}\|})\odot\eta_{t-1}\rangle\] \[\leq\frac{\gamma}{2\mu^{2}}\|\nabla f(x_{t})\odot\sqrt{\eta_{t-1 }}\|^{2}+\frac{2\mu^{2}\gamma L^{2}\rho_{t}^{2}}{\epsilon}. \tag{12}\]

Then we substitute them into the (III-A), and take the conditional expectation to get

\[\mathbb{E}f(z_{t+1})-f(z_{t})\] \[\leq\mathbb{E}\langle\nabla f(x_{t}),-\frac{\gamma}{b}\sum_{i\in B }\nabla f_{i}(x_{t}+\rho_{t}\frac{\nabla f(x_{t})}{\|\nabla f(x_{t})\|}) \odot\eta_{t-1}\rangle\] \[+\frac{\gamma}{2\mu^{2}}\|\nabla f(x_{t})\odot\sqrt{\eta_{t-1}} \|^{2}+\frac{\gamma}{1-\beta_{1}}G^{2}\|\eta_{t-1}-\eta_{t}\|_{1}\] \[+\mathbb{E}\langle\nabla f(z_{t})-\nabla f(x_{t}),-\frac{\gamma} {b}\sum_{i\in B}\nabla f_{i}(x_{t}+\rho_{t}\frac{s_{t}}{\|s_{t}\|})\odot\eta_ {t-1}\rangle\] \[+\frac{2\mu^{2}\gamma L^{2}\rho_{t}^{2}}{\epsilon}+\frac{L}{2} \mathbb{E}\|z_{t+1}-z_{t}\|^{2}, \tag{13}\]

where \(\mu>0\) is a constant to be determined. Next, from the Lemma 8, Lemma 10 and Lemma 9 in Appendix, we have

\[\mathbb{E}\langle\nabla f(x_{t}),-\frac{\gamma}{b}\sum_{i\in B} \nabla f_{i}(x_{t}+\rho_{t}\frac{\nabla f(x_{t})}{\|\nabla f(x_{t})\|}) \odot\eta_{t-1}\rangle\] \[\leq-\gamma\|\nabla f(x_{t})\odot\sqrt{\eta_{t-1}}\|^{2}+ \mathbb{E}\frac{\gamma}{2\alpha^{2}}\|\nabla f(x_{t})\odot\sqrt{\eta_{t-1}}\|^ {2}\] \[\quad+\frac{\gamma\alpha^{2}L^{2}\rho_{t}^{2}}{2\epsilon}, \tag{14}\] \[\frac{L}{2}\mathbb{E}\|z_{t+1}-z_{t}\|^{2}\leq\frac{LG^{2}\gamma ^{2}\beta_{1}^{2}}{(1-\beta_{1})^{2}}\mathbb{E}\|\eta_{t}-\eta_{t-1}\|^{2}\] \[\quad+\gamma^{2}L(3\frac{1+\beta}{\beta\epsilon}(\frac{L\rho_{t}^{ 2}}{\epsilon}+\frac{\sigma^{2}}{b\epsilon}+\mathbb{E}\|\nabla f(x_{t})\odot \sqrt{\eta_{t-1}}\|^{2})\] \[\quad+(1+\beta)G^{2}\mathbb{E}\|\eta_{t}-\eta_{t-1}\|^{2}),\] (15) \[\mathbb{E}\langle\nabla f(z_{t})-\nabla f(x_{t}),-\frac{\gamma}{b} \sum_{i\in B}\nabla f_{i}(x_{t}+\rho_{t}\frac{s_{t}}{\|s_{t}\|})\odot\eta_{t-1}\rangle\] \[\leq\frac{\gamma^{3}L^{2}\beta_{1}^{2}}{2\epsilon(1-\beta_{1})^{ 2}}(\frac{1}{\lambda_{1}^{2}}+\frac{1}{\lambda_{2}^{2}}+\frac{1}{\lambda_{3}^{ 2}})\frac{dG_{\infty}^{2}}{\epsilon^{2}}+\frac{\gamma L^{2}\rho_{t}^{2}}{2 \epsilon}(\lambda_{2}^{2}+4\lambda_{3}^{2})\] \[\quad+\frac{\gamma\lambda_{1}^{2}}{2}\|\nabla f(x_{t})\odot\sqrt{ \eta_{t-1}}\|^{2}. \tag{16}\]

Next, we substitute it into the (III-A). Taking the expectation over all history information yields

\[\mathbb{E}f(x_{t+1})-\mathbb{E}f(x_{t})\] \[\leq-\gamma(1-\frac{1}{2\mu^{2}}-\frac{1}{2\alpha^{2}}-\frac{3 \gamma L(1+\beta)}{\beta\epsilon}-\frac{\lambda_{1}^{2}}{2})\mathbb{E}\|\nabla f (x_{t})\odot\sqrt{\eta_{t-1}}\|^{2}\] \[+\frac{2\mu^{2}\gamma L^{2}\rho_{t}^{2}}{\epsilon}+\frac{\gamma}{1 -\beta_{1}}G^{2}\mathbb{E}\|\eta_{t-1}-\eta_{t}\|_{1}+\frac{\gamma\alpha^{2}L^{2 }\rho^{2}}{2\epsilon}\] \[+\frac{\gamma^{3}L^{2}\beta_{1}^{2}}{2\epsilon(1-\beta_{1})^{2}}( \frac{1}{\lambda_{1}^{2}}+\frac{1}{\lambda_{2}^{2}}+\frac{1}{\lambda_{3}^{3}}) \frac{dG_{\infty}^{2}}{\epsilon^{2}}+\frac{\gamma L^{2}\rho_{t}^{2}}{2\epsilon}( \lambda_{2}^{2}+4\lambda_{3}^{2})\] \[+\gamma^{2}LG^{2}((\frac{\beta_{1}}{1-\beta_{1}})^{2}+1+\beta) \mathbb{E}\|\eta_{t}-\eta_{t-1}\|^{2}\] \[+\frac{3\gamma^{2}L(1+\beta)}{\beta\epsilon}(\frac{L\rho_{t}^{2}}{ \epsilon}+\frac{\sigma^{2}}{b\epsilon}). \tag{17}\]

We set \(\mu^{2}=\alpha^{2}=8\), \(\beta=3\), \(\lambda_{1}^{2}=\frac{1}{4}\), \(\lambda_{2}^{2}=\lambda_{3}^{2}=1\) and we choose \(\frac{2\gamma L}{\epsilon}\leq\frac{1}{8}\). Note that \(\eta_{t}\) is bounded. We have

\[\frac{\gamma}{2G}\mathbb{E}\|\nabla f(x_{t})\|^{2}\leq\frac{\gamma}{2} \mathbb{E}\|\nabla f(x_{t})\odot\sqrt{\eta_{t-1}}\|^{2} \tag{18}\] \[\leq-\mathbb{E}f(x_{t+1})+\mathbb{E}f(x_{t})+\frac{45\gamma L^{2 }\rho_{t}^{2}}{2\epsilon}+\frac{4\gamma^{2}L}{\epsilon}(\frac{L\rho_{t}^{2}}{ \epsilon}+\frac{\sigma^{2}}{b\epsilon})\] \[+\frac{\gamma}{1-\beta_{1}}G^{2}\mathbb{E}\|\eta_{t-1}-\eta_{t }\|_{1}+\frac{3\gamma^{3}L^{2}\beta_{1}^{2}}{(1-\beta_{1})^{2}}\frac{dG_{ \infty}^{2}}{\epsilon^{3}}\] \[+(4+(\frac{\beta_{1}}{1-\beta_{1}})^{2})^{2}\gamma^{2}LG^{2} \mathbb{E}\|\eta_{t}-\eta_{t-1}\|^{2}. \tag{19}\]

\begin{table}
\begin{tabular}{l c c c c c c c c c} \hline \hline \multirow{2}{*}{**Model**} & **CoLA** &
Then, telescoping it from \(t=0\) to \(t=T-1\), and assuming \(\gamma\) is a constant, it follows that

\[\frac{1}{T}\sum_{t=0}^{T-1}\mathbb{E}\|\nabla f(x_{t})\|^{2}\leq \frac{2G(f(x_{0})-f^{*})}{\gamma T}+\frac{8G\gamma L}{\epsilon}\frac{\sigma^{2} }{b\epsilon}\] \[+\frac{45GL^{2}\rho_{t}^{2}}{\epsilon}+\frac{2G^{3}}{(1-\beta_{1 })T}d(\frac{1}{\epsilon}-\frac{1}{G})+\frac{6\gamma^{2}L^{2}\beta_{1}^{2}}{(1- \beta_{1})^{2}}\frac{dG^{3}}{\epsilon^{3}}\] \[+\frac{8G\gamma L}{\epsilon}\frac{L\rho_{t}^{2}}{\epsilon}+\frac{ 2(4+(\frac{\beta_{1}}{1-\beta_{1}})^{2})\gamma LG^{3}}{T}d(\epsilon^{-2}-G^{-2 }), \tag{20}\]

which completes the proof.

## IV Experiments

In this section, we apply AdaSAM to train language models and compare it with SGD, AMSGrad, and SAM to show its effectiveness. Due to space limitations, more experiments, including visualization, task description, implementation details and results description, are placed in the Appendix.

### _Experimental Setup_

**Tasks and Datasets.** We evaluate AdaSAM on a popular benchmark, _i.e._ General Language Understanding Evaluation (GLUE) [65], which consists of several language understanding tasks including sentiment analysis, question answering and textual entailment. For a fair comparison, we report the results based on single-task, without multi-task or ensemble training. We evaluate the performance with Accuracy ("_Acc_") metric for most tasks, except the F1 scores for QQP and MRPC, the Pearson-Spearman correlations ("_Peor/Scor_") for STS-B and the Matthew correlations ("_Mcc_") for CoLA. The performance is better as the metric is higher.

**Implementations.** We conduct our experiments using a widely-used pre-trained language model, RoBERTa-large1 in the open-source toolkit fairseq2, with 24 transformer layers, a hidden size of 1024. For fine-tuning on each task, we use different combinations of hyper-parameters, including the

Fig. 1: The loss and evaluation metric v.s. steps on MRPC, RTE, CoLA, SST-2, STS-B, MNLI, QQP, and QNLL(\(\beta_{1}=0.9\))
learning rate, the number of epochs, the batch size, _etc_3. In particular, for RTE, STS-B and MRPC of GLUE benchmark, we first fine-tune the pre-trained RoBERTa-large model on the MNLI dataset and continue fine-tuning the RoBERTa-large-MNLI model on the corresponding single-task corpus for better performance, as many prior works did [66, 7]. All models are trained on NVIDIA DGX SuperPOD cluster, in which each machine contains 8\(\times\)40GB A100 GPUs.

Footnote 3: Due to the space limitation, we show the details of the dataset and training setting in Appendix A.

### _Results on GLUE Benchmark_

Table I shows the performance of SGD, SAM, AMSGrad, and AdaSAM. For the AdaSAM, we tune the neighborhood size of the perturbation parameter from 0.01, 0.005, and 0.001. The result shows that AdaSAM outperforms AMSGrad on 6 tasks of 8 tasks except for QNLI and QQP. Overall, it improves the 0.28 average score than AMSGrad. On the other hand, Table I indicates that SAM is better than SGD on 7 tasks of 8 tasks except for RTE. And SAM can significantly improve performance. Comparing the results of Table I, we can find that the adaptive learning rate method is better than SGD tuned with handicraft learning rate. AdaSAM achieves the best metric on 6 tasks which is CoLA, SST-2, MRPC, STS-B, RTE, QNLI, and MNLI. In general, AdaSAM is better than the other methods.

In addition, Figure 3 shows the convergence speed of the detailed loss and evaluation metrics vs. the number of steps during training, respectively. The loss curve of AdaSAM decreases faster than SAM and SGD in all tasks, and it has a similar decreasing speed as the AMSGrad. The evaluation metric curve of AdaSAM and AMSGrad show that the AdaSAM is better than SGD and SAM and decreases the loss value as faster as the AMSGrad in all tasks.

### _Mini-batch Speedup_

In this part, we test the performance with different batch sizes to validate the linear speedup property. The experiments are conducted on the MRPC, RTE, and CoLA tasks. The batch size is set as 4, 8, 16, 32, respectively. We scale the learning rate as \(\sqrt{N}\), which is similar as [67], where \(N\) is the batch size. The results show that the training loss decreases faster as the batchsize increases, and the loss curve with the batch size of 32 achieves nearly half iterations as the curve with the batch size of 16.

### _Ablation Study_

In this subsection, we conduct the experiments the momentum hyper-parameter \(\beta_{1}\) is set to 0 to evaluate the influence of the momentum acceleration and the adaptive learning rate. Table II shows that AdaSAM outperforms AMSGrad on 6 tasks of 8 tasks except for SST-2 and RTE. In Table II, we also compare SGD and SAM, and without the momentum, SAM outperforms SGD on all tasks. Under this situation, AdaSAM without the momentum acceleration method is better than the other methods.

When comparing the result of Table I and Table II, we find that both the adaptive learning rate method and the momentum acceleration are helpful for the model's generalization ability. When there is no momentum term, SAM with an adaptive

\begin{table}
\begin{tabular}{l c c c c c c c c c} \hline \hline \multirow{2}{*}{**Model**} & **CoLA** & **SST-2** & **MRPC** & **STS-B** & **RTE** & **MNLI** & **QNLI** & **QQP** \\  & mcc. & Acc. & Acc./F1 & Pcor./Scor. & Acc. & m/mm. & Acc. & F1/ Acc. & \\ \hline SGD & 0 & 51.722 & 68.38/ 81.22 & 5.55/ 7.2 & 51.27 & 32.51/ 32.42 & 53.32 & 0 / 63.18 & 37.23 \\ SAM(\(\rho=\)0.01) & 41.91 & 95.3 & 68.38/ 81.22 & 9.21/ 10.38 & 53.07 & 87.99/ 87.8 & 51.24 & 83.44/ 87.27 & 63.1 \\ SAM(\(\rho=\)0.005) & 58.79 & 81.54 & 68.38/ 81.22 & 13.52/ 16.6 & 53.79 & 88.42/ 88.15 & 92.95 & 83.84/ 87.7 & 67.91 \\ SAM(best) & 58.79 & 95.3 & 68.38/ 81.22 & 13.52/ 16.6 & 53.79 & 88.42/ 88.15 & 92.95 & 83.84/ 87.7 & 67.90 \\ AMSGrad & 63.78 & 96.44 & 89.71/ 92.44 & 89.98/ 90.35 & 87.36 & 90.65/ 90.35 & 94.53 & 88.59/ 91.27 & 88.79 \\ \hline AdaSAM(\(\rho=\)0.01) & 69.23 & 96.22 & 89.96/ 92.84 & 88.83/ 89.07 & 87 & 90.83/ 90.41 & 94.8 & 88.67/ 91.38 & 89.1 \\ AdaSAM(\(\rho=\)0.005) & 68.47 & 96.22 & 89.96/ 92.82 & 91.59/ 91.22 & 73.65 & 90.75/ 90.42 & 94.73 & 88.72/ 91.46 & 88.33 \\ AdaSAM(best) & 69.23 & 96.22 & 89.96/ 92.84 & 91.59/ 91.22 & 87 & 90.83/ 90.42 & 94.8 & 88.72/ 91.46 & 89.52 \\ \hline \hline \end{tabular}
\end{table} TABLE II: Results of SGD, SAM, AMSGrad and AdaSAM on the GLUE benchmark without momentum, i.e., \(\beta_{1}=0\)

Fig. 2: The linear speedup verification of AdaSAM with the number of batch size of 4, 8, 16, 32.
learning rate improves the 0.74 average score to AMSGrad. With a momentum term, AdaSAM improves the 0.28 average score to AMSGrad. It shows that the adaptive method can improve the performance with or without momentum acceleration and it achieves the best performance with momentum acceleration. And we can find that momentum acceleration improves the performance of SAM, AMSGrad and AdaSAM.

## V Conclusion

In this work, we study the convergence rate of Sharpness aware minimization optimizer with an adaptive learning rate and momentum acceleration, dubbed AdaSAM in the stochastic non-convex setting. To the best of our knowledge, we are the first to provide the non-trivial \(\mathcal{O}(1/\sqrt{bT})\) convergence rate of AdaSAM, which achieves a linear speedup property with respect to mini-batch size \(b\). We have conducted extensive experiments on several NLP tasks, which verifies that AdaSAM could achieve superior performance compared with AMSGrad and SAM optimizers. Future works include extending AdaSAM to the distributed setting and reducing the twice gradient back-propagation cost.
