Mixture-of-Experts kernel engineering on Amazon SageMaker AI
https://aws.amazon.com/blogs/machine-learning/mixture-of-experts-kernel-engineering-on-amazon-sagemaker-ai/📌 【AWS 實測】Hopper GPU 上,把矩陣轉置一下就能讓 MoE 快 2-4%
TL;DR:AWS 拆解 MoE fused kernel 在 Hopper 架構上的瓶頸,靠 swap-AB 轉置與後端選型拿到實測效能提升。
一行矩陣轉置,能不能讓一整個 MoE 推理 pipeline 變快?AWS 這篇文章給出的答案是:在 Hopper GPU 上,答案是可以,而且背後的原因全部來自 WGMMA 指令集的硬體限制,而不是演算法本身。
🤔 decode 階段的 MoE,天生就和 Hopper 的指令集對不上
NVIDIA Hopper 架構靠 WGMMA(Warp Group Matrix Multiply Accumulate)指令來跑矩陣乘法,這個指令由 128 個執行緒組成的 warpgroup 協同執行。問題在於它的操作數不對稱:矩陣列維度(M)在硬體層級被固定成 64,行維度(N)則可變。Operand A 必須對齊這個固定的 M=64,Operand B 才對齊可變的 N。這個限制是 ISA 層級的硬體事實,不是軟體可以繞過的邏輯。
在標準 MoE 層的矩陣乘法中,M 是 token 數(batch size),K 是 hidden dimension,N 是專家輸出維度。但在 decode 階段,系統一次只處理每個使用者序列的一個 token,所以 M 經常小到 1。這代表 Operand A 變成一個極薄的橫向向量(例如 1×4096),根本填不滿硬體強制要求的 64 列,WGMMA 執行時,大量 Tensor Core 的運算單元就處於「空轉」狀態。
更麻煩的是,fused MoE kernel 為了避免替每個專家分別啟動 kernel,會把所有被選中的專家一起分組運算,這讓有效的 N 維度(專家數 × 專家 intermediate size)在 top-k=8 的情境下膨脹到數萬甚至數十萬。M 小到 1、N 大到誇張,這個層級的不對稱比一般 dense layer 嚴重得多。
🧩 把矩陣轉置,讓大維度去對齊硬體的「大列」
AWS 的做法是利用矩陣轉置的代數性質,在進入 PTX assembly 層之前,把矩陣乘法反過來:B^T [N,K] × A^T [K,M] → C^T [N,M]。這個「Swap-AB」的動作把原本龐大的專家輸出維度 N(例如 8192)挪到第一個 operand 位置,剛好輕鬆填滿硬體強制的 64 列門檻,讓 Tensor Core 不再空轉。同時,因為大型、連續的權重區塊被放進了主要插槽,Hopper 的硬體拷貝引擎可以非同步地把資料從 shared memory 預取進暫存器,讓運算管線保持飽和,不需要 CPU 介入排程。
📊 H200 上端到端輸出吞吐量提升 2-4%
AWS 表示,這個優化在大規模 MoE 模型上,於 H200 實測帶來 2-4% 的端到端輸出吞吐量提升,並已經把這個優化上游貢獻回 vLLM,從 0.24.0 版開始可用。
💡 後端選擇一樣重要:低併發下 Triton 比 CUTLASS 快
除了 kernel 本身的排列方式,vLLM 內建的 MoE oracle 會依量化方案把運算路由到 Triton、FlashInfer+CUTLASS 或其他後端。AWS 的 profiling 發現,對 Hopper 上採用 FP8 tensor 量化的模型,Triton 後端在低併發情境下持續勝過預設的 FLASHINFER_CUTLASS 路徑。原因在於 CUTLASS 仰賴標準 C++ template,把 FP8 scaling、矩陣乘法、專家彙總拆成多個獨立數學運算,過程中需要反覆讀寫 HBM;Triton 後端則把整條 pipeline 合併成單一硬體 pass,資料只從 HBM 讀取一次,全程留在 GPU 的 SRAM 暫存器與運算檔案裡處理完才寫回,省掉拖慢 CUTLASS 的來回搬資料開銷,還搭配針對可變 token bucketing 設計的動態排程。
⚠️ 優化高度綁定硬體世代
這整套優化的前提是 Hopper 的 WGMMA 固定 M=64 這個具體硬體事實,換到不同世代或不同廠商的架構,瓶頸位置與對應解法未必相同,需要重新做硬體層級的分析才能套用。
🎯 如果你在跑 MoE 推理服務
直接的行動項目很具體:確認你用的 vLLM 版本是否 ≥0.24.0 以吃到 Swap-AB 優化;如果你的 workload 是 FP8 量化且併發量不高,值得手動測試把 MoE 後端切到 Triton 是否比預設的 FlashInfer+CUTLASS 更快,而不是完全信任框架預設的路由決策。
🔗 來源
- 標題:Mixture-of-Experts kernel engineering on Amazon SageMaker AI
- 作者/機構:Mona Mona(AWS Machine Learning Blog)
- 連結:https://aws.amazon.com/blogs/machine-learning/mixture-of-experts-kernel-engineering-on-amazon-sagemaker-ai/
#MixtureOfExperts #GPUKernel #vLLM #NvidiaHopper #WGMMA #InferenceOptimization #CUDA #Triton #LLMInference #AWS
原始資料 AWS ML · 收集於 2026-10-13
摘要原文
Mixture-of-Experts (MoE) is a sparsely-activated architecture that scales large language model (LLM) capacity while keeping per-token compute low. Because its operations account for a large share of total compute time, inefficiencies there directly raise request latency and cost. Therefore, optimizing MoE operations through kernel engineering offers the largest opportunity for performance gains. Fused MoE is a key optimization technique for the MoE architecture. A fused MoE system merges token routing, data shuffling, and specialized network calculations into a single batched operation on the GPU. This replaces executing those routing and expert computations as separate, sequential loop operations. While this provides latency advantages, it also brings complexity in kernel implementations and debugging capability. High GPU utilization for this architecture requires hardware-specific optimizations. In this post, we describe the different kernel optimizations we implemented for fused MoE models and show you how you can benefit from this work. We used a representative MoE model as our primary example. To maximize tensor processing throughput, the NVIDIA Hopper architecture relies on the Warp Group Matrix Multiply Accumulate (WGMMA) instruction protocol, which executes collectively across a cooperative warpgroup of 128 threads. This operand asymmetry is fundamentally an instruction set architecture (ISA)-level property. In the underlying wgmma.mma_async assembly definition, the hardware fixes the matrix row dimension (M) at a constant scale of 64 to define its parallel thread-mapping topology. The column dimension (N) remains highly variable. Operand A strictly maps across this fixed M dimension (using registers or shared memory), and Operand B maps across the variable N dimension (through a shared memory descriptor). These operand slots are therefore structurally non-interchangeable. Consequently, peak hardware tile utilization is governed by these rigid shape encodings, requiring that the M dimension equals or exceeds 64 to prevent compute execution lanes from sitting idle. This structural preference stems from how Hopper Tensor Cores partition memory blocks. The hardware enforces a fixed minimum layout shape for the leading dimension of the first operand, locking the block height to exactly 64 (M=64). When a mathematical matrix possesses a large, continuous leading dimension in this primary slot, Hopper’s internal hardware schedulers can cleanly map and distribute data across the physical computing grid. This structural asymmetry becomes a severe performance bottleneck during the decode phase of MoE networks. In a standard MoE layer, the linear algebra governing expert computation is formulated using traditional matrix notation: Where (M) represents the token row count (or batch size), (K) represents the hidden feature dimension, and (N) represents the expansive expert output dimension. During the decode phase, the system processes only one token per user sequence at a time. Consequently, the batch space or token row size is profoundly thin, regularly dropping to a profile where (M < 64), and frequently down to a single row (M = 1). Under this standard structural layout: Matrix (A) transforms into a thin, fragmented horizontal vector (for example, 1 x 4096). Matrix (B) (the core expert weights) remains in a large, dense vertical block (for example, 4096 x 8192). Because the row count (M) is tiny, Matrix (A) is physically incapable of filling the hardware’s fixed (64)-row layout bucket for Operand A. When the WGMMA instruction executes over this layout, it leaves a vast portion of the physical Tensor Core capacity completely starved. The silicon spends clock cycles processing empty spatial slots, leading to poor hardware utilization. In a standard dense LLM layer, weight matrices are static, unified, and perfectly contiguous. In an MoE layer, however, the workload is fractured across a fleet of independent subnetworks (experts). This creates unique memory and layout bottlenecks that make it hypersensitive to the WGMMA constraints of the Hopper GPU. In a standard dense layer, matrix multiplication multiplies a token vector by a single layer weight matrix. The output dimension (N) is the model’s hidden size (for example, 4096). In a Fused MoE kernel, the GPU isn’t only multiplying against one expert. It’s grouping and running math for all active experts simultaneously to avoid launching individual kernels for each expert. For a standard dense layer, N is the hidden size (4,096). For a fused MoE layer, N is the number of experts chosen multiplied by the expert intermediate size. For a model running a topk=8 routine, the effective N dimension inside the fused kernel explodes into tens or hundreds of thousands of parameters. Because the N dimension is so disproportionately large compared to the tiny decode token size (M=1), the layout asymmetry becomes extreme. Transposing the matrices moves this large, combined MoE dimension into the primary slot, unlocking hardware prefetching at a scale that standard dense layers cannot match. To resolve this hardware misalignment without altering the underlying mathematical integrity of the model, we exploit the algebraic property of matrix transposition: B T [N,K] x A T [K,M] —> C T [N,M] Figure 1: Swap-AB for WGMMA utilization By programmatically flipping the matrix multiplication before it reaches the PTX assembly layer, we realigned the software to match the exact topography of the Hopper silicon. This transposition triggers two immediate architectural advantages on the GPU. First, it saturates the physical layout. The expansive expert dimension (N = 8192) is promoted to the first operand position (B T ), and because (N) is large it easily floods the hardware’s mandatory (64)-row threshold. The Tensor Cores then operate at maximum local density with no wasted execution cycles. Second, it activates asynchronous prefetching. Hopper’s dedicated hardware copy engines can prefetch memory addresses asynchronously from shared memory straight into registers before the math execution block fires. Placing the continuous, large weight blocks (B T ) in the leading slot lets these engines trace the data layout smoothly and keep the compute pipeline saturated without CPU intervention. This produced a 2–4 percent end-to-end output throughput improvement across large-scale MoE models on H200. We upstreamed this optimization to vLLM, and it is available in version 0.24.0 or later. Beyond kernel internals, the choice of which backend executes the MoE computation has a large effect on performance. The vLLM MoE oracle routes different quantization schemes to Triton, FlashInfer+CUTLASS, or other backends. Profiling revealed that for FP8 tensor-quantized models on Hopper, the Triton backend consistently outperforms the default FLASHINFER_CUTLASS path. The Triton backend outperforms CUTLASS in low-concurrency MoE workloads primarily because it replaces rigid, framework-level matrix math configurations with custom, operator-level register fusion. CUTLASS relies on standard C++ template structures that treat FP8 tensor scaling, matrix multiplication, and expert aggregation as discrete mathematical operations. The Triton backend instead consolidates this entire pipeline into a single hardware pass. Data is pulled from global high-bandwidth memory (HBM) exactly once and processed entirely within the GPU’s ultra-fast local SRAM registers and compute execution files. It’s then streamed back to the final matrix destination. This removes the slow global VRAM read/write roundtrips that compromise CUTLASS when it is forced to constantly step through intermediate tensor scaling steps. Furthermore, this register-level fusion is specifically paired with dynamic scheduling layouts designed around variable token bucketing, resolving a severe structural flaw in traditional compilation templates.
由 claude-code/sonnet 自動生成