Quicktour
Last updated
Last updated
๐ PEFT contains parameter-efficient finetuning methods for training large pretrained models. The traditional paradigm is to finetune all of a modelโs parameters for each downstream task, but this is becoming exceedingly costly and impractical because of the enormous number of parameters in models today. Instead, it is more efficient to train a smaller number of prompt parameters or use a reparametrization method like low-rank adaptation (LoRA) to reduce the number of trainable parameters.
This quicktour will show you ๐PEFTโs main features and help you train large pretrained models that would typically be inaccessible on consumer devices. Youโll see how to train the 1.2B parameter model with LoRA to generate a classification label and use it for inference.
Each ๐ PEFT method is defined by a class that stores all the important parameters for building a .
Because youโre going to use LoRA, youโll need to load and create a class. Within LoraConfig
, specify the following parameters:
the task_type
, or sequence-to-sequence language modeling in this case
inference_mode
, whether youโre using the model for inference or not
r
, the dimension of the low-rank matrices
lora_alpha
, the scaling factor for the low-rank matrices
lora_dropout
, the dropout probability of the LoRA layers
Copied
๐ก See the reference for more details about other parameters you can adjust.
Start by loading the base model you want to finetune.
Copied
Copied
Copied
Copied
If you have saved your adapter locally or on the Hub, you can leverage the AutoPeftModelForxxx
classes and load any PEFT model with a single line of code:
Copied
Currently, supported auto classes are: AutoPeftModelForCausalLM
, AutoPeftModelForSequenceClassification
, AutoPeftModelForSeq2SeqLM
, AutoPeftModelForTokenClassification
, AutoPeftModelForQuestionAnswering
and AutoPeftModelForFeatureExtraction
. For other tasks (e.g. Whisper, StableDiffusion), you can load the model with:
Copied
Feel free to also take a look at the task guides if youโre interested in training a model with a ๐PEFT method for a specific task such as semantic segmentation, multilingual automatic speech recognition, DreamBooth, and token classification.
A is created by the get_peft_model()
function. It takes a base model - which you can load from the ๐ Transformers library - and the containing the instructions for how to configure a model for a specific ๐ PEFT method.
Wrap your base model and peft_config
with the get_peft_model
function to create a . To get a sense of the number of trainable parameters in your model, use the print_trainable_parameters
method. In this case, youโre only training 0.19% of the modelโs parameters! ๐ค
That is it ๐! Now you can train the model using the ๐ Transformers , ๐ Accelerate, or any custom PyTorch training loop.
After your model is finished training, you can save your model to a directory using the function. You can also save your model to the Hub (make sure you log in to your BOINC AI account first) with the function.
This only saves the incremental ๐ PEFT weights that were trained, meaning it is super efficient to store, transfer, and load. For example, this model trained with LoRA on the subset of the RAFT only contains two files: adapter_config.json
and adapter_model.bin
. The latter file is just 19MB!
Easily load your model for inference using the function:
Now that youโve seen how to train a model with one of the ๐ PEFT methods, we encourage you to try out some of the other methods like prompt tuning. The steps are very similar to the ones shown in this quickstart; prepare a for a ๐ PEFT method, and use the get_peft_model
to create a from the configuration and base model. Then you can train it however you like!