# 使用 NVIDIA DGX Spark 和 Unsloth 微调 LLM

Unsloth 使得在 NVIDIA DGX™ Spark 上对最大 **200B 参数** 的大型语言模型进行本地微调成为可能。借助 128 GB 的统一内存，您可以训练诸如 **gpt-oss-120b**等大型模型，并在 DGX Spark 上直接运行或部署推理。

如在 [OpenAI DevDay](https://x.com/UnslothAI/status/1976284209842118714)所示，gpt-oss-20b 曾在 DGX Spark 上使用 RL 和 Unsloth 训练以自动赢得 2048。您可以在 DGX Spark 的 Docker 容器或虚拟环境中使用 Unsloth 进行训练。

<div align="center" data-full-width="false"><figure><img src="/files/3b32aa91b2119c7eb199e4de091ea48dacbe4d52" alt="" width="375"><figcaption></figcaption></figure> <figure><img src="/files/ed732a70e6b53b763b82a2907d7e1799b143503a" alt="" width="375"><figcaption></figcaption></figure></div>

在本教程中，我们将在 DGX Spark 上安装 Unsloth 后，使用 Unsloth 笔记本通过 RL 训练 gpt-oss-20b。gpt-oss-120b 将使用大约 **68GB** 的统一内存。

在 1,000 步和 4 小时的 RL 训练后，gpt-oss 模型在 2048 上大大优于原始模型，且更长时间的训练会进一步提升结果。

<div><figure><img src="/files/7458e91add103d6c790e8d6f988b7b94ef45f2f3" alt="" width="375"><figcaption><p>您可以观看 Unsloth 在 OpenAI DevDay 2025 上的展示 <a href="https://youtu.be/1HL2YHRj270?si=8SR6EChF34B1g-5r&#x26;t=1080">此处</a>.</p></figcaption></figure> <figure><img src="/files/6ead5d9c98a6ea7897df2933777427342ce22d34" alt="" width="375"><figcaption><p>使用 RL 训练的 gpt-oss 在 2048 上持续表现更佳。</p></figcaption></figure></div>

### ⚡ 逐步教程

{% stepper %}
{% step %}
**从 DGX Spark 的 Unsloth Docker 镜像开始**

首先，使用 DGX Spark Dockerfile 构建 Docker 镜像，该文件可以 [在此找到](https://raw.githubusercontent.com/unslothai/notebooks/main/Dockerfile_DGX_Spark)。您也可以在 DGX Spark 的终端中运行以下命令：

```bash
sudo apt update && sudo apt install -y wget
wget -O Dockerfile "https://raw.githubusercontent.com/unslothai/notebooks/main/Dockerfile_DGX_Spark"
```

然后，使用保存的 Dockerfile 构建训练用 Docker 镜像：

```bash
docker build -f Dockerfile -t unsloth-dgx-spark .
```

<figure><img src="/files/65e8dc1fa3a322fd705d77301fb6d8306fb462fb" alt="" width="563"><figcaption></figcaption></figure>

<details>

<summary>您也可以点击查看完整的 DGX Spark Dockerfile</summary>

```python
FROM nvcr.io/nvidia/pytorch:25.09-py3

# 设置 CUDA 环境变量
ENV CUDA_HOME=/usr/local/cuda-13.0/
ENV CUDA_PATH=$CUDA_HOME
ENV PATH=$CUDA_HOME/bin:$PATH
ENV LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
ENV C_INCLUDE_PATH=$CUDA_HOME/include:$C_INCLUDE_PATH
ENV CPLUS_INCLUDE_PATH=$CUDA_HOME/include:$CPLUS_INCLUDE_PATH

# 从源代码安装 triton 以支持最新的 blackwell
RUN git clone https://github.com/triton-lang/triton.git && \
    cd triton && \
    git checkout c5d671f91d90f40900027382f98b17a3e04045f6 && \
    pip install -r python/requirements.txt && \
    pip install . && \
    cd ..

# 从源代码安装 xformers 以支持 blackwell
RUN git clone --depth=1 https://github.com/facebookresearch/xformers --recursive && \
    cd xformers && \
    export TORCH_CUDA_ARCH_LIST="12.1" && \
    python setup.py install && \
    cd ..

# 安装 unsloth 及其他依赖
RUN pip install unsloth unsloth_zoo bitsandbytes==0.48.0 transformers==4.56.2 trl==0.22.2

# 启动 shell
CMD ["/bin/bash"]
```

</details>
{% endstep %}

{% step %}
**启动容器**

以 GPU 访问和卷挂载启动训练容器：

```bash
docker run -it \
    --gpus=all \
    --net=host \
    --ipc=host \
    --ulimit memlock=-1 \
    --ulimit stack=67108864 \
    -v $(pwd):$(pwd) \
    -v $HOME/.cache/huggingface:/root/.cache/huggingface \
    -w $(pwd) \
    unsloth-dgx-spark
```

<div><figure><img src="/files/08ad14f3cc26674e0f464e949f80f7ee45fcb1f6" alt=""><figcaption></figcaption></figure> <figure><img src="/files/0353f44250429ed4e3969077ecfadec79c7e4996" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

{% step %}
**启动 Jupyter 并运行笔记本**

在容器内，启动 Jupyter 并运行所需的笔记本。您可以使用“强化学习 gpt-oss 20b 赢取 2048” [笔记本在此](https://github.com/unslothai/notebooks/blob/main/nb/gpt_oss_\(20B\)_Reinforcement_Learning_2048_Game_DGX_Spark.ipynb)。实际上，所有 [Unsloth 笔记本](https://docs.unsloth.ai/get-started/unsloth-notebooks) 都可在 DGX Spark 中运行，包括 **120b** 笔记本！只需移除安装单元格即可。

<figure><img src="/files/ed732a70e6b53b763b82a2907d7e1799b143503a" alt="" width="563"><figcaption></figcaption></figure>

下面的命令也可用于运行 RL 笔记本。Jupyter Notebook 启动后，打开“`gpt_oss_20B_RL_2048_Game.ipynb`”

```bash
NOTEBOOK_URL="https://raw.githubusercontent.com/unslothai/notebooks/refs/heads/main/nb/gpt_oss_(20B)_Reinforcement_Learning_2048_Game_DGX_Spark.ipynb"
wget -O "gpt_oss_20B_RL_2048_Game.ipynb" "$NOTEBOOK_URL"

jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root
```

<figure><img src="/files/5e98bd35a0f01664a073302fea312f765dabb268" alt="" width="563"><figcaption></figcaption></figure>

别忘了 Unsloth 还允许您 [保存并运行](/docs/zh/ji-chu-zhi-shi/inference-and-deployment.md) 微调后的模型，以便您之后可以在本地直接将它们部署到 DGX Spark 上。
{% endstep %}
{% endstepper %}

非常感谢 [Lakshmi Ramesh](https://www.linkedin.com/in/rlakshmi24/) 和 [Barath Anandan](https://www.linkedin.com/in/barathsa/) 来自 NVIDIA 的帮助，他们协助 Unsloth 在 DGX Spark 上发布并构建了该 Docker 镜像。

### 统一内存使用情况

gpt-oss-120b QLoRA 4-bit 微调将使用大约 **68GB** 的统一内存。您的统一内存使用情况在 **之前** （左）和 **之后** （右）训练时应如下所示：

<div><figure><img src="/files/b39c64577f30a8b0c26b204a65697170b8e03efa" alt=""><figcaption></figcaption></figure> <figure><img src="/files/635db16a5f710c2a72e45a1e3b16dc5ae8185226" alt=""><figcaption></figcaption></figure></div>

就是这样！祝您在 NVIDIA DGX Spark 上完全本地训练和运行 LLM 玩得开心！

### 视频教程

感谢来自 [AnythingLLM](https://github.com/Mintplex-Labs/anything-llm) 的 Tim 提供了在 DGX Spark 上使用 Unsloth 进行微调的精彩教程：

{% embed url="<https://www.youtube.com/watch?t=962s&v=zs-J9sKxvoM>" %}


---

# 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-nvidia-dgx-spark-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.
