Abstract
General Matrix Multiplication (GEMM) is a critical component of scientific computing and deep learning. While high-performance libraries like Intel MKL and BLIS offer exceptional speed, they often rely on hand-coded assembly micro-kernels that are difficult to maintain and tune for the diverse matrix shapes found in modern workloads. This paper introduces an automated generation framework built on Apache TVM that mimics the structured blocking strategies of expert libraries. By utilizing analytical models for cache blocking and TVM’s scheduling primitives for micro-kernel generation, the authors produce GEMM routines that match or exceed the performance of vendor-optimized libraries across various architectures, including ARM and x86, while maintaining a significantly smaller binary footprint.
Introduction
The optimization of General Matrix Multiplication (GEMM) is essential for performance in deep learning and scientific applications. To achieve peak performance, developers typically rely on libraries built around "micro-kernels"—tiny, highly optimized pieces of code written in low-level assembly. However, this manual approach presents several challenges: it is labor-intensive to maintain across evolving architectures (ARM, x86, RISC-V), often rigid in its optimization for square matrices at the expense of the "skinny" matrices common in deep learning, and results in large library binaries unsuitable for edge computing. This research addresses these issues by automating the generation of high-performance GEMM routines using the Apache TVM compiler framework.
Methodology
The authors implement a "generator" for GEMM algorithms that follows the high-performance "GotoBLAS" strategy. Instead of relying on black-box auto-tuning, the framework explicitly structures the computation into a hierarchy of nested loops designed to maximize cache efficiency.
As illustrated in the figure above, the operation is decomposed into six nested loops (L1-L6). Loops L1 through L5 handle the blocking and packing of matrices into the L3, L2, and L1 caches. The innermost loop, L6, constitutes the micro-kernel. The authors use TVM’s scheduling primitives—such as tile, vectorize, and unroll—to generate these micro-kernels automatically. This allows for the creation of "Operand Resident" variants, where specific tiles of the $A$, $B$, or $C$ matrices are held in processor registers to minimize memory traffic.
To avoid the time-consuming brute-force search typical of auto-tuning, the authors employ analytical models. These models calculate optimal cache blocking sizes ($m_c, n_c, k_c$) based on hardware specifications, ensuring efficient cache utilization without extensive trial and error. Fine-grained optimizations, including software prefetching and SIMD vectorization, are implemented directly through TVM’s Python API.
Key Insights
A primary advantage of this automated approach is specialization. While standard libraries often use a single, general-purpose micro-kernel, the TVM-based generator can produce kernels optimized for specific matrix shapes. For instance, a $4 \times 28$ kernel might be generated for a skinny matrix to fill the L1 cache more effectively than a standard $8 \times 12$ kernel. Furthermore, the authors found that for most deep learning workloads, the "Resident C" approach—keeping a tile of the result matrix in registers—is superior as it reduces unnecessary memory writes in the innermost loop.
Results
The framework was evaluated on several platforms, including NVIDIA Carmel (ARM), AMD EPYC, and Intel Xeon (Icelake), using both synthetic square matrices and realistic layers from ResNet-50.
- Performance: On the ARM-based NVIDIA Carmel, the TVM-generated code outperformed the BLIS library on 40 out of 53 ResNet-50 layers. In specific cases, such as Layer 2, TVM achieved 22.4 GFLOPS compared to BLIS’s 13.3 GFLOPS.
- Square Matrix Parity: For standard $2000^3$ square matrices, the automated approach matched the performance of hand-tuned BLIS (~28.9 GFLOPS).
- Portability and Footprint: The generator successfully outperformed Intel MKL on several Icelake layers. Notably, the resulting GEMM executable was only 532 KiB, significantly smaller than the 29 MiB ARM Performance Libraries.
Discussion and Limitations
While the automated generator shows great promise, some gaps remain. TVM's current prefetching logic sometimes falls short of the sophisticated manual prefetching found in assembly-level libraries, particularly on AMD architectures. Additionally, sub-optimal register allocation for very large micro-kernels can lead to "spilling," which degrades performance. Future work will focus on improving prefetching, refining register allocation, and extending these techniques to GPU architectures.
Summary updated