Generating Efficient Kernels for Quantized Inference on Large Language Models

Tommaso Pegolotti, Elias Frantar, Dan Alistarh, Markus PüschelES-FoMO @ ICML 2023

TL;DR

This paper introduces QIGen, an automatic code generation system that creates highly efficient, custom kernels for running quantized Large Language Models (LLMs) on CPUs. By modeling hardware characteristics and quantization constraints, QIGen achieves state-of-the-art performance with minimal accuracy loss, outperforming existing solutions like llama.cpp.

What Problem Does This Paper Address?

Large Language Models (LLMs) are becoming incredibly powerful, but their massive size makes them difficult to run on user devices with limited computational power. Quantization, which reduces the precision of model weights, is a key technique to shrink LLMs. However, efficiently executing these quantized models, especially on CPUs, requires highly specialized code (kernels). Existing methods often involve manual, time-consuming kernel development for each specific hardware architecture and quantization format, which is error-prone and difficult to scale. This paper addresses the challenge of automatically generating these efficient, custom kernels for quantized LLM inference on CPUs.

Methods

QIGen employs an automatic code generation approach that leverages a performance model. This model considers both the target CPU architecture (e.g., cache sizes, available SIMD instructions like AVX2) and the specific quantization method's accuracy constraints (e.g., weight grouping). The core of the method involves generating optimized matrix-vector multiplication (GEMV) kernels.

Key techniques include:

  • Quantization Modeling: The paper details how LLM weights are quantized, often using techniques like GPTQ, where weights are grouped to maintain accuracy. They represent quantized weights efficiently, for example, packing 8 4-bit values into a single 32-bit integer.
  • Performance Modeling: QIGen uses a model that incorporates hardware characteristics (cache size) and quantization specifics (grouping).
  • Mini-GEMV and Micro-GEMV: The computation is broken down into smaller blocks (Mini-GEMVs) and then further optimized at the instruction level (Micro-GEMVs). This involves carefully selecting SIMD instructions (like AVX2) and managing register allocation to maximize parallelism and minimize memory access.
  • Empirical Search: Parameters like block sizes (mu, tu) are determined through empirical search to optimize cache utilization and reduce register spills.
  • Code Generation: The system automatically generates C++ code for these optimized kernels, which can then be integrated with frameworks like PyTorch.

Datasets

The paper evaluates its approach using the LLaMA family of models. The accuracy of the generated models is assessed using the standard Wikitext2 dataset.

Key Insights and Ideas

  • Architecture-Aware Code Generation is Crucial: The paper strongly emphasizes that simply having a good quantization algorithm (like GPTQ) isn't enough; efficient system support tailored to the target hardware is paramount for practical performance.
  • Performance Modeling as a Bridge: By explicitly modeling both hardware and quantization, QIGen can generate kernels that are both fast and accurate, bridging the gap between algorithmic advancements and system implementation.
  • The Power of CPU Optimization: While much research focuses on GPUs, this work demonstrates that significant performance gains are achievable on commodity CPUs through careful, automated kernel generation.
  • Trade-offs are Quantifiable: The paper shows how to systematically explore the trade-off between quantization group size, accuracy (perplexity), and inference speed (tokens/second).

Key Results

  • Performance: QIGen's generated kernels outperform the popular llama.cpp implementation by up to 2.6x on a 13 billion parameter model when using full columns and approximately 2x when using grouped implementations.
  • Accuracy: The generated kernels achieve accuracy comparable to existing methods, with perplexity values on Wikitext2 showing only a few percentage points degradation compared to floating-point baselines, especially for larger models and smaller group sizes.
  • Memory Reduction: 3-bit and 4-bit quantized kernels reduce memory usage by up to 4x and 3.3x, respectively, compared to FP32 implementations for generating a 128-token sentence.

Limitations and Future Work

  • CPU Focus: The current implementation is focused on general-purpose CPUs, specifically targeting AVX2 intrinsics.
  • Empirical Search: The determination of optimal block sizes (mu, tu) relies on empirical search, which could potentially be further automated or optimized.
  • Future Work: The authors suggest extending their approach to other CPU architectures and accelerator hardware. They also aim to improve the practical performance of existing kernels through fine-tuning and explore further directions in program generation and optimization.

Summary updated

BibTeX
@inproceedings{pegolotti2023generating,
  author = {Tommaso Pegolotti and Elias Frantar and Dan Alistarh and Markus Püschel},
  title = {{Generating Efficient Kernels for Quantized Inference on Large Language Models}},
  booktitle = {ES-FoMO @ ICML},
  year = {2023},
  url = {https://openreview.net/forum?id=jjazoNAf1S},
}
Figure