QiMeng-GEMM: Automatically Generating High-Performance Matrix Multiplication Code by Exploiting Large Language Models

Qirui Zhou, Yuanbo Wen, Ruizhi Chen, Ke Gao, Weiqiang Xiong, Ling Li, Qi Guo, Yanjun Wu, Yunji Chen

Introduction

General Matrix Multiplication (GEMM) serves as the computational backbone of modern artificial intelligence, accounting for more than 90% of the operations in deep learning workloads. Despite its mathematical simplicity, achieving peak performance on contemporary hardware—such as NVIDIA GPUs or RISC-V CPUs—is often considered a "black art." It requires an intimate understanding of complex hardware constraints, including memory hierarchies, vector instruction sets, and instruction-level parallelism.

Currently, developers are faced with a difficult trade-off. Manual optimization by domain experts can yield highly efficient kernels, but the process often takes months and results in code that is not easily portable across different architectures. Conversely, while Large Language Models (LLMs) have shown promise in general code generation, they typically produce "vanilla" implementations that are orders of magnitude slower than optimized libraries like OpenBLAS or cuBLAS.

The QiMeng-GEMM Framework

QiMeng-GEMM is a novel framework designed to bridge this performance gap by teaching LLMs to adopt the strategies of performance engineers. The system moves beyond simple prompting by integrating structured domain knowledge and automated feedback loops.

Meta-Prompts and Hardware Hints

The core of the framework lies in "meta-prompts" structured around five essential optimization primitives: Tiling, Reordering, Vectorization, Layout, and Pipelining. Each meta-prompt provides the LLM with a platform-agnostic description of the optimization logic alongside platform-specific "hints." These hints include critical hardware abstractions, such as specific RISC-V vector instructions or CUDA warp-level primitives, retrieved from a pre-defined repository. This approach allows the LLM to focus on high-level optimization logic while correctly utilizing low-level hardware features.

Comparison of GEMM code generation using vanilla and Chain-of-Thought (CoT) prompts, showing minimal performance gains over baseline libraries.

Iterative Auto-Tuning

Rather than relying on a single generation attempt, QiMeng-GEMM employs an iterative search strategy. The system starts with a basic GEMM implementation and asks the LLM to suggest and apply optimizations sequentially. Each generated kernel is compiled and executed on actual hardware to measure its performance. Using a tree-based search algorithm, the framework maintains a "Top-K" set of the best-performing versions to seed subsequent rounds of refinement, allowing it to discover hardware-specific "sweet spots" that might elude human experts.

Evaluation and Results

The researchers evaluated QiMeng-GEMM across a diverse testbed including the XuanTie C910 (RISC-V CPU), NVIDIA RTX 4070, and NVIDIA A100 GPUs. The framework was tested with several state-of-the-art LLMs, including GPT-4o, Claude 3.5 Sonnet, and DeepSeek-Coder-V2.

Performance Benchmarks

The results demonstrate that QiMeng-GEMM can generate code that rivals or even surpasses industry-standard, hand-tuned libraries.

  • RISC-V Performance: On the XuanTie C910 CPU, the generated code reached 211% of the performance of OpenBLAS.
  • GPU Performance: On NVIDIA hardware, the framework achieved 115% of the performance of the highly optimized cuBLAS library.
  • Speedup over Baselines: Compared to standard vanilla prompts, QiMeng-GEMM delivered speedups of up to 113.67× on CPUs and 9.65× on GPUs.
QiMeng-GEMM's iterative generation process, which enables LLMs to produce code reaching 211% of OpenBLAS performance on RISC-V architectures.

Development Efficiency

One of the most significant findings is the reduction in development time. While a human expert might spend months optimizing a single GEMM kernel for a new chip, QiMeng-GEMM produced superior results in just 7 to 10 minutes. This represents a staggering 240× reduction in engineering costs, making high-performance computing more accessible and portable.

Conclusion

QiMeng-GEMM demonstrates that LLMs, when properly guided by structured meta-prompts and iterative hardware feedback, can automate the complex task of performance engineering. While the current study focuses on GEMM, the underlying principles are extensible to other critical operators such as convolutions and attention mechanisms. Future work will focus on expanding the framework to support a wider array of architectures, including ARM and specialized AI accelerators, potentially transforming how high-performance software is developed.

Summary updated

BibTeX
@inproceedings{zhou2025qimeng,
  author = {Qirui Zhou and Yuanbo Wen and Ruizhi Chen and Ke Gao and Weiqiang Xiong and Ling Li and Qi Guo and Yanjun Wu and Yunji Chen},
  title = {{QiMeng-GEMM: Automatically Generating High-Performance Matrix Multiplication Code by Exploiting Large Language Models}},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year = {2025},
  doi = {10.1609/aaai.v39i21.34461},
}
Figure