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

Peft model signature #784

Merged
merged 18 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/peft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
prepare_model_for_kbit_training,
set_peft_model_state_dict,
shift_tokens_right,
update_forward_signature,
load_peft_weights,
)
from .config import PeftConfig, PromptLearningConfig
1 change: 1 addition & 0 deletions src/peft/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
_prepare_prompt_learning_config,
_is_valid_match,
infer_device,
update_forward_signature,
)
from .hub_utils import hub_file_exists
from .save_and_load import get_peft_model_state_dict, set_peft_model_state_dict, load_peft_weights
38 changes: 38 additions & 0 deletions src/peft/utils/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import inspect
import os
import warnings
from functools import update_wrapper
from types import MethodType
from typing import Optional

import accelerate
Expand Down Expand Up @@ -325,6 +327,42 @@ def _get_batch_size(input_ids: Optional[torch.Tensor], inputs_embeds: Optional[t
return batch_size


def default_forward(self, *args, **kwargs):
return self.get_base_model()(*args, **kwargs)


def update_forward_signature(model):
"""
Updates the forward signature of the PeftModel to include parents class signature
Args:
model (`PeftModel`): model
Example:

```python
>>> from transformers import WhisperForConditionalGeneration
>>> from peft import get_peft_model, LoraConfig, update_forward_signature

>>> model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-tiny.en")
>>> peft_config = LoraConfig(r=8, lora_alpha=32, lora_dropout=0.1, target_modules=["q_proj", "v_proj"])

>>> peft_model = get_peft_model(model, peft_config)
>>> update_forward_signature(peft_model)
```
"""
kiansierra marked this conversation as resolved.
Show resolved Hide resolved

# Only update signature when the current forward signature only has *args and **kwargs
current_signature = inspect.signature(model.forward)
if (
len(current_signature.parameters) == 2
and "args" in current_signature.parameters
and "kwargs" in current_signature.parameters
):
update_wrapper(
default_forward, type(model.get_base_model()).forward, assigned=("__doc__", "__name__", "__annotations__")
)
model.forward = MethodType(default_forward, model)


TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING = {
"t5": ["q", "v"],
"mt5": ["q", "v"],
Expand Down