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 / Quantizaiton: Revert back previous replacement logic #30779

Closed
wants to merge 1 commit into from

Conversation

younesbelkada
Copy link
Contributor

What does this PR do?

Fixes: huggingface/peft#1720

#29958 introduced a new logic for linear layer replacement for bnb which led to a corner case.

Before that PR, for a BertForSequenceClassification model:

from transformers import AutoModelForSequenceClassification, BitsAndBytesConfig

config = BitsAndBytesConfig(
    load_in_4bit=True, # quantize the model to 4-bits when you load it
    bnb_4bit_quant_type="nf4", # use a special 4-bit data type for weights initialized from a normal distribution
    bnb_4bit_use_double_quant=True, # nested quantization scheme to quantize the already quantized weights
    bnb_4bit_compute_dtype=torch.bfloat16, # use bfloat16 for faster computation,
)

model = AutoModelForSequenceClassification.from_pretrained(
    model_checkpoint,
    quantization_config=config
)

print(model)

Gives:

DistilBertForSequenceClassification(
  (distilbert): DistilBertModel(
    (embeddings): Embeddings(
      (word_embeddings): Embedding(30522, 768, padding_idx=0)
      (position_embeddings): Embedding(512, 768)
      (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
      (dropout): Dropout(p=0.1, inplace=False)
    )
    (transformer): Transformer(
      (layer): ModuleList(
        (0-5): 6 x TransformerBlock(
          (attention): MultiHeadSelfAttention(
            (dropout): Dropout(p=0.1, inplace=False)
            (q_lin): Linear4bit(in_features=768, out_features=768, bias=True)
            (k_lin): Linear4bit(in_features=768, out_features=768, bias=True)
            (v_lin): Linear4bit(in_features=768, out_features=768, bias=True)
            (out_lin): Linear4bit(in_features=768, out_features=768, bias=True)
          )
          (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
          (ffn): FFN(
            (dropout): Dropout(p=0.1, inplace=False)
            (lin1): Linear4bit(in_features=768, out_features=3072, bias=True)
            (lin2): Linear4bit(in_features=3072, out_features=768, bias=True)
            (activation): GELUActivation()
          )
          (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
        )
      )
    )
  )
  (pre_classifier): Linear(in_features=768, out_features=768, bias=True)
  (classifier): Linear(in_features=768, out_features=2, bias=True)
  (dropout): Dropout(p=0.2, inplace=False)
)

But after #29958 :

DistilBertForSequenceClassification(
  (distilbert): DistilBertModel(
    (embeddings): Embeddings(
      (word_embeddings): Embedding(30522, 768, padding_idx=0)
      (position_embeddings): Embedding(512, 768)
      (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
      (dropout): Dropout(p=0.1, inplace=False)
    )
    (transformer): Transformer(
      (layer): ModuleList(
        (0-5): 6 x TransformerBlock(
          (attention): MultiHeadSelfAttention(
            (dropout): Dropout(p=0.1, inplace=False)
            (q_lin): Linear4bit(in_features=768, out_features=768, bias=True)
            (k_lin): Linear4bit(in_features=768, out_features=768, bias=True)
            (v_lin): Linear4bit(in_features=768, out_features=768, bias=True)
            (out_lin): Linear4bit(in_features=768, out_features=768, bias=True)
          )
          (sa_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
          (ffn): FFN(
            (dropout): Dropout(p=0.1, inplace=False)
            (lin1): Linear4bit(in_features=768, out_features=3072, bias=True)
            (lin2): Linear4bit(in_features=3072, out_features=768, bias=True)
            (activation): GELUActivation()
          )
          (output_layer_norm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
        )
      )
    )
  )
  (pre_classifier): Linear4bit(in_features=768, out_features=768, bias=True)
  (classifier): Linear(in_features=768, out_features=2, bias=True)
  (dropout): Dropout(p=0.2, inplace=False)
)

The pre_classifier gets also quantized. In order to preserve BC (even though it might not be 100% what is intended) I propose to add that extra condition and make sure previous behaviour is maintained

cc @SunMarc @BenjaminBossan

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

@SunMarc
Copy link
Member

SunMarc commented May 13, 2024

Before the PR, pre_classifier was not quantized because we have modules_to_not_convert = ['classifier'] and classifier is in pre_classifier. I'm not sure about the right fix. One solution would be to fix get_keys_to_not_convert in such way that we get modules_to_not_convert = ['classifier', 'pre_classifier'] or we can just pass llm_int8_skip_modules=["classifier", "pre_classifier"]. However, going back to the previous logic is not optimal.

@younesbelkada
Copy link
Contributor Author

I agree, ideally we don't want to do that .. I also mentioned here: huggingface/peft#1720 (comment) that the user should use modules_to_not_convert, IMO pre_classifier shouldn't be un-quantized at first place .

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

I'm a bit confused, as this does not really revert the other commit, but introduces some extra logic. So I'm not really sure if the exact same keys are matched as before or not :D (are there tests)?

Anyway, do what you think is right :)

(key + "." in current_key_name_str) or (key == current_key_name_str) for key in modules_to_not_convert
(key + "." in current_key_name_str)
or (key == current_key_name_str)
or (key in ".".join(current_key_name))
Copy link
Member

Choose a reason for hiding this comment

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

Why not use current_key_name_str here?

@younesbelkada
Copy link
Contributor Author

Closing as I agree there is no point reverting a bug!

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.

RuntimeError: only Tensors of floating point dtype can require gradients for QLoRA since transformers 4.40
4 participants