> 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/ji-chu/nvfp4.md).

# 运行 Unsloth Dynamic NVFP4 指南

Unsloth Dynamic NVFP4 是一种量化模型格式，可在 NVIDIA Blackwell GPU 上运行，旨在实现更快、更准确的 4 位推理。它将 NVIDIA 原生的 NVFP4 精度与 [Unsloth Dynamic 2.0](/docs/zh/ji-chu/dynamic-3.0-ggufs.md) 量化结合起来，以在减少 VRAM 占用并提升速度的同时保留模型准确性。本指南将解释 FP4 量化，对比 NVFP4 与其他格式，并展示如何使用 [Qwen3.8](/docs/zh/mo-xing/qwen3.8.md), [Gemma 4](/docs/zh/mo-xing/gemma-4.md) 和 [Qwen3.6](/docs/zh/mo-xing/qwen3.6.md) 在本地使用 vLLM 或 SGLang 于 RTX 5050-5090、B200、RTX PRO 6000 以及更多 GPU 上运行。

Dynamic NVFP4 的工作方式是选择重要层保留为 FP8（W8A8）或 BF16，而其余层使用 W4A4（而不是 W4A16），而不是强制每一层都进入 FP4。这使得最高可达到 **2.5倍更快的推理** ，因为 W4A4 利用了 Blackwell GPU 的 FP4 Tensor Core。对于所有量化版本，我们还提供 FP8 KV cache 校准，从而实现 **2倍更长的上下文长度**.

{% hint style="success" %}
**8月14日：** [**Qwen3.8-27B**](/docs/zh/mo-xing/qwen3.8.md) **现已发布，并附带我们的 NVFP4 量化版本。**

**全部** [**Gemma 4**](#gemma-4) **模型现在都可作为 Unsloth Dynamic NVFP4 量化版本提供：** E2B、E4B、12B Unified、26B-A4B MoE 和 31B Dense。

探索 [Unsloth Dynamic NVFP4 集合](https://huggingface.co/collections/unsloth/nvfp4) 以查看我们所有的模型上传。
{% endhint %}

### Float4 与其他精度

更快 GPU 的诀窍在于 **降低矩阵乘法的数值精度**。矩阵乘法单元所需的晶体管数量与 **尾数平方**相关。尾数决定数字可以有多少“分数”小数位——因此位数越多，对小数的表示就越准确。例如，使用更多尾数位可以表示 0.121332，而尾数位很少时则会把它舍入为 0.1。

{% columns %}
{% column width="50%" %}
FP32 有 23 位尾数，因此需要 23^2 + 8 位指数 = 537 的空间。Bfloat16 有 7 位尾数，因此需要 7^2 + 8 位指数 = 57 的空间。这意味着 bfloat16 所需空间比 FP32 少约 9 倍！而当我们转到只有 3 位尾数的 float8 时，3^2 + 4 位指数 = 13——这比 FP32 少 41 倍！

最后，float4 只有 1 位尾数和 2 位指数，因此只需 3 个空间——比 FP32 足足少 179 倍——这本质上意味着一个 **GPU 在相同空间内可执行大约 179 倍更多的 FP4 矩阵乘法，而不是 FP32 乘法 FLOPs**!
{% endcolumn %}

{% column width="50%" %}
![](/files/465724340d038d0ba43e8fc743619fe6fcbf4efc)
{% endcolumn %}
{% endcolumns %}

### NVFP4 与 MXFP4

<div><figure><img src="/files/47022e867406f3cedc50a0d5083231d81acaab9e" alt=""><figcaption></figcaption></figure> <figure><img src="/files/aa01330c4a2591a03c3abc766a58ee226220aa75" alt=""><figcaption></figcaption></figure></div>

还有另一种叫 MXFP4 的 FP4 格式——由于两个原因，它比 NVFP4 不准确：

1. NVFP4 的块大小为 16，而 MXFP4 为 32——这使得异常值更容易被隔离，并且会为更小的权重子集提供缩放因子，从而提高准确性
2. 每个块使用的是 E4M3（FP8）缩放，而不是 E8M0（2 的幂缩放）。采用 FP8 类型的块大小看起来要好得多，尤其是对 LLM 而言。

### 性能分析

我们的新 动态 NVFP4 Qwen3.6 量化版本运行约**快 2.5 倍** 相比其他 NVFP4 量化版本，并且具有 **更好的性能** 且文件大小相当。运行 Qwen3.6-27B NVFP4 **快 2.5 倍** 在 **24GB 显存** 以及 Qwen3.6-35B-A3B **快 1.7 倍** 在 **32GB 显存**。我们还添加了 **FP8 KV cache 校准** ，以实现 2 倍更长的上下文长度！NVFP4 需要 NVIDIA 的 Blackwell GPU，例如 RTX 50X、DGX Spark（见 [#dgx-spark-with-nvfp4-quants](#dgx-spark-with-nvfp4-quants "mention")）

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

，B200、B300 GPU。对于较老的 GPU，我们的 GGUF 也表现良好！全部基准测试都使用 1x B200、128 并发。更高并发可将 35B 提升到 17,561 tokens/s。我们还刚刚发布了新的 [Qwen3.8](/docs/zh/mo-xing/qwen3.8.md) NVFP4 量化版本：

* [Qwen3.8-27B NVFP4](https://huggingface.co/unsloth/Qwen3.8-27B-NVFP4) （新）

我们还发布了两个 35B-A3B NVFP4 版本：

* [Qwen3.6-35B-A3B-NVFP4-Fast](https://huggingface.co/unsloth/Qwen3.6-35B-A3B-NVFP4-Fast) 这是一个完整的 W4A4 量化版本——快 1.79 倍
* [Qwen3.6-35B-A3B-NVFP4](https://huggingface.co/unsloth/Qwen3.6-35B-A3B-NVFP4) 它稍大一些，但更准确，并且快 1.56 倍

在准确性基准测试方面，我们对 FP8、BF16、NVIDIA 的 NVFP4 以及我们的 NVFP4s 进行了 MMLU-Pro、AIME 2025、GPQA 测试——结果显示我们更快的量化版本在所有项目上的表现都相近：

<figure><img src="/files/d7ee23f84bb6342b555e1ce4070a001d8a8fd327" alt=""><figcaption></figcaption></figure>

| Qwen3.8-27B（新）                                                            |
| ------------------------------------------------------------------------- |
| [Qwen3.8-27B NVFP4](https://huggingface.co/unsloth/Qwen3.8-27B-NVFP4) （新） |

<table><thead><tr><th width="372.5999755859375">Qwen3.6-35B-A3B</th><th>Qwen3.6-27B</th></tr></thead><tbody><tr><td><a href="https://huggingface.co/unsloth/Qwen3.6-35B-A3B-NVFP4">Qwen3.6-35B-A3B-NVFP4</a> （快 1.56 倍）</td><td><a href="https://huggingface.co/unsloth/Qwen3.6-27B-NVFP4">Qwen3.6-27B-NVFP4</a> （快 2.5 倍）</td></tr><tr><td><a href="https://huggingface.co/unsloth/Qwen3.6-35B-A3B-NVFP4-Fast">Qwen3.6-35B-A3B-NVFP4-Fast</a> （快 1.79 倍）</td><td></td></tr></tbody></table>

**MTP 张量也被直接构建进量化版本中，以进一步加速。** 准确性提升来自对 Qwen3.6 聊天模板和数据集校准的改进。我们使用此前对聊天模板的更新来提高代码与工具调用的一致性，同时减少循环和其他已报告问题。我们的校准结合了我们专门为代码、工具调用和聊天优化的数据集，以及 UltraChat。

在解码速度（tokens/s）方面，我们的 27B 快 1.03 倍，而 35B 分别快 1.17 倍和 1.22 倍。

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

### 概览

下面是可使用模型所需的硬件要求，包括 Gemma 4 和 Qwen3.6。也请查看你将获得的整体速度提升：

#### Gemma 4：

| Gemma 4 变体                                                         |  所需显存 | 比 BF16 更快 |
| ------------------------------------------------------------------ | ----: | --------: |
| [E2B](https://huggingface.co/unsloth/gemma-4-E2B-it-NVFP4)         |  7 GB |   快 1.12× |
| [E4B](https://huggingface.co/unsloth/gemma-4-E4B-it-NVFP4)         |  9 GB |   快 1.22× |
| [12B Unified](https://huggingface.co/unsloth/gemma-4-12b-it-NVFP4) | 11 GB |   快 1.26× |
| [26B A4B](https://huggingface.co/unsloth/gemma-4-26B-A4B-it-NVFP4) | 26 GB |   快 1.41× |
| [31B](https://huggingface.co/unsloth/gemma-4-31B-it-NVFP4)         | 32 GB |   快 1.45× |

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

#### Qwen3.6：

| Qwen3.6 变体                                                                |  所需显存 | 比其他 NVFP4 量化版本更快 |
| ------------------------------------------------------------------------- | ----: | ---------------: |
| [27B](https://huggingface.co/unsloth/Qwen3.6-27B-NVFP4)                   | 24 GB |          快 2.5 倍 |
| [35B A3B](https://huggingface.co/unsloth/Qwen3.6-35B-A3B-NVFP4)           | 32 GB |          快 1.56× |
| [35B A3B Fast](https://huggingface.co/unsloth/Qwen3.6-35B-A3B-NVFP4-Fast) | 32 GB |          快 1.79× |

### NVFP4 基准测试

NVFP4 直接在 Blackwell Tensor Core 上运行 4 位权重和矩阵乘法。我们的 Qwen3.6 NVFP4 量化版本使用 W4A4，因此它们实际上使用的是 FP4 Tensor Core，所以解码速度比使用 W4A16 的 NVIDIA 版本更快。我们还会动态量化各层以保留准确性，并对所有量化版本进行了 MMLU-Pro、AIME 2025、GPQA 测试，同时还与 FP8 和 BF16 做了比较。

**Qwen3.6-27B NVFP4 准确性基准测试**

| 提供方     | MMLU-Pro |  GPQA | AIME 2025 |
| ------- | -------: | ----: | --------: |
| Unsloth |    86.25 | 86.34 |     93.12 |
| NVIDIA  |    85.96 | 86.87 |     93.12 |
| FP8     |    86.11 | 86.87 |     93.75 |
| BF16    |    85.96 | 88.13 |     93.33 |

**Qwen3.6-35B-A3B NVFP4 准确性基准测试**

| 提供方             | MMLU-Pro |  GPQA | AIME 2025 |
| --------------- | -------: | ----: | --------: |
| Unsloth         |    85.85 | 86.74 |     92.29 |
| **Unsloth 快速版** |    85.58 | 87.75 |     91.67 |
| NVIDIA          |    85.60 | 87.12 |     91.88 |
| FP8             |    85.75 | 86.74 |     93.12 |
| BF16            |    85.75 | 86.36 |     92.50 |

我们还检查了所有基准测试的输出长度，结果是可比的，因此新的 NVFP4 量化版本并没有为了“想得更久”而抵消量化的意义！（即如果快 2 倍，但思考也多 2 倍，那就没用了）

<figure><img src="/files/783f967f83fe72b96f73ff318ab27ce4f63756d3" alt=""><figcaption></figcaption></figure>

## **运行 NVFP4 教程**

要运行 NVFP4 量化版本，请参见下方在 [vLLM](/docs/zh/ji-chu/inference-and-deployment/vllm-guide.md) 和 [SGLang](/docs/zh/ji-chu/inference-and-deployment/sglang-guide.md) 中运行 Qwen3.6-27B 的命令（你可以把模型名称改为 `Qwen3.6-35-A3B-NVFP4`).&#x20;

### **vLLM 教程**

你可以在 [vLLM](https://github.com/vllm-project/vllm)中运行所有 NVFP4 模型。不要选择任何 MoE 后端——让 vLLM 自动选择即可——例如 Marlin 会慢 2.5 倍！请参见 [#marlin-vs-flashinfer-vs-cutlass-vs-cute-dsl](#marlin-vs-flashinfer-vs-cutlass-vs-cute-dsl "mention")如果你有 DGX Spark，请参见 [#dgx-spark-serving](#dgx-spark-serving "mention") 你必须使用 `--moe-backend flashinfer_b12x` ，否则推理会慢很多。

要在单独的 venv 中安装 vLLM：

{% code overflow="wrap" expandable="true" %}

```bash
uv venv unsloth-nvfp4-env --python 3.13
source unsloth-nvfp4-env/bin/activate
uv pip install "vllm>=0.25.0" "flashinfer-python>=0.6.13" "nvidia-cutlass-dsl>=4.5.2" \\
    --torch-backend=auto
```

{% endcode %}

然后启动 35B Fast 版本：

```shell
vllm serve unsloth/Qwen3.6-35B-A3B-NVFP4-Fast
```

将 `unsloth/Qwen3.6-35B-A3B-NVFP4-Fast` 替换为 NVFP4 量化名称！

要启用 MTP / speculative decoding（解码更快，但吞吐会稍低），请使用：

```bash
vllm serve unsloth/Qwen3.6-35B-A3B-NVFP4-Fast
    --speculative-config '{"method": "mtp", "num_speculative_tokens": 2}'
```

如果你遇到 Torchcodec 问题，请务必先执行下面的步骤，然后重新启动 vllm。

{% code overflow="wrap" expandable="true" %}

```bash
sudo apt-get update
sudo apt-get install -y ffmpeg
```

{% endcode %}

### **DGX Spark 教程**

为确保 DGX Spark 使用正确的内核（否则你会得到 **快 2 倍的推理**），请先检查：

{% code overflow="wrap" expandable="true" %}

```bash
python -c "
import torch; from vllm.utils.flashinfer import has_flashinfer_b12x_gemm as g, has_flashinfer_b12x_moe as m
cap = torch.cuda.get_device_capability(); print('cap', cap, '| b12x gemm', g(), '| b12x moe', m()); assert cap[0] == 12 and g() and m(), 'b12x unavailable: serving would degrade to marlin W4A16'"
```

{% endcode %}

这不应该报错——如果报错了，请更新 vllm 或通过以下方式重新安装：

{% code overflow="wrap" expandable="true" %}

```bash
uv venv unsloth-nvfp4-env --python 3.13
source unsloth-nvfp4-env/bin/activate
uv pip install "vllm>=0.25.0" "flashinfer-python>=0.6.13" "nvidia-cutlass-dsl>=4.5.2" \\
    --torch-backend=auto
```

{% endcode %}

然后在 DGX Spark 上用 vLLM 提供服务：

{% code overflow="wrap" expandable="true" %}

```shellscript
export CUTE_DSL_ARCH=sm_121a
vllm serve unsloth/Qwen3.6-35B-A3B-NVFP4-Fast --moe-backend flashinfer_b12x
```

{% endcode %}

如果你遇到 Torchcodec 问题，请务必先执行下面的步骤，然后重新启动 vllm。

{% code overflow="wrap" expandable="true" %}

```bash
sudo apt-get update
sudo apt-get install -y ffmpeg
```

{% endcode %}

### **SGLang 教程：**

你可以在 [SGLang](https://github.com/sgl-project/sglang)。记得把模型名称替换成你想要的模型。

**Qwen3.6：**

```bash
python -m sglang.launch_server --model-path unsloth/Qwen3.6-27B-NVFP4 --speculative-algorithm NEXTN \\
     --speculative-num-steps 3 --speculative-eagle-topk 1 --speculative-num-draft-tokens 4
```

**Gemma 4：**

```bash
python -m sglang.launch_server --model-path unsloth/Gemma-4-31B-NVFP4 --speculative-algorithm NEXTN \\
     --speculative-num-steps 3 --speculative-eagle-topk 1 --speculative-num-draft-tokens 4
```

### Gemma 4 及其他

现在每个 Gemma 4 变体都有一个 Unsloth Dynamic NVFP4 检查点。

我们显示 Gemma-4 在 1x B200 上以 128 并发服务时，相比 BF16 的吞吐提升最高可达 1.44 倍。Qwen3.5-122B-A10B 快 1.38 倍，GLM-4.7-Flash 快 1.27 倍。

<figure><img src="/files/0ae75446ee05a7ece9e7c70605bf3764e7370a93" alt=""><figcaption></figcaption></figure>

### Marlin vs Flashinfer vs cutlass vs cute-DSL

我们还发现 Marlin kernel 对 W4A4 的支持并不好——启用它会导致 2.5 倍的性能下降——因此请使用 CUTLASS、Flashinfer-TRTLLM 或 Cute-DSL（在 vLLM 中自动启用）！如果你有 DGX Spark，也请参见 [#dgx-spark-serving](#dgx-spark-serving "mention") 你必须使用 `--moe-backend flashinfer_b12x` ，否则推理会慢 2.5 倍。

**所以不要设置任何后端——vLLM 会自动选择最佳方案。**

| 模型              | 方案    | 后端                 | 解码 tok/s | 吞吐 tok/s   |
| --------------- | ----- | ------------------ | -------- | ---------- |
| NVIDIA 27B      | W4A16 | marlin（自动）         | 115.6    | 2,403      |
| Unsloth 27B     | W4A4  | marlin             | 105.6    | 2,127      |
| Unsloth 27B     | W4A4  | cutlass            | 113.5    | 6,681      |
| Unsloth 27B     | W4A4  | flashinfer\_trtllm | 112.6    | 6,158      |
| Unsloth 27B     | W4A4  | **cute-DSL（自动）**   | 125.9    | **6,863**  |
| NVIDIA 35B-A3B  | W4A4  | marlin（自动）         | 240.8    | 8,721      |
| Unsloth 35B-A3B | W4A4  | marlin             | 215.8    | 8,619      |
| Unsloth 35B-A3B | W4A4  | cutlass            | 158.3    | 11,017     |
| Unsloth 35B-A3B | W4A4  | **cute-DSL（自动）**   | 295.2    | **15,636** |


---

# 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/ji-chu/nvfp4.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.
