PEFT low level API
Last updated
Last updated
Letโs cover in this section how you can leverage PEFTโs low level API to inject trainable adapters into any torch
module. The development of this API has been motivated by the need for super users to not rely on modeling classes that are exposed in PEFT library and still be able to use adapter methods such as LoRA, IA3 and AdaLoRA.
Currently the supported adapter types are the โinjectableโ adapters, meaning adapters where an inplace modification of the model is sufficient to correctly perform the fine tuning. As such, only , AdaLoRA and are currently supported in this API.
To perform the adapter injection, simply use inject_adapter_in_model
method that takes 3 arguments, the PEFT config and the model itself and an optional adapter name. You can also attach multiple adapters in the model if you call multiple times inject_adapter_in_model
with different adapter names.
Below is a basic example usage of how to inject LoRA adapters into the submodule linear
of the module DummyModel
.
Copied
If you print the model, you will notice that the adapters have been correctly injected into the model
Copied
Note that it should be up to users to properly take care of saving the adapters (in case they want to save adapters only), as model.state_dict()
will return the full state dict of the model. In case you want to extract the adapters state dict you can use the get_peft_model_state_dict
method:
Copied
When to use this API and when to not use it? Letโs discuss in this section the pros and cons
Pros:
The model gets modified in-place, meaning the model will preserve all its original attributes and methods
Works for any torch module, and any modality (vision, text, multi-modal)
Cons:
You need to manually writing BOINC AI from_pretrained
and save_pretrained
utility methods if you want to easily save / load adapters from the BOINC AI Hub.
You cannot use any of the utility method provided by PeftModel
such as disabling adapters, merging adapters, etc.