This commit is contained in:
younesbelkada
2023-04-04 07:59:03 +00:00
parent 3d1e87cb78
commit c7e22ccd75
2 changed files with 16 additions and 3 deletions
+7 -1
View File
@@ -19,6 +19,7 @@ from .peft_model import (
PeftModelForSeq2SeqLM,
PeftModelForSequenceClassification,
PeftModelForTokenClassification,
PeftModelForVision2Seq,
)
from .tuners import LoraConfig, PrefixTuningConfig, PromptEncoderConfig, PromptTuningConfig
from .utils import PromptLearningConfig
@@ -29,6 +30,7 @@ MODEL_TYPE_TO_PEFT_MODEL_MAPPING = {
"SEQ_2_SEQ_LM": PeftModelForSeq2SeqLM,
"CAUSAL_LM": PeftModelForCausalLM,
"TOKEN_CLS": PeftModelForTokenClassification,
"VISION_2_SEQ": PeftModelForVision2Seq,
}
PEFT_TYPE_TO_CONFIG_MAPPING = {
@@ -44,6 +46,7 @@ TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING = {
"bart": ["q_proj", "v_proj"],
"gpt2": ["c_attn"],
"bloom": ["query_key_value"],
"blip2": ["q", "v", "q_proj", "v_proj"],
"opt": ["q_proj", "v_proj"],
"gptj": ["q_proj", "v_proj"],
"gpt_neox": ["query_key_value"],
@@ -134,9 +137,12 @@ def get_peft_model(model, peft_config):
model ([`transformers.PreTrainedModel`]): Model to be wrapped.
peft_config ([`PeftConfig`]): Configuration object containing the parameters of the Peft model.
"""
model_config = model.config.to_dict()
peft_config.base_model_name_or_path = model.__dict__.get("name_or_path", None)
if peft_config.task_type == "VISION_2_SEQ" and not isinstance(peft_config, LoraConfig):
raise ValueError("Vision2Seq task type is only supported with LORA")
if peft_config.task_type not in MODEL_TYPE_TO_PEFT_MODEL_MAPPING.keys():
peft_config = _prepare_lora_config(peft_config, model_config)
return PeftModel(model, peft_config)
+9 -2
View File
@@ -394,6 +394,7 @@ class Linear(nn.Linear, LoraLayer):
self.lora_B.eval()
def forward(self, x: torch.Tensor):
if self.disable_adapters:
if self.r > 0 and self.merged:
self.weight.data -= (
@@ -401,14 +402,20 @@ class Linear(nn.Linear, LoraLayer):
)
self.merged = False
return F.linear(x, transpose(self.weight, self.fan_in_fan_out), bias=self.bias)
result = F.linear(x, transpose(self.weight, self.fan_in_fan_out), bias=self.bias)
return result
elif self.r > 0 and not self.merged:
result = F.linear(x, transpose(self.weight, self.fan_in_fan_out), bias=self.bias)
if self.r > 0:
x = x.to(self.lora_A.weight.dtype)
result += self.lora_B(self.lora_A(self.lora_dropout(x))) * self.scaling
return result
else:
return F.linear(x, transpose(self.weight, self.fan_in_fan_out), bias=self.bias)
result = F.linear(x, transpose(self.weight, self.fan_in_fan_out), bias=self.bias)
return result
class MergedLinear(nn.Linear, LoraLayer):