> 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/qwen4.md). Agents can also search and read this documentation through its [MCP server](https://unsloth.ai/docs/~gitbook/mcp).

# Qwen4 35B-A3B：如何在本地运行

使用 Unsloth 在本地运行 Qwen4-35B-A3B 的指南。

**Qwen4-35B-A3B** 是 Qwen 推出的全新 35B（激活参数为 3B）MoE 模型，专为智能体编程和视觉任务打造。它具有 **1M 上下文** 窗口，是 **最强的** 50B 参数以下模型，在 **61.8** 上得分 **DeepSWE v1.1** 和 **21.2** 上得分 **Terminal-Bench 4.1**.

使用 Unsloth 在本地运行 Qwen4 [Dynamic v3.1 GGUF](https://unsloth.ai/docs/zh/ji-chu-zhi-shi/dynamic-3.0-ggufs.md) 在长上下文智能体基准测试中表现出色，同时使用 **更少的 token**。3-bit GGUF 可在 **18GB RAM**的设备上运行，并在 SSD 上使用 20GB 的 Engram。通过开源的 [Unsloth Desktop](https://github.com/unslothai/unsloth) 图形界面运行和训练 Qwen4。

{% columns %}
{% column width="50%" %} <a href="https://unsloth.ai/docs/zh/mo-xing/qwen4.md#run-qwen4-guide" class="button primary">Qwen4 运行指南</a><a href="https://unsloth.ai/download" class="button secondary">下载 Unsloth</a>

* [启用 MTP](https://unsloth.ai/docs/zh/mo-xing/mtp.md) 以加快推理
* Unsloth 支持 **MLX**、GGUF 和 **NVFP4**。Unsloth 会自动选择最佳 Engram 设置，以获得最佳性能
* 建议从 `UD-IQ4_XS` 在 **24GB** 设备上开始

Qwen4 Unsloth 量化版本： [**GGUF**](https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF) • [**MLX**](https://huggingface.co/unsloth/Qwen4-35B-A3B-MLX-4bit)

感谢 Qwen 在首日提供访问权限。
{% endcolumn %}

{% column width="50%" %}

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FLMvt9omb7163dDoHqkQP%2FSnowbound_10s.gif?alt=media&amp;token=67a6de43-a24c-4539-9264-3eac9d07dfd4" alt="" width="540"><figcaption><p>Unsloth Desktop 中的 Qwen4 35B GGUF</p></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

##### ⚙️ 使用指南

Qwen4 借鉴了 [Qwen3.8-Flash-Next](https://unsloth.ai/docs/zh/mo-xing/qwen3.8-next.md)的相同架构，将 Engram 存储在 SSD 上——4-bit 量化版本中约有 30% 是 Engram！ [**Unsloth**](#run-qwen4-in-unsloth-desktop) **会自动管理 Engram——** 如果内存足够，我们会自动将其移至 RAM（速度稍快）；否则，它会留在 SSD 上。&#x20;

#### **Qwen4-35B-A3B 系统要求**

建议从 **4-bit** `UD-IQ4_XS` 在配备 **24 GB** 统一内存的 Mac 上，或配备 **24 GB GPU** 的设备上，例如 NVIDIA GeForce RTX 4090 或 AMD Radeon RX 7900 XTX。

对于 **18 GB** 总内存，可尝试 **3 位** `UD-IQ3_XXS` ，并使用较短的上下文。在配备 **16 GB GPU** 的设备上（例如 RTX 5080 或 Radeon RX 9070 XT2），请使用 2 位或 3 位量化版本。

**表格：硬件要求** （RAM + VRAM，或统一内存）：

| 1 位      | 2 位      | 3 位      | 4 位      | 5 位      | 6 位      | 8 位   |
| -------- | -------- | -------- | -------- | -------- | -------- | ----- |
| 10-11 GB | 12-16 GB | 17-19 GB | 21-25 GB | 26-28 GB | 29-31 GB | 41 GB |

{% hint style="success" %}
所有量化版本均包含 MTP（可选）。另请参阅 [视觉效果](#vision) ，了解测试过哪些量化版本。
{% endhint %}

**推荐设置**

Qwen4-35B-A3B 是一款 **混合思考** 模型，不同模式使用不同的采样设置。编程和智能体任务请使用思考模式，快速回复则使用非思考模式。Unsloth 会自动设置所有这些参数，但你也可以自行更改。

**原生上下文长度：** 最多 `1,048,576` 个 token

| 参数                   |    思考模式 |   非思考模式 |
| -------------------- | ------: | ------: |
| `temperature`        |     1.0 |     0.7 |
| `top_p`              |    0.95 |    0.80 |
| `top_k`              |      20 |      20 |
| `min_p`              |     0.0 |     0.0 |
| `presence_penalty`   | 0.0（关闭） |     1.5 |
| `repetition_penalty` | 1.0（关闭） | 1.0（关闭） |

* 输出长度：Qwen 建议最多使用 `262,144` 个 token 用于推理， `131,072` 个 token 用于智能体任务的最终响应。

{% hint style="info" %}
使用 UD-IQ1\_S、UD-IQ1\_M 和 UD-IQ2\_XXS 等较小量化版本时，请设置 `min_p=0.01` 并使用 `presence_penalty=1.5` ——Unsloth 会自动完成此设置。我们发现较小的量化版本有时会陷入循环。
{% endhint %}

#### **思考**

Qwen 支持四种推理强度： `xhigh`, `high` （默认）、 `low`和 `off`。关闭思考时，请使用上述非思考采样设置。

{% columns %}
{% column width="50%" %}
**保留思考** 会在多个轮次之间保留先前的推理，让 Qwen4 能够继续执行多轮任务。

**Unsloth 为所有这些设置提供了开关**，无需手动配置请求即可轻松开启或关闭它们，
{% endcolumn %}

{% column width="50%" %}

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F0QGmlq5yqEqgtR394pzi%2Fimage.png?alt=media&amp;token=d71d7415-36f8-4728-bbc5-d3cfb7d7d25b" alt=""><figcaption></figcaption></figure>

{% endcolumn %}
{% endcolumns %}

### Qwen4 运行指南 <a href="#run-qwen4-guide" id="run-qwen4-guide"></a>

你可以在 [Unsloth Desktop](#run-qwen4-in-unsloth-desktop) 和 llama.cpp 中运行 Qwen4-35B-A3B。Unsloth 会自动加载所有参数和最佳硬件设置等，但你之后仍可进行更改。GGUF： [Qwen4-35B-A3B-GGUF](https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF)

<a href="https://unsloth.ai/docs/zh/mo-xing/qwen4.md#run-qwen4-in-unsloth-desktop" class="button primary">在 Unsloth Desktop 中运行</a><a href="https://unsloth.ai/docs/zh/mo-xing/qwen4.md#run-qwen4-in-llama-cpp" class="button secondary">在 llama.cpp 中运行</a>

#### 🦥 在 Unsloth Desktop 中运行 Qwen4 <a href="#run-qwen4-in-unsloth-desktop" id="run-qwen4-in-unsloth-desktop"></a>

Qwen4 可以在 Unsloth Desktop 中运行。这是一款开源本地 AI 应用，支持 **macOS、Windows 和 Linux**. **Unsloth 会自动配置 engram** ，还会 **将部分任务卸载到 RAM** ，并且 **能够检测多 GPU 配置。**

{% columns %}
{% column %}

* 搜索、下载， [运行 GGUF 模型](https://unsloth.ai/docs/zh/xin/studio.md#run-models-locally)、MLX、NVFP4 和 safetensor 模型
* [**自我修复** 工具调用](https://unsloth.ai/docs/zh/xin/studio/chat.md#auto-healing-tool-calling) + **网络搜索**
* [**代码执行**](https://unsloth.ai/docs/zh/desktop.md#code-execution) （Python、Bash）
* [自动推理](https://unsloth.ai/docs/desktop#feature-deep-dive) 参数调优（temp、top-p 等）
* [训练 LLM](https://unsloth.ai/docs/zh/xin/studio.md#no-code-training) 速度提升 2 倍，显存占用减少 70%
  {% endcolumn %}

{% column %}

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F1VHa8LHpRz3B3rPYFyS6%2Fqwen4%20unsloth%20another.gif?alt=media&amp;token=3e6993f8-8159-4cc8-9287-082cf7ac6609" alt="Unsloth Desktop interface"><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

{% stepper %}
{% step %}
**安装 Unsloth**

开始使用最简单的方法是下载 [Unsloth Desktop 应用](https://unsloth.ai/docs/zh/desktop.md)。该应用支持 [macOS](https://unsloth.ai/docs/zh/kuai-su-kai-shi/install/mac.md), [Windows](https://unsloth.ai/docs/zh/kuai-su-kai-shi/install/windows-installation.md)和 [Linux](https://unsloth.ai/docs/zh/kuai-su-kai-shi/install/linux.md).

<a href="https://unsloth.ai/download" class="button primary" data-icon="down-to-bracket">下载 Unsloth</a>

* <i class="fa-apple">:apple:</i> [下载 macOS 版](https://unsloth.ai/download/mac)
* <i class="fa-windows">:windows:</i> [下载 Windows 版](https://unsloth.ai/download/windows)
* <i class="fa-linux">:linux:</i> [下载 Linux 版](https://unsloth.ai/download/linux)

你也可以手动安装 Unsloth。

{% tabs %}
{% tab title="原生安装" %}
**macOS、Linux、WSL**

{% code collapsedlinecount="10" %}

```bash
curl -fsSL https://unsloth.ai/install.sh | sh
```

{% endcode %}

**Windows**

{% code collapsedlinecount="10" %}

```powershell
irm https://unsloth.ai/install.ps1 | iex
```

{% endcode %}
{% endtab %}

{% tab title="Docker" %}
可在 Docker 上获取 [Unsloth Docker 镜像](https://hub.docker.com/r/unsloth/unsloth) `unsloth/unsloth` 。 [阅读 Docker 指南。](https://unsloth.ai/docs/get-started/install/docker)
{% endtab %}

{% tab title="Homebrew" %}
安装 Unsloth Desktop。

{% code collapsedlinecount="10" %}

```bash
brew install --cask unsloth
```

{% endcode %}

更新 Unsloth Desktop。

{% code collapsedlinecount="10" %}

```bash
brew upgrade --cask unsloth
```

{% endcode %}
{% endtab %}

{% tab title="WinGet" %}
安装 Unsloth Desktop。

{% code collapsedlinecount="10" %}

```powershell
winget install --id UnslothAI.Unsloth -e
```

{% endcode %}

更新 Unsloth Desktop。

{% code collapsedlinecount="10" %}

```powershell
winget upgrade --id UnslothAI.Unsloth -e
```

{% endcode %}
{% endtab %}
{% endtabs %}
{% endstep %}

{% step %}
**搜索并下载 Qwen4**

前往 [Unsloth Chat](https://unsloth.ai/docs/zh/xin/studio/chat.md) 或模型中心，搜索 `unsloth/Qwen4-35B-A3B-GGUF`，然后从 [需求表](#qwen4-35b-a3b-requirements).
{% endstep %}

{% step %}
**运行 Qwen4**

在 **聊天**中选择 GGUF 文件。Unsloth 会自动选择存放 engram 的位置。在 Mac 上，你也可以 [使用纯文本 MLX 量化版本](#run-qwen4-on-mac-with-mlx).
{% endstep %}

{% step %}
**使用 Unsloth 的 API 或 Desktop 部署 Qwen4**

如果你需要 API 服务器，请使用 `unsloth run` 或者直接使用 Unsloth Desktop。

{% code overflow="wrap" collapsedlinecount="10" %}

```bash
unsloth run --model unsloth/Qwen4-35B-A3B-GGUF:UD-Q4_K_XL
```

{% endcode %}
{% endstep %}

{% step %}
**Unsloth 已准备就绪**

在本地与 Qwen4 聊天，或通过 `unsloth start` 启动。请参阅 [Unsloth 启动指南](https://unsloth.ai/docs/zh/ji-cheng/unsloth-start.md).

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F1BsX4t32qLOQKC736Vcu%2FAllFour_grid.gif?alt=media&amp;token=f4000742-8d4e-4d9b-9cc0-e66e8334b8da" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

{% hint style="success" %}
**当模型无法装入 VRAM 时：** Unsloth 会将 MoE 保存在 GPU 缓存中，速度可提升 2 倍。

| VRAM          | 解码时使用 `--moe-cache-mib` |  正常解码 |
| ------------- | ----------------------: | ----: |
| 24 GB         |   **131–138（速度提升 2 倍）** | 69-72 |
| 16 GB         |                     127 |    70 |
| 8 GB          |                      95 |    53 |
| {% endhint %} |                         |       |

#### 在 llama.cpp 中运行 Qwen4 <a href="#run-qwen4-in-llama-cpp" id="run-qwen4-in-llama-cpp"></a>

{% stepper %}
{% step %}
**安装 llama.cpp**

从 [ggml-org/llama.cpp#33626](https://github.com/ggml-org/llama.cpp/pull/33626) 开始，主线版本的 llama.cpp 已支持 Qwen4，因此请构建最新的 `master`版本。使用 `-DGGML_CUDA=OFF` 可仅使用 CPU 或 Apple Mac / Metal 设备（Metal 默认启用）。Windows 和其他 GPU 后端请参阅 [llama.cpp 构建指南](https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md) 。

{% code overflow="wrap" collapsedlinecount="10" %}

```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
cmake --build llama.cpp/build --config Release -j --clean-first --target llama-cli llama-mtmd-cli llama-server llama-gguf-split
cp llama.cpp/build/bin/llama-* llama.cpp
```

{% endcode %}
{% endstep %}

{% step %}
**运行 Qwen4**

`-hf` 会下载量化文件（两个文件）和视觉投影器。 `--load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU` 会按需从 SSD 读取 engram。

思考模式（默认）：

{% code collapsedlinecount="10" %}

```bash
./llama.cpp/llama-server \
    -hf unsloth/Qwen4-35B-A3B-GGUF:UD-Q4_K_XL \
    --load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU \
    --temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0
```

{% endcode %}

非思考模式：

{% code collapsedlinecount="10" %}

```bash
./llama.cpp/llama-server \
    -hf unsloth/Qwen4-35B-A3B-GGUF:UD-Q4_K_XL \
    --load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU \
    --reasoning off \
    --temp 0.7 --top-p 0.8 --top-k 20 --min-p 0.0 --presence-penalty 1.5
```

{% endcode %}

{% hint style="info" %}
使用 UD-IQ1\_S、UD-IQ1\_M 和 UD-IQ2\_XXS 等较小量化版本时，请设置 `min_p=0.01` 并使用 `presence_penalty=1.5` ——Unsloth 会自动完成此设置。我们发现较小的量化版本有时会陷入循环。
{% endhint %}
{% endstep %}

{% step %}
**视觉功能与手动下载**

视觉投影器提供以下文件： `mmproj-BF16.gguf`, `mmproj-F16.gguf` 和 `mmproj-F32.gguf`。低比特量化版本请参阅 [视觉效果](#vision) 。

{% code collapsedlinecount="10" %}

```bash
pip install -U huggingface_hub
hf download unsloth/Qwen4-35B-A3B-GGUF \
    --include "UD-Q4_K_XL/*" "mmproj-BF16.gguf" \
    --local-dir unsloth/Qwen4-35B-A3B-GGUF

./llama.cpp/llama-server \
    --model unsloth/Qwen4-35B-A3B-GGUF/UD-Q4_K_XL/Qwen4-35B-A3B-UD-Q4_K_XL-00001-of-00002.gguf \
    --mmproj unsloth/Qwen4-35B-A3B-GGUF/mmproj-BF16.gguf \
    --load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU \
    --temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0
```

{% endcode %}
{% endstep %}
{% endstepper %}

#### MTP 推测解码

我们发现 **MTP 可实现 1.3 倍更快的解码。** 在消费级设备上追求速度（`UD-Q4_K_XL`，贪心解码、关闭思考、解码速度（tokens/s），使用主线版 llama.cpp）：

| 设备                 |  无草稿（代码 / 散文） |        n-max 1 代码 |        n-max 1 散文 |    n-max 3 代码 |    n-max 3 散文 |
| ------------------ | ------------: | ----------------: | ----------------: | ------------: | ------------: |
| RTX 5090（32 GB）    | 174.9 / 168.6 | 224.0 (**1.28x**) | 205.3 (**1.22x**) | 221.2 (1.27x) | 174.9 (1.04x) |
| Mac M5 Max（128 GB） |   91.2 / 91.2 | 118.5 (**1.30x**) | 111.1 (**1.22x**) | 118.8 (1.30x) |  95.9 (1.05x) |
| RTX Spark（128 GB）  |   62.8 / 62.8 |  81.3 (**1.29x**) |  75.1 (**1.20x**) |  84.9 (1.35x) |  65.8 (1.05x) |

**推荐将草稿深度 2 设为默认值** (`--spec-draft-n-max 2`)——我们在 Unsloth 中会自动设置该值。

* **A100：** 深度为 2 时为 216–219 tokens/s，深度为 1 时为 195–196；在 8K 上下文下分别为 188 和 175。
* **Mac M5 Max：** 代码 **1.40x** 深度为 2 时为 1.40 倍，深度为 1 时为 1.33 倍；散文分别为 1.21 倍和 1.24 倍。
* 深度 3 和 4 的速度都比深度 2 慢。

每个 Qwen4 GGUF 都包含 [MTP](https://unsloth.ai/docs/zh/mo-xing/mtp.md) 头，因此 **无需单独的 MTP 文件** 也不需要 `-md` 标志。

{% code collapsedlinecount="10" %}

```bash
./llama.cpp/llama-server \
    -hf unsloth/Qwen4-35B-A3B-GGUF:UD-Q4_K_XL \
    --spec-type draft-mtp --spec-draft-n-max 2 \
    --load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU \
    --temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0
```

{% endcode %}

MTP 头是一个额外层，与模型共享 token 嵌入和输出层：

| 量化版本                     | MTP 头精度 |    头部大小 |
| ------------------------ | ------- | ------: |
| UD-IQ1\_S 至 UD-Q3\_K\_XL | Q4\_K   | 0.49 GB |
| UD-IQ4 和 UD-Q4 量化版本      | Q5\_K   | 0.59 GB |
| UD-Q5 量化版本               | Q6\_K   | 0.71 GB |
| UD-Q6\_K、UD-Q6\_K\_XL    | Q8\_0   | 0.92 GB |
| UD-Q8\_K\_XL             | BF16    | 1.72 GB |

接受率方面，使用 `--spec-draft-n-max 1`时约为 0.89，设为 2 时为 0.68，设为 3 时为 0.48–0.51。

#### Engram 表交换

我们将 GGUF 文件列表中的最后一个文件设为 engram，而 [`engram/`](https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF/tree/main/engram) 文件夹中包含相同 engram 的其他精度版本，因此可以自由混搭任意量化版本！

<details>

<summary>点击展开完整的 engram 明细表：</summary>

| 文件                                      | 表类型     |       大小 | 相对于 BF16 的相对 RMSE | 与 UD-Q4\_K\_XL 的平均 KLD | 备注                                         |
| --------------------------------------- | ------- | -------: | ----------------: | ---------------------: | ------------------------------------------ |
| `Qwen4-35B-A3B-engram-BF16.gguf_file`   | BF16    | 40.96 GB |                 0 |                 0.0352 | 无损                                         |
| `Qwen4-35B-A3B-engram-Q8_0.gguf_file`   | Q8\_0   | 21.76 GB |            0.0053 |                 0.0352 | 与 UD-Q6\_K、UD-Q6\_K\_XL、UD-Q8\_K\_XL 中的表相同 |
| `Qwen4-35B-A3B-engram-Q5_1.gguf_file`   | Q5\_1   | 15.36 GB |            0.0377 |                 0.0358 | 与 UD-Q5 量化版本中的表相同                          |
| `Qwen4-35B-A3B-engram-IQ4_NL.gguf_file` | IQ4\_NL | 11.52 GB |            0.0758 |                 0.0375 | 与 UD-IQ1\_S 至 UD-Q4\_K\_XL 中的表相同           |

平均 KLD 使用与量化表相同的 11 个评估集。

</details>

![按下载大小和 engram 精度划分的平均 KLD](https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FGmGY50bHc5YAa0njDp9W%2Fqwen4-engram-kld-vs-size.png?alt=media)

我们还检查了各 engram 精度下的 KLD：在包含 32K token 文档的 WikiText 上，对比 IQ4\_NL、Q5\_1、Q8\_0 和 BF16 engram。Q8\_0 表将平均 KLD 降低了约 6%，在 `UD-Q4_K_XL` ，而 BF16 相较于 Q8\_0 没有任何提升。将 `UD-Q8_K_XL` 降至 IQ4\_NL 后，平均 KLD 增加了 33%。

#### **在 Unsloth 中切换 engram**

我们也支持直接在 Unsloth 中切换 engram！

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FM9TPVdcnEdkYOrYnP2Rz%2Fimage.png?alt=media&amp;token=16a6dc3d-f5d1-4303-aec2-ba03baa4a1cc" alt=""><figcaption></figcaption></figure>

##### **使用以下参数切换 engram： `--ple`**

在 llama.cpp 中，使用 `--ple`。然后下载所需的 engram，例如，为 UD-Q4\_K\_XL 量化版本下载 Q8\_0：

{% code expandable="true" collapsedlinecount="10" %}

```bash
hf download unsloth/Qwen4-35B-A3B-GGUF \
    --include "UD-Q4_K_XL/*" "engram/*Q8_0*" \
    --exclude "UD-Q4_K_XL/*-00002-of-00002.gguf" \
    --local-dir unsloth/Qwen4-35B-A3B-GGUF

./llama.cpp/llama-server \
    --model unsloth/Qwen4-35B-A3B-GGUF/UD-Q4_K_XL/Qwen4-35B-A3B-UD-Q4_K_XL-00001-of-00002.gguf \
    --ple Qwen4-35B-A3B-GGUF/engram/Qwen4-35B-A3B-engram-Q8_0.gguf_file \
    --load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU \
    --temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0
```

{% endcode %}

### MLX - 在 Mac 上运行

[**unsloth/Qwen4-35B-A3B-MLX-4bit**](https://huggingface.co/unsloth/Qwen4-35B-A3B-MLX-4bit) 是一个 **34.0 GB** MLX 4 位量化：路由专家和 engram 表采用 4 位（专家的下投影采用 5 位），其他所有层采用 8 位，MoE 路由器则保留 BF16。该模型仅支持文本（不含视觉编码器和 MTP 头）。

{% code expandable="true" collapsedlinecount="10" %}

```python
from mlx_qwen4_moe import patch_qwen4_moe
patch_qwen4_moe()

from mlx_lm import load, generate
from mlx_lm.sample_utils import make_sampler

model, tokenizer = load("unsloth/Qwen4-35B-A3B-MLX-4bit")
messages = [{"role": "user", "content": "Give me a short introduction to large language models."}]
prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, enable_thinking=True)
sampler = make_sampler(temp=1.0, top_p=0.95, top_k=20, min_p=0.0)
print(generate(model, tokenizer, prompt=prompt, max_tokens=4096, sampler=sampler, verbose=True))
```

{% endcode %}

**MacBook Pro M5 Max 上的速度** （128 GB，mlx 0.32.5，生成 128 个 token）：

| 提示词长度 | 预填充（tokens/s） | 解码（tokens/s） |    峰值内存 |
| ----: | ------------: | -----------: | ------: |
|   512 |         2,711 |           75 | 35.0 GB |
| 4,096 |         3,977 |           69 | 36.2 GB |

#### MLX 量化分析

<img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FRalqHdzHJFl6NUhMxjiR%2Fqwen4-mlx-kld-vs-gguf-r2.png?alt=media" alt="Unsloth MLX 4 位与标准 MLX 4 位的平均 KLD 与模型大小对比" width="563">

我们将 Unsloth 的 MLX Dynamic 4 位量化与标准的 `mlx_lm.convert -q` 转换结果进行比较。图中还展示了全部 21 个 Unsloth Dynamic GGUF，计算文件大小时不包含其 MTP 头。我们的动态 MLX 4 位量化的 **平均 KLD 低 87%** ，优于标准 `mlx_lm.convert -q` 4 位转换结果，且与我们的 UD3.1 GGUF 表现相当：

<details>

<summary>点击展开完整的 MLX 量化细分表：</summary>

| 模型                         | 格式   |      大小 |     平均 KLD | KLD 第 99 百分位 |  Top-1 一致率 | PPL 与 BF16 |
| -------------------------- | ---- | ------: | ---------: | -----------: | ---------: | ---------: |
| **Unsloth MLX 4 位**        | MLX  | 34.0 GB | **0.0432** |    **0.620** | **92.80%** | **+3.94%** |
| 标准 `mlx_lm.convert -q` 4 位 | MLX  | 31.3 GB |     0.3356 |        3.316 |     79.44% |    +21.04% |
| GGUF UD-IQ4\_NL（参考）        | GGUF | 32.5 GB |     0.0474 |        0.689 |     92.34% |     +3.83% |
| GGUF UD-Q4\_K\_S（参考）       | GGUF | 33.6 GB |     0.0429 |        0.632 |     92.72% |     +3.56% |
| GGUF UD-Q4\_K\_M（参考）       | GGUF | 34.6 GB |     0.0387 |        0.574 |     93.20% |     +3.59% |
| GGUF UD-Q4\_K\_XL（参考）      | GGUF | 35.6 GB |     0.0375 |        0.543 |     93.20% |     +3.49% |

</details>

### 量化基准测试

Unsloth 动态量化 `UD-Q4_K_XL` 在 GSM8K、MATH-500、CRUXEval-O 和 MMLU-Pro 上的得分为 95.0%，而 BF16 为 95.3%。我们测试了 300 道题，每题采样四次，并启用了思考功能。

{% columns %}
{% column width="50%" %}
![总体准确率与 GGUF 下载大小的关系](https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FEx09H39PyyLNC6ESVJ6Z%2Fqwen4-accuracy-vs-size.png?alt=media)
{% endcolumn %}

{% column width="50%" %}
![各基准测试的准确率与 GGUF 下载大小的关系](https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FZNbwSNvnYVZngXWAGWRu%2Fqwen4-accuracy-per-benchmark-vs-size.png?alt=media)
{% endcolumn %}
{% endcolumns %}

对于 Top-1% 和 KLD，我们也对所有量化版本进行了大量基准测试。

<img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FxywviZDEPXkNThXieoQG%2Fqwen4-top1-vs-size.png?alt=media" alt="Top-1 一致率与 GGUF 下载大小的关系" width="563">

| 量化版本         |   准确率 | Top-1 一致率 | 平均 KLD | 预填充（tok/s） | 解码（tok/s） |
| ------------ | ----: | --------: | -----: | ---------: | --------: |
| `UD-IQ1_S`   | 89.6% |    80.10% | 0.3008 |      6,887 |     168.2 |
| `UD-IQ2_XS`  | 93.8% |    85.42% | 0.1616 |      6,647 |     162.4 |
| `UD-Q3_K_XL` | 94.0% |    91.39% | 0.0569 |      6,606 |     170.6 |
| `UD-IQ4_XS`  | 95.2% |    91.83% | 0.0532 |      6,148 |     172.3 |
| `UD-Q4_K_XL` | 95.0% |    93.20% | 0.0375 |      7,533 |     167.3 |
| `UD-Q6_K_XL` | 94.9% |    94.52% | 0.0247 |      7,119 |     167.9 |
| `UD-Q8_K_XL` | 94.8% |    95.49% | 0.0171 |      7,727 |     152.5 |

#### 量化分析

我们计算了以下数据集的平均 KLD： **11 个评估集** （维基百科、聊天、代码、数学、智能体工具使用、中文、日语、法语、混合网页文本、多语言聊天和 32K 令牌文档）。

![平均 KLD 与 GGUF 下载大小的关系](https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FXZGhV47EU6H5cWpgYtID%2Fqwen4-kld-vs-size.png?alt=media) ![Top-1 一致率与 GGUF 下载大小的关系](https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FxywviZDEPXkNThXieoQG%2Fqwen4-top1-vs-size.png?alt=media)

![不含 engram 时的平均 KLD 与 GGUF 大小的关系](https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FD61HFcbMnjadLQDftcmt%2Fqwen4-kld-vs-size-no-engram.png?alt=media) ![不含 engram 时的 Top-1 一致率与 GGUF 大小的关系](https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FkqeDrc96PPtGTo6pMQdJ%2Fqwen4-top1-vs-size-no-engram.png?alt=media)

最后两张图展示了不含 engram 时的 KLD 和 Top-1%（因为它本来就在 SSD 上）。速度通过 llama-bench 测得（`-fa on`，engram 在 CPU 上运行，重复 5 次），使用一台 NVIDIA B200

<details>

<summary>点击查看完整的量化表</summary>

| 量化版本         | Engram 表          | MoE 总大小 |    下载大小 | 磁盘上启用 engram 的权重 | 平均 KLD | Top-1 一致率 | 准确率（相较于 BF16） | pp4096（t/s） | tg128（t/s） |
| ------------ | ----------------- | ------: | ------: | ---------------: | -----: | --------: | ------------: | ----------: | ---------: |
| UD-IQ1\_S    | IQ4\_NL / 11.5 GB |  7.6 GB | 21.3 GB |           9.8 GB | 0.3008 |    80.10% |  89.6% (-5.8) |        6887 |      168.2 |
| UD-IQ1\_M    | IQ4\_NL / 11.5 GB |  8.6 GB | 22.3 GB |          10.8 GB | 0.2458 |    81.99% |  93.4% (-1.9) |        6697 |      165.9 |
| UD-IQ2\_XXS  | IQ4\_NL / 11.5 GB |  9.5 GB | 23.4 GB |          11.9 GB | 0.1935 |    84.00% |  93.6% (-1.8) |        6942 |      165.0 |
| UD-IQ2\_XS   | IQ4\_NL / 11.5 GB | 10.4 GB | 24.5 GB |          13.0 GB | 0.1616 |    85.42% |  93.8% (-1.6) |        6647 |      162.4 |
| UD-IQ2\_S    | IQ4\_NL / 11.5 GB | 11.6 GB | 25.5 GB |          14.0 GB | 0.1312 |    86.80% |  94.7% (-0.7) |        6431 |      157.3 |
| UD-IQ2\_M    | IQ4\_NL / 11.5 GB | 12.6 GB | 26.6 GB |          15.1 GB | 0.1084 |    88.00% |  94.3% (-1.0) |        6511 |      156.9 |
| UD-Q2\_K\_XL | IQ4\_NL / 11.5 GB | 13.4 GB | 27.7 GB |          16.2 GB | 0.0928 |    88.98% |  94.8% (-0.6) |        6588 |      159.0 |
| UD-IQ3\_XXS  | IQ4\_NL / 11.5 GB | 14.4 GB | 28.7 GB |          17.2 GB | 0.0760 |    89.96% |  94.8% (-0.5) |        6881 |      163.1 |
| UD-IQ3\_S    | IQ4\_NL / 11.5 GB | 15.7 GB | 29.8 GB |          18.3 GB | 0.0664 |    90.66% |  94.7% (-0.7) |        6561 |      168.0 |
| UD-Q3\_K\_XL | IQ4\_NL / 11.5 GB | 16.4 GB | 30.9 GB |          19.4 GB | 0.0569 |    91.39% |  94.0% (-1.3) |        6606 |      170.6 |
| UD-IQ4\_XS   | IQ4\_NL / 11.5 GB | 17.7 GB | 32.0 GB |          20.5 GB | 0.0532 |    91.83% |  95.2% (-0.1) |        6148 |      172.3 |
| UD-IQ4\_NL   | IQ4\_NL / 11.5 GB | 18.5 GB | 33.1 GB |          21.6 GB | 0.0474 |    92.34% |  95.0% (-0.3) |        7075 |      171.8 |
| UD-Q4\_K\_S  | IQ4\_NL / 11.5 GB | 19.5 GB | 34.2 GB |          22.7 GB | 0.0429 |    92.72% |  94.5% (-0.8) |        6159 |      171.4 |
| UD-Q4\_K\_M  | IQ4\_NL / 11.5 GB | 20.4 GB | 35.2 GB |          23.7 GB | 0.0387 |    93.20% |  94.4% (-0.9) |        7450 |      169.7 |
| UD-Q4\_K\_XL | IQ4\_NL / 11.5 GB | 21.6 GB | 36.2 GB |          24.7 GB | 0.0375 |    93.20% |  95.0% (-0.3) |        7533 |      167.3 |
| UD-Q5\_K\_S  | Q5\_1 / 15.4 GB   | 22.3 GB | 41.3 GB |          25.9 GB | 0.0319 |    93.78% |   95.3% (0.0) |        6503 |      168.7 |
| UD-Q5\_K\_M  | Q5\_1 / 15.4 GB   | 23.8 GB | 42.4 GB |          27.0 GB | 0.0292 |    94.00% |  95.2% (-0.2) |        6849 |      169.1 |
| UD-Q5\_K\_XL | Q5\_1 / 15.4 GB   | 24.4 GB | 43.2 GB |          27.8 GB | 0.0278 |    94.20% |  95.0% (-0.3) |        6698 |      168.7 |
| UD-Q6\_K     | Q8\_0 / 21.8 GB   | 25.5 GB | 50.9 GB |          29.2 GB | 0.0259 |    94.35% |   95.3% (0.0) |        6816 |      167.0 |
| UD-Q6\_K\_XL | Q8\_0 / 21.8 GB   | 26.6 GB | 52.2 GB |          30.5 GB | 0.0247 |    94.52% |  94.9% (-0.4) |        7119 |      167.9 |
| UD-Q8\_K\_XL | Q8\_0 / 21.8 GB   | 34.6 GB | 62.3 GB |          40.5 GB | 0.0171 |    95.49% |  94.8% (-0.5) |        7727 |      152.5 |

</details>

#### 工具调用与长上下文基准测试

**工具调用** 测试在 A100 80GB 上使用 BFCL v3 AST（200 个项目 × 2 个样本，开启思考，采用官方采样方法）运行，并与 `UD-Q8_K_XL` 按项目配对（95% bootstrap 置信区间）进行比较。

<details>

<summary>点击查看完整的工具调用与长上下文分析表</summary>

| 量化版本         |    得分 | 相较于 UD-Q8\_K\_XL（分） | 95% 置信区间      |
| ------------ | ----: | ------------------: | ------------- |
| UD-IQ1\_S    | 0.818 |            **-5.2** | \[-9.2, -1.8] |
| UD-IQ1\_M    | 0.840 |                -3.0 | \[-6.5, +0.2] |
| UD-IQ2\_XXS  | 0.860 |                -1.0 | \[-4.0, +2.0] |
| UD-IQ2\_XS   | 0.875 |                +0.5 | \[-2.5, +3.8] |
| UD-IQ2\_S    | 0.873 |                +0.2 | \[-2.2, +2.8] |
| UD-IQ2\_M    | 0.868 |                -0.2 | \[-3.0, +2.5] |
| UD-Q2\_K\_XL | 0.873 |                +0.2 | \[-2.5, +2.8] |
| UD-IQ3\_XXS  | 0.868 |                -0.2 | \[-2.5, +1.8] |
| UD-IQ3\_S    | 0.863 |                -0.8 | \[-3.0, +1.5] |
| UD-Q3\_K\_XL | 0.868 |                -0.2 | \[-3.2, +2.8] |
| UD-IQ4\_XS   | 0.868 |                -0.2 | \[-3.0, +2.2] |
| UD-IQ4\_NL   | 0.885 |                +1.5 | \[-0.8, +4.0] |
| UD-Q4\_K\_XL | 0.863 |                -0.8 | \[-4.0, +2.3] |
| UD-Q8\_K\_XL | 0.870 |                 参考值 |               |

</details>

**长上下文** 通过一组类似 RULER 的测试进行评估（检索针、变量追踪，关闭思考，贪心解码），上下文长度为 32K、64K 和 128K。 `UD-Q8_K_XL` 在这三个长度下的得分均为 1.000。

`UD-Q3_K_XL`, `UD-IQ4_XS` 和 `UD-Q4_K_XL` 在三个长度下的得分也均为 1.000，而在 128K 下测试的其他所有量化版本得分均为 0.980 至 1.000。只有 `UD-IQ1_S` 在合并各长度的数据后明显较低（0.983，低 1.7 分［-3.9、-0.1］）

#### 视觉基准测试

我们在 A100 80GB 上测试了 300 个样本（100 个 ChartQA 宽松准确率、100 个 DocVQA ANLS、100 个 TextVQA VQA 准确率），采用贪心解码并关闭思考模式，使用 `mmproj-BF16`：

<details>

<summary>点击查看完整的视觉分析表</summary>

| 量化版本         |    得分 | ChartQA | DocVQA | TextVQA | 相较于 UD-Q8\_K\_XL（分） | 95% 置信区间      |
| ------------ | ----: | ------: | -----: | ------: | ------------------: | ------------- |
| UD-Q4\_K\_M  | 0.880 |   0.840 |  0.953 |   0.848 |                +0.8 | \[-1.2, +2.8] |
| UD-Q4\_K\_XL | 0.880 |   0.850 |  0.942 |   0.848 |                +0.7 | \[-1.6, +3.1] |
| UD-Q5\_K\_S  | 0.877 |   0.830 |  0.952 |   0.848 |                +0.4 | \[-1.7, +2.5] |
| UD-Q5\_K\_M  | 0.888 |   0.840 |  0.968 |   0.855 |                +1.5 | \[-0.5, +3.7] |
| UD-Q5\_K\_XL | 0.868 |   0.830 |  0.939 |   0.835 |                -0.4 | \[-2.9, +2.0] |
| UD-Q6\_K     | 0.883 |   0.850 |  0.954 |   0.844 |                +1.0 | \[-1.1, +3.1] |
| UD-Q6\_K\_XL | 0.874 |   0.830 |  0.933 |   0.859 |                +0.2 | \[-1.9, +2.2] |
| UD-Q8\_K\_XL | 0.873 |   0.830 |  0.947 |   0.841 |                 参考值 |               |

</details>

#### 官方基准测试

这些是 Qwen 官方基准测试，与上文的 GGUF 量化测试分开。

<figure><img src="https://content.gitbook.com/content/xhOjnexMCB3dmuQFQ2Zq/blobs/L1Dq9mZ0L4mKS1U691oz/qwen4-35b-a3b-official-benchmarks.png" alt="" width="563"><figcaption><p>原始得分和评估说明见 <a href="https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF#benchmark-results">Qwen4 基准测试结果</a>.</p></figcaption></figure>

| 基准测试                | Qwen4-35B-A3B | Qwen3.8-Flash-Next | Qwen3.8-27B | Inkling2 Mini |
| ------------------- | ------------: | -----------------: | ----------: | ------------: |
| Terminal Bench 4.1  |      **21.2** |               24.2 |         5.6 |           5.6 |
| DeepSWE v1.1        |      **61.8** |               58.7 |        42.2 |          48.2 |
| FrontierCode-AA 2.0 |      **16.1** |               27.1 |        14.9 |          15.1 |
| NL2Repo             |      **51.7** |               48.1 |        42.3 |          41.8 |
| APEX Agents v1.1    |      **59.0** |               61.0 |        47.5 |          50.9 |

<details>

<summary>显示更多基准测试</summary>

| 基准测试                     | Qwen4-35B-A3B | Qwen3.8-Flash-Next | Qwen3.8-27B | Inkling2 Mini |
| ------------------------ | ------------: | -----------------: | ----------: | ------------: |
| Work360                  |      **42.9** |               43.7 |        37.1 |          30.4 |
| AA-Briefcase v1.1（Elo）   |      **1486** |               1567 |        1391 |          1263 |
| AutomationBench-AA       |      **57.9** |               55.9 |        48.2 |          41.8 |
| HealthBench Professional |      **47.4** |               34.8 |        29.8 |          45.7 |
| GPQA                     |      **86.9** |               91.7 |        89.2 |          86.9 |
| Chartography             |      **27.8** |               24.6 |        27.8 |          22.1 |
| VideoIndex               |      **52.9** |               51.4 |        51.9 |          44.0 |

</details>

##### 关于 Qwen4-35B-A3B

根据 Qwen 的模型卡：

* **架构：** 共 40 层，分为 10 个块（3 个 Gated DeltaNet 线性注意力层 + 1 个 Qwen 稀疏注意力层），每层之后均接 MoE。隐藏层维度：2,048；词表：248,320 个 token。
* **MoE：** 256 个专家，其中 8 个路由专家和 1 个共享专家处于激活状态；专家中间层维度为 512。
* **参数：** 总计 35B（激活参数 3B），另有 20B 的 n-gram 嵌入（第 2 层包含 10M 个二元/三元语法条目）和 2B 的多步 MTP 模块。
* **上下文与输入：** 原生上下文长度为 1,048,576 个 token；支持文本、图像和视频。
* **长视频：** Qwen 建议将 `longest_edge` 设为 `469762048` 在 `video_preprocessor_config.json` 以支持长达一小时的视频约 224K 个视频 token。


---

# 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/mo-xing/qwen4.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.
