主题定位
本深度文档聚焦 Dispatcher 的核心数学问题:如何用单个 uint64_t 位集编码 backend(设备)× functionality(处理层) 的笛卡尔积,并在运行时 O(1) 定位到正确的 kernel。模块文件给出了 DispatchKeySet 的接口与设计动机,这里展开位运算的具体实现——combined_refcount_ 式的位段拆分、CLZ 优先级提取、runtime index 到 dispatch table 数组下标的映射。
核心原理
问题:笛卡尔积爆炸
PyTorch 的分发维度是正交的二维:
- backend(~12 种):CPU、CUDA、HIP、MPS、XPU、MTIA、HPU、PrivateUse1/2/3…
- functionality(~10 种):Dense、Sparse、SparseCsr、Quantized、BackendSelect、Fake、Python、Functionalize、Autograd(Dense/Sparse/…)、ADInplaceOrView…
若每个 (functionality, backend) 组合占一个 bit,需要 ~120 bit,uint64_t 装不下。更糟的是 dispatchTable_ 的 num_runtime_entries 会膨胀到 120,破坏”进 CPU cache”的设计前提。
解法:位段拆分
DispatchKeySet.h:35 的核心洞察:把 64 位拆成低位 backend 段 + 高位 functionality 段,一个 DispatchKey = (functionality, backend) 二维坐标,编码进单个 uint64_t:
uint64_t repr_:
┌─────────── high bits ───────────┬─── low bits ────┐
│ functionality bits (~10) │ backend bits (~12) │
│ Dense / Sparse / Autograd / ... │ CPU / CUDA / ... │
└──────────────────────────────────┴──────────────────┘
constexpr DispatchKeySet(DispatchKey k) 构造时自动拆分:纯 backend key(如 CPU)只置低位,纯 functionality key(如 Autograd)只置高位,组合 key(如 AutogradCPU)同时置两段。一个 tensor 的 key_set_ 是它所有身份的 OR——一个 requires_grad 的 CPU float tensor:key_set_ = CPU | Dense | AutogradCPU(低位有 CPU bit,高位有 Dense bit 和 Autograd bit)。
优先级:CLZ 取最高位
highestPriorityTypeId()(DispatchKeySet.h)用 count-leading-zeros 指令找最高位的 1。因为 functionality 在高位、backend 在低位,且 functionality 内部按优先级排列(Functionalize > Autograd > Dense > backend),CLZ 天然返回最高优先级的 key:
key_set_ = ... | Autograd(high) | Dense(mid) | CPU(low)
↑ CLZ 命中这里
→ 返回 AutogradCPU
一个 __builtin_clzll 指令完成 O(1) 优先级提取,无循环无分支。
runtime index → dispatch table 下标
OperatorEntry::lookup 的最后一步是把 DispatchKey 映射到 dispatchTable_ 数组下标:
// num_runtime_entries = num_functionality × num_backends
idx = ks.getDispatchTableIndexForDispatchKeySet();
kernel = dispatchTable_[idx]; // O(1) 数组下标
getDispatchTableIndexForDispatchKeySet 把 (functionality_index, backend_index) 二维坐标线性化成一维 index:idx = functionality_index * num_backends + backend_index。num_runtime_entries 在编译期固定(当前约 ~60-80),dispatchTable_ 是 std::array<KernelFunction, num_runtime_entries>,大小约几百字节,舒适地进 L1 cache。
DispatchKeyExtractor 预计算
DispatchKeyExtractor(DispatchKeyExtractor.h)避免运行时遍历参数栈提取 keyset:
// 构造时按 schema 预计算:哪些参数位是 tensor
std::bitset<64> dispatch_arg_indices_reverse_;
// computeDispatchKeySet: 一次位运算出最终集合
DispatchKeySet computeDispatchKeySet(DispatchKeySet tls_in, DispatchKeySet tls_ex) {
return ((ks | tls.included) - tls.excluded) & nonFallthroughKeys_;
}
nonFallthroughKeys_ 预记录”本算子非 fallthrough 的 key”——fallthrough key 直接穿透到下一层,不占 table 槽。(ks | tls.included) - tls.excluded 用 TLS(thread-local state)动态增减 key:tls.included 注入(如 torch.set_grad_enabled(False) 排除 Autograd),tls.excluded 移除。一次位运算完成 TLS 上下文合并。
性能与权衡
为什么不用哈希表? 哈希表查找需 ~2-3 cache miss + 哈希计算,dispatch table 数组下标是 1 cache miss + 无计算。算子调用是 PyTorch 最热路径(每次 tensor + tensor 都走),节省的纳秒在亿级调用下显著。
为什么不用虚函数表? 虚函数表绑定到 TensorImpl 的类型层级,无法表达”正交维度”——一个 CPU tensor 同时是 Variable、Sparse、需要 Functionalize,这些维度独立组合。虚函数表需要为每种组合定义子类(类爆炸),且不支持运行时注册新维度。DispatchKeySet 位集让任意组合用 8 字节编码,新 backend/新 functionality 只需加一个 bit。
num_runtime_entries 的扩张代价:每新增一个 functionality key,num_runtime_entries 增 num_backends 倍,所有算子的 dispatchTable_ 都变大。这是 PyTorch 严格限制 functionality key 数量的原因——PrivateUse1/2/3 backend bit 留给用户扩展,但 functionality 层近乎封闭。
与 std::variant 的对比:DispatchKeySet 本质是一个极简的 variant tag + 函数指针表,但比 std::variant 更灵活——支持多重身份(OR)、运行时增减(TLS)、优先级(CLZ),这些 std::variant 都做不到。