> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/bo-ke/fine-tuning-llms-with-nvidia-dgx-spark-and-unsloth.md).

# 使用 NVIDIA DGX Spark 和 Unsloth 对 LLM 进行微调

关于如何在 NVIDIA DGX Spark 上使用 OpenAI gpt-oss 进行微调和强化学习（RL）的教程。

Unsloth 使 LLM 可在本地进行微调，规模最高可达 **200B 参数** ，并在 NVIDIA DGX™ Spark 上运行。借助 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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-ff5c4752dccb8f922b937f8e3b0db58e2d836507%2Funsloth%20nvidia%20dgx%20spark.png?alt=media" alt="" width="375"><figcaption></figcaption></figure> <figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-a8472482c49e1763378b609f8f537ca89df60260%2FNotebooks%20on%20dgx.png?alt=media" 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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-3bdcb0fda2ad188142e58f04c855b6dcfbd5ba94%2Fopenai%20devday%20unsloth%20feature.png?alt=media" alt="" width="375"><figcaption><p>你可以观看在 OpenAI DevDay 2025 上亮相的 Unsloth <a href="https://youtu.be/1HL2YHRj270?si=8SR6EChF34B1g-5r&#x26;t=1080">这里</a>.</p></figcaption></figure> <figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-4a8bd4ecc7ee3d123c19158df5dfdcec35df8532%2FScreenshot%202025-10-13%20at%204.22.32%E2%80%AFPM.png?alt=media" 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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-7ebcf195c154b0e569115e1f9513cf002ee57b16%2Fdgx1.png?alt=media" 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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-a67c36494f5c4ab4017748d490fb258655cd2378%2Fdgx2.png?alt=media" alt=""><figcaption></figcaption></figure> <figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-b7758db087ab8b724049361781952b5ed154dfe8%2Fdgx5.png?alt=media" alt=""><figcaption></figcaption></figure></div>
{% endstep %}

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

在容器中，启动 Jupyter 并运行所需的笔记本。你可以使用 Reinforcement Learning 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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-a8472482c49e1763378b609f8f537ca89df60260%2FNotebooks%20on%20dgx.png?alt=media" 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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-0862eed0acf0656ff0cb802b6aebc30892997e3b%2Fdgx6.png?alt=media" 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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-e079a9aa8d853b319520fe0f0fbcca2e85b31ea6%2Fdgx7.png?alt=media" alt=""><figcaption></figcaption></figure> <figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-c389a73a48ad059bbb92121b328fa7ccc61bee95%2Fdgx8.png?alt=media" alt=""><figcaption></figcaption></figure></div>

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

### 视频教程

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

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/zh/bo-ke/fine-tuning-llms-with-nvidia-dgx-spark-and-unsloth.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
