PyTorch通过FlyDSL加速AMD MI350 GPU推理性能提升显著
Accelerating PyTorch on AMD GPUs with FlyDSL
AMD MI350生态的关键性能突破,实测推理加速超3倍,做国产算力替代或AMD部署的同学值得重点关注。
PyTorch users on AMD MI350-series GPUs can now use FlyDSL through existing operator APIs for dense and grouped GEMM, fused multi-head attention (FMHA/Attention), MXFP8/MXFP4 scaled GEMM, RMSNorm, and TopK, without requiring applications to call custom kernels. The optional backend supports GEMMs through torch.compile, while eligible RMSNorm and TopK calls dispatch automatically in eager mode when it is available and enabled. Unsupported inputs retain existing PyTorch implementations. FlyDSL attention kernels ship as an additional backend in AOTriton, the library behind PyTorch’s ROCm F.scaled_dot_product_attention (SDPA), starting with the 0.14b series.
AMD MI350系列GPU上的PyTorch用户现在可以通过现有的密集和分组GEMM、融合多头注意力(FMHA/Attention)、MXFP8/MXFP4缩放GEMM、RMSNorm和TopK算子API使用FlyDSL,而无需应用程序调用自定义内核。可选后端通过torch.compile支持GEMM,而在eager模式下,当可用且启用时,符合条件的RMSNorm和TopK调用会自动分发。不支持的输入保留现有的PyTorch实现。从0.14b系列开始,FlyDSL注意力内核作为AOTriton中的附加后端发布,AOTriton是PyTorch ROCm F.scaled_dot_product_attention (SDPA)背后的库。
Across the reported kernel-level operator suites, FlyDSL provides a 1.10x geometric-mean speedup for the 15-shape BF16 dense-GEMM suite over the faster of ATen and Triton at each shape, 1.58x and 1.68x geometric-mean speedups for MXFP8 and MXFP4, respectively, over ATen across 17 shapes per format, 1.20x geometric mean over Triton across all 24 grouped-GEMM cases, and 1.17x–3.66x over ATen across the 22 RMSNorm cases. For the 10 small-K TopK cases, the geometric mean is 4.82x over ATen with deterministic algorithms disabled. These gains are most relevant when the supported operations account for a significant share of model runtime. For attention, AOTriton 0.14.2b’s published gfx950 throughput ranges from 0.92x to 2.34x of 0.13b for forward and 0.93x to 2.47x for backward, across head dimensions and causal modes. In end-to-end vLLM A/B tests, whole-request speedup reaches 1.13x for BF16, 2.04x for MXFP8, and 3.01x for MXFP4, with the result depending on model and concurrency.
在报告的核级算子套件中,FlyDSL为15种形状的BF16密集GEMM套件提供了1.10倍的几何平均加速比,相较于每种形状下更快的ATen和Triton;对于MXFP8和MXFP4,分别在每种格式的17种形状上提供1.58倍和1.68倍的几何平均加速比,相较于ATen;在所有24个分组GEMM案例中,相较于Triton提供1.20倍的几何平均加速比;在22个RMSNorm案例中,相较于ATen提供1.17x–3.66倍的加速比。对于10个小K值的TopK案例,禁用确定性算法时,几何平均加速比为4.82倍。这些增益在支持的操作占模型运行时间的显著部分时最为相关。对于注意力机制,AOTriton 0.14.2b发布的gfx950吞吐量范围在正向传播时为0.92x到2.34x,反向传播时为0.93x到2.47x(相对于0.13b),涵盖不同的头维度和因果模式。在端到端vLLM A/B测试中,整体请求加速比达到BF16的1.13倍、MXFP8的2.04倍和MXFP4的3.01倍,结果取决于模型和并发量。
Why FlyDSL as a PyTorch Backend
为什么选择FlyDSL作为PyTorch后端
GPU DSLs choose different abstraction levels. The block-tensor programming model in Triton lets authors describe program-level tiles while the compiler manages much of the thread, wave, and instruction mapping. FlyDSL makes those mappings explicit through layout algebra: kernel authors can partition tiles across blocks, waves, threads, and values; select tiled-copy and MFMA atoms; define LDS swizzles and staging; and place synchronization or compiler-scheduling boundaries. The two approaches are complementary. FlyDSL is useful for architecture-specific templates where data movement and instruction ownership are part of the algorithm.
GPU DSL选择不同的抽象级别。Triton中的块张量编程模型允许作者描述程序级别的图块,而编译器管理大部分线程、波和指令映射。FlyDSL通过布局代数使这些映射显式化:内核作者可以将图块跨块、波、线程和值进行分区;选择分块复制和MFMA原子;定义LDS交错和暂存;并放置同步或编译器调度边界。这两种方法是互补的。FlyDSL适用于架构特定的模板,其中数据移动和指令所有权是算法的一部分。
These controls appear directly in the kernels: HTI and staged LDS in Dense/MXFP GEMM, matched operand-scale lifetimes and scaled MFMA in MXFP, and persistent expert scheduling in Grouped GEMM. FlyDSL expresses them in Python and lowers them through MLIR while preserving explicit hardware mapping.
这些控制直接出现在内核中:Dense/MXFP GEMM 中的 HTI 和分阶段 LDS,MXFP 中的匹配操作数比例生命周期和缩放 MFMA,以及 Grouped GEMM 中的持久化专家调度。FlyDSL 使用 Python 表达它们,并通过 MLIR 进行降级,同时保留明确的硬件映射。
Attention shows why this control matters. The FlyDSL attention kernels use a dual-wave software pipeline, which overlaps the softmax VALU work of one KV tile with the MFMA work of another. At the largest head dimensions, they also split the head dimension: in time through LDS, and across waves for the output accumulator. In FlyDSL, both schedules are written in the kernel itself, so they can be developed and tuned per architecture without changes to the compiler.
注意力机制展示了这种控制的重要性。FlyDSL 注意力内核采用双波软件流水线,将一个 KV tile 的 softmax VALU 工作与另一个的 MFMA 工作重叠。在最大的头维度下,它们还会分割头维度:通过 LDS 在时间上分割,并在输出累加器上跨波分割。在 FlyDSL 中,这两种调度都写在内核本身中,因此可以针对每种架构独立开发和调优,而无需更改编译器。
PyTorch turns those specialized templates into an additive, measurable backend. Unsupported workloads retain existing implementations:
PyTorch 将这些专用模板转化为一个可叠加、可测量的后端。不受支持的工作负载保留现有实现:
- Eager execution: eligible RMSNorm and TopK calls dispatch to FlyDSL when the optional runtime is installed and enabled; other calls retain ATen.
- torch.compile: TorchInductor filters eligible Dense, Grouped, and MXFP GEMM candidates, benchmarks FlyDSL beside ATen and Triton where supported, caches the winner for each workload, and runs the fastest measured kernel.
- Multi-head attention: PyTorch’s built-in operator F.scaled_dot_product_attention calls AOTriton, which now picks among FlyDSL, Triton, and AITER ASM kernels for each workload using its offline operator-tuning database.
- Eager 执行:符合条件的 RMSNorm 和 TopK 调用在安装并启用可选运行时后分发到 FlyDSL;其他调用保留 ATen。
- torch.compile:TorchInductor 筛选符合条件的 Dense、Grouped 和 MXFP GEMM 候选项,在支持的情况下将 FlyDSL 与 ATen 和 Triton 进行基准测试,为每个工作负载缓存获胜者,并运行测量最快的内核。
- 多头注意力:PyTorch 内置算子 F.scaled_dot_product_attention 调用 AOTriton,后者现在利用其离线算子调优数据库,为每个工作负载在 FlyDSL、Triton 和 AITER ASM 内核之间进行选择。
Figure 1. FlyDSL kernels reached through PyTorch APIs. Eager RMSNorm and TopK dispatch by input support; AOTriton selects a tuned SDPA implementation; and TorchInductor benchmarks eligible GEMM candidates.
图 1. 通过 PyTorch API 访问的 FlyDSL 内核。Eager RMSNorm 和 TopK 根据输入支持进行分发;AOTriton 选择经过调优的 SDPA 实现;TorchInductor 对符合条件的 GEMM 候选项进行基准测试。
Supported Features
支持的功能
Current support targets AMD MI350-series GPUs with the gfx950 architecture. The following features are available in PyTorch:
当前支持针对具有 gfx950 架构的 AMD MI350 系列 GPU。以下功能在 PyTorch 中可用:
| Operation | Mode | PyTorch API | Input dtype | Layout / shape | Output / options |
|---|---|---|---|---|---|
| Dense GEMM | Compile | torch.mm | FP16, BF16 | Static 2D; NN/NT/TN/TT | Same dtype |
| MXFP scaled GEMM | Compile | F.scaled_mm | MXFP8, MXFP4 | Static 2D; A row-major, B column-major | FP16/BF16; block scales |
| Grouped GEMM | Compile | F.grouped_mm | FP16, BF16 | Ragged 2D A; grouped 3D B | Uneven/empty groups |
| RMSNorm | Eager | F.rms_norm | FP16, BF16, FP32 | Contiguous; 1-D norm shape | Forward |
| TopK | Eager | torch.topk | FP32 | Contiguous; last dimension | Largest, sorted; functional/out= |
| Attention (via AOTriton) | Eager, compile | F.scaled_dot_product_attention | FP16, BF16 | D innermost; head dim 32–512 | Forward and backward |
| 操作 | 模式 | PyTorch API | 输入数据类型 | 布局/形状 | 输出/选项 |
|---|---|---|---|---|---|
| 密集 GEMM | 编译 | torch.mm | FP16、BF16 | 静态 2D;NN/NT/TN/TT | 相同数据类型 |
| MXFP 缩放 GEMM | 编译 | F.scaled_mm | MXFP8、MXFP4 | 静态 2D;A 为行优先,B 为列优先 | FP16/BF16;块缩放因子 |
| 分组 GEMM | 编译 | F.grouped_mm | FP16、BF16 | 不规则 2D A;分组 3D B | 不均匀/空分组 |
| RMSNorm | 急切执行 | F.rms_norm | FP16、BF16、FP32 | 连续;1-D 归一化形状 | 前向传播 |
| TopK | 急切执行 | torch.topk | FP32 | 连续;最后一维 | 最大值,已排序;函数式/out= |
| 注意力机制(通过 AOTriton) | 急切执行、编译 | F.scaled_dot_product_attention | FP16、BF16 | D 在最内层;头维度 32–512 | 前向和反向传播 |
Dense GEMM’s four-layout support accepts row-major and column-major inputs, including eligible transpose views. In the layout names, the first letter describes A and the second describes B; N and T denote non-transposed and transposed input layouts. Grouped GEMM supports different row counts per group, allowing experts to process uneven token assignments. Its current input-layout requirements are separate from dense GEMM’s four-layout coverage. RMSNorm backward continues through PyTorch’s existing implementation.
密集 GEMM 的四种布局支持接受行优先和列优先输入,包括符合条件的转置视图。在布局名称中,第一个字母描述 A,第二个字母描述 B;N 和 T 分别表示未转置和转置的输入布局。分组 GEMM 支持每个组不同的行数,允许专家处理不均匀的令牌分配。其当前的输入布局要求与密集 GEMM 的四种布局覆盖范围是分开的。RMSNorm 的反向传播继续通过 PyTorch 现有的实现进行。
MXFP scaled GEMM accepts FP8 E4M3 or packed FP4 E2M1 operands, with one E8M0 scale per 32 values along the reduction dimension (BlockWise1x32). Applications supply the quantized operands and contiguous, unswizzled scale tensors. Both formats use FP32 accumulation and return FP16 or BF16; the current path requires K to be a multiple of 128. It supports an optional one-dimensional bias whose length is N and whose dtype matches the output; fast accumulation is not supported. Under torch.compile, Inductor’s layout constraint runs before backend selection and requires unit stride along K for A and B—row-major A and column-major B—while preserving compatible leading strides. For incompatible inputs, Inductor inserts a layout copy. The FlyDSL MXFP kernel and the measurements below use these constrained layouts. This constraint is specific to scaled GEMM: Dense torch.mm detects eligible NN/NT/TN/TT strides and passes their layout flags to FlyDSL without applying the MXFP canonicalization.
MXFP 缩放 GEMM 接受 FP8 E4M3 或打包的 FP4 E2M1 操作数,沿归约维度每 32 个值有一个 E8M0 缩放因子(BlockWise1x32)。应用程序提供量化操作数和连续的、未交织的缩放张量。两种格式均使用 FP32 累加并返回 FP16 或 BF16;当前路径要求 K 是 128 的倍数。它支持一个可选的一维偏置,其长度为 N,且数据类型与输出匹配;不支持快速累加。在 torch.compile 下,Inductor 的布局约束在后端选择之前运行,并要求 A 和 B 沿 K 具有单位步幅——即行优先 A 和列优先 B——同时保留兼容的前导步幅。对于不兼容的输入,Inductor 会插入布局副本。FlyDSL MXFP 内核和下面的测量值使用这些受约束的布局。此约束特定于缩放 GEMM:密集的 torch.mm 检测符合条件的 NN/NT/TN/TT 步幅,并将它们的布局标志传递给 FlyDSL,而不应用 MXFP 规范化。
The FlyDSL attention kernels match the flash and memory-efficient attention capabilities of F.scaled_dot_product_attention, with one current limit: FP32 is not supported. FP32 inputs, and workloads that the tuning database assigns elsewhere, keep AOTriton’s Triton or AITER ASM kernels. AOTriton also ships FlyDSL attention kernels for RDNA4 (gfx1201). Under torch.compile, Inductor keeps SDPA’s flash and memory-efficient attention operators as calls to ATen rather than generating code for them, so they still reach the AOTriton kernels. Hand-written matmul–softmax–matmul patterns that Inductor rewrites into SDPA follow the same path. By contrast, torch.nn.attention.flex_attention is a separate API that Inductor compiles into its own Triton kernels; it does not use AOTriton.
FlyDSL attention 内核具备与 F.scaled_dot_product_attention 相同的 flash 和内存高效注意力能力,但存在一个当前限制:不支持 FP32。FP32 输入以及调优数据库将其分配至其他位置的负载,仍由 AOTriton 的 Triton 或 AITER ASM 内核处理。AOTriton 还为 RDNA4 (gfx1201) 提供了 FlyDSL attention 内核。在 torch.compile 下,Inductor 将 SDPA 的 flash 和内存高效注意力算子保留为对 ATen 的调用,而非为其生成代码,因此它们仍能调用 AOTriton 内核。Inductor 重写为 SDPA 的手写 matmul–softmax–matmul 模式遵循相同的路径。相比之下,torch.nn.attention.flex_attention 是一个独立的 API,Inductor 会将其编译为自己的 Triton 内核;它不使用 AOTriton。
Each operation has shape and alignment requirements. The detailed dense GEMM, MXFP scaled GEMM, grouped GEMM, RMSNorm, and TopK support descriptions define those boundaries. Unsupported eager calls retain ATen behavior; compiled GEMMs can use the other enabled backends. The AOTriton 0.14.2b update brings these kernels to PyTorch.
每个操作都有形状和对齐要求。详细的密集 GEMM、MXFP 缩放 GEMM、分组 GEMM、RMSNorm 和 TopK 支持说明定义了这些边界。不受支持的 eager 调用保留 ATen 行为;已编译的 GEMM 可以使用其他启用的后端。AOTriton 0.14.2b 更新将这些内核引入 PyTorch。
Kernel-Level Performance
内核级性能
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力