0298 STATIC SOURCE EXCERPTS
Derived from verification/0298/text.txt; SHA256 b66701e3a3bc869f61b56146879c254ee4c9c7e696dbdf8aae2636f04610be0c
Original papers/0298/0298.pdf SHA256 07535694d19cf2eca9ac919a5718f3900f42a8555290c45c1827d19912768ec2
Source excerpts only; no author code executed.

=== PDF page 17: Appendix C / Listing 1 ===
                         Avoiding spurious sharpness minimization broadens applicability of SAM

C. Code Snippets for SAM and F UNCTIONAL -SAM
from jax import grad, vjp
from jax.tree_util import tree_map
from utils import normalize_grad

def sam_gradients(params, loss_fn, rho):

     # compute the usual loss gradient
     dL_dtheta = grad(loss_fn)(params)

     # normalize the gradients
     dL_dtheta = normalize_grad(dL_dtheta)

     # perturb the parameters
     perturbed_params = tree_map(lambda a, b: a + rho * b, params, dL_dtheta)

     # compute the gradient as by SAM
     sam_grad = grad(loss_fn)(perturbed_params)

     return sam_grad


def functional_sam_gradients(params, loss_fn, network_fn, rho):

     # compute the usual loss gradient, but also extract dL_dlogits
     (dL_dlogits), dL_dtheta = grad(loss_fn, hax_aux=True)(params)

     # normalize the gradients
     dL_dtheta = normalize_grad(dL_dtheta)

     # perturb the parameters
     perturbed_params = tree_map(lambda a, b: a + rho * b, params, dL_dtheta)

     # set up the VJP at the perturbed parameters
     _, dF_dtheta_fn = vjp(lambda theta: network_fn(theta), perturbed_params)

     # do the VJP with the (unperturbed) dL_dlogits
     functional_sam_grad = dF_dtheta_fn(dL_dlogits)[0]

     return functional_sam_grad
Listing 1: Illustration of how to get the gradients in the two methods. Functional SAM differs from the SAM implementation
only in the last couple lines




                                                           17

=== Additional literal excerpts; contextual extracted-text line numbers ===
491: In Tables 6 and 7 of the appendix, we also illustrate how
492: the results fare if a common learning rate of 0.0001 is used           Here, we confirm that the generalization benefits imparted
493: across all model scales. Interestingly, we find that PRECOND           by PRECOND F UNCTIONAL -SAM are brought about
494: F UNCTIONAL -SAM is much less sensitive to the lack of                 convergence to a solution with lower curvature, as shown
495: rightly tuned learning rate than the baselines. As a matter            in the Table 4 for the 23.9M model. Similar results can also
496: of fact, in such settings we find the corresponding gains to           be found on other model sizes, and Table 9 of the Appendix

904: 
905: Table 6: Evaluation loss comparison of different methods in a fixed-length (10K steps) training setup at a common learning
906: rate of 0.001. Lower is better.
907: 
908:                                               PRECOND        PRECOND
909:                                   S IZE                                   SAM     A DAM W

916: 
917: Table 7: Evaluation loss comparison of different methods in Chinchilla like training (Everett et al., 2024) at a common
918: learning rate of 0.001. Lower is better.
919: 
920:                                               PRECOND        PRECOND
921:                                   S IZE                                   SAM     A DAM W

890: Table 5: Evaluation loss comparison of different methods in Chinchilla like training (Everett et al., 2024), where the optimal
891: learning rate is found for each model size separately. All results have been averaged over 5 seeds, except for the more
892: expensive billion parameter runs which are based on a single seed. Lower is better.
893: 
894:                                   PRECOND            PRECOND
895:                    S IZE                                                   SAM               A DAM W
