uvでUnslothをインストールする

これは何?
Pythonのプロジェクト管理ツールであるuvを用いてUnslothとvLLMをインストールする方法をまとめます。なおUnslothとvLLMの詳細については説明しません。uvのversionはv0.5.3
以上を推奨します。
Unslothのインストール
Unslothをuvでインストールする場合は、先にPyTorchをインストールしましょう。UnslothもvLLMもPyTorchのversionに制約があることに注意です。
unsloth 2024.12.4
現在、CUDA versionはcu118
, cu121
, cu124
を、PyTorch versionは2.1.1
, 2.1.2
, 2.2.0
, 2.3.0
, 2.4.0
, 2.5.0
をサポートしています。UnslothのREADME、またはpyproject.toml
の[project.optional-dependencies]
を確認しましょう。
今回はPython 3.11
に固定して、PyTorch v2.5.0
とcu124
をインストールします。Using uv with PyTorch - uvまたはPythonのプロジェクト管理ツール uv のv0.5.3までの便利な機能 - dependencies編を参考にインストールしましょう。
以下のようなpyproject.toml
になります。
[project]
name = "project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11,<3.12"
dependencies = [
"torch==2.5.0",
]
[[tool.uv.index]]
name = "pytorch-cu124"
url = "https://download.pytorch.org/whl/cu124"
explicit = true
[tool.uv.sources]
torch = { index = "pytorch-cu124" }
unsloth = { git = "https://github.com/unslothai/unsloth.git" }
次にUnslothをインストールします。UnslothはPyPIにも公開されていますが、README通りGitHubリポジトリのURL指定をして、extras名は[cu124-torch250 ]
を指定します。
[tool.uv.sources]
にunsloth = { git = "https://github.com/unslothai/unsloth.git" }
を指定して、uv add unsloth[cu124-torch250 ]
を実行します。また、追加でsetuptools
をインストールすると以下のようなpyproject.toml
になります。
[project]
name = "project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11,<3.12"
dependencies = [
"setuptools>=75.6.0",
"torch==2.5.0",
"unsloth[cu124-torch250]>=2024.12.4",
]
[[tool.uv.index]]
name = "pytorch-cu124"
url = "https://download.pytorch.org/whl/cu124"
explicit = true
[tool.uv.sources]
torch = { index = "pytorch-cu124" }
unsloth = { git = "https://github.com/unslothai/unsloth.git" }
以下を実行してimportできるか検証しましょう。無事importできれば完了です👏
uv run python -c "import unsloth"
🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
🦥 Unsloth Zoo will now patch everything to make training faster!

結果としてはうまくいかなかったのですが、メモとして残しておきます。
NVIDIA ampere device以上の場合のUnsloath
UnslothではNVIDIA ampereを含むそれ移行のアーキテクチャであるAdaやHooper(e.g., A100, RTX 3090, RTX 4090, H100)では、cu118-ampere
, cu121-ampere
, cu124-ampere
でのインストールが推奨されています。Unslothのpyproject.toml
を見ると、flash-attn
のインストールを行うことになります。そのためampere
のextrasの場合、Unslothをno-build-isolation
としてインストールする必要があります。
4.1 flash-attentionインストールをもとにすると以下のようになります。
[project]
name = "project"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11,<3.12"
dependencies = [
"setuptools>=75.6.0",
"torch==2.5.0",
]
[project.optional-dependencies]
build = [
"ninja>=1.11.1.2",
"packaging>=24.2",
"psutil>=6.1.0",
]
compile = [
"unsloth[cu124-ampere-torch250]>=2024.12.4",
]
[[tool.uv.index]]
name = "pytorch-cu124"
url = "https://download.pytorch.org/whl/cu124"
explicit = true
[tool.uv.sources]
torch = { index = "pytorch-cu124" }
unsloth = { git = "https://github.com/unslothai/unsloth.git" }
[tool.uv]
no-build-isolation-package = ["unsloth"]
以下のコマンドでunsloth[cu124-ampere-torch250]
をインストールします。
uv sync --extra build
uv sync --extra build --extra compile
以下を実行しましょう。
uv run python -c "import unsloth"
するとFlash Attention 2のインストールがうまくいっていないとのことでした。残念😢
🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
Unsloth: Your Flash Attention 2 installation seems to be broken?
A possible explanation is you have a new CUDA version which isn't
yet compatible with FA2? Please file a ticket to Unsloth or FA2.
We shall now use Xformers instead, which does not have any performance hits!
We found this negligible impact by benchmarking on 1x A100.
🦥 Unsloth Zoo will now patch everything to make training faster!