最近有个项目,想通过大预言模型的微调来进一步增强RAG的效率,才有了几天的折腾,虽然最后还是没有达到预期,还是记录一下这个过程,没准后期还得再来一遍。
unsloth是一个大语言模型微调工具,支持多种包括刚开源的Qwen2、llama3等主流开源模型,可导出为GGUF(这样就可以直接在ollama中使用了,巨方便),项目地址:https://github.com/unslothai/unsloth。
该项目号称Unsloth makes fine-tuning of LLMs 2.2x faster and use 80% less VRAM!,呃~,我觉得是4位量化这个前提下。
吐个槽,先不谈微调训练了,只是安装就快把我整吐了,对于python小白、非开发者、非N卡、低显存(16G以下)、网络不稳定的,劝退吧。
首先是安装环境,在乌班图系统中可能会更方便一些,但是懒得装双系统了,windows还是主力。
1、安装anaconda,必装!
下载地址:https://www.anaconda.com/download/success,一路回车就行
2、创建虚拟环境
在指定目录创建一个虚拟环境
conda create --prefix=I:\unsloth python=3.11
注意,官方的安装说明中使用的python用的是3.10,我这里使用的是3.11,要跟后面安装的pytorch等版本对齐。
# 激活
activate I:\unsloth
3、安装pytorch
不太建议用命令行,单独下载对应版本安装最快最稳。下载地址:https://download.pytorch.org/whl/torch/
这里要根据python版本和cuda版本来选择具体的版本,查看cuda版本命令:nvcc --version
常用的有cuda11.8和12.1,要根据上面查看的版本来决定。
然后执行安装:
pip install I:\qlora\down\torch-2.2.2+cu121-cp311-cp311-win_amd64.whl
官方的安装命令,我装了几次,最后总会装成cpu的版本,不知道问题在哪,最后自己下载单独安装一次成功,而且官方安装方法装完到最后还会有依赖冲突,如下
ERROR: pip’s dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
xformers 0.0.26.post1 requires torch==2.3.0, but you have torch 2.2.2+cu121 which is incompatible.
还有一点,一旦发生这类错误,必须先把torch卸载干净,然后重新指定版本安装,否则错误依旧!
4、安装xformers
pip install --no-deps trl peft accelerate bitsandbytes
pip install xformers
5、安装triton、deepspeed
pip install deepspeed-0.13.1+unknown-py3-none-any.whl
pip install triton-2.1.0-cp311-cp311-win_amd64.whl
从网盘下载上面的两个文件https://pan.quark.cn/s/5f5fe67940ab 提取码:2f7P,到任意文件夹,拖到pip install后面即可。
安装过程全程无报错就说明把环境搭好了,最后就是把项目拖到本地就行啦。
最后贴下我的pip list
(unsloth) I:\unsloth>pip list
Package Version
------------------ --------------
accelerate 0.31.0
aiohttp 3.9.5
aiosignal 1.3.1
annotated-types 0.7.0
attrs 23.2.0
bitsandbytes 0.43.1
Brotli 1.0.9
certifi 2024.6.2
charset-normalizer 2.0.4
colorama 0.4.6
datasets 2.20.0
deepspeed 0.13.1+unknown
dill 0.3.8
docstring_parser 0.16
filelock 3.13.1
frozenlist 1.4.1
fsspec 2024.5.0
gmpy2 2.1.2
hjson 3.1.0
huggingface-hub 0.23.4
idna 3.7
intel-openmp 2021.4.0
Jinja2 3.1.4
markdown-it-py 3.0.0
MarkupSafe 2.1.3
mdurl 0.1.2
mkl 2021.4.0
mkl-fft 1.3.1
mkl-random 1.2.2
mkl-service 2.4.0
mpmath 1.3.0
multidict 6.0.5
multiprocess 0.70.16
networkx 3.2.1
ninja 1.11.1.1
numpy 1.24.3
packaging 24.1
pandas 2.2.2
peft 0.11.1
pillow 10.3.0
pip 24.0
protobuf 3.20.3
psutil 6.0.0
py-cpuinfo 9.0.0
pyarrow 16.1.0
pyarrow-hotfix 0.6
pydantic 2.7.4
pydantic_core 2.18.4
Pygments 2.18.0
pynvml 11.5.0
PySocks 1.7.1
python-dateutil 2.9.0.post0
pytz 2024.1
PyYAML 6.0.1
regex 2024.5.15
requests 2.32.2
rich 13.7.1
safetensors 0.4.3
sentencepiece 0.2.0
setuptools 69.5.1
shtab 1.7.1
six 1.16.0
sympy 1.12.1
tbb 2021.13.0
tokenizers 0.19.1
torch 2.3.0+cu121
torchaudio 2.3.0
torchvision 0.18.0
tqdm 4.66.4
transformers 4.41.2
triton 2.1.0
trl 0.9.4
typing_extensions 4.11.0
tyro 0.8.4
tzdata 2024.1
unsloth 2024.6
urllib3 2.2.2
wheel 0.43.0
win-inet-pton 1.1.0
xformers 0.0.26.post1
xxhash 3.4.1
yarl 1.9.4
相关参考和资源:
1、unsloth ai https://huggingface.co/unsloth
2、Qwen2微调colab笔记 https://colab.research.google.com/drive/1mvwsIQWDs2EdZxZQF9pRGnnOvE86MVvR?usp=sharing
3、unsloth开源仓库 https://github.com/unslothai/unsloth
4、基于自己数据微调LLama3并本地化部署https://zhuanlan.zhihu.com/p/701751309
5、Qwen2在windows下的微调https://github.com/v3ucn/Unsloth-Windows-fineTuning-Qwen2