Skip to content

Chapter 19 Convolutional neural networks

Programming Massively Parallel Processors 5e — English original

Chapter 19 Convolutional neural networks

CHAPTER

With special contributions from Juan Gómez-Luna CONTENTS 19.1 Convolutional neural networks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454 19.2 A CUDA convolutional layer kernel. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460 19.3 Formulating convolutional layer as GEMM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 19.4 CUDNN library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472 19.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474 19.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474 References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475 Deep learning [1] is a branch of the machine learning discipline whose studies give computers the ability to learn to perform certain tasks without being explicitly programmed. Machine learning for a task starts with a human developer architecting a parameterized model for given datasets that explicitly or implicitly specify the expected action for each input instance. The model parameters are ``learned’’ by processing the datasets. The complexity and size of the machine learning models can vary widely based on the task being performed and the size of the datasets used. Deep learning models are designed for complex tasks, such as human facial recognition and natural language processing, whose expected actions can only be captured with and learned from datasets with massive numbers of examples. These models typically employ a deep hierarchy of classifiers that consist of a massive number of parameters to learn a large number of complex features. Thus, they can achieve more accurate pattern recognition results if there is enough training data to allow the system to properly learn the parameter values of all the layers of feature extractors in the model to automatically discover an adequate number of relevant patterns. While machine learning has existed as a subject of computer science for a long time and has been applied successfully to automate many simple, routine tasks, its deep learning branch had stayed dormant for decades because of the lack of massive datasets and the computing power needed to learn the values of the large number of model parameters from the massive datasets. Since 2012, deep learning based on multi-layer Artificial Neural Networks has gained a great deal of industry attention for two reasons. The first reason is the massive datasets available for training deep learning models from the pervasive use of the internet by both consumers and businesses. The second reason is the availability of ample computing power in GPU Programming Massively Parallel Processors. https://doi.org/10.1016/B978-0-44-343900-1.00030-5 Copyright © 2027 Elsevier Inc. All rights are reserved, including those for text and data mining, AI training, and similar technologies.

computing systems that can effectively and timely learn model parameter values from these massive datasets. The readers may find the introduction to machine learning and neural networks in Appendix B helpful. This chapter presents the acceleration of the convolutional neural networks (CNNs) whose demonstrated efficacy in computer vision triggered the first wave of deep learning revolution in 2012. In particular, the convolutional layers of the CNNs have a high compute-to-memory-access ratio, and high levels of parallelism, which makes them a perfect candidate for GPU acceleration. We will present a basic CUDA implementation of the forward (inference) path in a convolutional layer and a few optimizations that improve the speed of this basic implementation. We will then show how one can formulate the convolutional layers into matrix multiplication, which can be systematically accelerated using highly optimized hardware and software in modern GPUs.

Deep learning [1] is a branch of the machine learning discipline whose studies give computers the ability to learn to perform certain tasks without being explicitly programmed. Machine learning for a task starts with a human developer architecting a parameterized model for given datasets that explicitly or implicitly specify the expected action for each input instance. The model parameters are “learned” by processing the datasets. The complexity and size of the machine learning models can vary widely based on the task being performed and the size of the datasets used. Deep learning models are designed for complex tasks, such as human facial recognition and natural language processing, whose expected actions can only be captured with and learned from datasets with massive numbers of examples. These models typically employ a deep hierarchy of classifiers that consist of a massive number of parameters to learn a large number of complex features. Thus, they can achieve more accurate pattern recognition results if there is enough training data to allow the system to properly learn the parameter values of all the layers of feature extractors in the model to automatically discover an adequate number of relevant patterns.

While machine learning has existed as a subject of computer science for a long time and has been applied successfully to automate many simple, routine tasks, its deep learning branch had stayed dormant for decades because of the lack of massive datasets and the computing power needed to learn the values of the large number of model parameters from the massive datasets. Since 2012, deep learning based on multi-layer Artificial Neural Networks has gained a great deal of industry attention for two reasons. The first reason is the massive datasets available for training deep learning models from the pervasive use of the internet by both consumers and businesses. The second reason is the availability of ample computing power in GPU computing systems that can effectively and timely learn model parameter values from these massive datasets. The readers may find the introduction to machine learning and neural networks in Appendix B helpful.

This chapter presents the acceleration of the convolutional neural networks (CNNs) whose demonstrated efficacy in computer vision triggered the first wave of deep learning revolution in 2012. In particular, the convolutional layers of the CNNs have a high compute-to-memory-access ratio, and high levels of parallelism, which makes them a perfect candidate for GPU acceleration. We will present a basic CUDA implementation of the forward (inference) path in a convolutional layer and a few optimizations that improve the speed of this basic implementation. We will then show how one can formulate the convolutional layers into matrix multiplication, which can be systematically accelerated using highly optimized hardware and software in modern GPUs.

19.1 Convolutional neural networks

The CNNs, invented in late 1980s [2], are a type of feed-forward networks (Appendix B). By the early 1990s, CNNs had been experimentally applied to automated speech recognition, optical character recognition (OCR), handwriting recognition, and face recognition [3]. However, until the late 1990s, the mainstream of computer vision and that of automated speech recognition had been based on carefully engineered features. The amount of labeled data was insufficient for training a deep-learning system to compete with recognition/classification functions crafted by human experts. Furthermore, it was a common belief that it was computationally infeasible to automatically learn the parameter values of hierarchical feature extractors with enough layers to outperform human-crafted application-specific feature extractors even if the required labeled datasets existed Interest in deep, feed-forward networks were revived around 2006 by a group of researchers who introduced unsupervised learning methods that could create multilayer, hierarchical feature detectors without requiring labeled data [4,5]. The first major application of this approach was in speech recognition. The breakthrough was made possible by GPUs that allowed researchers to train networks ten times faster than traditional CPUs. This advancement, coupled with the massive amount of media data available online, drastically elevated the position of deep-learning approaches. Despite their success in speech, convolutional neural networks were largely ignored in the field of computer vision until 2012. In 2012, a group of researchers from the University of Toronto trained a convolutional neural network to classify 1000 different classes in the ILSVRC contest [6]. The network was huge by the norms of the time: it had approximately 60 million parameters and 650,000 neurons. It was trained on 1.2 million high-resolution images from the ImageNet database. The network was trained in only one week on two GPUs using a CUDA-based convolutional neural network library written by Alex FIGURE 19.1 LeNet-5, a convolutional neural network for handwritten digit recognition. The letter A in the input should be classified as none of the 10 classes (digits).

Krizhevsky [7]. The network achieved break-through results with a winning test error rate of 15.3%. In comparison, the second-place team that used the traditional computer vision algorithms had an error rate of 26.2%. This success triggered a revolution in computer vision, and convolutional neural networks became a mainstream tool in computer vision, natural language processing, reinforcement learning, and many other areas. We will use LeNet-5, a CNN designed in the late 1980s for digit recognition [3] as a working example. As shown in Fig. 19.1, LeNet-5 is composed from three types of layers: convolutional layers, subsampling layers, and fully connected layers. While these three types of layers continue to be important components of today’s neural networks, the convolutional layers account for the majority of the execution time of CNNs. We will focus on the logical design and acceleration of the convolutional layers. Interested readers can refer to Appendix B and the literature for more treatment of other types of CNN layers. The forward path of LeNet-5 receives input on the left side and flows through the layers from left to right. The input to the network is shown as a gray image with a hand-written digit represented as a 2D 32 × 32 pixel array. The last layer computes the output, which is the probability for the original image to belong to each one of the 10 classes (digits) that the network is set up to recognize. During inference, the output generated by the forward path of the network is used as the answer (i.e., which digit is recognized from the input image) for the input image. During training, on the other hand, the output of the forward path is compared against the label, i.e., the expected output value for the input image. If the output disagrees with the expected output, the backward-propagation path is activated to adjust the model parameters of the network (Appendix B). That is, the forward path is activated during both the training phase and the inference phase. We will therefore use the forward path to illustrate the acceleration of the convolutional layers.1 1 Appendix B also presents the backward-propagation path of CNNs. Interested readers should explore the CUDA kernel implementation of the backward-propagation of the CNN layers by applying the skills that they acquired from studying the forward path.

Figure 19.2

FIGURE 19.2. Forward propagation path of a convolutional layer.

The inputs to and outputs from the CNN layers are referred to as feature maps, or simply, features. For example, in Fig. 19.1, the computation of the C1 convolutional layer (Convolutions) at the input end of the network is organized to generate six (28 × 28) output feature maps from the (32 × 32) INPUT pixel array. The convolutional layer output to be produced for the input feature maps consists of pixels, each of which is produced by performing a convolution between a small local patch of the feature map pixels produced by the previous layer (INPUT in the case of C1) and a set of weights (i.e., a convolution mask as defined in Chapter 7) called a filter. The convolution result is then fed into an activation function like sigmoid (Appendix B) to produce an output pixel in the output feature map. One can think of the convolutional layer as a collection of perceptrons (Appendix B) where each pixel of an output feature map is generated by one of the perceptrons whose inputs are patches of pixels in the input feature maps. For the purpose of this chapter, we will focus on the efficient generation of the convolution result. Once an efficient kernel for generating the convolution result is in place, it is straightforward to add the activation functions either as a separate kernel or at the end the same kernel. That is, for the purpose of this chapter, the value of each output pixel is the sum of convolution results from the corresponding patches in all input feature maps. Fig. 19.2 shows a small convolutional layer example. There are three input feature maps, two output feature maps, and six filters. Different input and output feature map pairs in a layer use different filters. Since there are three input feature maps and two output feature maps in Fig. 19.2, we need 3 × 2 = 6 filters in the bank. For the C3 layer of LeNet in Fig. 19.1, there are 6 input feature maps and 16 output feature maps. Thus, a total of 6 × 16 = 96 filters are used in C3’s filter bank. Fig. 19.2(b) illustrates more details of the calculations done by a convolutional layer. We omitted the activation function for the output pixels for simplicity. We show that each output feature map is the sum of convolutions of all input feature maps. For example, the upper left corner element of output feature map 0 (value 14)

Figure 19.3

FIGURE 19.3. A C implementation of the forward propagation path of a convolutional layer.

is calculated as the convolution between the circled patches of the input feature maps and the corresponding filters2 : (1, 2, 1, 1) · (1, 1, 2, 2) + (0, 2, 0, 3) · (1, 1, 1, 1) + (1, 2, 0, 1) · (0, 1, 1, 0) = 1 + 2 + 2 + 2 + 0 + 2 + 0 + 3 + 0 + 2 + 0 + 0 = 14 (19.1) Although not shown in Fig. 19.1, all 2D filters used in LeNet-5 are 5 × 5 convolution filters. Recall from Chapter 7 that generating a convolution output image from an input image and a convolution filter requires one to make assumptions about the “ghost cells’’. Instead of making such assumptions, the LeNet-5 design simply uses four elements at the right edge and four elements at the bottom edge of each input feature map as ``ghost cells’’. This reduces the size of each dimension by four: four at the right and four at the bottom. As a result, we see that for layer C1, the 32 × 32 INPUT image results in an output feature map that is a 28 × 28 image. Fig. 19.1 illustrates this computation by showing that a pixel in the C1 layer is generated from a square (5 × 5 although not explicitly shown) patch of INPUT pixels. Fig. 19.3 shows convLayer_forward, a sequential C function implementation of the forward propagation path of a convolutional layer. The input feature maps are stored in a 3D array parameter X whose size is C×H×W, where C is the second input 2 Although we use integer values for elements in the feature maps and filter bank for simplicity, they arefloating numbers in reality.

parameter that specifies the number of input feature maps (i.e., number of channels), H is the third input parameter that specifies the height of each input map image, and W is the fourth input parameter that specifies the width of each input map image (line 1). That is, the highest dimension index for X selects one of the feature maps (often referred to as channels) and the indices of the lower two dimensions select one of the pixels in an input feature map. For example, the input feature maps for the C1 layer of Fig. 19.1 are stored in X whose dimensions are 1×32×32 since there is only one input feature map (INPUT in Fig. 19.1) that consists of 32 pixels in each of the x and y dimensions. This also reflects the fact that one can think of the 2D input feature maps to a layer altogether form a 3D input feature map, which is also referred to as the input tensor to the layer. The output feature maps of a convolutional layer are to be stored in a 3D M×(HK+1)×(W-K+1) array parameter Y, where M is the number of output feature maps and K is the height (and width) of each 2D filter (line 1). For example, the output feature maps for the C1 layer in Fig. 19.1 is stored in Y whose dimensions are 6×28×28 since C1 generates six output feature maps using the 5×5 filters. The filter bank is stored in a 4D M×C×K×K array parameter W. There are M×C filters, one for each pair of input feature map and output feature map. Filter F[m, c, _, _] is used when calculating the contribution of input feature map X[c, _, _] to output feature map Y[m, _, ], where ``’’ represents an index that ranges from 0 to K. Recall that each output feature map is the sum of convolutions of all input feature maps. Each iteration of the outermost (m) for-loop (lines 4-12) generates an output feature map. Each iteration of the next two levels (h and w) of for-loops (lines 5-12) generates one pixel of the current output feature map. The next three loop levels (lines 8-11) perform the convolution between the input feature maps and the associated filter banks before accumulating the convolution result for each input feature map into the output feature map (line 11). To complete our example, the convolutional layer C3 in Fig. 19.1 has 6 input feature maps and 16 output feature maps. Each input feature map is a 14×14 image and each output feature map is a 10×10 image. This layer has 6 × 16 = 96 filter banks and each filter bank has 5 × 5 = 25 weights. The output of C3 is passed into the subsampling layer S4 which generates 16 5 × 5 output feature maps. Finally, the last convolutional layer C5 which uses a bank of 16 *120 = 1920 5×5 filters to generate 120 one-pixel output feature maps from its 16 input feature maps. The output feature maps from C5 are passed through fully connected layer F6 which has 84 output units, where each output is fully connected to all inputs. The output is computed as a product of a weight matrix with an input vector, then a bias is added and the output is passed through sigmoid. For F6 in our example, the weight matrix (W) is a 120×84 matrix. In summary, the output is an 84-element vector Y6 = sigmoid(W * X + b). The reader should recognize that this is equivalent to 84 perceptrons and each perceptron takes all 120 one-pixel values generated by the C5 layer as its input. We leave the detailed implementation of a fully connected layer as an exercise.

Figure 19.4

FIGURE 19.4. Forward path of a batched convolutional layer.

The final stage is an output layer that uses Gaussian connections [3] to generate a vector of 10 elements, which correspond to the probability that the input image contains one of the 10 digits (0 through 9). Batched Convolution Layer. To make full use of the computation resources, implementers of CNNs often resort to batched execution by collecting multiple inputs and running them through the network as a batch. The input feature maps and output feature maps generated for each input in the batch will be collectively referred to as a sample in the batch. During batched execution, batching allows the user to launch a much larger grid with more thread blocks, which can significantly improve the utilization of GPU execution resources. Batched CNN execution is especially popular for the training phase since one must run a massive number of inputs through the CNN during training. It is very natural to divide up the large number of training inputs into batches before running them through the network. Fig. 19.4 shows the revised forward path implementation of a convolutional layer that generates the output feature maps for all the samples of a batch. When compared to Fig. 19.3, there is an additional outer loop (line 4) that iterates through all the samples in the batch. Note that X and Y have an additional dimension, which consists of the different samples in the batch, whereas F does not because the same weights are used across samples in the same batch. We will use this batched implementation as the base for the CUDA kernels.

19.2 A CUDA convolutional layer kernel

The computation pattern in training a convolutional neural network is like matrix multiplication: it is both compute intensive and highly parallel. We can process different samples in a mini-batch, different output feature maps for the same sample, and different elements for each output feature map in parallel. In Fig. 19.4, the nloop (line 4, over samples in a mini-batch), m-loop (line 5, over output feature maps), the nested h-w-loops (lines 6-7, over pixels of each output feature map) are all parallel loops in that their iterations can be executed in parallel. These four loop levels together offer a massive level of parallelism. The innermost three loop levels, the c-loop (over the input feature maps or channels) and the nested p-q-loops (over the weights in a filter bank), also offer a significant level of parallelism. However, to parallelize them, one would need to make heavy use of atomic operations in accumulating into the Y elements since different iterations of these loop levels would need to perform read-modify-write on the same Y elements. Therefore, we will keep these loops serial unless we really need more parallelism. Assuming that we exploit the four levels of easy'' parallelism (n, m, h, w) in the convolutional layer, the total number of parallel iterations is the product N \* M \* H_out \* W_out. This high degree of available parallelism makes the convolutional layer a promising candidate for GPU acceleration. We can easily design a kernel with thread organizations designed to capture the parallelism. We need to first make some high-level design decisions on the thread organization. Assume that we will have each thread to compute one element of one output feature map. We will use 2D thread blocks, where each thread block computes a tile of TILE_WIDTH \* TILE_WIDTH pixels in one output feature map. For example, if we set TILE_WIDTH = 16, we would have a total 256 threads per block. This captures part of the nested h-w-loop level parallelism in generating the pixels of each output feature map. By assigning each thread block to generate TILE_WIDTH \* TILE_WIDTH pixels in one of the output feature maps, the easy’’ parallelism can support up to N * M * (H_out/TILE_WIDTH) * (W_out/TILE_WIDTH) thread blocks. Thread blocks can be organized into a 3D grid in several different ways. Each option designates the grid dimensions to capture the n, m, and h-w parallelism in different combinations. We will present the details of one of the options and leave it as an exercise for the reader to explore different options and evaluate the potential pros and cons of each option. The option that we present in detail is as follows: 1. The first dimension (X) corresponds to the (M) output features maps covered by each block. This assignment allows the threads in a thread block to use the X dimension of their common block index (blockIdx.x) to identify the output feature map that is assigned to them. 2. The second dimension (Y) reflects the location of a block’s output tile inside the output feature map selected by X. This assignment allows the threads in a thread block to use the Y dimension of their block index (blockIdx.y) to produce their assigned tile within their assigned output feature map.

Figure 19.5

FIGURE 19.5. Host code for launching a convolutional layer kernel.

\3. The third dimension (Z) in the grid corresponds to inputs (N) in the batch. This assignment allows each block to use the Z dimension of their block index (blockIdx.z) to select one of the samples in the batch as its input. Fig. 19.5 shows the host code that launches a kernel based on the thread organization proposed above. The number of blocks in the X and Z dimensions of the grid are straightforward; they are simply M, the number of output feature maps, and N, the number of samples in a batch. That is, the X index and Z index of a thread block select the output feature map and the input sample of the current batch for the thread block to work on. The arrangement in the Y dimension is a little more complex and is illustrated in Fig. 19.6. Ideally, we would like to dedicate two dimensions of the grid indices to the vertical and horizontal tile indices for simplicity. However, we have only one dimension for both since we are using X for the output feature map index and Z for the sample index in a mini-batch. Therefore, we linearize the tile indices to encode both the horizontal and vertical tile indices of output feature map tiles. In the example in Fig. 19.6, each sample has 4 output feature maps (M = 4) and each output feature map consists of 2 × 2 tiles (W_grid = 2 in line 2 and H_grid = 2 in line 3) of 16 × 16 = 256 pixels each. The grid organization assigns each block to calculate one of these tiles. We have already assigned each output feature map to the X dimension, which is reflected as the four blocks in the X dimension each corresponding to one of the output feature maps. As shown in the bottom of Fig. 19.6, we linearize the 4 tiles in each output feature map and assign them to the blocks in the Y dimension. That is, tiles (0,0), (0,1), (1,0), and (1,1) are mapped using row-major order to the blocks with blockIdx.y values 0, 1, 2, and 3, respectively. Thus, the total number of blocks in the Y dimension is 4 (T = H_grid * W_grid = 4 in line 4). Therefore, we will launch a grid with gridDim(4, 4, N) in lines 6-7. Fig. 19.7 shows a kernel based on the thread organization above. Note that in the code, we use multi-dimensional indices in array accesses for clarity. We leave it to FIGURE 19.6 Mapping output feature map tiles to blocks in the X-Y dimension of the grid. The same mapping is done for all samples in the Z dimension.

Figure 19.7

FIGURE 19.7. Kernel for the forward path of a convolutional layer.

19.3 Formulating convolutional layer as GEMM

reader to translate this pseudo-code into regular C assuming that X, Y, and F must be accessed via linearized indexing based on row-major layout (Chapter 3). Each thread starts by generating the n (sample within batch), m (feature map), h (vertical), and w (horizontal) indices of its assigned output feature map pixel. The n (line 6) and m (line 3) indices are straightforward given the host code. For the h index calculation in line 4, the blockIdx.y value is first divided by W_grid to recover the tile index in the vertical direction, as illustrated in Fig. 19.6. This tile index is then expanded by the TILE_WIDTH and added to the threadIdx.y to form the actual vertical pixel index into the output feature map (line 4). The derivation of the horizontal pixel index is similar (line 5). Each thread generates an output feature map pixel iteratively. In each iteration, it performs a convolution between a patch of the input feature map pixels and the selected convolution filter bank (lines 7-12). The convolution result is accumulated in the acc local variable. The process completes when the thread finishes the convolution for the same patch over all input feature maps. The thread will then assign the final result into the output feature map pixel (line 13). The kernel in Fig. 19.7 has a high degree of parallelism but consumes too much global memory bandwidth. Like the convolution pattern discussions in Chapter 7, the execution speed of the kernel will be limited by the global memory bandwidth. As we have also seen in Chapter 7, we can use techniques such as shared memory tiling to dramatically reduce the global memory traffic and improve the execution speed of the kernel. These optimizations to the convolution layer kernel are left as an exercise for the reader. Instead, we will present a more systematic approach to reducing memory bandwidth consumption in the following section.

The convolutional layer presents multiple opportunities for reducing memory bandwidth consumption through data reuse. Since the execution speed of the kernel shown in Fig. 19.7 is limited by global memory bandwidth, any improvement in memory bandwidth consumption would likely lead to higher execution speed. For example, each input feature map is used in calculating all output feature maps. One can potentially tile the input feature maps and reuse these tiles in calculating multiple output feature maps. However, different thread blocks are used to calculate different output feature maps in the convolution layer kernel in Fig. 19.7. Tiling input feature maps into one thread block’s shared memory would not allow other thread blocks to reuse the tiled data. Enabling reuse of input feature map pixels would require major redesign of the kernel. This section presents an approach proposed by Chellapilla et al. [8] to formulating convolutional layers into equivalent matrix multiplication operations. With this formulation, well-defined tiling techniques (Chapter 15) can be readily used to enable the reuse of the input feature maps as well as the filter banks to reduce global memory bandwidth consumption. In fact, one can just use or slightly adapt one of thehighly optimized GEMM (General Matrix Multiply) kernels from the CUDA linear algebra library cuBLAS to implement high-performance convolutional-layer kernels. The central idea of the formulation is that each output feature map element is a dot product between elements of the input feature maps and the elements of their corresponding filter banks. Since matrix multiplication consists of dot products between row vectors of the first input and column vectors of the second input, one should be able to arrange the input feature maps and the filter banks into matrices so that each output element of the output feature map is an element of the product matrix. That is, one can make arrangements so that the filter banks needed to generate one output feature map pixel form one row vector of the first input matrix. One can also unfold and duplicate the input feature map pixels in such way that all elements needed to compute one output feature map pixel are stored as one column of the second input matrix.3 An example of such rearrangements is shown in Fig. 19.8. Consider a small example convolutional layer which takes as one input C = 3 feature maps each of which is of size 3×3 and produces M = 2 output features each of which is of size 2×2, as was shown in Fig. 19.2(b) and is shown again at the top of Fig. 19.8 for convenience. It uses M×C = 6 filter banks, where each filter bank is of size 2×2. Since the computation performed for each sample in a batch is completely independent of others in the same batch, Fig. 19.8 shows the input feature maps and the output feature maps of only one sample whose index is assumed to be n. Let’s consider the calculation needed to generate output feature map pixel Yn,0,0,0, i.e., pixel (0,0) of output feature map 0 of sample n shown at the upper left corner of Fig. 19.8, whose value should be 14. All the patches of input feature pixels needed for calculating Yn,0,0,0 are circled in the input feature maps. The expression for generating the Y0,0,0 is: Yn,0,0,0 = (1, 1, 2, 2) · (1, 2, 1, 1) + (1, 1, 1, 1) · (0, 2, 0, 3) + (0, 1, 1, 0) · (1, 2, 0, 1) = (1 + 2 + 2 + 2) + (0 + 2 + 0 + 3) + (0 + 2 + 0 + 0) = 14 (19.2) where the first input of each inner product is a vector that is formed by linearizing (in row-major order, Chapter 3) the filter bank used for the convolution. For example, the vector formed by linearizing filter bank 0 using row major order is (1, 1, 2, 2), which is the first input to the first inner product. Since the layout of C matrices in the global memory is already in row major order, there is no rearrangement needed inside these filter banks! All one needs to do is to ensure that the filter banks are concatenated in the order where all filter banks for an output feature with a larger index follow those for an output feature map with a smaller index. Among filter banks for the same output feature map, a filter bank for an input feature map with larger index follows those with smaller index. In our example, the filter banks should be concatenated in the order 3 See also https://petewarden.com/2015/04/20/why-gemm-is-at-the-heart-of-deep-learning/ for very de tailed explanation.

Figure 19.8

FIGURE 19.8. Formulation of convolutional layer as GEMM. Each sample in the batch has its own input feature maps and output feature maps but share the same filter banks. Since the samples within a batch are completely independent of each other, this figure shows the input feature maps and the output feature maps of only one sample (whose index is assumed to be n).

F0,0 , F0,1 , F0,2 , F1,0 , F1,1 , F1,2 . Recall that the dimensions of F array in Fig. 19.3 (line 11) and Fig. 19.7 (line 11) are assumed to be m×c×p×q. With the output feature map index as the highest dimension and the input feature map as the second highest dimension, the filter banks are already concatenated in the desired order! With this order, the filter banks form a row major 2 × (3 ∗ 4) = 2 × 12 matrix, as shown on the lower left side of Fig. 19.8. The bottom line is that the filter banks are already organized for the matrix multiplication formulation without any further rearrangement. The second input to each inner product for calculating Yn,0,0,0 is a vector formed by linearizing (in row-major order) a patch of input map X pixels which are circled in Fig. 19.8. For example, the vector formed by linearizing the circled patch in input feature map 0 using row major order is (1, 2, 1, 1), which is the second input to the first inner product. We will further concatenate the linearized vectors from all theinput feature maps into a single vector, which allows us to reformulate the three inner products for calculating Yn,0,0,0 into one inner product: Yn,0,0,0 = (1, 2, 1, 1, 0, 2, 0, 3, 1, 2, 0, 1) · (1, 1, 2, 2, 1, 1, 1, 1, 0, 1, 1, 0) = 1 + 2 + 2 + 2 + 0 + 2 + 0 + 3 + 0 + 2 + 0 + 0 = 14 (19.3) The first input to the inner product is the vector formed by concatenating F0,0, F0,1 , and F0,2 . The second input is the concatenation of the linearized patches of the input feature map. If we make the first input vector a row of a matrix F and the second input vector a column of a matrix B, the inner product for producing Yn,0,0,0 would be naturally part of the matrix-matrix multiplication of F and B. During matrix multiplication, one row of the filter bank matrix (F) and the column of the input feature matrix (B) will produce one pixel of the output feature map. Since we already know how to form matrix F, we just need to complete the formation of matrix B from the input feature map patches. Note that there are H_out * W_out pixels in each output feature map and each column of the B matrix is used to generate one output map pixel. Therefore, B must consist of H_out * W_out columns. In our example, H_out = 2 and W_out = 2 so matrix B consists of four columns, as shown in Fig. 19.8. Each column of B will be generated by unfolding and concatenating patches of the input feature maps. In our example, since we have determined that each column of B consists of 3 ∗ 4 = 12 elements, the B matrix is of size 12 × 4, as shown in Fig. 19.8. By ordering the columns of B properly, we can generate the output feature maps in their desired row-major order in Y. For example, in the bottom half of Fig. 19.8, the top section of the output feature map matrix is the row-major view of output feature map 0 and the bottom is that of output feature map 1. Both are already in rowmajor order assumed by the C language so they can be used as input feature maps for the next layer. In general, the output feature map matrix generated by the matrix multiplication is the correct form of Y[n, m, p, q] with the p and q dimensions viewed in their linearized row-major order. The matrix multiplication generates the output feature maps in exactly the form of the input feature maps that is expected by the following or downstream layers in the network. Cost of Explicitly Unfolding Input Feature Maps While it is tempting to present the details of rearranging the input feature maps X into matrix B as our next step, we will first take a pause and analyze the cost of explicitly generating and storing B. As we see in Fig. 19.8, the B matrix has 12 ∗ 4 = 48 elements whereas the three the input feature maps altogether consist of only 3 ∗ 3 ∗ 3 = 27 elements. That is, there is a significant size increase of 1.78× from X to B. The root cause of the size increase is that the patches of input feature map for calculating different pixels of the output feature map overlap with each other due to the nature of convolution. This means that each input feature map pixel can be replicated multiple times as we produce matrix B. For example, in Fig. 19.8 the center pixel of each 3 × 3 input feature map is used four times to compute the four pixels of an output feature, so it will be duplicated four times when generating the B matrix. The pixels in the middle the edges of each input feature map are used two times so it will be duplicated two times. The four pixels at corners of each input feature map are used only one time and will not need to be duplicated. Therefore, the total number of pixels in the matrix B for one input feature map is 4 * 1 + 2 * 4 + 1 * 4 = 16. Since each original input feature map has only 9 pixels, the explicit generation of matrix B incurs an expansion ratio of 16/9 = 1.78× over the original input feature maps. In general, the size of the unfolded input feature map matrix B can be derived from the number of input feature map elements required to generate each output feature map element. The height, or the number of rows, of the expanded matrix is the number of input feature elements contributing to each output feature map element, which is C * K * K; each output element is the convolution of K * K elements from each input feature map and there are C input feature maps. In our example, the K is 2 since the filter bank is 2 × 2 and there are three input feature maps. Thus, the height of the expanded matrix should be 3 * 2 * 2 = 12, which is exactly the height of the matrix shown in Fig. 19.8. The width, or the number columns, of the unfolded matrix B is the number of elements in each output feature map, i.e., H_out * W_out. In our example, each output feature map is a 2 × 2 matrix, i.e., H_out = 2 and W_out=2, yielding four columns in the expanded matrix. Note that the number of output feature maps M does not play into the duplication. This is because all output feature maps are computed from the same unfolded input feature map matrix B. The ratio of expansion for the input feature maps is the size of the expanded matrix over the total size of the original input feature maps. The reader should verify that the expansion ratio is: K 2 ∗ H _out ∗ W _out C ∗ K ∗ K ∗ H _out ∗ W _out = C ∗ H _in ∗ W _in H _in ∗ W _in (19.4)

where H_in and W_in are the height and width of each input feature map. In our example, the ratio is (3 * 2 * 2 * 2 * 2)/(3 * 3 * 3) = 16/9. In general, if the input feature maps and output feature maps are much larger than the filters, the ratio of expansion will approach K 2 . While the expansion seems to be modest for our small example, for practical feature map sizes where H_out and W_out are in the range of hundreds or thousands and K can be 5 or larger, the expansion can be easily 20× or more! Furthermore, explicit generation of unfolded matrix B requires an extra step that will further add to global memory bandwidth consumption, negating any reduction in global memory bandwidth consumption from the matrix multiplication formulation. This motivates the implicit approach as follows. An Implicit Approach to Unfolding Input Feature Maps To avoid the downside of input feature map size expansion, practical systems that implement convolutional layers with matrix multiplications typically generate the B matrix in a piece-meal manner as part of the tiled matrix multiplication process. The idea is that tiled matrix multiplication kernels, such as the one shown in Fig. 5.9, load FIGURE 19.9 During each phase of a tiled matrix multiplication that implements a convolutional layer, the B matrix tile elements are loaded from their corresponding X elements.

tiles of the two input matrices before performing a phase of the multiplication for a tile of the output matrix. This presents an opportunity for leaving the input feature maps in their original form in the global memory. When a tile of the B matrix is to be loaded from the global memory, we adapt the tile loading code to assemble the B matrix tile by loading the pixels from the corresponding locations in the X array. In computer science terms, matrix B is only conceptual and is materialized on-demand in a tile-by-tile manner during a tiled matrix multiplication process. The key to the implicit approach is a systematic mapping between the B matrix elements and the X array elements. That is, when conceptual B element B[u,v] is to be loaded into a tile, one can identify a unique X array element to be used. For example, assume that the matrix multiplication is performed as a tiled matrix multiplication with 2 × 2 tiles, which is illustrated in Fig. 19.9(a). In this small example, the 2 × 2 tile configuration will result in launching a grid that consists of two thread blocks with 2 ∗ 2 = 4 threads each. Each thread generates one output feature map pixel and each block will collaboratively generate a tile of output feature map pixels. We will focus on the thread block that generates the output tile that consists pixels, i.e., Y[n,0,0,0], Y[n,0,0,1], Y[n,0,1,0], and Y[n,0,1,1], i.e., the left half of the Y array in the marked box in Fig. 19.9(a). For the phase 0 of the matrix multiplication, the B tile to be loaded consists of B[0,0], B[0,1], B[1,0], and B[1,1] (upper left corner of B in the marked box in Fig. 19.9(a)). As shown by the arrows in Fig. 19.9, B[0,0] can be loaded from X[n,0,0,0], B[0,1] from X[n,0,0,1], B[1,0] from X[n,0,0,1], and B[1,1] from X[n,0,0,2]. Fig. 19.9(b) shows the relevant mappings for phase 1 of the matrix multiplication. The B tile to be loaded by the same thread block during this phase will consists of FIGURE 19.10 A mapping example for B[10,2], i.e., u=10 and v=2. B[2,0], B[2,1], B[3,0], and B[3,1]. As shown by the arrows in Fig. 19.9(b), B[2,0] can be loaded from X[n,0,1,0], B[2,1] from X[n,0,1,1], B[3.0] from X[n,0,1,1], and B[3,1] from X[n,0,1,2]. We will use these two tiles as examples as we derive the general formula for mapping from B elements to X elements. Assume that a thread is responsible for B[u,v] when its thread block loads a tile. From our discussion above, v is a row-major linearized index that identifies the output feature map pixel to receive the contribution from this B element. We can recover the last two dimensions of the index into Y as v/W_out, v%W_out. That is, the output feature map element is Y[n,m,v/W_out,v%W_out]. These two indexes also identify the beginning position (upper left corner) of the patch of input feature map pixels involved in the convolution operation that generates the output feature map pixel. The u index specifies the input feature map where the patch of pixels comes from and the row-major linearized index for its B element inside the patch. We can identify the channel number of the input feature map by u/(K 2 ). For example, in Fig. 19.10, u is equal to 2 for element B[10,2]. We know that B[10,2] comes from input feature map 2 (c = 2) since 10/22 = 2. Indeed, Fig. 19.10 shows that B[10,2] resides in the bottom section of B, which comes from input feature map 2.

Within the patch, u%K 2 is the row-major linearized index of the pixel. That is, (u%K 2 )/K is the local row index and (u%K 2 )%K the column index inside the patch. For example, for B[10,2], (10%4)/2 = 1 and (10%4)%2 = 0, which means that B[10,2] came from row 1 and column 0 of the input patch. Next, we project these local indexes within a patch onto the input feature map that contains the patch. This can be done by adding these local indexes to the indexes of the beginning pixel (upper left corner) of the patch, i.e., (v/W_out, v%W_out). Therefore, the row index and column index of the pixel within the selected input feature map are (u%K 2 )/K + v/W _out and (u%K 2 )%K + v%W _out, respectively. In our example, the input feature map pixel for B[10,2] has a row index of 1 + 1 = 2 and column index of 0 + 0 = 0. We also know that the pixel comes from input feature map 2. Thus, the pixel can be loaded from X[n,2,2,0] whose value is 3. In summary, we have the following mapping from the conceptual B matrix to the input feature map array X when formulating a batched convolutional layer into matrix-matrix multiplication: B[u, v] ← X[n, u/(K 2 ), (u%K 2 )/K + v/W _out, (u%K 2 )%K + v%W _out] (19.5) where n is the sample index in the batch currently being processed, K is the height and width of each filter bank of the convolutional layer, W _out is the width of each output feature map of the convolutional layer, / is integer divide operation, and % is integer remainder operation. Fig. 19.11 shows a CUDA tiled matrix multiplication kernel that implements a convolutional layer. The kernel is adapted from the tiled matrix multiplication kernel in Fig. 5.9. First, the parameters are adapted to specify the number of input maps (i.e., channels C), the number of output maps (M), the height of each input map (H), the width of each input feature map (W), the height/width of each filter (K), a pointer to the filter bank array (F), a pointer to the input feature map array (X), and a pointer to the output feature map array (Y) (line 3). The kernel assumes that it is launched with the input batch size as the Z dimension size (number of blocks) of the grid. Thus, each thread uses its blockIdx.z as an index to its assigned sample in the input feature map array (line 9). This index will be used when loading the tiles for B (line 28). As was in Fig. 5.9, each thread uses its block and thread indices in the X and Y dimensions to calculate the row and column indices of its assigned output feature map element (lines 14-15). The number of phases for the tiled matrix multiplication is the width of the F matrix (C×K×K) divided by the tile width (TILE_WIDTH) (line 19). Loading the F matrix tile (line 23) uses the same row and column indices as Fig. 5.9. Note that we did not include bounds checking in this version since we focus on the adaptation of the indices used for loading the B matrix tile. We leave the addition of bounds checking as an exercise. The row and column indices into B is the same as those used for matrix N in Fig. 5.9. The difference is that we need to map the access to B to an access to X, which follows Eq. (19.5). Note that we linearize the index to the 4D N×C×H×W X FIGURE 19.11 A tiled matrix multiplication kernel that implements a convolutional layer.

array using row-major layout (line 28). W _out is expressed as W-K+1, where W is the width of the input feature map. After the thread block completes all phases, each thread writes into its designated Y element using the sample index, the row index, and the column index (line 37).

Implementing convolutions with matrix multiplication can be very efficient since matrix multiplication is highly optimized on all hardware platforms. Tiled matrix multiplication is especially fast on GPUs because it has a high ratio of floating-point operations per byte of global memory data access. Recall from Chapter 5 that the number of reuse for each element is proportional to the dimension of the tiles. This implies that matrix multiplication is less efficient on small matrices whose small size may restrict the tile size. Furthermore, the kernel launch overhead tends to be significant for small matrix multiplication. Accordingly, the matrix multiplication approach to convolution is most effective when it creates large matrices for multiplication. As we mentioned earlier, the filter-bank matrix is an M × (C*K*K) matrix and the unfolded input feature map matrix is a (C*K*K) × (H_out*W_out) matrix. Note that except for the height of the filter-bank matrix, the sizes of all dimensions depend on products of the parameters to the convolution, not the parameters themselves. While individual parameters can be small, their products tend to be large. For example, it is often true that in early layers of a convolutional network, C is small, but H_out and W_out are large. On the other hand, at the end of the network, C is large, but H_out and W_out are small. Hence, the product C * H_out * W_out is usually large for all layers. This means that the sizes of the matrices tend to be consistently large for all layers and thus the utilization of GPU execution resources using this approach tends to be high for all layers in the network, resulting in high execution speed.

19.4 CUDNN library

CUDNN is a library of optimized routines for implementing deep learning primitives. It was designed to make it much easier for deep learning frameworks to take advantage of GPUs. It provides a flexible and easy-to-use C-language deep learning API that integrates neatly into existing deep learning frameworks (e.g., PyTorch, Caffe, TensorFlow, MXNet, . . . ). The library requires that input and output data be resident in the GPU device memory as we discussed earlier in this chapter. This requirement is analogous to that of cuBLAS. The library is thread-safe in that its routines can be called from different host threads. Let us consider CNNs to explain the capabilities provided by CUDNN. Convolutional routines for the forward and backward paths use a common descriptor that encapsulates the attributes of the layer. Tensors and filters are accessed through opaque descriptors, with the flexibility to specify the tensor layout using arbitrary strides along each dimension. The most important computational primitive in convolutional neural networks is a special form of batched convolution. In this section, we describe the forward form of this convolution. The CUDNN parameters governing this convolution are listed Table 19.1. There are two inputs to the convolution: • D is a four-dimensional N * C * H * W tensor, which contains the input data,4 4 Tensor is a mathematical term for arrays that have more than two dimensions. In mathematics, matriceshave only two dimensions. Arrays with three or more dimensions are called tensors. For the purpose of this book, one can simply treat a T-dimensional tensor as a T-dimensional array.

Table 19.1 Convolution parameters for CUDNN. Note that the CUDNN naming convention is slightly different than what we have been using in previous sections. Parameter N C H W K R S u v pad_h pad_w Meaning Number of images in mini-batch Number of input feature maps Height of input image Width of input image Number of output feature maps Height of filter Width of filter Vertical stride Horizontal stride Height of zero-padding Width of zero-padding • F is a four-dimensional K * C * R * S tensor, which contains the convolutional filters. The input data array (tensor) D ranges over N samples in a mini-batch, C input feature maps per sample, H rows per input feature map, and W columns per input feature map. The filters range over K output feature maps, C input feature maps, R rows per filter bank, and S columns per filter bank. The output is also a four-dimensional tensor O that ranges over N samples in the mini-batch, K output feature maps, P rows per output feature map, and Q columns per output feature map, where P = f(H; R; u; pad_h) and Q = f(W; S; v; pad_w), meaning that the height and width of the output feature maps depend on the input feature map and filter bank height and width, along with padding and striding choices. The striding parameters u and v allow the user to reduce the computational load by computing only a subset of the output pixels. The padding parameters allow users to specify how many rows or columns of 0 entries are appended to each feature map for improved memory alignment and/or vectorized execution. CUDNN [9] supports multiple algorithms for implementing a convolutional layer: matrix-multiplication-based (GEMM [10] and Winograd [11]), FFT-based [12], etc. The GEMM-based algorithm to implement the convolutions with a matrix multiplication is similar to the approach presented in Section 19.3. As we discussed at the end of Section 19.3, materializing the expanded input feature matrix in global memory can be costly in both the global memory space and bandwidth consumption. CUDNN avoids this problem by lazily generating and loading the expanded input feature map matrix into on-chip memory only (in a similar way to the implicit approach presented in the previous section), rather than by gathering it in off-chip memory before calling a matrix multiplication routine. NVIDIA provides a matrix-multiplication-based routine that achieves a high utilization of the maximaltheoretical floating-point throughput on GPUs. The algorithm for this routine is similar to the algorithm described in [10]. Fixed sized sub-matrices of the input matrices A and B are successively read into on-chip memory and are then used to compute a sub-matrix of the output matrix C. All indexing complexities imposed by the convolution are handled in the management of tiles in this routine. We compute on tiles of A and B while fetching the next tiles of A and B from off-chip memory into on-chip caches and other memories. This technique hides the memory latency associated with the data transfer, allowing the matrix multiplication computation to be limited only by the time it takes to perform the arithmetic calculations. Since the tiling required for the matrix multiplication routine is independent of any parameters from the convolution, the mapping between the tile boundaries of the expanded input feature map matrix and the convolution problem is non-trivial. Accordingly, the CUDNN approach entails computing this mapping and using it to load the correct elements of A and B into on-chip memories. This happens dynamically as the computation proceeds, which allows the CUDNN convolution implementation to exploit optimized infrastructure for matrix multiplication. It requires additional indexing arithmetic compared to a matrix multiplication, but fully leverages the computational engine of matrix multiplication to perform the work. After the computation is complete, CUDNN performs the required tensor transposition to store the result in the user’s desired data layout.

19.5 Summary

This chapter starts with a brief introduction to machine learning and convolutional neural networks. Then, we present a basic convolutional neural network and the implementation of its major types of layers. We then build on the convolution pattern in Chapter 7 to present a CUDA kernel implementation of the convolutional layer, the most computationally intensive layer of convolutional neural networks. We then present techniques for formulating convolutional layers as matrix multiplications by implicitly unrolling the input feature maps into a matrix. The conversion allows the convolutional layers to benefit from highly optimized GEMM libraries for GPUs. We also present the CUDA implementation of a matrix multiplication kernel that performs implicit unrolling of the input feature map. We end the chapter with an overview of the CUDNN library, which is used by most deep learning frameworks. Users of these frameworks can benefit from the highly optimized layer implementations without writing CUDA kernels themselves.

19.6 Exercises

\1. Implement the forward pass for the subsampling layer that is introduced in the Section 19.1. An explanation about this type of layer can be found in Appendix B.

References

\2. We used an [N * C * H * W] layout for input and output features. Can we reduce the memory bandwidth by changing it to an [N * H * W * C] layout? What are potential benefits of using a [C * H * W * N] layout? 3. Implement the backward pass for the convolutional layer introduced in the Section 19.1, and further described in Appendix B. 4. Analyze the memory access patterns in the matrix multiplication kernel of Fig. 19.11 and discuss whether the memory accesses to global memory are coalesced and whether the shared memory accesses may suffer from bank conflicts.

References [1] Y. LeCun, Y. Bengio, G. Hinton, Deep learning, Nature 521 (7553) (2015) 436–444. [2] Y. LeCun, L. Bottou, Y. Bengio, P. Haffner, Gradient-based learning applied to document recognition, Proceedings of the IEEE 86 (11) (1998) 2278–2324. [3] Y. LeCun, B. Boser, J. Denker, D. Henderson, R. Howard, W. Hubbard, L. Jackel, Handwritten digit recognition with a back-propagation network, Advances in Neural Information Processing Systems 2 (1989). [4] G.E. Hinton, S. Osindero, Y.-W. Teh, A fast learning algorithm for deep belief nets, Neural Computation 18 (7) (2006) 1527–1554. [5] R. Raina, A. Madhavan, A.Y. Ng, Large-scale deep unsupervised learning using graphics processors, in: Proceedings of the 26th Annual International Conference on Machine Learning, 2009, pp. 873–880. [6] A. Krizhevsky, I. Sutskever, G.E. Hinton, ImageNet classification with deep convolutional neural networks, Advances in Neural Information Processing Systems 25 (2012). [7] A. Krizhevsky, cuda-convnet, https://code.google.com/p/cuda-convnet/, 2014. [8] K. Chellapilla, S. Puri, P. Simard, High performance convolutional neural networks for document processing, in: Tenth International Workshop on Frontiers in Handwriting Recognition, Suvisoft, 2006. [9] S. Chetlur, C. Woolley, P. Vandermersch, J. Cohen, J. Tran, B. Catanzaro, E. Shelhamer, cuDNN: efficient primitives for deep learning, arXiv preprint, arXiv:1410.0759, 2014. [10] G. Tan, L. Li, S. Triechle, E. Phillips, Y. Bao, N. Sun, Fast implementation of DGEMM on Fermi GPU, in: Proceedings of 2011 International Conference for High Performance Computing, Networking, Storage and Analysis, 2011, pp. 1–11. [11] A. Lavin, S. Gray, Fast algorithms for convolutional neural networks, in: Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 2016, pp. 4013–4021. [12] N. Vasilache, J. Johnson, M. Mathieu, S. Chintala, S. Piantino, Y. LeCun, Fast convolutional nets with fbfft: a GPU performance evaluation, arXiv preprint, arXiv:1412.7580, 2014.

CHAPTER

Large language models

With special contributions from Juan Gómez-Luna

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.

References

[1] Y. LeCun, Y. Bengio, G. Hinton, Deep learning, Nature 521 (7553) (2015) 436–444.

[2] Y. LeCun, L. Bottou, Y. Bengio, P. Haffner, Gradient-based learning applied to document recognition, Proceedings of the IEEE 86 (11) (1998) 2278–2324.

[3] Y. LeCun, B. Boser, J. Denker, D. Henderson, R. Howard, W. Hubbard, L. Jackel, Handwritten digit recognition with a back-propagation network, Advances in Neural Information Processing Systems 2 (1989).

[4] G.E. Hinton, S. Osindero, Y.-W. Teh, A fast learning algorithm for deep belief nets, Neural Computation 18 (7) (2006) 1527–1554.

[5] R. Raina, A. Madhavan, A.Y. Ng, Large-scale deep unsupervised learning using graphics processors, in: Proceedings of the 26th Annual International Conference on Machine

[6] A. Krizhevsky, I. Sutskever, G.E. Hinton, ImageNet classification with deep convolutional neural networks, Advances in Neural Information Processing Systems 25 (2012).

[7] A. Krizhevsky, cuda-convnet, https://code.google.com/p/cuda-convnet/, 2014.

[8] K. Chellapilla, S. Puri, P. Simard, High performance convolutional neural networks for document processing, in: Tenth International Workshop on Frontiers in Handwriting

[9] S. Chetlur, C. Woolley, P. Vandermersch, J. Cohen, J. Tran, B. Catanzaro, E. Shelhamer, cuDNN: efficient primitives for deep learning, arXiv preprint, arXiv:1410.0759, 2014.

[10] G. Tan, L. Li, S. Triechle, E. Phillips, Y. Bao, N. Sun, Fast implementation of DGEMM on Fermi GPU, in: Proceedings of 2011 International Conference for High Performance Computing, Networking, Storage and Analysis, 2011, pp. 1–11.

[11] A. Lavin, S. Gray, Fast algorithms for convolutional neural networks, in: Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition, 2016,

[12] N. Vasilache, J. Johnson, M. Mathieu, S. Chintala, S. Piantino, Y. LeCun, Fast convolutional nets with fbfft: a GPU performance evaluation, arXiv preprint, arXiv:1412.7580,