Helion联合NVIDIA CompileIQ优化PTXAS编译器控制参数
One Search, Two Layers: Compiler Controls Inside PyTorch Helion using NVIDIA CompileIQ
深入解析了GPU内核优化的最后一步,给出了可复现的联合搜索工作流与代码示例,是高性能计算开发者的硬核参考。
With Helion’s release last year, it aimed at enabling developers to write GPU kernels in PyTorch-native Python and let its powerful autotuner do the work that used to require hand-written Triton and abstract away hardware-specific implementation details. Helion’s autotuner explores thousands of kernel configurations and routinely lands within a few percent of expert-tuned code, and oftentimes exceeding it.
去年 Helion 发布时,旨在使开发者能够使用 PyTorch 原生 Python 编写 GPU 内核,并让其强大的自动调优器完成过去需要手写 Triton 的工作,同时抽象掉硬件特定的实现细节。Helion 的自动调优器会探索数千种内核配置,通常能达到接近专家调优代码的性能(误差在几个百分点以内),并且经常超越它。
The question that now needs to be answered is this: If a kernel has already been autotuned and optimized to the best of a developer’s ability, is there anything left?
现在需要回答的问题是:如果一个内核已经被自动调优到开发者能力的极限,是否还有优化的空间?
There is. It lives below the kernel, in the compiler.
有的。它存在于内核之下,即编译器层面。
Every kernel autotuner in the ecosystem whether it is Helion’s, Triton’s, the one inside torch.compile, they all search the same thing: tile shapes, warp counts, pipeline stages, memory access strategy. All of those are decisions about what work each thread block does. Underneath them sits a second set of decisions about how that work becomes machine assembly instructions generated by ptxas, NVIDIA’s PTX assembler. Things like register allocation policy, fine-grained instruction scheduling, code-generation heuristics are left at their defaults for all kernels shipping today – not because the defaults are optimal, but because nothing was searching and optimizing them!
生态系统中的每个内核自动调优器——无论是 Helion 的、Triton 的,还是 torch.compile 内部的那个——它们搜索的都是相同的内容:图块形状(tile shapes)、线程束数量(warp counts)、流水线阶段数、内存访问策略。所有这些决策都关乎每个线程块执行什么工作。在这些决策之下,还有一组关于如何将工作转化为由 ptxas(NVIDIA 的 PTX 汇编器)生成的机器汇编指令的决策。诸如寄存器分配策略、细粒度指令调度、代码生成启发式规则等,在当前发布的内核中均保持默认设置——并非因为默认值是最优的,而是因为没有任何东西在搜索和优化它们!
Helion now enables this, and this post is about what that second compiler-specific space contains, why searching it jointly with the kernel configuration space shows further improvements on kernels that were already deemed to be optimal.
Helion 现在实现了这一点,本文旨在探讨这个特定于编译器的第二层空间包含什么内容,以及为什么将其与内核配置空间联合搜索,能在已被认为最优的内核上带来进一步的改进。
The searching is done by NVIDIA’s CompileIQ, which optimizes ptxas controls the way an autotuner optimizes tile sizes and packages the result as a portable Advanced Control File (ACF). Most users won’t run that search themselves: CompileIQ publishes pre-tuned ACF catalogs that Helion’s autotuner can now evaluate alongside the kernel configurations it already explores.
搜索工作由 NVIDIA 的 CompileIQ 完成,它优化 ptxas 的控制参数,其方式类似于自动调优器优化图块大小,并将结果打包为可移植的高级控制文件(ACF)。大多数用户不会自行运行该搜索:CompileIQ 会发布预调优的 ACF 目录,Helion 的自动调优器现在可以将其与自己正在探索的内核配置一起进行评估。
Starting off with highly-optimized kernels & optimizing the last-mile
从高度优化的内核出发 & 优化最后一公里
It matters a great deal what baseline one compares against. A “we made kernels faster” claim is easy if the starting point is a naive kernel, and close to meaningless for real-world workloads.
所比较的基线至关重要。如果起点是一个朴素的内核,那么声称“我们让内核更快”很容易做到;但对于真实世界的工作负载来说,这种说法几乎毫无意义。
The kernels we use here come from the pretuned_kernels/ in the Helion repository, roughly 27 kernels covering the shapes that show up in real model code: FP8 and NVFP4 quantization, RMSNorm variants, RoPE, grouped GEMM, causal depthwise convolution, Gated DeltaNet decode, and fused model regions like a Qwen3 decode layer.
我们在此使用的内核来自 Helion 仓库中的 pretuned_kernels/ 目录,大约包含 27 个内核,覆盖了真实模型代码中出现的各种形状:FP8 和 NVFP4 量化、RMSNorm 变体、RoPE、分组 GEMM、因果深度卷积、Gated DeltaNet 解码,以及像 Qwen3 解码层这样的融合模型区域。
The important property to note here is that these are not autotuned on the fly. Each one ships with ahead-of-time heuristic files per architecture: _helion_aot_rms_norm_cuda_sm90.py for H100, _helion_aot_rms_norm_cuda_sm100.py for B200 etc. that Helion selects at runtime. The autotuning search already happened offline, and its result is baked in. When this program is run, the user gets tuned performance with no search cost.
这里需要注意的重要属性是,这些内核并非在运行时进行自动调优。每个内核都随附针对每种架构的预先编译启发式文件:H100 对应 _helion_aot_rms_norm_cuda_sm90.py,B200 对应 _helion_aot_rms_norm_cuda_sm100.py 等,Helion 会在运行时选择相应的文件。自动调优搜索已在离线阶段完成,其结果已固化在内核中。当该程序运行时,用户即可获得经过调优的性能,而无需承担搜索开销。
That is our baseline: a kernel whose configuration space has already been swept, on the hardware it’s running on, by the kernel authors and the Helion compiler developers. If there’s headroom below that, it’s real headroom and that’s the last-mile focus of this post.
这就是我们的基线:一个内核配置空间已由内核作者和 Helion 编译器开发人员在目标硬件上遍历过的内核。如果在此基线之下仍有优化空间,那才是真实的优化空间,这也是本文关注的最后一公里重点。
What Helion’s autotuner searches, and where it stops
Helion 自动调优器的搜索范围及其停止条件
A Helion kernel is ordinary PyTorch with one new idea – tiles:
Helion 内核是普通的 PyTorch 代码,但引入了一个新概念——图块(tiles):
import torch, helion, helion.language as hl
@helion.kernel()
def softmax(x: torch.Tensor) -> torch.Tensor:
n, _m = x.size()
out = torch.empty_like(x)
for tile_n in hl.tile(n):
out[tile_n, :] = torch.nn.functional.softmax(x[tile_n, :], dim=1)
return outThe user never says how big a tile is. That’s the point: hl.tile declares that the iteration space is tiled, and the autotuner decides the rest. A single kernel definition implicitly generates a search space of thousands of Triton configurations that span block_sizes, loop_orders, l2_grouping, num_warps, num_stages, indexing (pointer arithmetic vs. block pointers vs. TMA descriptors), pid_type, reduction_loops, and more. Helion lowers through TorchInductor to Triton, compiles each candidate, times it, and keeps the winner.
用户从不指定图块的大小。这正是关键所在:hl.tile 声明迭代空间被划分为图块,其余部分由自动调优器决定。单个内核定义隐式生成了数千个 Triton 配置的搜索空间,涵盖 block_sizes、loop_orders、l2_grouping、num_warps、num_stages、索引方式(指针算术运算 vs. 块指针 vs. TMA 描述符)、pid_type、reduction_loops 等。Helion 通过 TorchInductor 将代码降级为 Triton,编译每个候选方案,测量其运行时间,并保留最优者。
Then the winning Triton kernel goes to ptxas, and everything from that point down happens at PTXAS’ default settings. The kernel autotuner’s search space is defined by what the kernel language can express, and no kernel language expresses “allocate registers differently”. This compiler control surface is a separate interface, exposed by ptxas itself starting from CUDA 13.3.
随后,获胜的 Triton 内核交由 ptxas 处理,从此点向下的所有操作均使用 PTXAS 的默认设置。内核自动调优器的搜索空间由内核语言所能表达的内容定义,而没有任何内核语言能够表达“以不同方式分配寄存器”。这种编译器控制接口是一个独立的界面,从 CUDA 13.3 开始由 ptxas 本身提供。
It is imperative to mention that this exploration is genuinely hard. There are complex interactions between various compiler controls: a change to register allocation shifts occupancy, which changes whether a scheduling decision helps or hurts. The right setting is workload-specific, and there’s no universal answer to publish. And the payoff per unit of human effort is poor, which is exactly the profile of a problem one hands off to a search algorithm.
必须强调的是,这种探索确实极具挑战性。各种编译器控制之间存在复杂的交互关系:寄存器分配的更改会影响占用率,进而改变调度决策是有益还是有害。最佳设置因工作负载而异,不存在可发布的通用答案。而且,单位人力投入的收益较低,这恰恰是将问题交给搜索算法处理的典型特征。
The joint search: Helion Autotuner and PTXAS’ Advanced Controls
联合搜索:Helion 自动调优器与 PTXAS 的高级控制
Helion exposes the underlying compiler control space through two settings. Controls are packaged in an Advanced Control File (ACF, typically ending with .acf) which is a binary artifact applied at compile time, portable and versionable, that users can ship alongside their kernel.
Helion 通过两个设置暴露底层编译器控制空间。控制参数被打包在高级控制文件(ACF,通常以 .acf 结尾)中,这是一个在编译时应用的二进制产物,具有可移植性和版本可控性,用户可以随其内核一起分发。
The first setting pins a known-good ACF as part of a config the user has already chosen:
第一个设置将已知的良好 ACF 固定为用户已经选择的配置的一部分:
softmax_tuned = helion.kernel(
config=helion.Config(
advanced_controls_file="h100_example1.acf",
block_sizes=[4],
num_warps=8,
num_stages=2,
reduction_loops=[512],
indexing=["pointer", "pointer", "tensor_descriptor",
"tensor_descriptor", "pointer", "pointer"],
),
static_shapes=True,
)(_softmax)The second is a more interesting case. It hands a list of ACFs to Helion’s own autotuner and lets them be searched as just another dimension, alongside tile sizes and warp counts:
第二个是更有趣的情况。它将一个 ACF 列表交给 Helion 自身的自动调优器,并允许它们作为另一个维度进行搜索,与块大小和线程束数量并列:
softmax_search = helion.kernel(
autotune_search_acf=[
"acfs/helion-booster-pack/candidate_03.acf",
"acfs/helion-booster-pack/candidate_11.acf",
"acfs/helion-booster-pack/candidate_17.acf",
],
autotune_compile_timeout=120,
static_shapes=True,
)(_softmax)Two things are worth pointing out in this mode:
在这种模式下有两点值得注意:
- The empty string is appended for you. Helion automatically adds “”, meaning “no ACF, compiler defaults”, to the list that is supplied. The default configuration is therefore always a candidate in the search, and the autotuner will only select an ACF if it actually won on measured time. The worst case of turning this on is that you spend some autotuning budget and get back the kernel you already had. That property is what makes it tempting to try on a kernel you care about.
- Sequential tuning finds a local optimum; this joint method doesn’t. This is the substantive argument for doing it in one loop rather than two. Suppose you autotune the kernel first, get block_sizes=[128, 128] with num_stages=4, and then go searching for compiler controls. You are now searching the control space conditioned on a kernel config that was itself chosen under default controls. But those dimensions are coupled through the hardware: block size and stage count determine register pressure and shared-memory footprint, and register allocation policy is precisely what decides whether that pressure causes spills. A control set that would shine at block_sizes=[64, 64] never gets evaluated there, because the kernel config was frozen before the compiler search began.The reverse ordering has the same problem. Searching both together is the only ordering that doesn’t have it, which is why autotune_search_acf lives inside Helion’s autotuner rather than being a post-processing step.
- 系统会自动为你追加空字符串。Helion 会自动添加“”,表示“无 ACF,使用编译器默认值”,并将其加入提供的列表中。因此,默认配置始终是搜索中的一个候选项,自动调优器仅在某个 ACF 在实际测量时间上胜出时才会选择它。开启此功能的最坏情况是你花费了一些自动调优预算,最终得到的仍是你原本的内核。正是这一特性使得尝试在你关心的内核上使用它变得诱人。
- 顺序调优只能找到局部最优解;而这种联合方法则不然。这是将其放在一个循环而非两个循环中执行的主要理由。假设你先对内核进行自动调优,得到 block_sizes=[128, 128] 且 num_stages=4,然后再去搜索编译器控制参数。此时你是在基于默认控制参数下选定的内核配置条件下搜索控制空间。但这些维度通过硬件相互耦合:块大小和阶段数决定了寄存器压力和共享内存占用,而寄存器分配策略恰恰决定了这种压力是否会导致溢出。本应在 block_sizes=[64, 64] 下表现优异的某组控制参数从未在此被评估,因为在内核搜索开始前,内核配置已被冻结。反向排序也存在同样的问题。同时搜索两者是唯一能避免该问题的顺序,这也是为什么 autotune_search_acf 位于 Helion 的自动调优器内部,而不是作为一个后处理步骤。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力