中文
通常有两个用于微调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