get_peft_model for quantized EsmForSequenceClassification fails due to "only Tensors of floating point dtype can require gradients"
System Info
python==3.10.8
accelerate==0.25.0 bitsandbytes==0.41.2.post2 datasets==2.15.0 peft==0.6.2 torch==2.1.1 transformers==4.35.2
ml.g5.2xlarge SageMaker notebook instance (8 vCPU + 32 GB Memory + 1 A10G GPU) Amazon Linux 2 JupyterLab 3
Who can help?
@BenjaminBossan
Information
- [ ] The official example scripts
- [X] My own modified scripts
Tasks
- [ ] An officially supported task in the
examplesfolder - [X] My own task or dataset (give details below)
Reproduction
from transformers import (
BitsAndBytesConfig,
EsmForSequenceClassification
)
from peft import (
get_peft_model,
LoraConfig,
prepare_model_for_kbit_training,
TaskType,
)
model = EsmForSequenceClassification.from_pretrained(
"facebook/esm2_t6_8M_UR50D",
quantization_config=BitsAndBytesConfig(
load_in_8bit=True,
),
)
peft_config = LoraConfig(
task_type=TaskType.SEQ_CLS,
inference_mode=False,
# bias="none",
# r=8,
# lora_alpha=16,
# lora_dropout=0.05,
target_modules=[
"query",
"key",
"value",
],
)
model = prepare_model_for_kbit_training(model, True)
model = get_peft_model(model, peft_config)
Expected behavior
I expect this code to successfully load the updated model (with quantization and LoRA adaptors) without error. Instead, it generates an error stating: "RuntimeError: only Tensors of floating point dtype can require gradients".
Please note that when I replace the EsmForSequenceClassification class with either EsmForTokenClassification or EsmForMaskedLM it completes successfully. Seems like something is amiss with the specific SEQ_CLS task type.
Full error trace:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[43], line 37
23 peft_config = LoraConfig(
24 task_type=TaskType.SEQ_CLS,
25 inference_mode=False,
(...)
34 ],
35 )
36 model = prepare_model_for_kbit_training(model, True)
---> 37 model = get_peft_model(model, peft_config)
File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/peft/mapping.py:116, in get_peft_model(model, peft_config, adapter_name)
114 if peft_config.is_prompt_learning:
115 peft_config = _prepare_prompt_learning_config(peft_config, model_config)
--> 116 return MODEL_TYPE_TO_PEFT_MODEL_MAPPING[peft_config.task_type](model, peft_config, adapter_name=adapter_name)
File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/peft/peft_model.py:799, in PeftModelForSequenceClassification.__init__(self, model, peft_config, adapter_name)
796 break
798 # to make sure classifier layer is trainable
--> 799 _set_trainable(self, adapter_name)
File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/peft/utils/other.py:274, in _set_trainable(model, adapter_name)
272 target.set_adapter(target.active_adapter)
273 else:
--> 274 new_module = ModulesToSaveWrapper(target, adapter_name)
275 new_module.set_adapter(adapter_name)
276 setattr(parent, target_name, new_module)
File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/peft/utils/other.py:172, in ModulesToSaveWrapper.__init__(self, module_to_save, adapter_name)
170 self._active_adapter = adapter_name
171 self._disable_adapters = False
--> 172 self.update(adapter_name)
File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/peft/utils/other.py:195, in ModulesToSaveWrapper.update(self, adapter_name)
193 self.original_module.requires_grad_(False)
194 if adapter_name == self.active_adapter:
--> 195 self.modules_to_save[adapter_name].requires_grad_(True)
File ~/anaconda3/envs/pytorch_p310/lib/python3.10/site-packages/torch/nn/modules/module.py:2440, in Module.requires_grad_(self, requires_grad)
2420 r"""Change if autograd should record operations on parameters in this
2421 module.
2422
(...)
2437 Module: self
2438 """
2439 for p in self.parameters():
-> 2440 p.requires_grad_(requires_grad)
2441 return self
RuntimeError: only Tensors of floating point dtype can require gradients
Hang on, I may have figured it out. Adding llm_int8_skip_modules=['classifier'] to the BitsAndBytes config seems to do the trick (for both 4bit and 8bit quant) - this prevents quantization of the EsmClassificationHead
Hi, I would like to ask you how you solved this last problem , I am also using the ESM2 model keeps reporting this error! Thank you!ðŸ˜