PyTorch Helion引入编译时启发式算法替代自动调优
Fast by Default: Compile-Time Heuristics for Helion Kernels
Helion作为PyTorch的高性能DSL,此次通过编译时启发式算法彻底解决自动调优耗时痛点,实测性能超越手写算子,对追求极致推理速度的开发者极具参考价值。
TL;DR
TL;DR(太长不看)
Helion now uses compile-time heuristics that analyze a kernel’s IR and dataflow to pick fast settings without slow autotuning. It classifies the kernel as matmul, reduction, or pointwise and chooses block sizes, warps, and stages, giving near-autotuned speed for free.
Helion 现在使用编译时启发式方法,通过分析内核的 IR(中间表示)和数据流来选择快速设置,而无需缓慢的自动调优。它将内核分类为矩阵乘法(matmul)、归约(reduction)或逐元素操作(pointwise),并选择块大小、线程束(warps)和存储阶段数,从而免费获得接近自动调优的速度。
Introduction
简介
Helion, PyTorch’s domain-specific language (DSL) for performance portable machine learning kernels, relies heavily on autotuning for its performance. Autotuning time can be a major bottleneck: for example, a previous team tuning a single paged-attention kernel across 72 shapes spent 10 hours in Helion’s quick mode, and 25 at full effort. To address these issues, we’ve developed a set of heuristics that read a kernel’s IR and dataflow at compile time and emit a config directly, without any autotuning.
Helion 是 PyTorch 用于性能可移植机器学习内核的领域特定语言(DSL),其性能严重依赖自动调优。自动调优时间可能成为主要瓶颈:例如,之前一个团队在 Helion 的快速模式下对单个分页注意力(paged-attention)内核进行跨 72 种形状的调整花费了 10 小时,而在全力模式下则花费了 25 小时。为解决这些问题,我们开发了一套启发式方法,能够在编译时读取内核的 IR 和数据流并直接生成配置,无需任何自动调优。
We classify kernels into broad families based on the operations they contain – matmul, reduction, or pointwise – and apply a family-specific heuristic to each. These families are intentionally general: the matmul heuristic, for example, looks for any kernel containing a matmul operation such as hl.dot or torch.baddbmm, not just standard m-n-k matrix multiplications. These are designed to be generic and apply broadly to custom kernels that users may write.
我们根据内核包含的操作将其分为大类——矩阵乘法(matmul)、归约(reduction)或逐元素操作(pointwise)——并对每一类应用特定的启发式规则。这些类别故意设计得较为通用:例如,矩阵乘法启发式会查找包含任何矩阵乘法操作(如 hl.dot 或 torch.baddbmm)的内核,而不仅仅是标准的 m-n-k 矩阵乘法。这些启发式旨在具有通用性,广泛适用于用户可能编写的自定义内核。
Highlights:
亮点:
- Autotuner Heuristics. Our heuristics cover a large set of kernels, including GEMM, reductions, and even linear attention.
- Linear attention. Over 96 kernel-and-shape cells spanning nine linear-attention variants, the computed config beats the hand-written Triton in Flash Linear Attention (FLA) by 1.18x in the forward pass, and by 1.12x with backward included on H100. Overall the heuristic is 2.13x faster than Helion’s previous no-autotune default on H100.
- vLLM reduction kernels. Over 153 kernel-and-shape cells across six production kernels on H100, the computed config is 1.27x faster than vLLM’s own handwritten CUDA/C++ operators, and 2.2x faster than Helion’s previous default.
- The analysis itself is cheap. Compiling a kernel with the heuristics on versus off differs negligibly (tens of milliseconds), which is lost in the noise of Triton compilation.
- 自动调优启发式。我们的启发式覆盖了大量内核,包括 GEMM、归约甚至线性注意力。
- 线性注意力。在涵盖九种线性注意力变体的 96 个内核-形状组合中,计算出的配置在前向传播中比 Flash Linear Attention (FLA) 中的手写 Triton 代码快 1.18 倍,在包含反向传播时在 H100 上快 1.12 倍。总体而言,该启发式在 H100 上比 Helion 之前的无自动调优默认值快 2.13 倍。
- vLLM 归约内核。在 H100 上跨越六个生产级内核的 153 个内核-形状组合中,计算出的配置比 vLLM 自己的手写 CUDA/C++ 算子快 1.27 倍,比 Helion 之前的默认值快 2.2 倍。
- 分析本身的成本很低。开启与关闭启发式进行内核编译的时间差异微乎其微(几十毫秒),这在 Triton 编译的噪声中可以忽略不计。
Our Approach: Decide the Config at Compile Time
我们的方法:在编译时确定配置
A single Helion kernel can lower into thousands of different Triton kernels. One definition covers block sizes, loop orders, indexing modes and pipelining strategies, but the autotuner has to choose which of those thousands to emit, by compiling and benchmarking many candidates. And this autotuning cost is per kernel and per shape, which multiplies the problem the more kernels, shapes, and hardware backends you want to support.
单个 Helion 内核可以生成成千上万种不同的 Triton 内核。一个定义涵盖了块大小、循环顺序、索引模式和流水线策略,但自动调优器必须通过编译和基准测试许多候选者来决定发射其中哪一种。这种自动调优成本是针对每个内核和每种形状计算的,这意味着你想要支持的内核、形状和硬件后端越多,问题就越复杂。
We compute the config at compile time instead of searching for it at tuning time. We read the device IR and record structural facts about it: which axes are tiled, which block sizes are tunable, which tiles are live at which part of the kernel, etc. A heuristic reads those facts and emits a config.
我们在编译时计算配置,而不是在调优时搜索它。我们读取设备 IR 并记录其结构事实:哪些轴被分块、哪些块大小是可调优的、哪些分块在内核的哪个部分是活跃的,等等。启发式算法读取这些事实并发射一个配置。
The point here is not to come up with a better config than the autotuner, which has the benefit of benchmarking potentially thousands of configs before selecting one. It is to add a point in the tradeoff space between performance and wait time.
这里的重点不是提出比自动调优器更好的配置,后者有在选定之前对潜在数千个配置进行基准测试的优势。而是要在性能和等待时间的权衡空间中添加一个点。
Our heuristics serve two purposes. A primary heuristic emits the default config used when autotuning is disabled. When autotuning is enabled, helper heuristics generate roughly 5–20 related or alternative configs that seed the autotuner. The goals are different: the default config should perform well on its own, while the seeds should give the autotuner several promising starting points to speed up autotuning. In this blog, we’ll touch on both, but focus on the primary heuristic that gets emitted with autotuning off.
我们的启发式算法有两个目的。主要启发式算法在禁用自动调优时发射默认配置。当启用自动调优时,辅助启发式算法生成大约 5–20 个相关的或替代的配置,作为自动调优器的种子。目标不同:默认配置应能独立表现良好,而种子应为自动调优器提供几个有希望的起点以加速自动调优。在本博客中,我们将简要介绍两者,但重点关注在关闭自动调优时发射的主要启发式算法。
How the Heuristics Work
启发式算法的工作原理
A heuristic sets the values for various config knobs, which must be tuned carefully to achieve high performance and are normally picked by the autotuner. These include block sizes for each tiled axis, num_warps, num_stages, the L2 grouping, load eviction policies, and more.
启发式算法为各种配置旋钮设置值,这些值必须仔细调优以实现高性能,通常由自动调优器选择。这包括每个分块轴的块大小、num_warps、num_stages、L2 分组、加载驱逐策略等。
We pick a heuristic by scanning the device IR for the operations a kernel contains. Any matmul routes to the matmul heuristic regardless of what else is in the kernel; a reduction with no matmul routes to the reduction heuristic; everything else receives the pointwise heuristic. If a kernel’s extents are not all known at compile time, no heuristic fires and Helion falls back to the original default.
我们通过扫描设备 IR 来查找内核包含的操作来选择启发式算法。任何矩阵乘法都会路由到矩阵乘法启发式算法,无论内核中还有什么其他内容;没有矩阵乘法的归约会路由到归约启发式算法;其他所有内容都接收逐元素启发式算法。如果内核的范围在编译时并非全部已知,则不会触发任何启发式算法,Helion 将回退到原始默认值。
Our heuristics all lean on the same underlying analysis, which estimates resource usage of the kernel. At any step of the kernel, the analysis determines: given a candidate set of block sizes, how many registers, shared memory, and — on Blackwell — how much tensor memory, one CTA needs at that moment.
我们的所有启发式算法都依赖于相同的底层分析,该分析估计内核的资源使用情况。在内核的任何步骤中,分析确定:给定一组候选块大小,CTA 在那一刻需要多少寄存器、共享内存以及在 Blackwell 上需要多少张量内存。
To do this, we run a liveness analysis on the compiler IR and walk each graph to find where every value is last used, and guess where it will be placed in memory. This is a hard problem: we are trying to predict what Triton will keep in registers, what it will stage through shared memory, what it will materialize at all rather than fuse away. Sometimes it is easier to predict (e.g., we assume the output of an hl.dot will be placed in TMEM on B200), but the estimate is approximate.
为此,我们在编译器 IR 上执行活性分析(liveness analysis),并遍历每个图以找出每个值的最后使用位置,进而推测其在内存中的放置位置。这是一个难题:我们试图预测 Triton 会将哪些值保留在寄存器中、哪些会通过共享内存进行暂存、哪些会被实际生成而非融合消除。有时预测较为容易(例如,我们假设 hl.dot 的输出将放置在 B200 的 TMEM 上),但这种估计只是近似的。
Using this we can estimate things that matter for performance, such as:
利用这一点,我们可以估算对性能至关重要的指标,例如:
- Occupancy: how many CTAs fit on an SM at once. Whichever space runs out first decides it, e.g., a CTA needing a tenth of the SM’s registers and a tenth of its shared memory, but six tenths of its tensor memory, gets one CTA per SM, since two CTAs would go over the tensor memory budget by 20% (0.6 x 2 = 1.2).
- Register pressure: Peak live registers spread over the threads we intend to launch tells us whether the assembler will be forced to spill.
- 占用率(Occupancy):一个 SM 上同时能容纳多少个 CTA。哪个资源先耗尽就由它决定,例如,一个需要 SM 十分之一寄存器和十分之一共享内存,但需要六分之一张量内存的 CTA,每个 SM 只能分配一个 CTA,因为两个 CTA 会使张量内存超出预算 20%(0.6 x 2 = 1.2)。
- 寄存器压力(Register pressure):在我们计划启动的线程范围内,峰值活跃寄存器数量告诉我们汇编器是否会被迫进行溢出存储(spill)。
These considerations inform the selection of different config knobs. We’ll illustrate this for the matmul heuristic:
这些考量影响着不同配置选项的选择。我们将以矩阵乘法启发式算法为例来说明:
- block_size: If the launch produces fewer CTAs than the GPU has SMs, we shrink block size on a grid axis to produce more of them.
- num_stages: More stages hide more load latency, so we want as many as shared memory allows. But stages can also accidentally lose occupancy: six of them might fit while eating the CTA’s entire shared-memory budget, leaving one CTA per SM, where three stages and two CTAs per SM is the better trade.
- num_warps. Two things pull on this:
- Which instruction Triton emits. Below four warps, or with an M block under 64, Triton uses the smaller synchronous MMA; otherwise it uses the big high-throughput one (wgmma on Hopper, tcgen05 on Blackwell). Usually we want the big one. But when there is little matmul work sometimes the better answer is many CTAs per SM running small MMAs that overlap each other’s loads. So we count dynamic matmul work (essentially counting the number of times the hl.dot will be executed dynamically) and choose based on that.
- Register pressure. Warps divide the tile across more threads, so more warps means less pressure on each one. If the peak live state at the current count would spill, that argues for increasing warps.
- Feasibility Check. A final pessimistic pass over the whole kernel: add up tensor memory, shared memory and registers, and confirm the config can actually launch. The adjustments above already respect these budgets, so this is mostly defensive.
- block_size:如果启动产生的 CTA 数量少于 GPU 拥有的 SM 数量,我们会缩小网格轴上的块大小以产生更多的 CTA。
- num_stages:更多的阶段可以隐藏更多的加载延迟,因此我们希望尽可能多地使用共享内存允许的阶段数。但阶段数也可能意外地降低占用率:六个阶段可能刚好塞满 CTA 的全部共享内存预算,导致每个 SM 只有一个 CTA,而三个阶段配合每个 SM 两个 CTA 则是更好的权衡。
- num_warps。有两个因素会影响这一设置:
- Triton 发出的指令类型。当 warp 数量少于四个或 M 块小于 64 时,Triton 使用较小的同步 MMA;否则使用高吞吐量的大型 MMA(Hopper 架构上使用 wgmma,Blackwell 架构上使用 tcgen05)。通常我们希望使用大型 MMA。但当矩阵乘法工作量较小时,有时更好的方案是每个 SM 运行多个 CTA,它们使用小型 MMA 并相互重叠加载操作。因此,我们计算动态矩阵乘法工作量(本质上是统计 hl.dot 动态执行的次数),并据此进行选择。
- 寄存器压力。Warp 将图块分配到更多线程上,因此更多的 warp 意味着每个 warp 的压力更小。如果当前数量下的峰值活跃状态会导致溢出存储,这就说明应该增加 warp 数量。
- 可行性检查。对内核进行最后一次悲观扫描:累加张量内存、共享内存和寄存器的占用量,并确认配置确实能够启动。上述调整已经尊重了这些预算限制,因此这一步主要是防御性的。
In the case where there are multiple matmuls in one kernel (for example for attention and linear attention), we run the matmul heuristic on each hl.dot and use those as the starting point. They may disagree: two dots can share one block-size knob — one dot’s N is frequently the next dot’s K — and there is only one num_warps for the whole kernel.
如果一个内核中包含多个矩阵乘法(例如用于注意力机制和线性注意力),我们会对每个 hl.dot 运行矩阵乘法启发式算法,并将其作为起始点。它们可能会产生分歧:两个 dot 可以共享一个 block-size 旋钮——一个 dot 的 N 通常是下一个 dot 的 K——而且整个内核只有一个 num_warps 值。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力