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

FIX [quantization / ESM] Fix ESM 8bit / 4bit with bitsandbytes #29329

Merged
merged 3 commits into from
Mar 1, 2024

Conversation

younesbelkada
Copy link
Contributor

@younesbelkada younesbelkada commented Feb 28, 2024

What does this PR do?

Fixes: #29323

Currently on main, simply running:

import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer

model = AutoModelForMaskedLM.from_pretrained("facebook/esm2_t36_3B_UR50D", load_in_4bit=True)

Fails with an error

  File "/home/younes_huggingface_co/code/transformers/src/transformers/modeling_utils.py", line 802, in _load_state_dict_into_meta_model
    or (not hf_quantizer.check_quantized_param(model, param, param_name, state_dict))
  File "/home/younes_huggingface_co/code/transformers/src/transformers/quantizers/quantizer_bnb_4bit.py", line 124, in check_quantized_param
    if isinstance(module._parameters[tensor_name], bnb.nn.Params4bit):
KeyError: 'inv_freq'

This is because the model pushed in "facebook/esm2_t36_3B_UR50D" do not contain the inv_freq. Maybe during the HfQuantizer refactor we did not properly dealt with that specific scenario, leading to this bug for transformers > 4.37

cc @SunMarc

I ran the quantization tests and they seem to all pass on my end

@@ -377,7 +377,7 @@ def forward(
if head_mask is not None:
attention_probs = attention_probs * head_mask

context_layer = torch.matmul(attention_probs, value_layer)
context_layer = torch.matmul(attention_probs.to(value_layer.dtype), value_layer)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was needed to perform correctly inference otherwise you get dtype mismatch

Copy link
Member

Choose a reason for hiding this comment

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

what do we get if we don't do this fix ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You get a dtype mismatch :/

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

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

LGTM !

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Thanks

src/transformers/quantizers/quantizer_bnb_4bit.py Outdated Show resolved Hide resolved
src/transformers/quantizers/quantizer_bnb_8bit.py Outdated Show resolved Hide resolved
younesbelkada and others added 2 commits March 1, 2024 02:36
Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>
@Titus-von-Koeller
Copy link
Contributor

Thanks for the quick fix, everyone!

itazap pushed a commit that referenced this pull request May 14, 2024
…29329)

* fix ESM 8bit

* Apply suggestions from code review

Co-authored-by: Arthur <48595927+ArthurZucker@users.noreply.github.com>

* fixup

---------

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

Successfully merging this pull request may close these issues.

transformers > 4.37 breaks bitsandbyte int8 inference of ESM models
5 participants