Methods, processors, computing devices, and program products that perform attention computations

By quantizing the K and V matrices in the Attention calculation, the problem of low memory transfer efficiency in Attention calculation is solved, the calculation efficiency is improved and the quantization error is reduced, thus ensuring the calculation accuracy.

CN123180219APending Publication Date: 2026-11-27HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510697904.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-27
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

During the Attention calculation process, the efficiency of moving the K and V matrices from memory to the processor cache is low, resulting in insufficient computational efficiency.

Method used

During the Attention calculation process, the K and V matrices are quantized. By setting the matrix blocks as the unit, the numerical precision of the matrix blocks is reduced, and outlier smoothing is performed on the K matrix to reduce the impact of outliers and improve loading efficiency.

Benefits of technology

This improves the efficiency of the processor in performing Attention calculations, reduces quantization errors, and ensures calculation accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123180219A_ABST
    Figure CN123180219A_ABST
Patent Text Reader

Abstract

The embodiment disclosed by the application belongs to the technical field of artificial intelligence, and particularly relates to a method for performing Attention calculation, a processor, a computing device and a program product. The method comprises the following steps: in the case that a first K vector is added to a K matrix, and the unquantized K vectors included in the K matrix reach a first number, performing quantization processing on a first matrix block composed of the first number of unquantized K vectors to obtain a second matrix block. Based on the second matrix block included in the K matrix, a mean vector for calculating the second matrix block, and the Q matrix, calculation is performed to obtain a similarity matrix. Based on the similarity matrix and the V matrix, calculation is performed to obtain a calculation result of the Attention calculation. By using the application, the efficiency of the processor in performing the Attention calculation can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of artificial intelligence technology, and in particular to a method, processor, computing device, and program product for performing attention calculations. Background Technology

[0002] Attention mechanisms are a way of processing input data in the field of artificial intelligence (AI) computing, including self-attention mechanisms, multi-head attention mechanisms, and cross-attention mechanisms.

[0003] In the attention mechanism, the input matrix of the AI ​​model can be used to generate corresponding query (Q) matrices, key (K) matrices, and value (V) matrices from pre-trained weight matrices. Then, attention calculations are performed based on the Q, K, and V matrices, including determining the similarity between the Q and K matrices, and then weighting the similarity with the V matrix to obtain the output matrix. The input matrix can consist of embedding vectors corresponding to multiple tokens.

[0004] Because the Attention calculation process involves a large amount of data movement, such as moving the K matrix from memory to the cache corresponding to the processor's computing unit multiple times, the efficiency of performing Attention calculations is relatively low. Summary of the Invention

[0005] This application provides a method, processor, computing device, and program product for performing attention calculations, improving the efficiency of the processor in performing attention calculations. The corresponding technical solutions are as follows:

[0006] Firstly, a method for performing attention computation is provided. This method can be executed by a processor (such as an NPU). The method includes obtaining the embedding vector corresponding to a first token, calculating an unquantized first K vector corresponding to the first token based on the embedding vector, and adding the unquantized first K vector to a K matrix. After adding the first K vector to the K matrix, if the number of unquantized K vectors in the K matrix reaches a first number, quantization processing is performed on a first matrix block composed of the first number of unquantized K vectors to obtain a second matrix block. The second matrix block includes a first number of quantized second K vectors. The first matrix block in the K matrix is ​​then updated to the second matrix block. The quantization processing includes calculating the mean vector of the first number of unquantized K vectors, calculating the difference vector between each unquantized K vector and the mean vector, and quantizing the resulting multiple difference vectors to obtain the first number of second K vectors. Based on the second matrix block in the K matrix, the mean vector used to calculate the second matrix block, and the Q matrix, a similarity matrix is ​​calculated. A calculation is performed based on the similarity matrix and the V matrix to obtain the result of the attention computation. The result is used to calculate the embedding vector corresponding to the second token following the first token.

[0007] In the scheme shown in this application, after generating the K vectors included in the K matrix each time, if it is determined that the unquantized K vectors included in the K matrix form a first matrix block, the first matrix block can be quantized to obtain a second matrix block. This way, updating the first matrix block of the K matrix to the quantized second matrix block reduces the storage space occupied by the K matrix. Subsequently, the efficiency of loading the K matrix from memory to the cache during each Attention calculation will be improved, thereby increasing the processor's efficiency in performing Attention calculations. Furthermore, considering the high probability of the K matrix containing outliers, the quantization process includes subtracting the corresponding mean vector from the K vectors in the first matrix block, which can reduce the abnormality of outliers. Therefore, quantizing the first matrix block after this process reduces quantization error.

[0008] In one feasible approach, after adding a first K vector to the K matrix, if the K matrix contains fewer than a first number of unquantized K vectors and includes a quantized second matrix block, a similarity matrix is ​​calculated based on the second matrix block included in the K matrix, the mean vector used to calculate the second matrix block, and a third matrix block composed of the unquantized K vectors included in the K matrix, along with the Q matrix. The similarity matrix is ​​then used to perform calculations based on the similarity matrix and the V matrix to obtain the result of the Attention calculation.

[0009] In the scheme shown in this application, if the K matrix already includes a second matrix block and unquantized K vectors, the processor can control the second matrix block included in the K matrix and the third matrix block composed of the unquantized K vectors to calculate with the Q matrix respectively to obtain the similarity matrix. In this way, on the one hand, including the quantized second matrix block in the K matrix improves the efficiency of loading the K matrix into the cache; on the other hand, the unquantized K vectors are the K vectors corresponding to the most recently generated multiple tokens, and have a high correlation with the next token to be generated. Therefore, calculating with the Q matrix using the unquantized third matrix block can improve the accuracy of generating the next token.

[0010] In the scheme shown in this application, a similarity matrix is ​​obtained by calculating the mean vector of the second matrix block included in the K matrix and the Q matrix. This includes: for each second matrix block included in the K matrix, calculating the fourth matrix block based on the second matrix block and its corresponding quantization coefficients with the Q matrix; calculating the fifth matrix block based on the mean vector of the second matrix block with the Q matrix; and summing the fourth and fifth matrix blocks to obtain a sixth matrix block. Based on the sixth matrix block corresponding to the second matrix block, normalization is performed to obtain the similarity matrix. Thus, during the calculation of the K and Q matrices, the processor can achieve dequantization of the second matrix block by calculating the fourth matrix block and compensate for the mean vector by calculating the fifth matrix block, thereby improving the accuracy of the calculated similarity matrix.

[0011] In one feasible approach, before performing calculations based on the similarity matrix and the V matrix, the method further includes: calculating an unquantized first V vector corresponding to the first token based on the embedding vector, and adding the unquantized first V vector to the V matrix. After adding the first V vector to the V matrix, if the V matrix contains a second number of unquantized V vectors, the seventh matrix block consisting of the second number of unquantized V vectors is quantized to obtain an eighth matrix block, which includes the second number of quantized second V vectors. The seventh matrix block included in the V matrix is ​​then updated to the eighth matrix block. Performing calculations based on the similarity matrix and the V matrix to obtain the result of the Attention calculation includes: performing calculations based on the eighth matrix block included in the V matrix and the similarity matrix to obtain the result of the Attention calculation.

[0012] In the scheme shown in this application, during the calculation of the K matrix, the first matrix block included in the K matrix can be quantized to improve the efficiency of loading the K matrix into the cache. Similarly, during the calculation of the V matrix, the seventh matrix block included in the V matrix can be quantized to improve the efficiency of loading the V matrix into the cache. Thus, by quantizing the K and V matrices, the efficiency of loading the K and V matrices into the cache can be improved, thereby improving the efficiency of the processor's attention calculation.

[0013] In one feasible approach, after adding a first V vector to the V matrix, if the V matrix contains fewer than a second number of unquantized V vectors and includes an eighth matrix block, a calculation is performed based on the similarity matrix and the V matrix to obtain the result of the Attention calculation. This includes: calculating the result of the Attention calculation based on the eighth matrix block included in the V matrix and a ninth matrix block composed of unquantized V vectors included in the V matrix, and comparing it with the similarity matrix.

[0014] In the scheme shown in this application, if the V matrix already includes an eighth matrix block and unquantized V vectors, the processor can control the eighth matrix block and the ninth matrix block composed of the unquantized V vectors in the V matrix to perform calculations with the similarity matrix to obtain the Attention calculation result. This improves the efficiency of loading the V matrix into the cache by including the quantized eighth matrix block, and the unquantized V vectors, which correspond to the recently generated multiple tokens, have a high correlation with the next token to be generated. Therefore, calculating the similarity matrix using the unquantized ninth matrix block improves the accuracy of generating the next token.

[0015] In one feasible approach, the second number is greater than the first number. Considering the low probability of the V matrix including outliers, the size of the seventh matrix block can be increased relative to the K matrix, thereby reducing the number of quantizations of the V matrix and improving the efficiency of the processor in performing attention calculations.

[0016] In one feasible approach, attention computation includes attention computation for each head in a multi-head attention mechanism, which can improve the efficiency of attention computation for each channel in the multi-head attention mechanism.

[0017] In a second aspect, a processor is provided that can be used to execute the method for performing attention calculations as described in the first aspect or any of the implementable methods of the first aspect.

[0018] Thirdly, a computing device is provided, the computing device including a memory and a processor as described in the second aspect above.

[0019] Fourthly, a computer program product is provided, comprising instructions that execute on a processor to implement the method for performing Attention calculation as described in the first aspect or any of the implementable methods in the first aspect.

[0020] Fifthly, a computer storage medium is provided, which stores instructions that are executed on a processor to implement the method for performing Attention calculation as described in the first aspect or any of the implementable methods in the first aspect. Attached Figure Description

[0021] Figure 1 This is a flowchart of the Attention calculation process in a processor.

[0022] Figure 2 This is a flowchart of a method for performing Attention calculation provided in an embodiment of this application;

[0023] Figure 3 This is a schematic diagram of an Attention calculation process provided in an embodiment of this application;

[0024] Figure 4 This is a schematic diagram illustrating the calculation of the K matrix and Q matrix provided in an embodiment of this application;

[0025] Figure 5 This is a flowchart of a method for quantizing a V matrix provided in an embodiment of this application;

[0026] Figure 6 This is a schematic diagram of an Attention calculation process provided in an embodiment of this application;

[0027] Figure 7 This is a schematic diagram illustrating the calculation of a V matrix and a similarity matrix provided in an embodiment of this application;

[0028] Figure 8 This is a flowchart of a method for performing Attention calculation provided in an embodiment of this application;

[0029] Figure 9 This is a schematic diagram of the structure of a processor provided in an embodiment of this application;

[0030] Figure 10 This is a schematic diagram of the structure of a computing device provided in an embodiment of this application. Detailed Implementation

[0031] To make the objectives, technical solutions, and advantages of this application clearer, the embodiments of this application will be described in further detail below with reference to the accompanying drawings.

[0032] Attention mechanisms are a way of processing input data in the field of artificial intelligence (AI) computing, including self-attention mechanisms, multi-head attention mechanisms, cross-attention mechanisms, etc.

[0033] In the attention mechanism, the input data of the AI ​​model can be used to generate Q, K, and V matrices from pre-trained weight matrices. Then, attention calculations can be performed on the Q, K, and V matrices to determine the similarity between the Q and K matrices and thus determine the attention weights. Finally, the V matrix is ​​weighted according to the attention weights to obtain the output matrix.

[0034] In one example, the AI ​​model can be a large language model, and the input data can be a text sequence. During the response generation process, the AI ​​model does not receive the entire text sequence as input, but rather some initial information (such as the question, hints, previous replies, etc.). This initial information is usually converted into a set of initial embedding vectors. In each response generation inference step, the AI ​​model, based on the output vectors of the previous inference step (in the first case, the initial embedding vectors), K and V vectors, adds the generated K vector to the K matrix and the V vector to the V matrix, calculates the similarity between the generated Q matrix and K matrix, and then transforms it into a probability distribution using a normalized exponential function (softmax function) to generate a similarity matrix. Finally, by performing operations on the similarity matrix and the V matrix, the output vector is obtained, such as the embedding vector corresponding to a token in the response, and is passed as the input vector to the next inference step to generate subsequent tokens. This process proceeds gradually, until the AI ​​model reaches the end marker (such as "..."). <eos>The process of generating a response will terminate when the target length is reached (marked) or when the predetermined length is reached. At this point, the AI ​​model has generated a complete response.

[0035] Data types are categories used in computer programs to represent data, defining the data's size, storage method, and executable operations. Common data types include integers (int), single-precision floating-point numbers (float), and double-precision floating-point numbers (double). Different data types occupy different numbers of bytes; generally, the more bytes a data type occupies, the larger the range of values ​​it can represent, and the higher its precision.

[0036] Quantization refers to the process of mapping continuous values ​​to a smaller set of discrete values. In the field of AI, quantization mainly involves converting the weight parameters and activation values ​​of an AI model from a high-precision data type (such as FP32) to a low-precision data type (such as INT8 or INT4). This can reduce the storage space and computational requirements of the AI ​​model, thereby improving inference performance and speeding up inference, but it will affect the inference accuracy of the AI ​​model.

[0037] Dequantization is the reverse process of quantization, restoring the quantized parameters to a high-precision data type. During dequantization, the quantized parameters are scaled and zero-point adjusted to restore them to a high-precision data type for further computation.

[0038] Figure 1 This is a flowchart of the attention calculation process in a processor. In one example, Figure 1 The processor shown can be a Neural Processing Unit (NPU), but it is not limited to a Graphics Processing Unit (GPU) or a Central Processing Unit (CPU). An NPU includes at least a control unit, a computation unit, and a cache. The control unit can be the control circuitry that implements the control functions within the NPU, such as data transfer and issuing computation instructions. The computation unit can be used to implement the computational circuitry required for Attention calculations, such as addition circuits for floating-point or integer calculations, exponentiation circuits, and circuits for vector or matrix calculations. Taking a large language model as an example, the process of generating a token through Attention calculation in the AI ​​model includes:

[0039] Step S1: The control unit loads the embedding vector corresponding to the most recently generated token and the weight matrix corresponding to the K matrix stored in memory into the cache, controls the calculation unit to execute the K vector by combining the embedding vector and the weight matrix corresponding to the K matrix, and then stores the K vector back into memory and merges it with the already generated K matrix to form a new K matrix.

[0040] Step S2: The control unit loads the embedding vector corresponding to the most recently generated token and the weight matrix corresponding to the Q matrix stored in memory into the cache, and controls the calculation unit to execute the calculation of the embedding vector and the weight matrix corresponding to the Q matrix to obtain the Q matrix.

[0041] Step S3: The control unit loads the K matrix currently stored in memory into the cache, controls the calculation unit to perform calculations on the K matrix and Q matrix to obtain the similarity matrix between the K matrix and Q matrix, and then normalizes the similarity matrix using the softmax function to obtain the normalized similarity matrix.

[0042] Step S4: The control unit loads the embedding vector corresponding to the most recently generated token and the weight matrix corresponding to the V matrix stored in memory into the cache, controls the calculation unit to execute the embedding vector and the weight matrix corresponding to the V matrix to obtain the V vector, and then stores the V vector back into memory and merges it with the already generated V matrix to form a new V matrix.

[0043] Step S5: The control unit loads the V matrix currently stored in memory into the cache, and controls the calculation unit to perform calculations on the V matrix and the attention weight matrix to obtain the output matrix. This output matrix is ​​used to calculate the embedding vector corresponding to the next token.

[0044] In implementation, by iteratively executing steps S1 to S5, the output response of the large language model can be obtained. However, each time steps S3 and S4 are executed, the K and V matrices stored in memory need to be loaded into the NPU's cache. Furthermore, as the number of iterations increases, the K and V matrices contain more K and V vectors respectively, leading to a corresponding increase in loading time. The time spent loading the K and V matrices into the cache is a key factor affecting the efficiency of NPU's Attention calculations.

[0045] This application provides a method for performing Attention calculations. During the processor's Attention calculation process, the K and V matrices are quantized in units of defined matrix blocks. By reducing the numerical precision of the matrix blocks included in the K and V matrices, efficient loading of the K and V matrices in steps S3 and S4 is achieved, thereby improving the processor's efficiency in performing Attention calculations. Considering the high probability of outliers in the K vector, outlier smoothing can be performed on the matrix blocks included in the K matrix during quantization to reduce the impact of outliers on the quantization results. Furthermore, after loading the quantized matrix blocks included in the K matrix into the cache, compensation can be applied to the smoothed matrix blocks to ensure the accuracy of the Attention calculations.

[0046] Figure 2 This is a flowchart illustrating a method for performing Attention calculation according to an embodiment of this application. The Attention calculation can be performed by the above-described... Figure 1 The processor shown executes the commands; see [link / reference]. Figure 2 The method includes:

[0047] Step 201: Obtain the embedding vector corresponding to the first token, calculate the unquantized first K vector corresponding to the first token based on the embedding vector, and add the unquantized first K vector to the K matrix.

[0048] Steps 201 to 203 shown in this embodiment represent the process of generating a token through the Attention calculation performed by the processor during AI model inference. Specifically, during the initial execution of step 201, the embedding vector corresponding to the first token can be the initial embedding vector. During the second and subsequent executions of step 201, the embedding vector corresponding to the first token can be the embedding vector corresponding to the token generated in the previous execution of steps 201 to 203. In one example, the processor can execute the inference process of a large language model. Steps 201 to 205 represent the process by which the large language model generates any word in the response text for the question text. The first token can be the token corresponding to the previous word in the response text generated by the large language model. The subsequent second token is the word generated after the processor executes steps 201 to 203, based on the output matrix, representing the word following the word corresponding to the first token. Here, a word can refer to a word, character, etc.

[0049] Figure 3 This is a schematic diagram illustrating an Attention calculation process provided in an embodiment of this application. For example... Figure 3 As shown, in implementation, the control unit can load the embedding vector corresponding to the first token from memory into the cache, and load the weight matrix corresponding to the K matrix into the cache. The control unit can control the computing unit to calculate the embedding vector and the weight matrix to obtain the first K vector, and then load the first K vector from the cache into memory.

[0050] The storage location of the K matrix can be set in memory. During the initial execution of step 201, the K matrix can be empty, and the generated first K vector can be stored according to the storage location, resulting in the first K vector included in the K matrix. During the second and subsequent executions of step 201, the offset of the storage location can be calculated based on the number of times step 201 has been executed. Then, the first K vector is stored according to this offset, thus sequentially storing the previously calculated first K vectors, thereby adding the first K vector to the K matrix.

[0051] In one example, the data types of the embedded vector and the weight matrix corresponding to the calculated K matrix are both high-precision floating-point numbers. The calculated unquantized first K vector can also be a high-precision floating-point number, such as FP16.

[0052] Step 202: After adding the first K vector to the K matrix, if the number of unquantized K vectors in the K matrix reaches a first number, perform quantization processing on the first matrix block composed of the first number of unquantized K vectors to obtain a second matrix block. The second matrix block includes the first number of quantized second K vectors. Update the first matrix block in the K matrix to the second matrix block. The quantization processing includes calculating the mean vector of the first number of unquantized K vectors, calculating the difference vector between each unquantized K vector and the mean vector, and quantizing the multiple difference vectors to obtain the first number of second K vectors.

[0053] After adding the first K vector to the K matrix each time, it can be determined whether the current K matrix contains a first number of unquantized K vectors. If it is determined that the K matrix contains a first number of unquantized K vectors, these first number of unquantized K vectors can form a first matrix block. For example... Figure 3 As shown, for the first matrix block, the control unit can load the first matrix from memory into the cache, and control the computing unit to perform quantization processing on the first matrix to obtain the quantized second matrix block. The control unit can load the second matrix block into memory, which can replace the first matrix block included in the K matrix. Since the second matrix block is quantized, it occupies less storage space than the first matrix block. In this way, by quantizing the first matrix block once, each subsequent loading of the K matrix from memory into the cache can replace the loading of the first matrix block with loading the second matrix block, thereby improving the loading efficiency of the K matrix each time, and thus improving the efficiency of the processor in performing Attention calculations.

[0054] The quantization process for the first matrix block may include outlier smoothing and quantization.

[0055] Considering that the K vectors in the K matrix have a high probability of including outliers, and that outliers cause significant quantization errors, this embodiment of the application centralizes the first matrix block before quantization to smooth outliers. Centralization involves determining the mean vector obtained by averaging multiple K vectors across the sequence dimension in the first matrix block. Specifically, multiple K vectors are summed digit by digit, and each digit of the sum is divided by the number of K vectors to obtain the mean vector. Then, the mean vector is subtracted from each K vector in the first matrix block to obtain the centralized first matrix block. This centralization smooths the distribution of outliers in the K vectors of the first matrix block, reduces the deviation of outliers, and consequently reduces the quantization error of the centralized first matrix block.

[0056] After smoothing outliers in the first matrix block, the smoothed first matrix block can be quantized to obtain a second matrix block containing a first number of second K vectors and the corresponding quantization parameters (Scale). The data type of the second K vectors represents a numerical precision lower than that represented by the data type of the second K vectors; for example, the data type of the second K vectors is INT8.

[0057] It should be noted that, when dequantizing the first matrix block after quantization, compensation for outlier smoothing can be performed based on the mean vector corresponding to the first matrix block to ensure the accuracy of subsequent calculations. The compensation for outlier smoothing will not be described here; please refer to step 203 for details.

[0058] In this embodiment, on the one hand, quantizing the first matrix block reduces the storage space occupied by the K matrix and improves the efficiency of loading the K matrix into the cache. On the other hand, quantizing the K matrix in units of the first matrix block reduces the number of quantization operations, avoiding quantization after each first K vector is obtained, thus reducing the impact of quantization on the execution efficiency of Attention calculation. Furthermore, considering the outlier characteristics of the K vector during quantization of the first matrix block, outlier smoothing is performed on the first matrix block to reduce the error caused by quantization and ensure the accuracy of Attention calculation.

[0059] Step 203: Based on the second matrix block included in the K matrix, the mean vector of the second matrix block is used to calculate the similarity matrix with the Q matrix. The similarity matrix and the V matrix are used to perform calculations to obtain the calculation result of the Attention calculation. The calculation result is used to calculate the embedding vector corresponding to the second token after the first token.

[0060] After replacing the first matrix block included in the K matrix with the second matrix block, the control unit can load at least one second matrix block included in the K matrix, the mean vector used to calculate the second matrix, and the quantization parameters corresponding to the second matrix into the cache, so as to calculate the similarity matrix with the Q matrix. In one example, the current step 203 omits the processor's calculation of the Q matrix and V matrix and the processing of loading them into the cache, as detailed in steps S1 to S5 above, and will not be repeated here.

[0061] The calculation of the second matrix block, the mean vector, and the Q matrix may include:

[0062] For each second matrix block in the K matrix, the fourth matrix block is calculated using the second matrix block, its corresponding quantization coefficients, and the Q matrix. The fifth matrix block is then calculated using the mean vector corresponding to the second matrix block and the Q matrix. The fourth and fifth matrix blocks are summed to obtain the sixth matrix block. Normalization is then performed on the sixth matrix block corresponding to the second matrix block to obtain the similarity matrix.

[0063] In implementation, since the Q matrix is ​​not quantized, the data type of the second matrix block can be converted to the same data type as the Q matrix before calculating the Q matrix and the second matrix block. Then, during the calculation of the Q matrix and the second matrix block, the second matrix block is dequantized, and compensation is performed for outlier smoothing. In other words, during the calculation of the Q matrix and the second matrix block, the quantization coefficients corresponding to the second matrix block and the mean vector used to calculate the second matrix block can be controlled to participate in the calculation of the Q matrix and the second matrix.

[0064] In one example, the quantization coefficients corresponding to the Q matrix and the second matrix block can be calculated. The result is then compared with the converted data type of the second matrix block to calculate the fourth matrix block after dequantization. Since each second matrix block undergoes outlier smoothing, the mean vector from the outlier smoothing process can be used to compensate for the fourth matrix block. For example, calculating the Q matrix and the mean vector yields the fifth matrix block. Adding the fourth and fifth matrix blocks together results in the sixth matrix block. After obtaining the sixth matrix block, it can be further processed using the softmax function to obtain the matrix blocks included in the similarity matrix.

[0065] For example, the Q matrix and the second matrix block can be calculated using the following formulas (1) and (2):

[0066]

[0067] in, This refers to changing the data type of the second matrix block from the quantized data type to the data type before quantization, for example, changing the second matrix block from INT8 to FP16. This refers to the second matrix block after the data type has been changed. q represents the Q matrix. This refers to the quantization coefficients for the second matrix block. It refers to the mean vector used to smooth out the outliers of the second matrix block.

[0068] In another example, the data type of the Q matrix can be quantized to be the same as that of the second matrix block before performing the calculation of the Q matrix and the second matrix block.

[0069] For example, the Q matrix and the second matrix block can be calculated using the following formulas (3) and (4):

[0070]

[0071] in, This refers to quantizing the Q matrix to the same data type as the second matrix block, such as INT8. (8) The quantized Q matrix, This is the corresponding quantization system. This is the second matrix block. b This represents the calculation result, and the corresponding data type is consistent with the data type of the Q matrix before quantization.

[0072] In one feasible approach, the calculation of the similarity matrix and the V matrix may include, after calculating the similarity matrix, the control unit loading the V matrix from memory into a cache, and controlling the calculation unit to calculate the similarity matrix and the V matrix stored in the cache to obtain the result of the Attention calculation. In one example, after obtaining the calculation result, the embedding vector corresponding to the next output token of the large language model can be determined based on the calculation result.

[0073] In one possible implementation, the process of updating the first matrix block to the second matrix block in step 202 can be performed after the second matrix block is calculated with the Q matrix in step 203. In this way, the second matrix block can be stored in a cache, calculated with the Q matrix first, and then written into memory to update the first matrix block. This reduces one step of reloading the second matrix block into the cache, improving the efficiency of the processor in performing Attention calculations.

[0074] In steps 201 to 203 above, since the first matrix block included in the K matrix is ​​quantized, the loading efficiency of the K matrix can be improved, thereby improving the efficiency of the processor in performing Attention calculations.

[0075] In one possible implementation, if, after adding the first K vector to the K matrix in step 201, the K matrix contains fewer than a first number of unquantized K vectors and does not include a second matrix block, it indicates that the K vectors in the current K matrix have not yet reached a first matrix block, meaning the condition for quantizing the K matrix has not been met. Therefore, in this case, step 202 will not be triggered. The Attention calculation performed on the K, Q, and V matrices in step 203 can be found in steps S2 to S5 above, and will not be repeated here.

[0076] In one possible implementation, after adding the first K vector to the K matrix in step 201, if the K matrix contains fewer unquantized K vectors than the first number, but still includes a second matrix block, it indicates that the current K matrix already includes the quantized second matrix block. However, the unquantized K vectors currently included in the K matrix cannot yet form the first matrix block, thus not satisfying the condition for quantizing the K matrix again. Therefore, in this case, step 202 will not be triggered.

[0077] The attention calculation performed on the K matrix, Q matrix, and V matrix in step 203 may include: calculating a similarity matrix based on the second matrix block included in the K matrix, the mean vector used to calculate the second matrix block, and the third matrix block composed of the unquantized K vectors included in the K matrix, and performing calculations on the Q matrix to obtain the result of the attention calculation.

[0078] Figure 4 This is a schematic diagram illustrating the calculation of the K and Q matrices according to an embodiment of this application. Figure 4 As shown, the second matrix block and Q matrix included in the K matrix can be calculated according to the calculation method shown in step 203 above, resulting in the matrix blocks included in the similarity matrix. This will not be elaborated further here. For the third matrix block in the K matrix, which ends with quantized K vectors, since the third matrix block is not quantized, its data type is consistent with that of the Q matrix. Therefore, the third matrix block and the Q matrix can be directly controlled for calculation to obtain the remaining uncalculated matrix blocks in the similarity matrix.

[0079] For example, the similarity matrix of each second matrix block and the Q matrix can be calculated based on the above formulas (3) and (4) to obtain the matrix blocks included in the similarity matrix without the softmax function processing, such as P1, P2, ..., P N For the remaining third matrix block The remaining matrix blocks of the similarity matrix before the softmax function can be obtained by calculating based on the following formula (5) and the Q matrix, such as P. t .

[0080]

[0081] Because the data types corresponding to the output of formula (4) and formula (5) are consistent, P1, P2, ..., P N With P t The similarity matrix P, processed by the softmax function, is obtained by splicing the components and then using the following formula (6).

[0082] P = [P1, P2, ..., P N ,P t S = softmax(P) Formula (6)

[0083] Since the correlation between adjacent tokens is high during the inference process of the AI ​​model, the token corresponding to the unquantized K vector at the end of the K matrix is ​​adjacent to the next generated token. Therefore, not quantizing the K vector at the end of the K matrix can improve the accuracy of the token generated by the AI ​​model.

[0084] In the embodiments of this application, the V matrix can also be quantized during the Attention calculation process. Figure 5 This is a flowchart of a method for quantizing a V matrix provided in an embodiment of this application, such as... Figure 5 As shown, the method includes:

[0085] Step 501: Calculate the unquantized first V vector corresponding to the first token based on the embedded vector, and add the unquantized first V vector to the V matrix.

[0086] Similar to steps 201 to 203 above, steps 501 to 503 can represent the process of generating a token through the processor's Attention calculation during AI model inference. Specifically, when step 501 is executed for the first time, the embedding vector corresponding to the first token can be the initial embedding vector. When step 501 is executed for the second time and thereafter, the embedding vector corresponding to the first token can be the embedding vector corresponding to the token generated in the previous execution of steps 501 to 503.

[0087] Figure 6 This is a schematic diagram illustrating an Attention calculation process provided in an embodiment of this application. For example... Figure 6 As shown, in implementation, the control unit can load the embedding vector corresponding to the first token from memory into the cache, and load the weight matrix corresponding to the V matrix into the cache. The control unit can control the computing unit to calculate the embedding vector and the weight matrix to obtain the first V vector, and then load the first V vector from the cache into memory.

[0088] The storage location of the V matrix can be set in memory. During the initial execution of step 501, the V matrix can be empty, and the generated first V vector can be stored according to the storage location, resulting in the first first V vector included in the V matrix. During the second and subsequent executions of step 501, the offset of the storage location can be calculated based on the number of times step 501 has been executed. Then, the first V vector is stored according to this offset, thus sequentially storing the previously calculated first V vectors, thereby adding the first V vector to the V matrix.

[0089] In one example, the data types of the embedding vector and the weight matrix corresponding to the calculated V matrix are both high-precision floating-point numbers. The calculated unquantized first V vector can also be a high-precision floating-point number, such as FP16.

[0090] Step 502: After adding the first V vector to the V matrix, if the number of unquantized V vectors in the V matrix reaches a second number, quantize the seventh matrix block composed of the second number of unquantized V vectors to obtain the eighth matrix block. The eighth matrix block includes the second number of quantized second V vectors. Update the seventh matrix block in the V matrix to the eighth matrix block.

[0091] After adding the first V vector to the V matrix each time, it can be determined whether the current V matrix contains a second number of unquantized V vectors. If it is determined that the V matrix contains a second number of unquantized V vectors, these second number of unquantized V vectors can form a seventh matrix block. For example... Figure 6 As shown, for the seventh matrix block, the control unit can load the seventh matrix from memory into the cache, and control the computing unit to perform quantization on the seventh matrix to obtain the quantized eighth matrix block. The control unit can then load the eighth matrix block into memory, potentially replacing the seventh matrix block included in the V matrix. Since the eighth matrix block is quantized, it occupies less storage space than the seventh matrix block. Thus, by quantizing the seventh matrix block once, subsequent loadings of the V matrix from memory into the cache can replace the loading of the seventh matrix block with loading the eighth matrix block, thereby improving the loading efficiency of the V matrix and consequently improving the efficiency of the processor in performing Attention calculations.

[0092] Considering the low probability of outliers in the V vector of the V matrix, outlier smoothing is unnecessary when quantizing the seventh matrix block within the V matrix. This improves the efficiency of quantization while maintaining accuracy. In one example, since the probability of outliers in the V vector is low, the second number can be set greater than the first. This ensures batch processing of the seventh matrix block within the V matrix, improving loading efficiency, while simultaneously reducing the number of quantization operations on the V matrix and improving the computational efficiency of Attention, all while maintaining accuracy.

[0093] Step 503: Based on the eighth matrix block included in the V matrix, perform calculations with the similarity matrix to obtain the calculation results of the Attention calculation.

[0094] In one example, steps 501 to 503 omit the processor's calculation of the Q matrix and V matrix and the loading of them into the cache. For details, please refer to steps S1 to S5 above, which will not be repeated here.

[0095] The calculation of the eighth matrix block of matrix V and the similarity matrix can include: performing calculations based on each eighth matrix block included in matrix V, and the quantization coefficients corresponding to each eighth matrix block, along with the similarity matrix, to obtain the calculation result. In one example, since the eighth matrix block is quantized, it can first be set to an eighth matrix block with the same data type as the similarity matrix. Then, the similarity matrix and the eighth matrix block are calculated, and the calculation result is then calculated with the quantization coefficients to achieve inverse quantization of the calculation result.

[0096] For example, the eighth block of the V matrix and the similarity matrix can be calculated using the following formulas (7) and (8):

[0097]

[0098] in, This refers to changing the data type of the eighth matrix block from the quantized data type to the data type before quantization, for example, changing the eighth matrix block from INT8 to FP16. This refers to the eighth matrix block after the data type has been changed. b Represents the similarity matrix. This refers to the quantization coefficients of the eighth matrix block.

[0099] In one possible implementation, the process of updating the seventh matrix block to the first matrix block in step 502 can be performed after the eighth matrix block is calculated with the similarity matrix in step 503. In this way, the first matrix block can be stored in a cache, first calculated with the similarity matrix, and then written into memory to update the seventh matrix block. This reduces one step of reloading the eighth matrix block into the cache, improving the processor's efficiency in performing Attention calculations.

[0100] In one possible implementation, if, after adding the first V vector to the V matrix in step 501, the V matrix contains fewer than the second number of unquantized V vectors and does not include an eighth matrix block, it indicates that the current V matrix does not yet contain a seventh matrix block, meaning the condition for quantizing the V matrix has not been met. Therefore, in this case, step 202 will not be triggered. For step 503, which calculates the V matrix and the similarity matrix, please refer to steps S2 to S5 above; it will not be repeated here.

[0101] In one possible implementation, if after adding the first V vector to the V matrix, the V matrix contains fewer unquantized V vectors than the second number, and the V matrix includes an eighth matrix block, it indicates that the current V matrix already includes the quantized eighth matrix block. However, the unquantized V vectors currently included in the V matrix are insufficient to form a seventh matrix block, thus not satisfying the condition for quantizing the V matrix again. Therefore, in this case, step 502 will not be triggered.

[0102] Step 503, which involves performing calculations on the V matrix and the similarity matrix, may include calculations based on the eighth matrix block included in the V matrix and the ninth matrix block composed of unquantized V vectors included in the V matrix, along with the similarity matrix, to obtain the calculation results of the Attention calculation.

[0103] Figure 7 This is a schematic diagram illustrating the calculation of a V matrix and a similarity matrix provided in an embodiment of this application. For example... Figure 7 As shown, the eighth matrix block included in the V matrix can be calculated according to the calculation method shown in step 503 above, resulting in the matrix block included in the similarity matrix. This will not be elaborated further here. For the ninth matrix block in the V matrix, which ends with quantized V vectors, since the ninth matrix block is not quantized, its data type is consistent with the data type corresponding to the similarity matrix. Therefore, the ninth matrix block can be directly controlled to calculate with the similarity matrix, obtaining the remaining uncalculated matrix blocks in the result matrix.

[0104] Since adjacent tokens are highly correlated during the inference process of the AI ​​model, the token corresponding to the unquantized V vector at the end of the V matrix is ​​adjacent to the next generated token. Therefore, not quantizing the V vector at the end of the V matrix can improve the accuracy of the token generated by the AI ​​model.

[0105] In one feasible approach, the attention calculation shown in the above embodiment can be applied to the attention calculation for each head in a multi-head attention mechanism. This can improve the execution efficiency of the attention calculation for each channel in the multi-head attention mechanism.

[0106] Figure 8 This is a flowchart illustrating a method for performing Attention calculation according to an embodiment of this application. This method can be executed by a processor, such as... Figure 8 As shown, the method includes:

[0107] Step 801: Obtain the embedding vector corresponding to the first token, calculate the unquantized first K vector corresponding to the first token based on the embedding vector, and add the unquantized first K vector to the K matrix.

[0108] Step 802: After adding the first K vector to the K matrix, if the number of unquantized K vectors in the K matrix reaches a first number, perform quantization processing on the first matrix block composed of the first number of unquantized K vectors to obtain a second matrix block. The second matrix block includes the first number of quantized second K vectors. Update the first matrix block in the K matrix to the second matrix block. The quantization processing includes calculating the mean vector of the first number of unquantized K vectors, calculating the difference vector between each unquantized K vector and the mean vector, and quantizing the multiple difference vectors to obtain the first number of second K vectors.

[0109] Step 803: Calculate the unquantized first V vector corresponding to the first token based on the embedded vector, and add the unquantized first V vector to the V matrix.

[0110] Step 804: After adding the first V vector to the V matrix, if the number of unquantized V vectors in the V matrix reaches a second number, quantize the seventh matrix block composed of the second number of unquantized V vectors to obtain the eighth matrix block. The eighth matrix block includes the second number of quantized second V vectors. Update the seventh matrix block in the V matrix to the eighth matrix block.

[0111] Step 805: Based on the second matrix block included in the K matrix, the mean vector of the second matrix block is used to calculate the similarity matrix with the Q matrix. Based on the eighth matrix block included in the V matrix, the similarity matrix is ​​used to perform calculation to obtain the calculation result of the Attention calculation.

[0112] The processing steps 801 to 805 are the same as steps 201 to 203 and 501 to 503 described above, and will not be repeated here. In this embodiment, after generating the K vectors included in the K matrix each time, if it is determined that the unquantized K vectors included in the K matrix form a first matrix block, the first matrix block can be quantized to obtain a second matrix block. After generating the V vectors included in the V matrix each time, if it is determined that the unquantized V vectors included in the V matrix form a seventh matrix block, the seventh matrix block can be quantized to obtain an eighth matrix block. In this way, quantizing the K and V matrices can reduce the storage space occupied by the K and V matrices. In subsequent Attention calculations, the efficiency of loading the K and V matrices from memory to the cache will be improved, thereby improving the efficiency of the processor in performing Attention calculations.

[0113] All of the above-mentioned optional technical solutions can be combined in any way to form the optional embodiments of this application, and will not be described in detail here.

[0114] Based on the same inventive concept, embodiments of this application also provide a processor. Figure 9 This is a schematic diagram of the structure of a processor provided in an embodiment of this application. Figure 9 As shown, the processor includes a processing unit, a computing unit, and a cache corresponding to the computing unit. In one example, the processor can be an NPU, capable of implementing the Attention calculation method described in the above embodiments, thereby improving the efficiency of the processor in performing Attention calculations.

[0115] Based on the same inventive concept, embodiments of this application also provide a computing device. Figure 10 This is a schematic diagram of the structure of a computing device provided in an embodiment of this application. Figure 10 As shown, the computing device includes memory and Figure 9 The processor shown is an example of a processor that can store input data for an AI model in memory. The processor can then perform attention calculations on this input data to improve the efficiency of attention calculations within the AI ​​model.

[0116] This application also provides a computer program product containing instructions. The computer program product may be software or program products containing instructions, capable of running on a computing device or stored on any usable medium. When the computer program product runs on a processor, it causes the processor to execute the method for performing attention calculations provided in this application.

[0117] This application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium that a computing device can store, or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state drive). The computer-readable storage medium includes instructions that instruct the processor of the computing device to execute the method for performing attention calculations provided in this application.

[0118] In this application, the term "at least one" means one or more, and the term "multiple" means two or more. The terms "first" and "second," etc., are used to distinguish identical or similar items with substantially the same function. It should be understood that there is no logical or temporal dependency between "first" and "second," nor does it limit the quantity or execution order. It should also be understood that although the following description uses the terms "first" and "second," etc., to describe various elements, these elements should not be limited by the terms. These terms are merely used to distinguish one element from another. For example, without departing from the scope of various examples, a first K-vector can be referred to as a second K-vector, and similarly, a second K-vector can be referred to as a first K-vector. The first K-vector and the second K-vector can respectively indicate K-vectors of different data types.

[0119] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the protection scope of the technical solutions of the embodiments of the present invention.< / eos>

Claims

1. A method for performing Attention calculation, characterized in that, The method includes: Obtain the embedding vector corresponding to the first token, calculate the unquantized first K vector corresponding to the first token based on the embedding vector, and add the unquantized first K vector to the K matrix; After adding the first K vector to the K matrix, if the number of unquantized K vectors included in the K matrix reaches a first number, quantization processing is performed on the first matrix block composed of the first number of unquantized K vectors to obtain a second matrix block. The second matrix block includes the first number of quantized second K vectors. The first matrix block included in the K matrix is ​​updated to the second matrix block. The quantization processing includes calculating the mean vector of the first number of unquantized K vectors, calculating the difference vector between each unquantized K vector and the mean vector, and quantizing the multiple difference vectors to obtain the first number of second K vectors. Based on the second matrix block included in the K matrix, the mean vector used to calculate the second matrix block, and the Q matrix, a similarity matrix is ​​obtained. Based on the similarity matrix and the V matrix, a calculation is performed to obtain the calculation result of the Attention calculation.

2. The method according to claim 1, characterized in that, The method further includes: After adding the first K vector to the K matrix, if the number of unquantized K vectors in the K matrix is ​​less than a first number, and the K matrix includes a second matrix block that has been quantized, a similarity matrix is ​​calculated based on the second matrix block included in the K matrix, the mean vector used to calculate the second matrix block, and the third matrix block composed of each unquantized K vector included in the K matrix, and the Q matrix. A similarity matrix is ​​then obtained based on the similarity matrix and the V matrix, and the calculation result of the Attention calculation is obtained.

3. The method according to claim 1 or 2, characterized in that, The similarity matrix is ​​obtained by calculating the mean vector of the second matrix block included in the K matrix and the Q matrix, including: For the second matrix block included in the K matrix, the fourth matrix block is obtained by calculating with the Q matrix based on the second matrix block and the quantization coefficient corresponding to the second matrix block. The fifth matrix block is obtained by calculating with the Q matrix based on the mean vector corresponding to the second matrix block. The sixth matrix block is obtained by summing the fourth matrix block and the fifth matrix block. Based on the sixth matrix block corresponding to the second matrix block, normalization is performed to obtain the similarity matrix.

4. The method according to any one of claims 1 to 3, characterized in that, Before performing the calculation based on the similarity matrix and the V matrix, the method further includes: Calculate the unquantized first V vector corresponding to the first token based on the embedded vector, and add the unquantized first V vector to the V matrix; After adding the first V vector to the V matrix, if the number of unquantized V vectors included in the V matrix reaches a second number, the seventh matrix block composed of the second number of unquantized V vectors is quantized to obtain an eighth matrix block, which includes the second number of quantized second V vectors. The seventh matrix block included in the V matrix is ​​then updated to the eighth matrix block. The calculation based on the similarity matrix and the V matrix to obtain the calculation result of the Attention calculation includes: performing calculation based on the eighth matrix block included in the V matrix and the similarity matrix to obtain the calculation result of the Attention calculation.

5. The method according to claim 4, characterized in that, After adding the first V vector to the V matrix, if the V matrix contains fewer than the second number of unquantized V vectors, and if the V matrix includes an eighth matrix block, the calculation based on the similarity matrix and the V matrix to obtain the result of the Attention calculation includes: Based on the eighth matrix block included in the V matrix and the ninth matrix block composed of unquantized V vectors included in the V matrix, the similarity matrix is ​​used to calculate the result of the Attention calculation.

6. The method according to claim 4 or 5, characterized in that, The second number is greater than the first number.

7. The method according to any one of claims 1 to 6, characterized in that, The attention calculation includes the attention calculation for each head in the multi-head attention mechanism.

8. A processor, characterized in that, The processor is used to perform the method as described in any one of claims 1 to 7 above.

9. A computing device, characterized in that, The computing device includes a memory and a processor as described in claim 8 above.

10. A computer program product, characterized in that, The computer program product includes instructions that execute on a processor to implement the method as described in any one of claims 1 to 7.