安装unsloth
我在llamafactory微调LLM,简单测了一些(很不精准),加速方法中unsloth比flash_attention速度快了40%,显存占用减少15%;
- 创建虚拟环境:
conda create -n env_name python=3.10
, 然后conda activate env_name
- 安装cudatoolkit:
conda install cudatoolkit=12.1
或conda install nvidia/label/cuda-12.1.0::cuda-toolkit
,可能失败 - 安装pytorch、triton和xformers:
pip install torch==2.4.0 triton pip install xformers==0.0.27.post2 --index-url https://download.pytorch.org/whl/cu121
(如果第二步失败,这步也会配上cuda环境)(嫌慢可以先去https://download.pytorch.org/whl/cu121把包下好,不过记得这几个包一起安装,不然可能会覆盖前面安装的,例如单独安装triton和xformers会下载cpu版torch) - 安装unsloth:
pip install "unsloth[cu121-torch240] @ git+https://github.com/unslothai/unsloth.git"
或者pip install --upgrade --force-reinstall --no-cache-dir git+https://github.com/unslothai/unsloth.git
- 记得比对一下包的版本是不是如上所述,因为有的包可能会把依赖包卸了装个新的。。(包不太容易安装,依赖很复杂,所以不建议用
--no-deps
和--ignore-installed
等方式)
第5步有大佬在博客说要装ampere,我是没装也能用(尝试装了下,在flash-attn那里卡住了),仅供参考:
pip install "unsloth[cu121-ampere-torch240] @ git+https://github.com/unslothai/unsloth.git"