Peft model for token classification tasks. Args: model ([`~transformers.PreTrainedModel`]): Base transformer model. peft_config ([`PeftConfig`]): Peft config. **Attributes**: - **config** ([`~transformers.PretrainedConfig`]) -- The configuration object of the b
| 1006 | |
| 1007 | |
| 1008 | class PeftModelForTokenClassification(PeftModel): |
| 1009 | """ |
| 1010 | Peft model for token classification tasks. |
| 1011 | |
| 1012 | Args: |
| 1013 | model ([`~transformers.PreTrainedModel`]): Base transformer model. |
| 1014 | peft_config ([`PeftConfig`]): Peft config. |
| 1015 | |
| 1016 | **Attributes**: |
| 1017 | - **config** ([`~transformers.PretrainedConfig`]) -- The configuration object of the base model. |
| 1018 | - **cls_layer_name** (`str`) -- The name of the classification layer. |
| 1019 | |
| 1020 | Example: |
| 1021 | |
| 1022 | ```py |
| 1023 | >>> from transformers import AutoModelForSequenceClassification |
| 1024 | >>> from utils.my_peft import PeftModelForTokenClassification, get_peft_config |
| 1025 | |
| 1026 | >>> config = { |
| 1027 | ... "peft_type": "PREFIX_TUNING", |
| 1028 | ... "task_type": "TOKEN_CLS", |
| 1029 | ... "inference_mode": False, |
| 1030 | ... "num_virtual_tokens": 20, |
| 1031 | ... "token_dim": 768, |
| 1032 | ... "num_transformer_submodules": 1, |
| 1033 | ... "num_attention_heads": 12, |
| 1034 | ... "num_layers": 12, |
| 1035 | ... "encoder_hidden_size": 768, |
| 1036 | ... "prefix_projection": False, |
| 1037 | ... "postprocess_past_key_value_function": None, |
| 1038 | ... } |
| 1039 | |
| 1040 | >>> peft_config = get_peft_config(config) |
| 1041 | >>> model = AutoModelForTokenClassification.from_pretrained("bert-base-cased") |
| 1042 | >>> peft_model = PeftModelForTokenClassification(model, peft_config) |
| 1043 | >>> peft_model.print_trainable_parameters() |
| 1044 | trainable params: 370178 || all params: 108680450 || trainable%: 0.3406113979101117 |
| 1045 | ``` |
| 1046 | """ |
| 1047 | |
| 1048 | def __init__(self, model, peft_config: PeftConfig = None, adapter_name="default"): |
| 1049 | super().__init__(model, peft_config, adapter_name) |
| 1050 | if self.modules_to_save is None: |
| 1051 | self.modules_to_save = {"classifier", "score"} |
| 1052 | else: |
| 1053 | self.modules_to_save.update({"classifier", "score"}) |
| 1054 | |
| 1055 | for name, _ in self.base_model.named_children(): |
| 1056 | if any(module_name in name for module_name in self.modules_to_save): |
| 1057 | self.cls_layer_name = name |
| 1058 | break |
| 1059 | |
| 1060 | # to make sure classifier layer is trainable |
| 1061 | _set_trainable(self, adapter_name) |
| 1062 | |
| 1063 | def forward( |
| 1064 | self, |
| 1065 | input_ids=None, |
nothing calls this directly
no outgoing calls
no test coverage detected