M40顯卡通過lora微調(diào)國產(chǎn)開源模型RWKV
第一部分:準(zhǔn)備階段
1.系統(tǒng):Ubuntu22或20或其他Linux系統(tǒng)(win系統(tǒng)有各種各樣問題,目前沒看到誰成功過)
2.顯卡:M40 24G
3.內(nèi)存:32G
4.顯卡驅(qū)動(dòng):525 (cuda≥11.7即可)
5.cuda:11.7(必須)
6.環(huán)境:conda
準(zhǔn)備過程就不作贅述了,網(wǎng)上都搜得到
第二部分:neox部署-預(yù)處理數(shù)據(jù)
從https://github.com/ridgerchu/gpt-neox-RWKV上pull后進(jìn)入該目錄下
1.搭建預(yù)處理環(huán)境
conda create -n pre-t python=3.8 numpy flask libopenmpi-dev
2.安裝依賴
pip install torch --extra-index-url https://download.pytorch.org/whl/cu117 --upgrade
pip install -r requirements/requirements.txt
python ./megatron/fused_kernels/setup.py install
3.TXT轉(zhuǎn)換jsonl
預(yù)處理需要的jsonl格式為
{"meta": {"ID": 101}, "text": "This is the first document."}?
{"meta": {"ID": 102}, "text": "Hello\nWorld"}?
{"meta": {"ID": 103}, "text": "1+1=2\n1+2=3\n2+2=4"}?
python3 t2jsonl.py(代碼如下)
------------------------------------------------------------
import json
# Generate a list of dictionaries
lines = []
with open("訓(xùn)練文件.txt", encoding="utf8") as f:
? ? for line in f.read().splitlines():
? ? ? ? if line:
? ? ? ? ? ? lines.append({"text": line})
# Convert to a list of JSON strings
json_lines = [json.dumps(l) for l in lines]
# Join lines and save to .jsonl file
json_data = '\n'.join(json_lines)
with open('訓(xùn)練文件.jsonl', 'w') as f:
? ? f.write(json_data)
------------------------------------------------------------
4.預(yù)處理
python3 tools/preprocess_data.py --input ./訓(xùn)練文件.jsonl --output-prefix ./data/訓(xùn)練文件 --vocab ./20B_tokenizer.json --dataset-impl mmap --tokenizer-type HFTokenizer --append-eod
微調(diào)完成后會(huì)生成相應(yīng)的bin文件和idx文件,即為lora訓(xùn)練需要的訓(xùn)練集
第三部分:lora訓(xùn)練
從https://github.com/Blealtan/RWKV-LM-LoRA上pull后進(jìn)入RWKV-LM-LoRA/RWKV-v4neo/目錄下,將以上bin文件和idx文件復(fù)制進(jìn)該目錄(或從絕對(duì)路徑調(diào)用)
1.環(huán)境
conda create -n train python=3.10.6 numpy tokenizers prompt_toolkit
2.依賴
pip install torch --extra-index-url https://download.pytorch.org/whl/cu117 --upgrade
pip install pytorch_lightning
pip install deepspeed
pip install transformers
pip install rwkv
3.訓(xùn)練
訓(xùn)練格式:
python3 train.py --load_model 底模型.pth --lora_load lora模型.pth? --proj_dir lora_checkpoints --data_file 預(yù)處理數(shù)據(jù)(不需要加bin或idx,只要文件名) --data_type binidx --vocab_size 50277 --ctx_len 1024(看顯存,越大越好) --accumulate_grad_batches 8 --epoch_steps 1000 --epoch_count 20 --epoch_begin 0 --epoch_save 5 --micro_bsz 1 --n_layer 32(看模型) --n_embd 2560(看模型) --pre_ffn 0 --head_qk 0 --lr_init 1e-5 --lr_final 1e-5 --warmup_steps 0 --beta1 0.9 --beta2 0.999 --adam_eps 1e-8 --accelerator gpu --devices 1 --precision fp16 --strategy ddp_find_unused_parameters_false --grad_cp 1(開啟加速) --lora --lora_r 8 --lora_alpha 32 --lora_dropout 0.01 --lora_parts=att,ffn,time,ln
對(duì)于7B大模型可以參考以下參數(shù),我的內(nèi)存條32G沒辦法訓(xùn)練,建議要訓(xùn)練的仁兄內(nèi)存加大
python3 train.py --load_model RWKV-4-Pile-7B-EngChn-testNovel-2119-ctx2048-20230313.pth --lora_load rwkv-0 --proj_dir lora_checkpoints --data_file 11 --data_type binidx --vocab_size 50277 --ctx_len 512 --accumulate_grad_batches 8 --epoch_steps 1000 --epoch_count 20 --epoch_begin 0 --epoch_save 5 --micro_bsz 1 --n_layer 32 --n_embd 4096 --pre_ffn 0 --head_qk 0 --lr_init 1e-5 --lr_final 1e-5 --warmup_steps 0 --beta1 0.9 --beta2 0.999 --adam_eps 1e-8 --accelerator gpu --devices 1 --precision fp16 --strategy ddp_find_unused_parameters_false --grad_cp 1 --lora --lora_r 8 --lora_alpha 32 --lora_dropout 0.01 --lora_parts=att,ffn,time,ln
對(duì)于3B大模型可以參考以下參數(shù),內(nèi)存占用20G,顯存16G
python3 train.py --load_model RWKV-4-Pile-3B-Chn-testNovel-done-ctx2048-20230312.pth --lora_load rwkv-0 --proj_dir lora_checkpoints --data_file 11 --data_type binidx --vocab_size 50277 --ctx_len 2048 --accumulate_grad_batches 8 --epoch_steps 1000 --epoch_count 20 --epoch_begin 0 --epoch_save 5 --micro_bsz 1 --n_layer 32 --n_embd 2560 --pre_ffn 0 --head_qk 0 --lr_init 1e-5 --lr_final 1e-5 --warmup_steps 0 --beta1 0.9 --beta2 0.999 --adam_eps 1e-8 --accelerator gpu --devices 1 --precision fp16 --strategy ddp_find_unused_parameters_false --grad_cp 1 --lora --lora_r 8 --lora_alpha 32 --lora_dropout 0.01 --lora_parts=att,ffn,time,ln
5.測(cè)試
RWKV_JIT_ON=1 python chat.py(需要在chat.py文件里提前寫好相應(yīng)配置,包括:模型地址、lora地址、lora訓(xùn)練參數(shù)、n_layer、n_embd等)
參考資料:
neox預(yù)處理(轉(zhuǎn)格式binidx):https://github.com/ridgerchu/gpt-neox-RWKV#training-and-finetuning
lora訓(xùn)練:https://github.com/Blealtan/RWKV-LM-LoRA#lora-fork-of-rwkv-lm
rwkv:https://github.com/BlinkDL/RWKV-LM#training--fine-tuning