A Fix for Softmax and Cross-Entropy explains loss spikes in grokking as a cycle: margins pass the precision cliff, target gradients round to exactly zero, Adam's second moment decays, weight decay pulls the margins back, and the gradient that switches back on is divided by a tiny . Reproduce the cycle with the smallest possible model: a single scalar margin whose loss is , trained with AdamW.
simulate_margin(steps, lam, eps, lr, dtype, fixed, beta1=0.9, beta2=0.98)
fixed=False, the standard gradient in dtype (np.float64, np.float32 or np.float16): compute in float64 and round it to dtype; form with the addition and the division performed in dtype; the gradient is in dtype, converted to a Python float. Past the cliff rounds to 1 and the gradient is exactly 0.0.fixed=True: the gradient is in float64 (the paper's hard-target fix for one example), never zero."margins" (the list of after each step, each rounded to 4 decimals), "final" and "peak" (last and largest margin, rounded to 4 decimals), "zero_steps" (number of steps whose gradient was exactly zero), "spikes" (number of steps whose gradient is nonzero while the previous step's gradient was exactly zero: a Slingshot, the moment a vanished gradient turns back on and meets a decayed second moment) and "max_drop" (the largest one-step decrease of , or 0.0 if it never decreases, rounded to 4 decimals).The float16 cliff is at (11-bit significand) rather than bfloat16's 5.5; float32's is at 16.6 and float64's at 36.7. The tests use so that the whole story fits in 500 steps.
np.asarray(x, dtype=dtype) rounds a float64 to the target format; the subsequent 1 + e and 1 / (...) must use dtype operands on both sides or numpy promotes back to float64.max_drop measures how far weight decay pulls while the gradient is switched off.