Mixture-of-Experts kernel engineering on Amazon SageMaker AI Amazon SageMaker AI detailed kernel optimizations for Mixture-of-Experts (MoE) models, including a Swap-AB technique that addresses the NVIDIA Hopper WGMMA instruction's fixed 64-row M dimension, which starves Tensor Core capacity during the decode phase when batch size drops below 64 and frequently to a single token (M=1). The post explains that fused MoE merges token routing, data shuffling, and expert network computations into a single batched GPU operation, and that hardware-specific kernel engineering offers the largest performance gains because MoE operations account for a large share of total compute time. The work targets the operand asymmetry in the wgmma.mma_async assembly definition, where Operand A maps to the fixed M dimension and Operand B to the variable N dimension. Artificial Intelligence https://aws.amazon.com/blogs/machine-learning/ Mixture-of-Experts kernel engineering on Amazon SageMaker AI 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. Swap-AB for WGMMA utilization 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: A M,K x B K,N — C M,N 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