# 使用 Blackwell、RTX 50 系列与 Unsloth 微调 LLM

Unsloth 现在支持 NVIDIA 的 Blackwell 架构 GPU，包括 RTX 50 系列 GPU（5060–5090）、RTX PRO 6000，以及 B200、B40、GB100、GB102 等 GPU！您可以阅读官方的 [NVIDIA 博客文章在这里](https://developer.nvidia.com/blog/train-an-llm-on-an-nvidia-blackwell-desktop-with-unsloth-and-scale-it/).

Unsloth 现在兼容自 2018 年以来的所有 NVIDIA GPU，包括 [DGX Spark](/docs/zh/bo-ke/fine-tuning-llms-with-nvidia-dgx-spark-and-unsloth.md).

> **我们的新** [**Docker 镜像**](#docker) **支持 Blackwell。运行该 Docker 镜像并开始训练！** [**指南**](/docs/zh/bo-ke/fine-tuning-llms-with-blackwell-rtx-50-series-and-unsloth.md)

### Pip 安装

只需安装 Unsloth：

```bash
pip install unsloth
```

如果遇到问题，另一种选择是创建一个独立的隔离环境：

```bash
python -m venv unsloth
source unsloth/bin/activate
pip install unsloth
```

注意 可能是 `pip3` 或 `pip3.13` 并且也可能是 `python3` 或 `python3.13`

您可能会遇到一些 Xformers 问题，在这种情况下您应当从源码构建：

{% code overflow="wrap" %}

```bash
# 首先卸载先前库安装的 xformers
pip uninstall xformers -y

# 克隆并构建
pip install ninja
export TORCH_CUDA_ARCH_LIST="12.0"
git clone --depth=1 https://github.com/facebookresearch/xformers --recursive
cd xformers && python setup.py install && cd ..
```

{% endcode %}

### Docker

[**`unsloth/unsloth`**](https://hub.docker.com/r/unsloth/unsloth) 是 Unsloth 的唯一 Docker 镜像。对于 Blackwell 和 50 系列 GPU，请使用相同的镜像——无需单独的镜像。

有关安装说明，请参阅我们的 [Unsloth Docker 指南](/docs/zh/bo-ke/how-to-fine-tune-llms-with-unsloth-and-docker.md).

### uv

```bash
uv pip install unsloth
```

#### uv（高级）

安装顺序很重要，因为我们希望用特定版本覆盖捆绑的依赖项（即， `xformers` 和 `triton`).

1. 我更喜欢使用 `uv` 而不是 `pip` 因为它更快并且在解析依赖项时更好，尤其是对于依赖 `torch` 但在此场景下需要特定的 `CUDA` 版本。

   安装 `uv`

   ```bash
   curl -LsSf https://astral.sh/uv/install.sh | sh && source $HOME/.local/bin/env
   ```

   创建项目目录和虚拟环境：

   ```bash
   mkdir 'unsloth-blackwell' && cd 'unsloth-blackwell'
   uv venv .venv --python=3.12 --seed
   source .venv/bin/activate
   ```
2. 安装 `vllm`

   ```bash
   uv pip install -U vllm --torch-backend=cu128
   ```

   注意我们必须指定 `cu128`，否则 `vllm` 将安装 `torch==2.7.0` 但会使用 `cu126`.
3. 安装 `unsloth` 依赖项

   ```bash
   uv pip install unsloth unsloth_zoo bitsandbytes
   ```

   如果您注意到由于 Xformers 导致的奇怪解析问题，您也可以从源码安装 Unsloth 而不安装 Xformers：

   ```bash
   uv pip install -qqq \
   "unsloth_zoo[base] @ git+https://github.com/unslothai/unsloth-zoo" \
   "unsloth[base] @ git+https://github.com/unslothai/unsloth"
   ```
4. 下载并构建 `xformers` （可选）

   Xformers 是可选的，但它确实更快且占用更少内存。如果您不想使用 Xformers，我们将使用 PyTorch 的原生 SDPA。注意，从源码构建 Xformers 可能会很慢，请注意！

   ```bash
   # 首先卸载先前库安装的 xformers
   pip uninstall xformers -y

   # 克隆并构建
   pip install ninja
   export TORCH_CUDA_ARCH_LIST="12.0"
   git clone --depth=1 https://github.com/facebookresearch/xformers --recursive
   cd xformers && python setup.py install && cd ..
   ```

   注意我们必须显式设置 `TORCH_CUDA_ARCH_LIST=12.0`.
5. `transformers` 安装任何版本的 transformers，但最好获取最新版本。

   ```bash
   uv pip install -U transformers
   ```

### Conda 或 mamba（高级）

1. 安装 `conda/mamba`

   ```bash
   curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
   ```

   运行安装脚本

   ```bash
   bash Miniforge3-$(uname)-$(uname -m).sh
   ```

   创建 conda 或 mamba 环境

   ```bash
   conda create --name unsloth-blackwell python==3.12 -y
   ```

   激活新创建的环境

   ```bash
   conda activate unsloth-blackwell
   ```
2. 安装 `vllm`

   确保您处于已激活的 conda/mamba 环境中。您应该在终端提示符前看到环境名称作为前缀，如下所示，您的 `(unsloth-blackwell)user@machine:`

   ```bash
   pip install -U vllm --extra-index-url https://download.pytorch.org/whl/cu128
   ```

   注意我们必须指定 `cu128`，否则 `vllm` 将安装 `torch==2.7.0` 但会使用 `cu126`.
3. 安装 `unsloth` 依赖项

   确保您处于已激活的 conda/mamba 环境中。您应该在终端提示符前看到环境名称作为前缀，如下所示，您的 `(unsloth-blackwell)user@machine:`

   ```bash
   pip install unsloth unsloth_zoo bitsandbytes
   ```
4. 下载并构建 `xformers` （可选）

   Xformers 是可选的，但它确实更快且占用更少内存。如果您不想使用 Xformers，我们将使用 PyTorch 的原生 SDPA。注意，从源码构建 Xformers 可能会很慢，请注意！

   您应该在终端提示符前看到环境名称作为前缀，如下所示，您的 `(unsloth-blackwell)user@machine:`

   ```bash
   # 首先卸载先前库安装的 xformers
   pip uninstall xformers -y

   # 克隆并构建
   pip install ninja
   export TORCH_CUDA_ARCH_LIST="12.0"
   git clone --depth=1 https://github.com/facebookresearch/xformers --recursive
   cd xformers && python setup.py install && cd ..
   ```

   注意我们必须显式设置 `TORCH_CUDA_ARCH_LIST=12.0`.
5. 更新 `triton`

   确保您处于已激活的 conda/mamba 环境中。您应该在终端提示符前看到环境名称作为前缀，如下所示，您的 `(unsloth-blackwell)user@machine:`

   ```bash
   pip install -U triton>=3.3.1
   ```

   `triton>=3.3.1` 是所需的 `Blackwell` 支持。
6. `Transformers` 安装任何版本的 transformers，但最好获取最新版本。

   ```bash
   uv pip install -U transformers
   ```

如果您使用 mamba 作为包管理器，只需将上面所有命令中的 conda 替换为 mamba。

### WSL 特定注意事项

如果您在使用 WSL（Windows 子系统 Linux）并在 xformers 编译过程中遇到问题（提醒：Xformers 是可选的，但对训练更快），请遵循这些额外步骤：

1. **增加 WSL 内存限制** 创建或编辑 WSL 配置文件：

   ```bash
   # 在您的 Windows 用户目录中创建或编辑 .wslconfig
   #（通常位于 C:\Users\YourUsername\.wslconfig）

   # 将这些行添加到文件中
   [wsl2]
   memory=16GB  # 建议至少 16GB 以进行 xformers 编译
   processors=4  # 根据您的 CPU 核心调整
   swap=2GB
   localhostForwarding=true
   ```

   在进行这些更改后，重启 WSL：

   ```powershell
   wsl --shutdown
   ```
2. **安装 xformers** 使用以下命令在 WSL 上以优化的编译方式安装 xformers：

   ```bash
   # 为 Blackwell GPU 设置 CUDA 架构
   export TORCH_CUDA_ARCH_LIST="12.0"

   # 使用优化的构建标志从源码安装 xformers
   pip install -v --no-build-isolation -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers
   ```

   该 `--no-build-isolation` 标志有助于避免在 WSL 环境中可能出现的构建问题。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unsloth.ai/docs/zh/bo-ke/fine-tuning-llms-with-blackwell-rtx-50-series-and-unsloth.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
