LLAMA2モデルのライセンスが変更され、市販されています。モデルが起動されたとき、llama2-chatも起動されました。 16G推論カード(https://zhuanlan.zhihu.com/p/645152512でllama-2-7b-chatの微調整を練習しました。ただし、中国の語彙リストが拡張されていても、推論効果はまだ良くなく、答えは主に英語です。
LLAMA2モデルがリリースされたとき、フルスケール、LORA、その他の方法の微調整をサポートするLlama Companion(https://github.com/facebookresearch/llama-recipes)と呼ばれる公式の微調整プログラムが開かれ、サードパーティのプログラムよりも比較的互換性があります。
この記事は、Llama-Recipesに基づいており、Adaptive Graphics Card Resourcesを変更し、LORAに基づいた元のLlama2-7Bモデルを微調整しています。結果は合理的な推論です。このプロジェクトは、テストプロセスとストリーミングインターフェイスも提供します。
16g以上、2つ以上のピースを用意するのが最善です。
2つのP100(16G)で100 m以上のコーパスを微調整するのに120時間かかります。したがって、V100、4090、およびその他の推論カードを使用して微調整することをお勧めします。
git clone https://github.com/git-cloner/Llama2-chinese
cd Llama2-chineseconda create -n llama-recipes python=3.9 -y
conda activate llama-recipes
# 因为requirements中有从github中安装的依赖,网络环境不佳,打开这两个参数可以观察进度
export GIT_TRACE=1
export GIT_CURL_VERBOSE=1
pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple --trusted-host=pypi.mirrors.ustc.edu.cn
# 问题比较多的是bitsandbytes,pip install后用以下命令验证
python -m bitsandbytes # 用本项目开发的下载器下载模型,可以断点续传和重连
python model_download.py --repo_id NousResearch/Llama-2-7b-hf
# 下载后的模型在 ./modelsNousResearchLlama-2-7b-hf 下コーパスはアルパカ形式です(Huggingface.coのAlpaca Corpusは非常に大きく、自分で整理できます)。パーソナライズの後、FT_DATASETS/ALPACA_DATA.JSONという名前です
# kill process force
pkill -9 -f llama_finetuning
# train,batch_size_training可按显存大小反复试,尽量把显存占满
# 本例是用两块P100,分别是第1、2块
# !注意如果用两块卡,nproc_per_node是1,不是2
CUDA_VISIBLE_DEVICES=1,2 nohup torchrun --nnodes 1 --nproc_per_node 1
llama_finetuning.py
--use_peft
--peft_method lora
--model_name ./models/NousResearch/Llama-2-7b-hf
--use_fp16
--output_dir output/model
--dataset alpaca_dataset
--batch_size_training 40
--num_epochs 3
--quantization > train.log 2>&1 &
# check log
tail -f train.logラウンドの微調整の後、PEFT増分モデルが生成されます。出力/モデルを下回ると、次のコマンドを使用して、クライアントでインタラクティブにテストします。ストリームモードは使用されないため、結果は一度に生成した後にのみ確認できますので、速度は遅くなります。
CUDA_VISIBLE_DEVICES=0 python generate.py
--base_model ' ./models/NousResearch/Llama-2-7b-hf '
--lora_weights ' ./output/model '
--load_8bit # 可以用4bit或8bit量化方式或半精度装入模型测试
# --load_4bit 需要约6G显存
# --load_8bit 需要9G显存
# 半精度 需要13G显存
CUDA_VISIBLE_DEVICES=0 nohup python -u api_stream.py
--load_4bit > api_stream.log 2>&1 &
tail -f api_stream.log # 多次发POST请求,直到返回的response中包含[stop]后停止调用
curl -X POST " http://127.0.0.1:8000/stream "
-H ' Content-Type: application/json '
-d ' {"prompt": "你好", "history": []} ' python inference/hf-text-generation-inference/merge_lora_weights.py
--base_model ./models/NousResearch/Llama-2-7b-hf
--peft_model output/model
--output_dir output/merged_model_output