Skip to content

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

@pytorch-bot
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.

❌ 2 New Failures

As of commit d0278ab with merge base 4f8021f (image):

NEW FAILURES - The following jobs have failed:

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

@facebook-github-bot
Copy link
Contributor

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 cla@meta.com. Thanks!

@facebook-github-bot
Copy link
Contributor

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.

Comment on lines +32 to +51
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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you try this? I don't think you need to use id(p) explicitly.

@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.
@nighting0le01 nighting0le01 force-pushed the asahni/low_bit_optim_layerwise branch from 8d7f968 to a0dc6a9 Compare December 5, 2024 14:28
@nighting0le01
Copy link
Author

@gau-nernst hi, sorry i was OOO for the last month, can you please run CI/CD now? verified test case passing locally

@nighting0le01
Copy link
Author

@gau-nernst hi, sorry i was OOO for the last month, can you please run CI/CD now? verified test case passing locally

@gau-nernst ran ruff check also now

@nighting0le01
Copy link
Author

nighting0le01 commented Dec 5, 2024

topic: new feature can you please add this @gau-nernst . or any other topic that is relevant

@gau-nernst gau-nernst added the topic: new feature Use this tag if this PR adds a new feature label Dec 5, 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.

Ruff lint is still failing. Can you double check?

Failing CUDA night seems to be unrelated.

Requested some changes because some of the code has been changed since you last opened this PR. Lmk if you have any questions.

Comment on lines +35 to +39
from torchao.utils import (
TORCH_VERSION_AT_LEAST_2_3,
TORCH_VERSION_AT_LEAST_2_4,
TORCH_VERSION_AT_LEAST_2_6,
)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't reimport these

loss1, loss2, msg=lambda msg: f"Iteration {idx}. {msg}"
)

@pytest.mark.skipif(not TORCH_VERSION_AT_LEAST_2_3, reason="requires PyTorch >= 2.3")
Copy link
Collaborator

Choose a reason for hiding this comment

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

In our CI, min PyTorch version is 2.3. We don't need to check >=2.3 anymore. You can remove this line

Comment on lines +32 to +51
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.

Can you try this? I don't think you need to use id(p) explicitly.


# follow bitsandbytes, only quantize tensors >= 4096 values
if local_p.numel() >= 4096 and local_p.numel() % self.block_size == 0:
if p.numel() >= 4096 and p.numel() % self.block_size == 0 and id(p) not in self.exclude_low_bit_optim_params_ids:
Copy link
Collaborator

Choose a reason for hiding this comment

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

You should keep using local_p here for FSDP to work correctly (the check on divisibility should be done on local tensor, not the full tensor)

Furthermore, the check id(p) not in self.exclude_low_bit_optim_params_ids should be done before this. Just short-circuit it e.g. (if p in self.exclude_low_bit_optim_params: return torch.zeros_like(p))

@jcaip
Copy link
Contributor

jcaip commented Mar 19, 2025

cc @nighting0le01 are you still planning on working on this?

@nighting0le01
Copy link
Author

nighting0le01 commented Mar 19, 2025 via email

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. topic: new feature Use this tag if this PR adds a new feature

6 participants