> 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/mo-xing/tutorials/deepseek-r1-0528-how-to-run-locally.md).

# DeepSeek-R1-0528：如何在本地运行

DeepSeek-R1-0528 是 DeepSeek 对其 R1 推理模型的新更新。完整的 671B 参数模型需要 715GB 的磁盘空间。量化动态 **1.66 位** 版本占用 162GB（体积减少 80%）。GGUF： [DeepSeek-R1-0528-GGUF](https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF)

DeepSeek 还通过微调 Qwen3（8B）发布了一个 R1-0528 蒸馏版本。该蒸馏模型实现了与 Qwen3（235B）相近的性能。 ***你也可以*** [***微调 Qwen3 Distill***](#fine-tuning-deepseek-r1-0528-with-unsloth) ***使用 Unsloth***。Qwen3 GGUF： [DeepSeek-R1-0528-Qwen3-8B-GGUF](https://huggingface.co/unsloth/DeepSeek-R1-0528-Qwen3-8B-GGUF)

所有上传都使用 Unsloth 的 [Dynamic 2.0](/docs/zh/ji-chu/unsloth-dynamic-2.0-ggufs.md) 以实现最先进的 5-shot MMLU 和 KL 散度性能，这意味着你可以在精度损失极小的情况下运行并微调量化版 DeepSeek LLM。

**教程导航：**

<a href="#run-qwen3-distilled-r1-in-llama.cpp" class="button secondary">在 llama.cpp 中运行</a><a href="#run-in-ollama-open-webui" class="button secondary">在 Ollama/Open WebUI 中运行</a><a href="#fine-tuning-deepseek-r1-0528-with-unsloth" class="button secondary">微调 R1-0528</a>

{% hint style="success" %}
全新：工具调用能力大幅提升，并修复了聊天模板。\
\
新的 [TQ1\_0 动态 1.66 位量化](https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF?show_file_info=DeepSeek-R1-0528-UD-TQ1_0.gguf) - 大小为 162GB。非常适合 192GB RAM（包括 Mac）和 Ollama 用户。试试： `ollama run hf.co/unsloth/DeepSeek-R1-0528-GGUF:TQ1_0`
{% endhint %}

## :gear: 推荐设置

对于 DeepSeek-R1-0528-Qwen3-8B，这个模型几乎适合任何配置，甚至 20GB RAM 左右的环境也可以。事先无需任何准备。\
\
不过，对于完整的 R1-0528 模型（大小为 715GB），你需要做额外准备。1.78 位（IQ1\_S）量化可以装入 1 张 24GB GPU（并将所有层卸载）。如果你另外还有 128GB RAM，预计这种配置下速度约为 5 token/s。

建议至少拥有 64GB RAM 来运行这个量化版本（如果没有 GPU，你将得到 1 token/s）。为了获得最佳性能，你至少需要 **180GB 统一内存或 180GB 的 RAM+VRAM 总和** 以达到 5+ token/s。

我们建议使用我们的 2.7 位（Q2\_K\_XL）或 2.4 位（IQ2\_XXS）量化，以平衡体积和精度！2.4 位版本也表现很好。

{% hint style="success" %}
虽然不是必须，但为了获得最佳性能，请确保你的 VRAM + RAM 总和 = 你正在下载的量化版本大小。
{% endhint %}

### 🐳 官方推荐设置：

根据 [DeepSeek](https://huggingface.co/deepseek-ai/DeepSeek-R1-0528)的说法，这些是 R1（R1-0528 和 Qwen3 蒸馏版应使用相同设置）推理的推荐设置：

* 将 <mark style="background-color:green;">**temperature 设为 0.6**</mark> 以减少重复和不连贯。
* 将 <mark style="background-color:green;">**top\_p 设为 0.95**</mark> （推荐）
* 运行多次测试并对结果取平均，以获得可靠评估。

### :1234: 聊天模板/提示词格式

R1-0528 使用与原始 R1 模型相同的聊天模板。你不需要强制加入 `<think>\n` ，但你仍然可以添加它！

```
<｜begin▁of▁sentence｜><｜User｜>What is 1+1?<｜Assistant｜>It's 2.<｜end▁of▁sentence｜><｜User｜>Explain more!<｜Assistant｜>
```

会被强制添加一个 BOS，并且每次交互之间由一个 EOS 分隔。为避免推理时出现双 BOS token，你应当只调用 `tokenizer.encode(..., add_special_tokens = False)` 因为聊天模板也会自动添加一个 BOS token。\
对于 llama.cpp / GGUF 推理，你应该跳过 BOS，因为它会自动添加：

```
<｜User｜>What is 1+1?<｜Assistant｜>
```

其中 `<think>` 和 `</think>` token 都有各自指定的 token。

## 模型上传

**我们所有的上传内容** - 包括那些不是基于 imatrix 或 dynamic 的版本，都使用了我们的校准数据集，该数据集专门针对对话、编程和语言任务进行了优化。

* Qwen3（8B）蒸馏版： [DeepSeek-R1-0528-Qwen3-8B-GGUF](https://huggingface.co/unsloth/DeepSeek-R1-0528-Qwen3-8B-GGUF)
* 下面是完整的 DeepSeek-R1-0528 模型上传内容：

我们还上传了 [IQ4\_NL](https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/IQ4_NL) 和 [Q4\_1](https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/Q4_1) 量化版本，它们分别在 ARM 和 Apple 设备上运行得更快。

<table data-full-width="false"><thead><tr><th>MoE 位数</th><th>类型 + 链接</th><th>磁盘大小</th><th>详情</th></tr></thead><tbody><tr><td>1.66 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF?show_file_info=DeepSeek-R1-0528-UD-TQ1_0.gguf">TQ1_0</a></td><td><strong>162GB</strong></td><td>1.92/1.56 位</td></tr><tr><td>1.78 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/UD-IQ1_S">IQ1_S</a></td><td><strong>185GB</strong></td><td>2.06/1.56 位</td></tr><tr><td>1.93 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/UD-IQ1_M">IQ1_M</a></td><td><strong>200GB</strong></td><td>2.5/2.06/1.56</td></tr><tr><td>2.42 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/UD-IQ2_XXS">IQ2_XXS</a></td><td><strong>216GB</strong></td><td>2.5/2.06 位</td></tr><tr><td>2.71 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/UD-Q2_K_XL">Q2_K_XL</a></td><td><strong>251GB</strong></td><td>3.5/2.5 位</td></tr><tr><td>3.12 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/UD-IQ3_XXS">IQ3_XXS</a></td><td><strong>273GB</strong></td><td>3.5/2.06 位</td></tr><tr><td>3.5 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/UD-Q3_K_XL">Q3_K_XL</a></td><td><strong>296GB</strong></td><td>4.5/3.5 位</td></tr><tr><td>4.5 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/UD-Q4_K_XL">Q4_K_XL</a></td><td><strong>384GB</strong></td><td>5.5/4.5 位</td></tr><tr><td>5.5 位</td><td><a href="https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF/tree/main/UD-Q5_K_XL">Q5_K_XL</a></td><td><strong>481GB</strong></td><td>6.5/5.5 位</td></tr></tbody></table>

我们还上传了 [BF16 格式](https://huggingface.co/unsloth/DeepSeek-R1-0528-BF16)，以及原始的 [FP8（float8）格式](https://huggingface.co/unsloth/DeepSeek-R1-0528).

## 运行 DeepSeek-R1-0528 教程：

### :llama: 在 Ollama/Open WebUI 中运行

1. 安装 `ollama` 如果你还没安装！你只能运行最大到 32B 大小的模型。要运行完整的 720GB R1-0528 模型， [请看这里](#run-full-r1-0528-on-ollama-open-webui).

```bash
apt-get update
apt-get install pciutils -y
curl -fsSL https://ollama.com/install.sh | sh
```

2. 运行模型！注意如果失败了，你可以在另一个终端中调用 `ollama serve`！我们在 Hugging Face 上传中的 `params` 里包含了我们所有的修复和建议参数（temperature 等）！

```bash
ollama run hf.co/unsloth/DeepSeek-R1-0528-Qwen3-8B-GGUF:Q4_K_XL
```

3. <mark style="color:绿色;background-color:yellow;">**（新）要在 Ollama 中运行完整的 R1-0528 模型，你可以使用我们的 TQ1\_0（162GB 量化）：**</mark>

```bash
OLLAMA_MODELS=unsloth_downloaded_models ollama serve &

ollama run hf.co/unsloth/DeepSeek-R1-0528-GGUF:TQ1_0
```

### :llama: 在 Ollama/Open WebUI 上运行完整 R1-0528

Open WebUI 已经制作了一个关于如何在这里运行 R1 的分步教程，而对于 R1-0528，你只需要将其中的 R1 替换为新的 0528 量化版本： <https://docs.openwebui.com/tutorials/integrations/llm-providers/deepseekr1-dynamic>

<mark style="background-color:green;">**（新）要在 Ollama 中运行完整的 R1-0528 模型，你可以使用我们的 TQ1\_0（162GB 量化）：**</mark>

```bash
OLLAMA_MODELS=unsloth_downloaded_models ollama serve &

ollama run hf.co/unsloth/DeepSeek-R1-0528-GGUF:TQ1_0
```

如果你想在 Ollama 上使用任何比 TQ1\_0（162GB）更大的量化版本，你需要先像下面的代码那样将 3 个 GGUF 分片文件合并成 1 个。然后你需要在本地运行该模型。

```bash
./llama.cpp/llama-gguf-split --merge \
  DeepSeek-R1-0528-GGUF/DeepSeek-R1-0528-UD-IQ1_S/DeepSeek-R1-0528-UD-IQ1_S-00001-of-00003.gguf \
	merged_file.gguf
```

### ✨ 在 llama.cpp 中运行 Qwen3 蒸馏版 R1

1. <mark style="background-color:yellow;">**要运行完整的 720GB R1-0528 模型，**</mark> [<mark style="background-color:yellow;">**请看这里**</mark>](#run-full-r1-0528-on-llama.cpp)<mark style="background-color:yellow;">**.**</mark> 获取最新的 `llama.cpp` 在 [GitHub 这里](https://github.com/ggml-org/llama.cpp)。你也可以按照下面的构建说明来做。如果你没有 GPU 或只想用 CPU 推理，请将 `-DGGML_CUDA=ON` 改为 `-DGGML_CUDA=OFF` 。 **对于 Apple Mac / Metal 设备**，设置 `-DGGML_CUDA=OFF` 然后像平常一样继续——Metal 支持默认启用。

```bash
apt-get update
apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev -y
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build \
    -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON -DLLAMA_CURL=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-cli llama-gguf-split
cp llama.cpp/build/bin/llama-* llama.cpp
```

2. 然后直接使用 llama.cpp 下载模型：

```bash
./llama.cpp/llama-cli -hf unsloth/DeepSeek-R1-0528-Qwen3-8B-GGUF:Q4_K_XL --jinja
```

### ✨ 在 llama.cpp 上运行完整 R1-0528

1. 获取最新的 `llama.cpp` 在 [GitHub 这里](https://github.com/ggml-org/llama.cpp)。你也可以按照下面的构建说明来做。如果你没有 GPU 或只想用 CPU 推理，请将 `-DGGML_CUDA=ON` 改为 `-DGGML_CUDA=OFF` 。 **对于 Apple Mac / Metal 设备**，设置 `-DGGML_CUDA=OFF` 然后像平常一样继续——Metal 支持默认启用。

```bash
apt-get update
apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev -y
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build \
    -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON -DLLAMA_CURL=ON
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-quantize llama-cli llama-gguf-split llama-mtmd-cli
cp llama.cpp/build/bin/llama-* llama.cpp
```

2. 如果你想使用 `llama.cpp` 来直接加载模型，你可以按下面这样做：(:IQ1\_S) 是量化类型。你也可以通过 Hugging Face 下载（第 3 点）。这类似于 `ollama run` 。使用 `export LLAMA_CACHE="folder"` 来强制 `llama.cpp` 保存到特定位置。

{% hint style="success" %}
请试试 `-ot ".ffn_.*_exps.=CPU"` 将所有 MoE 层卸载到 CPU！这样可以有效地让你把所有非 MoE 层放在 1 张 GPU 上，从而提高生成速度。如果你有更多 GPU 容量，也可以自定义这个正则表达式以适配更多层。

如果你有更多一点的 GPU 显存，试试 `-ot ".ffn_(up|down)_exps.=CPU"` 这会卸载 up 和 down projection 的 MoE 层。

试试 `-ot ".ffn_(up)_exps.=CPU"` 如果你有更多的 GPU 显存。这会只卸载 up projection 的 MoE 层。

最后，通过以下方式卸载所有层 `-ot ".ffn_.*_exps.=CPU"` 这会使用最少的 VRAM。

你也可以自定义这个正则表达式，例如 `-ot "\.(6|7|8|9|[0-9][0-9]|[0-9][0-9][0-9])\.ffn_(gate|up|down)_exps.=CPU"` 表示卸载 gate、up 和 down 的 MoE 层，但只从第 6 层开始。
{% endhint %}

```bash
export LLAMA_CACHE="unsloth/DeepSeek-R1-0528-GGUF"
./llama.cpp/llama-cli \
    -hf unsloth/DeepSeek-R1-0528-GGUF:IQ1_S \
    --cache-type-k q4_0 \
    --threads -1 \
    --n-gpu-layers 99 \
    --prio 3 \
    --temp 0.6 \
    --top-p 0.95 \
    --min-p 0.01 \
    --ctx-size 16384 \
    --seed 3407 \
    -ot ".ffn_.*_exps.=CPU"
```

3. 通过以下方式下载模型（安装 `pip install huggingface_hub hf_transfer` 之后）。你可以选择 `UD-IQ1_S`（动态 1.78 位量化）或其他量化版本，例如 `Q4_K_M` 。我们 <mark style="background-color:green;">**推荐使用我们的 2.7 位动态量化**</mark><mark style="background-color:green;">**&#x20;**</mark><mark style="background-color:green;">**`UD-Q2_K_XL`**</mark><mark style="background-color:green;">**&#x20;**</mark><mark style="background-color:green;">**来平衡体积与精度**</mark>。更多版本见： <https://huggingface.co/unsloth/DeepSeek-R1-0528-GGUF>

{% code overflow="wrap" %}

```python
# !pip install huggingface_hub hf_transfer
import os
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "0" # 有时会触发速率限制，因此设为 0 以禁用
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id = "unsloth/DeepSeek-R1-0528-GGUF",
    local_dir = "unsloth/DeepSeek-R1-0528-GGUF",
    allow_patterns = ["*UD-IQ1_S*"], # 动态 1bit（168GB）动态 2bit（251GB）请使用 "*UD-Q2_K_XL*"
)
```

{% endcode %}

4. 按照我们在 DeepSeek R1 的 1.58 位动态量化中描述的方式，运行 Unsloth 的 Flappy Bird 测试。
5. 编辑 `--threads 32` 用于设置 CPU 线程数， `--ctx-size 16384` 用于设置上下文长度， `--n-gpu-layers 2` 用于设置 GPU 卸载多少层。如果你的 GPU 显存不足，请尝试调整它。如果你只进行 CPU 推理，也请移除它。

{% code overflow="wrap" %}

```bash
./llama.cpp/llama-cli \
    --model unsloth/DeepSeek-R1-0528-GGUF/UD-IQ1_S/DeepSeek-R1-0528-UD-IQ1_S-00001-of-00004.gguf \
    --cache-type-k q4_0 \
    --threads -1 \
    --n-gpu-layers 99 \
    --prio 3 \
    --temp 0.6 \
    --top-p 0.95 \
    --min-p 0.01 \
    --ctx-size 16384 \
    --seed 3407 \
    -ot ".ffn_.*_exps.=CPU" \
    -no-cnv \
    --prompt "<｜User｜>用 Python 创建一个 Flappy Bird 游戏。你必须包含以下内容：\n1. 你必须使用 pygame。\n2. 背景颜色应随机选择，并且是浅色调。初始使用浅蓝色。\n3. 多次按下空格键会让小鸟加速。\n4. 小鸟的形状应随机选择为正方形、圆形或三角形。颜色应随机选择为深色。\n5. 在底部放置一些陆地，颜色随机选择深棕色或黄色。\n6. 在右上角显示分数。通过水管且不撞到时分数增加。\n7. 随机生成间距合适的水管。其颜色随机为深绿色、浅棕色或深灰色。\n8. 当你失败时，显示最高分。文本应显示在屏幕内。按 q 或 Esc 退出游戏。重新开始则再次按空格键。\n最终游戏应放在一个 Python 的 markdown 代码块中。在最终的 markdown 代码块之前，请检查你的代码是否有错误并修复它们。<｜Assistant｜>"
```

{% endcode %}

## :8ball: 七边形测试

你也可以通过 [r/Localllama](https://www.reddit.com/r/LocalLLaMA/comments/1j7r47l/i_just_made_an_animation_of_a_ball_bouncing/) 来测试我们的动态量化，它会测试模型创建一个基础物理引擎，以模拟小球在一个移动的封闭七边形中旋转。

<figure><img src="/files/d38353ab2a5265a67c2480d370a152680663bb43" alt="" width="563"><figcaption><p>目标是让七边形旋转，并且七边形内的球也应该移动。</p></figcaption></figure>

<details>

<summary>运行模型的完整提示词</summary>

{% code overflow="wrap" %}

```bash
./llama.cpp/llama-cli \
    --model unsloth/DeepSeek-R1-0528-GGUF/UD-IQ1_S/DeepSeek-R1-0528-UD-IQ1_S-00001-of-00004.gguf \
    --cache-type-k q4_0 \
    --threads -1 \
    --n-gpu-layers 99 \
    --prio 3 \
    --temp 0.6 \
    --top_p 0.95 \
    --min_p 0.01 \
    --ctx-size 16384 \
    --seed 3407 \
    -ot ".ffn_.*_exps.=CPU" \
    -no-cnv \
    --prompt "<｜User｜>编写一个 Python 程序，显示 20 个球在一个旋转的七边形内弹跳：\n- 所有球具有相同半径。\n- 所有球上都有从 1 到 20 的编号。\n- 开始时所有球都从七边形中心落下。\n- 颜色为：#f8b862, #f6ad49, #f39800, #f08300, #ec6d51, #ee7948, #ed6d3d, #ec6800, #ec6800, #ee7800, #eb6238, #ea5506, #ea5506, #eb6101, #e49e61, #e45e32, #e17b34, #dd7a56, #db8449, #d66a35\n- 球应受到重力和摩擦力影响，并且必须以真实方式从旋转墙壁反弹。球与球之间也应有碰撞。\n- 所有球的材质决定了它们碰撞后的反弹高度不会超过七边形的半径，但要高于球的半径。\n- 所有球都会在摩擦作用下旋转，球上的数字可用于表示球的旋转。\n- 七边形围绕其中心旋转，旋转速度为每 5 秒 360 度。\n- 七边形尺寸应足够大，以容纳所有球。\n- 不要使用 pygame 库；请自行实现碰撞检测算法、碰撞响应等。允许使用以下 Python 库：tkinter、math、numpy、dataclasses、typing、sys。\n- 所有代码应放在一个单独的 Python 文件中。<｜Assistant｜>"
```

{% endcode %}

</details>

## 🦥 使用 Unsloth 微调 DeepSeek-R1-0528

要微调 **DeepSeek-R1-0528-Qwen3-8B** 并使用 Unsloth，我们制作了一个新的 GRPO notebook，其中包含一个自定义奖励函数，旨在显著增强多语言输出——特别是将所需语言响应的比例（在我们的示例中使用印尼语，但你可以使用任何语言）提高 40% 以上。

* [**DeepSeek-R1-0528-Qwen3-8B notebook**](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/DeepSeek_R1_0528_Qwen3_\(8B\)_GRPO.ipynb) **- 新**

虽然许多推理型 LLM 具备多语言能力，但它们往往会在推理轨迹中生成混合语言输出，将英语与目标语言混杂在一起。我们的奖励函数通过强力鼓励使用目标语言输出，有效缓解了这个问题，从而大幅提升了语言一致性。

这个奖励函数也完全可定制，你可以将其调整用于其他语言，或针对特定领域和用例进行微调。

{% hint style="success" %}
关于这个奖励函数和 notebook 最棒的一点是，你不需要语言数据集来强制模型学习某种特定语言。该 notebook 不包含印尼语数据集。
{% endhint %}

Unsloth 让 R1-Qwen3 蒸馏版微调速度提高 2×，显存占用减少 70%，并支持长达 8× 的上下文长度。


---

# 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 dynamically by asking a question.

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

```
GET https://unsloth.ai/docs/zh/mo-xing/tutorials/deepseek-r1-0528-how-to-run-locally.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
