Load a trained adapter into the model. The name for the new adapter should be unique. The new adapter is not automatically set as the active adapter. Use [`PeftModel.set_adapter`] to set the active adapter. Args: model_id (`str` or `os.PathLike
(
self,
model_id: Union[str, os.PathLike],
adapter_name: str,
is_trainable: bool = False,
torch_device: Optional[str] = None,
autocast_adapter_dtype: bool = True,
ephemeral_gpu_offload: bool = False,
low_cpu_mem_usage: bool = False,
key_mapping: Optional[dict[str, str]] = None,
**kwargs: Any,
)
| 1322 | warnings.warn(msg) |
| 1323 | |
| 1324 | def load_adapter( |
| 1325 | self, |
| 1326 | model_id: Union[str, os.PathLike], |
| 1327 | adapter_name: str, |
| 1328 | is_trainable: bool = False, |
| 1329 | torch_device: Optional[str] = None, |
| 1330 | autocast_adapter_dtype: bool = True, |
| 1331 | ephemeral_gpu_offload: bool = False, |
| 1332 | low_cpu_mem_usage: bool = False, |
| 1333 | key_mapping: Optional[dict[str, str]] = None, |
| 1334 | **kwargs: Any, |
| 1335 | ): |
| 1336 | """ |
| 1337 | Load a trained adapter into the model. |
| 1338 | |
| 1339 | The name for the new adapter should be unique. |
| 1340 | |
| 1341 | The new adapter is not automatically set as the active adapter. Use [`PeftModel.set_adapter`] to set the active |
| 1342 | adapter. |
| 1343 | |
| 1344 | Args: |
| 1345 | model_id (`str` or `os.PathLike`): |
| 1346 | The name of the PEFT configuration to use. Can be either: |
| 1347 | - A string, the `model id` of a PEFT configuration hosted inside a model repo on the Hugging Face |
| 1348 | Hub. |
| 1349 | - A path to a directory containing a PEFT configuration file saved using the `save_pretrained` |
| 1350 | method (`./my_peft_config_directory/`). |
| 1351 | adapter_name (`str`): |
| 1352 | The name of the adapter to be added. |
| 1353 | is_trainable (`bool`, *optional*, defaults to `False`): |
| 1354 | Whether the adapter should be trainable or not. If `False`, the adapter will be frozen and can only be |
| 1355 | used for inference. |
| 1356 | torch_device (`str`, *optional*, defaults to None): |
| 1357 | The device to load the adapter on. If `None`, the device will be inferred. |
| 1358 | autocast_adapter_dtype (`bool`, *optional*, defaults to `True`): |
| 1359 | Whether to autocast the adapter dtype. Defaults to `True`. Right now, this will only cast adapter |
| 1360 | weights using float16 and bfloat16 to float32, as this is typically required for stable training, and |
| 1361 | only affect select PEFT tuners. If set to `False`, the dtypes will stay the same as those of the |
| 1362 | corresponding layer. |
| 1363 | ephemeral_gpu_offload (`bool`, *optional*, defaults to `False`): |
| 1364 | Whether to use ephemeral GPU offloading for partially loaded modules. Defaults to `False`. |
| 1365 | low_cpu_mem_usage (`bool`, `optional`, defaults to `False`): |
| 1366 | Create empty adapter weights on meta device before loading the saved weights. Useful to speed up the |
| 1367 | process. |
| 1368 | key_mapping (dict, *optional*, defaults to None) |
| 1369 | Extra mapping of PEFT `state_dict` keys applied before loading the `state_dict`. When this mapping is |
| 1370 | applied, the PEFT-specific `"base_model.model"` prefix is removed beforehand and the adapter name (e.g. |
| 1371 | `"default"`) is not inserted yet. Only pass this argument if you know what you're doing. |
| 1372 | kwargs: (`optional`): |
| 1373 | Additional arguments to modify the way the adapter is loaded, e.g. the token for Hugging Face Hub. |
| 1374 | """ |
| 1375 | from .mapping import PEFT_TYPE_TO_CONFIG_MAPPING |
| 1376 | |
| 1377 | hf_hub_download_kwargs, kwargs = self._split_kwargs(kwargs) |
| 1378 | if torch_device is None: |
| 1379 | torch_device = infer_device() |
| 1380 | |
| 1381 | if adapter_name not in self.peft_config: |