arganzheng · 2026-08-25
十四篇正文 · 一条请求路径 · 一切围绕动态请求、KV 状态与 GPU 资源
一个持续生成的请求,如何在 SLO、KV 显存与 GPU 算力之间被系统持续协调?
| 变化 | 版本 | 涉及 |
|---|---|---|
| Model Runner V2 成为默认,V1 只作回退 | v0.29 | 03 · 06 · 10 |
| 持有 KV 的等待请求单独排队、优先调度 | v0.31 | 04 · 08 |
| TP all-reduce 默认走 FlashInfer | v0.29 | 07 |
max_num_batched_tokens 默认 8192 → 16384 |
v0.28 | 04 · 09 |
| 删除 Medusa 与旧 Ray executor | v0.33 rc | 03 · 06 |
%%{init: {"flowchart": {"wrappingWidth": 300}}}%%
flowchart TB
subgraph S1["一台机器"]
direction LR
A["01 为什么难<br/>02 怎么衡量"] --> B["03 一个请求的路径"] --> C["04 调度 · 05 KV Cache<br/>06 GPU 执行与投机解码"]
end
subgraph S2["扩展与变化"]
direction LR
D["07 Multi-GPU<br/>08 PD 分离"] ~~~ E["请求级:09 输入 · 10 输出"] --> F["部署级:11 模型 · 12 硬件"]
end
subgraph S3["演进与验收"]
direction LR
G["13 下一站"] --> H["14 对照 SGLang"]
end
S1 --> S2 --> S3
| 机制 | 输出分布 | 归属 |
|---|---|---|
| 投机解码 | 不变(拒绝采样保证无损) | 06 执行效率 |
| 采样参数、结构化输出 | 改变「输出什么」 | 10 输出在变 |
| multi-LoRA、多模态 | 每个请求带进来的东西不同 | 09 输入在变 |
变化段按粒度排:请求级(输入、输出,同一 batch 逐请求不同)→ 部署级(模型、硬件,实例启动时定下)。
接纳时不知道要跑几轮 forward,KV 又跨步常驻、越长越大——请求有了生命周期
Prefill 偏计算密集,Decode 偏内存带宽;两者竞争同一组 GPU 资源。
| 核心问题 | 主要机制 | 篇 |
|---|---|---|
| 这一轮谁执行、执行多少? | Continuous Batching、Token Budget、Chunked Prefill、抢占 | 《Scheduler》 |
| 历史状态放哪、如何复用? | PagedAttention、Prefix Cache、GQA / MLA、KV 量化 | 《KV Cache》 |
| 这一轮如何算得更快? | FlashAttention、CUDA Graph、融合、量化、投机解码 | 《GPU 执行》 |
| 一张卡不够,如何扩展? | TP、PP、EP、CP、DP;PD 分离 | 《Multi-GPU》·《PD 分离》 |
横切约束,从请求到芯片:输入在变 · 输出在变 · 模型在变 · 硬件在变
吞吐算系统做了多少,延迟算用户等多久,Goodput 算 SLO 内有效工作
%%{init: {"flowchart": {"nodeSpacing": 10, "rankSpacing": 36, "padding": 10}}}%%
%% 图:TTFT 变差时的排查顺序与第一个要试的参数
flowchart LR
S{"TTFT 变差"}
S --> T{"queue_time 与 prefill_time<br/>谁占大头?"}
T -->|queue_time| Q{"waiting_by_reason<br/>与 KV 用量、running 数"}
Q -->|"capacity,KV 用量 ≈ 1"| Q1["<b>KV 池不够</b><br/>kv_cache_dtype=fp8 或扩副本"]
Q -->|"capacity,running = max_num_seqs"| Q2["<b>并发上限</b><br/>max_num_seqs ↑(先看 TPOT 余量)"]
Q -->|"capacity,KV 与 running 都有余量"| Q4["<b>预算被长 prompt 吃光</b><br/>long_prefill_token_threshold"]
T -->|prefill_time| P{"前缀命中率下降了?"}
P -->|是| P1["<b>前缀没复用上</b><br/>固定前缀、KV 容量 ↑"]
P -->|否| P2["<b>prefill 算量本身</b><br/>预算 ↑、TP ↑、PD 分离"]
TTFT 先拆排队与 prefill:排队长是容量或预算,prefill 长是算得多或算得慢。
%% 图:ITL / TPOT 变差时的排查顺序与第一个要试的参数
flowchart LR
S{"ITL / TPOT 变差"}
S --> I{"均值高,还是只有 P99 高?"}
I -->|均值| I1["<b>每步太重</b><br/>max_num_seqs ↓<br/>KV fp8、TP ↑"]
I -->|P99| R{"抢占速率 > 0?"}
R -->|是| R1["<b>重算式抢占</b><br/>KV 容量 ↑、watermark"]
R -->|否| R2["<b>大 prefill 块混进 decode 步</b><br/>max_num_batched_tokens ↓<br/>或 PD 分离"]
均值高是每步都重,只有 P99 高是个别步突然变重。
| 调大 | 变好 | 变坏 |
|---|---|---|
max_num_batched_tokens 2048 → 8192 |
总吞吐 20.3k → 21.4k tok/s(+5%) | 同一步 64 个 decode 流的 ITL 99 → 376 ms |
max_num_seqs 128 → 256 |
Output tok/s +54% | TPOT 11.8 → 15.4 ms,越过 15 ms SLO 后 Goodput 断崖 |
每个参数都是在两个指标之间换:一步多装一点,摊薄权重读取,这一步也更长。
/metrics 的 8 个面板| # | 面板 |
|---|---|
| 1 | 请求与 token 流量(按 finished_reason) |
| 2 | TTFT 分位数与排队 |
| 3 | ITL 与 TPOT(P99 / P50) |
| 4 | 队列:running、waiting_by_reason |
| 5 | KV 用量与抢占速率 |
| 6 | Prefix cache 命中率 |
| 7 | 每步 token 数与请求形状 |
| 8 | 效率:MFU 估算、投机接受率 |
控制面在 CPU 只动元数据,数据面在 GPU 只动张量;边界上只过两条消息
%%{init: {"flowchart": {"wrappingWidth": 220}}}%%
flowchart TB
subgraph CP["控制面 · CPU · 只处理元数据"]
direction LR
API["<b>API Server + AsyncLLM</b><br/>tokenize、流式输出"]
EngineCore["<b>EngineCore</b><br/>step() 主循环"]
Scheduler["<b>Scheduler</b><br/>每轮给谁几个 token"]
KV["<b>KVCacheManager + BlockPool</b><br/>请求 → block id"]
API --> EngineCore --> Scheduler --> KV
end
subgraph DP["数据面 · GPU · 只处理张量"]
direction LR
Worker["<b>GPU Worker × N</b><br/>ModelRunner:准备输入、前向、采样"]
Mem["<b>GPU 显存</b><br/>权重分片 · KV Cache · 激活"]
Worker --> Mem
end
CP <==>|"↓ SchedulerOutput:请求 id、token 数、新 block id<br/>↑ ModelRunnerOutput:采样出的 token id"| DP
classDef cp fill:#eaf2fb,stroke:#5b8fd6,color:#1d3557
classDef dp fill:#fdf0e6,stroke:#d9822b,color:#5a2e0e
class API,EngineCore,Scheduler,KV cp
class Worker,Mem dp
Executor 是引擎内对象:DP 定 EngineCoreProc 数、TP×PP 定 WorkerProc 数
%%{init: {"flowchart": {"wrappingWidth": 260}}}%%
graph TB
EX["<b>Executor</b><br/><i>执行抽象:单设备或多设备模型调用</i>"]
EX --> W0 & W1 & WN
subgraph W0["Worker 0 (GPU 0)"]
direction TB
M0["ModelRunner"] --- S0["Model Weights(分片)<br/>KV Cache"]
end
subgraph W1["Worker 1 (GPU 1)"]
direction TB
M1["ModelRunner"] --- S1["Model Weights(分片)<br/>KV Cache"]
end
subgraph WN["Worker N (GPU N)"]
direction TB
MN["ModelRunner"] --- SN["Model Weights(分片)<br/>KV Cache"]
end
NC["NCCL:TP all-reduce / PP 激活收发"]
W0 & W1 & WN <-.-> NC
Executor 定交给哪些 Worker,Worker 管一张卡,ModelRunner 跑完一轮。
实线是一条请求从入口驱动到 kernel 的调用方向,虚线是主干之外的直接依赖。
%%{init: {"flowchart": {"wrappingWidth": 480}}}%%
%% 图:vLLM v0.27.1(CUDA)的源码与 wheel 制品:Python 源码原样打包,csrc 与 FlashAttention 经 CMake 编译成扩展模块
flowchart LR
subgraph SRC["源码(vllm 仓库 + CMake 拉取的依赖)"]
PY["<b>vllm/ 下的 .py</b><br/>entrypoints · v1 · model_executor · distributed"]
CS["<b>csrc/libtorch_stable/</b><br/>layernorm、量化、cache、sampler、all-reduce"]
CM["<b>csrc/libtorch_stable/moe/</b><br/>moe_align_sum · topk_softmax"]
CA["<b>csrc/cumem_allocator.cpp</b><br/>sleep mode 的显存分配器"]
FA["<b>vllm-project/flash-attention</b><br/>CMake FetchContent,固定 commit"]
end
subgraph WHL["wheel 里的制品"]
W1["<b>vllm/ 下的 .py</b><br/>原样打包"]
W2["<b>vllm/_C_stable_libtorch.abi3.so</b><br/>注册 torch 自定义算子"]
W3["vllm/_moe_C_stable_libtorch.abi3.so"]
W4["vllm/cumem_allocator.abi3.so"]
W5["<b>vllm/vllm_flash_attn/</b><br/>_vllm_fa2_C · _vllm_fa3_C"]
end
PY -->|"setup.py 收集"| W1
CS -->|"CMake · nvcc"| W2
CM -->|"CMake · nvcc"| W3
CA -->|"CMake"| W4
FA -->|"CMake · nvcc"| W5
classDef src fill:#f4f4f4,stroke:#888,color:#333
classDef art fill:#f2eefb,stroke:#7a68a6,color:#2e2450
class PY,CS,CM,CA,FA src
class W1,W2,W3,W4,W5 art
Python 源码原样打包;csrc 与 FlashAttention 经 CMake 编译成扩展模块。
%%{init: {"flowchart": {"wrappingWidth": 260}}}%%
%% 图:两机 TP=8 × PP=2:节点 0 跑 API Server、EngineCoreProc 和 PP stage 0,节点 1 以 --headless 只拉起 PP stage 1 的 8 个 WorkerProc
flowchart LR
subgraph N0["节点 0 · --node-rank 0"]
API["<b>API Server 进程</b>"]
ECP["<b>EngineCoreProc</b><br/>Scheduler · MultiprocExecutor"]
S0["<b>WorkerProc rank 0–7</b><br/>PP stage 0 · TP=8"]
end
subgraph N1["节点 1 · --node-rank 1 --headless"]
HL["<b>headless 启动进程</b><br/>只拉起本机 Worker"]
S1["<b>WorkerProc rank 8–15</b><br/>PP stage 1 · TP=8"]
end
API <--> ECP
ECP -->|"本机共享内存"| S0
ECP -->|"跨机 ZMQ"| S1
HL --> S1
S0 ==>|"PP 激活"| S1
S1 -.->|"output_rank = 8 回传"| ECP
classDef cp fill:#eaf2fb,stroke:#5b8fd6,color:#1d3557
classDef dp fill:#fdf0e6,stroke:#d9822b,color:#5a2e0e
class API,ECP,HL cp
class S0,S1 dp
两机 TP=8 × PP=2:节点 0 有完整的控制面,节点 1 只有 WorkerProc。
只有显存域跨请求共享;跨进程只传整数 block_id,KVCacheBlock 对象不出 EngineCore。
%%{init: {"flowchart": {"wrappingWidth": 160}}}%%
%% 图:调度器一侧:request_id 找到 KVCacheBlock,跨进程只剩 block_id
flowchart LR
subgraph SB["EngineCoreProc:Scheduler / KVCacheManager"]
direction TB
REQ["<b>Request</b><br/>• request_id、status、num_computed_tokens<br/>• output_token_ids、block_hashes"]
R2B["<b>req_to_blocks</b><br/>request_id → list#91;KVCacheBlock#93;"]
BLK["<b>KVCacheBlock</b><br/>block_id、ref_cnt、block_hash"]
POOL["<b>BlockPool</b><br/>• blocks#91;block_id#93;:全部块<br/>• block hash → 块:前缀缓存表"]
end
SO["<b>SchedulerOutput</b><br/>• 每步一份,广播给每个 Worker<br/>• 只带 block_id 整数,不带 KVCacheBlock 对象"]
REQ -- "request_id" --> R2B --> BLK
BLK -- "block_id 是下标" --> POOL
REQ -. "block_hashes 查前缀命中" .-> POOL
R2B -- "只取出 block_id" --> SO
classDef state fill:#e8f1ff,stroke:#3b6fb6;
classDef msg fill:#fff4e0,stroke:#d9822b;
classDef mem fill:#e9f7e9,stroke:#3a8a3a;
class REQ,R2B state;
class SO msg;
class BLK,POOL mem;
%%{init: {"flowchart": {"wrappingWidth": 160}}}%%
%% 图:Worker 一侧:block_id 落到 InputBatch 的行与 BlockTable,派生出 slot_mapping
flowchart LR
SO2["<b>SchedulerOutput</b><br/>• 新请求:prompt token ids + block_ids<br/>• 已有请求:new_block_ids 增量"]
subgraph WK["WorkerProc:GPUModelRunner"]
direction TB
CRS["<b>CachedRequestState</b><br/>• 每个请求一份<br/>• block_ids、num_computed_tokens、output_token_ids"]
IB["<b>InputBatch 的一行</b><br/>• req_id → 行号<br/>• token_ids_cpu#91;row, :#93;"]
BT["<b>BlockTable</b><br/>• 每个 KV cache group 一张<br/>• block_table#91;row, j#93; = block_id"]
SM["<b>slot_mapping</b><br/>• slot = block_id × block_size + offset<br/>• 由 Triton kernel 在 GPU 上算"]
KV["<b>KV Cache 张量(GPU HBM)</b><br/>kv_cache#91;block_id#93;#91;offset#93;"]
end
SO2 --> CRS
CRS --> IB
IB --> BT
BT --> SM
SM --> KV
BT -.-> KV
classDef state fill:#e8f1ff,stroke:#3b6fb6;
classDef msg fill:#fff4e0,stroke:#d9822b;
classDef mem fill:#e9f7e9,stroke:#3a8a3a;
class CRS,IB,BT state;
class SO2 msg;
class SM,KV mem;
| 边界 | 过界前有什么 | 过界后只剩什么 |
|---|---|---|
| API Server → 引擎 | 原始 messages、工具定义、文本 | 一串 token id 加采样参数 |
| 调度器 → Worker | 块对象、引用计数、块哈希、队列顺序 | 整数块号与「这一步推进几个 token」 |
可见性边界就是进程边界:执行侧不知道前缀是否命中。
schedule ~0.05 ms + prepare ~0.1 ms,对十毫秒量级的 forward 约 1%,且被流水线化。
十一步、三道边界:③ ZMQ 与 ⑤ 共享内存队列是进程边界,⑦ 是 Python → CUDA。
跨进程传递的是请求元数据、token IDs 与调度结果,不是完整模型状态。
主线程立即回到 schedule;序列化走输出线程,detokenize 与 stop 检查在 API Server 进程。
每轮结合 token budget、请求状态与 KV 空间,决定运行哪些请求、每个请求推进多少 token。
%% 图:Scheduler 的全貌:Token Budget、KV Cache、Mixed Batch
flowchart TB
S["<b>Scheduler</b>"] --> TB["<b>Token Budget</b>"]
TB --> A["<b>Request A</b><br/>256 · Prefill"] & B["<b>Request B</b><br/>1 · Decode"] & C["<b>Request C</b><br/>5 · Spec Decode"]
A & B & C --> M["<b>Mixed Batch</b>"]
classDef card fill:#ffffff,stroke:#a9b6c4,color:#243447
classDef compute fill:#eef5ff,stroke:#5880b4,color:#1d3557
classDef memory fill:#eff9f4,stroke:#4b9477,color:#1f4a37
classDef request fill:#e9f4ee,stroke:#70a582,color:#1f4a37
classDef waiting fill:#fff5e6,stroke:#d79a45,color:#5a3a0e,stroke-dasharray:5 4
class S card
class TB compute
class A,B,C request
class M memory
block_size=16;2,050 token 占 129 块,Prefix Cache 只复用满块
Gemma-3-27B 一个 32K 请求:不区分窗口 15.5 GiB → 窗口感知约 3 GiB(5.2×)
| 场景(每卡约 50 GiB KV 池) | BF16 | FP8(E4M3) |
|---|---|---|
| 每卡块池 | 81,920 块 | 163,840 块 |
| 2,350 token 的请求 | 557 | 1,114 → max_num_seqs 1,024 封顶 |
| 8K 上下文 | 160 | 320 |
| 32K 上下文 | 40 | 80 |
每 token 每卡 40 KiB → 20 KiB:并发翻倍,decode 每步读的 KV 也减半。
Decode 权重读取下界约 5.3 ms、实测约 10 ms;decode 占总时 97%
| 浪费形态 | 症状 | 对策 |
|---|---|---|
| GPU 在等 CPU 发指令 | kernel 之间有气泡 | CUDA Graph |
| GPU 在等 HBM 送数据 | 算力闲置、访存打满 | FlashAttention、算子融合 |
| 搬的每个数太胖 | 带宽被低信息密度的数据占满 | FP8 / INT8 / INT4 量化 |
| 轮次本身太多 | 每轮只产出 1 个 token | 投机解码 |
Prefill 2050 token ≈ 92 ms 占 3%,300 步 decode ≈ 3000 ms 占 97%——优化都冲着 Decode。
slot_mapping 说 KV 写去哪,block_table 说能读哪些块;整步唯一的同步点在采样结果 D2H。
Gated DeltaNet 每请求每层只存一份状态;prefill 2050 token = 33 块,串行 33 步而不是 2050 步。
无损:只改每步决定的位置数;batch 1、接受长度 2.5 约 2.1×,batch 128 每步慢 1.9 倍
「KV 回滚」就是 num_computed_tokens -= num_rejected:被拒槽位不清理、不释放,下一步直接覆盖。
| batch | K | 每步 token | 验证免费? | 每步(估) |
|---|---|---|---|---|
| 1 | 3 | 4 | 是 | ≈ 10 ms |
| 32 | 3 | 128 | 是 | ≈ 10–11 ms |
| 64 | 3 | 256 | 接近临界 | ≈ 12 ms |
| 128 | 3 | 512 | 否,≈ 1.7× 临界 | ≈ 17 ms |
| 128 | 0(不开) | 128 | —— | ≈ 10 ms |
batch=128 时每步慢 1.9 倍、只换来 2.5 倍步数减少——系统吞吐反降。
TP/PP/EP/CP 解决装不下,DP 解决要更多吞吐且在最外层
| 遇到的问题 | 策略 | 切的是什么 |
|---|---|---|
| 单卡放不下一个 Linear 层 | TP | 层内的矩阵,按行 / 列切 |
| 单层放得下,整个模型太大 | PP | 按层切,分段流水 |
| MoE 的 Expert 太多 | EP | 按 Expert 切 |
| 上下文太长,KV 装不下 | CP | 按 token 序列切 |
| 装得下,但要更多吞吐 | DP | 不切,整个模型复制一份 |
TP 组是相邻的卡、走 NVLink;PP 组隔一个 TP 组;DP 组隔一整个副本、跨机。
匹配≠就绪、算完≠可回收;分离换来配置自由与交接成本
8 个 rank-16 槽位静态占 1.44 GB/卡,与实际加载数无关
一张图 encoder 输出 9–22 MB,它占的 KV 184–438 MB,约 20 倍
| 步 | 函数 | 做什么 |
|---|---|---|
| 1 | OnlineRenderer.render_chat() |
校验 tool_choice 与启动参数 |
| 2 | parse_chat_messages() |
图片进 mm_data,留下占位 |
| 3 | safe_apply_chat_template() |
选模板,Jinja 渲染出文本 |
| 4 | tokenize_prompts_async() |
文本 → token id |
| 5 | _process_multimodal() |
一个图片占位 → N 个 |
| 6 | Parser.adjust_request() |
按 tool_choice 写约束 |
| 7 | InputProcessor.process_inputs() |
并入停止 token |
引擎只看到 prompt_token_ids;模板就是训练格式,配错不报错。
<|im_start|>system
You are Qwen, created by Alibaba Cloud. ...
# Tools
<tools>{"type": "function", "function": {"name": "get_weather", ...}}</tools>
...<|im_end|>
<|im_start|>user
深圳现在多少度?<|im_end|>
<|im_start|>assistant
<tool_call>{"name": "get_weather", "arguments": {"city": "深圳"}}</tool_call><|im_end|>
...
<|im_start|>assistant
Qwen2.5-7B-Instruct 两轮对话加一个工具:232 token,不带 tools 只有 100。
add_generation_prompt=false:模型替用户接着说--tool-call-parser 不配:调了工具,客户端收不到同一 batch 每行按自己的采样参数;grammar 变成每步一张 16 KB / 行的 bitmask
| 层 | 采样语义 | 结构化输出 |
|---|---|---|
| API | 校验参数;开投机时拒绝 min_p / logit_bias |
校验 schema、选后端 |
| Scheduler | —— | 异步编译 grammar、每步填 bitmask、推进 FSM |
| KV Cache | —— | —— |
| ModelRunner / Sampler | 每请求参数按行向量化 | GPU 上按行打掩码 |
| 多卡 | TP 每 rank 各自采样;PP 末 stage 采样 | GrammarOutput 广播给所有 worker |
| PD 分离 | 参数随请求交接 | P、D 各编译一次,都落在 TTFT 上 |
output_rank 回复%% 图:一步采出的 token 在 API Server 进程里变成 OpenAI 响应字段
flowchart LR
core["<b>EngineCore</b><br/>新 token id"]
subgraph api["API Server 进程"]
direction LR
det["<b>OutputProcessor</b><br/>detokenize"]
rp{"推理已结束?"}
rs["<b>ReasoningParser</b><br/>切出 reasoning"]
tp["<b>ToolParser</b><br/>抠出 tool_calls"]
dm["<b>DeltaMessage</b><br/>三个字段"]
det --> rp
rp -- "否" --> rs
rp -- "是" --> tp
rs -- "标记之后" --> tp
rs --> dm
tp --> dm
end
core -- "ZMQ" --> det
解析在 API Server;约束在前、解析在后。
tool_choice 落在采样与解析两层tool_choice |
采样时(EngineCore) | 解析时(API Server) |
|---|---|---|
none |
不约束 | 全部是 content |
auto |
不约束 | 事后抠,坏了退回 content |
auto + strict: true |
structural tag,<tool_call> 触发 |
调用部分已保证合法 |
required |
structural tag,至少一个调用 | 同一个 hermes 解析器 |
| 点名函数 | structural tag,恰好一个调用 | 同上,finish_reason: stop |
要格式保证就约束采样;auto 不标 strict 时格式全靠模型。
| 步 | 新 token | 本步发出的 delta |
|—:|—|—|
| 1–8 | <tool_call> … _weather | 无:名字的右引号还没到 |
| 9 | ", | 函数名 get_weather + 调用 id |
| 13–18 | ` {“ … ”}}\n | 六段 arguments 片段,拼起来是 {“city”: “广州”} |
| 19 | </tool_call> | 无 |
| 20 | <code><|im_end|></code> | 空 delta,finish_reason: “tool_calls”` |
名字整块发,参数按文本差分发;客户端拼完才是合法 JSON。
模型变化按「算什么 / 状态长什么样 / 怎么算」落到模型层、运行时层、算子层
| 模型变化 | 模型层 | 运行时层 | 算子层 |
|---|---|---|---|
| 激活、归一化 | 组合公共层 | 不动 | 缺算子时加 CustomOp |
| MLA | MLA attention 层 | MLAAttentionSpec |
MLA 专用后端 |
| Mamba / 混合 | IsHybrid 声明 |
MambaSpec 定长状态 |
mamba 算子切图 |
| MoE | FusedMoE + 路由 |
EP 组、EPLB | fused MoE、All-to-All |
| MTP | DeepSeekMTP 类 |
回退 num_computed_tokens |
RejectionSampler |
| 新量化 | 不改 | 只有 KV 量化要改 | LinearMethodBase 子类 |
%%{init: {"flowchart": {"wrappingWidth": 200}}}%%
%% 图:一个模型从 checkpoint 到第一次 forward:主进程只解析配置,Worker 进程才 import 模型、建结构、装权重
flowchart TB
subgraph P1["引擎主进程:不 import 模型文件"]
direction LR
CFG["<b>ModelConfig</b><br/>读 config.json 与命令行"]
INS["<b>inspect_model_cls</b><br/>子进程里做 Protocol 检查"]
INFO["<b>_ModelInfo 能力位</b><br/>不支持 PP 却开 PP → 启动即报错"]
CFG --> INS --> INFO
end
subgraph P2["每个 Worker 进程:建模型、装权重"]
direction LR
INIT["<b>initialize_model</b><br/>这时才 import 模型文件"]
LW["<b>load_weights</b><br/>AutoWeightsLoader + WeightsMapper"]
WRAP["<b>按能力包装</b><br/>isinstance:LoRA、EPLB、CUDA Graph"]
SPEC["<b>get_kv_cache_spec</b><br/>逐层收集 KVCacheSpec"]
INIT --> LW --> WRAP --> SPEC
end
P1 --> P2
MLA 每 token 存 512 维 latent + 64 维 RoPE key,get_num_kv_heads() 返回 1。
核心依赖抽象能力而非具体芯片;OOT 靠主仓库的稳定契约
四转变:配置→自动计划、缓存→分布式状态、执行→多阶段、目标→Goodput
| 维度 | vLLM v0.27.1 | SGLang v0.5.18 |
|---|---|---|
| 出发点 | PagedAttention:显存碎片 → 块 | RadixAttention:前缀复用 → 树 |
| 调度 | 一个调度器,token 预算,无阶段 | 每 rank 一份调度器,prefill 优先 |
| KV | block hash、block_table |
RadixCache、req_to_token |
| 进程 | SchedulerOutput 跨进程到 WorkerProc | Scheduler 与 ModelRunner 同进程 |
一句话:vLLM 是“一个调度器 + 块”,SGLang 是“N 个相同调度器 + 树”。
| 篇 | 这个请求遭遇了什么 | 数字 |
|---|---|---|
| 05 | KV 切成块,125 个 system prompt 块可复用 | 147 块 / 734 MB |
| 04 | 没有 prefill 阶段,被持续发放 token 额度 | chunk=512 分 5 段 |
| 06 | 97% 的时间花在 300 步 decode 上 | 92 ms + 3,000 ms |
| 07 | 每 token 每步在 8 卡间同步 | 约 2.5 MB、~0.09 ms |
| 08 | 拆成 PD 两池,KV 跨节点搬一次 | 641 MB ≈ 13 ms |
没有哪一个数字能单独说明问题,合在一起才是系统。
| 活多久 | 有哪些对象 |
|---|---|
| 整个实例 | 权重分片、KV 池、CUDA Graph 与它的固定输入 buffer、adapter 槽位、平台对象 |
| 整个请求 | 请求对象与它在执行侧的镜像、块号列表、输出解析器的流式状态 |
| 一步 | 调度结果、token 预算、输入张量与 slot_mapping、结构化输出的掩码 |
跨档的只有两个特例:KV 块,和 PD 分离的源块。