Package: peft
🤗 PEFTState-of-the-art Parameter-Efficient Fine-Tuning (PEFT) methodsFine-tuning large pretrained models is often prohibitively costly due to their scale. Parameter-Efficient Fine-Tuning (PEFT) methods enable efficient adaptation of large pretrained models to various downstream applications by only fine-tuning a small number of (extra) model parameters instead of all the model's parameters. This significantly decreases the computational and storage costs. Recent state-of-the-art PEFT techniques achieve performance comparable to fully fine-tuned models.PEFT is integrated with Transformers for easy model training and inference, Diffusers for conveniently managing different adapters, and Accelerate for distributed training and inference for really big models.[!TIP]
Visit thePEFTorganization to read about the PEFT methods implemented in the library and to see notebooks demonstrating how to apply these methods to a variety of downstream tasks. Click the "Watch repos" button on the organization page to be notified of newly implemented methods and notebooks!Check the PEFT Adapters API Reference section for a list of supported PEFT methods, and read theAdapters,Soft prompts, andIA3conceptual guides to learn more about how these methods work.QuickstartInstall PEFT from pip:pipinstallpeftPrepare a model for training with a PEFT method such as LoRA by wrapping the base model and PEFT configuration withget_peft_model. For the bigscience/mt0-large model, you're only training 0.19% of the parameters!fromtransformersimportAutoModelForSeq2SeqLMfrompeftimportget_peft_config,get_peft_model,LoraConfig,TaskTypemodel_name_or_path="bigscience/mt0-large"tokenizer_name_or_path="bigscience/mt0-large"peft_config=LoraConfig(task_type=TaskType.SEQ_2_SEQ_LM,inference_mode=False,r=8,lora_alpha=32,lora_dropout=0.1)model=AutoModelForSeq2SeqLM.from_pretrained(model_name_or_path)model=get_peft_model(model,peft_config)model.print_trainable_parameters()"trainable params: 2359296 || all params: 1231940608 || trainable%: 0.19151053100118282"To load a PEFT model for inference:frompeftimportAutoPeftModelForCausalLMfromtransformersimportAutoTokenizerimporttorchmodel=AutoPeftModelForCausalLM.from_pretrained("ybelkada/opt-350m-lora").to("cuda")tokenizer=AutoTokenizer.from_pretrained("facebook/opt-350m")model.eval()inputs=tokenizer("Preheat the oven to 350 degrees and place the cookie dough",return_tensors="pt")outputs=model.generate(input_ids=inputs["input_ids"].to("cuda"),max_new_tokens=50)print(tokenizer.batch_decode(outputs,skip_special_tokens=True)[0])"Preheat the oven to 350 degrees and place the cookie dough in the center of the oven. In a large bowl, combine the flour, baking powder, baking soda, salt, and cinnamon. In a separate bowl, combine the egg yolks, sugar, and vanilla."Why you should use PEFTThere are many benefits of using PEFT but the main one is the huge savings in compute and storage, making PEFT applicable to many different use cases.High performance on consumer hardwareConsider the memory requirements for training the following models on theought/raft/twitter_complaintsdataset with an A100 80GB GPU with more than 64GB of CPU RAM.ModelFull FinetuningPEFT-LoRA PyTorchPEFT-LoRA DeepSpeed with CPU Offloadingbigscience/T0_3B (3B params)47.14GB GPU / 2.96GB CPU14.4GB GPU / 2.96GB CPU9.8GB GPU / 17.8GB CPUbigscience/mt0-xxl (12B params)OOM GPU56GB GPU / 3GB CPU22GB GPU / 52GB CPUbigscience/bloomz-7b1 (7B params)OOM GPU32GB GPU / 3.8GB CPU18.1GB GPU / 35GB CPUWith LoRA you can fully finetune a 12B parameter model that would've otherwise run out of memory on the 80GB GPU, and comfortably fit and train a 3B parameter model. When you look at the 3B parameter model's performance, it is comparable to a fully finetuned model at a fraction of the GPU memory.Submission NameAccuracyHuman baseline (crowdsourced)0.897Flan-T50.892lora-t0-3b0.863[!TIP]
The bigscience/T0_3B model performance isn't optimized in the table above. You can squeeze even more performance out of it by playing around with the input instruction templates, LoRA hyperparameters, and other training related hyperparameters. The final checkpoint size of this model is just 19MB compared to 11GB of the full bigscience/T0_3B model. Learn more about the advantages of finetuning with PEFT in thisblog post.QuantizationQuantization is another method for reducing the memory requirements of a model by representing the data in a lower precision. It can be combined with PEFT methods to make it even easier to train and load LLMs for inference.Learn how to finetunemeta-llama/Llama-2-7b-hfwith QLoRA and theTRLlibrary on a 16GB GPU in theFinetune LLMs on your own consumer hardware using tools from PyTorch and Hugging Face ecosystemblog post.Learn how to finetune aopenai/whisper-large-v2model for multilingual automatic speech recognition with LoRA and 8-bit quantization in thisnotebook(see thisnotebookinstead for an example of streaming a dataset).Save compute and storagePEFT can help you save storage by avoiding full finetuning of models on each of downstream task or dataset. In many cases, you're only finetuning a very small fraction of a model's parameters and each checkpoint is only a few MBs in size (instead of GBs). These smaller PEFT adapters demonstrate performance comparable to a fully finetuned model. If you have many datasets, you can save a lot of storage with a PEFT model and not have to worry about catastrophic forgetting or overfitting the backbone or base model.PEFT integrationsPEFT is widely supported across the Hugging Face ecosystem because of the massive efficiency it brings to training and inference.DiffusersThe iterative diffusion process consumes a lot of memory which can make it difficult to train. PEFT can help reduce the memory requirements and reduce the storage size of the final model checkpoint. For example, consider the memory required for training a Stable Diffusion model with LoRA on an A100 80GB GPU with more than 64GB of CPU RAM. The final model checkpoint size is only 8.8MB!ModelFull FinetuningPEFT-LoRAPEFT-LoRA with Gradient CheckpointingCompVis/stable-diffusion-v1-427.5GB GPU / 3.97GB CPU15.5GB GPU / 3.84GB CPU8.12GB GPU / 3.77GB CPU[!TIP]
Take a look at theexamples/lora_dreambooth/train_dreambooth.pytraining script to try training your own Stable Diffusion model with LoRA, and play around with thesmangrul/peft-lora-sd-dreamboothSpace which is running on a T4 instance. Learn more about the PEFT integration in Diffusers in thistutorial.AccelerateAccelerateis a library for distributed training and inference on various training setups and hardware (GPUs, TPUs, Apple Silicon, etc.). PEFT models work with Accelerate out of the box, making it really convenient to train really large models or use them for inference on consumer hardware with limited resources.TRLPEFT can also be applied to training LLMs with RLHF components such as the ranker and policy. Get started by reading:Fine-tune a Mistral-7b model with Direct Preference Optimizationwith PEFT and theTRLlibrary to learn more about the Direct Preference Optimization (DPO) method and how to apply it to a LLM.Fine-tuning 20B LLMs with RLHF on a 24GB consumer GPUwith PEFT and theTRLlibrary, and then try out thegpt2-sentiment_peft.ipynbnotebook to optimize GPT2 to generate positive movie reviews.StackLLaMA: A hands-on guide to train LLaMA with RLHFwith PEFT, and then try out thestack_llama/scriptsfor supervised finetuning, reward modeling, and RL finetuning.Model supportUse thisSpaceor check out thedocsto find which models officially support a PEFT method out of the box. Even if you don't see a model listed below, you can manually configure the model config to enable PEFT for a model. Read theNew transformers architectureguide to learn how.ContributeIf you would like to contribute to PEFT, please check out ourcontribution guide.Citing 🤗 PEFTTo use 🤗 PEFT in your publication, please cite it by using the following BibTeX entry.@Misc{peft,title={PEFT: State-of-the-art Parameter-Efficient Fine-Tuning methods},author={Sourab Mangrulkar and Sylvain Gugger and Lysandre Debut and Younes Belkada and Sayak Paul and Benjamin Bossan},howpublished={\url{https://github.com/huggingface/peft}},year={2022}}