forked from huggingface/peft
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DOC: How to configure new transformers models (huggingface#1195)
I believe that new transformers architectures could be the most common case of users wanting to apply PEFT on a model that is not supported out of the box. Thus I added a section specifically to help users configure their configs for new transformers models. As I wanted to point users to a single file that contains all the existing transformers models, I added a new file `src/peft/utils/constants.py`, which contains all the mappings that previously lived in `src/peft/utils/other.py`. LMK if that makes sense. Notes To be absolutely backwards compatible, I re-imported the moved constants into `other.py`. This way, if there is code that imports them directly from there, it should continue to work. To avoid getting a linter error for unused imports, I added those constants to the `__all__` list in `other.py`. --------- Co-authored-by: Younes Belkada <[email protected]>
- Loading branch information
1 parent
1a7433b
commit c22a8e5
Showing
3 changed files
with
202 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# coding=utf-8 | ||
# Copyright 2023-present the HuggingFace Inc. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import torch | ||
|
||
|
||
# needed for prefix-tuning of bloom model | ||
def bloom_model_postprocess_past_key_value(past_key_values): | ||
past_key_values = torch.cat(past_key_values) | ||
total_layers, batch_size, num_attention_heads, num_virtual_tokens, head_dim = past_key_values.shape | ||
keys = past_key_values[: total_layers // 2] | ||
keys = keys.transpose(2, 3).reshape( | ||
total_layers // 2, batch_size * num_attention_heads, head_dim, num_virtual_tokens | ||
) | ||
values = past_key_values[total_layers // 2 :] | ||
values = values.reshape(total_layers // 2, batch_size * num_attention_heads, num_virtual_tokens, head_dim) | ||
|
||
return tuple(zip(keys, values)) | ||
|
||
|
||
# needed for prefix-tuning of StarCoder models | ||
def starcoder_model_postprocess_past_key_value(past_key_values): | ||
result = [] | ||
for k in past_key_values: | ||
k = k[:, :, 0] | ||
k = k.permute([1, 2, 0, 3]) | ||
k = k.reshape(*k.shape[:-2], -1) | ||
result.append(k) | ||
return tuple(result) | ||
|
||
|
||
TRANSFORMERS_MODELS_TO_PREFIX_TUNING_POSTPROCESS_MAPPING = { | ||
"bloom": bloom_model_postprocess_past_key_value, | ||
"gpt_bigcode": starcoder_model_postprocess_past_key_value, | ||
} | ||
|
||
|
||
TRANSFORMERS_MODELS_TO_LORA_TARGET_MODULES_MAPPING = { | ||
"t5": ["q", "v"], | ||
"mt5": ["q", "v"], | ||
"bart": ["q_proj", "v_proj"], | ||
"gpt2": ["c_attn"], | ||
"bloom": ["query_key_value"], | ||
"blip-2": ["q", "v", "q_proj", "v_proj"], | ||
"opt": ["q_proj", "v_proj"], | ||
"gptj": ["q_proj", "v_proj"], | ||
"gpt_neox": ["query_key_value"], | ||
"gpt_neo": ["q_proj", "v_proj"], | ||
"bert": ["query", "value"], | ||
"roberta": ["query", "value"], | ||
"xlm-roberta": ["query", "value"], | ||
"electra": ["query", "value"], | ||
"deberta-v2": ["query_proj", "value_proj"], | ||
"deberta": ["in_proj"], | ||
"layoutlm": ["query", "value"], | ||
"llama": ["q_proj", "v_proj"], | ||
"chatglm": ["query_key_value"], | ||
"gpt_bigcode": ["c_attn"], | ||
"mpt": ["Wqkv"], | ||
"RefinedWebModel": ["query_key_value"], | ||
"RefinedWeb": ["query_key_value"], | ||
"falcon": ["query_key_value"], | ||
"btlm": ["c_proj", "c_attn"], | ||
"codegen": ["qkv_proj"], | ||
"mistral": ["q_proj", "v_proj"], | ||
"stablelm": ["q_proj", "v_proj"], | ||
"phi": ["Wqkv", "out_proj", "fc1", "fc2"], | ||
} | ||
|
||
TRANSFORMERS_MODELS_TO_IA3_TARGET_MODULES_MAPPING = { | ||
"t5": ["k", "v", "wo"], | ||
"mt5": ["k", "v", "wi_1"], | ||
"gpt2": ["c_attn", "mlp.c_proj"], | ||
"bloom": ["query_key_value", "mlp.dense_4h_to_h"], | ||
"roberta": ["key", "value", "output.dense"], | ||
"opt": ["q_proj", "k_proj", "fc2"], | ||
"gptj": ["q_proj", "v_proj", "fc_out"], | ||
"gpt_neox": ["query_key_value", "dense_4h_to_h"], | ||
"gpt_neo": ["q_proj", "v_proj", "c_proj"], | ||
"bart": ["q_proj", "v_proj", "fc2"], | ||
"gpt_bigcode": ["c_attn", "mlp.c_proj"], | ||
"llama": ["k_proj", "v_proj", "down_proj"], | ||
"bert": ["key", "value", "output.dense"], | ||
"deberta-v2": ["key_proj", "value_proj", "output.dense"], | ||
"deberta": ["in_proj", "output.dense"], | ||
"RefinedWebModel": ["query_key_value", "dense_4h_to_h"], | ||
"RefinedWeb": ["query_key_value", "dense_4h_to_h"], | ||
"falcon": ["query_key_value", "dense_4h_to_h"], | ||
} | ||
|
||
TRANSFORMERS_MODELS_TO_IA3_FEEDFORWARD_MODULES_MAPPING = { | ||
"t5": ["wo"], | ||
"mt5": [], | ||
"gpt2": ["mlp.c_proj"], | ||
"bloom": ["mlp.dense_4h_to_h"], | ||
"roberta": ["output.dense"], | ||
"opt": ["fc2"], | ||
"gptj": ["fc_out"], | ||
"gpt_neox": ["dense_4h_to_h"], | ||
"gpt_neo": ["c_proj"], | ||
"bart": ["fc2"], | ||
"gpt_bigcode": ["mlp.c_proj"], | ||
"llama": ["down_proj"], | ||
"bert": ["output.dense"], | ||
"deberta-v2": ["output.dense"], | ||
"deberta": ["output.dense"], | ||
"RefinedWeb": ["dense_4h_to_h"], | ||
"RefinedWebModel": ["dense_4h_to_h"], | ||
"falcon": ["dense_4h_to_h"], | ||
} | ||
|
||
TRANSFORMERS_MODELS_TO_ADALORA_TARGET_MODULES_MAPPING = { | ||
"t5": ["q", "k", "v", "o", "wi", "wo"], | ||
"mt5": ["q", "k", "v", "o", "wi_0", "wi_1", "wo"], | ||
"bart": ["q_proj", "k_proj", "v_proj", "out_proj", "fc1", "fc2"], | ||
"gpt2": ["c_attn"], | ||
"bloom": ["query_key_value"], | ||
"opt": ["q_proj", "k_proj", "v_proj", "out_proj", "fc1", "fc2"], | ||
"gptj": ["q_proj", "v_proj"], | ||
"gpt_neox": ["query_key_value"], | ||
"gpt_neo": ["q_proj", "v_proj"], | ||
"llama": ["q_proj", "v_proj"], | ||
"bert": ["query", "value"], | ||
"roberta": ["query", "key", "value", "dense"], | ||
# "xlm-roberta": ["query", "value"], | ||
# "electra": ["query", "value"], | ||
"deberta-v2": ["query_proj", "key_proj", "value_proj", "dense"], | ||
"gpt_bigcode": ["c_attn"], | ||
"deberta": ["in_proj"], | ||
# "layoutlm": ["query", "value"], | ||
} | ||
|
||
COMMON_LAYERS_PATTERN = ["layers", "h", "block", "blocks", "layer"] | ||
WEIGHTS_NAME = "adapter_model.bin" | ||
SAFETENSORS_WEIGHTS_NAME = "adapter_model.safetensors" | ||
CONFIG_NAME = "adapter_config.json" | ||
EMBEDDING_LAYER_NAMES = ["embed_tokens", "lm_head"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters