跳到主要内容

vLLM — 扩展与插件系统

9.1 Python Entry Point 插件系统

位置: vllm/plugins/__init__.py

插件组 (Plugin Groups)

vLLM 使用 Python 的 importlib.metadata.entry_points() 进行插件发现 (plugin discovery)。定义了四个插件组:

组名加载位置用途
vllm.general_plugins所有进程通用插件(在 API server、engine core 和 workers 中加载)
vllm.io_processor_plugins仅进程 0I/O 处理器扩展
vllm.platform_plugins所有进程平台特定插件(首次访问 current_platform 时加载)
vllm.stat_logger_plugins仅进程 0自定义统计日志记录器实现

插件发现 (Plugin Discovery)

def load_plugins_by_group(group: str) -> dict[str, Callable[[], Any]]:
from importlib.metadata import entry_points
discovered_plugins = entry_points(group=group)
# Filter by VLLM_PLUGINS env var if set
# Load matching plugins and return dict[name, callable]

访问控制:

  • VLLM_PLUGINS 环境变量限制加载哪些插件
  • 如果未设置 VLLM_PLUGINS,则加载所有已发现的插件
  • 如果已设置,则仅加载列出的插件名称

创建插件 (Creating a Plugin)

要创建 vLLM 插件:

  1. pyproject.toml 中创建带有 entry point 的 Python 包:
[project.entry-points."vllm.general_plugins"]
my_plugin = "my_package.vllm_plugin:register"
  1. 实现注册函数:
def register():
# Register custom components, hooks, etc.
pass
  1. 安装该包:pip install my_plugin_package

9.2 Attention Backend 注册表

位置: vllm/v1/attention/backends/registry.py

机制 (Mechanism)

Attention backend 通过名称注册,并在启动时根据硬件 (hardware)、模型架构 (model architecture) 和用户配置 (user configuration) 进行选择。

可用 backend:

Backend文件硬件说明
flash_attnflash_attn.pyNVIDIA GPUFlashAttention-2,CUDA 的默认选择
flashinferflashinfer.pyNVIDIA GPUFlashInfer 库
rocm_attnrocm_attn.pyAMD GPUROCm attention
rocm_aiter_farocm_aiter_fa.pyAMD GPUAITER FlashAttention for ROCm
cpu_attncpu_attn.pyCPUx86 CPU attention
triton_attntriton_attn.pyNVIDIA GPU基于 Triton 的 attention
tree_attntree_attn.pyNVIDIA GPU用于投机解码 (speculative decoding) 的树形 attention
flex_attentionflex_attention.pyNVIDIA GPUPyTorch flex attention (SDPA)
linear_attnlinear_attn.py任意线性 attention 模型
mamba1_attnmamba1_attn.py任意Mamba 1 SSM
mamba2_attnmamba2_attn.py任意Mamba 2 SSM
mamba_attnmamba_attn.py任意Mamba(通用)
mla/*mla/NVIDIA GPUMulti-head Latent Attention (DeepSeek)
short_conv_attnshort_conv_attn.py任意短卷积 attention (short convolution attention)

选择逻辑 (Selection Logic)

平台特定的注册表根据以下条件选择最佳可用 backend:

  1. 用户通过 VLLM_ATTENTION_BACKEND 环境变量覆盖
  2. 硬件平台(CUDA → flash_attn,ROCm → rocm_attn,CPU → cpu_attn)
  3. 模型架构需求(Mamba → mamba_attn,MLA → mla)

9.3 结构化输出 Backend (Structured Output Backends)

位置: vllm/v1/structured_output/

机制 (Mechanism)

StructuredOutputManager 在初始化时根据 StructuredOutputsConfig 选择 backend:

Backend文件描述
xgrammarbackend_xgrammar.pyXGrammar 引擎(基于 C++,速度快)
outlinesbackend_outlines.pyOutlines 库
guidancebackend_guidance.pyGuidance 库
lm-format-enforcerbackend_lm_format_enforcer.pyLM Format Enforcer

接口: StructuredOutputBackend — 抽象类,包含:

  • compile_grammar() — 将结构化输出规范 (structured output spec) 编译为 grammar 对象
  • allocate_token bitmask() — 获取下一个 token 的位掩码 (bitmask) 以强制执行 grammar 约束

异步编译 (Async compilation): grammar 编译可以在线程池中异步进行(由于跨 TP rank 的确定性要求,在 external_launcher 模式下禁用)。


9.4 模型架构注册表 (Model Architecture Registry)

位置: vllm/model_executor/models/(100+ 个模型实现)

机制 (Mechanism)

每个模型文件通过 _MODELS 字典或自动发现 (auto-discovery) 注册自身。添加新模型的方式:

  1. vllm/model_executor/models/ 中创建新文件
  2. 按照 vLLM 规范实现模型类:
    • 继承 nn.Module
    • 使用 vLLM 的自定义线性层(ColumnParallelLinearRowParallelLinear 等)
    • 实现带有 attn_metadata 参数的 forward() 方法
  3. __init__.py 中注册

9.5 工具解析器扩展 (Tool Parser Extension)

位置: vllm/entrypoints/tool_parsers/

机制 (Mechanism)

工具解析器 (tool parser) 将模型输出转换为结构化的工具调用 (tool call),用于 chat completions 和 responses API。

注册: ToolParserManager.import_tool_parser() — 动态导入自定义工具解析器。

内置解析器: 各种模型特定的解析器(例如用于函数调用格式)。

自定义解析器: 可通过 --tool-parser-plugin CLI 标志加载。


9.6 推理解析器扩展 (Reasoning Parser Extension)

位置: vllm/reasoning/

机制 (Mechanism)

推理解析器 (reasoning parser) 处理推理模型(如 DeepSeek-R1)的"思考" (thinking) 输出。

注册: ReasoningParserManager.import_reasoning_parser() — 动态导入自定义解析器。

自定义解析器: 可通过 --reasoning-parser-plugin CLI 标志加载。


9.7 投机解码扩展 (Speculative Decoding Extensions)

位置: vllm/v1/spec_decode/

多种投机解码 (speculative decoding) 策略以可插拔组件 (pluggable component) 的形式实现:

策略文件描述
n-gramngram_proposer.py, ngram_proposer_gpu.pyn-gram 查找生成 draft token
Eagleeagle.pyEagle 投机解码头
Medusamedusa.pyMedusa 多头投机解码
Draft modeldraft_model.py用于投机解码的小型 draft 模型
Suffix decodingsuffix_decoding.py基于后缀的 draft token 生成
dFlashdflash.py基于 Flash 的投机解码

9.8 量化方法注册表 (Quantization Method Registry)

位置: vllm/model_executor/layers/quantization/

支持 20+ 种量化方法作为可插拔 backend:

方法文件描述
FP8fp8.pyFP8 权重/KV 缓存量化
AWQawq.py, awq_marlin.py感知激活的权重量化 (Activation-aware weight quantization)
GPTQgptq.py, gptq_marlin.pyGPTQ 权重量化
BitsAndBytesbitsandbytes.pyNF4/int8 BnB 量化
GGUFgguf.pyGGUF 量化格式
FBGEMM FP8fbgemm_fp8.pyFBGEMM FP8 内核
ModelOptmodelopt.pyNVIDIA ModelOpt 量化
Compressed Tensorscompressed_tensors/Neural Magic 压缩格式
INT8 Expertsexperts_int8.pyINT8 MoE 专家量化
KV Cache Quantkv_cache.pyKV 缓存量化(FP8、INT8、NVFP4)

每种量化方法实现一个 QuantizationConfig 子类,定义权重如何加载以及使用哪些自定义内核 (custom kernel)。