跳到主内容
@wquguru
精选88PyTorch 博客(RSS)技巧与观点

TLX.ops:桥接内核研究与实际性能,GEMM提速1.4倍

TLX.ops: Bridging Kernel Research and Real-World Performance

原文
发到 X
推荐理由

硬核工程实践,详细拆解了Blackwell和MI350x上的底层优化技巧,GEMM性能提升显著,做高性能算子开发的同学值得收藏参考。

High-performance GPU kernels are rarely one-size-fits-all. Their optimization strategies are impacted by GPU architectures, tensor shapes and evolving real-world workload demand. Even SOTA kernels optimized for specific cases can be difficult to generalize and keep performant as hardware and workload requirements change.

高性能 GPU 内核很少能一刀切。其优化策略受 GPU 架构、张量形状以及不断演变的实际工作负载需求的影响。即使是针对特定场景优化的 SOTA(最先进)内核,在硬件和工作负载要求发生变化时,也难以泛化并保持高性能。

TLX.ops bridges this gap by turning SOTA, architecture-specific TLX kernels into reusable implementations. Its catalog spans GEMM (including low-precision and grouped variants), attention, MoE, normalization and fused operators. TLX.ops maintains rigorous correctness, performance and compile-time validation against shape lists derived from real-world workloads. In this blog, we provide detailed, first-principles walkthroughs of representative TLX.ops kernels, explaining how architecture capabilities and workload demands shape those kernels. The demonstrated GEMM kernels achieve up to 1.4x speedup over vendor libraries (cuBLAS/hipBLASLt).

TLX.ops 通过将这些 SOTA、架构特定的 TLX 内核转化为可重用的实现来填补这一空白。其目录涵盖 GEMM(包括低精度和分组变体)、注意力机制、MoE(混合专家模型)、归一化和融合算子。TLX.ops 针对源自实际工作负载的形状列表,保持了严格的正确性、性能验证和编译时验证。在本博客中,我们提供了代表性 TLX.ops 内核的详细、基于第一性原理的逐步解析,解释了架构能力和工作负载需求如何塑造这些内核。演示的 GEMM 内核相比厂商库(cuBLAS/hipBLASLt)最高可实现 1.4 倍加速。

TLX.ops serves the following targeted users:

TLX.ops 服务于以下目标用户:

  • For kernel engineers, the organized code base makes it easy to fork an existing kernel, build a variant, or contribute new implementations.
  • For ML engineers, usage is as simple as from triton.tlx.ops import mm as tlx_mm; out = tlx_mm(a, b)
  • For kernel-authoring agents, we will keep the source code symbolic and structurally consistent, making it easier for agents to understand, modify, and generate kernels. We also provide TLX-agents (to be covered in a future post) to enable co-evolution between the kernel library and kernel-authoring agents.
  • 对于内核工程师,结构化的代码库使得 fork 现有内核、构建变体或贡献新实现变得轻而易举。
  • 对于 ML 工程师,使用方式极其简单,例如 from triton.tlx.ops import mm as tlx_mm; out = tlx_mm(a, b)
  • 对于内核编写智能体,我们将保持源代码的符号化和结构一致性,使智能体更容易理解、修改和生成内核。我们还提供 TLX-agents(将在后续文章中介绍),以实现内核库与内核编写智能体之间的协同进化。

Workload-Driven Performance Contract

工作负载驱动的性能契约

Rather than treating benchmark shapes as a static showcase of performance, TLX.ops maintains them as evolving optimization targets. This established an ongoing commitment to correctness, performance, and compile-time quality, guiding kernel optimization, validation, and regression detection.

TLX.ops 不将基准测试形状视为静态的性能展示,而是将其作为不断演进的优化目标。这确立了对正确性、性能和编译时质量的持续承诺,指导内核优化、验证和回归检测。

Kernel Deep Dives: Architecture-Specific Optimizations

内核深度解析:架构特定优化

In this section, we walk through five kernels, four GEMMs and one LayerNorm, targeted at NVIDIA (Hopper/Blackwell) and AMD (MI300x/MI350x) GPUs. More examples are available in the fbtriton repo. Each subsection focuses on first-principles explanations of what/why and a short selection of shape-specific optimizations.

在本节中,我们将逐步介绍五个内核:四个 GEMM 和一个 LayerNorm,针对 NVIDIA(Hopper/Blackwell)和 AMD(MI300x/MI350x)GPU。更多示例可在 fbtriton 仓库中找到。每个小节专注于基于第一性原理解释“是什么/为什么”,并精选一些针对特定形状的优化。

Example 1. NVIDIA Blackwell GEMM

示例 1. NVIDIA Blackwell GEMM

On NVIDIA Blackwell, tcgen05 MMAs execute asynchronously, with accumulators residing in TMEM (256 KB per SM) rather than registers. Separate warps handle operand loading, MMA issuance and accumulator epilogues, creating opportunities for overlap but also synchronization challenges.

在 NVIDIA Blackwell 架构上,tcgen05 MMA 异步执行,累加器位于 TMEM(每个 SM 256 KB)而非寄存器中。不同的 warp 分别负责操作数加载、MMA 发射和累加器后处理,这创造了重叠机会,但也带来了同步挑战。

Architectural Overview

架构概览

In a simple case (1-CTA, SPLIT_K=1, power-of-two BN), each CTA computes one BMxBN output tile. TMA copies A/B tiles from GMEM to an SMEM buffer ring. Tile A is split into MMA_GROUPS row slices and each slice runs an async tcgen05 MMA against the shared B tile into its own FP32 TMEM accumulator. The epilogue moves the accumulator to registers, stages it in SMEM buffers and writes them back with TMA stores.

在简单情况下(1-CTA,SPLIT_K=1,2 的幂次 BN),每个 CTA 计算一个 BM×BN 的输出图块。TMA 将 A/B 图块从 GMEM 复制到 SMEM 缓冲环。图块 A 被拆分为 MMA_GROUPS 行切片,每个切片对共享的 B 图块执行异步 tcgen05 MMA,结果存入各自的 FP32 TMEM 累加器。后处理阶段将累加器移至寄存器,在 SMEM 缓冲中暂存,并通过 TMA 存储写回。

Figure-1: Architectural overview before the optimizations (1-CTA, split-K-free configuration)

图-1:优化前的架构概览(1-CTA,无 split-K 配置)

Asynchronous TMEM Hand-off and CLC scheduling

异步 TMEM 交接与 CLC 调度

The key challenge is to avoid Tensor Core idle time at tile boundaries. Waiting for the final tcgen05 MMAs to complete before notifying the epilogue would drain the MMA pipeline, even though the next tile can accumulate into a different TMEM buffer. Instead, tlx.tcgen05_commit asynchronously signals tmem_full upon MMA completion. With two or more TMEM buffers, the MMA warp then starts the next tile in another buffer. With one buffer, it waits on tmem_empty until the epilogue releases that buffer. Meanwhile, epilogue warps release each TMEM buffer once its last accumulator slice has been read into registers without waiting for the subsequent TMA stores. Cluster launch control (CLC) further reduces tile-boundary gaps by dynamically assigning pending tiles to available clusters. On 2-CTA FP16 1024×12800×1152, asynchronous commit reduced NCU-measured duration by 2.2% (#2008), with CLC scheduling contributing another 2.1% relative to its immediate baseline (#2251).

关键挑战在于避免 Tensor Core 在图块边界处的空闲时间。如果在通知后处理之前等待所有 tcgen05 MMA 完成,即使下一个图块可以累加到不同的 TMEM 缓冲中,也会耗尽 MMA 流水线。相反,tlx.tcgen05_commit 会在 MMA 完成后异步发出 tmem_full 信号。拥有两个或更多 TMEM 缓冲时,MMA warp 随后会在另一个缓冲中启动下一个图块。若只有一个缓冲,则需等待 tmem_empty,直到后处理释放该缓冲。与此同时,后处理 warp 在读取完最后一个累加器切片至寄存器后即释放每个 TMEM 缓冲,无需等待后续的 TMA 存储。集群启动控制(CLC)通过动态将待处理的图块分配给可用集群,进一步减少了图块边界间隙。在 2-CTA FP16 1024×12800×1152 场景下,异步提交使 NCU 测量的持续时间减少了 2.2%(#2008),而 CLC 调度相较于其直接基线又贡献了 2.1% 的提升(#2251)。

Figure-2: Asynchronous TMEM hand-off and CLC scheduling

图-2:异步 TMEM 交接与 CLC 调度

Multi-CTA cooperation

多 CTA 协作

With NUM_CTAS=2, each CTA holds its own A rows, half of B, and its own accumulator, reducing B’s per-CTA SMEM footprint and TMA traffic. tlx.async_descriptor_load(..., two_ctas=True) routes both CTAs’ TMA completion to leader-side barriers. Only CTA 0 issues the cooperative cta_group::2 MMA through tlx.async_dot(..., two_ctas=True). Finally, tlx.tcgen05_commit(..., two_ctas=True) multicasts MMA completion notification to both CTAs, while both epilogues signal the leader’s tmem_empty barrier. The leader-only protocol eliminated per-K-step CTA synchronization, reducing dynamic instructions by 4.2% with approximately unchanged performance (#2291).

当 NUM_CTAS=2 时,每个 CTA 持有其自身的 A 行、B 的一半数据以及各自的累加器,从而减少了 B 在每个 CTA 上的共享内存(SMEM)占用和 TMA 流量。tlx.async_descriptor_load(..., two_ctas=True) 将两个 CTA 的 TMA 完成信号路由至 leader 侧的屏障。仅 CTA 0 通过 tlx.async_dot(..., two_ctas=True) 发起协作式 cta_group::2 MMA。最后,tlx.tcgen05_commit(..., two_ctas=True) 将 MMA 完成通知多播给两个 CTA,同时两个 epilogue 均向 leader 的 tmem_empty 屏障发出信号。这种仅由 leader 处理的协议消除了每 K 步的 CTA 同步开销,在性能基本不变的情况下(#2291),动态指令数减少了 4.2%。

Figure-3: Multi-CTA cooperation

图-3:多 CTA 协作

Deterministic Split-K for Under-Filled, Long-K Outputs

针对填充不足且 K 维度较长的输出的确定性 Split-K

In our workload, 38% of shapes have fewer 128×128 output tiles than SMs, most with K ≥ 100,000, leaving much of the GPU underutilized. Split-K partitions the K dimension into S ranges, creating S × num_mn_tiles independent work items that CLC schedules like ordinary output tiles. Each split writes its FP32 partial result to a separate workspace slab. A second kernel _reduce_k_kernel accumulates these partials in a fixed order and casts to the output dtype, ensuring run-to-run determinism without atomic reductions.

在我们的工作负载中,38% 的形状其输出 tile 数量少于 128×128 且低于 SM 数量,其中大多数 K ≥ 100,000,导致 GPU 大量未充分利用。Split-K 将 K 维度划分为 S 个范围,生成 S × num_mn_tiles 个独立的工作项,CLC 像调度普通输出 tile 一样调度它们。每个 split 将其 FP32 部分结果写入独立的 workspace slab。第二个内核 _reduce_k_kernel 以固定顺序累积这些部分结果并转换为输出数据类型,确保运行间确定性,无需原子归约操作。

代码 · 8 行
# Simplified from _process_tile_epilogue_inner and _reduce_k_kernel
split_id = tile_id // num_mn_tiles
row_base = split_id * num_pid_m * BLOCK_SIZE_M
# Store FP32 partial to the split's workspace slab
# Reduction kernel: fixed accumulation order
for s in range(SPLIT_K):
    acc += tl.load(workspace_ptr + base_offs + s * ROWS_PER_SPLIT * N, ...)
tl.store(c_ptr + base_offs, acc.to(OUTPUT_DTYPE), ...)

Configurations are heuristic picks from get_heuristic_config, computed on the CPU without launching kernels; the default space="full" autotunes each shape, so its measured winners may differ. Split-K increases parallelism for small outputs with long reduction dimensions, while 2-CTA configurations are favored for larger outputs. The 148-SM line marks a reference underutilization threshold for 128×128 tiles.

配置参数是从 get_heuristic_config 中启发式选取的,在 CPU 上计算且不启动内核;默认 space="full" 会对每种形状进行自动调优,因此其测得的获胜配置可能有所不同。Split-K 增加了具有长归约维度但较小输出的并行度,而较大输出则倾向于使用 2-CTA 配置。148-SM 线标记了 128×128 tile 的参考未利用率阈值。

Figure-4: Workload-driven configuration selection across 422 BF16 GEMM shapes

图-4:基于工作负载的配置选择,涵盖 422 种 BF16 GEMM 形状

Examples of Other Shape-Specific Optimizations

其他特定形状优化的示例

  • Very tall outputs (M>>N): For example 32768×1024×1152. For 2-CTA clusters, pruning retains GROUP_SIZE_M=NUM_CTAS=2, so consecutive CTA pairs traverse the few N blocks while retaining the same two M blocks. This favors A reuse across nearby tiles while the small B remains L2-resident. (#958, #2294).
  • Col-major operands: A is col-major in 200 out of 422 shapes, the TMA descriptor uses the transposed view and tlx.local_trans feeds the MMA without a copy (#1116).
  • Tiny split-K outputs: (256×256×2433024) The reduction kernel uses a smaller 32×32 (instead of 128×128) block, so a 256×256 output launches 64 reduction CTAs instead of 4. (#1118)
  • Tall, memory-bound outputs: For example 589824×2048×512. For GPU-saturated shapes with M/N > 4 and K ≤ 1.5·min(M,N), the heuristic favors narrower 256×128 tiles. Their smaller accumulators permit two TMEM buffers instead of one, overlapping the next tile’s MMAs with the current epilogue within the 256 KiB TMEM budget per SM (#960)
  • One MMA group: With NUM_MMA_GROUPS=1, each MMA step consumes exactly one A tile and one B tile, so A and B share a single transaction-byte-counted full barrier per SMEM slot by having the producer set the combined byte count. (#2275)
  • Non-power-of-two N: Split a NPOT tile into POT pieces (for example, 208=128+64+16), each with its own B ring, accumulator and MMA. Adjacent piece MMAs also share operand A in the TensorCore collector buffer. (#4161)
  • 极高输出(M >> N):例如 32768×1024×1152。对于 2-CTA 集群,剪枝保留 GROUP_SIZE_M=NUM_CTAS=2,因此连续的 CTA 对遍历最少的 N 块,同时保持相同的两个 M 块。这有利于邻近 tile 间的 A 复用,而较小的 B 保持在 L2 缓存中。(#958, #2294)。
  • 列主序操作数:在 422 种形状中有 200 种的 A 为列主序,TMA 描述符使用转置视图,tlx.local_trans 在不复制的情况下为 MMA 提供数据(#1116)。
  • 极小的 Split-K 输出:(256×256×2433024)。归约内核使用更小的 32×32(而非 128×128)block,因此 256×256 的输出会启动 64 个归约 CTA,而不是 4 个。(#1118)
  • 高且受内存带宽限制的输出:例如 589824×2048×512。对于 GPU 饱和形状,当 M/N > 4 且 K ≤ 1.5·min(M,N) 时,启发式算法倾向于使用更窄的 256×128 图块。其较小的累加器允许使用两个 TMEM 缓冲区而非一个,从而在每 SM 256 KiB 的 TMEM 预算内,将下一个图块的 MMA 与当前尾过程的执行重叠 (#960)
  • 单个 MMA 组:当 NUM_MMA_GROUPS=1 时,每个 MMA 步骤恰好消耗一个 A 图块和一个 B 图块,因此 A 和 B 通过生产者设置组合字节计数,在每个 SMEM 槽位共享一个按事务字节计数的完整屏障 (#2275)
  • 非 2 的幂次 N:将一个 NPOT(非 2 的幂次)图块拆分为 POT(2 的幂次)片段(例如,208=128+64+16),每个片段拥有独立的 B 环、累加器和 MMA。相邻片段的 MMA 也在 TensorCore 收集器缓冲区中共享操作数 A (#4161)

Example 2. AMD MI350x GEMM MXFP8

示例 2. AMD MI350x GEMM MXFP8

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →

相似阅读

关联信息,但可能不是同一事件