# 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](https://unsloth.ai/docs/zh/ji-chu/unsloth-dynamic-2.0-ggufs) 用于 SOTA 的 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 内存（包括 Mac）和 Ollama 用户。试试： `ollama run hf.co/unsloth/DeepSeek-R1-0528-GGUF:TQ1_0`
{% endhint %}

## :gear: 推荐设置

对于 DeepSeek-R1-0528-Qwen3-8B，该模型几乎可以适配任何配置，甚至仅有约 20GB 内存的设备也能运行。无需事先做任何准备。\
\
然而，对于完整的 R1-0528 模型（大小为 715GB），您将需要额外的准备。1.78 位（IQ1\_S）量化可适配一块 24GB 的 GPU（所有层均被卸载）。如果您同时还有额外的 128GB 内存，使用此配置预计约为每秒 5 个 token。

建议至少拥有 64GB 内存来运行此量化（没有 GPU 时大约每秒 1 个 token）。要获得最佳性能，您至少需要 **180GB 统一内存或 180GB 组合的 RAM+VRAM** 以达到每秒 5+ 个 token。

我们建议使用我们的 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 令牌，你应该只调用 `tokenizer.encode(..., add_special_tokens = False)` 因为聊天模板会自动添加一个 BOS token。\
对于 llama.cpp / GGUF 推理，您应该跳过 BOS，因为它会自动添加：

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

最新的 `<think>` 和 `</think>` tokens 有它们各自指定的 token。

## 模型上传

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

* 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.66bit</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.56bit</td></tr><tr><td>1.78bit</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.56bit</td></tr><tr><td>1.93bit</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.42bit</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.06bit</td></tr><tr><td>2.71bit</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.5bit</td></tr><tr><td>3.12bit</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.06bit</td></tr><tr><td>3.5bit</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.5bit</td></tr><tr><td>4.5bit</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.5bit</td></tr><tr><td>5.5bit</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.5bit</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 上传中包含了所有修复和建议参数（如 temperature 等），位于 `params` 中！

```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 量化即可： [docs.openwebui.com/tutorials/integrations/deepseekr1-dynamic/](https://docs.openwebui.com/tutorials/integrations/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)。您也可以按照下面的构建说明进行。若 `-DGGML_CUDA=ON` 更改为 `-DGGML_CUDA=OFF` 如果您没有 GPU 或仅想要在 CPU 上进行推理。 **对于 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)。您也可以按照下面的构建说明进行。若 `-DGGML_CUDA=ON` 更改为 `-DGGML_CUDA=OFF` 如果您没有 GPU 或仅想要在 CPU 上进行推理。 **对于 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 层放在一块 GPU 上，从而提高生成速度。如果您有更多 GPU 容量，可以自定义正则表达式以卸载更多层。

如果你有稍多的 GPU 内存，尝试 `-ot ".ffn_(up|down)_exps.=CPU"` 这会卸载上投影和下投影的 MoE 层。

尝试 `-ot ".ffn_(up)_exps.=CPU"` 如果你有更多的 GPU 内存。这只会卸载上投影的 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"` 表示从第 6 层起卸载 gate、up 和 down 的 MoE 层。
{% 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.78bit 量化）或其他量化版本，例如 `Q4_K_M` 。我们 <mark style="background-color:green;">**建议使用我们的 2.7bit 动态量化**</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](https://huggingface.co/unsloth/DeepSeek-V3-0324-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.58bit 动态量化描述，运行 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. 多次按下 SPACE 将加速小鸟.\n4. 小鸟的形状应随机选择为方形、圆形或三角形。颜色应随机选择为深色.\n5. 在底部放置一些土地，颜色随机为深棕色或黄色.\n6. 在右上角显示分数。通过通过管道且未碰撞时增加分数.\n7. 随机间隔生成管道并保持足够的间距。管道颜色随机为深绿色、浅棕色或深灰色调.\n8. 当你失败时，显示最佳得分。将文本显示在屏幕内。按 q 或 Esc 将退出游戏。重新开始是再次按 SPACE.\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="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fgit-blob-1371de5a93c6c5b0e43e8bb51980d84554b199f4%2Fsnapshot.jpg?alt=media" 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 笔记本，包含一个自定义奖励函数，旨在显著增强多语种输出——特别是提高目标语言响应的比例（在我们的示例中使用印尼语，但您可以使用任何语言）超过 40%。

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

尽管许多推理类大型语言模型具有多语种能力，但它们在推理痕迹中常常产生混合语言输出，将英语与目标语言混合在一起。我们的奖励函数通过强烈鼓励以期望语言输出，有效缓解了这一问题，从而显著提高了语言一致性。

该奖励函数也完全可定制，允许您将其适配到其他语言或针对特定领域或用例进行微调。

{% hint style="success" %}
关于这个奖励函数和笔记本，最棒的部分是您并不需要语言数据集来强制模型学习特定语言。该笔记本没有使用印尼语数据集。
{% endhint %}

Unsloth 使 R1-Qwen3 蒸馏微调快 2 倍，使用 70% 更少的显存，并支持 8 倍更长的上下文长度。


---

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