Home

Awesome

<!--- Copyright 2023 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <h1 align="center"> <img src="picture.jpg" width="73" height="114"> <p> LLM-Adapters</p> </h1> <h3 align="center"> <p>LLM-Adapters: An Adapter Family for Parameter-Efficient Fine-Tuning of Large Language Models </p> </h3> LLM-Adapters is an easy-to-use framework that integrates various adapters into LLMs and can execute adapter-based PEFT methods of LLMs for different tasks. LLM-Adapter is an extension of HuggingFace's PEFT library, many thanks for their amazing work! Please find our paper at this link: https://arxiv.org/abs/2304.01933.

The framework includes state-of-the-art open-access LLMs: LLaMa, OPT, BLOOM, and GPT-J, as well as widely used adapters such as Bottleneck adapters, Parallel adapters, and LoRA.

Supported Adapters:

  1. LoRA: LORA: LOW-RANK ADAPTATION OF LARGE LANGUAGE MODELS
  2. AdapterH: Parameter-Efficient Transfer Learning for NLP
  3. AdapterP: GMAD-X: An Adapter-Based Framework for Multi-Task Cross-Lingual Transfer
  4. Parallel: TOWARDS A UNIFIED VIEW OF PARAMETER-EFFICIENT TRANSFER LEARNING
  5. Prefix Tuning: Prefix-Tuning: Optimizing Continuous Prompts for Generation, P-Tuning v2: Prompt Tuning Can Be Comparable to Fine-tuning Universally Across Scales and Tasks
  6. P-Tuning: GPT Understands, Too
  7. Prompt Tuning: The Power of Scale for Parameter-Efficient Prompt Tuning

Latest News 🔥🔥

Special Announcement

The math_10k.json data is collected with the training sets of GSM8K, MAWPS, and AQuA(1000 examples). However, MAWPS consists of AddSub, MultiArith, SingleOp, SingleEq, SimulEq-S, SimulEq-L. Thus, we can't utilize MultiArith, AddSub, and SingleEq as evaluation benchmarks with models trained with math_10k.json. We evaluate the PEFT methods on the MAWPS test set instead, and the result table has been updated (The findings in the paper are consistent). Furthermore, two variations of math_10k.json have been uploaded, math_7K.json where the MAWPS samples have been deleted, and math_14k.json where the MAWPS samples have been deleted as well and we combine ChatGPT and GPT-4 rationales. Sincerely apologize for any inconvenience!

Setup

  1. Install dependencies
pip install -r requirements.txt
  1. Set environment variables, or modify the files referencing BASE_MODEL:
# Files referencing `BASE_MODEL`
# export_hf_checkpoint.py
# export_state_dict_checkpoint.py

export BASE_MODEL=yahma/llama-7b-hf

Both finetune.py and generate.py use --base_model flag as shown further below.

  1. If bitsandbytes doesn't work, install it from source. Windows users can follow these instructions.

Training(finetune.py)

This file contains some code related to prompt construction and tokenization.In this file, specify different adapters and different sets of data, so that different models can be trained.

Example usage for multiple GPUs:

WORLD_SIZE=2 CUDA_VISIBLE_DEVICES=0,1 torchrun --nproc_per_node=2 --master_port=3192 finetune.py \
  --base_model 'yahma/llama-7b-hf' \
  --data_path 'math_10k.json' \
  --output_dir './trained_models/llama-lora' \
  --batch_size 16 \
  --micro_batch_size 4 \
  --num_epochs 3 \
  --learning_rate 3e-4 \
  --cutoff_len 256 \
  --val_set_size 120 \
  --adapter_name lora

The math_10k.json data is collected with the training sets of GSM8K, MAWPS, and AQuA(1000 examples). yahma/llama-7b-hf is a base model, LLaMa-7B. Add lora adapter to this model.

Example usage for Single GPUs:

CUDA_VISIBLE_DEVICES=0 python finetune.py \
  --base_model 'yahma/llama-7b-hf' \
  --data_path 'math_10k.json' \
  --output_dir './trained_models/llama-lora' \
  --batch_size 16 \
  --micro_batch_size 4 \
  --num_epochs 3 \
  --learning_rate 3e-4 \
  --cutoff_len 256 \
  --val_set_size 120 \
  --adapter_name lora

Moreover, you can use --use_gradient_checkpointing to save more GPU memory, but it will increase the training time.

To use the AdapterH, just add the following arguments:

--adapter_name bottleneck # use the bottleneck adapter, refers to AdapterH in the result table

To use the AdapterP, just add the following arguments:

--adapter_name bottleneck 
--use_adapterp  # use the AdapterP, refers to AdapterP in the result table

To use parallel adapter, just add the following arguments:

--adapter_name bottleneck
--use_parallel_adapter

Note that, In order to facilitate INT8 training of large models with parallel adapters, we have adopted a technique whereby the parallel adapter layers are incorporated into multi-head attention layers and MLP layers, in parallel with Linear layers. It is different from Hu et al. (2021).

Inference (generate.py)

This file reads the foundation model from the Hugging Face model hub and the LoRA weights from './trained_models/llama-lora' , and runs a Gradio interface for inference on a specified input. Users should treat this as example code for the use of the model, and modify it as needed. Example usage:

CUDA_VISIBLE_DEVICES=0 torchrun generate.py \
    --base_model 'yahma/llama-7b-hf' \
    --lora_weights './trained_models/llama-lora'

Evaluation (evaluate.py)

To evaluate the performance of the finetuned model on the Arithmetic Reasoning tasks, you can use the following command:

CUDA_VISIBLE_DEVICES=0 python evaluate.py 
    --model LLaMA-7B \ #specify the base model
    --adapter LoRA \   #specify the adapter name ["LoRA", "AdapterH", "AdapterP", "Parallel", "Scaled_Parallel""]
    --dataset SVAMP \  #specify the test dataset
    --base_model 'yahma/llama-7b-hf' \
    --lora_weights './trained_models/llama-lora'
<!-- ## Resource Consumption There is a table of resouce needed for different adapters, which contains Trainable Parameters, GPU RAM Usage, and Fine-tuning Time on the Arithmetic Reasoning dataset `math_10k.json` Hyper-parameter setting: num_epochs=3, lora_r=8, lora_alpha=16, bottleneck_size=256 Models: LLaMA-13B, LLaMA-7B, BLOOM-6.7B, GPT-j-6B Dataset: 3.2K math word problems Hardware: 2*3090 GPUs | Model | Trainable Parameters | GPU RAM Usage | Fine-tuning Time | |-----------------------|----------------------|---------------|------------------| | LLaMA-7B-LoRA | 4.2M | 18GB | 4h | | LLaMA-7B-AdapterH | 200M | 22GB | 4h | | LLaMA-7B-AdapterP | 200M | 22GB | 4h | | LLaMA-7B-Parallel | 200M | 22GB | 4h | -->

Finetune Result

There are the finetune results in different models with 4 math reasoning datasets, which contains GSM8K, AQuA, SVAMP, and MAWPS. In this table, we use the optimal configuration and placement of Prefix-Tuning, Series Adapter, LoRA, and Parallel Adapter according to the empirical study in our paper.

ModelGSM8KAQuAMAWPSSVAMPAverage
GPT-3.556.438.987.469.963.2
BLOOMz-7B-Prefix13.812.547.524.124.5
BLOOMz-7B-Series14.320.562.238.133.8
BLOOMz-7B-Parallel18.518.970.636.436.1
BLOOMz-7B-LoRA17.421.370.241.037.5
GPT-j-6B-Prefix16.014.759.231.030.2
GPT-j-6B-Series19.515.080.343.639.6
GPT-j-6B-Parallel18.917.978.241.139.0
GPT-j-6B-LoRA23.016.179.446.041.1
LLaMA-7B-Prefix24.414.263.438.135.0
LLaMA-7B-Series33.315.077.752.344.6
LLaMA-7B-Parallel35.318.182.449.646.4
LLaMA-7B-LoRA37.518.979.052.146.9
LLaMA-13B-Prefix31.115.766.841.438.8
LLaMA-13B-Series44.022.078.650.848.9
LLaMA-13B-Parallel43.320.581.155.750.2
LLaMA-13B-LoRA47.518.583.654.651.1

There are the finetune results in different models with eight commonsense reasoning datasets.

ModelBoolQPIQASIQAHellaSwagWinoGrandeARC-eARC-cOBQAAverage
ChatGPT73.185.468.578.566.189.879.974.877.0
BLOOMz-7B-Prefix45.653.746.326.749.552.139.744.344.7
BLOOMz-7B-Series65.470.473.653.469.372.355.968.066.0
BLOOMz-7B-Parallel64.171.572.152.967.070.554.769.665.3
BLOOMz-7B-LoRA65.975.374.557.372.574.657.873.468.9
GPT-j-6B-Prefix63.166.968.734.464.564.446.859.058.5
GPT-j-6B-Series62.163.572.330.668.063.948.163.859.0
GPT-j-6B-Parallel62.269.770.041.765.060.244.658.259.0
GPT-j-6B-LoRA62.468.649.543.157.343.431.046.650.2
LLaMA-7B-Prefix64.376.873.942.172.172.954.060.664.6
LLaMA-7B-Series63.079.276.367.975.774.557.172.470.8
LLaMA-7B-Parallel67.976.478.869.878.973.757.375.272.3
LLaMA-7B-LoRA68.980.777.478.178.877.861.374.874.7
LLaMA-13B-Prefix65.375.472.155.268.679.562.968.068.4
LLaMA-13B-Series71.883.079.288.182.482.567.381.879.5
LLaMA-13B-Parallel72.584.879.892.184.784.271.282.481.5
LLaMA-13B-LoRA72.183.580.590.583.782.868.382.480.5

Adapter support matrix

This metrix shows whether different models can use LoRA,AdapterH,AdapterP,Parallel and Scaled Parallel adapters.

AdapterLoRAAdapterHAdapterPParallelPrefix TuningP-TuningPrompt Tuning
LLaMA✅✅✅✅✅✅✅
BLOOM✅✅✅✅✅✅✅
GPT-J✅✅✅✅✅✅✅
OPT✅✅✅✅✅✅✅
GPT-2✅🔧Developing🔧Developing🔧Developing✅✅✅
GPT-Neo✅✅✅✅✅✅✅
GPT-NeoX-20B✅🔧Developing🔧Developing🔧Developing✅✅✅
ChatGLM✅✅✅✅✅✅✅

TODO List

:star: Star History

Star History Chart

Citing <img src="picture.jpg" width="14px" height="14px"> LLM-Adapter

If you use <img src="picture.jpg" width="14px" height="14px"> LLM-Adapters in your publication, please cite it by using the following BibTeX entry.

@article{hu2023llm,
  title={LLM-Adapters: An Adapter Family for Parameter-Efficient Fine-Tuning of Large Language Models},
  author={Hu, Zhiqiang and Lan, Yihuai and Wang, Lei and Xu, Wanyu and Lim, Ee-Peng and Lee, Roy Ka-Wei and Bing, Lidong and Poria, Soujanya},
  journal={arXiv preprint arXiv:2304.01933},
  year={2023}
}

Acknowledgement

This repo benefits from PEFT, Adapter-Transformer, Alpaca-lora. Thanks for their wonderful works. Additionally, we thank DONG Shan and dream.ai for the exceptional logo design, which has added immense value to our project.