[TensorRT-LLM 源码解读] 模型定义

models 是 80+ 模型家族的定义层——AutoModelForCausalLM 注册表 + 懒加载 + 每模型特化的 HF→TRT-LLM 权重转换。

src/pages/articles/_md/AI/Infra/Inference/TensorRT-LLM/CodeWiki/1.3.0/04-models.md

← 返回概览


模块定位

models 是模型定义层——每个模型家族(Llama、DeepSeekV3、Gemma4 等)一个 modeling_*.py 文件,定义 forward 结构和权重加载逻辑。模块独立存在是因为每个模型的权重转换高度特化(MLA 的 kv_b_proj 转置、NVFP4 融合、MoE 权重重命名等无法通用化),且依赖隔离(每个文件 import 不同的 triton/CUDA kernel)。AutoModelForCausalLM 提供注册表 + 懒加载机制,让 80+ 模型按需导入。

模块架构

AutoModelForCausalLM (modeling_auto.py)    ← 工厂入口,70 行极简
  ├─ _resolve_class(config)                ← 查注册表
  └─ from_config(config)                   ← 实例化(MetaInit)

_arch_index.py                             ← 静态映射表(arch→module)
models/__init__.py __getattr__             ← PEP 562 懒加载代理
modeling_utils.py                          ← MODEL_CLASS_MAPPING + @register_auto_model

DecoderModelForCausalLM (基类)             ← forward 骨架 + PostInitCaller 元类
  └── DeepseekV3ForCausalLM 等             ← 具体模型

调用链路

从 HF checkpoint 到运行模型的完整流程:

ModelLoader._load_model()                  [pyexecutor/model_loader.py:552]
  ├─ checkpoint_loader.load_config()       → HF config.json → ModelConfig
  ├─ AutoModelForCausalLM._resolve_class() [modeling_auto.py:13]
  │   └─ get_registered_model_class(arch)  [modeling_utils.py:952]
  │       └─ _ensure_model_registered()    [modeling_utils.py:916]
  │           └─ importlib.import_module(modeling_xxx)  ← 触发 @register_auto_model
  ├─ AutoModelForCausalLM.from_config()    [modeling_auto.py:44]
  │   ├─ config.skip_create_weights = True  ← MetaInit 模式
  │   └─ model = cls(config)               → 构建 embed + layers + norm
  └─ model.load_weights(weights)           → WeightLoader 逐模块分发
方法 一行职责 关键设计决策
_resolve_class() 从 config 解析模型类 三种特殊路由:多模态/Eagle3/MTP
from_config() 工厂实例化 MetaInit 延迟分配显存
load_weights() HF→TRT-LLM 权重转换 逐模块按类型策略分发

核心实现

注册表 + 懒加载双层设计

MODEL_CLASS_MAPPING(modeling_utils.py:863)是动态注册表,@register_auto_model(name) 装饰器在 import 时填充。_arch_index.py 维护三张静态映射表(MODEL_ARCH_TO_MODULE 等,80+ 条目),让懒加载解析器知道”去哪 import”而无需执行任何 modeling_* 模块。

models/__init__.py 的 __getattr__(PEP 562,__init__.py:99)是桥梁:查 MODEL_CLASS_TO_MODULE 得模块名 → importlib.import_module → 装饰器执行填入动态表 → 返回类。_ensure_model_registered() in modeling_utils.py:916 是统一入口。Why:避免启动时加载 80+ 个模型文件(每个有大量 CUDA/triton 依赖),把导入成本推迟到真正需要时。test_lazy_model_zoo.py 保证静态表与装饰器不漂移。

MetaInit 模式

from_config() 设置 config.skip_create_weights_in_init = True,在 with MetaInitMode(): 下 cls(config) 时不分配显存,仅创建 meta tensor,延迟到 load_weights() 才分配实际显存。Why:大模型(如 DeepSeek V3 671B)初始化时若先分配全量显存再加载权重会 OOM。

权重加载策略分发

以 DeepseekV3WeightLoader in modeling_deepseekv3.py:164 为例,load_weights() 遍历 model.named_modules(),按 names[-1] 策略分发:

  • kv_b_proj → load_kv_b_proj_and_k_b_proj_trans()(TP 切分 + 转置,MLA 特有)
  • kv_a_proj_with_mqa → NVFP4 融合 q_a_proj + kv_a_proj_with_mqa(scale 对齐 + requantize)
  • gate_up_proj → 拆分为 gate_proj + up_proj
  • experts → rename_moe_weight()(HF down_proj/up_proj/gate_proj → TRT-LLM w2/w3/w1)

ConsumableWeightsDict 包装器支持 mark_consumed() 标记已消费权重优化内存。TP 切分在加载时完成(split_matrix_tp()),而非运行时。

三阶段初始化(PostInitCaller 元类)

DecoderModelForCausalLM 用 PostInitCaller 元类(modeling_utils.py:361),实例化流程 __init__() → __post_init__() → __pp_init__()。__pp_init__ 在 Pipeline Parallel 场景跳过非本 rank 的层,设置 forward_after_recv / forward_before_send 包装器实现 PP 通信。

一文件多架构复用

@register_auto_model 可 stacked——modeling_deepseekv3.py 注册了 DeepseekV3ForCausalLM、DeepseekV32ForCausalLM、GlmMoeDsaForCausalLM 三个架构名到同一个类,避免代码重复。

设计模式

模式 位置 为什么用
注册表 @register_auto_model + MODEL_CLASS_MAPPING in modeling_utils.py:892 80+ 模型可扩展,内置不覆盖外部
懒加载代理 __getattr__ + _arch_index.py in __init__.py:99 按需导入,避免启动全量加载
工厂方法 AutoModelForCausalLM.from_config() in modeling_auto.py:44 统一实例化入口
策略 WeightLoader 按 module 类型分发 in modeling_deepseekv3.py:164 每模型特化的权重转换隔离

模块间交互

models 向下依赖 modules/——模型类继承 modules 基类(如 DeepseekV3Attention(MLA)、DeepseekV3DecoderLayer(DecoderLayer)),在其上添加模型特定的初始化和 forward 路由。以 DeepseekV3 为例:DeepseekV3ForCausalLM 持有 DeepseekV3Model(embed_tokens + layers + norm)+ lm_head;DeepseekV3DecoderLayer 持有 self_attn: MLA + mlp: Deepseekv3MoE | GatedMLP + layernorm。

向上被 pyexecutor/model_loader.py 调用:_resolve_class() 解析类 → from_config() 实例化 → load_weights() 加载权重 → 推理时 model.forward(attn_metadata, input_ids, ...)。

扩展方式

新增模型架构 FooForCausalLM:

  1. 新建 modeling_foo.py:定义 FooModel(DecoderModel) + FooDecoderLayer(DecoderLayer) + FooForCausalLM(DecoderModelForCausalLM),加 @register_auto_model("FooForCausalLM"),实现 load_weights()
  2. 修改 _arch_index.py:MODEL_ARCH_TO_MODULE["FooForCausalLM"] = "modeling_foo" + MODEL_CLASS_TO_MODULE
  3. 修改 __init__.py:__all__ 添加导出
  4. 可选:若用新 attention 类型在 modules/ 新增;若有 model-specific KV cache 需求实现 get_preferred_kv_cache_manager_version()

不需要修改:modeling_auto.py、modeling_utils.py、model_loader.py——通用逻辑无需改动。