The allure of superhuman-level capabilities has led to considerable interest in language models like GPT-3 and T5, wherein the research has, by and large, revolved around new model architectures, training tasks, and loss objectives, along with substantial engineering efforts to scale up model capacity and dataset size. Comparatively little work has been done to improve the generalization of these models through better optimization. In this work, we show that Sharpness-Aware Minimization (SAM), a recently proposed optimization procedure that encourages convergence to flatter minima, can substantially improve the generalization of language models without much computational overhead. We show that SAM is able to boost performance on SuperGLUE, GLUE, Web Questions, Natural Questions, Trivia QA, and TyDiQA, with particularly large gains when training data for these tasks is limited. Over the last several years, remarkable progress has been made within the domain of natural language understanding, with machine-learned models able to solve some tasks at near or above human-level performance. This progress has, by and large, been fueled by research centered around 1) better inductive biases , such as the attention-enabled Transformer architecture (Vaswani et al., 2017), 2) the clever leverage of massive corpora of textual data that was historically disregarded as un-labeled, usually in the form of pre-training objectives that strive to teach the model the structure of language (Radford et al., 2019; Devlin et al., 2018), 3) scaling up model capacity and the methods to support it (Shazeer and Stern, 2018), 4) multi-task learning (Raffel et al., 2019), and lastly, 5) larger and more diverse datasets along with ever-improving benchmarks that attempt to test the true capabilities of these models. Although these efforts all share the single goal of improving the model's generalization, doing so by explicit changes to the optimization of the loss function has received less attention in comparison. Recently, motivated by the both empirical and theoretical findings that flatter minima lead to better generalization (Kleinberg et al., 2018; Shirish Keskar et al., 2016; Chaudhari et al., 2019; Smith and Le, 2017), Foret et al. (2020) proposed a novel modification to vanilla stochastic gradient descent they term Sharpness-Aware Minimization, or SAM. They show theoretically and empirically that optimizing with SAM encourages convergence to flatter points in the loss landscape and with it comes the anticipated improvement in out-of-sample error. While their empirical findings are limited to computer vision tasks and datasets using convolutional neural networks (ResNets), follow-up work (Chen et al., 2021) showed how SAM is particularly effective on Vision transformers (ViTs) (Dosovitskiy et al., 2020) and MLP-Mixers (Tolstikhin et al., 2021), architectures that are more prone than convolutional ones to land in sharp minima. Crucially, they show that when equipped with SAM, ViTs outperform ResNets of similar size and throughput without the need for large-scale pre-training . Encouraged by wins in the vision domain, we ask whether SAM can deliver similar gains in the language domain. Our contributions are as follows: 1. We show that blithely applying SAM when fine-tuning public pre-trained checkpoints of the text-to-text transformer (T5) (Raffel et al., 2019) and its multilingual counterpart, mT5 (Xue et al., 2020) on SuperGLUE (Wang et al., 2019), GLUE (Wang et al., 2018), TyDiQA-GoldP (Clark et al., 2020) and the Closed-Book Question Answering (CBQA) tasks from Roberts et al. (2020)  Web Questions (Berant et al., 2013), Natural Questions (Kwiatkowski et al., 2019), and Trivia QA (Joshi et al., 2017)  improves test perfor-7360 mance quite markedly. Furthermore, by employing an approximation suggested by Brock et al. (2021), these gains come only at the cost of about 25% extra compute. 2. The improvement brought by SAM often increases with less labeled training data, making SAM indispensable for data-limited tasks. We test this by subsampling the training splits of CBQA and SuperGLUE datasets at rates ranging from 2% to 80% . Better Generalization. In light of flatter minima generalizing better, Smith and Le (2017) showed that the inherent noise in SGD serves as a form of implicit regularization , preventing the optimization from ever entering sharp valleys. Like SAM, entropy SGD (Chaudhari et al., 2019) explicitly encourages flatter minima. Smith et al. (2021); Barrett and Dherin (2020) analyzed SGD's generalization formally by way of continuous-time gradient flow. Optimization routines based on adversarial risk (Zhu et al., 2019; He et al., 2020) and trust regions (Jiang et al., 2019; Aghajanyan et al., 2020) have been proposed and shown to improve generalization across settings. While the number of methods which provide implicit or explicit regularization is overwhelmingly large, methods like early stopping, weight decay (or (cid:96) 2 -regularization), dropout (Srivastava et al., 2014), teacher-student or self-distillation (Hinton et al., 2015; Mobahi et al., 2020), label smoothing (Mller et al., 2019), batch normalization (Ioffe and Szegedy, 2015), mixup (Zhang et al., 2017), and data-augmentation more broadly are among the most widely used in practice. Marginalization of Bayesian neural networks, though challenging, has been shown to result in superior generalization in some settings (Wilson and Izmailov, 2020; MacKay, 1995). While first-order optimization via SGD has been the prevailing way of training neural networks due to its efficiency and effectiveness, alternative second-order methods like K-FAC (Martens and Grosse, 2015) and Shampoo (Gupta et al., 2018) have slowly gained traction, often enabled by clever engineering to make them feasible at scale. Notably, Anil et al. (2020) presents a scalable implementation of Shampoo that provides significant convergence and wall-clock time improvements compared to first-order methods. They demonstrate superior performance on machine translation and language modeling. SAM. While this is, to the best of our knowledge, the first work detailing the benefits of SAM for language tasks, there have been successful applications of SAM in the vision domain. Notably, Chen et al. (2021) showed that convolution-free vision models like vision transformers (ViTs) (Dosovit-skiy et al., 2020) and MLP-Mixers (Tolstikhin et al., 2021) suffer from sharp minima and that SAM indeed smooths their loss landscapes. They crucially show that ViTs and MLP-Mixers outperfom ResNets of similar and greater size on ImageNet without the use of pre-training or data augmentations that would otherwise be necessary to achieve reasonable performance. They show that SAM induces sparsity in both architectures and leads to more perceptive attention maps in ViTs. They observe empirically that data augmentation and SAM are alike in that they both smooth the landscape on average, but the latter does so by explicitly controlling the worst-case curvature, whereas the former smooths over the directions induced by the augmentations. Furthermore, they observe that SAM encourages linearity with respect to the input, exhibiting an effect similar to that of mixup (Zhang et al., 2017). Lastly, they show that SAM helps contrastive learning and that it enables better robustness on corrupted examples from ImageNet-C (Hendrycks and Dietterich, 2019) and ImageNet-R (Hendrycks et al., 2021). In a similar spirit, Brock et al. (2021) proposed speeding up SAM significantly by using fewer examples when computing the ascent step, a strategy which we employ in this work, and they were able to apply it to ResNet model variants to advance the state of the art on ImageNet without extra data. Meanwhile, in an attempt to make SAM's radius  invariant to the scale of the model parameters, Kwon et al. (2021) proposed an adaptive version named Adaptive Sharpness-Aware Minimization (ASAM), which they then show empirically to outperform normal SAM on a set of benchmark vision tasks. We begin by briefly reviewing the SAM algorithm; interested readers can see the original paper for a thorough treatment. In our presentation, we use the (cid:96) 2 norm ( p = 2 using notation from the origi-7361 nal paper), assume a general optimizer (instead of vanilla SGD), and use the approximation proposed by Brock et al. (2021) to compute the ascent gradient (adversarial point) efficiently. Given a loss function L : W  X  Y  R + , SAM seeks to find the parameter w whose neighborhood has low training loss by optimizing the minimax objective: min w max || (cid:15) || 2   L train ( w + (cid:15) ) . Finding the exact optima (cid:15)  of the inner-maximization is challenging, so Foret et al. (2020) employ a first-order approximation, resulting in:  (cid:15) ( w ) = argmin || (cid:15) || 2   L train ( w ) + (cid:15) T  w L train ( w ) =   w L train ( w ) / || w L train ( w ) || 2 . That is,  (cid:15) is just a scaling of the loss gradient at the current parameters. After computing  (cid:15) ( w ) , SAM performs gradient descent using the gradient  w L train ( w ) | w adv at the nearby adversarial point w adv ( w ) (cid:44) w +  (cid:15) ( w ) . Put another way, SAM plugs-and-plays with any first-order optimizer by simply replacing the gradient of the mini-batch B at the current model weights w t  W with the gradient computed at w adv . w adv itself is computed by taking a gradient ascent step of size  along the unit gradient vector  w LM ( w ) / || w LM ( w ) || 2 | w t , where M can be the mini-batch B , or a subset of it for enhanced efficiency. We found that setting M to be 1 / 4 -th of B sped up the method significantly with little loss in quality, in line with the recommendation of Brock et al. (2021). The end-to-end algorithm is outlined in Algorithm 1. 4 Experiments With SAM reviewed, we now discuss our experiments. We evaluate SAM on a range of natural language understanding tasks using the T5 (text-to-text Transformer) framework (Raffel et al., 2019). T5 casts NLU tasks as sequence-to-sequence ones that are learned using an encoder-decoder Transformer (Vaswani et al., 2017) architecture setup. These Transformer models are typically pre-trained on large corpora, like the Colossal Clean Crawled Corpus (C4) (Raffel et al., 2019), with, for example, the objective of predicting a short contiguous span of text that was intentionally corrupted in a snippet of input text. The pre-trained model is typically fine-tuned on a single task or a mixture of Algorithm 1 Efficient SAM Algorithm. 2: initialize parameters w 0 , t = 0 . 3: while not converged do 4: sample batch B = { ( x 1 , y 1 ) , ..., ( x b , y b ) } . 5: sample ascent micro-batch M = { ( x 1 , y 1 ) , ..., ( x a , y a ) } . 6: compute adversarial (ascent) point: w adv = w t +   w LM ( w ) || w LM ( w ) || 2 | w t . 7: compute gradient approximation for the SAM objective: g adv =  w LB ( w ) | w adv . 8: update parameters: w t +1 = opt( w t , g adv ) . 9: t = t + 1 . 10: end while 11: return w t multiple tasks, the latter enabled by the fact that the framework treats all tasks as simple input-to-target sequence predictions. To this end, we evaluate SAM in two ways: 1. When publicly available pre-trained checkpoints of the T5.1.1 model variant are fine-tuned with and without SAM, on SuperGLUE, GLUE, TyDiQA, and the Closed-Book Question Answering benchmarks: Web Questions, Natural Questions, TriviaQA. We show SAM improves generalization across benchmarks and four model sizes: Small (77M parame-ters), Base (250M), Large (800M), and XL (3B). 2. To show how it helps when task data is limited, we report results when the training splits of these benchmarks at various rates, ranging from 2% to 80% . Framework. For all experiments, we train using Jax (Bradbury et al., 2018) and Google Cloud TPUs. To ensure fair comparisons, eliminate the impact of exogenous factors, and reduce the possibility of software bugs, we train both standard and SAM-enabled models using the same codebase and settings, so that the code paths are identical except for the gradient calculation at each step, wherein 7362 Model SGlue BoolQ CB CoPA MultiRC ReCoRD RTE WiC WSC Small 67.7 72.6 89.4 / 89.3 67.0 68.5 / 21.4 61.7 / 60.8 69.3 65.4 72.1 Small + SAM (0.05) 68.4 73.5 92.1 / 89.3 61.0 68.5 / 22.8 62.1 / 61.0 69.7 65.7 79.8 Base 75.3 80.0 91.7 / 94.6 71.0 75.4 / 35.4 76.2 / 75.4 80.9 69.3 76.9 Base + SAM (0.15) 78.5 82.2 93.7 / 94.6 78.0 77.5 / 39.1 78.2 / 77.2 85.9 70.4 81.7 Large 84.3 86.6 99.4 / 98.2 89.0 83.7 / 51.0 86.5 / 85.6 89.2 72.9 84.6 Large + SAM (0.15) 84.6 88.0 95.0 / 96.4 86.0 84.0 / 53.7 87.3 / 86.4 89.2 75.2 86.5 XL 87.2 88.6 93.7 / 96.4 95.0 86.9 / 61.1 89.5 / 88.4 91.3 74.9 89.4 XL + SAM (0.15) 89.1 89.4 100.0 / 100.0 95.0 87.9 / 63.7 90.9 / 90.0 92.1 75.5 94.2 Table 1: Experimental results (dev scores) on the (full) SuperGLUE benchmark. Efficient SAM. In Foret et al. (2020), the idea of partitioning the ascent mini-batch into m disjoint micro-batches and computing a distinct adversarial point for each micro-batch and then averaging the SAM-gradients at each of these points was proposed under the name m -sharpness. It was noted there and in follow-up work (Chen et al., 2021) that m > 1 can result in better performance. This modification incurs m -times more compute under a naive sequential implementation (though it can be parallelized well if multiple devices are available). Meanwhile, Brock et al. (2021) suggests (in the Appendix) using roughly 20% of the examples from the mini-batch for computing the adversarial point, observing little loss in model quality. With m = 1 , this approximation roughly reduces SAM's relative runtime from 2x to 1 . 2 x. Since we understand how a 2 m -x slow-down of model training may be prohibitive or significantly deter SAM's widespread adoption, we, at the possible 1 https://github.com/google-research/ sam 7363 Model Natural Q. Web Q. TriviaQA Small 16.7 / 12.4 22.8 / 16.5 10.2 / 7.3 Small + SAM (0.05) 17.5 / 13.1 23.5 / 16.9 11.0 / 7.8 Base 23.2 / 18.1 29.7 / 22.5 19.3 / 15.3 Base + SAM (0.15) 25.7 / 20.6 31.0 / 24.5 21.5 / 17.4 Large 27.4 / 22.3 34.3 / 27.6 25.2 / 20.9 Large + SAM (0.15) 30.6 / 25.0 36.4 / 29.6 28.5 / 24.2 XL 33.5 / 27.5 39.3 / 31.6 36.5 / 31.1 XL + SAM (0.15) 34.7 / 28.8 40.7 / 33.3 38.0 / 32.6 Table 4: Experimental results (F1/EM) (test scores) on the (full) CBQA tasks. loss of larger improvements, set m = 1 and use 1/4-th (25%) of the mini-batch, or the number of available training devices (TPU cores in our case), whichever is larger, to compute SAM's adversarial point. This is necessary because the mini-batch gradient computation is parallelized over devices and each device must receive at least one example. We've observed from wall-clock times that with these settings, SAM is all in all about 25% slower than standard training. Hyper-parameters. SAM has a single hyper-parameter  , which is size of the step taken along the unit adversarial gradient vector. We search the range [0 . 02 , 0 . 05 , 0 . 1 , 0 . 15 , 0 . 2 , 0 . 3] a single time only when fine-tuning on SuperGLUE. We found that 0 . 05 is a reasonable choice for T5.1.1 small models, and 0 . 15 for the Base, Large, and XL variants, and so for all subsequent experiments except for TyDiQA, we use these choices without additional tuning. For the mT5 model on Ty-DiQA, we found that a smaller  was necessary for good performance. For this, we searched the range [0 . 01 , 0 . 02 , 0 . 05] . For all fine-tuning, we use the AdaFactor optimizer with learning rate 1e-3, 128 batch size, and the T5.1.1 settings. For SuperGLUE, we use 10% dropout rate, 512 input sequence length, 62 target sequence length, and fine-tune for 250k steps. For Natural Questions, Web Questions, and TriviaQA, we use 5% dropout, 38 input sequence length, 18 target sequence length, and fine-tune for 20k steps. For TyDiQA, we use the official, public mT5 checkpoints, 10% dropout, 1024 input sequence length, 512 target sequence length, and fine-tune for 20k steps. We run each experiment once, due to resource constraints, and we take the best checkpoint (stored every 1k steps for SuperGLUE and GLUE and every 200 steps for all other datasets) across 7364 Figure 1: CBQA results at various training data sampling rates, for the Small ( top half ) and Base ( bottom half ) models. training steps. Following standard practice, we report the best checkpoint for each task-metric pair (e.g. SuperGLUE CB F1) individually. Results for SuperGLUE and GLUE are shown in Table 1 and Table 2 respectively. We observe that SAM improves the overall scores for both benchmarks across all T5 model sizes. For Base and XL sizes on SuperGLUE, SAM brings 4.2% and 2.1% relative gains in overall score respectively, while the gain for Large on GLUE is 2.4%. As shown in Table 4, on Natural Questions, Web Questions, and Trivia QA tasks, we observe improvements for each task, metric (F1 and EM), and model size. For Base, we see a 13.8%, 8.8%, and 13.7% gain on the exact match metric for Natural Questions, Web Questions, and Trivia QA respectively. For Large, these figures are 12.1%, 7.2%, and 15.7%. Table 3 shows the results for TyDiQA-GoldP. Here, we observe more modest improvements in the 1-2% range. SAM improves performance on all model sizes. In light of the conventional wisdom that larger models generalize better, we suspected, a priori, that SAM would be more helpful for the smaller models we consider, like Small and Base, and that we should expect substantial diminishing returns 7365 Model SGlue BoolQ CB CoPA MultiRC ReCoRD RTE WiC WSC Small 50.2 60.8 37.0 / 55.4 52.0 60.1 / 11.0 33.9 / 32.5 54.5 54.4 65.4 Small + SAM (0.05) 51.9 60.5 45.6 / 66.1 53.0 61.1 / 12.5 36.7 / 34.5 52.3 55.2 66.3 Base 52.9 59.6 32.3 / 55.4 53.0 60.2 / 11.8 47.8 / 46.5 58.5 57.2 68.3 Base + SAM (0.15) 56.7 61.4 41.8 / 64.3 55.0 62.4 / 15.7 59.7 / 57.9 62.5 55.3 68.3 Large 62.8 65.3 40.1 / 62.5 62.0 71.6 / 24.0 80.4 / 78.9 69.7 57.7 69.2 Large + SAM (0.15) 64.3 77.3 47.9 / 69.6 59.0 69.0 / 20.4 81.5 / 80.0 65.0 59.6 69.2 XL 75.9 84.5 57.0 / 82.1 86.0 82.4 / 48.7 83.3 / 81.7 78.7 66.0 74.0 XL + SAM (0.15) 77.0 82.5 58.9 / 83.9 85.0 79.9 / 45.3 86.8 / 85.6 80.5 64.4 83.7 Table 6: SuperGLUE results when only 5% of the training data is available. as we scale up the model size. Surprisingly, we did not observe any clear pattern with regards to size: indeed, sometimes the gains on XL were larger than those on Small. Thus, we lean to recommend SAM to all practitioners regardless of the regime in model capacity they are working in. SAM improves single-task and multi-task learning alike. Thus far, SAM has been trained on a mixture of tasks, where the influence of a particular task is proportional to the number of examples in its training split (i.e. no artificial up or down-weighting). To rule out the possibility that the gains observed are solely due to some ability of SAM's to leverage multi-task learning and improve cross-task transfer, we conduct the following ablation. For each of the three CBQA tasks, we train only on a single task and report the performance on that task's test set. Results are shown in Table 5. Indeed, we see similar gains when training and testing on each single task individually. We conclude that the mechanism driving SAM's improvements affect single-task and multi-task learning alike. We now switch gears and evaluate whether or not SAM helps when training data is scarce. Prior work (Chen et al., 2021) showed that for vision models and tasks, SAM helps more when there is less training data to learn from. To test whether this holds for language, we do as follows: we subsample the training splits for both SuperGLUE and CBQA datasets at rates ranging from 2% to 80% , and observe test performance when the public checkpoint is fine-tuned with and without SAM. SuperGLUE and CBQA results at a 5% sampling rate are shown in Tables 6 and 7 respectively. In both cases we see again that SAM boosts performance across the board, adding, for example, a whopping 7 . 2% relative improvement on the Base model on 5% SuperGLUE and a relative 8 . 86% / 16 . 6% to F1/EM on Natural Questions. Figure 1 plots the performance on the three CBQA tasks as a function of the sampling rate. We observe consistent gains from SAM across the size of the subsampled training set, with the relative improvement appearing largest when the subsampling 7366 rate is around 20% . Figure 2 shows the impact of SAM's hyper-parameters  , the ascent micro-batch size a , and the sharpness factor m on the (full) SuperGLUE benchmark for the Base model. For  , we see that all tested values perform better than fine-tuning without SAM. However, 0 . 15 is a sweet spot, performing better than values below or above it. Thus, practitioners with little computational budget for hyper-parameter tuning may still see large gains by using a non-optimal  , while those with a generous budget should consider tuning. For the ascent micro-batch size a , we see that when the normal (descent) batch size is 128, there is improvement as a is increased to 32 but little past this point. Thus, setting a to be 1/4-th the descent batch size, as we do throughout our experiments, provides a good trade-off between performance and computational overhead. Increasing the sharpness m , where each of the m ascent micro-batches has size 32 /m , does not improve performance here. We thus recommend a default of 1, which is the setting used across our experiments. Full results are shown in the Appendix. To the best of our knowledge, this paper is the first to demonstrate how the recently-proposed Sharpness-Aware Minimization can be applied for fine-tuning the ubiquitous text-to-text Transformer (T5) and its multilingual counterpart mT5 on language tasks of broad interest. We thereby corroborate the already-documented success the method has had in the vision domain. Furthermore, we reveal SAM's benefits when data is limited by fine-tuning on subsamples of the original task training split. By approximating the ascent step of the algorithm via fewer samples, we show how large gains can be had across benchmarks and model sizes while adding only around 25% additional compute and wall-clock training time. Our hope is that this work will spur SAM's adoption in the natural language processing community the way it is starting to in the vision one.
abstain abstain objective result abstain other abstain abstain abstain abstain abstain method objective result objective objective abstain other other other other other other other other other other other objective other other other other other other abstain other abstain abstain abstain other abstain abstain abstain abstain abstain abstain abstain method abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain method abstain abstain abstain abstain method method abstain abstain abstain abstain abstain abstain abstain other method abstain method abstain abstain method method abstain method method method method method method method method abstain method abstain result abstain result result abstain abstain result abstain result abstain result abstain abstain abstain result abstain abstain result abstain method abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain abstain method abstain method abstain objective abstain result result abstain