中文
通常有兩個用於微調Facebook/Llama的方案。一個是斯坦福大學的羊駝系列,另一個是基於ShareGpt語料庫的Vicuna。 Vicuna使用多輪對話語料庫,訓練效果比羊駝毛更好,羊駝毛默認為單輪對話。因此,建議根據Vicuna微調美洲駝。在以下項目中詳細描述了兩種微調方式(FastChat中LORA模式的描述相對簡單)。
https://github.com/tloen/alpaca-lora
https://github.com/lm-sys/fastchat
羊駝羊駝的內存需求較低,大約12G 2080TI可以支持,但是訓練諸如Vicuna之類的多輪會話模型需要高GPU內存。 Vicuna模型培訓至少需要24克GPU內存[官方建議為4 * V100(32G)]。如果您有高端圖形卡,只需按照文件進行訓練即可。如果您只有16G圖形卡,但想自定義語料庫以復制Vicuna模型,則必須考慮多種方法,將精度從32位降低到一半的精度16位,然後從16位從16位到8位,並加速訓練方法才能實現目標。
•使用洛拉方法僅訓練一部分參數
•基本模型採用半精確的Llama-7b-HF
•使用load_in_8bit加載基本模型
•使用PEFT技術進行微調
•使用bitsandbytes加速
然後,我們基於FastChat,本文修改了LORA培訓代碼,使用ShareGPT語料庫和16G卡上的微型,佔據了約13克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.5克內存。在一輪訓練的情況下,大約需要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