Add an adapter to the model based on the passed configuration. This adapter is not trained. To load a trained adapter, check out [`PeftModel.load_adapter`]. The name for the new adapter should be unique. The new adapter is not automatically set as the active adapt
(
self,
adapter_name: str,
peft_config: PeftConfig,
low_cpu_mem_usage: bool = False,
autocast_adapter_dtype: bool = True,
)
| 1018 | return self.base_model if self.active_peft_config.is_prompt_learning else self.base_model.model |
| 1019 | |
| 1020 | def add_adapter( |
| 1021 | self, |
| 1022 | adapter_name: str, |
| 1023 | peft_config: PeftConfig, |
| 1024 | low_cpu_mem_usage: bool = False, |
| 1025 | autocast_adapter_dtype: bool = True, |
| 1026 | ) -> None: |
| 1027 | """ |
| 1028 | Add an adapter to the model based on the passed configuration. |
| 1029 | |
| 1030 | This adapter is not trained. To load a trained adapter, check out [`PeftModel.load_adapter`]. |
| 1031 | |
| 1032 | The name for the new adapter should be unique. |
| 1033 | |
| 1034 | The new adapter is not automatically set as the active adapter. Use [`PeftModel.set_adapter`] to set the active |
| 1035 | adapter. |
| 1036 | |
| 1037 | Args: |
| 1038 | adapter_name (`str`): |
| 1039 | The name of the adapter to be added. |
| 1040 | peft_config ([`PeftConfig`]): |
| 1041 | The configuration of the adapter to be added. |
| 1042 | low_cpu_mem_usage (`bool`, `optional`, defaults to `False`): |
| 1043 | Create empty adapter weights on meta device. Useful to speed up the process when loading saved |
| 1044 | adapters. Don't use this option when creating a new PEFT adapter for training. |
| 1045 | autocast_adapter_dtype (`bool`, *optional*, defaults to `True`): |
| 1046 | Whether to autocast the adapter dtype. Defaults to `True`. Right now, this will only cast adapter |
| 1047 | weights using float16 and bfloat16 to float32, as this is typically required for stable training, and |
| 1048 | only affect select PEFT tuners. If set to `False`, the dtypes will stay the same as those of the |
| 1049 | corresponding layer. |
| 1050 | """ |
| 1051 | prefix = PEFT_TYPE_TO_PREFIX_MAPPING.get(peft_config.peft_type) |
| 1052 | if prefix and adapter_name in prefix: |
| 1053 | warnings.warn( |
| 1054 | f"Adapter name '{adapter_name}' should not be contained in the prefix '{prefix}'. " |
| 1055 | "This may lead to reinitialization of the adapter weights during loading." |
| 1056 | ) |
| 1057 | |
| 1058 | if peft_config.peft_type != self.peft_type: |
| 1059 | raise ValueError( |
| 1060 | f"Cannot combine adapters with different peft types. " |
| 1061 | f"Found {self.peft_type} and {peft_config.peft_type}." |
| 1062 | ) |
| 1063 | |
| 1064 | try: |
| 1065 | if peft_config.is_prompt_learning: |
| 1066 | self.peft_config[adapter_name] = peft_config |
| 1067 | peft_config = _prepare_prompt_learning_config(peft_config, self.config) |
| 1068 | self._setup_prompt_encoder(adapter_name) |
| 1069 | set_additional_trainable_modules( |
| 1070 | model=self.base_model, |
| 1071 | peft_config=peft_config, |
| 1072 | model_config=BaseTuner.get_model_config(self), |
| 1073 | adapter_name=adapter_name, |
| 1074 | ) |
| 1075 | elif peft_config.is_adaption_prompt: |
| 1076 | self.base_model.add_adapter(adapter_name, peft_config) |
| 1077 | set_additional_trainable_modules( |