[Fix] Apply weight decay in SignSGD - #518
Merged
Merged
Conversation
`weight_decay` and `weight_decouple` were validated and stored in `defaults` but never used, so both were silently ignored. Apply them in both step paths, after gradient maximisation and before the momentum buffer update, following `Tiger`. `SGDW` decays after the buffer update, which works there because `grad` aliases the buffer; in `SignSGD` the two stay separate, so coupled decay would be dropped.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #518 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 128 128
Lines 12442 12444 +2
=========================================
+ Hits 12442 12444 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
kozistr
approved these changes
Aug 23, 2026
kozistr
left a comment
Owner
There was a problem hiding this comment.
looks good to me! thanks for your contribution :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (Why?)
close #517
SignSGDvalidatesweight_decayand stores it withweight_decoupleindefaults, but neither isused in either step path, so both are silently ignored.
Solution (What/How?)
_step_foreachand_step_per_paramBoth calls go after gradient maximisation and before the momentum buffer update, following
Tiger.SGDWapplies it after the buffer update instead, which is fine there becausegrad = bufaliases thebuffer; in
SignSGDthe buffer and the gradient stay separate, so decay applied after the lerp would bedropped for the coupled case.
fixed_decay=Falseis hardcoded as inSGDW, sinceSignSGDhas nofixed_decayargument. Happy toadd one if you would rather have it configurable.
Other changes (bug fixes, small refactors)
N/A
Notes
This changes results for anyone currently passing a non-zero
weight_decay, since it did nothingbefore. The default path is unaffected: at
weight_decay=0.0the parameters are bit-identical tomainacross momentum,
weight_decoupleandforeachsettings.One thing I noticed while testing, not caused by this change: the two step paths can disagree when the
momentum buffer lands near zero, since
_step_per_paramusesbuf.mul_().add_()and_step_foreachuses_foreach_lerp_- algebraically equal, different rounding, andsign()turns1e-17into a fulllrstep. It reproduces on
mainatweight_decay=0.0. Happy to file it separately.Three tests added, each parametrised over both
foreachpaths. Withp=2.0, zero gradient,lr=0.1,momentum=0.9,weight_decay=0.2: decoupled gives2.0 * (1 - 0.2 * 0.1) = 1.96, coupled givesgrad = 0.2 * 2.0 = 0.4so the update is-lr * sign(0.04) = -0.1andp = 1.9. The four decay testsfail on
mainand pass with this change; theweight_decay=0.0test passes on both.Checklist
just formatbefore commitjust checkshows no errors)just testruff checkis clean on both files; I did not runpyright. Test suite: 2488 passed before, 2494 after,with the same two pre-existing failures (
test_complex_optimizers[FTRL_...]andtest_parse_version).