Skip to content

[Fix] Match foreach and per-parameter rounding in SignSGD and Tiger - #520

Merged
kozistr merged 1 commit into
kozistr:mainfrom
11NOel11:fix-signsgd-tiger-foreach-parity
Aug 24, 2026
Merged

[Fix] Match foreach and per-parameter rounding in SignSGD and Tiger#520
kozistr merged 1 commit into
kozistr:mainfrom
11NOel11:fix-signsgd-tiger-foreach-parity

Conversation

@11NOel11

@11NOel11 11NOel11 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Problem (Why?)

Close #519

Partly addresses #519, the SignSGD / Tiger half.

_step_foreach uses _foreach_lerp_ while _step_per_param uses mul_().add_(). Same result in exact
arithmetic, different rounding, and sign() turns that into a full step when the buffer lands near zero.

Solution (What/How?)

  • use lerp_ in _step_per_param so both paths round the same way

One line in each. This keeps _foreach_lerp_ as a single kernel. Moving the foreach path to
_foreach_mul_ + _foreach_add_ instead would work equally well if you would rather match
torch.optim.SGD.

SGDW is left alone here, since fixing it changes results for anyone using it with momentum.

Other changes (bug fixes, small refactors)

N/A

Notes

Added test_sign_based_foreach_parity for both optimizers. It fails on main and passes with this change.

Checklist

  • Make sure to run just format before commit
  • My code adheres to the style guidelines of this project (just check shows no errors)
  • Both new and existing unit tests pass successfully on my local environment by running just test
  • I have made the necessary changes to the documentation

ruff check is clean; I did not run pyright. Suite: 2494 passed before, 2496 after, same two
pre-existing failures.

…ger`

`_step_foreach` uses `_foreach_lerp_` while `_step_per_param` uses
`mul_().add_()`. The two are equal in exact arithmetic but round
differently, and a `sign()` update turns that into a full step when the
momentum buffer lands near zero.

Use `lerp_` in the per-parameter path so both agree.
@11NOel11
11NOel11 requested a review from kozistr as a code owner August 24, 2026 03:53
@github-actions github-actions Bot added the optimizer about optimizer label Aug 24, 2026
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (833817c) to head (5627365).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #520   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          128       128           
  Lines        12444     12444           
=========================================
  Hits         12444     12444           
Flag Coverage Δ
3.10 100.00% <100.00%> (ø)
3.11 100.00% <100.00%> (ø)
3.12 100.00% <100.00%> (ø)
3.13 100.00% <100.00%> (ø)
3.14 100.00% <100.00%> (ø)
3.8 99.98% <100.00%> (ø)
3.9 99.98% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kozistr kozistr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me!

afair, _foreach_lerp_ landed in Pytorch 2.0, and MPS support was added in 2.4 (but, we've already used here). so maybe, it's time to bump the minimum pytorch version from 1.10 to 2.0.

anyway, thanks!

@kozistr
kozistr merged commit d880323 into kozistr:main Aug 24, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

optimizer about optimizer size/XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

foreach step paths do not match the single-tensor paths in SGDW, SignSGD and Tiger

2 participants