Implementing block-sparse matrix multiplication kernels using Triton

Priya Mishra, Trevor Gale, Matei Zaharia, Cliff Young, Deepak NarayananES-FoMO @ ICML 2023

TL;DR

This paper tackles the performance bottleneck in MegaBlocks, a leading system for training Mixture-of-Experts (MoE) models, by rewriting its highly specialized CUDA kernels in Triton. The authors demonstrate that Triton can achieve comparable performance to hand-tuned CUDA while offering significant improvements in code compactness, portability, and flexibility, removing previous restrictions on block size, data type, and GPU architecture.

What Problem Does This Paper Address?

Training large MoE models, which dynamically route data to specialized "experts," requires efficient matrix multiplication. MegaBlocks is a state-of-the-art system that optimizes these computations using block-sparse matrix multiplication. However, MegaBlocks' performance relies on hand-tuned CUDA kernels that are painstakingly optimized for a very specific set of parameters: a block size of 128, FP16 data type, and Ampere GPU architecture. This specificity makes it difficult to adapt MegaBlocks to different hardware, data types, or model configurations without extensive, non-trivial kernel rewrites. This severely limits the system's flexibility and broad applicability for MoE research and deployment.

Methods

The core of this work is the reimplementation of MegaBlocks' block-sparse matrix multiplication kernels using Triton. Triton is a Python-embedded domain-specific language (DSL) designed for writing high-performance GPU kernels. The authors leverage Triton's higher-level abstractions to simplify kernel development. They focus on three key matrix multiplication operations: SDD (sparse output, dense inputs), DSD (dense output, sparse input), and DDS (dense output, sparse input).

Key aspects of their approach include:

  • Triton DSL: Utilizing Triton's Pythonic syntax to express GPU computations, abstracting away low-level CUDA details like shared memory management and synchronization.
  • Hybrid Blocked CSR-COO Encoding: Continuing to use MegaBlocks' existing sparse matrix encoding, which efficiently handles sparse matrix transpositions.
  • Kernel Restructuring: Experimenting with different loop structures and kernel formulations to trigger crucial optimizations like software pipelining, which was initially a performance bottleneck.
  • Debugging with PTX: Relying on the generated PTX (Parallel Thread Execution) assembly code to understand Triton's compilation process and debug performance issues, as Triton's documentation and debugging tools were still under development.

Datasets

The paper does not explicitly mention training or evaluating on specific large-scale datasets for MoE models. Instead, it focuses on benchmarking the performance of the block-sparse matrix multiplication kernels themselves using synthetic matrix operations and varying matrix sizes (sequence lengths). The "datasets" in this context are the benchmark configurations for these matrix operations.

Key Insights and Ideas

  • Triton's Power for High-Performance Kernels: The paper strongly suggests that Triton is a viable and powerful alternative to hand-tuned CUDA for complex GPU kernels, offering a compelling balance between performance and developer productivity.
  • The Trade-off in Kernel Optimization: Hand-tuned CUDA kernels achieve peak performance by exploiting specific hardware features and parameter combinations. However, this comes at the cost of extreme specialization and difficulty in generalization. Triton offers a path to achieve near-peak performance with significantly less effort and much greater flexibility.
  • Active Development Challenges: The authors highlight that Triton, while promising, is still under active development. This means developers might encounter unexpected behaviors, limited documentation, and the need for deep dives into compiler internals (like PTX) to achieve optimal performance.
  • Software Pipelining is Crucial: The initial performance gap between Triton and CUDA was largely due to missing software pipelining for indirect memory accesses. This underscores the importance of this optimization for efficient data loading in sparse computations.

Key Results

  • Performance Parity: The Triton-based MegaBlocks kernels (MegaBlocks-Triton) achieve performance comparable to the original hand-tuned CUDA kernels (MegaBlocks-CUDA), with throughput ranging from 0.96x to 1.1x.
  • Code Compactness: MegaBlocks-Triton has a dramatically smaller codebase, with 10x fewer lines of code (298 SLOC) compared to MegaBlocks-CUDA (3139 SLOC).
  • Portability and Generality: The Triton kernels are portable across different GPU architectures and, crucially, support arbitrary block sizes and data types (like bfloat16) without requiring any code changes. This removes the previous limitations of MegaBlocks.
  • Throughput with New Configurations: For bfloat16 and block size 64, the Triton implementation achieved average throughputs of 170 TFLOPS and 130 TFLOPS, respectively, demonstrating its ability to handle configurations not supported by the original CUDA kernels.

Limitations and Future Work

  • Triton's Maturity: The paper acknowledges that Triton is under active development, which leads to challenges with documentation, potential for unexpected behavior, and the need for deep debugging efforts.
  • Debugging Support: The authors suggest that improved debugging support within Triton would significantly ease the development and optimization process.
  • Future Work: The immediate future work is the successful replacement of the CUDA kernels with Triton kernels in MegaBlocks, enabling a more general and flexible system for MoE training. The authors imply that further exploration of Triton's optimization capabilities and potential for other high-performance kernels is a natural next step.

Summary updated

BibTeX
@inproceedings{mishra2023implementing,
  author = {Priya Mishra and Trevor Gale and Matei Zaharia and Cliff Young and Deepak Narayanan},
  title = {{Implementing block-sparse matrix multiplication kernels using Triton}},
  booktitle = {ES-FoMO @ ICML},
  year = {2023},
  url = {https://openreview.net/forum?id=doa11nN5vG},
}
Figure