我在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"