Skip to content

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

Merged
younesbelkada merged 3 commits intohuggingface:mainfrom
younesbelkada:fix-8bit-esm
Mar 1, 2024
Merged

FIX [quantization / ESM] Fix ESM 8bit / 4bit with bitsandbytes#29329
younesbelkada merged 3 commits intohuggingface:mainfrom
younesbelkada:fix-8bit-esm

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

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

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!

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