Chapter 20 Large language models
Chapter 20 Large language models
CHAPTER
Large language models
With special contributions from Juan Gómez-Luna CONTENTS 20.1 Transformer architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478 20.2 Multi-head attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482 20.3 Implementing attention in CUDA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486 20.4 KV caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488 20.5 Flash attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492 20.6 KV cache arithmetic intensity and memory requirement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504 20.7 Alleviating the memory requirements of the attention mechanism. . . . . . . . . . . . . . . . . . . . 508 20.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510 20.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511 Before Google Researchers introduced Transformers [1] in 2017, language tasks had been predominantly based on statistical language modeling techniques pioneered by IBM (e.g., [2]). In 2018, Google introduced the BERT language model based on Transformers and achieved unprecedented results in natural language tasks. Since then, the Transformer architecture1 has become the backbone of the deep learning approaches to natural language processing (NLP) based on neural networks. Since then, LLMs have captured the attention of the industry and advanced rapidly, demonstrating human-level performance in diverse tasks such as text generation, summarization, translation, and conversation. These language models have grown by orders of magnitude in size compared to BERT to accommodate more training data. The term Large Language Models (LLMs) has been coined to distinguish them from previous generations of language models. While LLMs were originally developed to process and generate information in text form, multi-modal models (i.e., trained with and used for different types of visual, audio, and other types of data such as scientific data) started to appear. The core idea of LLMs is to predict the next token (e.g., a word, sub-word, character) given a sequence of previous tokens. In chatbot applications such as OpenAI 1 In this chapter, the term architecture'' refers to neural network architecture’’, not ``hardware architec ture’’ as we do through the rest of the book. Programming Massively Parallel Processors. https://doi.org/10.1016/B978-0-44-343900-1.00031-7 Copyright © 2027 Elsevier Inc. All rights are reserved, including those for text and data mining, AI training, and similar technologies.
ChatGPT [3], the sequence of previous tokens, collectively called the context, is composed of the system prompt, user input, and the tokens already generated by the model. In order for LLMs to generate accurate results,2 they contain billions of parameters (i.e., tens of layers with hundreds of millions of parameters each) that must be trained on huge datasets. During inference, large contexts help to provide more accurate responses. A mechanism, referred to as attention is needed to maintain the context and to identify the parts of the large context that are the most relevant to the generation of the next token. It is therefore important to understand the attention mechanism as well as the Transformer Architecture [1] which is the most popular approach to incorporating attention mechanisms into LLMs.3 20.1 Transformer architecture Most LLMs today employ the transformer architecture and its attention mechanism to capture dependencies between tokens and their relative importance in generating the next token. The attention mechanism resembles a classification process within a neural network. It uses the softmax function to generate probabilities. The probability assigned to each token in the sequence of previous tokens serves as an indication of the relative importance of that token to the generation of the next token. The transformer architecture was originally proposed for transduction tasks, such as machine translation. As such, it was designed with two types of layers. The encoder layers contextualize (or mix) the input tokens by generating a sequence of continuous representations, i.e., floating-point embedding vectors, from a sequence of symbols (e.g., tokens) in the source language. The decoder layers take the sequence of continuous representations from the encoder and generate output tokens (one at a time) in the target language. The decoder does not only take the output of the encoder as input but also the most recent output tokens generated by itself. For this reason, the transformer models are auto-regressive models. Since its first introduction, transformers have been adapted to other types of tasks. Depending on the specific task being performed, a transformer may not need both types of layers. For example, models that perform discriminative tasks such as classification do not need a decoder because they do not generate text in a target language. On the other hand, decoder-only models can perform generative tasks, such as text generation. This is the case of LLMs, where text is generated as a response to a user’s query but not as a sequence-to-sequence transformation. Such generative tasks 2 For some LLM tasks (e.g., text summarization), accuracy may not be the right metric, since many dif ferent answers can be correct. However, in this chapter, we use accurate'' with the more general meaning of correct’’, valid'', or useful’’. 3 Although we focus on Transformers and the attention mechanism in this chapter, the research community keeps actively exploring other approaches to LLMs that provide different advantages. Notable examples are state-space models (e.g., Mamba [4]), convolutional models (e.g., Hyena [5]), and hybrid models. They may provide certain advantages over transformers, e.g., less memory requirements.
Before Google Researchers introduced Transformers [1] in 2017, language tasks had been predominantly based on statistical language modeling techniques pioneered by IBM (e.g., [2]). In 2018, Google introduced the BERT language model based on Transformers and achieved unprecedented results in natural language tasks. Since then, the Transformer architecture has become the backbone of the deep learning approaches to natural language processing (NLP) based on neural networks.
Since then, LLMs have captured the attention of the industry and advanced rapidly, demonstrating human-level performance in diverse tasks such as text generation, summarization, translation, and conversation. These language models have grown by orders of magnitude in size compared to BERT to accommodate more training data. The term Large Language Models (LLMs) has been coined to distinguish them from previous generations of language models. While LLMs were originally developed to process and generate information in text form, multi-modal models (i.e., trained with and used for different types of visual, audio, and other types of data such as scientific data) started to appear.
The core idea of LLMs is to predict the next token (e.g., a word, sub-word, character) given a sequence of previous tokens. In chatbot applications such as ChatGPT [3], the sequence of previous tokens, collectively called the context, is composed of the system prompt, user input, and the tokens already generated by the model. In order for LLMs to generate accurate results, they contain billions of parameters (i.e., tens of layers with hundreds of millions of parameters each) that must be trained on huge datasets. During inference, large contexts help to provide more accurate responses. A mechanism, referred to as attention is needed to maintain the context and to identify the parts of the large context that are the most relevant to the generation of the next token. It is therefore important to understand the attention mechanism as well as the Transformer Architecture [1] which is the most popular approach to incorporating attention mechanisms into LLMs.
20.1 Transformer architecture
FIGURE 20.1. Decoder-only transformer architecture. This is a baseline architecture without KV caching.
execute the model multiple times. Each time, the model predicts the likelihood of candidates for a new output token following a sequence of previous tokens. In this section, we focus on the architecture of decoder-only transformers. For more details on the encoder-decoder transformer architecture, we refer the reader to [1]. A sample LLM design based on the decoder-only transformer architecture is shown in Fig. 20.1. We briefly describe its main components as follows. The Tokenizer ➊ converts the text for the input document or query into a sequence of tokens, i.e., integer IDs that represent a character or a short sequence of characters. The mapping of text into tokens is based on the concept that a finite vocabulary can be learned from any given set of training documents in the target language. The collection of unique tokens thus learned and recognized by the tokenizer in an LLM form the vocabulary of the LLM. Each tokenizer has its own definition of tokens and approach to translating text input into tokens. When an LLM is trained, unique IDs are assigned to the tokens as they are encountered in the training document. That is, each time the tokenizer decides to form a token from the next text fragment, it will check if the same text fragment was previously encountered. If not, a new text fragment has been discovered and a new ID number is assigned to this text segment and will be used as the token representation of the text fragment. A mapping from the text fragment to its assigned token ID is recorded for future lookup. If the text fragment was encountered earlier in the training process, the previously assigned token ID is found through the mapping and will be used to represent the text fragment. The mapping from text fragments to token IDs is part of the outcome of training. During inference, each time the tokenizer decides to form a token from a text fragment, the mapping is used to look up the token ID assigned to the text fragment. This token ID becomes the next output of the tokenizer. Strictly speaking, tokens are the text fragments that form the vocabulary of an LLM whereas token IDs are the integer representation of the tokens inside the LLM. When we describe the operations of an LLM, we will use the terms token and token ID interchangeably unless there is a need to be precise. The Embedding layer ➋ implements a function that converts tokens, i.e., token IDs generated by the tokenizer, into embeddings, i.e., their floating-point vector representation. We will use the terms embedding'' and embedding vector’’ interchangeably. This is done via a lookup into an embedding table. The embedding value for each token is also learned from the documents during training. The reason for this conversion is that LLMs are machine learning models that are trained using numerical methods. The training process typically employs gradient-descent approaches that require all functions of the model to be differentiable and all input and parameter values to be in continuous form which is approximated by the floating-point representation. The Positional Encoding layer ➌ adds sequence order information to the input embedding vectors of the input tokens. Positional encoding informs the attention mechanism of the physical position and proximity of tokens in the input, a factor that is considered when determining the relevance of previous input and output tokens to the generation of the next output token. The embeddings of the entire sequence of tokens of the current conversation between the user and the system (user input, system added input, and output) are accumulated into a matrix according to their order of generation. Each row of the matrix is the embeddings of one token. This matrix is the input of the first transformer layer. The Transformer layers ➍ transform the embeddings to extract dependencies among tokens. Each transformer layer contains a multi-head attention sub-layer, an addition and normalization sub-layer, a feed-forward sub-layer, and another addition and normalization sub-layer. The multi-head attention sub-layer ➎ consists of a parallel array of attention heads, each of which performs a linear projection of the embeddings using matrix multiplication. The matrices used in the linear projection are learned in the training process and effectively perform a coordinate transformation on the embedding vectors to make subsequent similarity comparisons more effective. The projections then go through another matrix multiplication that measures the similarity between a token and all the tokens that were generated before it. The outputs from the multiple attention heads are concatenated together and then usually linearly transformed to produce the combined output of the multi-head attention sub-layer. An Addition and Normalization (Add & Norm) sub-layer ➐ follows the multihead attention sub-layer. The addition operator after each feed-forward sub-layer orattention sub-layer enforces a residual connection to help retain information from the original input. The normalization operator stabilizes the process and improves convergence by normalizing the resulting vectors to fit the desired numerical range of input values to the next layer. Its formula takes the mean (μ) and the standard deviation (σ ) of its input embedding vector elements and learned parameters (γ , β). The Feed-forward sub-layer ➏ typically consists of two linear transformations with a ReLU activation in between.4 Intuitively, the attention layers identify similarities/dependencies between tokens and produce new versions of the token embeddings based on similarity-weighted linear combination of token embeddings generated before them. The feed-forward sub-layers add semantic meaning to these new versions of the token embeddings. Another Addition and Normalization sub-layer follows the feed-forward sublayer to help retain the information from the original input to the feed-forward sub-layer and improve convergence while finalizing the output of the transformer layer. The number of transformer layers is referred to as the model depth and varies across LLMs. The attention heads, which perform large-scale matrix multiplications, are the most computationally expensive components of the transformer architecture and will be the focus of the remaining parts of this chapter. The Un-embedding layer ➑ converts the embeddings from the last transformer layer into probability distributions among tokens, which is used to generate the next output token. Model dimensions are critical for generating accurate answers. First, model depth specifies the number of transformer layers in the model. Deeper models provide levels of classifiers and thus can capture more general patterns of dependencies as explained in Appendix B. The second dimension of the model is the number of elements in each embedding vector, called head dimension. Wide embedding vectors, i.e., large head dimensions, capture more semantic information, disambiguate across different concepts, and identify more meaningful dependencies. The third model dimension is the number of heads in each attention sub-layer. These heads capture different types of dependencies between tokens. The size of the model in terms of the number of parameters is the product of the three model dimensions and can be very large, reaching billions or even trillions of parameters (i.e., model weights) in practice. While LLMs require long context for accurate responses, the user inputs are typically too terse and implicit to provide adequate contextual information. As a remedy, the user input is often extended with additional contextual information using a technique called retrieval-augmented generation (RAG). RAG enhances the generation of responses by retrieving relevant documents that provide more specific and up-todate contextual information and concatenate them with the user input to form a much larger, richer context for the LLMs.
4 See Appendix B for the definition of ReLU and activation.
During LLM training, the most relevant computation is large-scale matrix-matrix multiplication, which is performed by the attention heads and feed-forward sublayers and can take advantage of the advanced optimization strategies covered in Chapter 15. These matrix multiplications make the attention heads the most computationally expensive computational components of LLM models. We will elaborate on the design of attention heads and the nature of the matrix multiplications that they perform in Section 20.2.
20.2 Multi-head attention
Each attention sub-layer contains multiple heads which collectively perform multiple classification tasks simultaneously and capture different types of dependencies such as positional (e.g., distance between tokens), semantic (e.g., synonyms or topics), contextual (e.g., connecting articles to names), and syntactic (e.g., connecting verb and object) dependencies. This specialization of attention heads happens during training. The weight matrices of different heads are initialized differently. Then, the gradient descent during back propagation avoids overlap between different heads and prevents overfitting by amplifying gradient variance with techniques such as dropout (i.e., randomly zeroing outputs/probabilities) and layer normalization (i.e., normalizing activations). Attention heads are independent of each other, so they can be easily concurrently computed over multiple thread blocks. They are identical in their design and thus we show the design of one head in this chapter. The computation of an attention head is illustrated in Fig. 20.2. It starts with input matrix X, whose rows correspond to the embedding vectors for the sequence of tokens generated by the LLM so far. Recall that this includes all the tokens generated from the system prompt, the user input, any system augmentation to the user input, and the output tokens. For the attention sublayer of the first transformer layer, each row of X contains an embedding generated by the Position Encoding component. For the attention sub-layer of a subsequent transformer layer, the input X is simply the output of the preceding transformer layer. The number of tokens generated so far, and thus the number of rows in matrix X, is referred to as the sequence length or context length and is represented by the symbol N for the remainder of this chapter. For example, in chatbot applications, N grows as the conversation between the user and the system progresses. Models that support large N provide token generation with longer contexts and are more reliable because the additional context information can be used to disambiguate terms and phrases, identify contextual relevance, maintain consistency across successive responses, etc. The attention sub-layers typically enforce a causality policy that the generation of the current token depends only on the tokens that were generated before, and ignores future tokens. The number of elements in each embedding vector, i.e., the head dimension, is represented by the symbol d. Thus, X is an N×d matrix. The head dimension and the number of heads in each sub-layer are often referred to as the hidden dimensions of FIGURE 20.2 Matrices (2D tensors) involved in the computation of an attention head. For simplicity, a scaling factor √1 that is multiplied to all elements of S = QK T is omitted from this figure d and those derived from this figure. Recall that d is the head dimension or the number of elements in each embedding vector. This scaling factor is applied to the elements of S to prevent the matrix multiplication results from becoming too large and causing converging issues for training processes based on gradient-descent methods. We will include the scaling factor in the CUDA implementations later in this chapter.
an LLM as they are internal design parameters and are typically not exposed to the users of the LLM. The first computational step of an attention sub-layer is to calculate three matrices called the query (Q), key (K), and value (V) matrices. These matrices are obtained with linear projection or coordinate transformation of the row vectors of X, i.e., multiplying the input matrix X by three weight matrices, which we call WQ , WK , and WV , as shown on the left side of Fig. 20.2. These d×d coordinate transformation matrices are obtained from the training process.5 Mathematically, each element in a row of the Q, K, and V matrices is a linear combination of the d elements in the same row in X. The difference between the transformations performed on Q, K, and V is defined by the difference in the weight values of WQ, WK , and WV . The second step of an attention sub-layer is specified by Eq. (20.1) and illustrated on the right side of Fig. 20.2. QK T O = sof tmax( √ + M)V d (20.1)
5 For simplicity, in this chapter, we assume that the weight matrices are square. In the general case, W , Q WK , and WV are, respectively, of sizes dx × dq , dx × dk , and dx × dv . However, as per QK T , dq isalways equal to dk .
Since Q is an N × d matrix and K T (transposed matrix K) is a d × N matrix, the matrix multiplication QK T generates an N×N matrix, which we call S in Fig. 20.2. Element (i, j) of QK T is an inner product between a linearly projected embedding vector for token i (i.e., row i of matrix Q) and that for token j (i.e., column j of matrix K T or row j of K). Mathematically, an inner product calculates the cosine similarity between its two input vectors assuming that both vectors are of unit length, which can be enforced with the normalization mechanism. Recall that the more similar the two vectors, i.e., the smaller the angle between them, the larger the value of the cosine similarity. Therefore, the N×N product matrix is a map of similarity between embedding vectors. The scaling factor √1 is applied to all elements of S = QK T to prevent the dot d products of the matrix multiplication from growing too large for large head dimensions. Excessively large S elements may cause the softmax function to saturate and produce tiny (vanishing) gradients during training [1]. We omit the scaling factor from Fig. 20.2 as well as its associated discussions for simplicity. As we show later in this chapter, it is straightforward to incorporate the scaling factor into the CUDA implementation of the softmax function. T √ The mask matrix M adds −∞ to all elements of QK whose column indices are d greater than their row indices. This transformation enforces the causality policy that the generation of a token should not be influenced by the tokens that are generated after the token. The softmax function in Eq. (20.1) translates each row of QK T + M into a probability distribution. Let us denote element (r, c) at column c in row r of the QK T + M matrix as logit lr,c that the softmax function takes as inputs to generate probabilities pr,c , as Eq. (20.2) shows: elr,c elr,c −mr pr,c = ∑︁N = ∑︁N lr,j lr,j −mr j =1 e j =1 e (20.2)
where mr is the maximum element value of row r. Note that lr,c can be large, so elr,c can overflow. For this reason, we subtract mr = maxN j =1 (lr,j ), from lr,c before applying the exponential function, which prevents the exponential function from overflowing and thus maintains numerical stability. Because the subtraction of mr from lr,c is equivalent to multiplying elr,c by e−mr and both the numerator and the denominator are multiplied by e−mr , the resulting p elements are mathematically equivalent. With the causality mask matrix, any logit element whose row index r is smaller than the column index c becomes −∞. This makes elr,c −mr = e−∞−mr = e−∞ = 0. That is, each row of the output matrix of P = sof tmax(QK T + M) in Fig. 20.2, is a probability distribution based on similarity between token r’s embedding vector in Q and those for the tokens that were generated before token r in K. All remaining elements of the row are 0s after softmax. That is, the N×N matrix generated by sof tmax(QK T + M) is a lower-triangular matrix where all elements with indices (r, c) are zeros if r < c.
FIGURE 20.3. LLM stages: summarization and generation.
The final step of the attention sub-layer is the matrix multiplication of the N × N probability matrix P and the V matrix. For each token, this matrix multiplication generates a new embedding vector where each element is a similarity/probability weighted sum of the same element of all embeddings in the V matrix. That is, the new embedding vector for each token in the output matrix O is a similarity-weighted sum of the embedding vectors of all previously generated tokens. The output O matrix then goes through the addition-and-normalization and feed-forward sub-layers to become the input matrix X for the next attention sub-layer. Inferencing with an LLM based on the decoder-only architecture in Fig. 20.1 starts with a summarization phase where the system prompt tokens, the user input tokens, and the system-augmented (e.g., RAG) tokens are concatenated to form the X input matrix of the first transformer layer. During the summarization phase, the matrices involved and the computation for each attention head are as shown in Fig. 20.2. The accumulated X matrix is fed to the transformer layers. Each transformer layer generates the input for the next transformer layer. Upon completion of the last transformer layer, the un-embedding layer is used to generate an output token as a text fragment. The next inference iteration starts the autoregression phase, which is also called the decoding phase or the generation phase. The generated output text fragment is fed to the tokenizer, appending the generated embedding vector to the X matrix of the first transformer layer, and activating the transformer layers to generate the next text fragment. Fig. 20.3 illustrates the two phases of LLM inference. During the summarization phase, the initial sequence of the system’s prompt as well as the initial user’s prompt is processed by the tokenizer and embedding layers and accumulated into the X matrix ➊. For simplicity, we omit the systems’ prompt and the typical preamble of the user input in this toy example. As shown in Fig. 20.3, the user wants the LLM to complete the title of this book by entering a prompt The title of this book is Programming''. For simplicity, we assume that the LLM in this example uses entire words as tokens. Thus, during the summarization phase, the tokenizer layer, the embedding layer, and the position encoding layer accumulate the embedding vectors for the seven tokens generated from the user input into the X matrix of the first transformer layer. The LLM then invokes the first transformer layer whose completion activates the second transformer layer, etc. When activated, each transformer layer calculates its K, V , Q, QK T , and O before activating the next transformer layer. After all transformer layers and the final un-embedding layer complete their calculation, the LLM produces the first output token (Massively’’ in our example ➋). With the generation of the first output token, the LLM enters the generation phase. During the generation phase, each output token is generated based on the tokens of the initial prompt and the output tokens of all previous iterations. In our example, the token produced at the end of the summarization phase (Massively'') becomes part of the input to the first iteration of the generation phase, which will produce the next output token. For each new output token, the LLM generates a corresponding new embedding vector (i.e., a new row of matrix X) that will go through the transformer layers ➌. At the end of the first iteration, we obtain a new output token (Parallel’’), which will be the input of the next iteration of the generation phase ➎. The generation phase continues iterating until an end-of-sequence (EOS) token is generated ➏.
20.3 Implementing attention in CUDA
Attention computation in the transformer architecture shown in Fig. 20.1 consists of two matrix multiplications, one matrix element-wise scaling, one matrix addition, and the softmax computation. The matrix multiplications can be performed with either custom CUDA matrix multiplication kernels or calls to the highly optimized GEMM functions in available libraries such as cuBLAS. The scaling factor √1 can be applied d to the QK T multiplication in the same custom kernel, or as the scaling factor α of cuBLAS’s GEMM API. The only new aspect of attention computation is the implementation of the softmax function. We will thus focus on the CUDA implementation of the softmax function and leave the rest as an exercise. Fig. 20.4 shows a CUDA kernel that fuses the matrix addition with M and the softmax computation within the same kernel. The fused kernel avoids an additional pass through its input S matrix before the softmax computation and improves the arithmetic intensity and thus GPU utilization. The execution configuration of this kernel (not shown in Fig. 20.4) is a 1D grid of thread blocks. The kernel assumes that the number of thread blocks (i.e., gridDim.x)
FIGURE 20.4. A CUDA kernel implementation for the softmax function.
is set to the number of rows in matrix S, i.e., the sequence length N. Both gridDim.y and gridDim.z are 0. This assumption limits the kernel to a context length that is less than or equal to the maximal number of thread blocks allowed in the x dimension. If a larger context length is needed, one can make the code more general by adding an outer loop to the kernel code so that the threads in the grid iterate until all rows of S are processed. Each thread block is assumed to be 1D with BLOCK_SIZE threads. BLOCK_SIZE is a compile-time constant that is set by the programmer. Since all threads in the block collaboratively process a row of S, one should choose a BLOCK_SIZE value that minimizes the number of expected iterations for a thread block to process its assigned row while maximizing the occupancy of the SMs in the GPU. With the scaling factor applied as part of the matrix multiplication QK T and the causality fused on the softmax, each S element of a row can be viewed as lr,c in Eq. (20.2). Lines 6-14 in Fig. 20.4 obtain the maximum element value in the row assigned to the thread block, i.e., mr in Eq. (20.2), and deposit it into local variablemax_val_row. In each iteration of the for loop in lines 7-8, each thread in the blockexamines one S element in a section of blockDim.x elements of the assigned row and update a thread-private variable max_val_thread. Note that the exit condition of the for loop in 7-8 is idx <= blockIdx.x. This way, the code ignores row elements whose row index (blockIdx.x) is less than their column index (idx), which implements the causality policy without explicitly adding matrix M in Eq. (20.1), i.e., adding −∞ to all elements of S with column indices greater than their row indices. After the threads exit the for loop in lines 7-8, each thread holds a thread-private maximal value among the S elements examined by the thread. In line 10, all threads of the block synchronize to ensure that all completed examining their assigned S elements. In line 11, all threads in the block collaborate to obtain the maximum of the entire row by calling the BlockReduce function of the CUB library. The total reduction result is obtained by the thread with threadIdx.x == 0. Thus, this thread needs to share the result with all other threads via the shared memory variable max_or_sum (line 12). After synchronizing (line 13), all threads read max_or_sum (line 14). Note that the code in lines 7-14 is essentially a reduction with thread coarsening, which we already introduced in Chapter 10. Next, lines 15-23 obtain the denominator of Eq. (20.2). In lines 16-17, the threads calculate a thread-private sum. The exit condition of the for loop implements the causality policy. After synchronizing (line 19), CUB’s BlockReduce function computes the total sum for the row. In lines 21-23, thread 0 broadcasts the final sum result via max_or_sum in shared memory. As we have mentioned above for the max reduction, lines 15-23 implement a sum reduction with coarsening, similar to the one covered in Chapter 10. Finally, lines 24-25 calculate the softmax output for each element of S and store them in memory (in output matrix P). Note that this for loop iterates over all elements of a row, and those elements with column index greater than row index are set to 0, per the causality policy. The denominator is also stored in output vector D (line 27) for reuse during training. In summary, the softmax kernel in Fig. 20.4 implements the causality policy and the softmax function within one kernel. No global barriers are needed (i.e., no additional kernel for adding M); only the block-level __syncthreads() barrier is used. The reader is encouraged to complete the implementation of the attention sub-layer by adding the matrix multiplication for generating S and the matrix multiplication for generating O.
20.4 KV caching
The transformer architecture shown in Fig. 20.1 has an obvious inefficiency, if it is implemented in the straightforward way that Fig. 20.3 illustrates. In each iteration, five matrix multiplications are performed in each transformer layer. Since the sequence length N can grow to the level of tens of thousands of tokens or more, these matrix multiplications can be very expensive. Since there is only one additional row FIGURE 20.5 Optimization opportunities for reducing the matrix multiplication work from one LLM iteration to the next.
in the input matrix X from one iteration of the decoding phase to the next, one should ask if we really need to perform full matrix multiplications in every iteration. The answer is no. Fig. 20.5 highlights the new parts of each matrix generated from iteration i (N = i) to iteration i + 1 (N = i + 1). For input matrix X, one new row, i.e., the embedding vector of the new token, is attached to the bottom, which increases the dimensions of X from N×d to (N + 1)×d. The effect of this new row of X on Q, K, and V is straightforward. Each of them becomes an (N + 1)×d matrix. The new row in the bottom of each matrix is generated by performing a vector-matrix multiplication between the new bottom row of X and WQ , WK and WV , respectively. The step of generating QK T is a bit more complicated. The new product is an (N + 1) × (N + 1) matrix. The contents of the matrix are largely the same as the previous iteration, except for the (N +1)th row and the (N +1)th column (highlighted with darker color in Fig. 20.5). For example, element (0, 0) of the product matrix is generated with an inner product between the 0th row of Q and the 0th row of K (i.e., the 0th column of K T ). Note that both of them are the same as the previous iteration so there is no change in the element (0, 0) of the product matrix. In general, the value of any element (r, c) of QK T remains the same as the previous iteration as long as both r and c are less than N since the element is generated from rows of Q and K that remain the same as the previous iteration. The new N th row of QK T is calculated as a vector-matrix multiplication between the (new) N th row of matrix Q and matrix K T . The new N th column of QK T consists of all zero elements because of the zero-masking performed under the causality policy: the tokens generated by the LLM should not be influenced by the token generated after them. The only exception is the N th element of the N th column. This element value is not zero and is covered by the generation of the new N th row.
FIGURE 20.6. Vectors and matrices involved in the generation phase of the attention computation with KV caching.
The output of the softmax function can also be incrementally updated from the previous iteration. Recall that softmax is applied to each row of the QK T matrix. Since the new N th element of each row (except for the new N th row) is 0, these new elements do not impose any change to the elements in these rows from iteration N to iteration N + 1 as they do not dilute any of the probabilities from these previous elements. The reader should verify that all first N (0th through (N − 1)th ) rows of the O matrix in iteration N + 1 remain the same as the previous iteration. One simply needs to perform a vector-matrix multiplication between the new row of sof tmax(QK T ) and the new V . The vectors and matrices involved in generating the new parts of Q, K, V , and O are shown in Fig. 20.6. Note that for each iteration, a multi-head attention sub-layer only needs to receive one new row of X. Therefore, each of them only needs to deliver a new row of its output O to the next layer. Note that the attention computation during the generation phase still requires the entire K and V . This requirement can be met by memoizing K and V (i.e., storing and reusing later) in the form of the so-called KV cache,6 which is a common optimization of attention sub-layers.
6 Note that the term KV in LLM inference should not be confused with the term key-value in the key value store literature but they are analogous terms. LLM inference produces an output that is a weighted sum of values (V), where the weight is proportional to how close a query (Q) and the keys (K) are [1].
FIGURE 20.7. LLM stages: summarization and generation with KV caching.
Fig. 20.7 revises Fig. 20.3 to illustrate the two phases of LLM inference with KV caching. In order to avoid recomputation in each iteration of the LLM inference, the K and V generated by each transformer layer are entered into the layer’s KV cache to be reused for the generation of future tokens. During the summarization phase, all transformer layers generate their initial K and V matrices and fill their KV caches ➊. With KV caching, the summarization phase is also called the prefill phase as the transformer layers prefill their KV caches with the initial contents of their K and V matrices during this phase. Then, during the generation phase, each output token is generated based on the last output token as well as the K and V matrices of all previous iterations. The last generated text fragment is used to obtain a new embedding vector that is fed to the first transformer layer ➋. In each transformer layer, three vector-matrix multiplications are first performed to generate one new row for Q, K, and V respectively. These new rows are shown as vectors Q′ , K ′ , and V ′ in Fig. 20.6. The LLM attaches these new rows to the K and V matrices from the previous iteration in the KV cache ➌ to form the new K and V matrices for the current iteration and use them to calculate O’ and send it through the rest of the layer to generate the new input vector for the next transformer layer. It should be clear to the reader that the prefill (summarization) phase still requires one transformer pass for a large number of tokens and invokes several large matrix-matrix multiplications (GEMMs). It is typically compute-bound (high arithmetic intensity) and can achieve high GPU utilization. In Section 20.5, we present flash attention, a technique to further improve the speed of the prefill phase by reducing the consumption of global memory bandwidth during the transitions between these matrix multiplications and the softmax operation.
FIGURE 20.8. Tiling approach used in flash attention. Note that we use the same tile in shared memory S_i to store the tile of P.
In contrast, the generation phase invokes one transformer pass per output token. With KV caching, each pass performs mostly vector-matrix multiplications (GEMVs) and is typically memory bandwidth bound (low arithmetic intensity). Thus, it tends to under-utilize the GPU compute resources. In Section 20.6, we introduce some optimization techniques to increase the arithmetic intensity of the generation phase (namely, batching and speculative decoding).
20.5 Flash attention
The reader may observe that the implementation of attention with a separate softmax kernel, as the one presented in Fig. 20.4, implies global barriers at kernel boundaries as well as several loads/stores of entire matrices from/to the global memory. These global barriers and global memory accesses create a major performance bottleneck that the flash attention formulation [6] alleviates. Flash attention mathematically reformulates the attention heads so that the operations in each attention head can be reordered and fused into a single kernel, and reorganized in a tiled manner. Within the fused kernel, as shown in Fig. 20.8, each thread block takes a horizontal panel (contiguous rows) of Q ➊, the entire K T ➋, and the entire V ➑ to compute their contribution to a horizontal panel of O ➏. The inputs and output for each thread block are shaded gray in Fig. 20.8.
Furthermore, flash attention enables each thread block to iterate through the K T and V matrices in a tiled manner while calculating their contribution to the O tile. These tiles are illustrated in Fig. 20.8 and they are small enough to fit into the onchip memory. In each iteration, each thread block loads a vertical panel (contiguous columns) of K T ➋ and a horizontal panel of V ➑ to compute their contribution to the O tile. All thread blocks execute in parallel, without any global barrier. This iterative approach allows the kernel to keep all intermediate matrices ➌➎ only in the on-chip memory in tiled form and eliminate the global memory traffic for these intermediate matrices. Flash attention is an exact reformulation of the attention mechanism in transformers. It is mathematically identical to the original formulation. flash attention is applicable to training (both forward pass and backward pass) and inference (forward pass). In this chapter, we focus on the forward pass. The backward pass is conceptually simpler because it does not require softmax rescaling, but it requires five matrix multiplications while the forward pass requires only two. For details on applying flash attention to the backward pass, we refer the reader to the original flash attention paper [6]. The key idea of flash attention is tiling. However, tiling in attention head implementations is not as straightforward as in matrix multiplication, because the formulation of softmax in Eq. (20.2) requires multiple passes through each row of its input. The reason is that the computation (see Eq. (20.2)) depends on mr , which is the maximum ∑︁ of all values in a row r of the matrix that results from QK T + M, and the sum lr,j −mr , which is used as the denominator when generating p elements Dr = N j =1 e in Eq. (20.2). With this formulation, one would need to scan through the row at least once to gather both the max value and the sum before doing another pass to calculate the elements of the row in the output P . We would need a global barrier synchronization to ensure that all elements of the QK T + M matrix are available before calling the softmax kernel. In CUDA, a global barrier synchronization can be done by terminating a kernel and launching a new one. Alternatively, one could launch a persistent grid and use grid-level cooperative group synchronization API to avoid kernel call overheads. However, good thread grid configurations for matrix multiplication are likely different from those for the softmax operation. This is an important reason for launching separate kernels, each with its own grid configuration, for matrix multiplications and the softmax in the baseline CUDA implementation of the transformer architecture. However, regardless of which global synchronization strategy we choose, the multiple passes through the rows of S of the softmax incurs global memory traffic that can significantly limit the throughput of our implementation. Fortunately, we can remove the need for multiple passes of softmax through entire rows of S by defining a partial series for the denominator, as Eq. (20.3) shows: Dr,A = ∑ j ∈Aelr,j −mr,A (20.3)
where A is a subset of column positions in row r. Note that when A encompasses all column positions of row r, Dr,A is equal to Dr . If we can derive a composition rule for merging the partial series for A with the partial series for another subset of column positions B into a partial series for subset A ∪ B, we have a path to compute the partial series for subsets independently (potentially, in parallel) and simply merge them together as they become available [7]. Once the merged subset contains all column positions of the row r, we have the correct value for Dr . Eq. (20.4) shows the composition rule for merging Dr,A and Dr,B into Dr,A∪B : ∑ Dr,A∪B = elr,j −mr,A∪B = j ∈A∪B ( ∑ ∑ elr,j −mr,A∪B + j ∈Aelr,j −mr,A )emr,A −mr,A∪B + ( j ∈A ∑ ∑ elr,j −mr,A∪B = j ∈Belr,j −mr,B )emr,B −mr,A∪B = j ∈B Dr,A emr,A −mr,A∪B + Dr,B emr,B −mr,A∪B (20.4)
where mr,A∪B is the larger one between mr,A and mr,B . Note that the second step of the derivation is correct as long as A and B do not have common elements. With this composition rule, we can easily merge two partial series sums as long as we also keep the maximal values with each subset, i.e., mr,A and mr,B . In the CUDA implementation of flash attention that we present below, the subsets A and B are tiles of each row of matrix S, and are guaranteed to have no common elements. Flash attention [6] takes this approach one step further: it applies the composition rule to the output matrix O as well. Let’s first focus on the computation of an O matrix element or,c , which is an inner product between row r of P and column c of V . We express the contribution of the subset A of row r to or,c as or,c,A in Eq. (20.5): or,c,A = ∑ pr,j,A vj,c = j ∈A 1 ∑ lr,j −mr,A e vj,c Dr,A (20.5)
j ∈A
where pr,j,A is defined as the intermediate value of the P matrix element pr,j when considering only the subset of elements in A. Similarly, mr,A is defined as the maximal value among the elements in A, not the entire row. The difference between pr,j,A and pr,j is that the denominator and the maximal value are both based on only the elements in A, not all the elements in row r. As more elements are merged into A, it will eventually contain all elements of row r. The following composition rule is derived to guarantee that pr,j,A will become pr,j when A contains all elements of row r. or,c,A∪B = ∑ j ∈A∪Bpr,j,A∪B vj,c = ∑ elr,j −mr,A∪B vj,c Dr,A∪Bj ∈A∪B = ∑ elr,j −mr,A∪B j ∈A = ∑ elr,j −mr,A∪B +mr,A −mr,A Dr,A∪Bj ∈Aj ∈A = Dr,A∪B Dr,A Dr,A Dr,A∪Bj ∈Bvj,c + vj,c + vj,c + Dr,A∪Bvj,c ∑ elr,j −mr,A∪B +mr,B −mr,B Dr,A∪Bvj,c ∑ elr,j −mr,A∪B +mr,B −mr,B Dr,B j ∈B ∑ elr,j −mr,A emr,A −mr,A∪B Dr,A j ∈A ∑ elr,j −mr,Bj ∈B ∑ elr,j −mr,A∪B +mr,A −mr,A Dr,A = = Dr,A∪Bvj,c + Dr,A∪B Dr,B ∑ elr,j −mr,B emr,B −mr,A∪B Dr,B j ∈B Dr,B Dr,A∪Bvj,c vj,c Dr,A emr,A −mr,A∪B ∑ elr,j −mr,A Dr,B emr,B −mr,A∪B ∑ elr,j −mr,B vj,c + vj,c Dr,A∪B Dr,A Dr,A∪B Dr,B = Dr,Aj ∈A m −m ∑ r,A r,A∪B e Dr,A∪Bj ∈Apr,j,A vj,c + Dr,Bj ∈B m −m ∑ r,B r,A∪B e Dr,A∪Bpr,j,B vj,cj ∈B Dr,A emr,A −mr,A∪B Dr,B emr,B −mr,A∪B = or,c,A + or,c,B Dr,A∪B Dr,A∪B (20.6)
where or,c,A and or,c,B are versions of the or,c that account for the contributions of subsets A and B of row r of P . In particular, the Dr and mr values used to calculate these P element values in these subsets also only account for their respective subset elements. Thus, the composition rule calls for rescaling or,c,A and or,c,B before merging them into or,c,A∪B , as shown in the final step of the derivation in Eq. (20.6). While the composition rule in Eq. (20.6) applies to any subset of a row of P , it is especially useful when the subset is a contiguous segment of P that is generated from tiles of Q and K T . Fig. 20.8 illustrates the flash attention tiling approach [8] that is enabled by the composition rule. Fig. 20.9 shows a kernel that implements this tiling approach. The code corresponds to the computation of the forward pass of the attention layer of one head. The entire grid is involved in this computation. In a more complete implementation, multiple heads can be computed in the same kernel by launching additional dimensions of thread blocks in the grid. The same tiling approach also applies to the summarization phase in LLM inference. The code in Fig. 20.9 computes on 32-bit floating point values, but state-of-the-art implementations typically operate on 16-bit floating point (half precision) or even 8-bit floating point values. Such implementations can benefit from the sheer performance of GPU tensor cores. The flash attention kernel code assumes that the input matrices Q, K, and V are of dimensions N × d, as shown in Fig. 20.8. The host code (not shown and left as an exercise) allocates global memory space, initializes the contents of these matrices in the global memory and passes them by reference as kernel arguments. The host code also allocates space in the global memory for the output matrix O (out_O in the code in Fig. 20.9) whose contents will be generated by the execution of the kernel. The FIGURE 20.9 A CUDA kernel implementation of the forward pass of flash attention [8].
kernel generates an N-element output vector D (out_D in the code). Each element of D contains the final value of the denominator used to calculate the probability values in a row of P. This output vector is needed for training, not inference, and is included in the kernel code so that the kernel can be used in training as well.
At the high level, the flash attention kernel is designed to be executed by a 1D grid of thread blocks, i.e., only the x-dimension is used. Each block is responsible for generating a horizontal panel (a tile of a contiguous collection of rows) ➏ of the output matrix O. The horizontal dimension of the output tile is d, the width of the attention head. The vertical dimension of the output tile, i.e., the number of rows in the horizontal panel, is specified by B_r whose value is set to maximize the level of data reuse in the on-chip memory as limited by the amount of on-chip memory available as well as the number of thread blocks needed to fully utilize the GPU. The value of B_r_warp is B_r divided by the number of warps per thread block. The outer-most for loop in line 16 ensures that all O panels are generated. In each iteration, every thread block processes one panel of Q and generates one panel of O. Thus, all blocks in the grid collectively generate gridDim.x panels of O in each N iteration of the for loop. All thread blocks iterate until all the T_r = B_r (line 10) panels of O are generated. For each panel of Q, its designated thread block generates the corresponding O tile in three conceptual steps that the code in Fig. 20.9 shows between lines 25 and 40. First, each thread block performs a matrix multiplication between a B_r × d horizontal panel of Q ➊ and the entire KT matrix ➋ to generate a B_r × N horizontal panel of S ➌. The code of the device function compute_S_and_max() (line 31) is in Fig. 20.12. We will discuss it shortly. In the second step, the S panel is translated by softmax into its corresponding B_r × N horizontal panel of P. The code of the device function compute_P_and_update_D() (line 35) is in Fig. 20.15. The P panel is then multiplied with the entire V matrix to generate its B_r × d output tile of O. The code of the device function compute_O() (line 37) is in Fig. 20.13. As we discussed at the beginning of this section, a naive implementation of these three steps could use three kernels. These kernels would write and read S and P to and from the global memory, resulting in low arithmetic intensity as well as kernel invocation overhead. The flash attention formulation in Eq. (20.6) allows the three conceptual steps to be fused into one kernel and performed in a piece-meal, iterative manner in on-chip memory, which eliminates the need to materialize S and P in the global memory as well as the corresponding undesirable global memory traffic. We will apply both shared memory tiling and register tiling to matrix-matrix multiplication, which take advantage of the bandwidth of both the register file and the shared memory. We will choose registers for Q and O tiles and the shared memory for K T , S/P , and V tiles. Line 14 of the code declares S_i in the shared memory to hold both the input S and output P tiles of softmax. Lines 13 and 15 allocate space in shared memory for panels of the input matrices, KT_j and V_j, as illustrated in Fig. 20.8. In lines 17-21, each thread allocates registers for values of the row maximum (i.e., m_i ➍ in Fig. 20.8), the denominator of the softmax (i.e., D_i ➍ in Fig. 20.8), and the output (O_i), and the device function called in line 21 initializes them. We leave the implementation of this device function as an exercise for the reader. Line 22 allocates registers for Q_i and the device function load_Q() (line 24) loads the corresponding panel from global memory. Note that the total number of registers needed to host FIGURE 20.10 Flash attention: device function to load Q into registers.
the tiles of Q and O, as well as D_i and m_i, is determined by the dimension d and B_r. Furthermore, each thread only holds a portion of these tiles and exchange the elements with each other through the warp-shule API. As shown in Fig. 20.8, each panel of Q ➊ for a thread block is further divided into sub-panels of size B_r_warp × d. Each sub-panel is assigned to a warp in the thread block. The device function load_Q() that loads a sub-panel of Q into registers is shown in Fig. 20.10. Within a sub-panel, each row ii is assigned to the threads in the warp in an interleaved manner (note that in this device function and later ones in this chapter, we use the device functions laneIdx() and warpIdx() defined in Chapter 10). That is, a row is divided into sections of WARP_SIZE elements. The elements of a section are assigned sequentially to the threads in the warp. This way, each section of the Q rows can be loaded by the warp in a coalesced manner. With this arrangement, each thread holds B_r_warp × (d/WARP_SIZE) elements of Q in its registers. For simplicity, we assume that d is a multiple of WARP_SIZE for our example kernel. Interested readers are encouraged to extend the kernel to handle arbitrary d values.7 For simplicity, let us first ignore softmax. Without softmax, it is conceptually easier to fuse the three steps of each thread block. In a fused kernel, the thread blocks take an iterative approach to performing the two conceptual matrix multiplications. Each thread block loads its Q tile into registers (line 24) and initializes its O tile elements to zero by calling device function initialize() in line 21 before going into the iterative process that implements the two conceptual steps (matrix-matrix multiplications) in a tiled manner. The iterative process for a thread block to perform the two conceptual matrix multiplications has two levels: the block level and the warp level. Each iteration at the block level is an iterative process at the warp level. At the block level, each thread 7 This distribution of Q corresponds to the second version of flash attention [8]. It is different from thefirst version [6] that distributes K and V across warps (a scheme called split-K), which is inefficient because warps need to write intermediate results to shared memory, synchronize, and reduce them. In the second version [8], warps do not need to exchange any intermediate data with each other. All data exchanges are between threads within the same warp.
FIGURE 20.11. flash attention: device function to load KT and V into shared memory.
block starts from the left end of KT ➋ and the top of V ➑. All thread blocks iterate from left to right for KT and from top to bottom for V. Each iteration covers B_c columns of KT and B_c rows of V. Since there are N columns in KT and N rows in V, each thread N block will take T_c = B_c (line 11) iterations to complete the process. Each iteration requires a merge operation on all elements in the block’s O tile. Therefore, the value of B_c is set by the host code to minimize the number of iterations and thus the number of merges required to produce the O elements (Eq. (20.6)). The value of B_c is constrained by the available shared memory. For simplicity, we only allow B_c value to be multiples of WARP_SIZE, which avoids the need for checking boundary conditions or data padding. The reader is encouraged to extend the code to allow more general B_c values. In a block-level iteration, each block starts by loading B_c columns of KT ➋ and T B_c rows of V ➎ into the shared memory (lines 27-28). These are the tiles of K and V to be used by the block for the iteration. The device function load_KTV() called in line 27 is shown in Fig. 20.11. For efficiency, we load the tile of KT from K in a transposed manner (line 4). To avoid shared memory bank conflicts, we pad each column of KT_j when allocating it in the shared memory in line 13 of Fig. 20.9. With padding, the index used in each access to KT_j is transformed by the addr(x) = x + (x » LOG_NUM_BANKS) introduced in Chapter 16. The barrier synchronization in line 28 of Fig. 20.9 ensures that all the tiles are in place before proceeding with the first matrix multiplication. The generation of each O tile is implemented by the nested for loop from line 29 to 38 as a warp-level iterative process. The reader should refer to Fig. 20.8 for better understanding of the code. In an iteration, each warp of the block takes a row of its Q sub-panel ➊, performs a vector-matrix multiplication between the row and the current panel of KT ➋ to generate the corresponding row of S_i ➌ (compute_S_and_max() in line 31), uses softmax to transform the row of S_i into a segment of P in place ➎ (compute_P_and_update_D() in line 35), and performs a vector-matrix multiplication between the row of S_i and the current panel V ➑ to generate a subset contribution to be merged into its panel of O ➏ (compute_O() in line 37). Since we are ignoring the softmax step for now, we will focus on the two vector-matrix multiplications.
FIGURE 20.12. flash attention: device function to compute a tile of S and its maximum.
For the first vector-matrix multiplication, the kernel calls compute_S_and_max(), which Fig. 20.12 shows. The current panel of KT is further divided into sub-panels of WARP_SIZE columns each. The for loop in line 5 iterates through the sub-panels. In each iteration, each thread in the warp performs an inner product between the current row of the warp’s Q sub-panel and a column of the current KT sub-panel (lines 7-10 in Fig. 20.12). The columns of the KT sub-panel are assigned to the threads in the warp sequentially. Thus, in each iteration of the for loop, the warp performs a vectormatrix multiplication between the current row of the Q sub-panel and a d × WARP_SIZE T K sub-panel. The result of the iteration is a segment of WARP_SIZE elements in the corresponding row in the S tile (see ➌ in Fig. 20.8). In each iteration of the for loop in line 5 of Fig. 20.12, each thread executes the for loop in line 7 to sequentially perform an inner product. In each iteration, all threads in the warp need the same Q element to multiply with their respective KT elements. Recall that the row of Q elements is stored in an interleaved manner in the registers of threads in the warp (➊ in Fig. 20.8). Thus, each in iteration, the thread who has the Q element for that iteration broadcasts the element to all other threads in the same warp using the __shfl_sync() intrinsic function in line 8. Line 13 implements the causality policy. It compares the row index and the column index in the entire S matrix. If the column index is greater than the row index, we store −∞ in the corresponding shared memory position. If not, we multiply the element S_ij by a scaling factor ( √1 ) and dstore the result into the S tile element in the shared memory (line 14).8 8 Note that the shuffle instruction in line 8 broadcast a value of Q from one thread register to threads in the warp. This implementation of matrix multiplication uses tiling in shared memory and registers, but it FIGURE 20.13 flash attention: device function to compute O.
Since we are ignoring softmax for the moment, we will skip to line 37 of Fig. 20.9. In this line, the kernel calls the compute_O() device function, which is shown in Fig. 20.13. This device function consists of a nested for loop where each warp multiplies its current row of S_i with the sub-panel of V to produce their contribution to the elements in the output tile of O. This is performed as a warp-level iterative process similar to the first vector-matrix multiplication (Fig. 20.12). In the outer for loop (line 2 of Fig. 20.13), the warps iterate through the sub-panels of V. In each iteration of the outer for loop, all threads in the warp collectively perform a vector-matrix multiplication between the row of S_i and the current sub-panel of V. Each thread executes the inner for loop (line 5) to perform an inner product between the row of S_i and a column of V. Note that each element of the output matrix O is an inner product of one full row of the S matrix and one full column of the V matrix. Each row in the S tile and column in the V sub-panel only accounts for a B_c section of the inner product for one O element. Thus, the warp accumulates the vector-matrix multiplication result between the S_i row and the V sub-panel into the elements of the O tile at the end of each block-level iteration (line 8 of Fig. 20.13). At the exit of the for loop in line 29 of Fig. 20.9, each warp completes an iteration of the block-level iterative process. Each warp has completed the contribution of its assigned sub-panel of Q, the current sub-panel of KT , and the current sub-panel of V to its assigned sub-panel of O. We are now ready to add softmax back into each iteration. After generating each row of S_i, all threads in a warp collaborate to derive the maximum of the row (m_i) and the denominator of the row (D_i) ➍. The thread block then uses m_i and D_i to obtain a P row ➎, by applying the softmax function to the row of S_i ➌. The S_i could benefit from more sophisticated optimizations (e.g., hierarchical tiling, tensor cores) as shown in Chapter 15.
FIGURE 20.14. flash attention: device function to update m and D.
elements can be updated into the corresponding P elements in the shared memory. The P row is then multiplied with the V sub-panel and merged into the O tile ➐, using the composition rule in Eq. (20.6). Lines 15-18 of Fig. 20.12 compute the maximum of the S row. First, each thread obtains the current maximum of the S elements that it has computed (line 15). Second, we use CUB’s WarpReduce to compute the maximum inside a warp in line 17. Third, because only thread 0 of a warp receives the result of WarpReduce, the shuffle instruction in line 18 broadcasts the maximum to all threads of the warp. Each thread of the warp saves the maximum in curr_max_warp (line 31 of Fig. 20.9) and call update_m_and_D() (line 33), which is a device function that is shown in Fig. 20.14. This function updates the maximum and the denominator of the row. Inside update_m_and_D() (Fig. 20.14), the threads save the current row maximum m_i into last_m (line 2), and update it if the maximum curr_max_warp calculated for the row is greater than m_i[ii] (line 4). Note that these three values correspond to mr,A (last_m), mr,B (curr_max_warp), and mr,A∪B (m_i[ii]) in Eqs. (20.4) and (20.6), where A is the previous subset of column positions in an S row, and B corresponds to the new subset (i.e., the results of the last matrix multiplication). Next, line 6 computes the term Dr,B emr,B −mr,A∪B of Eq. (20.4). After updating the maximum and the denominator, the threads call compute_P_and_update_D() in line 35 of Fig. 20.9. Fig. 20.15 shows the code of this device function. This function computes the exponential step of softmax and applies the causality policy (line 8) stores them in S_i in shared memory ∑︁(line 9), and accumulates them into D (lines 10-14). This corresponds to the sum j ∈B elr,j −mr,B in Eq. (20.4). In line 10, each thread accumulates the exponentials that it has computed into curr_sum. Line 12 computes the sum reduction in a warp using CUB’s WarpReduce. The subsequent shuffle instruction (line 13) broadcasts the reduction result to all threads of the warp, since they will need to use the denominator for the calculation of their O elements. Line 14 updates the denominator D_i[ii]. Each element or,c should receive contributions from all elements of row r of P. However, each element of the matrix multiplication between the P tile and the V tile FIGURE 20.15 flash attention: device function to compute P and update D.
only carries the contributions from a segment of the row. Thus, one needs to scale the current content of the O tile elements, i.e., A in Eq. (20.6), and the matrix multiplications results, i.e., B in Eq. (20.6), using Eq. (20.6) before accumulating the matrix multiplication result into the output tile. The compute_O() device function, called in line 37 of Fig. 20.9, performs the merge operation that corresponds to Eq. (20.6). This function is in Fig. 20.13 and we discussed the vector-matrix multiplication of S_i and V above. To perform the merge operation, line 3 multiplies O_i[ii][ddd] by exp(last_m - m_i[ii]), which corresponds to the numerator of the first addend of the equation. As we discussed above, the inner for loop (line 5 of Fig. 20.13) performs the inner product between the row of the S_i and a column of V and merges the second addend of the equation. Note that the new contributions are computed based on the maximal of all S elements up to the new row in line 8 of Fig. 20.15. There is no need to rescale these P elements when merging them into O. At the end of the iterative process for a thread block, its output tile contains the full contributions from the conceptual S and P matrices although they were never fully generated and only existed in tiled form in the shared memory. In line 42, the threads call the store_O device function, which stores the output tile O_i and the denominators of softmax D_i into out_O and out_D in global memory. This function is shown in Fig. 20.16. Note that we divide the output values by the denominator, same as in Eq. (20.6), right before storing into global memory (line 6). Note that the iterative process described in this section is only one of the many possibilities. One would use a different process, for example, if the tensor cores are used to compute the two matrix multiplications performed by each warp. We encourage interested readers to explore these potential variations.
FIGURE 20.16. flash attention: device function to store O and the denominator into global memory.
The flash attention kernel in Fig. 20.9 applies to the baseline transformer implementation without KV caching. It also applies to the prefill phase of inference with KV caching. For the generation (decode) phase, with KV caching, Q, S, P , and O matrices become vectors Q′ , S ′ , P ′ , and O ′ . Thus, the computations inside a single head change from matrix multiplications to vector-matrix multiplications. To maintain a similar work distribution to that in Fig. 20.8, we can batch multiple requests, i.e., multiple Q_i, into a single matrix. We will discuss batching in the next section. The most recent version of flash attention [9] takes advantage of new capabilities of the Hopper GPU architecture for an additional performance boost. These capabilities are (1) the Tensor Memory Accelerator (TMA), which can asynchronously move tiles from global memory to shared memory for use by the tensor cores without involving compute threads, (2) the tensor core WarpGroup-wide Matrix Multiply Accumulate (WGMMA) instructions, which are asynchronous in contrast to prior architectures, and (3) FP8 precision of the tensor cores. By using warp specialization, this flash attention version achieves better overlap of data movement and computation, and hides softmax execution with asynchronous block-wise GEMMs.
20.6 KV cache arithmetic intensity and memory requirement
Fig. 20.2 illustrates the generation of the K and V matrices during the prefill phase of an LLM inference with KV caching. K and V elements are calculated and cached in memory. Then, during the generation phase (Fig. 20.6), one new row of K and one new row of V are generated for each new token and appended to the KV cache. These K and V rows are generated with vector-matrix operations, which have low arithmetic intensity and underutilize GPU compute resources (see Chapter 5). For this reason, there are a number of optimizations, such as batching and speculative decoding that aim to increase the arithmetic intensity of the generation phase. Batching is a common optimization in all types of DNNs, and has traditionally been used to increase their arithmetic, i.e., computational, intensity. For example, a FIGURE 20.17 Batching in LLM inference.
linear layer with 1024 inputs and 4096 outputs (in 16-bit precision) has a very low arithmetic intensity of 1 floating point operation per byte (FLOPS/B) when the batch size is 1, but its arithmetic intensity increases to 315 FLOPS/B with batch size 512.9 The increased arithmetic intensity is due to the reuse of weights across input vectors. Such a high arithmetic intensity is critical for high GPU utilization. However, LLMs impose a tradeoff between batch size and context length (i.e., KV cache size). In LLM inference, since input queries from multiple users use the same model (e.g., in a chatbot LLM), the weights can be shared among them. This increases the utilization of the GPU in different parts of the computation. For example, the left part of Fig. 20.17 illustrates batching in the fully-connected layers that generate matrices Q, K, and V . Batching increases the arithmetic intensity of the QKV projection by as many times as the batch size. However, note that the KV cache is dependent on the user’s prompt and context. Thus, attention layers do not benefit from batching because they need to maintain and access separate KV caches for different user’s conversations. As a result, batching in LLMs is limited due to the memory requirements of the model weights and the KV cache. Let’s analyze the memory requirements of LLM inference with KV caching and batching. The two main contributors are the model weights (e.g., matrices WQ, WK , and WV ) and the KV cache. The memory size of the model weights depends on the number of parameters of the model (typically, an order of magnitude of several 9 This is the theoretical increase of the arithmetic intensity. In practice, it depends on the tile size used for the implementation.
billions). For example, a model with 7 billion parameters loaded in 16-bit format (FP16 or BF16) takes about 14 GB. Interested readers are referred to the literature for details on the memory requirements of model weights (e.g., [10,11]). The size of the KV cache depends on the number of transformer layers (l) and the hidden dimension. Recall that the hidden dimension is defined as the product of number of (attention) query heads (hq ) and the embedding dimension (d). Eq. (20.7) shows the size of the KV cache per token in bytes for multi-head attention (MHA). Note that we multiply by 2 to include both K and V matrices. p is the precision in bytes. KV _cache_size_MH A = 2 × l × hq × d × p (20.7)
To estimate the memory requirements in a real scenario, we need to consider the KV cache size across all tokens of the sequences in a batch. Eq. (20.8) show the total size of the KV cache across a batch of b sequences of a certain length N. T otal_KV _cache_size_MH A = b × N × 2 × l × hq × d × p (20.8)
For example, let us find the total KV cache size in 16-bit precision for 1 sequence of 4096 tokens for different example LLMs. For the PaLM 2 LLM from Google, the total KV cache size is 1 × 4096 × 2 × 64 × 8192 × 2 = 8 GB, where 64 is the number of transformer layers (l) and 8192 is the hidden dimension (hq × d). For the GPT-3 LLM from OpenAI, the total size is 1 × 4096 × 2 × 96 × 12288 × 2 = 18 GB. For the Llama 2 7B LLM from Meta, the total size is 1 × 4096 × 2 × 32 × 4096 × 2 = 2 GB. With such large KV cache sizes, memory requirements for batching multiple queries in LLM inference can easily exceed the memory capacity of a single GPU. As a result, multi-GPU and multi-node systems are commonly necessary for LLM inference. Note that in the previous analysis, we assume that all sequences of a batch are of the same length. However, in reality, since queries from different users are different, the input sequences as well as the generated output sequences will likely have very different lengths across different users. Thus, batching in LLM inference may cause imbalance across thread blocks, which would result in under-utilization of the GPU resources. To avoid this situation, state-of-the-art LLM model serving systems apply inflight batching mechanisms [12], which schedule and execute batches of multiple requests at a time and schedule a new request to a batch with the lowest workload as soon as a previous one is served. As mentioned before, while batching is effective at increasing the arithmetic intensity (AI) of linear projection operations, batching is not effective at increasing the arithmetic intensity of MHA during inference. We can estimate the arithmetic intensity of MHA by approximating the amount of data movement and the number of floating point operations [13]. For one layer and sequence length N (and assuming p = 1), from Eq. (20.7), the KV cache size is 2 × hq × d × N . The amount of data movement for reading Q’ and writing O’ is 2 × hq × d. The number of floating point FIGURE 20.18 Speculative decoding.
operations to compute (Q′ K T )V is 2 × N × hq × d (we ignore softmax in this estimate). Eq. (20.9) shows that the AI of MHA is approximately as low as 1 floating point operation per byte. AIMH A ≈ 2 × N × hq × d N = ≈1 2 × hq × d + 2 × hq × d × N 1+N (20.9)
A different approach to increase the arithmetic intensity of LLM inference is speculative decoding [14]. This is a model service optimization where a tiny model called the draft model, predicts the output tokens of a large model called the target model. The target model verifies the predictions by the draft model. While the target model has much longer latency, it performs verification on batches of tokens produced by the draft model in parallel. Fig. 20.18 shows an example of speculative decoding. In this example, the speculation depth is 3, i.e., the draft model predicts three consecutive output tokens. Then, the target model verifies the three tokens in parallel. In the first iteration of this example, the target model accepts Programming'' and Massively’’ but rejects ``Efficient’’. After the verification by the target model, the accepted tokens are incorporated to the input tokens for the second iteration of the draft model, which will predict three new tokens. Speculative decoding is similar to batching, which is why it increases the arithmetic intensity of the linear projection operations. However, it is a special form of batching where all the tokens in the batch have a common initial set of tokens. For this reason, when applying the attention mechanism, the same KV cache data for these common input tokens is loaded and reused to compute all the output tokens. This reuse of KV cache data is what results in the increase in arithmetic intensity. Without speculative decoding, this KV cache data would have been loaded multiple times, once for each output token.
20.7 Alleviating the memory requirements of the attention mechanism
As discussed above, the attention mechanism employs multiple heads. Each of them uses different matrices of weights to perform different projections of Q, K, and V matrices. This is a main reason for the large memory requirements of multi-head attention (MHA) that Eq. (20.8) shows. Two approaches have been proposed to reduce the memory requirements while maintaining an acceptable model accuracy: multi-query attention (MQA) and grouped-query attention (GQA). Figs. 20.19(a), (b), and (c) depict in a simplified way the memory requirements of MHA, MQA, and GQA, respectively. MQA shares keys and values across the attention heads. This reduces the size of the hidden dimension. Eq. (20.10) shows the total KV cache size for MQA. The reduction with respect to Eq. (20.8) is inversely proportional to the number of heads. T otal_KV _cache_size_MQA = b × N × 2 × l × d × p (20.10)
As a result of this reduction in the KV cache size, the AI of MQA is approximately hq times higher than that of MHA, as Eq. (20.11) shows. AIMQA ≈ 2 × N × hq × d N × hq = ≈ hq hq + N 2 × hq × d + 2 × d × N (20.11)
GQA [15] balances between MHA and MQA by using a few groups (gq ) of query heads. Eq. (20.12) shows the total memory requirements of GQA. T otal_KV _cache_size_GQA = b × N × 2 × l × hq ×d ×p gq (20.12)
FIGURE 20.19. Q, K, and V matrices in multi-head attention, multi-query attention, grouped-query attention, and multi-head latent attention.
In the case of GQA, the AI increases by the number of groups gq , as Eq. (20.13) shows. AIGQA ≈ 2 × N × hq × d h 2 × hq × d + 2 × gqq × d × N = N × hq hhq + gqq × N ≈ gq (20.13)
All three of MHA, GQA, and MQA can take advantage of the flash attention tiling approach explained in Section 20.5. The three alternatives entail different tradeoffs in terms of computation, memory requirements, and model accuracy. Although MQA and GQA alleviate the memory requirements of the KV cache, the batch size and the context length can be still constrained by the limited GPU memory capacity. This limitation is worsened by the fact that the memory reserved for KV caches is typically over-provisioned for the largest possible input. As a result, memory tends to be fragmented and wasted. An example attempt to overcome this scenario is the PagedAttention algorithm [12], which is inspired by the traditional paging mechanisms of operating systems. PagedAttention partitions the KV cache into blocks of equal size that are loaded into the GPU memory (from the larger system memory) when they are needed for computation. Another approach to managing the KV cache is Multi-head Latent Attention (MLA) [16], which employs low-rank joint compression of K and V to reduce memory usage drastically. The key idea of MLA is to compress both K and V vectors for each token into a compressed latent vector (Fig. 20.19(d)). This compression is done in all heads and layers so the reduction in the KV caching memory requirement can be very significant, as Eq. (20.14) shows. T otal_KV _cache_size_MLA = b × N × l × d × p (20.14)
The arithmetic intensity (Eq. (20.15)) increases due to sharing a single latent head across a large number of query heads. AIMLA ≈ 2 × N × hq 2 × N × hq × d = ≈ 2 × hq 2 × hq × d + d × N hq + N (20.15)
The projection matrices used in the compression scheme are learned during training. During inference, K and V are generated (decompressed) by multiplying their latent representations by two up-projection matrices before they are used in the attention calculation. As a result, K and V only need to be preserved in their latent, compressed form between iterations in KV caches of all attention heads and decompressed only when a head is active during the generation phase of an LLM inference. A different, orthogonal technique for scaling LLMs is the Mixture of Experts technique (MoE, e.g., Mixtral [17]). MoE is a sparsely activated architecture where a gating mechanism decides what small subset of specialized sub-networks is used for each input. This allows models to be huge in parameter count but have reduced computational needs per token.
20.8 Summary
In this chapter, we introduced the operations that underlie Large Language Models (LLMs). As the leading LLMs are based on the Transformer Architecture, we focus on the unique computational components of Transformers, namely attention heads. We start with a naive, multi-kernel approach with emphasis on the softmax kernel, which is the most novel component of the attention heads. We then discuss KV caching, an important optimization that reduces the redundant computation in each iteration of the generation phase of an LLM inference. We further presented the flash attention formulation and the resulting attention head kernel that improves the arithmetic intensity and reduces the latency of the prefill phase of the LLM inference. The chapter ends with a discussion of the memory requirements of KV caching and techniques to alleviate such requirements when batching is used to improve the GPU utilization during inference. It should be clear that this chapter is designed to provide the reader with a clear understanding of the fundamentals of LLM implementations on GPUs. There is a very large design space that the reader is ready to explore after reading this chapter.
References
20.9 Exercises
\1. Complete the host code for invoking the flash attention kernel in Fig. 20.9. Refer to Fig. 20.8 when setting the sizes of the input and output arrays based on the dimensions of the matrices they hold. 2. Read the CUB document for details of the data types in the WarpReduce namespace and the semantics of the arguments when calling the Reduce function. Pay special attention to the variety of arithmetic functions available to the reduction. 3. Write the initialization device function in line 21 in Fig. 20.9. Explain the reasons why O_i and D_i elements need to be initialized to 0.f. 4. Show that all the loads from K and V in the nested for loop in the device function of Fig. 20.11 are coalesced.
References [1] A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A.N. Gomez, L. Kaiser, I. Polosukhin, Attention is all you need, in: Proceedings of the 31st International Conference on Neural Information Processing Systems, 2017, pp. 6000–6010. [2] D. Ferrucci, E. Brown, J. Chu-Carroll, J. Fan, D. Gondek, A.A. Kalyanpur, A. Lally, J.W. Murdock, E. Nyberg, J. Prager, et al., Building Watson: an overview of the DeepQA project, AI Magazine 31 (3) (2010) 59–79. [3] OpenAI, ChatGPT, https://chat.openai.com/, 2025. (Accessed 25 January 2025). [4] A. Gu, T. Dao, Mamba: linear-time sequence modeling with selective state spaces, arXiv preprint, arXiv:2312.00752, 2023. [5] M. Poli, S. Massaroli, E. Nguyen, D.Y. Fu, T. Dao, S. Baccus, Y. Bengio, S. Ermon, C. Ré, Hyena hierarchy: towards larger convolutional language models, in: International Conference on Machine Learning, PMLR, 2023, pp. 28043–28078. [6] T. Dao, D. Fu, S. Ermon, A. Rudra, C. Ré, FlashAttention: fast and memory-eicient exact attention with IO-awareness, Advances in Neural Information Processing Systems 35 (2022) 16344–16359. [7] M. Milakov, N. Gimelshein, Online normalizer calculation for softmax, arXiv preprint, arXiv:1805.02867, 2018. [8] T. Dao, FlashAttention-2: faster attention with better parallelism and work partitioning, arXiv preprint, arXiv:2307.08691, 2023. [9] J. Shah, G. Bikshandi, Y. Zhang, V. Thakkar, P. Ramani, T. Dao, FlashAttention-3: fast and accurate attention with asynchrony and low-precision, arXiv preprint, arXiv:2407. 08608, 2024. [10] Z. Zhang, D. Zhao, X. Miao, G. Oliaro, Q. Li, Y. Jiang, Z. Jia, Quantized side tuning: fast and memory-eicient tuning of quantized large language models, arXiv preprint, arXiv:2401.07159, 2024. [11] J. Zhao, Z. Zhang, B. Chen, Z. Wang, A. Anandkumar, Y. Tian, Galore: memory-eicient llm training by gradient low-rank projection, arXiv preprint, arXiv:2403.03507, 2024. [12] W. Kwon, Z. Li, S. Zhuang, Y. Sheng, L. Zheng, C.H. Yu, J. Gonzalez, H. Zhang, I. Stoica, Efficient memory management for large language model serving with PagedAttention, in: Proceedings of the 29th Symposium on Operating Systems Principles, 2023, pp. 611–626.
CHAPTER
Electrostatic potential map
With special contributions from John Stone
References
[13] T. Zadouri, H. Strauss, T. Dao, Hardware-eicient attention for fast decoding, arXiv preprint, arXiv:2505.21487, 2025. [14] Y. Leviathan, M. Kalman, Y. Matias, Fast inference from transformers via speculative decoding, in: International Conference on Machine Learning, PMLR, 2023, pp. 19274–19286. [15] J. Ainslie, J. Lee-Thorp, M. de Jong, Y. Zemlyanskiy, F. Lebrón, S. Sanghai, GQA: training generalized multi-query transformer models from multi-head checkpoints, arXiv preprint, arXiv:2305.13245, 2023. [16] A. Liu, B. Feng, B. Wang, B. Wang, B. Liu, C. Zhao, C. Dengr, C. Ruan, D. Dai, D. Guo, et al., Deepseek-v2: a strong, economical, and efficient mixture-of-experts language model, arXiv preprint, arXiv:2405.04434, 2024. [17] A.Q. Jiang, A. Sablayrolles, A. Roux, A. Mensch, B. Savary, C. Bamford, D.S. Chaplot, D.d.l. Casas, E.B. Hanna, F. Bressand, et al., Mixtral of experts, arXiv preprint, arXiv: 2401.04088, 2024.