Attention calculation processing method and device

By dividing the query matrix, key matrix and value matrix into small blocks and operating them in registers, the problem of low operating efficiency and high memory access overhead of the attention mechanism is solved, and more efficient attention operations are achieved.

CN118585249BActive Publication Date: 2024-10-08SUZHOU YIZHU INTELLIGENT TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411053802.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-01
Publication Date
2024-10-08
Estimated Expiration
2044-08-01

AI Technical Summary

Technical Problem

In the existing technology, the attention mechanism has low operational efficiency and high memory access overhead. This is mainly because the query matrix, key matrix and value matrix are stored in the global memory, resulting in slow thread access speed.

Method used

By dividing the query matrix, key matrix and value matrix into smaller blocks, scheduling them to the registers of the computing unit for operations, and using the tensor processor and exponential processing unit to perform matrix multiplication and exponential processing, reducing the impact on global memory. access.

Benefits of technology

It improves the efficiency of attention operations, reduces memory access overhead, shortens the reading and writing back time of matrix data, and improves the processing speed.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118585249B_ABST
    Figure CN118585249B_ABST
Patent Text Reader

Abstract

The present disclosure provides an attention operation processing method and device, the method comprising: dividing a query matrix, a key matrix and a value matrix into a query matrix block, a key matrix block and a value matrix block respectively, and scheduling them into a register; reading the query matrix block and the key matrix block from the register through a tensor processor, and performing matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix and write it back to the register; performing indexation processing based on the sub-product matrix in the register through an indexation processing unit to obtain a sub-index matrix and write it back to the register; reading the sub-index matrix and the value matrix block from the register, and performing matrix multiplication operation based on the sub-index matrix and the value matrix block to generate an attention operation result sub-block; obtaining an attention operation result matrix based on each attention operation result sub-block. Based on the attention operation processing method disclosed in the present disclosure, the memory access overhead in the attention operation process can be reduced, and the efficiency of the attention operation can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of data processing, and in particular to an attention operation processing method and device. Background Art

[0002] The attention mechanism is a commonly used technology in neural network models. For example, in the Transformer model, the self-attention mechanism is the core of the model. These neural network models often convert the input vector or sequence into the corresponding query matrix, key matrix and value matrix, and then input the query matrix, key matrix and value matrix into the attention mechanism module. The attention mechanism module obtains the attention weight matrix by performing multiple matrix operations and exponential normalization on the query matrix, key matrix and value matrix, thereby determining the attention weight of each element in the vector or sequence.

[0003] In the prior art, query matrices, key matrices, and value matrices are generally stored in the global memory of computing components such as GPUs. Global memory allows all threads of computing components to access it. Threads read matrix data from global memory and then transfer the data to tensor processors or exponential normalization modules for corresponding matrix operations. However, since threads are executed by compute units, global memory is independent of the outside of the compute units, and threads inside the execution units have a relatively slow access to global memory, which makes the attention mechanism inefficient and has a large memory access overhead. Summary of the invention

[0004] The embodiments of the present disclosure provide an attention operation processing method and device, which can improve the operation efficiency of the attention mechanism and reduce memory access overhead.

[0005] According to one aspect of the present disclosure, there is provided an attention operation processing method for an attention operation processing device, the attention operation processing device comprising a tensor processor and a computing unit, the computing unit comprising a register and an index processing unit, the attention operation processing method comprising: dividing a query matrix, a key matrix and a value matrix used for a target attention operation into a query matrix block, a key matrix block and a value matrix block, respectively, and scheduling them into a register; reading the query matrix block and the key matrix block from the register through the tensor processor, and performing matrix multiplication operations on the query matrix block and the key matrix block to obtain a sub-product matrix, and writing the sub-product matrix back to the register; performing index processing on the sub-product matrix in the register through the index processing unit to obtain a sub-index matrix, and writing the sub-index matrix back to the register index processing unit; reading the sub-index matrix and the value matrix block corresponding to the sub-index matrix from the register through the tensor processor, and performing matrix multiplication operations based on the sub-index matrix and the value matrix block to generate an attention operation result sub-block; obtaining an attention operation result matrix based on each attention operation result sub-block.

[0006] According to another aspect of the present disclosure, there is provided an attention operation processing device, comprising a tensor processor and a computing unit, the computing unit comprising a register and an index processing unit, wherein the register is used to store a query matrix block, a key matrix block and a value matrix block for a target attention operation; the tensor processor is used to read the query matrix block and the key matrix block from the register, and perform a matrix multiplication operation based on the query matrix block and the key matrix block to obtain a sub-product matrix, and write the sub-product matrix back to the register; the index processing unit is used to perform an index processing on the sub-product matrix in the register to obtain a sub-exponent matrix, and write the sub-exponent matrix back to the register; the tensor processor is also used to read the sub-exponent normalization matrix and the value matrix block from the register, and perform a matrix multiplication operation based on the sub-exponent matrix and the value matrix block to obtain an attention operation result sub-block.

[0007] Optionally, the attention operation processing device also includes a blocking unit, which is specifically used to: divide the query matrix, key matrix and value matrix used for the target attention operation into query matrix blocks, key matrix blocks and value matrix blocks according to the same division rule; put the multiple query matrix blocks divided into the register in sequence according to the first cycle, wherein each first cycle includes a first number of second cycles, and the first number is the number of key matrix blocks that need to be multiplied with one query matrix block; in each second cycle of the first cycle, each key matrix block that needs to be multiplied with the query matrix block is placed in the register in sequence, and each value matrix block corresponding to the sub-index matrix is ​​placed in the register in sequence.

[0008] Optionally, there are multiple computing units, and the blocking unit is further used to: in each of the first cycles, place the multiple query matrix blocks into the registers of the multiple computing units respectively, wherein the register of each computing unit places one query matrix block, and the same query matrix block is only placed in the register of one computing unit in one first cycle; in each of the second cycles of the first cycle, place each of the key matrix blocks and the value matrix blocks corresponding to the key matrix blocks in the register of each computing unit in turn.

[0009] Optionally, the tensor processor includes a matrix multiplication unit and an accumulation buffer, and the tensor processor is also used to: read the query matrix block and the key matrix block from the register through the matrix multiplication operation unit in each second cycle of the first cycle, perform matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix, and write the sub-product back to the register.

[0010] Optionally, the calculation unit is also used to: in the first and second periods of the first period, sum each matrix row of the sub-exponential matrix to obtain the first sum of each matrix row, and determine the normalization coefficient matrix based on each first sum, and write the normalization coefficient matrix into the register; in each second period of the first period, sum each matrix row of the corresponding sub-exponential matrix to obtain the first sum of each matrix row, and respectively add the first sum of each matrix row to the corresponding matrix row in the normalization coefficient matrix to update the normalization coefficient matrix, wherein the sub-exponential matrix is ​​obtained by taking each element of the corresponding sub-product matrix as the exponent of a preset base.

[0011] Optionally, the tensor processor is also used to: initialize the accumulation buffer in each first cycle to obtain the initialized attention intermediate result sub-matrix; in each second cycle of the first cycle, add the attention operation result sub-block corresponding to the current second cycle to the attention intermediate result sub-matrix in the accumulation buffer, wherein the attention operation result sub-block is obtained by multiplying the sub-index matrix and the value matrix block corresponding to the current second cycle; after accumulating the attention operation result sub-block corresponding to the last second cycle of the first cycle to the attention intermediate result sub-matrix, correct the attention intermediate result sub-matrix based on the normalization coefficient matrix to obtain the attention result sub-matrix corresponding to the first cycle; and concatenate the attention result sub-matrices corresponding to each first cycle to obtain the attention operation result matrix.

[0012] Optionally, the calculation unit is also used to: initialize a row maximum value vector in each first cycle; determine the element maximum value of each matrix row in the corresponding sub-product matrix in the Mth second cycle of the first cycle, and when the element maximum value is greater than the row maximum value of the corresponding matrix row in the row maximum value vector, update the corresponding row maximum value based on the element maximum value to obtain the row maximum value corresponding to the Mth second cycle, where 1≤M≤N, and N is a first number; the exponential processing unit is also used to: in the Mth second cycle of the first cycle, read the row maximum value of each matrix row from the register through the exponential processing unit, determine the first difference corresponding to each element of the sub-product matrix based on each element of the corresponding sub-product matrix and the corresponding row maximum value, and use each first difference as an exponent of a preset exponent to obtain a sub-exponential matrix corresponding to the sub-product matrix.

[0013] Optionally, the tensor processor is also used to: in the Mth second period, determine the correction coefficient based on the row maximum value corresponding to the Mth second period and the row maximum value corresponding to the M-1th second period, correct the attention intermediate result sub-matrix of the accumulation buffer based on the correction coefficient, and accumulate the attention operation result sub-block corresponding to the Mth second period to the corrected attention intermediate result sub-matrix; the exponential processing unit is also used to: in the Mth second period in the first period, correct the normalization coefficient matrix corresponding to the M-1th second period based on the correction coefficient, and accumulate the first sum of each matrix row of the sub-exponential matrix corresponding to the Mth second period to the corresponding matrix row in the corrected normalization coefficient matrix to obtain the normalization coefficient matrix corresponding to the Mth second period.

[0014] Optionally, the indexation processing unit is also used to: execute multiple thread bundles in parallel, wherein each thread bundle obtains a second number of normalization coefficients corresponding to a second number of matrix rows from the normalization coefficient matrix through the indexation processing unit, and corrects the second number of matrix rows based on the second number of normalization coefficients to obtain a second number of normalized attention result matrix rows, wherein each matrix row corresponds to a normalization coefficient; and splices and merges the normalized attention result matrix rows corresponding to multiple thread bundles to obtain an attention result sub-matrix corresponding to the first cycle.

[0015] Optionally, the tensor processor includes a matrix multiplication unit and an accumulation buffer, and the tensor processor is also used to: read the query matrix block and the key matrix block from the register through the matrix multiplication operation unit in each second cycle of the first cycle, and perform matrix multiplication operation on the query matrix block and the key matrix block to obtain the sub-product matrix, and write the sub-product back to the register; the calculation unit is also used to: write the sub-exponent matrix back to the accumulation buffer, and cascade the sub-exponent matrices in the accumulation buffer to obtain a cascaded exponent matrix.

[0016] Optionally, the exponential processing unit is also used to: read the sub-exponential matrix corresponding to the current second period from the register through the exponential processing unit, and obtain the cascaded exponential matrix from the accumulation buffer; cascade the cascaded exponential matrix and the sub-exponential matrix and perform normalization processing to obtain a cascaded exponential normalized matrix, and separate the sub-exponential normalized matrix corresponding to the sub-exponential matrix from the cascaded exponential normalized matrix, and write the sub-exponential normalized matrix back to the register.

[0017] Optionally, the tensor processor is also used to: read the sub-exponent normalization matrix and the value matrix block from the register through the tensor processor in each second cycle of the first cycle, and perform matrix multiplication operation on the sub-exponent normalization matrix and the value matrix block through the matrix multiplication unit to generate an attention operation result sub-block, and write the attention operation result sub-block to the accumulation buffer.

[0018] Optionally, the attention operation processing device also includes a correction unit, which is used to: split out a corrected sub-exponent normalized matrix corresponding to each sub-exponent matrix in the cascaded exponent normalized matrix; for each sub-exponent matrix, determine a first difference matrix based on the corresponding corrected sub-exponent normalized matrix and the corresponding sub-exponent normalized matrix, and perform matrix multiplication on the first difference matrix and the corresponding value matrix block to obtain a corrected attention operation result sub-block, write the corrected attention operation result sub-block into an accumulation buffer, and accumulate the corrected attention operation result sub-block to the attention operation result sub-block corresponding to the sub-exponent matrix in the accumulation buffer.

[0019] Optionally, the tensor processor is also used to: accumulate the attention operation result sub-blocks corresponding to each second period in the first period in the accumulation buffer to obtain the attention result sub-matrix corresponding to the first period; and concatenate the attention result sub-matrices corresponding to each first period to obtain the attention operation result matrix.

[0020] Optionally, the indexation processing unit is also used to: execute multiple thread bundles in parallel, wherein each thread bundle performs indexation processing on a second number of matrix rows corresponding to the thread bundle through the indexation processing unit to obtain a second number of sub-index matrix rows corresponding to the thread bundle; merge the sub-index matrix rows corresponding to the multiple thread bundles into a sub-index matrix, and write the sub-index matrix back to the register.

[0021] Optionally, the indexation processing unit is further used to: determine the total number of enabled thread bundles based on the ratio of the number of matrix rows to the second number; determine the number of idle thread bundles of each execution unit; distribute the total number of enabled thread bundles among the execution units based on the ratio of the number of idle thread bundles of each execution unit to obtain the number of enabled thread bundles corresponding to each execution unit; store each matrix row in the sub-product matrix into the register of each execution unit based on the product of the number of enabled thread bundles corresponding to each execution unit and the second number; in each execution unit, enable the corresponding number of enabled thread bundles in parallel, so that each thread bundle passes through the indexation processing unit, takes out the corresponding second number of matrix rows from the register of the execution unit corresponding to the thread bundle, and performs indexation processing on each matrix row to obtain the sub-index matrix row corresponding to the thread bundle.

[0022] The attention operation processing method provided by the present invention divides the query matrix, key matrix and value matrix into multiple relatively small query matrix blocks, key matrix blocks and matrix blocks, and then schedules the query matrix blocks, key matrix blocks and value matrix blocks to the registers of the computing unit. The threads of the computing unit can quickly read the query matrix blocks and key matrix blocks from the registers through the tensor processor to perform matrix multiplication operations. After calculating the corresponding sub-product matrix, the sub-product matrix is ​​written back to the register. The sub-product matrix in the register is indexed by the index processing unit inside the computing unit, and the obtained sub-index matrix is ​​written back to the register. The tensor processor then quickly reads the sub-index matrix and the value matrix block from the register, and obtains the attention operation result sub-block based on the sub-index matrix and the value matrix block. Based on this, during the entire calculation process, the tensor processor and the index processing unit read data from the register, and the intermediate data generated is also stored through the register without accessing the global memory, reducing the memory access overhead. At the same time, the fast access speed of the register is utilized to shorten the time required to read the matrix data and write back the data. After completing the previous step of calculation, the next step of calculation can be performed quickly. There will not be too long a time gap between each calculation in the attention calculation process due to waiting for the data to be written back and read, and the processing efficiency is higher, thereby improving the efficiency of the attention calculation.

[0023] Other features and advantages of the present disclosure will be described in the following description, and partly become apparent from the description, or understood by practicing the present disclosure. The purpose and other advantages of the present disclosure can be realized and obtained by the structures particularly pointed out in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] The accompanying drawings are used to provide further understanding of the technical solution of the present disclosure and constitute a part of the specification. Together with the embodiments of the present disclosure, they are used to explain the technical solution of the present disclosure and do not constitute a limitation on the technical solution of the present disclosure.

[0025] Figure 1 is a system architecture diagram of the attention operation processing method using the embodiment of the present disclosure;

[0026] Figure 2 is a main flow chart of an attention operation processing method according to an embodiment of the present disclosure;

[0027] Figure 3 yes Figure 2 A flowchart of step 210;

[0028] Figure 4 is a flowchart of performing attention operation after dividing the query matrix, the key matrix and the value matrix into blocks according to an embodiment of the present disclosure;

[0029] Figure 5 yes Figure 3 A combined flow chart of step 310 and step 320;

[0030] Figure 6 According to an embodiment of the present disclosure, a flow chart is provided for performing calculations based on the current matrix block in each second cycle, and accumulating the sum of each matrix row of the sub-index matrix of each second cycle into a register;

[0031] Figure 7 According to an embodiment of the present disclosure, a flowchart is provided in which an attention operation result sub-block is calculated based on a sub-index matrix and a value matrix block in each second cycle, and the attention operation result sub-block is normalized in the last second cycle;

[0032] Figure 8 is a flow chart of performing indexing processing in combination with row maximum values ​​according to an embodiment of the present disclosure;

[0033] Fig. 9 is a flow chart of modifying the attention intermediate result sub-matrix and the normalization coefficient matrix when the row maximum value is updated according to an embodiment of the present disclosure;

[0034] Fig.10 is a flow chart of normalizing the attention intermediate result sub-matrix through multi-thread concurrency according to an embodiment of the present disclosure;

[0035] Fig.11 According to an embodiment of the present disclosure, a flow chart of writing a sub-index matrix into a register and an accumulation buffer in sequence;

[0036] Fig.12 yes Figure 2 A flow chart of step 230;

[0037] Fig.13 yes Figure 2 A flowchart of step 240;

[0038] Fig.14 is a flow chart for correcting the attention operation result sub-blocks corresponding to each second cycle according to an embodiment of the present disclosure;

[0039] Fig.15 yes Figure 2 A flowchart of step 250;

[0040] Fig.16 yes Figure 2 A flow chart of step 230;

[0041] Fig.17According to an embodiment of the present disclosure, under the premise of considering the load balancing of each execution unit, each matrix row of the sub-product matrix is ​​stored in the register of each execution unit, and a flowchart of exponential processing of each matrix row is concurrently executed by multiple thread warps;

[0042] Fig.18 It is a block diagram of an attention operation processing device proposed in one embodiment of the present disclosure. DETAILED DESCRIPTION

[0043] In order to make the purpose, technical solution and advantages of the present disclosure more clear, the present disclosure is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present disclosure and are not used to limit the present disclosure.

[0044] Before further describing the embodiments of the present disclosure in detail, the nouns and terms involved in the embodiments of the present disclosure are described. The nouns and terms involved in the embodiments of the present disclosure are subject to the following interpretations:

[0045] Compute Unit (CU): A processing module in an electronic device such as a GPU. A GPU can include multiple compute units, which contain multiple execution units. A compute unit can be regarded as a processing core in a GPU. Each compute unit can execute instructions independently and process data in parallel.

[0046] Register: A high-speed storage resource in the GPU. Registers exist inside the computing unit, and threads inside the computing unit can quickly access registers.

[0047] Global Memory: Global memory is a storage resource in the GPU. Each computing unit of the GPU can access the global memory. The global memory has a relatively large storage space and can be used to store large-scale data. However, since the global memory exists independently outside the computing unit, the computing unit accesses the global memory relatively slowly.

[0048] The attention mechanism is a commonly used technology in neural network models. For example, in the Transformer model, the self-attention mechanism is the core of the model. These neural network models often convert the input vector or sequence into the corresponding query matrix, key matrix and value matrix, and then input the query matrix, key matrix and value matrix into the attention mechanism module. The attention mechanism module obtains the attention weight matrix by performing multiple matrix operations and exponential normalization on the query matrix, key matrix and value matrix, thereby determining the attention weight of each element in the vector or sequence.

[0049] In the prior art, query matrices, key matrices, and value matrices are generally stored in the global memory of computing components such as GPUs. Global memory allows all threads of computing components to access it. Threads read matrix data from global memory and then transfer the data to tensor processors or exponential normalization modules for corresponding matrix operations. However, since threads are executed by compute units, global memory is independent of the outside of the compute units, and threads inside the execution units have a relatively slow access to global memory, which makes the attention mechanism inefficient and has a large memory access overhead.

[0050] Based on this, the present disclosure proposes an attention operation processing method and device. The attention operation processing method and device provided by the embodiments of the present disclosure can improve the computational efficiency of the attention mechanism and reduce memory access overhead.

[0051] Figure 1 1 is a system architecture diagram used in the attention operation processing method of the embodiment of the present disclosure, which includes: a tensor processor 110, at least one computing unit 120, the computing unit 120 includes a register 121 and an index processing unit 122, and the computing unit 120 runs a plurality of thread warps.

[0052] The tensor processor 110 is a unit for performing matrix multiplication operations. The tensor processor 110 includes a first buffer 111 and a second buffer 114, a matrix multiplication unit 112 and an accumulation buffer 113. The first buffer 111 and the second buffer 114 are communicatively connected to the register 121 of the computing unit 120. The first buffer 111 and the second buffer 114 read matrix data from the register 121 of the computing unit 120. The matrix multiplication unit 112 is dedicated hardware for performing matrix multiplication operations. The matrix multiplication unit 112 is connected to the first buffer 111 and the second buffer 114, and reads the matrices of the first buffer 111 and the second buffer 114 for matrix multiplication. The accumulation buffer 113 is used to accumulate the matrices output by the matrix multiplication unit 112 or cache the matrices output by the matrix multiplication unit 112.

[0053] The computing unit 120 may be regarded as a computing core of the system. The computing unit may be a graphics processing unit (GPU). The register 121 is a high-speed storage area inside the computing unit 120 for storing data. The register 121 allows thread warps inside the computing unit 120 to access it.

[0054] A warp is a basic unit of parallel execution in the system. A warp contains multiple threads. A thread is the smallest execution unit of the system. A warp completes actual data processing through the multiple threads it contains. Multiple warps can execute the same instructions in parallel within one clock cycle, thereby achieving efficient parallel computing. A warp can be used to access register 121 inside computing unit 120, read data from register 121, write data back to register 121, or process data in register 121 by calling other units.

[0055] The exponential processing unit 122 is hardware inside the computing unit 120 for performing exponential processing on matrices. It may be composed of a general arithmetic logic unit (ALU) inside the computing unit 120 or a special function unit (SFU) dedicated to performing exponential processing.

[0056] The present disclosure provides an attention operation processing method, which is applied to an attention operation processing device. Figure 2 , the attention operation processing method includes:

[0057] Step 210, dividing the query matrix, key matrix and value matrix used for the target attention operation into query matrix blocks, key matrix blocks and value matrix blocks respectively, and dispatching them into registers;

[0058] Step 220, read the query matrix block and the key matrix block from the register through the tensor processor, perform matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix, and write the sub-product matrix back to the register;

[0059] Step 230, performing index processing on the sub-product matrix in the register through an index processing unit to obtain a sub-index matrix, and writing the sub-index matrix back to the register;

[0060] Step 240, reading the sub-index matrix and the value matrix block corresponding to the sub-index matrix from the register through the tensor processor, and performing a matrix multiplication operation based on the sub-index matrix and the value matrix block to generate an attention operation result sub-block;

[0061] Step 250, obtaining an attention operation result matrix based on each attention operation result sub-block.

[0062] The following is an overview of steps 210 to 250.

[0063] In step 210, the query matrix, key matrix and value matrix are matrices required for calculating the attention weights of the input sequence, and the query matrix, key matrix and value matrix can be obtained by performing matrix operations on the input sequence and the corresponding weight matrix in the attention mechanism. Specifically, the input sequence is recorded as, which can be represented as a vector. After inputting into the attention mechanism, the three weight matrices used to calculate the query matrix, key matrix and value matrix in the attention mechanism are respectively matrix-multiplied to obtain the corresponding query matrix, key matrix and value matrix, as shown below: in, , , They are the query matrix, key matrix, and value matrix; , , They are the three weight matrices used to calculate the query matrix, key matrix, and value matrix. These three weight matrices are the parameters of the attention mechanism itself and are determined by iteration in the process of training the neural network model based on the attention mechanism. They will not be described here. In addition, the query matrix, key matrix, and value matrix can be generated based on different input sequences. For example, in the cross attention mechanism, the query matrix is ​​the input sequence and The key matrix and value matrix are obtained by multiplying the input sequence and as well as Multiply together.

[0064] The query matrix block, key matrix block and value matrix block are matrix blocks obtained after the input query matrix, key matrix and value matrix are respectively divided based on preset rules. It can be understood that in the relevant technology, the register is an on-chip storage resource inside the computing unit, and the storage space of the register is generally small. If the entire query matrix, key matrix and value matrix are stored in the register, it will cause the memory of the register to overflow and lose a large amount of data in the matrix. Based on the nature of matrix operations, the matrix to be operated can be divided into matrix blocks, and then local operations are performed based on the matrix blocks to obtain local operation results, and then the local operation results are cascaded to obtain the matrix operation results. Based on this, in the present disclosure, the query matrix, key matrix and value matrix are first divided into multiple query matrix blocks, key matrix blocks and value matrix blocks. Specifically, the query matrix, key matrix and value matrix can be divided according to the size of the register storage space of the computing unit. In this way, the size of each matrix block is relatively small and can be dispatched to the register of the computing unit for storage.

[0065] It should be noted that, in the related art, when performing attention operations, it is necessary to transpose the key matrix before performing matrix multiplication operations on the query matrix and the transposed key matrix. Based on this, in the present disclosure, in the process of dividing the key matrix into key matrix blocks, the key matrix can be transposed first, and then the transposed key matrix can be divided into key matrix blocks; or, in the present disclosure, the result of the matrix multiplication operation of the input sequence and the key weight matrix can be transposed and used as the key matrix. In another embodiment of the present disclosure, the key matrix can also be divided into multiple matrix blocks, and then the divided matrix blocks can be transposed to serve as key matrix blocks.

[0066] It is understandable that when performing attention operations, the shapes of the query matrix, key matrix, and value matrix are often the same, that is, the number of matrix rows and the number of matrix columns of the query matrix, key matrix, and value matrix are the same. For example, the query matrix is ​​a matrix with a shape of (1024, 128), and the key matrix and value matrix are also matrices with a shape of (1024, 128). In this way, after transposing the key matrix, a matrix with a shape of (128, 1024) is obtained.

[0067] After obtaining the query matrix block, key matrix block and value matrix block, they are scheduled to be stored in the register of the computing unit to wait for the subsequent attention operation. It can be understood that the computing unit contains multiple registers, and each register storage space is relatively small and cannot completely store the query matrix block, key matrix block or value matrix block. Based on this, in the present disclosure, the query matrix block, key matrix block and value matrix block can be allocated to different registers for storage. Specifically, in the present disclosure, the query matrix block and the value matrix block can be allocated to registers by row for storage, and one register stores several matrix rows in the query matrix block or the value matrix block; and the key matrix block is obtained by transposing the key matrix. When the attention operation is performed later, the key matrix is ​​matrix multiplied by the corresponding row in the query matrix by column. Based on this, the key matrix block can be stored in the register by column, and one register stores several matrix columns in the key matrix block.

[0068] In step 220, the computing unit runs multiple threads, which serve as the smallest units for the computing unit to perform specific tasks. After the query matrix block, key matrix block and value matrix block are scheduled to be stored in the registers of the computing unit, the threads inside the computing unit can call the tensor processor to access the registers and read the query matrix block and key matrix block stored in the registers. Specifically, the query matrix block and key matrix block in the registers are read into the first buffer and the second buffer of the tensor processor respectively as matrices to be calculated. After that, the tensor processor calls the matrix multiplication unit to perform matrix multiplication on the matrices in the first buffer and the second buffer, that is, the dot product of the data of each matrix row in the query matrix block and each matrix column in the key matrix block is summed, thereby obtaining the sub-product matrix corresponding to the query matrix block and the key matrix block. It can be understood that during the attention operation, the sub-product matrix needs to be processed later. Based on this, the sub-product matrix is ​​written back to the register of the computing unit. When the key matrix block and the query matrix block are multiplied, the key matrix block in the register is taken out to the tensor processor, and the space originally used to store the key matrix block in the register is cleared, and the subsequent attention operation process no longer needs to use the key matrix block. Therefore, the sub-product matrix can be written back to the position in the register originally used to store the key matrix block.

[0069] In one embodiment, the result of the sub-product matrix may be scaled to reduce the difference in the value of each element in the sub-product matrix, and then the scaled sub-product matrix is ​​stored in a register for subsequent operations. Specifically, refer to the following formula: in, is the sub-product matrix scaled and stored in registers, is the query matrix block, is the key matrix block, is the number of rows of the key matrix block, that is, the number of elements in a column of the key matrix block. It is understandable that since the neural network model often needs to be optimized through the back propagation algorithm, this process requires calculating the gradient of each element of the sub-product matrix and then updating the model parameters through the gradient descent method. During the forward propagation process, since the elements in the sub-product matrix are obtained by calculating the dot product of a row of elements of the query matrix block and the corresponding column elements of the transposed key matrix block, when the number of rows of the transposed key matrix block is large, that is, the number of elements in a column of the key matrix block is large, the range of dot product results will be relatively large, that is, the value difference of each element in the sub-product matrix calculated during the forward propagation process is relatively large, and during the backward propagation process, the gradient of each element in the sub-product matrix is ​​determined based on the element, that is, the gradient size of each element of the sub-product matrix calculated during the backward propagation process is related to the size of each element in the sub-product matrix calculated during the forward propagation process. When the size difference of the elements in the sub-product matrix calculated during the forward propagation process is too large, the gradient corresponding to the large value element in the sub-product matrix will also be very large in the backward propagation, while the gradient corresponding to the small value element will be close to 0 in the backward propagation process, resulting in the gradient disappearance during the backward propagation process. Based on this, in the present disclosure, during the forward propagation process, the elements of the sub-product matrix are scaled based on the number of rows of the key matrix block. This can reduce the size difference of each element in the sub-product matrix calculated during the forward propagation process, thereby avoiding the gradient disappearance caused by the large size difference of the elements of the sub-product matrix during the backward propagation process.

[0070] In step 230, after obtaining the sub-product matrix of the query matrix block and the key matrix block, the attention score of each part of the input sequence is determined. The higher the attention score, the higher the importance of the corresponding part in the input sequence. In the present disclosure, the sub-product matrix in the register is indexed by the index processing unit inside the computing unit. Specifically, the sub-product matrix is ​​read from the register of the computing unit by the thread of the computing unit, and then the index processing is performed by the index processing unit in the computing unit. Since the thread only needs to access the register inside the computing unit to read the sub-product matrix without accessing the off-chip storage space outside the computing unit, the time required for the thread to read the sub-product matrix is ​​shorter, and the index processing process is more efficient. Specifically, since the sub-exponential matrix is ​​obtained by exponentially processing each element of the sub-product matrix respectively, the shapes of the sub-exponential matrix and the sub-product matrix are consistent. When the sub-product matrix is ​​exponentially processed, the sub-product in the register is taken out to the exponential processing unit, and the space originally used to store the sub-product in the register is cleared, and the subsequent attention operation process no longer needs to use the sub-product matrix. Therefore, the sub-exponential matrix can be written back to the position in the register originally used to store the sub-product matrix, thereby ensuring that the memory of the register will not overflow.

[0071] In step 240, after obtaining the sub-index matrix, the sub-index matrix indicates the attention weights corresponding to each part of the query matrix block, and the sub-index matrix is ​​matrix-multiplied with the corresponding value matrix block to obtain the intermediate output with the query matrix block. Specifically, in the present disclosure, since the sub-index matrix and the value matrix block are stored in the register of the computing unit, the thread reads the sub-index matrix and the corresponding value matrix block from the register of the computing unit by calling the tensor processor, and then performs matrix multiplication operation on the sub-index matrix and the value matrix through the general matrix multiplication module of the tensor processor.

[0072] It should be noted that in the process of attention operation, the attention score matrix needs to be exponentially normalized to convert the attention score into attention weight. The process of exponential normalization is to exponentially normalize the elements of the matrix and then calculate the ratio between the exponential form of each element and the sum of the exponential forms of all elements of the same matrix row; that is, the normalization process is actually to exponentially normalize the elements of each matrix row in the sub-index matrix and then divide them by the normalization coefficients corresponding to each matrix row. Based on the properties of matrix operations, it can be known that the sub-index matrix can be normalized first, and then the normalized sub-index matrix can be matrix multiplied with the value matrix to obtain the attention operation result sub-block; or the sub-index matrix and the corresponding value matrix block can be matrix multiplied first, and then the product result can be normalized to obtain the attention operation result sub-block. The detailed process of performing matrix multiplication on the sub-index matrix and the value matrix block to obtain the attention operation result sub-block will be explained later and will not be repeated here. In step 250, the attention operation result sub-block represents the attention weight result calculated by a query matrix block and a corresponding key matrix block and value matrix block. Based on the multiple attention operation result sub-blocks calculated by the same query matrix block and each key matrix block and each value matrix block, the attention operation result corresponding to the query matrix block can be determined, and then the attention operation results corresponding to all query matrix blocks cut from the query matrix are spliced ​​to obtain a complete attention operation result matrix. For example, in step 210, the query matrix is ​​cut into query matrix blocks along the matrix row dimension, then multiple attention operation result sub-blocks are calculated based on each query matrix block, key matrix block and value matrix block, and then the multiple attention operation result sub-blocks corresponding to the same query matrix block are accumulated to obtain the attention weight matrix corresponding to the query matrix block, and then the attention weight matrices corresponding to each query matrix block are spliced ​​along the matrix row dimension to obtain the attention operation result matrix. In the embodiment disclosed in steps 210 to 250, the query matrix, key matrix and value matrix are first divided into corresponding matrix blocks, and then the query matrix blocks, key matrix blocks and value matrix blocks are scheduled to the registers of the computing unit. The threads inside the computing unit read the corresponding matrix blocks from the registers and hand them over to the tensor processor for calculation and write the generated sub-product matrices back to the registers. The indexing unit then indexes the sub-product matrices in the registers to obtain sub-index matrices and writes them back to the registers. The tensor processor then reads the sub-index matrices and value matrix blocks from the registers, obtains the attention operation result sub-blocks based on the sub-index matrices and value matrix blocks, and then determines the attention operation result matrix based on each attention operation result sub-block.By cutting the query matrix, key matrix and value matrix into smaller matrix blocks and scheduling them to be stored in the registers of the computing unit, during the entire process of attention operation, it is only necessary to read and write data from the registers without accessing the global memory, thereby reducing the memory access overhead. At the same time, since the access speed of the registers is faster than that of the global memory, the time required for the tensor processor and the index processing unit to read and write matrix data is shortened. The tensor processor can quickly read the required data from the register for matrix multiplication and write the result back to the register. The index processing unit can also quickly access the sub-product matrix in the register for index processing. The various steps in the attention operation process can be executed compactly, thereby improving the efficiency of the attention operation.

[0073] The above describes an implementation method of implementing the attention operation method of the present disclosure through a single computing unit. The following describes an implementation method of concurrently executing the attention operation method of the present disclosure through multiple computing units.

[0074] It is understandable that, due to the small storage space of the register of the computing unit, the multiple query matrix blocks, key matrix blocks and value matrix blocks obtained by segmenting the query matrix, key matrix and value matrix cannot be written into the register of the computing unit at the same time. Based on this, in one embodiment, referring to Figure 3 , step 210 comprises:

[0075] Step 310, dividing the query matrix, key matrix and value matrix used for target attention operation into query matrix blocks, key matrix blocks and value matrix blocks respectively according to the same division rule;

[0076] Step 320, placing the divided multiple query matrix blocks into a register in sequence according to the first cycle, wherein each first cycle includes a first number of second cycles, and the first number is the number of key matrix blocks that need to be multiplied with one query matrix block;

[0077] Step 330, in each second cycle of the first cycle, each key matrix block to be multiplied with the query matrix block is sequentially placed in the register, and each value matrix block corresponding to the sub-index matrix is ​​sequentially placed in the register.

[0078] In step 310, the division rule is a preset rule for dividing the query matrix, the key matrix and the value matrix into corresponding query matrix blocks, key matrix blocks and value matrix blocks, respectively. When performing the attention operation, it is necessary to perform matrix multiplication operation on the query matrix and the key matrix. At this time, the query matrix is ​​operated in units of matrix rows, that is, the elements of the matrix rows of the query matrix are dot-producted with the elements of the corresponding columns in the key matrix to obtain an element in the sub-product matrix. The operations of the elements of different matrix rows in the query matrix are independent of each other. Based on this, the query matrix block can be segmented along the dimension of the matrix row; and because the key matrix is ​​transposed, in the process of the attention operation, the key matrix is ​​used for matrix multiplication with the query matrix, that is, the matrix rows of the query matrix and the matrix columns of the key matrix are dot-producted and summed. Based on this, for the key matrix, it is necessary to segment according to the dimension of the matrix column; for the value matrix, it should be segmented along the dimension of the matrix row. However, in order to ensure the accuracy of the calculation results during the attention calculation process, the query matrix blocks, key matrix blocks and value matrix blocks that the tensor processor needs to read need to correspond to each other. For example, each key matrix block includes x matrix columns, and the sub-product matrix obtained by matrix multiplication of the query matrix block and the key matrix block also has x matrix columns, that is, each matrix row of the sub-product matrix has x elements, and each matrix column of the corresponding value matrix block should include x elements, that is, the number of matrix rows of the value matrix block is also x. It can be understood that since the key matrix and the value matrix have the same shape before transposition, the number of columns of the key matrix block and the number of rows of the value matrix are consistent when splitting. Therefore, the number of key matrix blocks and the number of value matrix blocks are actually equal, and there is actually a one-to-one correspondence between the key matrix blocks and the value matrix blocks. For example, the key matrix block composed of the first 128 matrix columns of the key matrix corresponds to the value matrix block composed of the first 128 matrix rows of the value matrix.

[0079] In the process of attention operation, a set of query matrix blocks, key matrix blocks and value matrix blocks required for attention operation need to be simultaneously scheduled to the register of the same computing unit. At this time, in order to avoid the memory overflow of the register, in one embodiment, the query matrix, key matrix and value matrix can be segmented based on the memory space size of the register. Figure 4 In one embodiment, the memory space of the register can store 128 matrix rows of the query matrix, 128 matrix columns of the key matrix, and 128 matrix rows of the value matrix. Thus, every 128 matrix rows in the query matrix are divided into a query matrix block, and n query matrix blocks are obtained from Block_q1 to Block_qn; every 128 matrix columns in the key matrix are divided into a key matrix block, and m key matrix blocks are obtained from Block_k1 to Block_km; every 128 matrix rows in the value matrix are divided into a value matrix block, and m value matrix blocks are obtained from Block_v1 to Block_vm.

[0080] In another embodiment, since the attention operation tasks corresponding to different matrix rows of the query matrix are independent of each other, and the computing unit actually executes specific attention operation tasks through thread bundles, each thread bundle can execute the corresponding task concurrently. At this time, if the size of a query matrix block exceeds the amount of data that the thread bundles of the computing unit can process concurrently, multiple thread bundles cannot complete the corresponding computing tasks concurrently at the same time. For example, multiple thread bundles of the computing unit can only execute tasks corresponding to 128 matrix rows concurrently, and the query matrix block includes 144 matrix rows. The attention operation tasks corresponding to 16 matrix rows need to wait until the attention operations of the previous 128 matrix rows are completed before they can be executed, which leads to reduced efficiency. Based on this, in the present disclosure, the query matrix, key matrix and value matrix can also be divided based on the product of the number of thread bundles of the computing unit and the number of matrix rows that a thread bundle can process concurrently. For example, the computing unit includes 32 thread bundles, each of which can concurrently complete the running tasks corresponding to 4 matrix rows. At this time, every 32*4=128 matrix rows in the query matrix can be divided into a query matrix block. Similarly, every 128 matrix columns in the key matrix can be divided into a key matrix block, and every 128 matrix rows in the value matrix can be divided into a value matrix block.

[0081] In addition, since the intermediate data (such as sub-product matrix and sub-exponent matrix) generated during the attention operation also needs to be written into the register, referring to the description of steps 220 and 230 above, it can be seen that the key matrix block, sub-product matrix and sub-exponent matrix are written into the same position in the register in sequence. In order to ensure that the memory of the register will not overflow when writing the sub-product matrix and sub-exponent matrix into the register, the query matrix, key matrix and value matrix can also be divided into multiple square matrices of the same size as query matrix blocks, key matrix blocks and value matrix blocks. In this way, the scale of the intermediate data generated during the attention operation can be made the same as the scale of the key matrix block. In this way, it is only necessary to clear the key matrix block in the register before writing the sub-product matrix, and then write the sub-product matrix into the position of the key matrix block. Similarly, it is only necessary to clear the sub-product matrix in the register before writing the sub-exponent matrix, and then write the sub-exponent matrix into the position where the sub-product matrix is ​​stored.

[0082] In step 320, the first cycle is the cycle required for calculating the attention weight corresponding to a query matrix block. In a first cycle, the query matrix blocks stored in the register of the computing unit are the same. It can be understood that in the process of attention calculation, it is necessary to perform dot product summation on each matrix row of the query matrix and each matrix column of the key matrix. For example, referring to Figure 4In the process of attention operation, the first query matrix block Block_q1 needs to perform matrix multiplication operation with Block_k1 to Block_km in sequence to obtain Block_q1k1 to Block_q1km. Since the key matrix also needs to be divided into multiple key matrix blocks, and multiple key matrix blocks cannot be put into the register at the same time, based on this, a first cycle will include a first number of second cycles, where the first number is the number of key matrix blocks obtained from the key matrix. For example, refer to Figure 4 , m key matrix blocks are cut out from the key matrix, and the first number is m. Then in the first cycle, Block_q1 is put into the register, and then according to the second cycle, multiple key matrix blocks and multiple value matrix blocks are sequentially put into the register.

[0083] In step 330, since the first number of key matrix blocks need to perform matrix multiplication operations with the query matrix block in sequence, and the key matrix block and the value matrix block are in one-to-one correspondence, based on this, in each second cycle in the first cycle, a key matrix block and a value matrix block corresponding to the key matrix block are sequentially placed in the register, that is, a value matrix block corresponding to the sub-index matrix obtained based on the query matrix block and the key matrix block. For example, referring to Figure 4 , in the first second cycle of the first cycle, Block_k1 and Block_v1 are placed in the register, and in the second second cycle of the same first cycle, Block_k2 and Block_v2 are placed in the register, and so on. At the same time, each second cycle only needs to execute the attention operation tasks corresponding to one query matrix block, one key matrix block, and one value matrix block, and the amount of intermediate data generated is also greatly reduced. These intermediate data can also be written to the register, so that the entire attention operation process does not need to access the global memory, reducing the memory access overhead. Exemplarily, the size of each matrix block (including the query matrix block, the key matrix block, and the value matrix block) can be pre-set, and then, according to the first cycle and the second cycle, the matrix data is read from the temporary buffer for receiving the corresponding matrix based on the set matrix block size to form a matrix block and write it into the register of the computing unit. For example, the size of each matrix block is set by the following statement:

[0084] accbuf_h=128

[0085] accbuf_w=128

[0086] B_q_size=accbuf

[0087] B_kv_size=accbuf_w

[0088] Among them, B_q_size is a parameter for controlling the number of matrix rows read from the query matrix at a time, and B_q_size is a parameter for controlling the number of matrix rows read from the key matrix and the value matrix at a time. By setting the values ​​of the two parameters accbuf_h and accbuf_w, and assigning these two parameters to B_q_size and B_kv_size, in each first cycle, according to the parameter B_q_size, 128 matrix rows are read from the buffer storing the query matrix as a query matrix block and written into the register; in each second cycle of the first cycle, according to the parameter B_kv_size, 128 matrix rows are read from the buffer storing the key matrix and the value matrix as the key matrix block and the value matrix block and written into the register. In this way, there is no need to write the query matrix, key matrix and value matrix into the global memory, and then the thread of each computing unit obtains the corresponding matrix data by accessing the global memory.

[0089] Exemplarily, the first cycle and the second cycle may be set in a nested loop manner, and the steps required to be executed in the corresponding cycle may be executed in the loop through corresponding statements. For example, one execution method is as follows:

[0090] for 1≤i≤outer_n;

[0091] q[i]; / / Write the query matrix block corresponding to the first cycle of the i-th period into the register;

[0092] for 1≤j≤inner_n;

[0093] k[j], v[j]; / / Write the key matrix block and value matrix block corresponding to the jth second period into the register;

[0094] attention(q[i], k[j], v[j]); / / Attention is performed on the query matrix block q[i] corresponding to the i-th first cycle, the key matrix block k[j] and the value matrix block v[j] corresponding to the j-th second cycle;

[0095] end for

[0096] end for

[0097] Among them, outer_n represents the number of the first cycle, that is, the number of query matrix blocks, which can be determined based on the quotient of the number of matrix rows of the query matrix and the set query matrix block size, for example, outer_n=N_Q / B_q_size, where N_Q represents the number of matrix rows of the key matrix, and B_q_size refers to the above formula, which is the number of matrix rows read from the query matrix at a single time, that is, the size of the query matrix block.

[0098] inner_n represents the number of the second period, that is, the number of key matrix blocks (or value matrix blocks), which can be determined based on the quotient of the number of matrix rows of the key matrix (or value matrix) and the set key matrix block (or value matrix block) size, for example, inner_n=N_K / B_kv_size, where N_K represents the number of matrix rows of the key matrix, and B_kv_size refers to the above formula, which is the number of matrix rows read from the key matrix at a single time, that is, the size of the key matrix block.

[0099] In step 310 to step 330, the query matrix, key matrix and value matrix are cut into query matrix blocks, key matrix blocks and value matrix blocks of specified sizes based on preset rules. After that, the query matrix blocks are sequentially placed into registers according to the first cycle, and the key matrix blocks and value matrix blocks are sequentially placed into registers according to the second cycle. In this way, multiple query matrix blocks, key matrix blocks and value matrix blocks are written into registers one after another, thereby avoiding memory overflow of registers. In this way, during the attention operation, the matrix data to be read can be placed in the registers of the computing units for reading and writing without having to be written to off-chip storage spaces such as global memory, thereby reducing memory access overhead, and utilizing the fast reading and writing speed of registers to reduce the delay caused by the tensor processor reading and writing data during the operation, thereby improving operation efficiency.

[0100] In the above steps 310 to 330, an embodiment of dispatching the query matrix block, the key matrix block and the value matrix block to the register of the computing unit is described in detail, taking a single computing unit as an example. In another embodiment, the computing unit is a plurality of computing units. Figure 5 In one embodiment, step 320 includes:

[0101] Step 510, in each first cycle, multiple query matrix blocks are placed in registers of multiple computing units respectively, wherein the register of each computing unit is placed in one query matrix block, and the same query matrix block is only placed in the register of one computing unit in one first cycle;

[0102] In step 510, referring to the description of step 310 above, it can be known that in the attention operation process, the operations corresponding to different matrix rows of the query matrix are independent of each other, and the attention operation processes corresponding to the query matrix blocks obtained by splitting the query matrix based on the matrix row dimension are also independent of each other. Based on this, in the present disclosure, when there are multiple computing units, the multiple query matrix blocks are respectively placed in the registers of the multiple computing units, and one query matrix block is placed in the register of each computing unit. In this way, the attention operations corresponding to the multiple query matrix blocks are performed in parallel by the multiple computing units. Specifically, refer to Figure 4After the query matrix is ​​split, n query matrix blocks are obtained from Block_q1 to Block_qn, and the computing units include computing units 1 to computing units n, at this time, the n query matrix blocks are placed in the register of a computing unit respectively, for example, Block_q1 is placed in the register of computing unit 1, and computing unit 1 is responsible for the attention operation process corresponding to Block_q1; Block_q2 is placed in the register of computing unit 2, and computing unit 2 is responsible for the attention operation process corresponding to Block_q2; and so on.

[0103] It is understandable that in another embodiment, the number of computing units will be less than the number of query matrix blocks. For example, the number of computing units is n / 2. In this case, in the first cycle, the first n / 2 query matrix blocks are placed in each computing unit respectively. After that, when a computing unit completes the attention operation of the corresponding query matrix block, the query matrix blocks from n / 2+1 to n are placed in the registers of the idle computing units, and the corresponding computing units perform attention calculations.

[0104] It is understandable that, at this time, the number of tensor processors can also be multiple, and multiple computing units can share multiple tensor processors to perform attention operations corresponding to each query matrix block in parallel.

[0105] Step 330 includes:

[0106] Step 520: In each second cycle of the first cycle, each key matrix block and the value matrix block corresponding to the key matrix block are sequentially placed in the register of each computing unit.

[0107] In step 520, referring to the relevant description of step 330 above, it can be known that in the process of attention operation, the query matrix block needs to perform matrix multiplication operation with each key matrix block in turn. Based on this, in each second cycle of the first cycle, each key matrix and the value matrix block corresponding to the key matrix block need to be placed in the register of each computing unit in turn, so that each computing unit can complete the attention operation task of the corresponding query matrix block.

[0108] In the embodiment disclosed in steps 510 to 520, multiple query matrix blocks are respectively scheduled to the registers of multiple computing units, and multiple key matrix blocks and corresponding value matrix blocks are scheduled to the registers of each computing unit in each second cycle of the first cycle. In this way, attention operations can be performed in parallel by multiple computing units to improve computing efficiency.

[0109] In one embodiment, the tensor processor includes a matrix multiplication unit and an accumulation buffer. After the query matrix block, the key matrix block, and the value matrix block are placed in the register according to the first cycle and the second cycle, the tensor processor refers to Figure 6 , step 220, comprising:

[0110] Step 610, through the matrix multiplication operation unit, in each second cycle of the first cycle, read the query matrix block and the key matrix block from the register, and perform matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix, and write the sub-product back to the register.

[0111] In each second cycle of the first cycle, the register of the computing unit stores the query matrix block corresponding to the first cycle and the key matrix block corresponding to the second cycle. Specifically, referring to the relevant description of step 210, it can be known that the computing unit actually contains multiple registers, and the multiple matrix rows of the query matrix block are stored in multiple registers respectively, and the multiple matrix columns of the key matrix block are also stored in multiple registers respectively. There are multiple threads running inside the computing unit, and each thread can access the specified register. At this time, in the computing unit, the corresponding registers can be accessed by multiple threads, and the corresponding matrix rows or matrix columns are read out and input into the tensor processor. For example, some threads read the matrix rows of the query matrix block from the corresponding registers and input them into the first buffer of the tensor processor, thereby forming a completed query matrix block in the tensor processor; another part of the threads reads the matrix columns of the key matrix block from the corresponding registers and inputs them into the second buffer of the tensor processor to form a key matrix block. After that, the tensor processor performs matrix multiplication operations on the query matrix block in the first buffer and the key matrix block in the second buffer through the matrix multiplication unit, thereby obtaining the corresponding sub-product matrix. After obtaining the sub-product matrix, the sub-product matrix needs to be subsequently processed, and based on this, the sub-product matrix is ​​written back to the register. It can be understood that after the sub-product matrix is ​​obtained based on the query matrix block and the key matrix block, the key matrix block is no longer needed in the subsequent attention operation process of the first cycle. Based on this, there is no need to write the key matrix block back to the register. That is, at this time, the space originally used to store the key matrix block in the register has been cleared. At this time, the sub-product matrix can be written into the register of the computing unit, so that the memory overflow of the register will not be caused.

[0112] Exemplarily, in the i-th second cycle of the j-th first cycle, the statement for calling the matrix multiplication unit in the tensor processor to perform a matrix multiplication operation on the query matrix block and the key matrix block may refer to the following format:

[0113] s[i]=GEMM(q[j],k_t[i]);

[0114] Among them, s[i] represents the sub-product matrix of the i-th second cycle, q[j] represents the query matrix block placed in the register in the j-th first cycle, k_t[i] represents the key matrix block placed in the register in the i-th second cycle, and GEMM() represents calling the matrix multiplication unit to perform matrix multiplication operations.

[0115] After step 230, the attention operation processing method further includes:

[0116] Step 620, in the first second cycle of the first cycle, summing up each matrix row of the sub-index matrix to obtain a first sum of each matrix row, determining a normalization coefficient matrix based on each first sum, and writing the normalization coefficient matrix into a register;

[0117] Step 630, in each second period of the first period, sum each matrix row of the corresponding sub-exponential matrix to obtain a first sum of each matrix row, and add the first sum of each matrix row to the corresponding matrix row in the normalization coefficient matrix to update the normalization coefficient matrix, wherein the sub-exponential matrix is ​​obtained by taking each element of the corresponding sub-product matrix as the exponent of a preset base.

[0118] Specifically, after obtaining the sub-product matrix, the sub-product matrix is ​​indexed by the exponential processing unit. Specifically, each element in the sub-product matrix is ​​used as the exponent of the preset base to obtain the corresponding sub-exponential matrix. Based on the properties of matrix operations [x*A]·[B]=x*[A]·[B], where x represents a constant coefficient, [A] and [B] are matrices that need to be multiplied, it can be seen that the sub-exponential matrix and the corresponding value matrix block can be matrix multiplied first, and then each matrix row of the product result is divided by the corresponding normalization coefficient to obtain the attention result. The normalization coefficient is actually the sum of the exponential results of all elements in each matrix row in the product result of the query matrix block and the complete key matrix. However, since in this embodiment, the sub-exponential matrix is ​​only the product result of the query matrix block and a key matrix block, each matrix row of the sub-exponential matrix only includes a part of the elements of each matrix row in the product result of the query matrix block and the complete key matrix, there will be errors in the result of directly normalizing the sub-exponential matrix. Based on this, in this embodiment, in the first second period of the first period, each matrix row of the sub-exponential matrix is ​​summed up respectively to obtain the first sum corresponding to each matrix row, and a normalization coefficient matrix is ​​constructed based on the first sum and written into the register. Afterwards, for each second period of the same first period, each matrix row of the sub-exponential matrix is ​​summed up respectively, and the summed results of each matrix row are respectively accumulated to the corresponding matrix rows in the normalization coefficient matrix. In this way, when accumulated to the last second period in the first period, each matrix row in the normalization coefficient matrix stores the sum of the exponential results of all elements of each matrix row in the product result of the query matrix block and the complete key matrix, that is, the accurate normalization coefficient.

[0119] Exemplarily, the updating of the normalization coefficient matrix can be achieved by adding the following statement in the loop of each second period:

[0120] d_current=d_pre+rowsum([pi]);

[0121] d_pre = d_current;

[0122] Among them, [pi] represents the sub-index matrix of the ith second period, rowsum() represents the function of summing the matrix rows, and d_pre and d_current represent the normalized coefficient matrices before and after the accumulation of the sub-index matrix of the ith second period, respectively. It can be understood that since the normalized coefficient matrix is ​​only used to save the summation results of the matrix rows of the sub-index matrix, it only needs one matrix column, and its number of matrix rows is only consistent with the number of matrix rows of the sub-index matrix, that is, it is consistent with the number of matrix rows of the query matrix block, that is, the shape of the normalized coefficient matrix can be expressed as [b_q_size,1]. It can be seen that the scale of the normalized coefficient matrix is ​​relatively small and can be directly written into the register without causing insufficient memory in the register.

[0123] In one embodiment, multiple second cycles of the same first cycle are used to calculate the attention results corresponding to the same query matrix block, and the attention operation result is obtained by normalizing the index of the product matrix of the query matrix block and the key matrix, and performing matrix multiplication on the sum value matrix. The process of matrix multiplication is to perform dot product summation on the elements of the matrix rows of the product matrix after the index normalization and the elements of the matrix columns of the sum value matrix. Since the key matrix and the value matrix are divided into multiple key matrix blocks and value matrix blocks, and are calculated separately in each second cycle, it is necessary to accumulate the attention operation results of each second cycle to obtain the attention operation result corresponding to the query matrix. Based on this, in one embodiment, referring to Figure 7 , step 250 comprises:

[0124] Step 710, in each first cycle, initialize the accumulation buffer to obtain the initialized attention intermediate result sub-matrix;

[0125] Step 720, in each second cycle of the first cycle, accumulating the attention operation result sub-block corresponding to the current second cycle to the attention intermediate result sub-matrix in the accumulation buffer, wherein the attention operation result sub-block is obtained by multiplying the sub-index matrix corresponding to the current second cycle and the value matrix block;

[0126] Step 730, after accumulating the attention operation result sub-block corresponding to the last second cycle of the first cycle to the attention intermediate result sub-matrix, correct the attention intermediate result sub-matrix based on the normalization coefficient matrix to obtain the attention result sub-matrix corresponding to the first cycle;

[0127] Step 740, concatenate the attention result sub-matrices corresponding to each first period to obtain an attention operation result matrix.

[0128] In step 710, since different first cycles calculate the attention result sub-matrices corresponding to different query matrix blocks, and the attention result sub-matrices corresponding to different query matrix blocks are irrelevant, in each first cycle, before placing the corresponding query matrix block into the register, it is necessary to take out the attention result sub-matrix corresponding to the previous first cycle from the accumulation buffer, so as to initialize the accumulation buffer and obtain the initialized attention intermediate result sub-matrix.

[0129] In step 720, in each second cycle, an attention operation is performed based on the key matrix block and the value matrix block placed in the register to obtain an attention operation result sub-block, which is then written into the accumulation buffer and accumulated into the attention intermediate result sub-matrix. In this way, the attention operation result sub-block corresponding to each second cycle is accumulated through the accumulation buffer.

[0130] Specifically, the process of calculating the attention operation result sub-block includes: performing matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix, then exponentially processing the sub-product matrix to obtain a sub-exponent matrix, performing matrix multiplication on the sub-exponent matrix and the value matrix block in the register to obtain the attention operation result sub-block corresponding to the current second cycle.

[0131] In step 730, in the last second cycle of the first cycle, after the corresponding attention operation result sub-block is written into the accumulation buffer and accumulated into the attention intermediate result sub-matrix, it indicates that the attention operation corresponding to the query matrix block and all key matrices and value matrix blocks has been completed, that is, at this time, the attention intermediate result sub-matrix is ​​the attention operation result obtained based on the query matrix block, the complete key matrix and the value matrix. Since in the above step 720, the sub-index matrix and the corresponding value matrix block are directly matrix multiplied to obtain the attention operation result sub-block, and the sub-index matrix is ​​not normalized to convert it into the corresponding attention weight, refer to the above step 6 It can be seen from step 20 and step 630 that in the last second cycle of the first cycle, the normalization coefficient matrix in the register stores the sum of the elements of each matrix row of each sub-index matrix, that is, when the sub-index matrix is ​​normalized, each sub-index matrix needs to be divided by the coefficient. Based on the properties of matrix operations, it can be seen that at this time, the attention intermediate result sub-matrix can be corrected based on the normalization coefficient matrix. Specifically, the elements of each matrix row of the attention intermediate result sub-matrix are divided by the elements of the corresponding matrix row in the normalization coefficient matrix. For example, each element of the first matrix row of the attention intermediate result sub-matrix is ​​divided by the elements of the first matrix row in the normalization coefficient matrix. In this way, an accurate attention result sub-matrix can be obtained.

[0132] Exemplarily, the process from step 710 to step 730 may be implemented by the following pseudo code:

[0133] for 1<=j<=outer n:

[0134] o_pre = 0;

[0135] for 1<=i<=inner n:

[0136] s[i]=GEMM(q[j],kt[i])

[0137] p[i]=exp(s[i])

[0138] d current =d_pre+ row_sum(p[i])

[0139] o[j]=o_pre+GEMM(p[i],v[i])

[0140] d pre =d current

[0141] o_pre =o[j]

[0142] o =o[inner n] / d_current

[0143] Among them, outer n represents the number of first cycles; inner n represents the number of second cycles within a first cycle, that is, the first number; q[j] represents the query matrix block placed in the register in the jth first cycle; kt[i] represents the result of the transposition of the key matrix block placed in the register in the ith second cycle; v[i] represents the value matrix block placed in the register in the ith second cycle; row_sum() represents a function for summing each matrix row of a matrix; s[i] represents a sub-product matrix; p[i] represents a sub-exponential matrix; GEMM() represents matrix multiplication through a matrix multiplication unit; exp() represents an exponential function, that is, taking the input elements as the exponent of the natural base e; o_pre and o[j] respectively represent the attention intermediate result submatrices before and after each second cycle update; d_current and d_pre respectively represent the normalization coefficient matrices before and after each second cycle update; o[innern] represents the attention intermediate result submatrix of the inner nth second cycle, that is, the last second cycle.

[0144] In step 740, after obtaining the attention result sub-matrices corresponding to each first period, that is, the attention result sub-matrices corresponding to each query matrix block, each attention result sub-matrix is ​​spliced ​​along the direction of the matrix row to obtain the attention result matrix corresponding to the query matrix.

[0145] In registers, data is stored in binary format. The size of the values ​​that a register can support is limited. For example, a register may only support the storage of [ ] interval, and the elements in the sub-product matrix need to be used as the exponents of the preset exponents during the exponential processing, which makes the data in the sub-index matrix relatively large and easily exceeds the data range supported by the register. Based on this, in one embodiment, the register stores a row maximum value vector, and each element in the row maximum value vector is used as the row maximum value corresponding to each matrix row in the sub-product matrix. Figure 8 , before step 230, the attention operation processing method further includes:

[0146] Step 810, in each first cycle, initializing a row maximum value vector;

[0147] Step 820, in the Mth second period of the first period, determine the element maximum value of each matrix row in the corresponding sub-product matrix, and when the element maximum value is greater than the row maximum value of the corresponding matrix row in the row maximum value vector, update the corresponding row maximum value based on the element maximum value to obtain the row maximum value corresponding to the Mth second period, where 1≤M≤N, and N is the first number.

[0148] In step 810, since each first cycle calculates the attention results corresponding to different query matrix blocks, the product results of different query matrix blocks and the key matrix are in different matrix rows, then the corresponding row maximum value vectors cannot be shared. Therefore, at the beginning of each first cycle, the row maximum value vector is initialized. Specifically, before the query matrix block corresponding to the first cycle is placed in the register, the row maximum value vector in the register is initialized.

[0149] In step 820, the element maximum value refers to the maximum value of the elements of each matrix row in the sub-product matrix of the Mth second period. For example, if the elements of a matrix row in the sub-product matrix are [1, 8, 6, 12, 10], then the corresponding row maximum value of the matrix row is 12. Since the attention operation process requires the accumulation of attention operation result sub-blocks calculated in multiple second periods of the same first period to obtain the attention result sub-matrix corresponding to the first period, and each second period can only calculate the product of the query matrix block and the key matrix block corresponding to the current second period, if each second period is indexed based on the maximum value of the elements of each matrix row in the sub-product matrix of the current second period, then the row maximum value corresponding to each second period is inconsistent, which will cause the attention result obtained by accumulating the attention operation result sub-blocks of each second period to be wrong. In order to ensure the consistency of the row maximum values ​​of different second periods of the same first period, in the Mth second period, the element maximum values ​​corresponding to each matrix row in the corresponding sub-product matrix are compared with the corresponding row maximum values ​​in the row maximum value vector. When the element maximum value of a certain matrix row in the sub-product matrix of the Mth second period is greater than the row maximum value of the corresponding matrix row in the row maximum value vector, the corresponding row maximum value in the row maximum value vector is updated based on the element maximum value. In this way, the row maximum value vector stores the maximum values ​​of each matrix row in the sub-product matrix of the first M second periods, so that the consistency of the row maximum values ​​of the sub-product matrix of the first M periods during the index processing can be maintained. When the row maximum value of a certain matrix row is updated in the M+1th second period, the correction coefficient can also be determined based on the row maximum values ​​before and after the update to correct the calculation results of the first M second periods; when the row maximum value changes, the process of correcting the calculation results of the previous second period will be described later, so it will not be repeated here.

[0150] In the case where a row maximum value vector is stored in the register, step 230 includes:

[0151] Step 830, in the Mth second cycle of the first cycle, read the row maximum value of each matrix row from the register through the exponential processing unit, determine the first difference corresponding to each element of the sub-product matrix based on each element of the corresponding sub-product matrix and the corresponding row maximum value, and use each first difference as the exponent of the preset exponent to obtain the sub-index matrix corresponding to the sub-product matrix.

[0152] In step 830, the row maximum value corresponding to each matrix row is determined by reading the row maximum value vector in the register, and the first difference between each element of the sub-product matrix and the row maximum value vector is calculated. At this time, the value range of the first difference is (-∞,0], and the first difference is used as the index of the preset index to obtain a sub-index matrix. The size of each element in the sub-index matrix falls within the range of (0,1], thereby preventing the indexed elements from being too large and exceeding the value range supported by the register. Exemplarily, the formula for implementing step 830 is as follows: ,in, represents the i-th element of a matrix row in the sub-product matrix, Represents the row maximum of the corresponding matrix row.

[0153] In one embodiment, since the row maximum value in the row maximum value vector can only reflect the row maximum value of each matrix row in all sub-product matrices of the first M-1 second cycles in the M-1 second cycle, and the row maximum value may be updated in the M-1 second cycle, it is necessary to correct the attention operation result sub-block of the M-1 second cycle. Based on this, refer to Fig. 9 , step 720 includes:

[0154] Step 910, in the Mth second period, based on the row maximum value corresponding to the Mth second period and the row maximum value corresponding to the M-1th second period, determine the correction coefficient, correct the attention intermediate result sub-matrix of the accumulation buffer based on the correction coefficient, and accumulate the attention operation result sub-block corresponding to the Mth second period to the corrected attention intermediate result sub-matrix.

[0155] In step 910, since the row maximum value of each second period may change compared with the previous second period, when the sub-product matrix is ​​indexed, it is indexed in combination with the corresponding row maximum value. When the row maximum value changes, the indexation result of the previous second period needs to be corrected. Referring to step 830, the form of the elements in the sub-index matrix obtained after the indexation process is , when the row maximum value changes, for example, in the Mth second cycle, the row maximum value of a matrix row changes from becomes , the elements of the corresponding matrix rows in the sub-index matrix need to be changed from Corrected to , at this time, we can use the maximum value of the row in the Mth second cycle and the row maximum value of the previous second cycle Determine the correction factor ( ) and then ( )*( ), in this way, the sub-index matrix of the first M-1 second periods can be corrected, and since in the first M-1 second periods, the sub-index matrix needs to be matrix multiplied with the corresponding value matrix block to obtain the attention operation result sub-block and write it into the accumulation buffer and accumulate it into the attention intermediate result sub-matrix, based on [x*A]·[B]=x*[A]·[B], it can be known that the attention result sub-matrix can be directly corrected by the correction coefficient, so as to correct the attention intermediate result sub-matrix of the first M-1 second periods, and then accumulate the attention operation result sub-block of the current second period into the corrected attention intermediate result sub-matrix, thereby ensuring the accuracy of the attention intermediate result sub-matrix when the row maximum value is updated.

[0156] It can be understood that since the normalized coefficient matrix is ​​obtained by summing the matrix rows of the sub-exponential matrices of the second period, and since it is necessary to calculate the first difference between the elements of the sub-product matrix and the corresponding row maximum value when the sub-product matrix is ​​indexed, and perform indexation processing based on the first difference, when the row maximum value is updated, the result of the indexation processing should also be corrected, that is, the normalized coefficient matrix needs to be corrected synchronously. Based on this, refer to Fig. 9 In step 630, the first sums of the matrix rows are respectively added to the corresponding matrix rows in the normalized coefficient matrix to update the normalized coefficient matrix, including:

[0157] Step 920, in the Mth second period in the first period, correct the normalized coefficient matrix corresponding to the M-1th second period based on the correction coefficient, and add the first sum of each matrix row of the sub-index matrix corresponding to the Mth second period to the corresponding matrix row in the corrected normalized coefficient matrix to obtain the normalized coefficient matrix corresponding to the Mth second period.

[0158] In step 920, it can be understood that since the elements in the normalized coefficient matrix are obtained by summing the elements of each matrix row of each sub-index matrix of the second period, each element in the normalized coefficient matrix can be expressed as: ,in, represents the i-th element of the corresponding matrix row in the first M-1 sub-product matrices of the second period; represents the result of the exponential processing; j represents the total number of elements in the same matrix row in multiple sub-exponential matrices of the first M-1 second periods.

[0159] It is not difficult to see from the above formula that when the row maximum value is updated, the elements in the normalized coefficient matrix can be modified directly by correction, so as to obtain the result of accumulating the sub-index matrix after correction. For example, in the Mth second period, the row maximum value of a matrix row changes from becomes , determine the correction factor as ( ), multiply the elements of the corresponding matrix rows in the correction coefficient and the normalization coefficient matrix, that is: , then all elements of each matrix row in the sub-index matrix of the Mth second period are accumulated to the normalization coefficient of the corresponding matrix row in the normalization coefficient matrix, thereby ensuring the accuracy of the normalization coefficient matrix when the row maximum value is updated.

[0160] Exemplarily, when the sub-product matrix is ​​indexed in combination with the row maximum, the attention operation process can be expressed as:

[0161] for 1<=j<=outer n:

[0162] o_pre=0

[0163] d_pre=0

[0164] for 1<=i<=inner n:

[0165] s[i]=GEMM(q[j],kt[i])

[0166] m_current = row_max(s[i])

[0167] m_current=max(m_current,m_pre)

[0168] p[i]=exp(s[i]- m_current)

[0169] rescale_factor=exp(m_pre-m_current)

[0170] d current =mul(d_pre,rescale_factor)+ row_sum(p[i])

[0171] o[j]=mul(o_pre,rescale_factor)

[0172] o[j]=o_pre+GEMM(p[i],v[i])

[0173] d pre = d current

[0174] m_pre = m_current

[0175] o_pre =o[j]

[0176] o =o[inner n] / d_current

[0177] Among them, m_current and m_pre respectively represent the row maximum values ​​before and after the update of each matrix row in each second cycle; rescale_factor represents the correction factor; row_max() represents taking the maximum value of the elements in each matrix row; max() represents taking the maximum value of the two input data; mul() represents multiplying the matrix row by row.

[0178] In one embodiment, the computing unit further includes a plurality of thread warps, each thread warp corresponding to a second number of matrix rows in the attention intermediate result submatrix, referring to Fig.10 , based on the normalized coefficient matrix, the attention intermediate result sub-matrix is ​​modified, including:

[0179] Step 1010, executing a plurality of thread warps in parallel, wherein each thread warp obtains a second number of normalization coefficients corresponding to a second number of matrix rows from a normalization coefficient matrix through an indexing processing unit, and corrects the second number of matrix rows based on the second number of normalization coefficients, respectively, to obtain a second number of normalized attention result matrix rows, wherein each matrix row corresponds to a normalization coefficient;

[0180] Step 1020 , concatenate and merge the rows of the normalized attention result matrices corresponding to the multiple thread warps to obtain an attention result sub-matrix corresponding to the first cycle.

[0181] The attention intermediate result submatrix is ​​obtained by accumulating the attention operation result sub-blocks. Referring to the relevant description of step 730, it can be known that the attention intermediate result submatrix needs to be normalized based on the normalization coefficient matrix. Since the normalization coefficients corresponding to each matrix row in the attention intermediate result submatrix in the normalization coefficient matrix are different, the normalization process of each matrix row is independent of each other. Based on this, multiple matrix rows in the attention intermediate result submatrix can be corrected in parallel by multiple thread bundles of the computing unit. Each thread bundle is responsible for correcting the elements of the second number of matrix rows in the attention intermediate result submatrix to obtain the second number of normalized attention result matrix rows corresponding to the thread bundle. After that, the normalized attention result matrix rows of each thread bundle are spliced ​​to obtain the attention result submatrix corresponding to the first cycle. By using multiple thread bundles in parallel, the efficiency of correction can be improved.

[0182] In one embodiment, after the query matrix block is sequentially written into the register according to the first cycle, and the key matrix block and the value matrix block are sequentially written into the register according to the second cycle, referring to Fig.11 , step 220 comprises:

[0183] Step 1110, through the matrix multiplication operation unit, in each second cycle of the first cycle, read the query matrix block and the key matrix block from the register, perform matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix, and write the sub-product back to the register;

[0184] After step 230, the attention calculation method further includes:

[0185] Step 1120, write the sub-index matrix back to the accumulation buffer, and cascade the sub-index matrices in the accumulation buffer to obtain a cascaded index matrix.

[0186] The details of step 1110 may refer to the relevant description of the above step 610, which will not be repeated here.

[0187] In step 1120, the cascade index matrix is ​​a matrix obtained by cascading the sub-index matrices corresponding to the multiple second cycles that have been executed in the current first cycle. For example, Figure 4 , currently in the fourth second cycle of the first cycle corresponding to Block_q1, the cascade index matrix is ​​the matrix obtained by exponentially processing and cascading Block_q1k1 to Block_q1k4.

[0188] It is understandable that after obtaining the sub-index matrix, the sub-index matrix needs to be normalized, and the normalization process actually needs to be based on each matrix row in the product result of the query matrix block and the complete key matrix. Figure 4 , after performing matrix multiplication on Block_q1 and Block_k1 to Block_km respectively, multiple sub-product matrices such as Block_q1k1 to Block_q1km are obtained respectively, and during normalization, the sub-index matrices corresponding to Block_q1k1 to Block_q1km need to be cascaded, and then each matrix row needs to be normalized. In the present disclosure, due to the storage space limitation of the register, it is impossible to write all the sub-index matrices corresponding to each second period into the register of the calculation unit at the same time. At the same time, since the key matrix is ​​divided into multiple key matrix blocks, each sub-index matrix can only reflect the indexed form of the product result of the query matrix block and a part of the matrix columns in the key matrix. There will be a certain error between the result obtained by local normalization based on the sub-index matrix and the result of global normalization. Based on this, in the present disclosure, in each second cycle, after obtaining the sub-index matrix, the sub-index matrix is ​​also written into the accumulation buffer, and then the sub-index matrix corresponding to each key matrix block is cascaded in the accumulation buffer, and then the result obtained by local normalization processing based on the sub-index matrix is ​​corrected. The correction process will be explained later and will not be repeated here.

[0189] In the embodiment disclosed in steps 1110 to 1120, in each second cycle of the first cycle, the query matrix block corresponding to the first cycle and the key matrix block corresponding to the second cycle are taken out from the register of the computing unit through the tensor processor, and then the query matrix block and the key matrix are subjected to matrix multiplication operation by the matrix multiplication unit to obtain a sub-product matrix, and then the sub-product matrix is ​​written back to the register for subsequent attention operation processing. At the same time, after the sub-product is exponentially processed, the sub-exponential matrix is ​​written back to the accumulation buffer to correct the sub-exponential normalized matrix obtained by local normalization later.

[0190] In one embodiment, referring to Fig.12 , step 230 comprises:

[0191] Step 1210, reading the sub-index matrix corresponding to the current second period from the register through the index processing unit, and obtaining the cascade index matrix from the accumulation buffer;

[0192] Step 1220, cascade the cascade index matrix and the sub-index matrix and perform normalization processing to obtain a cascade index normalized matrix, separate a sub-index normalized matrix corresponding to the sub-index matrix from the cascade index normalized matrix, and write the sub-index normalized matrix back to the register.

[0193] In step 1210, the cascade execution matrix is ​​a matrix obtained by cascading the sub-index matrices corresponding to the second periods before the current second period. For example, if the current second period is the fifth second period in the first period, the cascade index matrix is ​​a matrix obtained by cascading the sub-index matrices corresponding to the first four second periods in the first period. Figure 1 It can be seen that the exponential processing unit can access the accumulation buffer and the register through the thread warp, so as to obtain the cascaded exponential matrix and the sub-exponential matrix.

[0194] In step 1220, the cascaded index matrix and the sub-index matrix are cascaded and then normalized. The resulting cascaded index normalized matrix will include a sub-index normalized matrix corresponding to the sub-index matrix of the current second period, and index normalization results corresponding to each part of the cascaded index matrix. Thus, the sub-index normalized matrix corresponding to the current second period can be split out from the cascaded index normalized matrix. For example, the current second cycle is the fifth second cycle in the first cycle, and the sub-exponential matrix of each second cycle contains 128 matrix columns, and the cascaded exponential matrix is ​​a matrix containing 512 matrix columns. After cascading the sub-exponential matrix and the cascaded exponential matrix, a cascaded sub-exponential matrix with 640 matrix columns is obtained. After normalization, a cascaded exponential normalized matrix containing 640 matrix columns is obtained, where every 128 matrix columns correspond to a sub-exponential matrix of the second cycle, and the last 128 matrix columns are the sub-exponential normalized matrix corresponding to the current second cycle. The sub-exponential normalized matrix is ​​split out and written back to the register, so that the tensor processor can read the sub-exponential normalized matrix and the corresponding value matrix block from the register and perform matrix multiplication.

[0195] It can be understood that in step 1220, by cascading the sub-index matrix and the cascade index matrix and then performing index normalization, the error will be relatively smaller than performing local index normalization only according to the sub-index matrix corresponding to the current second period.

[0196] It can be understood that the exponential processing unit can be an arithmetic logic unit (ALU) inside the computing unit. In addition to exponentially processing the elements in the matrix, it can also be used to perform other arithmetic logic operations. For example, the normalization process is to calculate the ratio of the elements in the sub-exponential matrix to the sum of the elements in the matrix rows. Therefore, normalization can be performed by the exponential processing unit. Similarly, other arithmetic logic operations are also performed subsequently by the exponential unit.

[0197] In one embodiment, referring to Fig.13 , step 240 comprises:

[0198] Step 1310, through the tensor processor, in each second cycle of the first cycle, read the sub-index normalization matrix and the value matrix block from the register, and perform matrix multiplication operation on the sub-index normalization matrix and the value matrix block through the matrix multiplication unit to generate an attention operation result sub-block, and write the attention operation result sub-block to the accumulation buffer.

[0199] In step 1310, after writing the sub-index normalized matrix back to the register, the computing unit reads the corresponding sub-index normalized matrix and value matrix block from the register through the thread, and then writes them into the first buffer and the second buffer of the tensor processor. The tensor processor performs matrix multiplication operation on the sub-index normalized matrix in the first buffer and the value matrix block in the second buffer through the matrix multiplication unit, thereby obtaining the attention operation result sub-block corresponding to the current second cycle. It can be understood that, on the one hand, the sub-index normalized matrix used to calculate the attention operation result sub-block is the result of local index normalization, and it has certain errors that need to be corrected later. On the other hand, referring to Figure 4 It can be seen that the attention operation result sub-block only represents the result of the attention operation based on a single query matrix block, a single key matrix block and a single value matrix block. For example, the attention operation result sub-block of the first and second cycles only represents the matrix multiplication result of Block_S11 and Block_v1, and the attention weight result corresponding to a query matrix block is obtained by matrix multiplying the product of the query matrix block and the entire key matrix with the entire value matrix. For example, the attention weight result corresponding to the query matrix block Block_q1 requires Block_S11 to Block_S1m and Block_v1 To Block_vm for matrix multiplication, based on the properties of matrix operations, Block_S11 to Block_S1m and Block_v1 to Block_vm perform matrix multiplication, which is equivalent to accumulating the matrix multiplication result of Block_S11 and Block_v1 to Block_S1m and Block_vm, that is, accumulating the attention operation result sub-block corresponding to the first second cycle to the attention operation result sub-block corresponding to the last second cycle, thereby writing the attention operation result sub-block into the accumulation buffer, so as to be accumulated with the attention operation result sub-blocks of other second cycles.

[0200] In one embodiment, referring to the relevant description of step 1120, there is a certain error between the result obtained by performing local exponential normalization processing based on the sub-product matrix and the result of global exponential normalization. Based on this, in one embodiment, referring to Fig.14 In one embodiment, after step 1220, the attention operation processing method further includes:

[0201] Step 1410, splitting the cascade index normalization matrix into modified sub-index normalization matrices corresponding to each sub-index matrix in the cascade index matrix;

[0202] Step 1420, for each sub-index matrix, determine the first difference matrix based on the corresponding modified sub-index normalization matrix and the corresponding sub-index normalization matrix, and perform matrix multiplication on the first difference matrix and the corresponding value matrix block to obtain a modified attention operation result sub-block, and write the modified attention operation result sub-block into the accumulation buffer, and accumulate the modified attention operation result sub-block to the attention operation result sub-block corresponding to the sub-index matrix in the accumulation buffer.

[0203] In step 1410, the modified sub-index normalization matrix is ​​a normalization result obtained by cascading the corresponding sub-index matrix with the sub-index matrices of several subsequent second periods and then performing normalization processing. It can be understood that, before normalization of each second period, only the sub-index matrix corresponding to the second period and the previous second period can be cascaded and index-normalized, and it is impossible to cascade and normalize with the sub-index matrix of the second period after the second period, which requires that when the subsequent second period is normalized, the calculation results corresponding to the previous second period are constantly corrected. For example, when the Xth second period is index-normalized, only the sub-index matrices of the first X second periods can be cascaded and normalized. When the X+1th second period is index-normalized, after the sub-index matrices of the first X+1 second periods are cascaded and index-normalized, it is necessary to correct the calculation results of the first X second periods based on the cascaded index-normalized matrix corresponding to the X+1th second period.

[0204] Referring to the relevant description of step 1220, it can be known that the cascade index matrix is ​​obtained by cascading the sub-index matrices corresponding to other second periods before the current second period, and the cascade index normalization matrix includes the index normalization results corresponding to each part in the cascade index matrix, that is, the part corresponding to the sub-sub-index matrix corresponding to other second periods before the current second period, so that the cascade index normalization matrix is ​​divided by column to obtain the corrected sub-index normalization matrix corresponding to each sub-index matrix. Specifically, for example, the current second period is the fifth second period in the first period, and the sub-index matrix corresponding to each second period includes 128 matrix columns, then the cascade index normalization matrix includes 640 matrix columns, among which the first 128 matrix columns constitute the corrected sub-index normalization matrix corresponding to the sub-index matrix of the first second period, and the 129th to 256th matrix columns constitute the corrected sub-index normalization matrix corresponding to the sub-index matrix of the second second period, and so on.

[0205] In step 1420, since the corresponding attention operation result sub-block is calculated based on the sub-index normalization matrix of the current second period and the corresponding value matrix block in each second period, and this attention operation result sub-block is determined based on the result of the local attention calculation, there is actually a certain error between the attention operation result sub-block of the second period and the accurate attention operation result sub-block. Based on this, in the present disclosure, for each second period, after determining the corrected sub-index normalization matrix, the corresponding first difference matrix is ​​determined based on the corrected sub-index normalization matrix and the corresponding sub-index normalization matrix. Specifically, the corrected sub-index normalization matrix and the corresponding sub-index normalization matrix are subtracted to obtain the corresponding first difference matrix, which represents the difference between the index normalization result obtained by cascading the sub-index matrices of the previous X second periods and the index normalization result obtained by cascading the sub-index matrices of the previous X+1 second periods. Based on the distributive law of matrix multiplication, it can be known that the modified attention operation result sub-block obtained by performing matrix multiplication operation on the first difference matrix and the corresponding matrix block represents the difference between the attention result obtained when the sub-exponential matrices of the first X second periods are cascaded and the attention result obtained when the sub-exponential matrices of the first X+1 second periods are cascaded. The modified attention operation result sub-block is written into the accumulation buffer and accumulated with the corresponding attention operation result sub-block, so that the attention operation result sub-block can be corrected to the attention operation result when the sub-exponential matrices of the first X+1 second periods are cascaded and normalized. When corrected to the last second period in the first period, an accurate attention operation result can be obtained.

[0206] In one embodiment, referring to Fig.15 In one embodiment, step 250 includes:

[0207] Step 1510, in the accumulation buffer, accumulating the attention operation result sub-blocks corresponding to each second cycle in the first cycle to obtain the attention result sub-matrix corresponding to the first cycle;

[0208] Step 1520, concatenate the attention result sub-matrices corresponding to each first period to obtain an attention operation result matrix.

[0209] In step 1510, in the first cycle, each second cycle will calculate a corresponding attention operation result sub-block, and after obtaining the attention operation result sub-blocks corresponding to each second cycle, it indicates that the attention operation result sub-block corresponding to the last second cycle in the first cycle has been calculated, and when calculating the attention operation result sub-block of the last second cycle, the query matrix block and all key matrix blocks are matrix multiplied, and all sub-index matrices are cascaded and global index normalization is performed to obtain an accurate index normalization result, and the attention operation result sub-block corresponding to each second cycle is corrected based on the index normalization result, and the accurate attention operation result sub-block of each second cycle is obtained. Referring to the relevant description of the above step 1310, it can be known that after accumulating the attention operation result sub-blocks corresponding to each second cycle in the first cycle, the attention result sub-matrix corresponding to the first cycle can be obtained, that is, the attention result sub-matrix corresponding to the query matrix block placed in the register in the first cycle.

[0210] In step 1520, after obtaining the attention result sub-matrix corresponding to each query matrix block, the attention result sub-matrices corresponding to each query matrix block are concatenated along the dimension of the matrix row to obtain a complete attention operation result matrix.

[0211] The parallel index processing implementation refers to an implementation in which the index processing of each matrix row of the sub-product matrix is ​​performed in parallel by a plurality of thread warps in a computing unit.

[0212] In one embodiment, the computing unit further includes a plurality of thread warps, each thread warp corresponding to a second number of matrix rows in the sub-product matrix, referring to Fig.16 , step 230, comprising:

[0213] Step 1610, executing a plurality of thread warps in parallel, wherein each thread warp performs index processing on a second number of matrix rows corresponding to the thread warp through an index processing unit to obtain a second number of sub-index matrix rows corresponding to the thread warp;

[0214] Step 1620: merge the sub-index matrix rows corresponding to the multiple warps into a sub-index matrix, and write the sub-index matrix back to the register.

[0215] In step 1610, each thread warp can independently call the index processing unit, and multiple thread warps can be executed in parallel. The sub-product matrix includes multiple matrix rows. When performing index processing, each matrix row is actually indexed. After the elements in the matrix are converted into index form, the index processing of each matrix row in the sub-product matrix is ​​actually independent of each other and does not interfere with each other. Therefore, each matrix row can be indexed in parallel by multiple thread warps, thereby improving the efficiency of index processing.

[0216] In step 1620, after exponential processing is performed on each matrix row in parallel by multiple thread warps, a second number of sub-index matrix rows corresponding to each thread warp are obtained, each sub-index matrix row corresponds to a matrix row in the sub-product matrix, and after merging the sub-index matrix rows, a sub-index matrix corresponding to the sub-product matrix is ​​obtained, and the sub-index matrix is ​​written back to the register so that the tensor processor can read the sub-index matrix from the register.

[0217] In the embodiment disclosed in steps 1610 to 1620, each matrix row in the sub-product matrix is ​​indexed in parallel by multiple thread warps, thereby improving the efficiency of the indexing of the sub-product matrix, thereby improving the efficiency of the attention operation.

[0218] In one embodiment, exponential processing is performed on the sub-product matrix, including:

[0219] By means of an exponential processing unit, each element of a second number of matrix rows corresponding to the thread warp is used as an exponent of a preset exponent to obtain a power corresponding to the element, thereby obtaining a sub-exponential matrix;

[0220] The cascade index matrix and the sub-index matrix are cascaded and normalized, including:

[0221] Cascade the sub-index matrix and the cascade index matrix to obtain a cascaded matrix;

[0222] For each matrix row of the concatenated matrix, calculate the sum of the powers corresponding to each element of the matrix row;

[0223] Based on the ratio of the power corresponding to each element to the sum of the corresponding matrix row, a second number of normalized matrix rows corresponding to the warp is determined.

[0224] The preset exponent can be the base of the natural logarithm, e. It is understandable that the attention mechanism is applied in the neural network model, and the neural network model often optimizes the model parameters by the gradient descent method. When calculating the gradient, the derivative needs to be calculated. The original form of each element in the sub-product matrix is ​​not conducive to the calculation of the derivative, that is, it is not conducive to the calculation of the gradient. Based on this, when performing the normalization operation, each element in the product matrix is ​​first used as the exponent of the preset exponent e to obtain the power corresponding to each element. ,in Represents the i-th element of the matrix row. Since the exponential derivative of e is still itself, the gradient can be easily calculated when optimizing the parameters of the neural network model that includes the attention mechanism.

[0225] After obtaining the power corresponding to each element, for each matrix row, the powers corresponding to all elements in the matrix row are added together to obtain the sum corresponding to each matrix row.

[0226] Among them, based on the ratio of the power corresponding to each element and the sum of the corresponding matrix row, the attention weight corresponding to each element in the matrix row can be determined, thereby making the attention weight of each element in a matrix row between 0 and 1, and the sum of the attention weights of all elements in a matrix row is 1, and a normalized matrix row corresponding to the thread warp is obtained. For the second number of matrix rows corresponding to the thread warp, the ratio of the power of the elements of each matrix row and the sum corresponding to the matrix row is calculated in parallel, and the second number of normalized matrix rows corresponding to the thread warp can be obtained.

[0227] Specifically, each matrix row after the sub-index matrix and the cascade index matrix are cascaded can be normalized by the following formula: ; where n represents the number of elements in a matrix row, represents the i-th element in the matrix row, represents the jth element in a matrix row, Represents the sum of the powers of all elements in the corresponding matrix row.

[0228] In the above disclosed embodiments, the elements in the matrix row corresponding to the thread warp are converted into the corresponding power form so that the gradient can be conveniently calculated in the subsequent optimization model. Then, for each matrix row corresponding to the thread warp, the sum of the powers of all elements in the matrix row is calculated in parallel to obtain the sum corresponding to each matrix row. Then, based on the ratio of the power corresponding to each element and the sum corresponding to the matrix row where the element is located, the attention weight corresponding to each element is determined, thereby obtaining the second number of normalized matrix rows corresponding to the thread warp.

[0229] In one embodiment, the computing unit includes a plurality of execution units, each execution unit includes a register, each execution unit includes at least one thread warp, and Fig.17 , in step 220, writing the sub-product matrix back to the register includes:

[0230] Step 1710, determining the total number of enabled warps based on a ratio of the number of matrix rows to the second number;

[0231] Step 1720, determine the number of idle thread warps of each execution unit;

[0232] Step 1730, based on the ratio of the number of idle warps of each execution unit, the total number of enabled warps is distributed among the execution units to obtain the number of enabled warps corresponding to each execution unit;

[0233] Step 1740 , based on the product of the number of enabled warps corresponding to each execution unit and the second number, store each matrix row in a register of each execution unit respectively;

[0234] In step 1710, since each warp can execute the first number of matrix rows in parallel, and the sub-product matrix includes multiple matrix rows, based on the ratio of the number of matrix rows of the sub-product matrix to the second number, the total number of enabled warps is determined based on the ratio, and then the warps are enabled based on the total number of enabled warps, so that the enabled warps can simultaneously perform index processing on all matrix rows of the sub-product matrix. It should be noted that when the ratio of the number of matrix rows to the second number is not an integer, the result of rounding up the ratio is determined as the total number of enabled warps.

[0235] In step 1720, the attention operation processing device may need to execute other tasks at the same time in addition to executing the current attention operation processing task, which may cause a part of the thread bundles in the execution unit to be used by other tasks, and the number of idle thread bundles that can be used to execute the index processing task of the matrix row in each execution unit is different. Specifically, the use status of the thread bundles in each execution unit can be determined by polling the thread bundles in each execution unit. If the thread bundles are not used, it is determined that the thread bundles are idle thread bundles, thereby determining the number of idle thread bundles in each execution unit.

[0236] In step 1730, if the index processing tasks of multiple matrix rows of the sub-product matrix are evenly distributed to the thread bundles of several execution units, for the execution unit with fewer idle thread bundles, the idle thread bundles in the execution unit may need to perform more index processing of matrix rows, which results in that the multiple matrix rows allocated to the execution unit cannot be concurrently executed by the thread bundles at the same time. On the contrary, when the number of idle thread bundles of an execution unit is large, the matrix rows allocated to the execution unit may not be able to fully utilize the processing capacity of all idle thread bundles of the execution unit. For example, there is only one idle thread bundle in execution unit A, and there are 5 idle thread bundles in execution unit B. One idle thread bundle can concurrently perform index processing of 4 matrix rows. At this time, each execution unit is allocated 12 matrix rows. Then, since there is only one idle thread bundle in execution unit A, it is necessary to divide the 12 matrix rows into three batches for index processing in sequence, while in execution unit B, only 3 thread bundles are needed to concurrently perform index processing on the 12 matrix rows, and there are still two idle thread bundles that are not used, which results in a reduced efficiency of index processing on the sub-product matrix. Based on this, in this embodiment, the total number of enabled thread warps is allocated to each execution unit based on the ratio of the number of idle thread warps of each execution unit, so as to determine the number of enabled thread warps of each execution unit. For example, the total number of enabled thread warps is 10, the computing unit has 4 execution units, and the number of idle thread warps of each execution unit is 8, 4, 2, and 6 respectively. After the 10 enabled thread warps are allocated to each execution unit according to the ratio of the idle thread warps, the number of enabled thread warps of each execution unit is 4, 2, 1, and 3 respectively.

[0237] In step 1740, after determining the number of enabled thread warps of each execution unit, since the registers exist inside the execution unit, each thread warp can access the registers inside the corresponding execution unit. At this time, it is necessary to allocate each matrix row of the sub-product matrix to the registers of each execution unit for storage, so that the enabled thread warps of each execution unit can read the corresponding matrix row from the register of the execution unit and perform index processing through the arithmetic logic unit. Specifically, since each thread warp can concurrently perform index processing of the second number of matrix rows at the same time, based on the product of the number of enabled thread warps of each execution unit and the second number, the number of matrix rows required to be stored in the register of the execution unit can be determined. For example, the number of enabled thread warps of an execution unit is 2, and a thread warp can concurrently perform index processing of 4 matrix rows at the same time. At this time, 8 matrix rows in the sub-product matrix are stored in the register of the execution unit. Therefore, each thread warp enabled in the subsequent execution unit only needs to read the matrix row from the corresponding execution unit to perform index processing without accessing the global memory.

[0238] In the case where each matrix row is stored in a register of each execution unit based on the product of the number of enabled warps corresponding to each execution unit and the second number, referring to Fig.17 , step 1610, comprising:

[0239] Step 1750, in each execution unit, enable the corresponding number of enabled thread warps in parallel, so that each thread warp takes out the corresponding second number of matrix rows from the register of the execution unit corresponding to the thread warp through the index processing unit, and performs index processing on each matrix row to obtain the sub-index matrix row corresponding to the thread warp.

[0240] In step 1750, based on the number of enabled thread warps corresponding to each execution unit, thread warps are enabled in the execution unit. At this time, since each matrix row of the sub-product matrix has been allocated to the register of the execution unit for storage, each thread warp reads the corresponding second number of matrix rows in the register of the corresponding execution unit through the index processing unit, and the index processing of each matrix row in the sub-product matrix is ​​completed inside the execution unit, and the efficiency of index processing for the sub-product matrix is ​​higher. At the same time, since the number of enabled thread warps of each execution unit is determined based on the ratio of the number of idle thread warps of the execution unit and the total number of enabled thread warps, the execution unit with more idle thread warps needs to enable more thread warps to perform index processing on more matrix rows, while the execution unit with fewer idle thread warps only needs to perform index processing on a few matrix rows through a small number of thread warps, so that the load of each execution unit is relatively balanced, and the thread warps of multiple execution units can relatively synchronously and concurrently complete the index processing of the corresponding matrix rows, thereby improving the efficiency of index processing. In the embodiment disclosed in steps 1710 to 1750, the total number of enabled thread warps is allocated to each execution unit based on the ratio of the number of idle thread warps of each execution unit, and then the matrix rows of the sub-product matrix are allocated to the registers of each execution unit based on the product of the number of enabled thread warps of each execution unit and the second number. After that, each execution unit enables thread warps based on the corresponding number of enabled thread warps, thereby achieving load balancing of the idle thread warps in each execution unit, so that the thread warps of multiple execution units can complete the indexation processing and normalization processing relatively synchronously, thereby improving the utilization rate of the thread warps, thereby improving the efficiency of the indexation processing; at the same time, each enabled thread warp reads the corresponding second number of matrix rows in the register inside the corresponding execution unit through the indexation processing unit and performs indexation and normalization processing. The entire normalization and indexation processing process is completed inside the execution unit without accessing the memory or other units outside the execution unit, thereby improving the efficiency of the thread warp reading the matrix rows, thereby improving the efficiency of the indexation processing, and reducing the memory access overhead.

[0241] It is to be understood that, although the steps in the above-mentioned flowcharts are sequentially displayed according to the characterization of arrows, these steps are not necessarily executed in sequence according to the order of arrow characterization. Unless there is a clear description in the present embodiment, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the above-mentioned flowcharts can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a portion of the steps or stages in other steps.

[0242] Reference Fig.18 The present disclosure also proposes an attention operation processing device 1800, which includes: a tensor processor 1810 and a computing unit 1820, wherein the computing unit 1820 includes a register 1821 and an index processing unit 1822, wherein the register 1821 is used to store a query matrix block, a key matrix block and a value matrix block for a target attention operation; the tensor processor 1810 is used to read the query matrix block and the key matrix block from the register 1821, and perform a matrix multiplication operation based on the query matrix block and the key matrix block to obtain a sub-product matrix, and write the sub-product matrix back to the register; the index processing unit 1822 is used to index the sub-product matrix in the register 1821 to obtain a sub-index matrix, and write the sub-index matrix back to the register 1821; the tensor processor 1810 is also used to read the sub-index matrix and the value matrix block from the register 1821, and perform a matrix multiplication operation based on the sub-index matrix and the value matrix block to obtain an attention operation result matrix.

[0243] The attention operation processing device disclosed in the present invention is used to execute the attention operation processing method as in the above-mentioned embodiment. Its specific processing process is the same as the attention operation processing method in the above-mentioned embodiment, and will not be repeated here.

[0244] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present disclosure described herein can, for example, be implemented in an order other than those illustrated or described herein. In addition, the terms "comprises" and "comprising" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0245] It should be understood that in the present disclosure, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.

[0246] It should be understood that in the description of the embodiments of the present disclosure, the meaning of multiple (or multiple items) is more than two, greater than, less than, exceed, etc. are understood as not including the number, and above, below, within, etc. are understood as including the number.

[0247] In the several embodiments provided in the present disclosure, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0248] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0249] In addition, each functional unit in each embodiment of the present disclosure may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0250] It should also be understood that the various implementations provided in the embodiments of the present disclosure can be combined arbitrarily to achieve different technical effects.

[0251] The above is a specific description of the implementation methods of the present disclosure, but the present disclosure is not limited to the above implementation methods. Technical personnel familiar with the art can also make various equivalent modifications or substitutions without violating the spirit of the present disclosure. These equivalent modifications or substitutions are all included in the scope defined by the claims of the present disclosure.

Claims

1. A method for processing attention calculation, characterized in that: Used in an attention operation processing device, the attention operation processing device includes a tensor processor and a computing unit, the computing unit includes a register and an index processing unit, and the attention operation processing method includes: Dividing the query matrix, key matrix and value matrix used for the target attention operation into a query matrix block, a key matrix block and a value matrix block respectively, and dispatching them into the register; Reading the query matrix block and the key matrix block from the register through the tensor processor, performing a matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix, and writing the sub-product matrix back to the register; The index processing unit performs index processing on the sub-product matrix in the register to obtain a sub-index matrix, and writes the sub-index matrix back to the register index processing unit; Reading the sub-index matrix and the value matrix block corresponding to the sub-index matrix from the register through the tensor processor, and performing a matrix multiplication operation based on the sub-index matrix and the value matrix block to generate an attention operation result sub-block; Based on each of the attention operation result sub-blocks, an attention operation result matrix is ​​obtained.

2. The attention calculation processing method according to claim 1, characterized in that: The step of dividing the query matrix, the key matrix and the value matrix for the target attention operation into a query matrix block, a key matrix block and a value matrix block respectively, and dispatching them to the register comprises: The query matrix, key matrix and value matrix used for target attention operation are divided into query matrix block, key matrix block and value matrix block respectively according to the same partitioning rule; Put the divided multiple query matrix blocks into the register in sequence according to the first cycle, wherein each first cycle includes a first number of second cycles, and the first number is the number of key matrix blocks that need to be multiplied with one query matrix block; In each second cycle of the first cycle, each key matrix block to be multiplied with the placed query matrix block is sequentially placed in the register, and each value matrix block corresponding to the sub-index matrix is ​​sequentially placed in the register.

3. The attention calculation processing method according to claim 2, characterized in that: The computing unit is a plurality of computing units, and the plurality of query matrix blocks divided into blocks are sequentially placed into the register according to the first cycle, including: In each of the first cycles, a plurality of the query matrix blocks are respectively placed in registers of a plurality of the computing units, wherein the register of each computing unit is placed in one query matrix block, and the same query matrix block is only placed in the register of one computing unit in one of the first cycles; In each second cycle of the first cycle, each key matrix block to be multiplied with the query matrix block is sequentially placed in the register, and each value matrix block corresponding to the sub-index matrix is ​​sequentially placed in the register, including: In each of the second cycles of the first cycle, each of the key matrix blocks and the value matrix blocks corresponding to the key matrix blocks are sequentially placed in the register of each of the computing units.

4. The attention calculation processing method according to claim 2, characterized in that: The tensor processor includes a matrix multiplication operation unit and an accumulation buffer; The step of reading the query matrix block and the key matrix block from the register by the tensor processor, performing a matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix, and writing the sub-product matrix back to the register comprises: By means of the matrix multiplication operation unit, in each second cycle of the first cycle, the query matrix block and the key matrix block are read from the register, and a matrix multiplication operation is performed on the query matrix block and the key matrix block to obtain the sub-product matrix, and the sub-product is written back to the register; After the exponential processing unit performs exponential processing on the sub-product matrix in the register to obtain a sub-exponential matrix, the method further includes: In the first of the second cycles of the first cycle, summing each of the matrix rows of the sub-index matrix to obtain a first sum of each matrix row, determining a normalization coefficient matrix based on each of the first sums, and writing the normalization coefficient matrix into a register; In each second period of the first period, each matrix row of the corresponding sub-exponential matrix is ​​summed to obtain a first sum of each matrix row, and the first sum of each matrix row is respectively added to the corresponding matrix row in the normalization coefficient matrix to update the normalization coefficient matrix, wherein the sub-exponential matrix is ​​obtained by taking each element of the corresponding sub-product matrix as the exponent of a preset base.

5. The attention calculation processing method according to claim 4, characterized in that: The attention operation result matrix is ​​obtained based on each of the attention operation result sub-blocks, including: In each of the first cycles, the accumulation buffer is initialized to obtain an initialized attention intermediate result sub-matrix; In each second cycle of the first cycle, accumulating the attention operation result sub-block corresponding to the current second cycle to the attention intermediate result sub-matrix in the accumulation buffer, wherein the attention operation result sub-block is obtained by multiplying the sub-index matrix corresponding to the current second cycle and the value matrix block; After accumulating the attention operation result sub-block corresponding to the last second cycle of the first cycle to the attention intermediate result sub-matrix, correcting the attention intermediate result sub-matrix based on the normalization coefficient matrix to obtain the attention result sub-matrix corresponding to the first cycle; The attention result sub-matrices corresponding to each of the first cycles are concatenated to obtain the attention operation result matrix.

6. The attention calculation processing method according to claim 5, characterized in that: The register stores a row maximum value vector, each element in the row maximum value vector is used as the row maximum value corresponding to each matrix row in the sub-product matrix. Before the index processing unit performs index processing on the sub-product matrix in the register to obtain a sub-index matrix, the method further includes: Initializing the row maximum value vector in each of the first cycles; In the Mth second period of the first period, determining the maximum value of each matrix row in the corresponding sub-product matrix, and when the maximum value of the element is greater than the row maximum value of the corresponding matrix row in the row maximum value vector, updating the corresponding row maximum value based on the maximum value of the element, to obtain the row maximum value corresponding to the Mth second period, wherein 1≤M≤N, and N is the first number; The exponential processing unit performs exponential processing on the sub-product matrix in the register to obtain a sub-exponential matrix, including: In the Mth second cycle of the first cycle, the row maximum value of each matrix row is read from the register through the exponential processing unit, and the first differences corresponding to each element of the sub-product matrix are determined based on each element of the corresponding sub-product matrix and the corresponding row maximum value, and each first difference is used as the exponent of the preset base to obtain a sub-exponential matrix corresponding to the sub-product matrix.

7. The attention calculation processing method according to claim 6, characterized in that: The step of accumulating the attention operation result sub-block corresponding to the current second cycle to the attention intermediate result sub-matrix in the accumulation buffer includes: In the M-th second period, a correction coefficient is determined based on the row maximum value corresponding to the M-th second period and the row maximum value corresponding to the M-1-th second period, the attention intermediate result sub-matrix of the accumulation buffer is corrected based on the correction coefficient, and the attention operation result sub-block corresponding to the M-th second period is accumulated to the corrected attention intermediate result sub-matrix; The step of accumulating the first sums of the respective matrix rows to the corresponding matrix rows in the normalization coefficient matrix comprises: In the Mth second period in the first period, the normalized coefficient matrix corresponding to the M-1th second period is corrected based on the correction coefficient, and the first sum of each matrix row of the sub-index matrix corresponding to the Mth second period is added to the corresponding matrix row in the corrected normalized coefficient matrix to obtain the normalized coefficient matrix corresponding to the Mth second period.

8. The attention calculation processing method according to claim 5, characterized in that: The computing unit further includes a plurality of thread warps, each of the thread warps corresponding to a second number of matrix rows in the attention intermediate result sub-matrix, and the modifying the attention intermediate result sub-matrix based on the normalization coefficient matrix includes: Executing a plurality of the thread warps in parallel, wherein each of the thread warps obtains a second number of normalization coefficients corresponding to the second number of matrix rows from the normalization coefficient matrix through the indexation processing unit, and respectively corrects the second number of matrix rows based on the second number of normalization coefficients to obtain a second number of normalized attention result matrix rows, wherein each of the matrix rows corresponds to a normalization coefficient; The normalized attention result matrix rows corresponding to the multiple thread warps are concatenated and merged to obtain an attention result sub-matrix corresponding to the first cycle.

9. The attention calculation processing method according to claim 2, characterized in that: The tensor processor includes a matrix multiplication operation unit and an accumulation buffer; The step of reading the query matrix block and the key matrix block from the register by the tensor processor, performing a matrix multiplication operation on the query matrix block and the key matrix block to obtain a sub-product matrix, and writing the sub-product matrix back to the register comprises: By means of the matrix multiplication operation unit, in each second cycle of the first cycle, the query matrix block and the key matrix block are read from the register, and a matrix multiplication operation is performed on the query matrix block and the key matrix block to obtain the sub-product matrix, and the sub-product is written back to the register; After the exponential processing unit performs exponential processing on the sub-product matrix in the register to obtain a sub-exponential matrix, the method further includes: The sub-index matrix is ​​written back to the accumulation buffer, and the sub-index matrices in the accumulation buffer are cascaded to obtain a cascaded index matrix.

10. The attention calculation processing method according to claim 9, characterized in that: After the index processing unit performs index processing on the sub-product matrix in the register to obtain a sub-index matrix, the method further comprises: Reading the sub-index matrix corresponding to the current second cycle from the register through the index processing unit, and acquiring the cascade index matrix from the accumulation buffer; The cascade index matrix and the sub-index matrix are cascaded and normalized to obtain a cascade index normalized matrix, and a sub-index normalized matrix corresponding to the sub-index matrix is ​​separated from the cascade index normalized matrix, and the sub-index normalized matrix is ​​written back to the register.

11. The attention calculation processing method according to claim 10, characterized in that: The performing a matrix multiplication operation based on the sub-index matrix and the value matrix block to generate an attention operation result sub-block includes: Through the tensor processor, in each of the second cycles of the first cycle, the sub-exponent normalized matrix and the value matrix block are read from the register, and the matrix multiplication unit performs matrix multiplication operation on the sub-exponent normalized matrix and the value matrix block to generate an attention operation result sub-block, and the attention operation result sub-block is written to the accumulation buffer.

12. The attention calculation processing method according to claim 10, characterized in that: After the cascade index matrix and the sub-index matrix are cascaded and normalized to obtain a cascade index normalized matrix, the method further includes: Splitting out modified sub-index normalized matrices corresponding to each of the sub-index matrices in the cascade index normalized matrix from the cascade index normalized matrix; For each sub-index matrix, a first difference matrix is ​​determined based on the corresponding corrected sub-index normalization matrix and the corresponding sub-index normalization matrix, and the first difference matrix is ​​matrix multiplied with the corresponding value matrix block to obtain a corrected attention operation result sub-block, and the corrected attention operation result sub-block is written into the accumulation buffer, and the corrected attention operation result sub-block is accumulated to the attention operation result sub-block corresponding to the sub-index matrix in the accumulation buffer.

13. The attention calculation processing method according to claim 9, characterized in that: The attention operation result matrix is ​​obtained based on each of the attention operation result sub-blocks, including: In the accumulation buffer, the attention operation result sub-blocks corresponding to each of the second cycles in the first cycle are accumulated to obtain an attention result sub-matrix corresponding to the first cycle; The attention result sub-matrices corresponding to each of the first cycles are concatenated to obtain the attention operation result matrix.

14. The attention calculation processing method according to claim 1, characterized in that: The computing unit further includes a plurality of warps, each of the warps corresponding to a second number of matrix rows in the sub-product matrix; The step of performing exponential processing on the sub-product matrix in the register by the exponential processing unit to obtain a sub-exponential matrix, and writing the sub-exponential matrix back to the register includes: executing the plurality of thread warps in parallel, wherein each of the thread warps performs index processing on a second number of matrix rows corresponding to the thread warp through the index processing unit to obtain a second number of sub-index matrix rows corresponding to the thread warp; The sub-index matrix rows corresponding to the plurality of warps are merged into the sub-index matrix, and the sub-index matrix is ​​written back to the register.

15. The attention calculation processing method according to claim 14, characterized in that: The computing unit includes a plurality of execution units, each of the execution units includes a register, and each of the execution units includes at least one thread warp; Writing the sub-product matrix back to the register comprises: determining a total number of enabled warps based on a ratio of the number of matrix rows to the second number; Determining the number of idle thread warps of each of the execution units; Based on the ratio of the number of idle warps of each of the execution units, the total number of enabled warps is distributed among the execution units to obtain the number of enabled warps corresponding to each of the execution units; Based on the product of the number of enabled warps corresponding to each execution unit and the second number, storing each of the matrix rows in the sub-product matrix into a register of each execution unit respectively; The executing the plurality of thread warps in parallel comprises: In each of the execution units, thread warps corresponding to the number of enabled thread warps are enabled in parallel, so that each of the thread warps passes through the indexation processing unit, takes out the corresponding second number of matrix rows from the register of the execution unit corresponding to the thread warp, and performs indexation processing on each of the matrix rows to obtain the sub-index matrix rows corresponding to the thread warp.

16. An attention operation processing device, characterized in that: It includes a tensor processor and a computing unit, wherein the computing unit includes a register and an index processing unit, wherein: The register is used to store a query matrix block, a key matrix block and a value matrix block for target attention operation; The tensor processor is used to read the query matrix block and the key matrix block from the register, perform a matrix multiplication operation based on the query matrix block and the key matrix block to obtain a sub-product matrix, and write the sub-product matrix back to the register; The exponential processing unit is used to perform exponential processing on the sub-product matrix in the register to obtain a sub-exponential matrix, and write the sub-exponential matrix back to the register; The tensor processor is also used to read the sub-index matrix and the value matrix block from the register, and perform matrix multiplication operations based on the sub-index matrix and the value matrix block to obtain an attention operation result sub-block, and determine the attention operation result matrix based on the attention operation result sub-block.