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

remove to restriction for 4-bit model #33122

Merged
merged 5 commits into from
Sep 2, 2024
Merged

remove to restriction for 4-bit model #33122

merged 5 commits into from
Sep 2, 2024

Conversation

SunMarc
Copy link
Member

@SunMarc SunMarc commented Aug 26, 2024

What does this PR do ?

Since bnb 0.43.0, you freely move bnb models across devices. This PR removes the restriction we put in place.
Needs to be tested. cc @matthewdouglas

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@matthewdouglas
Copy link
Member

Thanks @SunMarc! I've tested moving between gpu->cpu->gpu, but not yet on multiple GPUs. We'll still see a warning from accelerate:

You shouldn't move a model that is dispatched using accelerate hooks.

@matthewdouglas
Copy link
Member

Reference note: this should fix #24540 for 4bit.

For 8bit there is still a blocker: bitsandbytes-foundation/bitsandbytes#1332; once that's fixed & released on the bitsandbytes side we can do an additional PR.

SunMarc and others added 2 commits August 27, 2024 14:15
Co-authored-by: Matthew Douglas <38992547+matthewdouglas@users.noreply.github.com>
if getattr(self, "quantization_method", None) == QuantizationMethod.BITS_AND_BYTES:
if getattr(self, "is_loaded_in_4bit", False):
if version.parse(importlib.metadata.version("bitsandbytes")) < version.parse("0.43.0"):
if version.parse(importlib.metadata.version("bitsandbytes")) < version.parse("0.43.2"):
Copy link
Member

Choose a reason for hiding this comment

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

@SunMarc I've bumped this to 0.43.2 since that's when bitsandbytes-foundation/bitsandbytes#1279 was landed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice, thanks for updating the PR !

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! This looks good

Comment on lines 2872 to 2876
raise ValueError(
"Calling `cuda()` is not supported for `4-bit` quantized models. Please use the model as it is, since the"
" model has already been set to the correct devices and casted to the correct `dtype`. "
"However, if you still want to move the model, you need to install bitsandbytes >= 0.43.2 "
)
Copy link
Member

Choose a reason for hiding this comment

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

The warning isn't super clear to me in terms of what the user should or should not do; should they install the new version or should they just let the model there? I'd try to clarify this a bit

Copy link
Member

Choose a reason for hiding this comment

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

Good feedback, thanks! Updated. I think in most cases the user would be using .cuda() without realizing it is already on a GPU so I put the current model.device in the message. That should help inform on whether they really meant to move it somewhere else and need to upgrade.

@SunMarc SunMarc merged commit 9ea1eac into main Sep 2, 2024
24 checks passed
@SunMarc SunMarc deleted the remove_to_4bit branch September 2, 2024 14:28
sayakpaul added a commit to huggingface/diffusers that referenced this pull request Sep 3, 2024
zucchini-nlp pushed a commit to zucchini-nlp/transformers that referenced this pull request Sep 3, 2024
* remove to restiction for 4-bit model

* Update src/transformers/modeling_utils.py

Co-authored-by: Matthew Douglas <38992547+matthewdouglas@users.noreply.github.com>

* bitsandbytes: prevent dtype casting while allowing device movement with .to or .cuda

* quality fix

* Improve warning message for .to() and .cuda() on bnb quantized models

---------

Co-authored-by: Matthew Douglas <38992547+matthewdouglas@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants