mirror of
https://github.com/wassname/peft.git
synced 2026-09-09 11:28:32 +08:00
getting rid to forward call linking
This commit is contained in:
+6
-3
@@ -21,7 +21,7 @@ from .peft_model import (
|
||||
PeftModelForTokenClassification,
|
||||
)
|
||||
from .tuners import LoraConfig, PrefixTuningConfig, PromptEncoderConfig, PromptTuningConfig
|
||||
from .utils import PromptLearningConfig
|
||||
from .utils import PeftType, PromptLearningConfig
|
||||
|
||||
|
||||
MODEL_TYPE_TO_PEFT_MODEL_MAPPING = {
|
||||
@@ -135,8 +135,11 @@ def get_peft_model(model, peft_config):
|
||||
|
||||
model_config = model.config.to_dict()
|
||||
peft_config.base_model_name_or_path = model.__dict__.get("name_or_path", None)
|
||||
if not isinstance(peft_config, PromptLearningConfig):
|
||||
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)
|
||||
peft_config = _prepare_prompt_learning_config(peft_config, model_config)
|
||||
if not isinstance(peft_config, PromptLearningConfig):
|
||||
peft_config = _prepare_lora_config(peft_config, model_config)
|
||||
else:
|
||||
peft_config = _prepare_prompt_learning_config(peft_config, model_config)
|
||||
return MODEL_TYPE_TO_PEFT_MODEL_MAPPING[peft_config.task_type](model, peft_config)
|
||||
|
||||
@@ -76,7 +76,6 @@ class PeftModel(PushToHubMixin, torch.nn.Module):
|
||||
if getattr(self.peft_config, "modules_to_save", None) is not None:
|
||||
self.modules_to_save = self.peft_config.modules_to_save
|
||||
_set_trainable(self)
|
||||
self.forward = self.base_model.forward
|
||||
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
|
||||
def save_pretrained(self, save_directory, **kwargs):
|
||||
@@ -246,6 +245,8 @@ class PeftModel(PushToHubMixin, torch.nn.Module):
|
||||
|
||||
def __getattr__(self, name: str):
|
||||
"""Forward missing attributes to the wrapped module."""
|
||||
if name == "forward":
|
||||
return getattr(self.base_model, name)
|
||||
try:
|
||||
return super().__getattr__(name) # defer to nn.Module's logic
|
||||
except AttributeError:
|
||||
|
||||
@@ -115,7 +115,6 @@ class LoraModel(torch.nn.Module):
|
||||
self.model = model
|
||||
self._find_and_replace()
|
||||
mark_only_lora_as_trainable(self.model, self.peft_config.bias)
|
||||
self.forward = self.model.forward
|
||||
|
||||
def _find_and_replace(self):
|
||||
kwargs = {
|
||||
@@ -174,6 +173,8 @@ class LoraModel(torch.nn.Module):
|
||||
|
||||
def __getattr__(self, name: str):
|
||||
"""Forward missing attributes to the wrapped module."""
|
||||
if name == "forward":
|
||||
return getattr(self.model, name)
|
||||
try:
|
||||
return super().__getattr__(name) # defer to nn.Module's logic
|
||||
except AttributeError:
|
||||
|
||||
Reference in New Issue
Block a user