
Share
Researchers explore the theoretical speed limits of large language models using CUDA, pushing the boundaries of performance and efficiency in LLM inference.
15 Mar 2024
In the development of CALM, a minimal and fast CUDA implementation for transformer-based language model (LLM) inference, one of the key challenges was determining the theoretical speed limit-often referred to as the "speed of light" in this context-and measuring our progress against it. This article delves into the mechanics of LLM inference and explores how we can optimize performance using CUDA.
When an LLM generates tokens, it does so sequentially, one token at a time. Specifically, a decoder-only text transformer model takes a token as input and outputs an array of probabilities for all possible tokens in the vocabulary (typically 50-250K tokens). The program then samples from these probabilities to produce the next token, repeating this process until the desired sequence length is reached. This sequential nature means that there's no room for parallelism when generating a single sequence of text.
The LLM performs two primary types of operations during inference:
Both matrix-vector multiplication and attention computation share a critical characteristic: for each element read from memory (either the matrix or KV-cache), only a small number of floating-point operations (FLOPs) are performed. This means that the performance bottleneck is often the memory bandwidth rather than the computational power.
Matrix-Vector Multiplication:
Attention Computation:

To optimize LLM inference on CUDA, we need to focus on maximizing the use of memory bandwidth and minimizing latency. Here are some key strategies:
The theoretical speed limit for LLM inference can be derived by considering the memory bandwidth of the GPU. For example, if a GPU has a memory bandwidth of 1 TB/s and each token requires reading 8 MB of data from the KV-cache, the maximum tokens per second (TPS) would be:
[ \text{TPS} = \frac{\text{Memory Bandwidth}}{\text{Data Per Token}} = \frac{1 , \text{TB/s}}{8 , \text{MB/token}} = 125,000 , \text{tokens/s} ]
However, this is a theoretical upper bound. In practice, other factors like computational overhead and kernel launch latency will reduce the actual TPS.
In our implementation of CALM, we achieved the following benchmarks:
Matrix-Vector Multiplication:
Attention Computation:
By understanding the theoretical limits and optimizing for memory bandwidth
Tags
Original Sources
About the author
Kai built ML infrastructure at a Bay Area startup before developing an obsession with transformer architectures and inference optimisation that eventually pulled him out of product work entirely. A stint at a compute research lab sharpened his instinct for what actually matters in a model release versus what is marketing. He writes from the inside — from the perspective of someone who has debugged the systems he is describing at three in the morning. He is allergic to hype and instinctively drawn to the unglamorous plumbing questions that everyone else skips over.
More from The Engineer →This Week's Edition
18 March 2024
133 articles
Related Articles
Related Articles
More Stories