
llama-lora-fine-tuning
单GPU微调LLaMA模型的高效方法
本项 目展示了在单个16G GPU上微调vicuna-7b模型的方法。通过采用LoRA、半精度模型和8位加载等技术,有效降低了内存需求。项目详细说明了环境配置、模型准备、语料处理和微调过程,并提供P100和A100的性能数据。这种方法使研究者和开发者能在有限硬件资源下进行大型语言模型的定制化训练。
在单个16G GPU上微调vicuna-7b
1. 概述
微调FaceBook/LLaMA通常有两种方案。一种是斯坦福大学的alpaca系列,另一种是基于shareGPT语料库的Vicuna。Vicuna使用多轮对话语料,训练效果优于默认单轮对话的alpaca。因此,建议基于Vicuna来微调Llama。 以下项目详细描述了这两种微调方式(FastChat中对lora模式的描述相对简单)。<br> https://github.com/tloen/alpaca-lora <br> https://github.com/lm-sys/FastChat <br> Alpaca-lora的内存要求较低,约12G的2080Ti就可以支持,但训练像Vicuna这样的多轮会话模型需要较高的GPU内存。Vicuna模型训练至少需要24G GPU内存[官方建议是4 * V100 (32G)]。 如果你有高端显卡,只需按文件说明进行训练即可。如果你只有16G显卡但想自定义语料库重现Vicuna模型,就必须想办法不断降低精度,从32位到半精度16位,再从16位到8位,并加速训练方法以实现目标。
2. 微调方法
• 使用lora方法仅训练部分参数 <br> • 基础模型采用半精度llama-7b-hf <br> • 使用load_in_8bit加载基础模型 <br> • 使用peft技术进行微调 <br> • 使用bitsandbytes加速 <br> 然后我们基于FastChat,本文修改了lora训练代码,使用shareGPT语料库,并在16G显卡上进行微调,占用约13G的GPU内存。<br> • 操作系统:centos或ubuntu <br> • NVIDA P100或T4:16G GPU内存或以上 <br> • CUDA,conda
3. 微调过程
3.1 安装依赖环境
3.1.1 下载源代码
git clone https://github.com/git-cloner/llama-lora-fine-tuning cd llama-lora-fine-tuning
3.1.2 安装微调依赖环境
3.1.2.1 安装pkg-config
wget https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz tar -zxvf pkg-config-0.29.2.tar.gz cd pkg-config-0.29.2 ./configure --with-internal-glib make -j4 make check sudo make install
3.1.2.2 安装libicu
wget https://mirrors.aliyun.com/blfs/conglomeration/icu/icu4c-73_1-src.tgz tar xf icu4c-73_1-src.tgz cd icu/source ./configure make make check sudo make install sudo ldconfig
3.1.2.3 安装软件包
conda create -n llama-lora python=3.10 conda activate llama-lora pip3 install -r requirements.txt
3.2 准备Llama模型
你可以下载原始模型并将其转换为半精度,或直接从https://huggingface.co/decapoda-research/llama-7b-hf下载已转换的半精度模型。
3.2.1 下载Llama模型
export GIT_TRACE=1 export GIT_CURL_VERBOSE=1 pip3 install git+https://github.com/juncongmoo/pyllama -i https://pypi.mirrors.ustc.edu.cn/simple --trusted-host=pypi.mirrors.ustc.edu.cn python -m llama.download --model_size 7B
3.2.2 将模型转换为huggingface格式
CUDA_VISIBLE_DEVICES=1 python3 ./convert_llama_weights_to_hf.py --input_dir ./pyllama_data --model_size 7B --output_dir ./pyllama_data/output/7B
3.3 组织语料库
3.3.1 语料库下载
下载52k ShareGPT:https://huggingface.co/datasets/RyokoAI/ShareGPT52K 其他语料库参考:https://github.com/Zjh-819/LLMDataHub 将sg_90k_part1.json和sg_90k_part2.json下载到data目录
3.3.2 合并语料库文件
python3 fastchat/data/merge.py --in ./data/sg_90k_part1.json ./data/sg_90k_part2.json ./data/dummy_cn.json ./data/dummy_en.json --out ./data/sg_90k.json
3.3.3 Html转Markdown
python3 fastchat/data/clean_sharegpt.py --in ./data/sg_90k.json --out ./data/sharegpt_clean.json
3.3.4 移除一些未使用的语言(可选)
python3 fastchat/data/optional_clean.py --in ./data/sharegpt_clean.json --out ./data/sharegpt_clean_1.json --skip-lang SOME_LANGUAGE_CODE SOME_LANGUAGE_CODE的值如下: en - 英语 es - 西班牙语 fr - 法语 de - 德语 it - 意大利语 ja - 日语 ko - 韩语 zh - 中文 ar - 阿拉伯语 ru - 俄语 pt - 葡萄牙语 nl - 荷兰语
3.3.5 将长对话拆分为短对话
CUDA_VISIBLE_DEVICES=1 python3 fastchat/data/split_long_conversation.py --in ./data/sharegpt_clean.json --out ./data/sharegpt_clean_split.json --model-name ./pyllama_data/output/7B
3.4 微调
3.4.1 微调命令
# 禁用wandb wandb disabled # 为了防止SSH终端断开导致训练停止,可以让训练在后台运行(删除三处#即可在后台运行) # 如果有多个GPU,使用--num_gpus参数 CUDA_VISIBLE_DEVICES=0,1 \ #nohup \ deepspeed --num_gpus=2 fastchat/train/train_lora.py \ --deepspeed ./deepspeed-config.json \ --lora_r 8 \ --lora_alpha 16 \ --lora_dropout 0.05 \ --model_name_or_path ./pyllama_data/output/7B \ --data_path ./data/sharegpt_clean_split.json \ --fp16 True \ --output_dir ./output \ --num_train_epochs 1 \ --per_device_train_batch_size 14 \ --per_device_eval_batch_size 14 \ --gradient_accumulation_steps 1 \ --evaluation_strategy "no" \ --save_strategy "steps" \ --save_steps 2400 \ --save_total_limit 5 \ --learning_rate 2e-5 \ --weight_decay 0. \ --warmup_ratio 0.03 \ --lr_scheduler_type "cosine" \ --logging_steps 1 \ --model_max_length 512 \ --gradient_checkpointing True #>> lora.log 2>&1 & # 如果在后台运行,可以通过tail lora.log查看训练进度 tail -f lora.log
3.4.2 微调性能
在P100(16G)上进行微调占用13.5G内存。在一轮训练的情况下,需要120小时,约5天,仍然非常耗时。生成模型的效果需要验证。 model_max_length会影响训练时间。如果设置为1024,时间将比2048减半,但会影响推理效果。
3.4.3 在A100上微调
在单个A100上微调大约需要16小时。
deepspeed fastchat/train/train_lora.py \ --deepspeed ./deepspeed-config.json \ --lora_r 8 \ --lora_alpha 16 \ --lora_dropout 0.05 \ --model_name_or_path ./pyllama_data/output/7B \ --data_path ./data/sharegpt_clean_split.json \ --fp16 True \ --output_dir ./output \ --num_train_epochs 1 \ --per_device_train_batch_size 56 \ --per_device_eval_batch_size 56 \ --gradient_accumulation_steps 1\ --evaluation_strategy "no" \ --save_strategy "steps" \ --save_steps 1200 \ --save_total_limit 5 \ --learning_rate 2e-5 \ --weight_decay 0. \ --warmup_ratio 0.03 \ --lr_scheduler_type "cosine" \ --logging_steps 1 \ --model_max_length 1024 \ --gradient_checkpointing True
4、测试训练好的模型
4.1 模型文件结构
训练好的LoRa peft模型由adapter_config.json、adapter_model.bin和trainer_state.json组成。以下是peft和原始llama模型的文件结构。
model ───llama-peft │ adapter_config.json │ adapter_model.bin │ trainer_state.json │ └──llama_7b config.json generation_config.json pytorch_model-00001-of-00002.bin pytorch_model-00002-of-00002.bin pytorch_model.bin.index.json special_tokens_map.json tokenizer.json tokenizer.model tokenizer_config.json
4.2 测试生成
CUDA_VISIBLE_DEVICES=0 python generate.py --base_model ./model/llama-7b --lora_weights ./model/llama-peft