[FlashInfer 源码解读] Autotuner

FlashInfer Autotuner 解读:AutoTuner singleton、TunableRunner 策略、四级缓存、M-bucketing、CUDA Graph 计时、跨 rank 同步。

src/pages/articles/_md/AI/Infra/Inference/FlashInfer/CodeWiki/0.6.17/06-autotuner.md

← 返回概览


模块定位

Autotuner 是 FlashInfer 的调优基础设施层——为不同 input shape / GPU 架构自动选择最优 kernel tile config(tactic)。问题:同一个 CUTLASS GEMM kernel 在 M=128 和 M=4096 下的最优 tile size 完全不同,静态启发式无法覆盖所有 shape。Autotuner 的解法是:首次遇到某 shape 时,实际在 GPU 上 profiling 所有候选 tactic,选最快的缓存;后续同 bucket shape 直接命中缓存。它是横切关注点——GEMM、MoE、MLA decode 共用同一调优框架,跨 backend(cuDNN/CUTLASS/cuBLASLt/cute-dsl)统一比较。

模块边界:autotuner 管”选 tactic”,不管 kernel 编译(在 jit)、不管算子逻辑(在各算子)。文件少而精:autotuner.py(核心,~2457 行)、initializers.py(tensor 初始化器)、__init__.py。

模块架构

Autotuner 的核心是 AutoTuner singleton(autotuner.py:1072)持有 profiling_cache dict,通过 choose_one 方法(autotuner.py:1419)选最优 runner+tactic。TunableRunner(autotuner.py:560)是抽象策略接口,每个 backend 实现子类提供 get_valid_tactics(返回候选 tactic)和 forward(执行)。TuningConfig(autotuner.py:456)定义动态维度 bucketing 策略(DynamicTensorSpec)。ProfilingCacheKey(autotuner.py:951)是缓存键,含 op 名 + runner 类名 + bucket 映射后的 shape + extras。

autotune()(autotuner.py:643)是用户面对的 @contextmanager,管理 tuning mode 生命周期:进入时 push override/skip_ops 栈 + 加载 cache 文件,退出时 pop 栈 + 若 _dirty 则 save_configs。支持嵌套(per-thread 栈)。

调用链路

首次调用(tuning mode)

with autotune(True, cache="configs.json"):
    model(inputs) → 最终调用 tuner.choose_one(...)
      │
      choose_one(custom_op, runners, tuning_config, inputs)    [autotuner.py:1419]
      ├── skip_ops 检查? → 直接返回 runners[0], -1             [行1459]
      ├── _apply_tuning_overrides                              [行1470]
      ├── search_cache(custom_op, runners, input_shapes, ...)  [行1476]
      │   查找: 1) 内存 profiling_cache  2) _file_configs (JSON)
      │         3) bundled .py configs (legacy)  4) fallback (False, 0, -1)
      ├── is_tuning_mode=True 且 cache miss:
      │   ├── _generate_optimization_profiles(tuning_config, inputs)  [行1549]
      │   │   对 DynamicTensorSpec 的 gen_tuning_buckets 做笛卡尔积 → profiles
      │   └── for each profile p:                               [行1562]
      │       ├── search_cache(p) → 已调优则跳过
      │       ├── _prepare_input_tensors(p, inputs)             # 合成测试张量
      │       ├── inputs_pre_hook(tensors)                      # 可选
      │       └── for each runner r:                            [行1609]
      │           ├── r.get_valid_tactics(tensors, p)           # 候选 tactic
      │           ├── blocklist.filter(custom_op, r, tactics)   # 过滤已知失败
      │           ├── r(tensors, tactic=-1, do_preparation=True) # 一次性准备
      │           └── for each tactic tac:
      │               ├── _profile_single_kernel(r, tensors, tac)  [行1752]
      │               │   ├── warmup (3次)
      │               │   ├── pure_profile: CUDA Graph / cudaEvent 计时
      │               │   │   ├── delay_kernel (消除 host overhead)
      │               │   │   ├── record_start → run → record_end
      │               │   │   └── elapsed_time / repeat
      │               │   └── (可选) all-reduce 跨 rank 同步计时
      │               └── 保留 min_time 对应 (runner_id, tactic)
      │       ├── profiling_cache[cache_key] = (tactic, p)      [行1713]
      │       └── _dirty = True
      └── 返回 (runner, tactic)

后续命中(inference mode)

with autotune(False, cache="configs.json"):
    model(inputs) → tuner.choose_one(...)
      ├── is_tuning_mode = False
      ├── search_cache: 内存 profiling_cache 命中? → _file_configs 命中? → fallback
      └── 返回 (runner, tactic)
方法速查表
方法 一行职责 关键设计决策
autotune (autotuner.py:643) contextmanager 管 tuning mode 嵌套栈 + exit 自动 save
AutoTuner.get (autotuner.py:1222) 双检锁 singleton 全局唯一保缓存一致
choose_one (autotuner.py:1419) 调优主入口 模板方法:profiles→遍历→profiling→选最优
search_cache (autotuner.py:1267) 四级缓存查找 内存→文件→bundled→fallback
_profile_single_kernel (autotuner.py:1752) GPU 计时 cudaEvent / globaltimer 双后端
_generate_optimization_profiles (autotuner.py:1919) 生成调优 shape 笛卡尔积 + ConstraintSpec
_find_nearest_profile (autotuner.py:2020) shape→bucket 映射 @lru_cache(16384)

核心实现

Tactic 抽象与 TunableRunner

tactic 是 Any 类型(autotuner.py:563),不限于整数——cuDNN 是 plan index(整数),cute-dsl 可能是 (tile_size, gemm1_tactic, gemm2_tactic) 元组(_tactic_to_json/_json_to_tactic 递归处理,行 47-77)。autotuner 对 tactic 语义透明——只传递、计时、缓存,不解释。tactic=-1 是特殊值:代表 fallback kernel,必须能处理任意 shape(行 570-576),使 autotuning 成为可选流程。

TunableRunner(autotuner.py:560)抽象基类定义契约:get_valid_tactics(inputs, profile) 返回候选列表,forward(inputs, tactic) 执行,get_cache_key_extras(inputs) 提供额外缓存键。每个 backend(cuDNN/CUTLASS/cuBLAS/cute-dsl)继承实现。

四级缓存

search_cache(autotuner.py:1267)四级查找:(1) 进程内 profiling_cache dict(本进程 live 调优结果);(2) _file_configs(从用户指定 JSON 加载);(3) bundled .py configs(legacy,需环境变量);(4) fallback tactic=-1。_find_nearest_profile(行 2020)用 @lru_cache(16384) 缓存 shape→bucket 映射。load_from_file 也有 lru_cache(行 1031)。Cache 文件携带 _metadata(行 297)记录 flashinfer_version/cuda_version/cublas_version/cudnn_version/gpu,加载时检查匹配(行 2274),不匹配拒绝使用,避免跨环境用无效 tactic。

Config 空间生成

_generate_optimization_profiles(autotuner.py:1919):以实际输入 shape 为 base profile(StaticDim);对每个 DynamicTensorSpec 取 gen_tuning_buckets(tuple 或 callable)作为候选值;对所有动态维度做笛卡尔积(itertools.product,行 1993);ConstraintSpec 根据 infer_shape 从其他维度推断约束维度。initializers.py 提供 6 种 tensor 初始化器(empty/zeros/ones/randn/rand/rand_scaled),默认 rand_scaled([-5,5] 均匀分布)。调用方可为每个输入指定不同初始化器(如 MoE output 用 empty,hidden_states 用 randn)。

缓存 Key 设计

ProfilingCacheKey(autotuner.py:951)含 custom_op(如 "bf16_gemm")、runner_class_name、runner_hash(含 dtype 等,但不进 file_key)、nearest_profile(经 map_to_tuning_buckets 映射的 shape)、extras(runner 自定义,如 output dtype)。关键设计:file_key(行 968)排除 runner_hash(跨进程地址不同);_find_nearest_profile 将 runtime shape 映射到 bucket shape,缓存粒度由 bucket 决定而非精确 shape。

计时精度与分布式

_profile_single_kernel(autotuner.py:1752)支持两种计时后端:cudaEvent(默认,torch.cuda.Event(enable_timing=True))和 globaltimer kernel(用于 Confidential Computing 环境,is_confidential_compute() 行 1168,cudaEvent 在该环境不可靠),通过 FLASHINFER_AUTOTUNE_TIMER 环境变量指定。set_autotune_process_group(行 912)允许跨 rank all-reduce 计时结果,确保所有 rank 选同一 tactic——避免 NCCL symmetric-memory 分配死锁。

与 CUTLASS autotuning 的关系

FlashInfer 的 autotuner 是自己的 Python 层框架,与 CUTLASS 自身 C++ autotuning 不同。每个 CUTLASS kernel 被 JIT 编译为一个 TunableRunner,get_valid_tactics 返回 tile config 列表(tile size / cluster / pipeline 组合的枚举索引),autotuner 在 Python 层做实际 GPU profiling。这意味着可跨 backend(cuDNN/cuBLASLt/CUTLASS/cute-dsl)统一调优——一次 choose_one 可同时比较 5 种 backend(gemm_base.py:1437)。

设计模式

模式 位置 为什么用
Singleton AutoTuner.get 双检锁 in autotuner.py:1222 全局唯一保缓存一致,RLock 可重入
Context Manager autotune() in autotuner.py:643 tuning mode 生命周期 + 嵌套
Strategy TunableRunner ABC in autotuner.py:560 多 backend 可互换,运行时选最优
多级缓存 search_cache 四级 in autotuner.py:1267 进程内→文件→bundled→fallback
Template Method choose_one 骨架 in autotuner.py:1419 调优流程固化,子类实现 tactics/forward

模块间交互

GEMM 调用(gemm_base.py):定义 module 级 TuningConfig(如 _FP8_GEMM_SM100_TUNING_CONFIG 行 1116),每个 backend 实现 TunableRunner 子类(如 CutlassBf16GemmRunner 行 1145),调用 tuner.choose_one("bf16_gemm", runners, config, inputs)(行 1430)。GEMM 还通过 get_effective_map_to_tuning_buckets(行 3560 等)确保 runtime bucket 映射与 profiling 时一致——否则 graph cache key 不匹配会用错图。

MoE 调用(fused_moe/core.py):MoERunner(TunableRunner)(行 1438)调 tuner.choose_one 分别调优 GEMM1 和 GEMM2(行 579)。MoE runner 的 get_valid_tactics 按 gemm_idx_for_tuning 过滤策略(行 389)。

MLA decode 调用(mla/_core.py:3754):构建 List[TunableRunner](TrtllmGenMlaDecodeRunner + CuteDslMlaDecodeRunner)调 choose_one。

与 jit 的关系:autotuner 引用 flashinfer.jit.core.logger(autotuner.py:34),但无直接调用。runner 的 get_valid_tactics 返回 JIT 编译好的 kernel 的 tactic 编号——JIT 负责编译,autotuner 负责选参数,通过 TunableRunner 间接协作。

扩展方式

新增一类 tile config 候选:(1) Runner 的 get_valid_tactics(如 CutlassBf16GemmRunner.get_valid_tactics in gemm_base.py:1146)修改返回的 tactic 列表,若需新 JIT 编译还要改 kernel 生成代码;(2) TuningConfig 的 dynamic_tensor_specs 若涉及新动态维度或 bucket 值需调整 gen_tuning_buckets/map_to_tuning_buckets;(3) initializers.py 若需特殊输入分布做准确 profiling 可能新增初始化器;(4) Cache 失效——_nvfp4_cutlass_version(行 44)和 _collect_metadata 的 flashinfer_version 自动使旧 cache 失效,nvfp4 cutlass 改动需手动递增;(5) TacticsBlocklist 若新 tactic 有已知失败 case 需更新;(6) _tactic_to_json/_json_to_tactic 若新 tactic 数据结构非已有格式需扩展序列化。