Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support exlusion of params when using low bit optim #1225

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

nighting0le01
Copy link

This PR allows, exclusion and inclusion of params layerwise when using low bit optimizers. this will allow for improving stability by running certain layers with 32 bit adam. https://huggingface.co/docs/bitsandbytes/main/en/optimizers

Copy link

pytorch-bot bot commented Nov 5, 2024

🔗 Helpful Links

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

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

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

@facebook-github-bot
Copy link

Hi @nighting0le01!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 6, 2024
Copy link
Collaborator

@gau-nernst gau-nernst left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! Left some small comments.

test/prototype/test_low_bit_optim.py Outdated Show resolved Hide resolved
test/prototype/test_low_bit_optim.py Outdated Show resolved Hide resolved
test/prototype/test_low_bit_optim.py Outdated Show resolved Hide resolved
test/prototype/test_low_bit_optim.py Outdated Show resolved Hide resolved
Comment on lines +32 to +34
self.exclude_low_bit_optim_params_ids = set(
id(p) for p in exclude_low_bit_optim_params
) if exclude_low_bit_optim_params else set()
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can hash tensor directly (it will use object id internally). PyTorch optimizer already hashes tensors when it uses params as keys in self.state.

torchao/prototype/low_bit_optim/adam.py Show resolved Hide resolved
@nighting0le01
Copy link
Author

@gau-nernst shall i also add configurable min_8bit_size like https://github.com/bitsandbytes-foundation/bitsandbytes/blob/9568735b21b9325e4789d6a5004517f2287f47c8/bitsandbytes/optim/optimizer.py#L603

over here :

if p.numel() >= 4096 and p.numel() % self.block_size == 0:

@gau-nernst
Copy link
Collaborator

@nighting0le01 Adding something like min_8bit_size should be good. Though personally I don't know if having it is useful in any ways (does anyone use it / does adjust it help with stability?) If you still want to add it, maybe we can call it more generic, like min_size_for_low_bit, since we also have 4-bit and FP8.

Do you mind rebase/merge from main and make sure the tests pass?

@nighting0le01
Copy link
Author

hi @gau-nernst !

  1. yes i have rebased and confirmed all test cases pass.
  2. min_size_for_low_bit. why i propose this is to allow running gradient exploding or unstable layers in 32bit precision. similar motivation to https://huggingface.co/docs/bitsandbytes/main/en/optimizers#optimize-unstable-parameters
  3. i can push it in another PR if you suggest

@gau-nernst
Copy link
Collaborator

gau-nernst commented Nov 7, 2024

  1. There are conflicts in your branch, hence I can't run the CI. Do you mind double-check? (from Github UI it shows test_low_bit_optim.py and adam.py have conflicts) Seem like you rebase from an outdated main? The diff for this PR looks kinda strange (there are changes in unwanted places)
  2. From what I understand min_size_for_low_bit (or the original min_8bit_size) is to skip small params that don't contribute much memory savings if we use low-bit optim state for them (e.g. biases, norm params). How would it improve exploding gradients or instability? Usually instability appears in embedding layer or LM head I think (correct me if I'm wrong), which are large params but receive (somewhat) sparse gradients. In other words, how does increasing (or decreasing) the threshold help to improve stability? With this PR, the users can already select which specific params they want to keep optim state in original precision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants