前文介紹了
然后發(fā)現(xiàn) DFlash 更猛:加速高達(dá) 6 倍 ?
同等輸出質(zhì)量、完全無損,開源、即插即用 ?
DFlash 其實(shí)我之前簡單介紹過:
本文事無巨細(xì)把 DFlash 這個(gè)項(xiàng)目掰開揉碎講一遍
簡介
大模型生成的本質(zhì)是「自回歸」 —— 第 N 個(gè) token 必須等第 N-1 個(gè) token 算完才能開始,token 之間是串行的,怎么也快不起來
業(yè)界目前最主流的解法叫 Speculative Decoding(投機(jī)解碼):
找一個(gè)小的 draft 模型先飛快地"猜"出一串 token
大的 target 模型并行驗(yàn)證這串 token
驗(yàn)證通過的直接采納,驗(yàn)證不過的丟掉重來
理論上能把吞吐拉很高,但目前最強(qiáng)的 EAGLE-3 也只能做到 2-3× 加速,因?yàn)?draft 模型自己也是自回歸的,仍然是一個(gè) token 一個(gè) token 出,draft 這步本身就是瓶頸
DFlash 干的事很狠:把 draft 模型從自回歸換成了 block diffusion(塊擴(kuò)散)模型
一次前向傳播直接生成一整塊 16 個(gè) token,不再串行
結(jié)果就是:
Qwen3-8B 上做到 6× 無損加速
比 EAGLE-3 快 2.5×
推理模型(開 thinking)上也有 4.5× 加速
直接把 diffusion 模型縮小當(dāng) drafter,效果其實(shí)很一般(5 層的樸素?cái)U(kuò)散 drafter 加速只有 3× 左右)—— 因?yàn)樗×耍霃牧泐A(yù)測未來 token 不現(xiàn)實(shí)
但作者發(fā)現(xiàn)了一個(gè)白吃的午餐:大的 target 模型在生成第 N 個(gè) token 時(shí),hidden states 里其實(shí)已經(jīng)隱含了第 N+1、N+2、N+3...的信息
那思路就清晰了 —— 把 target 的 hidden features 喂給 draft,讓 draft "站在巨人的肩膀上"猜,而不是從零猜
為什么 diffusion 才是最佳形態(tài)
自回歸 drafter 的成本隨 token 數(shù)線性增長,所以 EAGLE-3 不得不把網(wǎng)絡(luò)砍到只剩 1 層 transformer,質(zhì)量自然受限
擴(kuò)散 drafter 一次前向出全部 token,成本和 token 數(shù)幾乎無關(guān)
? 一個(gè)多層的 DFlash 生成 16 個(gè) token,比 1 層 EAGLE-3 生成 8 個(gè) token 還快
更深的網(wǎng)絡(luò) + 更多的 token + 更低的延遲,聽起來像作弊但確實(shí)成立
![]()
draft 模型直接復(fù)用 target 的 embedding 和 LM head,只有中間幾層是新訓(xùn)練的,參數(shù)量保持極低
整套流程分三步:
Feature Fusion :從 target 模型多層均勻采樣 hidden features,經(jīng)過一個(gè)輕量級投影融合
KV Injection :融合后的特征直接注入 draft 模型 每一層 的 K/V 投影里,存進(jìn) KV cache —— 這是和 EAGLE-3 最關(guān)鍵的區(qū)別。EAGLE-3 只在第一層喂特征,越往后稀釋越嚴(yán)重;DFlash 每層都灌,acceptance length 隨深度 正向 增長
Parallel Drafting :基于這套豐富 context 一次性預(yù)測下一塊 token
Qwen3-8B 在各類 benchmark 上的 greedy decoding 加速倍數(shù)(DFlash 用 block 16 + 1 步去噪,EAGLE-3 用 spec 長度 7):
任務(wù)
原速
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×
數(shù)學(xué)和代碼場景下,DFlash 直接是 EAGLE-3 的 2 倍以上速度
溫度采樣(temp=1)以及開 thinking 模式下,DFlash 同樣有 ~4.5× 的穩(wěn)定加速
已支持的模型
DFlash 把現(xiàn)在的開源主力基本兜住了:
類別
模型
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 就行,作者也表示會開源訓(xùn)練 recipe,到時(shí)候你自己訓(xùn)一個(gè) draft 模型加速任意 LLM
安裝
DFlash 同時(shí)支持四個(gè)后端 —— vLLM、SGLang、Transformers、MLX(M 系列 Mac),按需選
# Transformers
uv pip install -e ".[transformers]"
# SGLang
uv pip install -e ".[sglang]"
# vLLM(v0.20.1+ 已經(jīng)核內(nèi)核支持)
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 啟服務(wù)(以 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 實(shí)測可用)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× 推理加速了,這條對蘋果用戶非常友好
總結(jié)
DFlash 這個(gè)項(xiàng)目最大的價(jià)值,老章總結(jié)成一句話:
? 它把擴(kuò)散模型的角色重新定義了 —— 擴(kuò)散模型不需要去和自回歸 LLM 比生成質(zhì)量,它只要做好「極快極準(zhǔn)的 drafter」就夠了,質(zhì)量由 target 模型最后做投機(jī)驗(yàn)證來兜底
適合誰用?
生產(chǎn)環(huán)境部署 LLM 的同學(xué) :vLLM / SGLang 都已經(jīng)原生支持,加個(gè)
--speculative-config就能上,開發(fā)成本極低手頭有 Apple Silicon 的同學(xué) :MLX 后端實(shí)測可用,本地大模型一夜之間快 5 倍
做推理加速 / 投機(jī)解碼方向研究的同學(xué) :論文 + 代碼 + 訓(xùn)練 recipe 即將全開源,是個(gè)好的二次創(chuàng)新基礎(chǔ)
不適合誰?
單卡顯存吃緊的同學(xué)要注意,draft 模型也要占顯存
極小模型(< 3B)加速空間本身就不大,性價(jià)比一般
支持的模型清單還在快速擴(kuò)張,DeepSeek-V4 系列、GLM-5.1 都在 coming soon 里,未來一段時(shí)間值得持續(xù)關(guān)注
制作不易,如果這篇文章覺得對你有用,可否點(diǎn)個(gè)關(guān)注。給我個(gè)三連擊:點(diǎn)贊、轉(zhuǎn)發(fā)和在看。若可以再給我加個(gè),謝謝你看我的文章,我們下篇再見!
特別聲明:以上內(nèi)容(如有圖片或視頻亦包括在內(nèi))為自媒體平臺“網(wǎng)易號”用戶上傳并發(fā)布,本平臺僅提供信息存儲服務(wù)。
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.