vLLM推理服务教程 · 07-算子注册与平台分发

07 - 算子注册与平台分发

所属系列:vLLM 推理服务教程 | 前置:06-ModelRunner前向与采样 | 下一篇:08-kernel择优与量化底座
证据基线:commit 2e738227。主战场 vllm/model_executor/custom_op.py、vllm/utils/torch_utils.py、vllm/platforms/。
06 章的 kernel 是被调用的,本章回答它们凭什么被选中:两条注册轨道 + 一条平台解析链 + 一个亚字节类型底座。

本篇知识图谱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
%%{init: {'theme':'base','themeVariables':{'primaryColor':'#eef2ff','primaryTextColor':'#1b1b1f','primaryBorderColor':'#6366f1','lineColor':'#6366f1','textColor':'#1b1b1f','fontSize':'15px'}}}%%
mindmap
root((算子注册与平台分发))
类级轨道
op_registry :21
op_registry_oot :22
CustomOp new替换 :107
实例化一次分发 :131
dispatch_forward 五平台 :172
PluggableLayer 整层替换 :32
函数级轨道
direct_register :1009
绕开custom_op开销 :1018
define与impl :1043
dispatch_key取平台 :1038
eager_bypass :1049
平台解析链
五内置插件 :231
OOT单激活 :271
cpu目标权威 :244
Unspecified兜底 :290
CPU特化
ISA三库 :681
MLA AMX优先 :167
zen zentorch :12
ScalarType
亚字节表达 :24
bias偏置语义 :28
int4 fp8 fp6 fp4
GPTQ uint4b8 :346

本篇目标

  1. 分清类级与函数级两条算子注册轨道:前者换实现类,后者直接挂 torch dispatcher。
  2. 理解 dispatch_forward 的一次性分发设计——为什么 vLLM 敢假设"只为一个后端构建"。
  3. 掌握平台解析链:五个内置插件、OOT 插件强制单激活、VLLM_TARGET_DEVICE=cpu 的权威性。
  4. 认识 ScalarType:torch.dtype 表达不了的 sub-byte 类型(int4/FP8/FP6/FP4/GPTQ 偏置系)靠它进入类型系统。

1. 双轨注册总览

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%%{init: {'theme':'base','themeVariables':{'primaryColor':'#eef2ff','primaryTextColor':'#1b1b1f','primaryBorderColor':'#6366f1','lineColor':'#6366f1','textColor':'#1b1b1f','fontSize':'15px'}}}%%
flowchart TD
subgraph CLASS["类级轨道 custom_op.py"]
REG["op_registry / op_registry_oot :21-22"]
CO["CustomOp 实例化<br/>__new__ OOT 替换 :107-126<br/>__init__ 一次分发 :128-131"]
DF["dispatch_forward :172-205<br/>按 current_platform 返回方法"]
PL["PluggableLayer 整层替换 :32-99"]
end
subgraph FUNC["函数级轨道 torch_utils.py"]
DR["direct_register_custom_op :1009"]
LI["Library.define + impl<br/>挂 dispatch_key :1043-1044"]
EB["eager_bypass_custom_op :1049<br/>eager 直呼 op_func"]
end
REG --> CO --> DF
REG --> PL
DR --> LI --> EB

2. 类级轨道:CustomOp 与 PluggableLayer

2.1 双注册表与实例化时的 OOT 替换

两张表在模块顶部(vllm/model_executor/custom_op.py:21-22,Observed):op_registry(in-tree)与 op_registry_oot(out-of-tree),值都是类对象。两个钩子在实例化瞬间生效:

  • CustomOp.__new__(:107-126):按类名查 op_registry_oot,命中就用 OOT 类替换(:117-126)——OOT kernel 可以无侵入接管 in-tree 算子。
  • CustomOp.__init__(:128-131):self._forward_method = self.dispatch_forward(compile_native=compile_native)——平台分发只在实例化时做一次,之后 forward 直接调缓存的方法(:133-134)。forward_hip 默认回落 forward_cuda(:147-149),hip 后端复用 CUDA 实现。

2.2 dispatch_forward:五平台一选一

dispatch_forward(:172-205,Observed)开头是 NOTE(woosuk):“Here we assume that vLLM was built for only one specific backend. Currently, we do not support dynamic dispatching”(:173-174)。逻辑:先按 enabled()/_enforce_enable 记入 compilation_config 的启用/禁用名单(:183-187);未启用则可选地把 forward_native 编译掉(:189-192);启用则按 current_platform 顺序返回 forward_hip(:194-195)/forward_cpu(:196-197)/forward_tpu(:198-199)/forward_xpu(:200-201)/forward_oot(:202-203),兜底 forward_cuda(:205)。

2.3 PluggableLayer:另一种扩展粒度

PluggableLayer(:32-99)与 CustomOp 的分工写在 docstring(:40-43):“Unlike CustomOp, PluggableLayer does NOT provide per-platform forward_* dispatch. Instead, it supports out-of-tree (OOT) replacement of the entire layer class at instantiation time”——整层(含子模块组合)替换,__new__ 查 op_registry_oot(:56-65),注册装饰器 register/register_oot(:68-99)。

3. 函数级轨道:direct_register_custom_op 与 eager_bypass

3.1 为什么要绕开 torch.library.custom_op

direct_register_custom_op(vllm/utils/torch_utils.py:1009-1046)的 docstring 一针见血(:1018-1022,Observed):“torch.library.custom_op can have significant overhead because it needs to consider complicated dispatching logic. This function directly registers a custom op and dispatches it to the CUDA backend”。实现(:1040-1044):infer_schema 推导 schema → my_lib.define(op_name + schema_str, tags) → my_lib.impl(op_name, op_func, dispatch_key);dispatch_key 缺省取 current_platform.dispatch_key(:1035-1038)——同一函数注册到哪个平台由当前平台决定。fake impl 走 _register_fake(:1045-1046)。

3.2 eager_bypass:eager 路径零分发

eager_bypass_custom_op(:1049-1065,Observed):只让 Dynamo 追踪走 dispatcher(图边界需要算子节点),eager 调用直接调 op_func(:1060-1063)。适用场景写在 docstring:“host-bound hot paths such as per-linear GEMM calls, where dispatch costs several µs per call”(:1056-1057)——每个 GEMM 几微秒的分发税,在 host-bound 的 CPU/小 batch 场景不可忽略。

4. 平台解析链:五个插件与强制单激活

vllm/platforms/__init__.py:231-294(Observed):

  • 五个内置平台插件(:231-237):tpu、cuda、rocm、xpu、cpu——各自探测函数返回平台类限定名或 None。
  • OOT 平台插件(:250):load_plugins_by_group(PLATFORM_PLUGINS_GROUP) 从 importlib entry-point 组加载(NPU 等第三方硬件的扩展路径)。
  • 强制单激活(:271-283):OOT 插件激活数 ≥2 或内置插件激活数 ≥2 都直接 RuntimeError;有且仅有一个 OOT 激活时 OOT 优先(:276-278)——第三方硬件一旦声明,就整体接管平台语义。
  • 兜底:全都未激活 → UnspecifiedPlatform(:290-291)。
  • CPU 目标权威(:244-248):VLLM_TARGET_DEVICE == "cpu" 时直接返回 CPU 平台、不再探测其它插件——注释解释这是防止"加速器 wheel 在 CPU CI 主机上把 CPU 和宿主加速器同时激活"。与 02 章的 macOS 强制 cpu 一脉相承。

5. CPU 平台特化:ISA 分库与 Zen 路由

5.1 按 ISA 加载三个 .so

CpuPlatform.import_kernels(vllm/platforms/cpu.py:672-709,Observed):X86 架构下按 CPU 能力三级加载——AVX512 + AVX512-BF16 都有 → import vllm._C(:684);仅 AVX512 → vllm._C_AVX512(:691);否则 vllm._C_AVX2(:699)。:675-679 注释解释了一个怪象:库文件名叫 _C_AVX2/AVX512 但模块名都叫 _C,导入报"module export function"异常属预期、直接忽略。

5.2 MLA 优先 AMX、AMD Zen 路由 zentorch

  • AMX 优先(cpu.py:163-175,Observed):amx_available 且未显式指定 CPU_MLA 后端时,自动切 AMX_MLA——注释"Prefer AMX when available, unless CPU_MLA was explicitly requested"(:167-168)。
  • AMD Zen(vllm/platforms/zen_cpu.py:12,Observed):ZenCpuPlatform(CpuPlatform)——“CPU platform with AMD Zen (ZenDNN/zentorch) optimizations”,模型加载时把 linear 路由到 zentorch_linear_unary,VLLM_ZENTORCH_WEIGHT_PREPACK=1 时预打包权重;fp16 支持依赖 AVX512-FP16(AMD 自 Venice 代起)。

6. 底座:ScalarType 的亚字节类型系统

ScalarType(vllm/scalar_type.py:23-60,docstring Observed):表示浮点/整数尤其是 sub-byte 类型——“something that torch.dtype currently does not support”;还支持偏置编码:stored_value = value + bias,“standard GPTQ 4bit uses a bias of 8”(:28-31)。C++ 实现在 csrc/core/scalar_type.hpp,两侧签名必须同步。

预定义类型集中在 scalar_types 类(:315-346,Observed):

族 实例 行号
整数 int4、uint4、int8、uint8 及 uint2-7 :317-327
FP8 float8_e4m3fn、float8_e5m2、float8_e8m0fnu :328-330
FP6 float6_e3m2f、float6_e2m3f(OCP MX 规范) :336-338
FP4 float4_e2m1f :341
GPTQ 偏置系 uint2b2、uint3b4、uint4b8(bias=8) :344-346

命名规范注释在 :306-315(ml_dtypes 风格:float<size>_e<m>m<n>[flags]、[u]int<size>[b<bias>])。08 章的量化 kernel 择优,其参数类型就是这套 ScalarType。

本篇小结

  • 双轨:类级 CustomOp/PluggableLayer 管"类替换 + 平台方法选择"(custom_op.py:21-205),函数级 direct_register/eager_bypass 管"torch dispatcher 挂载与避税"(torch_utils.py:1009-1065)。
  • 一次分发:__init__ 时 dispatch_forward 按 current_platform 定死 _forward_method(:128-134)——“built for only one specific backend”(:173-174)。
  • 平台解析链(platforms/init.py:231-294):5 内置插件 + OOT entry-point 插件强制单激活、OOT 优先,cpu 目标权威,Unspecified 兜底。
  • CPU 特化:ISA 三库 _C/_C_AVX512/_C_AVX2(cpu.py:681-704)、MLA 优先 AMX(:167-175)、AMD Zen 路由 zentorch(zen_cpu.py:12)。
  • 底座:ScalarType 让 sub-byte + bias 类型(int4/FP8/FP6/FP4/uint4b8)进入类型系统(scalar_type.py:23-60/:315-346)。

前置:06-ModelRunner前向与采样 | 下一篇:08-kernel择优与量化底座 | 回目录:README