llama2 lora fine tuning
1.0.0
Tune o modelo LLAMA-2-7B-CAT em dois P100s (16g).
A fonte de dados adota o formato da ALPACA e consiste em duas fontes de dados: trem e validação.
16g de memória de vídeo e acima (P100 ou T4 e acima), um ou mais blocos.
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.modelExistem vários parâmetros que podem ser ajustados:
| parâmetro | ilustrar | Obtenha o valor |
|---|---|---|
| load_in_bits | Precisão do modelo | 4 e 8. Se a memória de vídeo não estiver transbordada, tente escolher alta precisão 8. |
| block_size | Comprimento máximo de token | Primeira escolha 2048, Memory Overflow, 1024, 512, etc. |
| per_device_train_batch_size | Número de lotes por cartão carregado a cada vez durante o treinamento | Enquanto a memória não transbordar, tente ir para a eleição geral |
| per_device_eval_batch_size | Número de lotes por cartão carregado a cada vez durante a avaliação | Enquanto a memória não transbordar, tente ir para a eleição geral |
| incluir | Sequências de placas gráficas usadas | Por exemplo, duas peças: localhost: 1,2 (observe que a sequência não é necessariamente a mesma que o que NVIDIA-SMI vê) |
| NUM_TRAIN_EPOCHS | Número de rodadas de treinamento | Pelo menos 3 rodadas |
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