中文
通常、Facebook/Llamaを微調整するための2つのスキームがあります。 1つはスタンフォードのアルパカシリーズで、もう1つはSharegpt Corpusに基づいたVicunaです。 Vicunaはマルチラウンドダイアログコーパスを使用しており、トレーニング効果は、デフォルトでシングルラウンドダイアログになったAlpacaよりも優れています。したがって、Vicunaに基づいてLlamaを微調整することをお勧めします。 2つの微調整方法については、次のプロジェクトで詳しく説明しています(FastChatのLORAモードの説明は比較的簡単です)。
https://github.com/tloen/alpaca-lora
https://github.com/lm-sys/fastchat
Alpaca-Loraにはメモリ要件が低く、約12G 2080TIがサポートできますが、Vicunaのようなマルチラウンドセッションモデルのトレーニングには高いGPUメモリが必要です。 Vicunaモデルのトレーニングには、少なくとも24gのGPUメモリが必要です[公式推奨は4 * V100(32G)です]。ハイエンドのグラフィックカードがある場合は、ファイルに従ってトレーニングしてください。 16gのグラフィックカードしか持っていないが、Vicunaモデルを再現するためにコーパスをカスタマイズしたい場合は、32ビットから半精度16ビット、16ビットから8ビットから8ビットから8ビットから8ビットに継続的に削減するための多くの方法を考える必要があります。
•LORAメソッドを使用して、パラメーターの一部のみをトレーニングします
•基本モデルは、半精度のLlama-7B-HFを採用しています
•load_in_8bitを使用して、基本モデルを読み込みます
•微調整にはPEFTテクノロジーを使用します
•BitsandBytesを使用して加速します
次に、FastChatに基づいて、この記事はLORAトレーニングコードを変更し、ShareGPTコーパスを使用し、16Gカードで微調整を使用して、約13gのGPUメモリを占めています。
•オペレーティングシステム:CentosまたはUbuntu
•NVIDA P100またはT4:16G GPUメモリ以上
•Cuda、Conda
git clone https://github.com/git-cloner/llama-lora-fine-tuning
cd llama-lora-fine-tuningwget 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 installwget 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 ldconfigconda create -n llama-lora python=3.10
conda activate llama-lora
pip3 install -r requirements.txt元のモデルをダウンロードしてハーフ精度に変換するか、https://huggingface.co/decapoda-research/llama-7b-hfから変換された半精度モデルを直接ダウンロードできます。
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 7BCUDA_VISIBLE_DEVICES=1 python3 ./convert_llama_weights_to_hf.py --input_dir ./pyllama_data --model_size 7B --output_dir ./pyllama_data/output/7BDownload 52k ShareGPT: https: // huggingface.co/datasets/RyokoAI/ShareGPT52K
Other corpora refer to: https: // github.com/Zjh-819/LLMDataHub
Download sg_90k_part1.json and sg_90k_part2.json into the data directorypython3 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.jsonpython3 fastchat/data/clean_sharegpt.py --in ./data/sg_90k.json --out ./data/sharegpt_clean.jsonpython3 fastchat/data/optional_clean.py --in ./data/sharegpt_clean.json --out ./data/sharegpt_clean_1.json --skip-lang SOME_LANGUAGE_CODE
The values of SOME_LANGUAGE_CODE are as follows:
en - English
es - Spanish
fr - French
de - German
it - Italian
ja - Japanese
ko - Korean
zh - Chinese
ar - Arabic
ru - Russian
pt - Portuguese
nl - DutchCUDA_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 # Disable wandb
wandb disabled
# In order to prevent the SSH terminal from disconnecting and stopping the training, the training can run in the background (remove the # in three places to run in the background)
# If you have multiple GPUs,using --num_gpus parameter
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 &
# If running in the background, tail lora.log to check the training progress
tail -f lora.logP100(16G)の微調整は、13.5gのメモリを占めます。 1ラウンドのトレーニングの場合、120時間かかり、約5日かかりますが、これは非常に時間がかかります。結果のモデルの効果を検証する必要があります。 model_max_lengthは、トレーニング時間に影響します。 1024に設定すると、2048年と比較して時間が半分になりますが、推論効果に影響します。
シングル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訓練された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.jsonCUDA_VISIBLE_DEVICES=0 python generate.py --base_model ./model/llama-7b --lora_weights ./model/llama-peft