Neural network acceleration method and device, accelerator, and storage medium

By converting input images and convolution kernels into matrices and performing convolution operations on the systolic array, and optimizing the self-attention layer with quantization methods, the problem of Vision Transformer's slow operation speed on edge devices is solved, and efficient computing and high performance are achieved. Accurate neural network acceleration.

CN117973443BActive Publication Date: 2024-09-03GUOWEI INTELLIGENT CHIP (TIANJIN) TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202410181082.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-02-18
Publication Date
2024-09-03
Estimated Expiration
2044-02-18

AI Technical Summary

Technical Problem

The deployment of Vision Transformer neural network on edge devices is limited by low CPU throughput and low energy efficiency of GPU, resulting in slow computing speed. An effective acceleration method is needed to improve its computing efficiency.

Method used

Convert the input image and the preset convolution kernel into a matrix, and perform convolution operations through the pulsating array. Combined with the quantization method, the normalization calculation of the self-attention layer is optimized, and the pulse array is used to achieve efficient multiplication operations and accurate quantization.

Benefits of technology

By extracting feature maps and calculating the quantified normalization layer, the computing speed of the Vision Transformer neural network is significantly improved while maintaining high computational accuracy, making it suitable for efficient reasoning on edge devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117973443B
    Figure CN117973443B
Patent Text Reader

Abstract

The present disclosure provides a neural network acceleration method and device, an accelerator, and a storage medium, the method comprising: converting an input image and a preset convolution kernel into a first matrix and a second matrix respectively; inputting the first matrix and the second matrix into a systolic array to implement a convolution operation, and obtaining a third matrix; normalizing the third matrix by a first formula; and using the normalized third matrix to perform a self-attention operation of a neural network. The neural network acceleration method and device, accelerator, and storage medium provided by the present disclosure can improve the operation speed of a Vision Transformer neural network.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure belongs to the field of data processing technology, and more specifically, to a neural network acceleration method and device, an accelerator, and a storage medium. Background Art

[0002] In recent years, Vision Transformer neural networks have shown a series of advantages, including the ability to model global visual information, powerful feature representation, good scalability and generalization, multi-scale processing, and a certain degree of interpretability. The core of these networks is to use self-attention mechanisms to model sequence data, rather than traditional reliance on recursive or convolutional operations. The key advantage of the self-attention mechanism is that it can consider all positions in the sequence at the same time and dynamically assign weights to each position based on the context.

[0003] However, the excellent performance of the Vision Transformer neural network is often accompanied by a huge amount of parameters and computation, which is particularly prominent when deployed on edge devices. In this environment, its performance is limited by the low throughput and computational efficiency of the CPU and the low energy efficiency of the GPU. Therefore, it is urgent to propose an effective acceleration method to speed up the operation of the Vision Transformer neural network. Summary of the invention

[0004] The purpose of the present disclosure is to provide a neural network acceleration method and device, an accelerator, and a storage medium to increase the computing speed of a Vision Transformer neural network.

[0005] A first aspect of an embodiment of the present disclosure provides a neural network acceleration method, comprising:

[0006] Convert the input image and the preset convolution kernel into the first matrix and the second matrix respectively;

[0007] Inputting the first matrix and the second matrix into a systolic array to perform a convolution operation to obtain a third matrix;

[0008] The third matrix is ​​normalized by the first formula; the normalized third matrix is ​​used for performing a self-attention operation of a neural network;

[0009] Among them, the first formula is:

[0010]

[0011]

[0012] in, is the normalization function, is the third matrix, is the quantization matrix of the third matrix, is the preset quantization factor, is the preset scale parameter, is the preset bias parameter, The third matrix The cumulative sum of a row of data, The third matrix The cumulative sum of squares of a row of data, W is the number of columns of the third matrix.

[0013] A second aspect of the embodiments of the present disclosure provides a neural network acceleration device, including:

[0014] A matrix conversion module, used to convert an input image and a preset convolution kernel into a first matrix and a second matrix respectively;

[0015] a matrix operation module, configured to input the first matrix and the second matrix into a systolic array to perform a convolution operation, thereby obtaining a third matrix;

[0016] A normalization module, used to normalize the third matrix using the first formula; the normalized third matrix is ​​used to perform a self-attention operation of the neural network;

[0017] Among them, the first formula is:

[0018]

[0019]

[0020] in, is the normalization function, is the third matrix, is the quantization matrix of the third matrix, is the preset quantization factor, is the preset scale parameter, is the preset bias parameter, The third matrix The cumulative sum of a row of data, The third matrix The cumulative sum of squares of a row of data, W is the number of columns of the third matrix.

[0021] According to a third aspect of an embodiment of the present disclosure, an accelerator is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above-mentioned neural network acceleration method when executing the computer program.

[0022] According to a fourth aspect of an embodiment of the present disclosure, a computer storage medium is provided, wherein the computer storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the above-mentioned neural network acceleration method are implemented.

[0023] The beneficial effects of the neural network acceleration method and device, accelerator, and storage medium provided by the embodiments of the present disclosure are:

[0024] In this embodiment, performing a convolution operation on the input image can extract a feature map of the input image. To speed up the convolution operation, this embodiment first converts the input image and the preset convolution kernel into a first matrix and a second matrix respectively, and then inputs the first matrix and the second matrix into a pulse array to implement the convolution operation. This can utilize the advantages of the pulse array to achieve efficient multiplication operations and improve the operation speed.

[0025] On the other hand, the original calculation formula of the normalization layer is:

[0026] ,

[0027] Quantifying the original calculation formula, we get:

[0028]

[0029] Through the above quantization formula, the normalization calculation of the third matrix can be accurately quantized, which is beneficial to improving the calculation accuracy. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] In order to more clearly illustrate the technical solutions in the embodiments of the present disclosure, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present disclosure. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0031] Figure 1 A flowchart of a neural network acceleration method provided by an embodiment of the present disclosure;

[0032] Figure 2 A block diagram of the Vision Transformer neural network structure provided in one embodiment of the present disclosure;

[0033] FIG. 3 is a schematic diagram of Img2Col transformation provided by an embodiment of the present disclosure Figure 1 ;

[0034] Figure 4 An embodiment of the present disclosure provides The pipeline architecture diagram of the module;

[0035] Figure 5 A schematic diagram of a self-attention operation module provided in one embodiment of the present disclosure;

[0036] Figure 6 An embodiment of the present disclosure provides The pipeline architecture diagram of the module;

[0037] Figure 7 Schematic diagram of Img2Col transformation provided in one embodiment of the present disclosure Figure 2 ;

[0038] FIG8 is a schematic diagram of systolic array calculation provided by an embodiment of the present disclosure;

[0039] Fig. 9 A schematic diagram of an on-chip cache provided by an embodiment of the present disclosure;

[0040] Fig.10 A structural diagram of a neural network acceleration device provided in one embodiment of the present disclosure;

[0041] Fig.11 A schematic block diagram of an accelerator provided in one embodiment of the present disclosure. DETAILED DESCRIPTION

[0042] In the following description, specific details such as specific system structures and technologies are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present disclosure. However, it should be clear to those skilled in the art that the present disclosure may be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to avoid obstructing the description of the present disclosure with unnecessary details.

[0043] In order to make the purpose, technical solutions and advantages of the present disclosure more clear, specific embodiments will be described below in conjunction with the accompanying drawings.

[0044] Please refer to Figure 1 , Figure 1 A flowchart of a neural network acceleration method provided by an embodiment of the present disclosure, the method comprising:

[0045] S101: Convert an input image and a preset convolution kernel into a first matrix and a second matrix respectively.

[0046] Please refer to Figure 2In this embodiment, the input image and the preset convolution kernel are both multi-dimensional vectors. The Img2Col (Image to column) unit can be used to convert the multi-dimensional input image and the preset convolution kernel into a two-dimensional matrix, that is, a first matrix and a second matrix, and then a convolution operation is performed based on the first matrix and the second matrix. Compared with directly performing a multi-dimensional convolution operation, the operation speed can be improved.

[0047] like Figure 3a , Figure 3b As shown, let the dimension of the input image be [B, H, W, C] and the dimension of the convolution weight be [ , , , C], the dimension of the output feature map is [B, , , ], the input image is expanded into a dimension of [ , K² C], the input weights are expanded into a matrix A of dimension [K² C, ] is a matrix B. Where B represents the number of each batch of input images, H represents the height of the input image, W represents the width of the input image, and C represents the input channel of the input image. is the channel of the output feature map, is the height of the output feature map, The width of the output feature map. The matrix data is generated in the order of one sliding window after another. The data of a sliding window loaded on the chip will complete the calculation of the entire VIT neural network on the programmable logic module and write its results back to the external storage.

[0048] S102: Input the first matrix and the second matrix into the systolic array to implement a convolution operation, thereby obtaining a third matrix.

[0049] In this embodiment, data in the systolic array flows in a rhythmic manner, usually in a PE (Processing Element) array, and efficient matrix multiplication and other computing tasks can be performed. The first matrix and the second matrix are input to the systolic array to implement the convolution operation, thereby converting the convolution operation into a matrix multiplication operation, which can increase the speed of the convolution operation.

[0050] S103: normalizing the third matrix using the first formula; the normalized third matrix is ​​used for performing a self-attention operation of the neural network.

[0051] Among them, the first formula is:

[0052]

[0053]

[0054] in, is the normalization function, is the third matrix, is the quantization matrix of the third matrix, is the preset quantization factor, is the preset scale parameter, is the preset bias parameter, The third matrix The cumulative sum of a row of data, The third matrix The cumulative sum of squares of a row of data, W is the number of columns of the third matrix.

[0055] In this embodiment, the original calculation formula of LayerNorm is:

[0056]

[0057] The third matrix is a two-dimensional matrix, That is, the i-th column of the matrix X can also be considered as all the points on the i-th channel of the output feature map, and the mean and variance need to be calculated dynamically. Since the output of LayerNorm is unevenly distributed on each channel, directly applying quantization methods such as MiniMax to LayerNorm will cause serious loss of accuracy, so a better quantization method is needed for LayerNorm. This embodiment uses the FQ-Vit quantization method to quantize layerNorm. The quantization process is also called PTF quantization. After quantization, there is almost no loss in VIT inference accuracy.

[0058] The core idea of ​​the FQ-Vit quantization method is to let different channels use different quantization factors instead of using one quantization factor for all channels.

[0059]

[0060] Wherein, c is a preset constant, which is set to 3 in this embodiment. As can be seen from the above formula, for the entire X matrix, there is a global quantization factor , similarly, each channel has its own quantization factor .

[0061] Using the above quantization factors Quantify and get the quantitative results , and use the quantitative results express ,get:

[0062]

[0063] in, , is zero point, for function, It is a rounding operation, equivalent to the round() function, and b is the quantization bit width.

[0064] After obtaining the quantization factor through PTF, the layerNorm formula needs to be further sorted out to adapt to hardware calculation.

[0065]

[0066]

[0067]

[0068] Because in reality is very small and can be ignored here. Further we get:

[0069]

[0070] From the above, it can be concluded that the present embodiment performs convolution operation on the input image to extract the feature map of the input image. In order to speed up the convolution operation, the present embodiment first converts the input image and the preset convolution kernel into a first matrix and a second matrix respectively, and then inputs the first matrix and the second matrix into the pulsation array to realize the convolution operation. The advantage of the pulse array to realize efficient multiplication operation can be utilized to improve the operation speed.

[0071] At the same time, through the above quantization formula, the normalization calculation of the third matrix can be accurately quantized, which is beneficial to improving the calculation accuracy.

[0072] In one embodiment of the present disclosure, normalizing the third matrix by using the first formula includes:

[0073] The third matrix is ​​input into a preset programmable logic control module, and normalization calculation is performed on the third matrix row by row;

[0074] For any row of data , programmable logic control module is used for:

[0075] Load any row of data ;

[0076] Through the adder The data in is accumulated to obtain , through the multiplier and Multiply to get ;

[0077] Through the multiplier Multiplying by W gives , and The calculation result is stored in the cache space of the memory;

[0078] Through the multiplier and adder and After multiplication and accumulation, we get ;

[0079] Through the subtractor and Subtract ,calculate Get B 1 ;

[0080] Read from the cache , through the subtractor and Subtract ;

[0081] Will Multiply it by the preset quantization factor Scale to get , and Store in queue;

[0082] Through the multiplier With B 1 Multiply to get ;

[0083] Through the adder Add the preset bias parameter Bias to get any row of data The quantitative calculation results of Bias.

[0084] Please refer to Figure 4 In this embodiment, the normalization calculation process is divided into two stages. The first stage is to calculate the input Preprocessing, before loading the matrix When a row of data is read, the row of data will also be dynamically calculated. When this row is loaded, we can finally get In addition, when calculating At the same time, The value of is stored in the second-stage memory for the second-stage calculation; similarly, in the second-stage calculation, the cached The value of and the value calculated in the previous stage Calculated ,Then Multiply it by the preset parameter Scale to get ,Will The result is pushed into the FIFO buffer and waits Complete the calculation and continue to load the matrix The next row of data is read, thereby starting the first stage of calculation for the next row, and reusing this memory space to store the matrix Next line The value of can finally realize the secondary pipeline calculation, and the required calculation time is ,in is the maximum calculation delay of all internal computing units. In this embodiment, the computing unit with the largest delay is According to the calculation, the delay of this part is 33 clks.

[0085] In one embodiment of the present disclosure, the neural network acceleration method further includes:

[0086] Construct the query vector Q, key vector K and value vector V in the self-attention operation based on the normalized third matrix;

[0087] Perform self-attention operations on the query vector Q, key vector K, and value vector V in turn to obtain the self-attention operation results , T is the transpose operation, and d represents the dimension of the key vector K.

[0088] Please refer to Figure 5-Figure 6 In this embodiment, the third matrix is ​​a standard two-dimensional matrix. After being normalized by LayerNorm, the Linear layer constructs Q, K, and V and performs self-attention operations head by head: Due to the fixed size of the systolic array and limited on-chip storage, when the size of the matrix is ​​large, the matrix calculation needs to be performed multiple times in blocks and the calculation results need to be concat to obtain the complete matrix multiplication result.

[0089] In one embodiment of the present disclosure, self-attention operations are performed on the query vector Q, the key vector K and the value vector V in sequence to obtain a self-attention operation result. ,include:

[0090] The matrix Input into the preset programmable logic control module to control the matrix conduct Calculate and get the Softmax operation result ;

[0091] Softmax operation results based on Log-2 quantization method Quantify;

[0092] Quantize the vector V based on the uniform quantization method;

[0093] based on The quantization result of and the quantization result of vector V are used to calculate the attention operation result. ;

[0094] Among them, the matrix conduct Calculate and get the Softmax operation result ,include:

[0095] Traverse any row of data All data, determine the maximum value of all data ;

[0096] Subtract the vector Data in Subtract from MAX to get the difference ;

[0097] The difference is converted into Perform index calculation to get the difference index ;

[0098] The input vector is added through the adder The difference index corresponding to all data is accumulated to obtain ;

[0099] Will and Perform logarithmic operations respectively, and subtract the two logarithmic operation results to obtain the data of any row Softmax operation result of ;

[0100] The second formula is:

[0101]

[0102] ,

[0103] in, , , exp is the result of exponential calculation, is the i-th element in vector x.

[0104] In this embodiment, before calculating Softmax, it is necessary to traverse all inputs to find the maximum value of the input, and let each input subtract this maximum value before participating in the softmax calculation, which can effectively prevent the problem of exceeding the bit width due to too large input. Therefore, Softmax can be transformed into the following formula:

[0105] ,

[0106] In hardware implementation, it can be calculated by multiple cycles However, it is foreseeable that such calculation delay is extremely large and time is uncontrollable. In order to design an efficient Softmax calculation pipeline, we can For approximate calculations, refer to i-exp. Now it is a non-negative number, so any negative number can be deconstructed into , where Z is a non-negative number, . Let input

[0107]

[0108] It can be further approximated by the following second-order polynomial

[0109]

[0110] then

[0111] Next, we need to further organize the formula to obtain the final shaping forward propagation formula, and design the shaping calculation pipeline based on this formula.

[0112] The uniform quantization method is used to obtain The quantitative results , and use the quantitative results express ,get:

[0113] ,

[0114] in, ,

[0115] ,

[0116] ,

[0117] is the quantization factor, zp2 is the zero point, for Function, b is the quantization bit width.

[0118] then ,

[0119] ,

[0120] ,

[0121] ,

[0122]

[0123] because and The parameters are all known values, so they can be fixed-pointed and used as constants in the calculation during the hardware implementation. Fully-integer fixed-point calculations are implemented.

[0124] Obtained through I-Exp After the approximate value of The cumulative sum ,Finally, the Softmax result Attn is quantized to 4 bits by Log-2 quantization method. The calculation of Log-2 is shown in the following formula

[0125] ,

[0126] ,

[0127] It is worth noting that when performing Log-2 quantization, we take the inverse of the original value. This is because the result of Softmax is between 0 and 1, and taking the inverse of its result can get a positive integer. .

[0128] The Log-2 quantization method is used to quantize the Softmax result Attn:

[0129]

[0130] in for The clip quantization results.

[0131] The uniform quantization method is used to quantize the vector V to obtain:

[0132]

[0133] are the quantization factor and zero point of V respectively.

[0134] Finally, in the calculation , we only need to perform a shift operation:

[0135]

[0136] Since in the Softmax calculation process, it is necessary to first traverse all inputs to find the maximum value, and after obtaining the current maximum value of the input, perform the exponential power calculation of e and also need to calculate the cumulative sum of the exponential powers of e, so it is necessary to repeat the traversal of the input data twice to complete a complete round of Softmax calculation. In order to reduce the calculation time and storage overhead, this embodiment designs a 3-Stage data flow: Stage 1 is used to traverse the input data to find the dynamic maximum value, and Stage 2 is used to calculate And execute Calculation, at the same time, will also The results are cached for Stage 3 calculation. Complete the final calculate,

[0137] In one embodiment of the present disclosure, the neural network acceleration method further includes:

[0138] Splitting the first matrix and the second matrix based on the size of the systolic array to obtain a plurality of sub-matrices;

[0139] The first matrix and the second matrix are input into the systolic array to implement a convolution operation, and a third matrix is ​​obtained, including:

[0140] Inputting the corresponding sub-matrices into the systolic array in sequence for multiplication operation, and obtaining multiple sub-matrix calculation results;

[0141] The calculation results of multiple sub-matrices are concatenated and accumulated to obtain a third matrix.

[0142] In this embodiment, for a size For a systolic array, the maximum supported matrix multiplication is:

[0143] ,in Represents the storage depth of Matrix Multiplication Cache and Matrix B Cache. When the number of rows of Matrix A exceeds the Height of the systolic array, or the number of columns of Matrix B exceeds the Slice of the systolic array, Width, the matrix needs to be processed in blocks. The compiler will convert the matrix calculation into multiple sub-matrix calculations and generate corresponding calculation instructions, and reasonably arrange the address of the results in storage so that further Concat and Add operations can be performed on the calculation results of the sub-matrices in the future to obtain the final complete matrix multiplication result.

[0144] The convolution operation in the Vision Transformer algorithm is mapped into a size of Each row processes the data of a sliding window, and each column processes the data of a convolution kernel corresponding to the sliding window. Figure 7 It shows that the input image data of the Img2Col transformation is stored in the form of channel priority, and the three-dimensional image data in each sliding window is expanded into a two-dimensional matrix in a channel-priority manner.

[0145] Figure 8a It shows that for a size For a two-dimensional systolic array, the i-th row data of matrix A and the i-th column data of matrix B will enter the systolic array with a delay of (i-1) clock cycles, thereby ensuring that every point in each row and column of matrix A accurately performs multiplication and accumulation operations in the corresponding processing unit of the systolic array, and the data of matrix A is reused. times, the data of matrix B is reused .

[0146] Figure 8b shows Figure 8a The specific implementation method of data delay input; take a systolic array of size 3×3, the input matrix A of size 3×6, and the input matrix B of size 6×m (m≥1) as an example: in the initial cycle, only the three points of the first row of matrix A must be obtained, and these three parallel data points are converted into three continuous serial data points and input into the first row of the systolic array. Then the first three points of the second row of matrix A are input into the second row of the systolic array, and so on, until the fourth cycle is reached, and the processing element PE in the upper left corner of the systolic array completes the calculation of all three valid data points. While the data flow remains uninterrupted, after the fifth cycle (clk4), each processing element PE can perform a multiplication and addition operation within one clock cycle.

[0147] Figure 8c Shown Figure 8a The size of the two-dimensional systolic array after expansion is The two-dimensional plane form of the three-dimensional systolic array and the data input format and output format. In this three-dimensional systolic array, the matrix Each Row data, i.e. submatrix will be with the matrix Zhongmei Column data is double counted times, each time the matrix is ​​output Submatrix of It is worth noting that the submatrix The i-th row of the systolic array is input to the ( )OK , and the matrix The jth column of the systolic array is Tile Column Input .

[0148] In one embodiment of the present disclosure, the neural network acceleration method further includes:

[0149] When the first matrix and the second matrix are input into the systolic array to implement the convolution operation, the first matrix is ​​cached based on the first-level FIFO and the second-level FIFO, and the caching process includes:

[0150] The k row base addresses required for this convolution operation and the stride row base addresses after the k row base addresses are cached into the first-level FIFO; among them, is the size of the convolution kernel, and stride is the step size of the convolution operation;

[0151] Push the K row base addresses in the first-level FIFO into the second-level FIFO for convolution operation of k row data;

[0152] In response to the convolution operation of the k rows of data being completed, the first-stage FIFO is controlled to pop out stride row base addresses, and the first stride row base addresses of the first-stage FIFO are updated based on the stride.

[0153] Please refer to Fig. 9 In this embodiment, due to the limited space of the on-chip cache, in order to efficiently reuse the storage space to realize the vertical sliding of the sliding window, two FIFOs for cyclically updating the row base address are added. The specific implementation is as follows: the first-level FIFO (FIFO1) will cache the k row base addresses required for each calculation and the stride row base addresses after the k row base addresses; the second-level FIFO (FIFO2) only caches k row base addresses. When processing the 1st to kth row data, FIFO2 only needs to cyclically pop out 1 to k row base addresses. At the same time, a counter module is set to count the columns, and the horizontal sliding control of the sliding window is performed according to the column count. After processing the first k rows of data, the sliding window will slide down stride rows. At this time, FIFO1 only needs to pop out stride address data and push these stride addresses back to FIFO1, and then POP k data to update FIFO2. By analogy, the vertical sliding control of the sliding window is realized.

[0154] Taking k=5 and stride=3 as an example, in the first convolution operation, the base addresses of rows 1 to 5 and rows 6 to 8 are pushed into FIFO1 in sequence, and FIFO2 caches the base addresses of rows 1 to 5 for the convolution operation of rows 1 to 5 of image data; when the convolution operation of rows 1 to 5 of image data is completed, FIFO1 first pops out the base addresses of rows 1 to 3, and pushes the base addresses of rows 1 to 3 back into FIFO1, and then pops out the base addresses of rows 4 to 8 to update FIFO2, and performs the convolution operation of rows 4 to 8 of image data. Before the next convolution operation, the base addresses of rows 9 to 11 are used to overwrite the three base addresses at the end of the FIFO1 queue (the base addresses of rows 1 to 3). And so on, until the convolution operation of all rows of data is completed.

[0155] In one embodiment of the present disclosure, before performing the convolution operation, the method further includes:

[0156] Uniformly quantizing the matrix data using a third formula; the matrix data includes the first matrix or the second matrix;

[0157] The third formula includes:

[0158] ,

[0159] ,

[0160]

[0161] in, Represents matrix data, s is the quantization factor, zp is the zero point, for Function, b is the quantization bit width.

[0162] In this embodiment, before the convolution operation, the floating point numbers in the first matrix or the second matrix are converted into fixed point numbers for calculation, which can further improve the operation speed. Similarly, before performing the self-attention calculation, the same method is used to convert the input matrix into fixed point numbers to speed up the operation speed.

[0163] Corresponding to the neural network acceleration method of the above embodiment, Fig.10 This is a block diagram of a neural network acceleration device provided by an embodiment of the present disclosure. For ease of explanation, only the parts related to the embodiment of the present disclosure are shown. Fig.10 The neural network acceleration device 20 includes: a matrix conversion module 21, a matrix operation module 22 and a normalization module 23.

[0164] The matrix conversion module 21 is used to convert the input image and the preset convolution kernel into a first matrix and a second matrix respectively;

[0165] A matrix operation module 22, configured to input the first matrix and the second matrix into the systolic array to perform a convolution operation to obtain a third matrix;

[0166] A normalization module 23, used to normalize the third matrix using the first formula; the normalized third matrix is ​​used to perform a self-attention operation of the neural network;

[0167] Among them, the first formula is:

[0168]

[0169]

[0170] in, is the third matrix, is the quantization matrix of the third matrix, is the preset quantization factor, is the preset scale parameter, is the preset bias parameter, The third matrix The cumulative sum of a row of data, The third matrix The cumulative sum of squares of a row of data, W is the number of columns of the third matrix.

[0171] In one embodiment of the present disclosure, the normalization module 23 is specifically used for:

[0172] The third matrix is ​​input into a preset programmable logic control module, and normalization calculation is performed on the third matrix row by row;

[0173] For any row of data , programmable logic control module is used for:

[0174] Load any row of data ;

[0175] Through the adder The data in is accumulated to obtain , through the multiplier and Multiply to get ;

[0176] Through the multiplier Multiplying by W gives , and The calculation result is stored in the cache space of the memory;

[0177] Through the multiplier and adder and After multiplication and accumulation, we get ;

[0178] Through the subtractor and Subtract ,calculate Get B 1 ;

[0179] Read from the cache , through the subtractor and Subtract ;

[0180] Will Multiply it by the preset quantization factor Scale to get , and Store in queue;

[0181] Through the multiplier With B 1 Multiply to get ;

[0182] Through the adder Add the preset bias parameter Bias to get any row of data The quantitative calculation results of Bias.

[0183] In one embodiment of the present disclosure, the neural network acceleration device further includes a self-attention operation module 24, which is used to:

[0184] Construct the query vector Q, key vector K and value vector V in the self-attention operation based on the normalized third matrix;

[0185] Perform self-attention operations on the query vector Q, key vector K, and value vector V in turn to obtain the self-attention operation results , T is the transpose operation, and d represents the dimension of vector K.

[0186] In one embodiment of the present disclosure, the self-attention operation module 24 is further used to:

[0187] The matrix Input into the preset programmable logic control module to control the matrix conduct Calculate and get the Softmax operation result ;

[0188] Softmax operation results based on Log-2 quantization method Quantify;

[0189] Quantize the vector V based on the uniform quantization method;

[0190] based on The quantization result of and the quantization result of vector V are used to calculate the attention operation result. ;

[0191] Among them, the matrix conduct Calculate and get the Softmax operation result ,include:

[0192] Traverse any row of data All data, determine the maximum value of all data ;

[0193] Subtract the vector Data in Subtract from MAX to get the difference ;

[0194] The difference is converted into Perform index calculation to get the difference index ;

[0195] The input vector is added through the adder The difference index corresponding to all data is accumulated to obtain ;

[0196] Will and Perform logarithmic operations respectively, and subtract the two logarithmic operation results to obtain the data of any row Softmax operation result of ;

[0197] The second formula is:

[0198]

[0199] ,

[0200] in, , , exp is the result of exponential calculation, is the i-th element in vector x.

[0201] In one embodiment of the present disclosure, the matrix operation module 22 is further used for:

[0202] Splitting the first matrix and the second matrix based on the size of the systolic array to obtain a plurality of sub-matrices;

[0203] The first matrix and the second matrix are input into the systolic array to implement a convolution operation, and a third matrix is ​​obtained, including:

[0204] Inputting the corresponding sub-matrices into the systolic array in sequence for multiplication operation, and obtaining multiple sub-matrix calculation results;

[0205] The calculation results of multiple sub-matrices are concatenated and accumulated to obtain a third matrix.

[0206] In one embodiment of the present disclosure, the matrix operation module 22 is further used for:

[0207] When the first matrix and the second matrix are input into the systolic array to implement the convolution operation, the first matrix is ​​cached based on the first-level FIFO and the second-level FIFO, and the caching process includes:

[0208] The k row base addresses required for this convolution operation and the stride row base addresses after the k row base addresses are cached into the first-level FIFO; among them, is the size of the convolution kernel, and stride is the step size of the convolution operation;

[0209] Push the K row base addresses in the first-level FIFO into the second-level FIFO for convolution operation of k row data;

[0210] In response to the convolution operation of the k rows of data being completed, the first-stage FIFO is controlled to pop out stride row base addresses, and the first stride row base addresses of the first-stage FIFO are updated based on the stride.

[0211] In one embodiment of the present disclosure, the matrix operation module 22 is further used for:

[0212] Clip quantization is performed on the matrix data by using a third formula; the matrix data includes the first matrix or the second matrix;

[0213] The third formula includes:

[0214] ,

[0215] ,

[0216]

[0217] in, Represents matrix data, s is the quantization factor, zp is the zero point, for Quantization function, b is the quantization bit width.

[0218] See also Fig.11 , Fig.11 A schematic block diagram of an accelerator provided in one embodiment of the present disclosure. Fig.11The accelerator in the embodiment shown may include: a processor system, a programmable logic module, a dynamic random access memory DRAM and a direct memory access DMA unit.

[0219] The processor system PS contains an embedded processor for performing efficient task management and system monitoring. It reads instructions from the dynamic random access memory DRAM and parses the instructions. It can configure the core hardware architecture through the Axi-Lite interface, plan the selection of data paths, ensure efficient transmission of data flows between different components, and control the startup and interruption of programmable logic PL calculations.

[0220] The programmable logic module is used to perform inference operations of the Vision Transformer (VIT) neural network, including on-chip data cache, convolution operation unit, multi-head self-attention MSA operation unit and nonlinear operator operation unit;

[0221] Convolution operations and multi-head self-attention MSA operation units complete calculations by calling configurable three-dimensional systolic arrays;

[0222] The nonlinear operator operation unit includes a Softmax operation unit and a Layer Norm operation unit;

[0223] The dynamic random access memory DRAM is responsible for storing all input data required for the Vision-Transformer neural network calculation and quantization, including image data, weight data, and quantization parameters. These data are organized and optimized before calculation. After the programmable logic PL completes the inference calculation of the VIT neural network model, the calculation results will be written back to the dynamic random access memory DRAM, and the calculation results will be processed and analyzed by the processor system PS.

[0224] The direct memory access DMA unit uses the AXI bus protocol for data exchange between the processor system PS, programmable logic PL and dynamic random access memory DRAM.

[0225] In one embodiment of the present disclosure, the processor system, programmable logic module, dynamic random access memory DRAM and direct memory access DMA unit are all implemented by field programmable gate array (FPGA). Compared with traditional central processing unit (CPU) and graphics processing unit (GPU), FPGA can provide a high energy efficiency solution for the reasoning deployment of Vision Transformer model on edge devices due to its high energy efficiency and customizable computing and storage design.

[0226] In one embodiment of the present disclosure, the accelerator may further include a host computer, which is used for overall system control, including task scheduling, parameter configuration, data uploading, and result downloading.

[0227] The above are only specific embodiments of the present disclosure, but the protection scope of the present disclosure is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present disclosure, and these modifications or replacements should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on the protection scope of the claims.

Claims

1. A neural network acceleration method, characterized in that: include: Convert the input image and the preset convolution kernel into the first matrix and the second matrix respectively; Inputting the first matrix and the second matrix into a systolic array to perform a convolution operation to obtain a third matrix; The third matrix is ​​normalized by the first formula; the normalized third matrix is ​​used for performing a self-attention operation of a neural network; Among them, the first formula is: in, is the normalization function, is the third matrix, is the quantization matrix of the third matrix, is the preset quantization factor, is the preset scale parameter, is the preset bias parameter, The third matrix The cumulative sum of a row of data, The third matrix The cumulative sum of squares of a row of data, W is the number of columns of the third matrix; Before the convolution operation, it also includes: Uniformly quantizing the matrix data using a third formula; the matrix data includes the first matrix or the second matrix; The third formula includes: , , in, Represents matrix data, s is the quantization factor, zp is the zero point, ()for function, is the rounding operation, b is the quantization bit width; Also includes: Construct the query vector Q, key vector K and value vector V in the self-attention operation based on the normalized third matrix; Perform self-attention operations on the query vector Q, key vector K, and value vector V in turn to obtain the self-attention operation results , T is the transpose operation, and d represents the dimension of the key vector K.

2. The neural network acceleration method according to claim 1, wherein the normalizing the third matrix by the first formula comprises: Inputting the third matrix into a preset programmable logic control module, and performing normalization calculation on the third matrix row by row; For any row of data , the programmable logic control module is used for: Load any row of data ; Through the adder The data in is accumulated to obtain , through the multiplier and Multiply to get ; Through the multiplier Multiplying by W gives , and The calculation result is stored in the cache space of the memory; Through the multiplier and adder and After multiplication and accumulation, we get ; Through the subtractor and Subtract ,calculate Get B1; Read from the cache space , through the subtractor and Subtract ; Will Multiply it by the preset quantization factor Scale to get , and Store in queue; Through the multiplier Multiplying by B1 gives ; Through the adder Add the preset bias parameter Bias to get any row of data The quantitative calculation results of Bias.

3. The neural network acceleration method according to claim 1, characterized in that: The query vector Q, the key vector K and the value vector V are sequentially subjected to self-attention operations to obtain the self-attention operation results. ,include: The matrix Input into the preset programmable logic control module to control the matrix conduct Calculate and get the Softmax operation result ; Softmax operation results based on Log-2 quantization method Quantify; Quantize the vector V based on the uniform quantization method; based on The quantization result of and the quantization result of vector V are used to calculate the attention operation result. ; Among them, for the matrix conduct Calculate and get the Softmax operation result ,include: Traverse any row of data All data, determine the maximum value of all data ; Subtract the vector Data in Subtract from MAX to get the difference ; The difference is converted into Perform index calculation to get the difference index ; The input vector is added through the adder The difference index corresponding to all data is accumulated to obtain ; Will and Perform logarithmic operations respectively, and subtract the two logarithmic operation results to obtain the data of any row Softmax operation result of ; The second formula is: , in, , , exp is the result of exponential calculation, is the i-th element in vector x.

4. The neural network acceleration method according to claim 1, characterized in that: Also includes: Splitting the first matrix and the second matrix based on the size of the systolic array to obtain a plurality of sub-matrices; The step of inputting the first matrix and the second matrix into a systolic array to implement a convolution operation to obtain a third matrix comprises: Inputting the corresponding sub-matrices into the systolic array in sequence for multiplication operation, and obtaining multiple sub-matrix calculation results; The calculation results of multiple sub-matrices are concatenated and accumulated to obtain a third matrix.

5. The neural network acceleration method according to claim 1, characterized in that: Also includes: When the first matrix and the second matrix are input into the systolic array to implement the convolution operation, the first matrix is ​​cached based on the first-level FIFO and the second-level FIFO, and the caching process includes: The k row base addresses required for this convolution operation and the stride row base addresses after the k row base addresses are cached into the first-level FIFO; among them, is the size of the convolution kernel, and stride is the step size of the convolution operation; Push the K row base addresses in the first-level FIFO into the second-level FIFO for convolution operation of k row data; In response to the completion of the convolution operation of the k rows of data, the first-stage FIFO is controlled to pop out stride row base addresses, and the stride row base addresses at the tail of the first-stage FIFO are updated based on the stride.

6. A neural network acceleration device, characterized in that: include: A matrix conversion module, used to convert an input image and a preset convolution kernel into a first matrix and a second matrix respectively; a matrix operation module, configured to input the first matrix and the second matrix into a systolic array to perform a convolution operation, thereby obtaining a third matrix; A normalization module, used to normalize the third matrix using the first formula; the normalized third matrix is ​​used to perform a self-attention operation of the neural network; Among them, the first formula is: in, is the normalization function, is the third matrix, is the quantization matrix of the third matrix, is the preset quantization factor, is the preset scale parameter, is the preset bias parameter, The third matrix The cumulative sum of a row of data, The third matrix The cumulative sum of squares of a row of data; The matrix operation module is also used for: Before performing the convolution operation, the matrix data is clip quantized by using the third formula; the matrix data includes the first matrix or the second matrix; The third formula includes: , , in, Represents matrix data, s is the quantization factor, zp is the zero point, for Quantization function, b is the quantization bit width; Also included is a self-attention operation module, the self-attention operation module is used to: Construct the query vector Q, key vector K and value vector V in the self-attention operation based on the normalized third matrix; Perform self-attention operations on the query vector Q, key vector K, and value vector V in turn to obtain the self-attention operation results , T is the transpose operation, and d represents the dimension of vector K.

7. An accelerator, comprising a processor system, a programmable logic module, a dynamic random access memory DRAM and a direct memory access DMA unit, wherein a computer program is stored in the programmable logic module, characterized in that: When the processor system executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.

8. A computer storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.

Citation Information

Patent Citations

  • Data processing acceleration method of dynamic sparse attention mechanism capable of being realized by hardware

    CN113918882A

  • Hardware accelerator applied to Transform neural network and calculation method thereof

    CN114118344A

  • Separable image processing neural network accelerator and acceleration method

    CN114723034A

  • Method, accelerator, and electronic device with tensor processing

    US20210406646A1