本文基于开源代码https://github.com/Lightning-AI/lit-llama/tree/main执行微调
其他参考链接:
Accelerating LLaMA with Fabric: A Comprehensive Guide to Training and Fine-Tuning LLaMA - Lightning AI
结构化数据示例: BelleGroup/train_0.5M_CN · Datasets at Hugging Face
finetune记录
在Alpaca数据集上finetune
下载代码,配置环境:https://github.com/Lightning-AI/lit-llama/blob/main/README.md
准备模型:https://github.com/Lightning-AI/lit-llama/blob/main/howto/download_weights.md
finetune on Alpaca dataset:https://github.com/Lightning-AI/lit-llama/blob/main/README.md
在个人数据集finetune
准备数据集:
对于结构化数据集(json结构,包含“instructions”、“output”字段),复制lit-llama-main/scripts/prepare_alpaca.py代码,修改相应的输入文件、训练校验集划分、保存路径等
对于非结构化数据集,复制lit-llama-main/scripts/prepare_any_text.py代码,修改相应内容。可参考以下链接https://github.com/Lightning-AI/lit-llama/blob/main/howto/unstructured_dataset.md
训练:
修改lit-llama-main/finetune/lora.py代码中的数据集路径、输出数据路径,运行代码即开始训练
推理:
修改lit-llama-main/generate/lora.py模型路径,进行推理
改bug记录
bug:
执行generate.py出错:ValueError: Precision 'bf16-true' is invalid. Allowed precision values: ('16-mixed', 'bf16-mixed', '32-true', '64-true', 64, 32, 16, '64', '32', '16', 'bf16')
解决:
lightning 版本错误
pip uninstall -y lightning; pip install -r requirements.txt
或下载lighning zip,然后 pip install lightning-master.zip -U
GitHub - Lightning-AI/lightning: Deep learning framework to train, deploy, and ship AI products Lightning fast.cc
参考:
ValueError: Precision 'bf16-true' is invalid. Allowed precision values: ('16-mixed', 'bf16-mixed', '32-true', '64-true', 64, 32, 16, '64', '32', '16', 'bf16') · Issue #241 · Lightning-AI/lit-gpt · GitHub
bug:No module named 'torch.utils._device'
解决:No module named 'torch.utils._device' · Issue #135 · Lightning-AI/lit-llama · GitHub
bug:RuntimeError: CUDA error: device-side assert triggered CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect. For debugging consider passing CUDA_LAUNCH_BLOCKING=1. Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
解决:bug记录往上翻看到是max_seq_length的问题,定位到对应函数,传入更大的max_seq_length。