llama2 lora fine tuning
1.0.0
在兩塊P100(16G)上微調Llama-2-7b-chat模型。
數據源採用了alpaca格式,由train和validation兩個數據源組成。
16G顯存及以上(P100或T4及以上),一塊或多塊。
git clone https://github.com/git-cloner/llama2-lora-fine-tuning
cd llama2-lora-fine-tuning # 创建虚拟环境
conda create -n llama2 python=3.9 -y
conda activate llama2
# 下载github.com上的依赖资源(需要反复试才能成功,所以单独安装)
export GIT_TRACE=1
export GIT_CURL_VERBOSE=1
pip install git+https://github.com/PanQiWei/AutoGPTQ.git -i https://pypi.mirrors.ustc.edu.cn/simple --trusted-host=pypi.mirrors.ustc.edu.cn
pip install git+https://github.com/huggingface/peft -i https://pypi.mirrors.ustc.edu.cn/simple
pip install git+https://github.com/huggingface/transformers -i https://pypi.mirrors.ustc.edu.cn/simple
# 安装其他依赖包
pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple
# 验证bitsandbytes
python -m bitsandbytespython model_download.py --repo_id daryl149/llama-2-7b-chat-hf # 使用了https://github.com/ymcui/Chinese-LLaMA-Alpaca.git的方法扩充中文词表
# 扩充完的词表在merged_tokenizes_sp(全精度)和merged_tokenizer_hf(半精度)
# 在微调时,将使用--tokenizer_name ./merged_tokenizer_hf参数
python merge_tokenizers.py
--llama_tokenizer_dir ./models/daryl149/llama-2-7b-chat-hf
--chinese_sp_model_file ./chinese_sp.model有以下幾個參數可以調整:
| 參數 | 說明 | 取值 |
|---|---|---|
| load_in_bits | 模型精度 | 4和8,如果顯存不溢出,盡量選高精度8 |
| block_size | token最大長度 | 首選2048,內存溢出,可選1024、512等 |
| per_device_train_batch_size | 訓練時每塊卡每次裝入批量數 | 只要內存不溢出,盡量往大選 |
| per_device_eval_batch_size | 評估時每塊卡每次裝入批量數 | 只要內存不溢出,盡量往大選 |
| include | 使用的顯卡序列 | 如兩塊:localhost:1,2(特別注意的是,序列與nvidia-smi看到的不一定一樣) |
| num_train_epochs | 訓練輪數 | 至少3輪 |
chmod +x finetune-lora.sh
# 微调
./finetune-lora.sh
# 微调(后台运行)
pkill -9 -f finetune-lora
nohup ./finetune-lora.sh > train.log 2>&1 &
tail -f train.logCUDA_VISIBLE_DEVICES=0 python generate.py
--base_model ' ./models/daryl149/llama-2-7b-chat-hf '
--lora_weights ' output/checkpoint-2000 '
--load_8bit #不加这个参数是用的4bit