前文介紹了
然后發現 DFlash 更猛:加速高達 6 倍 ?
同等輸出質量、完全無損,開源、即插即用 ?
DFlash 其實我之前簡單介紹過:
本文事無巨細把 DFlash 這個項目掰開揉碎講一遍
簡介
大模型生成的本質是「自回歸」 —— 第 N 個 token 必須等第 N-1 個 token 算完才能開始,token 之間是串行的,怎么也快不起來
業界目前最主流的解法叫 Speculative Decoding(投機解碼):
找一個小的 draft 模型先飛快地"猜"出一串 token
大的 target 模型并行驗證這串 token
驗證通過的直接采納,驗證不過的丟掉重來
理論上能把吞吐拉很高,但目前最強的 EAGLE-3 也只能做到 2-3× 加速,因為 draft 模型自己也是自回歸的,仍然是一個 token 一個 token 出,draft 這步本身就是瓶頸
DFlash 干的事很狠:把 draft 模型從自回歸換成了 block diffusion(塊擴散)模型
一次前向傳播直接生成一整塊 16 個 token,不再串行
結果就是:
Qwen3-8B 上做到 6× 無損加速
比 EAGLE-3 快 2.5×
推理模型(開 thinking)上也有 4.5× 加速
直接把 diffusion 模型縮小當 drafter,效果其實很一般(5 層的樸素擴散 drafter 加速只有 3× 左右)—— 因為它太小了,想從零預測未來 token 不現實
但作者發現了一個白吃的午餐:大的 target 模型在生成第 N 個 token 時,hidden states 里其實已經隱含了第 N+1、N+2、N+3...的信息
那思路就清晰了 —— 把 target 的 hidden features 喂給 draft,讓 draft "站在巨人的肩膀上"猜,而不是從零猜
為什么 diffusion 才是最佳形態
自回歸 drafter 的成本隨 token 數線性增長,所以 EAGLE-3 不得不把網絡砍到只剩 1 層 transformer,質量自然受限
擴散 drafter 一次前向出全部 token,成本和 token 數幾乎無關
? 一個多層的 DFlash 生成 16 個 token,比 1 層 EAGLE-3 生成 8 個 token 還快
更深的網絡 + 更多的 token + 更低的延遲,聽起來像作弊但確實成立
![]()
draft 模型直接復用 target 的 embedding 和 LM head,只有中間幾層是新訓練的,參數量保持極低
整套流程分三步:
Feature Fusion :從 target 模型多層均勻采樣 hidden features,經過一個輕量級投影融合
KV Injection :融合后的特征直接注入 draft 模型 每一層 的 K/V 投影里,存進 KV cache —— 這是和 EAGLE-3 最關鍵的區別。EAGLE-3 只在第一層喂特征,越往后稀釋越嚴重;DFlash 每層都灌,acceptance length 隨深度 正向 增長
Parallel Drafting :基于這套豐富 context 一次性預測下一塊 token
Qwen3-8B 在各類 benchmark 上的 greedy decoding 加速倍數(DFlash 用 block 16 + 1 步去噪,EAGLE-3 用 spec 長度 7):
任務
原速
EAGLE-3
DFlash
GSM8K
1×
2.13×
5.20×
MATH-500
1×
2.18×
6.17×
AIME24
1×
2.25×
5.91×
AIME25
1×
2.18×
5.85×
HumanEval
1×
2.48×
5.20×
MBPP
1×
2.27×
4.75×
LiveCodeBench
1×
2.24×
5.43×
SWE-Bench
1×
1.90×
2.92×
MT-Bench
1×
1.94×
2.79×
Alpaca
1×
1.88×
2.27×
數學和代碼場景下,DFlash 直接是 EAGLE-3 的 2 倍以上速度
溫度采樣(temp=1)以及開 thinking 模式下,DFlash 同樣有 ~4.5× 的穩定加速
已支持的模型
DFlash 把現在的開源主力基本兜住了:
類別
模型
Gemma 系列
gemma-4-26B-A4B-it / gemma-4-31B-it
Qwen 系列
Qwen3.6-27B / Qwen3.6-35B-A3B / Qwen3.5-4B/9B/27B/35B-A3B/122B-A10B
Coder 系列
Qwen3-Coder-Next / Qwen3-Coder-30B-A3B
大廠模型
MiniMax-M2.5(preview)/ Kimi-K2.5
OSS
gpt-oss-20b / gpt-oss-120b
即將到來
DeepSeek-V4-Flash / V4-Pro / MiniMax-M2.7 / GLM-5.1
要新模型?GitHub 提 issue 就行,作者也表示會開源訓練 recipe,到時候你自己訓一個 draft 模型加速任意 LLM
安裝
DFlash 同時支持四個后端 —— vLLM、SGLang、Transformers、MLX(M 系列 Mac),按需選
# Transformers
uv pip install -e ".[transformers]"
# SGLang
uv pip install -e ".[sglang]"
# vLLM(v0.20.1+ 已經核內核支持)
uv pip install -e ".[vllm]"# MLX(Apple Silicon)
pip install -e ".[mlx]"
Gemma 4 的 vLLM 支持還在 PR 階段,作者直接給了 docker 鏡像:
docker pull ghcr.io/z-lab/vllm-openai:gemma4-dflash-cu130
使用 vLLM 啟服務(以 Qwen3.5-27B 為例)vllm serve Qwen/Qwen3.5-27B \
--speculative-config '{"method": "dflash", "model": "z-lab/Qwen3.5-27B-DFlash", "num_speculative_tokens": 15}' \
--attention-backend flash_attn \
--max-num-batched-tokens 32768
Gemma 4 用 docker 一把梭docker run --rm -it \
--gpus all --ipc=host --shm-size=16g \
-p 8000:8000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
ghcr.io/z-lab/vllm-openai:gemma4-dflash-cu130 \
google/gemma-4-26B-A4B-it \
--host 0.0.0.0 --port 8000 \
--speculative-config '{"method": "dflash", "model": "z-lab/gemma-4-26B-A4B-it-DFlash", "num_speculative_tokens": 15, "attention_backend": "flash_attn"}' \
--attention-backend triton_attn \
--max-num-batched-tokens 32768 \
--trust-remote-code
SGLangTransformers (Qwen3 / LLaMA-3.1)export SGLANG_ALLOW_OVERWRITE_LONGER_CONTEXT_LEN=1python -m sglang.launch_server \
--model-path Qwen/Qwen3.5-35B-A3B \
--speculative-algorithm DFLASH \
--speculative-draft-model-path z-lab/Qwen3.5-35B-A3B-DFlash \
--speculative-num-draft-tokens 16 \
--tp-size 1 \
--attention-backend trtllm_mha \
--speculative-draft-attention-backend fa4 \
--mem-fraction-static 0.75 \
--trust-remote-code
MLX(M5 Pro 實測可用)from transformers import AutoModel, AutoModelForCausalLM, AutoTokenizer
draft = AutoModel.from_pretrained("z-lab/Qwen3-8B-DFlash-b16",
trust_remote_code=True, dtype="auto", device_map="cuda:0").eval()
target = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-8B",
dtype="auto", device_map="cuda:0").eval()
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-8B")
messages = [{"role": "user", "content": "How many positive whole-number divisors does 196 have?"}]
input_ids = tokenizer.apply_chat_template(messages, return_tensors="pt",
add_generation_prompt=True, enable_thinking=False).to(draft.device)output = draft.spec_generate(
input_ids=input_ids, max_new_tokens=2048, temperature=0.0,
target=target, stop_token_ids=[tokenizer.eos_token_id])
print(tokenizer.decode(output[0], skip_special_tokens=False))
from dflash.model_mlx import load, load_draft, stream_generate
model, tokenizer = load("Qwen/Qwen3.5-4B")
draft = load_draft("z-lab/Qwen3.5-4B-DFlash")
messages = [{"role": "user", "content": "How many positive whole-number divisors does 196 have?"}]
prompt = tokenizer.apply_chat_template(messages, tokenize=False,
add_generation_prompt=True, enable_thinking=True)tps = 0.0
for r in stream_generate(model, draft, tokenizer, prompt,
block_size=16, max_tokens=2048, temperature=0.6):
print(r.text, end="", flush=True)
tps = r.generation_tps
print(f"\nThroughput: {tps:.2f} tok/s")
Mac 用戶終于能在本地享受到 5× 推理加速了,這條對蘋果用戶非常友好
總結
DFlash 這個項目最大的價值,老章總結成一句話:
? 它把擴散模型的角色重新定義了 —— 擴散模型不需要去和自回歸 LLM 比生成質量,它只要做好「極快極準的 drafter」就夠了,質量由 target 模型最后做投機驗證來兜底
適合誰用?
生產環境部署 LLM 的同學 :vLLM / SGLang 都已經原生支持,加個
--speculative-config就能上,開發成本極低手頭有 Apple Silicon 的同學 :MLX 后端實測可用,本地大模型一夜之間快 5 倍
做推理加速 / 投機解碼方向研究的同學 :論文 + 代碼 + 訓練 recipe 即將全開源,是個好的二次創新基礎
不適合誰?
單卡顯存吃緊的同學要注意,draft 模型也要占顯存
極小模型(< 3B)加速空間本身就不大,性價比一般
支持的模型清單還在快速擴張,DeepSeek-V4 系列、GLM-5.1 都在 coming soon 里,未來一段時間值得持續關注
制作不易,如果這篇文章覺得對你有用,可否點個關注。給我個三連擊:點贊、轉發和在看。若可以再給我加個,謝謝你看我的文章,我們下篇再見!
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.