Skip to content

Add SignSGD - #130215

Closed
BeeGass wants to merge 13 commits into
pytorch:mainfrom
BeeGass:main
Closed

BeeGass wants to merge 13 commits into
pytorch:mainfrom
BeeGass:main

Conversation

@BeeGass

@BeeGass BeeGass commented Jul 7, 2024 •

Copy link
Copy Markdown

Implement SignSGD optimizer

Motivation:

SignSGD is becoming increasingly important in deep learning research and optimization. The original SignSGD paper has over 1000 citations, and sign-based methods are prevalent in modern optimizers (e.g., variants of RMSprop, Adam, and Lion). However, PyTorch currently lacks native support for SignSGD. This PR aims to address this gap by implementing the SignSGD optimizer.

Implementation details:

  1. Add SignSGD class extending torch.optim.Optimizer
  2. Implement sign-based gradient updates in _init_group method
  3. Support momentum, weight decay, and Nesterov momentum
  4. Optimize performance with foreach and fused operations
  5. Handle sparse gradients and AMP scaling
  6. Provide single-tensor and multi-tensor update functions

The implementation follows PyTorch's optimizer conventions and integrates seamlessly with existing PyTorch functionality.

Benefits:

  • Enables easier experimentation with sign-based methods
  • Provides a foundation for implementing other sign-based optimizers
  • Aligns with the growing trend of sign-based methods in optimization

Performance considerations:

  • Efficient sign computation
  • Leverages existing PyTorch foreach and fused operations for potential speedups

Documentation:

  • Added detailed docstrings following PyTorch conventions
  • Updated optimizer documentation to include SignSGD

I hope this addition makes it easier for PyTorch users to experiment with sign-based methods and define new sign-based optimizers. Looking forward to your comments and suggestions for improvement.

nullonesix and others added 13 commits July 6, 2024 21:14
Revert "sign gradient descent"
Implement SignSGD optimizer

- Add SignSGD class extending Optimizer
- Modify gradient handling to use sign instead of raw values
- Support momentum, weight decay, and Nesterov momentum
- Implement foreach and fused operations for performance
- Add multi-tensor and single-tensor update functions
- Include sparse gradient and AMP scaling support
- Provide detailed documentation and mathematical formulation
update documentation
@BeeGass
BeeGass requested review from albanD and janeyx99 as code owners July 7, 2024 01:57
@pytorch-bot

pytorch-bot Bot commented Jul 7, 2024 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/130215

Note: Links to docs will display an error until the docs builds have been completed.

❌ 73 New Failures

As of commit 91ee0af with merge base a3ce9ed (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 7, 2024 •

Copy link
Copy Markdown

CLA Signed


The committers listed above are authorized under a signed CLA.

@nullonesix

Copy link
Copy Markdown

lr = 1/n_params is recommended

@nullonesix

nullonesix commented Jul 7, 2024 •

Copy link
Copy Markdown

for example, sign sgd outperforms adamw on nanoGPT:

adamw:

step 5000: train loss 0.6171, val loss 1.6973
iter 5000: loss 0.8138, time 31669.98ms, mfu 4.02%

sign sgd:

step 5000: train loss 1.0883, val loss 1.4821
iter 5000: loss 1.1968, time 34425.73ms, mfu 3.27%

@janeyx99

janeyx99 commented Jul 9, 2024

Copy link
Copy Markdown
Contributor

Hello, thank you for taking the time to tell us about SignSGD. To save time and facilitate more open discussion, it would be better to open an issue first motivating the feature request before delving into the implementation. I do see that you added some motivation in the PR body, so I shall respond to that here:

From what I gather, this PR is intended to be a herald for more sign-based optimizers in core, starting with SignSGD. As you may already know, PyTorch is quite selective with the features we add, see https://github.com/pytorch/pytorch/wiki/Developer-FAQ#i-have-a-new-function-or-feature-id-like-to-add-to-pytorch-should-it-be-in-pytorch-core-or-a-library-like-torchvision.

From the perspective of wanting to ensure stability, composability, and extensibility of the core optimizers, I am not convinced that adding a new optimizer class for SignSGD is the right approach. PyTorch is written to promote exploration and encourages everyone to experiment with their own optimizers. To that end, it looks like SignSGD is already implemented in third party libraries such as from the OG author: https://github.com/jxbz/signSGD and also with their improved algorithm (published a year after) at https://github.com/jiaweizzhao/signSGD-with-Majority-Vote. There does not seem to be a need in adding this specific optimizer to live within core pytorch/pytorch.

The more interesting question, I believe, is how to introduce more sign based optimizations as an general option across optimizers. The way is likely not to add more and more optimizer classes with lots of duplicated code, but perhaps to add some mechanism that would sign-ify the parameter update value. If there is no generic solution that would be performant, then I would still expect for a lot of the SGD code to be shared in the solution that would be proposed. If you want to pursue this path further, please open a separate issue and we can discuss there!

@github-actions

github-actions Bot commented Sep 7, 2024

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Sep 7, 2024
@github-actions github-actions Bot closed this Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open source release notes: optim Stale triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants