Data processing method, processor and computer device
The method addresses the computational inefficiencies of the self-attention mechanism by filtering and normalizing matrices to achieve efficient and accurate self-attention processing, reducing latency in transformer models.
Patent Information
- Application Number
- PCT/CN2025/109751
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2024-07-29
- Filing Date
- 2025-07-22
- Publication Date
- 2026-02-05
AI Technical Summary
The self-attention mechanism in artificial intelligence processing requires significant computational resources and leads to high processing delays, particularly in transformer models, with a substantial portion of the latency attributed to the self-attention mechanism.
A data processing method that involves determining a first similarity matrix, filtering target elements based on conditions, normalizing the matrix, and calculating a self-attention matrix to reduce computational load and delay, using a processor with modules for determining, selecting, and normalizing matrices to achieve sparse calculation.
This approach simplifies the calculation process, reduces effective computation, and decreases processing delay while maintaining accuracy by ensuring the attention weight matrix satisfies normalized distribution.
Smart Images

Figure CN2025109751_05022026_PF_FP_ABST
Abstract
Description
DATA PROCESSING METHOD, PROCESSOR AND COMPUTER DEVICECROSS-REFERENCES TO RELATED APPLICATION
[0001] This application claims priority to Chinese Patent Application No. 202411028350.9 filed on July 29, 2024, the entire content of which is incorporated herein by reference. FIELD OF TECHNOLOGY
[0002] The present disclosure relates to, but is not limited to, the field of artificial intelligence technology and, more specifically, to a data processing method, a processor and a computer device.BACKGROUND
[0003] The self-attention mechanism is an important technology in the field of artificial intelligence. It allows the model to consider the relationship between each element and all other elements in the sequence when processing a sequence. This mechanism helps the model to better understand the contextual information in the sequence by calculating the correlation (or weight) between each element and other elements in the sequence, thereby processing sequence data more accurately. However, the processing flow of the self-attention mechanism in the related art requires a large amount of computation and has high processing delay.SUMMARY
[0004] One aspect of this disclosure provides a data processing method. The data processing method includes determining a first similarity matrix based on a query matrix and a key matrix corresponding to a word vector sequence; filtering out a target element whose value meets a condition from each row of the first similarity matrix, and setting all elements other than the target element in each row to zero to obtain a second similarity matrix; normalizing the second similarity matrix to obtain an attention weight matrix; and determining a self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and a value matrix corresponding o the word vector sequence.
[0005] Another aspect of this disclosure provides a processor. The processor includes a first determination module, a selection module, a normalization module and a second determination module. The first determination module is configured to determine a first similarity matrix based on a query matrix and a key matrix corresponding to a word vector sequence. The selection module is configured to select a target element whose value meets a condition from each row of the first similarity matrix, and set other elements in each row other than the target element to zero to obtain a second similarity matrix. The normalization module is configured to normalize the second similarity matrix to obtain an attention weight matrix. The second determination module is configured to determine a self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and a value matrix corresponding to the word vector sequence.
[0006] Another aspect of this disclosure provides a computer device. The computer device includes one or more processors and one or more memories coupled to the one or more processors and storing a plurality of computer instructions that, when being executed, cause the one or more processors to determine a first similarity matrix based on a query matrix and a key matrix corresponding to a word vector sequence; filter out a target element whose value meets a condition from each row of the first similarity matrix, and set all elements other than the target element in each row to zero to obtain a second similarity matrix; normalize the second similarity matrix to obtain an attention weight matrix; and determine a self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and a value matrix corresponding o the word vector sequence.BRIEF DESCRIPTION OF THE DRAWINGS
[0007] In order to provide a clearer illustration of various embodiments of the present disclosure or technical solutions in conventional technology, the drawings used in the description of the disclosed embodiments or the conventional technology are briefly described below. The following drawings are merely embodiments of the present disclosure. Other drawings may be obtained based on the disclosed drawings by those skilled in the art without creative efforts.
[0008] FIG. 1 is a flowchart of a data processing method according to some embodiments of the present disclosure.
[0009] FIG. 2 is a schematic diagram of a composition structure of a processor according to some embodiments of the present disclosure.
[0010] FIG. 3 is a schematic diagram of a composition structure of a selection module according to some embodiments of the present disclosure.
[0011] FIG. 4 is a schematic diagram of a composition structure of the selection module according to some embodiments of the present disclosure.
[0012] FIG. 5 is a schematic diagram of a composition structure of the selection module according to some embodiments of the present disclosure.
[0013] FIG. 6 is a schematic diagram of a composition structure of the selection module according to some embodiments of the present disclosure.
[0014] FIG. 7 is a schematic diagram of a calculation flow of a self-attention mechanism in the related art.
[0015] FIG. 8 is a schematic diagram of a calculation flow of the self-attention mechanism in the related art.
[0016] FIG. 9 is a schematic diagram of a calculation flow of the self-attention mechanism according to some embodiments of the present disclosure.
[0017] FIG. 10 is a schematic diagram of a hardware architecture of a Top_K sel module according to some embodiments of the present disclosure.
[0018] FIG. 11 is a schematic diagram of an implementation flow of the Top_K element selection method according to some embodiments of the present disclosure.
[0019] FIG. 12 is a schematic diagram showing a hardware entity of a computer device according to some embodiments of the present disclosure.DETAILED DESCRIPTION
[0020] To make the purposes, technical solutions, and advantages of the present disclosure clearer, the present disclosure is described in detail in connection with the accompanying drawings. The described embodiments should not be considered to limit the scope of the present disclosure. All other embodiments obtained by those skilled in the art without creative efforts are within the scope of the present disclosure.
[0021] In the following description, the term “some embodiments” describes a subset of all possible embodiments. “Some embodiments” can be a same subset or different subsets of all possible embodiments and can be combined with each other when there is no conflict.
[0022] In the following description, the terms “first, ” “second, ” and “third” are merely used to distinguish similar objects and do not represent a specific order. The terms “first, ” “second, ” and “third” can be interchanged in a specific sequence or order. Thus, embodiments of the present disclosure can be implemented in an order other than the order shown or described here.
[0023] Unless otherwise defined, all technical and scientific terms of the present disclosure have the same meaning as commonly understood by those skilled in the art. The terms used in the present disclosure are merely for the purpose of describing embodiments of the present disclosure and are not intended to limit the present disclosure. In addition, the technical features described in the different embodiments and implementations of the present disclosure described below can be combined with each other as long as they do not conflict with each other.
[0024] To better understand the technical solutions of the embodiments of the present disclosure, the self-attention mechanism in the related art will be described first.
[0025] In the self-attention mechanism, the query matrix (denoted as Q matrix) , the key matrix (denoted as K matrix) and the value matrix (denoted as V matrix) are three key components, which represent the representation of the input sequence in different dimensions.
[0026] The Q matrix is the query information extracted from the input sequence. Each row in the Q matrix represents a word / word vector (i.e., a token) in the input sequence, which is used to evaluate the correlation between the currently processed word / token and other words / tokens.
[0027] The K matrix is the key information extracted from the input sequence. The K matrix is the label or description of all words / tokens in the input sequence, similar to what is used to match when searching for related words / tokens. In the attention mechanism, the relationship between the Q matrix and the K matrix can be used to determine the correlation between different words / tokens.
[0028] The V matrix is the value information extracted from the input sequence. Each row in the V matrix is a value vector, which is also used to represent a word / token in the input sequence, generally learned through a neural network. After using the Q matrix and K matrix to evaluate the correlation between different words / tokens, these correlations and the value vector of the current word can be used to calculate the final representation of the current word. The value vector can be weighted to represent the meaning or importance of the current word.
[0029] In the self-attention mechanism, the K matrix, Q matrix, and V matrix can all be generated by different linear transformations of the input sequence. For example, the K matrix, Q matrix, and V matrix can be obtained by mapping the input sequence through the neural network layer. These matrices can be used in the attention calculation process to measure the correlation between the elements in the input sequence, thereby achieving a weighted summation of the input sequence. The attention mechanism calculates the dot product of the Q matrix and the K matrix to obtain the similarity (the similarity is also referred to as attention score, or dependency) between each pair of elements in the input sequence, and then normalizes the similarity through the normalization function (e.g., the softmax function) to obtain the attention weight between each pair of elements in the input sequence. In the end, these attention weights are dot-producted with the V matrix to obtain the weighted output sequence (i.e., the self-attention matrix) .
[0030] Take the self-attention mechanism in the transformer model as an example. In related art, a simple encoder in the transformer model often introduces a delay of about 100 microseconds, which is about 10 times the delay of a typical convolutional neural network (CNN) model. Different Transformer models are generally stacked with multiple encoders and decoders of similar size. In such a high latency, about 60%of the time consumption is concentrated in the processing flow of the self-attention mechanism.
[0031] In response to the high computational latency of the self-attention mechanism described above, there have been some research attempts to optimize the self-attention mechanism, such as sparse attention and attention approximation with Top_K candidate index being a high-precision self-attention mechanism optimization scheme.
[0032] The inspiration of Top_K candidate index comes from the high redundancy of human language, that is, it is possible to safely delete unimportant tokens in the text, such as prepositions, articles, adverbs and other structural and meaningless tokens, with little impact on the results of text understanding.
[0033] For the Q matrix, K matrix and V matrix corresponding to the input sequence of the self-attention mechanism, the attention weight of each token to other tokens can be obtained by the following formula (1-1) :
[0034]
[0035] Attention (Q, K, V) is the attention weight matrix. After multiplying the Q matrix by the transpose of the K matrix KT and scaling it, the attention weight matrix Attention (Q, K, V) can be obtained by normalizing softmax () . dk is the number of columns in the K matrix. The attention weight matrix Attention (Q, K, V) can include the attention weight of each token for other tokens.
[0036] The Top_K candidate index selects the largest values of each token’s attention weights for other tokens, that is, it calculates the corresponding row sequences of the K matrix and deletes other values with smaller attention weights, which ensures the calculation accuracy and greatly reduces the amount of effective calculation.
[0037] The hardware accelerator design of the Top_K candidate index can be implemented using different implementation methods.
[0038] The first implementation method is the low bit width quantization Top_K selection.
[0039] In this case, an ultra-low bit widths such as 4 bits or 2 bits is used to pre-compute the attention weight matrix calculation in the self-attention mechanism. The K row sequences with the highest attention weights in the K matrix (i.e., the Top-K row sequences) is selected, and the row index corresponding to the Top-K row sequences (i.e., the Top_K candidate index) is recorded. Then, a higher precision (such as 16 bits or 8 bits) is used to recalculate the row sequence corresponding to the Top_K candidate index in the self-attention mechanism, and other row sequences are set to zero to complete the fast selection and calculation of the Top_K candidate index and realize the sparse calculation of the self-attention mechanism.
[0040] However, in the implementation method described above, the calculation accuracy of the self-attention mechanism with low bit width is poor, resulting in large selection errors. In addition, after selecting the Top_K sequence, the attention weight matrix needs to be recalculated, and the calculation process is redundant.
[0041] In the second implementation method, the attention weight matrix calculation in the self-attention mechanism is completed in advance, and the Top-K candidate index is selected through the bubble algorithm after the normalization calculation layer. Then, based on the selected Top-K candidate index, the Top-K row sequence in the K matrix is re-addressed and other row sequences are set to 0, and the attention weight matrix in the self-attention mechanism is recalculated. Compared with the first implementation method, the second implementation method has less accuracy loss and simpler logic.
[0042] However, in the second implementation method described above, time complexity of selecting the Top_K candidate index is high, which is O (n2) . After selecting the Top_K sequence, the attention weight matrix needs to be recalculated, and the calculation process is redundant.
[0043] Based on this, embodiments of the present disclosure provide a data processing method. The method can be performed by a processor of a computer device. For example, the computer device may include, but is not limited to, a server, a laptop, a tablet computer, a desktop computer, a smart TV, a set-top box, and / or a mobile device (e.g., a mobile phone, a portable video player, a personal digital assistant, a dedicated messaging device, a portable gaming device) and other devices with data processing capabilities. The processor may include, but is not limited to, at least one of a central processing unit (CPU) , a graphics processing unit (GPU) , a microprocessor unit (MPU) , a digital signal processor (DSP) , or a field programmable gate array (FPGA) .
[0044] FIG. 1 is a flowchart of a data processing method according to some embodiments of the present disclosure. The method will be described in detail below.
[0045] 101, determining a first similarity matrix based on the query matrix and the key matrix corresponding to the word vector sequence.
[0046] In some embodiments, the word vector sequence may refer to a set of word vector representations to be processed by the self-attention mechanism. In implementation, the word vector sequence may be the input sequence of the self-attention mechanism.
[0047] For example, the input text unit sequence can be encoded to obtain a word vector sequence corresponding to the text unit sequence, and the word vector sequence can include the word vector representation corresponding to each text unit in the text unit sequence. Text units may include, but are not limited to, words, single characters, phrases, etc.
[0048] In another example, an input image can be encoded to obtain a word vector sequence corresponding to the image, and the word vector sequence may include word vector representations corresponding to at least one region of the image.
[0049] In some embodiments, the query matrix and key matrix corresponding to the word vector sequence may be generated by different linear transformations of the word vector sequence. For example, by mapping the word vector sequence through different neural network layers, the query matrix and key matrix can be obtained.
[0050] The first similarity matrix can represent the similarity between each word vector in the word vector sequence. During implementation, those skilled in the art may determine the first similarity matrix based on the query matrix and the key matrix in any appropriate manner based on actual conditions, which is not limited in the present disclosure.
[0051] In some embodiments, each row in the query matrix may represent a word vector in the word vector sequence, and each row in the key matrix may represent a word vector in the word vector sequence. The key matrix can be transposed to obtain the transposed key matrix. Then, based on the product of the query matrix and the transposed key matrix, the first similarity matrix can be determined. For example, the product of the query matrix and the transposed key matrix may be determined as the first similarity matrix. In another example, the product of the query matrix and the transposed key matrix may be scaled to obtain the first similarity matrix.
[0052] In some embodiments, each row in the query matrix may represent a word vector in the word vector sequence, each column in the key matrix may represent a word vector in the sequence of word vectors, and the first similarity matrix may be obtained based on the product of the query matrix and the key matrix. For example, the product of the query matrix and the key matrix may be determined as the first similarity matrix. In another example, the product of the query matrix and the key matrix may be scaled to obtain the first similarity matrix.
[0053] 102, filtering out a target element whose value meets a condition from each row of the first similarity matrix, and setting other elements in each row except the target element to zero to obtain a second similarity matrix.
[0054] In some embodiments, a row of elements in the first similarity matrix can represent the similarity between a word vector in the word vector sequence and each word vector in the word vector sequence.
[0055] The target elements in a row of elements of the first similarity matrix may include elements in the row whose values meet the conditions. During implementation, those skilled in the art may use any appropriate screening condition to screen out at least one target element from each row of the first similarity matrix based on actual conditions, which is not limited in the present disclosure.
[0056] In some embodiments, a Top-K screening method may be used to screen out at least one element with the largest value in each row of the first similarity matrix as a target element.
[0057] In some embodiments, at least one element having a value greater than a preset threshold may be screened out from each row of the first similarity matrix as the target element. During implementation, the preset threshold may be set by those skilled in the art based on actual conditions, which is not limited in the present disclosure.
[0058] 103, normalizing the second similarity matrix to obtain the attention weight matrix.
[0059] Any suitable normalization method can be used to normalize the second similarity matrix, which is not limited in the embodiments of the present disclosure. For example, a linear normalization function, a standard score (Z-score) normalization function, and / or a normalized exponential (Softmax) function may be used to perform normalization processing on the second similarity matrix.
[0060] 104, determining the self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and the value matrix corresponding to the word vector sequence.
[0061] In some embodiments, the value matrix corresponding to the word vector sequence may be generated by linearly transforming the word vector sequence. For example, the value matrix may be obtained by mapping the word vector sequence through a neural network layer.
[0062] During implementation, those skilled in the art can use any appropriate method to determine the self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and the value matrix based on the actual situation, which is not limited in the present disclosure.
[0063] In some embodiments, the self-attention matrix corresponding to the word vector sequence may be determined based on the product of the attention weight matrix and the value matrix. For example, the product of the attention weight matrix and the value matrix may be determined as the self-attention matrix corresponding to the word vector sequence. In another example, the product of the attention weight matrix and the value matrix may be scaled to obtain the self-attention matrix corresponding to the word vector sequence.
[0064] Consistent with the present disclosure, after determining the first similarity matrix based on the query matrix and key matrix corresponding to the word vector sequence, the selection logic of the target element can be embedded, and the elements other than the target element in each row of the first similarity matrix can be set to zero to obtain the second similarity matrix. Then, the second similarity matrix can be normalized to obtain the attention weight matrix, and based on the attention weight matrix and the value matrix corresponding to the word vector sequence, the self-attention matrix corresponding to the word vector sequence can be determined. Accordingly, the selection of target elements and the calculation process of the attention weight matrix can be realized through the calculation process of a single self-attention mechanism, thereby realizing the sparse calculation of the self-attention mechanism, greatly simplifying the calculation process, reducing the effective calculation amount, and reducing the processing delay of the self-attention mechanism. In addition, the attention weight matrix can satisfy the normalized distribution, and the calculated value corresponding to the target element in the attention weight matrix is not affected by the zeroing of other elements, thereby improving the calculation accuracy of the self-attention matrix.
[0065] In some embodiments, the process at 102 may include the following processes.
[0066] 111, for each row in the first similarity matrix, selecting K elements with the largest values from the row as target elements, K being a positive integer that is less than the total number of elements in the row.
[0067] 112, setting elements other than the target element in each row of the first similarity matrix to zero to obtain the second similarity matrix.
[0068] During implementation, K can be set based on actual conditions, which are not limited in the embodiments of the present disclosure.
[0069] Accordingly, in the process of selecting the target element, the selection of Top_K elements can be realized such that the determined target element is more important than other elements, thereby obtaining a more accurate self-attention matrix.
[0070] In some embodiments, in the process at 111 where K elements with the largest values are selected from the row as the target elements may include the following processes.
[0071] 121, traversing each element in the row in sequence, and performing the following operations on the ith element currently visited during the traversal process: if i<K+1, storing the ith element in the candidate element queue; if i>K, comparing the ith element with the smallest element in the candidate element queue; and if ith element is greater than the smallest element in the candidate element queue, replacing the smallest element in the candidate element queue with the ith element.
[0072] 122, determining each element in the candidate element queue as a target element.
[0073] In some embodiments, for the first K elements of each row in the first similarity matrix, the element can be directly stored in the candidate element queue, and for each row of elements after the Kth element, the element can be compared with the smallest element in the candidate element queue. If the element is greater than the smallest element, the smallest element in the candidate element queue can be replaced by the element. Accordingly, the process of selecting target elements can be simplified, the selection efficiency can be improved, and the computational efficiency of the self-attention matrix can be improved.
[0074] In some embodiments, the data processing method may further include the following process.
[0075] 131, when the smallest element in the candidate element queue is swapped out, temporarily storing the swapped out smallest element as the latest first temporarily stored element.
[0076] Any suitable storage method may be used to temporarily store the swapped out smallest element. The first temporarily stored element may be stored in a storage space such as a memory, a register, and / or a disk. For example, the swapped out smallest element may be temporarily stored in the first register as the latest first temporarily stored element. In another example, the swapped out smallest element may be temporarily stored in the first address in the memory as the latest first temporarily stored element. Both the first register and the first address may be preset.
[0077] It should be understood that in the process of temporarily storing the replaced smallest element as the latest first temporarily stored element, if the first temporarily stored element has been temporarily stored in the history, the first temporarily stored element temporarily stored in the history may be removed or overwritten by the latest first temporarily stored element.
[0078] In some embodiments, the process of comparing the ith element with the smallest element in the candidate element queue described in the process at 121 may include the following process.
[0079] 132, if the ith element is greater than the first temporarily stored element, comparing the ith element with the current smallest element in the candidate element queue.
[0080] In some embodiments, when the ith element is less than or equal to the first temporarily stored element, the ith element will not be compared with the current smallest element in the candidate element queue, and the next element in the current row can be directly accessed.
[0081] In some embodiments, the smallest element in the candidate element queue that has been replaced can be temporarily stored as the first temporarily stored element. If the currently traversed element is greater than the first temporarily stored element, the element can be compared with the current smallest element in the candidate element queue to determine whether the current smallest element in the candidate element queue needs to be replaced with the element. Accordingly, since the first temporarily stored element is the smallest element to be swapped out in the candidate element queue, if the currently accessed element is less than or equal to the first temporarily stored element, then the element must be smaller than the current smallest element in the candidate element queue. Therefore, only when the currently accessed element is greater than the first temporarily stored element, the element is compared with the current smallest element in the candidate element queue, which can reduce the number of comparisons and thus improve the efficiency of selecting the target element.
[0082] In some embodiments, storing the ith element in the candidate element queue when i is less than K+1 described in the process at 121 may include the following processes.
[0083] 141, when i is greater than 1 and less than K+1, comparing the ith element with the current smallest element in the candidate element queue.
[0084] 142, if the ith element is greater than the current smallest element in the candidate element queue, temporarily storing the current smallest element as the latest first temporarily stored element, and storing the ith element in the candidate element queue.
[0085] 143, if the ith element is less than or equal to the current smallest element in the candidate element queue, temporarily storing the ith element as the latest first temporarily stored element in the candidate element queue.
[0086] Accordingly, when traversing to the K+1th element, the latest first temporarily stored element used for comparison with the K+1th element is already the current smallest element in the candidate element queue. Accordingly, the number of subsequent comparisons of the K+1th element with the current smallest element in the candidate element queue can be further reduced, thereby further improving the efficiency of selecting the target element.
[0087] In some embodiments, the candidate elements in the candidate element queue may be arranged in order from largest to smallest. In some embodiments, replacing the smallest element in the candidate element queue with the ith element when the ith element is greater than the smallest element in the candidate element queue described in the process at 121 may include the following processes.
[0088] 151, temporarily storing the ith element as the latest second temporarily stored element.
[0089] Any suitable storage method may be used to temporarily store the second temporarily stored element. The second temporarily stored element may be stored in a storage space such as a memory, a register, and / or a disk. For example, the ith element currently being accessed may be temporarily stored in the second register as the latest second temporarily stored element. In another example, the ith element currently being accessed may be temporarily stored in the second address in the memory as the latest second temporarily stored element. Both the second register and the second address may be preset.
[0090] It should be understood that, in the process of temporarily storing the currently accessed ith element as the latest second temporarily stored element, if the second temporarily stored element has been temporarily stored in the history, the second temporarily stored element temporarily stored in the history can be removed or overwritten by the latest second temporarily stored element.
[0091] 152, sequentially removing each candidate element in the candidate element queue, comparing the removed candidate element with the second temporarily stored element, temporarily storing the larger element in the candidate element queue and the smaller element as the latest second temporarily stored element, and temporarily storing the smaller element as the latest second temporarily stored element.
[0092] It should be understood that the elements in the candidate element queue can be removed and stored based on the first-in-first-out principle. That is, each time a candidate element is taken out of the candidate element queue, it is the first element in the candidate element queue. When a new element is stored in the candidate element queue, it is stored at the end of the candidate element queue.
[0093] Accordingly, through two-level temporary storage, on the one hand, the first temporarily stored element used for comparison with the currently accessed element during each traversal process can be the one of the elements that has been accessed and is second only to the current smallest element in the candidate element queue, thereby minimizing the number of subsequent comparisons with the current smallest element in the candidate element queue and improving the efficiency of selecting the target element. On the other hand, the candidate elements in the candidate element queue can be arranged in descending order such that the smallest element in the candidate element queue can be accurately and efficiently determined.
[0094] In some embodiments, the process at 101 may include the following processes.
[0095] 161, performing a matrix multiplication operation on the query matrix and the transposed key matrix to obtain an intermediate matrix.
[0096] 162, scaling the intermediate matrix to obtain the first similarity matrix based on the number of columns of the key matrix.
[0097] In some embodiments, the number of columns of the key matrix may be used as a transformation scale, and the intermediate matrix may be divided by the transformation scale to obtain a first similarity matrix.
[0098] In some embodiments, the square root of the number of columns of the key matrix may be used as a transformation scale, and the intermediate matrix may be divided by the transformation scale to obtain a first similarity matrix.
[0099] In some embodiments, after performing matrix multiplication on the query matrix and the transposed key matrix, the result of the matrix multiplication may be scaled such that the values of each element in the first similarity matrix can be within an appropriate range to reduce excessive element values caused by matrix multiplication.
[0100] In some embodiments, before the process at 101, the data processing method may also include the following processes.
[0101] 171, obtaining the word vector sequence corresponding to the to-be-processed text.
[0102] 172, mapping the word vector sequence into the query matrix, the key matrix and the value matrix.
[0103] In some embodiments, the to-be-processed text may be any suitable to-be-processed text for semantic understanding.
[0104] In some embodiments, the first weight matrix may be used to perform a dot product operation with the word vector sequence, and then a first bias term can be added to obtain the query matrix of the word vector sequence. The second weight matrix may be used to perform a dot product operation with the word vector sequence, and then a second bias term can be added to obtain the key matrix of the word vector sequence. The third weight matrix may be used to perform a dot product operation with the word vector sequence, and then a third bias term can be added to obtain the value matrix of the word vector sequence. The first weight matrix, the first bias term, the second weight matrix, the second bias term, the third weight matrix and the third bias term can all be obtained through model training and learning.
[0105] Embodiments of the present disclosure provide a processor. FIG. 2 is a schematic diagram of a composition structure of a processor 20 according to some embodiments of the present disclosure. As shown in FIG. 2, the processor 20 includes a first determination module 21, a selection module 22, a normalization module 23 and a second determination module 24.
[0106] In some embodiments, the first determination module 21 may be configured to determine the first similarity matrix based on the query matrix and the key matrix corresponding to a word vector sequence.
[0107] In some embodiments, the selection module 22 may be configured to select a target element whose value meets a condition from each row of the first similarity matrix, and set other elements in each row except the target element to zero to obtain the second similarity matrix.
[0108] In some embodiments, the normalization module 23 may be configured to normalize the second similarity matrix to obtain the attention weight matrix.
[0109] In some embodiments, the second determination module 24 may be configured to determine the self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and the value matrix corresponding to the word vector sequence.
[0110] Consistent with the present disclosure, the processor can realize the selection of target elements and the calculation process of attention weight matrix through the calculation process of a single self-attention mechanism, thereby realizing the sparse calculation of the self-attention mechanism, greatly simplifying the calculation process, reducing the effective calculation amount, and reducing the processing delay of the self-attention mechanism. In addition, the attention weight matrix can satisfy the normalized distribution, and the calculated value corresponding to the target element in the attention weight matrix is not affected by the zeroing of other elements, thereby improving the calculation accuracy of the self-attention matrix.
[0111] FIG. 3 is a schematic diagram of a composition structure of the selection module 22 according to some embodiments of the present disclosure. As shown in FIG. 3, the selection module 22 includes a selection submodule 221 and a clearing submodule 222.
[0112] In some embodiments, the selection submodule 221 may be configured to select K elements with the largest value from each row in the first similarity matrix as target elements, K being a positive integer that is less than the total number of elements in the row.
[0113] In some embodiments, the clearing submodule 222 may be configured to set all elements except the target elements in each row of the first similarity matrix to zero to obtain the second similarity matrix.
[0114] In some embodiments, after selecting the target element in a row of the first similarity matrix, the selection submodule 221 may send the positions of the selected target elements in the row to the clearing submodule 222. The clearing submodule 222 may set all elements except the target elements in each row of the first similarity matrix to zero based on the positions of the target elements in the row, and output the second similarity matrix.
[0115] In some embodiments, the selection module may implement the selection of Top_K elements in the process of selecting target elements such that the determined target elements are more important than other elements, thereby obtaining a more accurate self-attention matrix.
[0116] FIG. 4 is a schematic diagram of a composition structure of the selection module 22 according to some embodiments of the present disclosure. As shown in FIG. 4, the selection module 22 includes a replacement unit 2211, a bypass circuit 2212 and a storage queue 2213.
[0117] In some embodiments, the bypass circuit 2212 may be configured to bypass the replacement unit 2211 when it is turned on to store the ith element currently input in the current row into the storage queue 2213.
[0118] In some embodiments, the replacement unit 2211 may be configured to compare the ith element currently input in the current row with the smallest element in the storage queue 2213, and when the ith element is greater than the smallest element in the storage queue 2213, replace the smallest element in the storage queue 2213 with the ith element.
[0119] In some embodiments, when i is less than K+1, the bypass circuit 2212 may be turned on, and when i is greater than K, the bypass circuit 2212 may be turned off to enable the replacement unit 2211.
[0120] During implementation, the bypass circuit may be implemented in any suitable manner, which is not limited in the embodiments of the present disclosure.
[0121] In some embodiments, the bypass circuit 2212 may include a switch that is connected in parallel with the replacement unit. When the switch is closed, the bypass circuit 2212 can be turned on and the replacement unit 2211 can be bypassed; when the switch is opened, the bypass circuit 2212 can be disconnected and the replacement unit 2211 can be enabled.
[0122] In the above embodiment, by controlling whether the replacement unit can be enabled or disabled by turning on and off the bypass circuit, the hardware implementation of the selection submodule can be simplified and the selection efficiency can be improved, thereby improving the calculation efficiency of the self-attention matrix.
[0123] FIG. 5 is a schematic diagram of a composition structure of the selection module 22 according to some embodiments of the present disclosure. As shown in FIG. 5, the replacement unit 2211 includes a first register Reg1, a comparator Comp and a replacement subunit Rep_Unit.
[0124] The comparator Comp has a first input terminal In1, a second input terminal In2, and a first output terminal O1.
[0125] The first input terminal In1 may be configured to receive the ith element, the second input terminal In2 may be configured to receive the first temporarily stored element output by the first register Reg1, and the first output terminal O1 may be configured to output the ith element to the replacement subunit Rep_Unit when the ith element is greater than the first temporarily stored element.
[0126] The replacement subunit Rep_Unit may be configured to compare the ith element with the smallest element in the storage queue 2213. The Rep_Unit may be further configured to, when the ith element is greater than the smallest element in the storage queue 2213, replace the smallest element in the storage queue 2213 with the ith element, and temporarily store the replaced smallest element as the latest first temporarily stored element in the first register Reg1.
[0127] It should be noted that the storage queue 2213 may correspond to the candidate element queue in the foregoing embodiment, and the elements stored in the storage queue 2213 may be the candidate elements in the foregoing embodiment.
[0128] During implementation, the elements stored in the storage queue 2213 may be arranged in a specific order or may not be arranged in a specific order, which is not limited in the embodiments of the present disclosure.
[0129] In some embodiments, the elements stored in the storage queue 2213 may be arranged in a set order. In the process of replacing the smallest element in the storage queue 2213 with the ith element, the elements in the storage queue 2213 may also be sorted such that after the smallest element is replaced, the elements stored in the storage queue 2213 may still be arranged in the set order. For example, the set order may include but is not limited to an order from the largest to the smallest or an order from the smallest to the largest.
[0130] Consistent with the present disclosure, first register can be used to temporarily store the smallest element replaced in the candidate element queue, and when the currently traversed element is greater than the first temporarily stored element temporarily stored in the first register, the element can be output to the replacement subunit such that the replacement subunit can compare the element with the current smallest element in the candidate element queue to determine whether the current smallest element in the candidate element queue needs to be replaced with the element. Accordingly, since the first temporarily stored element in the first register is the smallest element to be swapped out in the candidate element queue, if the currently accessed element is less than or equal to the first temporarily stored element, then the element must be less than the current smallest element in the candidate element queue. Therefore, the comparator outputs the element to the replacement subunit for comparison with the current smallest element in the candidate element queue only when the currently accessed element is greater than the first temporarily stored element. Accordingly, the number of comparisons in the replacement subunit can be reduced, thereby improving the efficiency of selecting the target element.
[0131] FIG. 6 is a schematic diagram of a composition structure of the selection module 22 according to some embodiments of the present disclosure. As shown in FIG. 6, the replacement subunit Rep_Unit comprises a second register Reg2 and a selector Sel.
[0132] The first output terminal O1 is used to temporarily store the ith element as the latest second temporarily stored element in the second register Reg2. The storage queue 2213 is used to output the stored candidate elements to the selector Sel in sequence. The selector Sel is used to receive the candidate elements output by the storage queue 2213, and select the larger element v1 from the candidate elements and the second temporarily stored elements stored in the second register Reg2 to store in the storage queue 2213, and select the smaller element v2 as the latest second temporarily stored element to be temporarily stored in the second register Reg2. In addition, the smaller element v2 is temporarily stored in the first register Reg1 as the latest first temporarily stored element.
[0133] Accordingly, two-level temporary storage is realized by the first register and the second register such that the first temporarily stored element stored in the first register during the traversal process is the one whose size is second only to the current smallest element in the candidate element queue among the traversed elements. Accordingly, the number of comparisons in subsequent replacement subunits can be minimized, thereby improving the efficiency of selecting the target elements.
[0134] In some embodiments, the selector Sel may directly temporarily store the selected smaller element in the first register Reg1, or may temporarily store the selected smaller element in the first register Reg1 through the second register Reg2, which is not limited in the embodiments of the present disclosure.
[0135] The following describes the application of the data processing method and processor provided in the embodiments of the present application in an actual scenario.
[0136] The calculation process of a typical self-attention mechanism is shown in Figure 7. First, the K matrix is transposed using the transposition module Transp, then, the matrix product module MatMul is used to perform matrix multiplication on the Q matrix and the transposed K matrix to obtain the matrix QKT. Then, the scale transformation module Scale_Trans is used to scale the matrix QKT to obtain the matrix The scale transformation can keep the inner product from being too large after multiplying the Q matrix with the transposed K matrix. Next, the normalization module Softmax is used to normalize the matrix to obtain the attention weight matrix. Lastly, the matrix product module MatMul is used to perform matrix multiplication on the attention weight matrix and the V matrix to obtain the self-attention matrix. In the calculation process of the self-attention mechanism, after the Q matrix and the transposed K matrix complete the matrix multiplication, the scale transformation and normalization calculation are performed to obtain the attention weight matrix.
[0137] In some embodiments, the process of Top_K candidate index selection is introduced to the self-attention mechanism (such as the second implementation scheme method described above) . As shown in FIG. 8, in the processing flow of the self-attention mechanism based on Top_K candidate index selection, based on the calculation flow of the self-attention mechanism shown in FIG. 7, after completing the normalization calculation, the Top_K selection module Top_K sel is used to select the largest K candidate index, and then the Top-K row sequence in the K matrix is re-addressed, the other row sequences are set to 0, and the attention weight matrix in the self-attention mechanism is recalculated.
[0138] The calculation process of Softmax is shown in the following formula 1-2.
[0139]
[0140] where Xi represents the ith element in the row sequence X in the matrix to be normalized, softmax (Xi) represents the normalized value corresponding to Xi, Xj represents the jth element in the row sequence X, dk represents the total number of elements in the row sequence X, that is, the number of columns of the matrix, i is a positive integer that is less than or equal to dk, and Xmax is the maximum value in the row sequence X.
[0141] The calculation process of Softmax is a linear calculation process. Therefore, the results of Top_K candidate index selection before and after Softmax calculation are consistent. Using this feature, an embodiment of the present disclosure provides a processing flow of a self-attention mechanism. As shown in FIG. 9, the Top_K sel module is processed before Softmax. Without affecting the Top_K candidate results, it also ensures that the Softmax distribution sum after Top_K is still 1. There is no need to recalculate the self-attention weight matrix, which greatly saves calculation steps. After the Scale_Trans module completes the calculation, the Top_K sel module selects the largest K values on each row in the matrix and sets them to valid values, and sets other values to zero. After the Top_K sel module, the zeroed matrix is output to the Softmax module to complete other subsequent calculations of the self-attention mechanism.
[0142] FIG. 10 is a schematic diagram of a hardware architecture of the Top_K sel module according to some embodiments of the present disclosure. As shown in FIG. 10, for each row in the matrix, each element in the row can be input into the Top_K sel module in sequence to select the K elements with the largest values (i.e., the Top_K elements) . For the ith element data_in (i) currently input to the Top_K sel module, the input element is compared with the attention weight matrix in the first register min_reg by the comparator Comp, and if the input element is greater than the first temporarily stored element, the input element is stored in the second register temp_reg. The comparator Comp can be bypassed by bypass circuit. When the bypass circuit is turned on, the comparator Comp is bypassed and the input element can be directly stored in the second register temp_reg. The selector Sel is used to select a larger value and a smaller value from the second temporarily stored element in the second register temp_reg and the output element of the storage queue FIFO_TOP. The larger value output by the selector Sel is cached in the storage queue FIFO_TOP, and the smaller value is temporarily stored in the second register temp_reg for comparison and selection with other elements in the storage queue FIFO_TOP. In addition, the smaller value temporarily stored in the second register temp_reg is also updated to the first register min_reg as the first temporarily stored element. The storage queue FIFO_TOP is used to store the values of the Top_K elements selected during the selection process and their position information in the corresponding row.
[0143] In some embodiments, the Top_K sel module may perform sorting and selection based on the following processes.
[0144] 301, for the first input element, bypass the comparator Comp and the selector Sel using the bypass circuit, and directly cache the input element into the storage queue FIFO_TOP.
[0145] 302, for the second to Kth input elements, bypass the comparator Comp using the bypass circuit bypass, cache the input elements into the second register temp_reg, and remove the first candidate element in the storage queue FIFO_TOP and input it into the selector Sel. In the selector Sel, select the larger value and the smaller value from the second temporarily stored element and the input candidate element, the selected larger value is stored in the storage queue FIFO_TOP, and the selected smaller value is cached in the second register temp_reg. Update the first temporarily stored element in the first register min_reg with the selected smaller value until all candidate elements in the storage queue FIFO_TOP are traverse once, and then continue to input the next input element and sort the selection. When each input element is input, it can be ensured that the data in the storage queue FIFO_TOP is arranged in descending order.
[0146] 303, for the K+1th input element and other input elements after the K+1th input element, first compare the input element with the first temporary element in the first register min_reg, if the input element is greater than the first temporarily stored element in the first register min_reg, cache the input element in the second register temp_reg as the latest second temporarily stored element. At the same time, the first candidate element in the storage queue FIFO_TOP is removed, and the larger and smaller values of the second temporarily stored element and the input candidate element are selected in the selector Sel. The selected larger value is stored in the storage queue FIFO_TOP, and the selected smaller value is cached in the second register temp_reg. The selected smaller value is used to update the first temporarily stored element in the first register min_reg until all candidate elements in the storage queue FIFO_TOP are traversed once, and then continue to input the next input element for sorting and selection.
[0147] 304, after completing the Top_K selection and sorting of a row of elements in the matrix, the Top_K candidate elements cached in the storage queue FIFO_TOP are output to the clear output module Out. The clearing output module Out sets the elements at other positions in the row to zero based on the position information of each candidate element in the Top_K candidate elements in the corresponding row, and finally outputs the elements in the row after being set to zero.
[0148] FIG. 11 is a schematic diagram of an implementation flow of the Top_K element selection method according to some embodiments of the present disclosure. As shown in FIG. 11, the Top_K element selection method includes the following processes.
[0149] 401, input element data_in (i) .
[0150] 402, determine whether i≤K. If so, proceed to the process at 403; otherwise, proceed to the process at 415. K is a positive integer and K is less than the total number of elements in a row of the matrix.
[0151] 403, temporarily store data_in (i) in temp_reg and set j to 0.
[0152] In some embodiments, the element stored in the second register temp_reg may be the second temporarily stored element Tmp. Each time a new element is stored in temp_reg, the new element will serve as the latest Tmp to replace the original Tmp.
[0153] 404, take out a candidate element from FIFO_TOP and assign it to A, and add 1 to j.
[0154] In some embodiments, the elements in the storage queue FIFO_TOP can be taken out and stored based on the first-in-first-out principle.
[0155] 405, determine whether A<Tmp or A is empty.
[0156] 406, store A in FIFO_TOP.
[0157] 407, determine whether j=i. If not, proceed to the process at 404; otherwise, proceed to the process at 408.
[0158] 408, store Tmp in FIFO_TOP.
[0159] 409, store Tmp in min_reg.
[0160] In some embodiments, the element stored in the first register min_reg may be the first temporarily stored element M. Each time a new element is stored in min_reg, the new element will be used as the latest M to replace the original M.
[0161] 410, store Tmp in FIFO_TOP.
[0162] 411, determine whether j=1. If so, proceed to the process at 412; otherwise, proceed to the process at 414.
[0163] 412, store A in FIFO_TOP.
[0164] 413, store A in min_reg.
[0165] 414, temporarily store A in temp_reg.
[0166] In some embodiments, A may be temporarily stored in the second register temp_reg as the latest second temporarily stored element Tmp.
[0167] 415, determine whether data_in (i) >M. If so, proceed to the process at 416; otherwise, proceed to the process at 424.
[0168] 416, temporarily store data_in (i) in temp_reg and set j to 0.
[0169] 417, take out a candidate element from FIFO_TOP and assign it to A, and add 1 to j.
[0170] 418, determine whether A>Tmp. If so, proceed to the process at 419; otherwise, proceed to the process at 421.
[0171] 419, store A in FIFO_TOP.
[0172] 420, temporarily store A in temp_reg.
[0173] 421, store Tmp in FIFO_TOP.
[0174] 422, determine whether j=K. If so, proceed to the process at 423; otherwise, proceed to the process at 420.
[0175] 423, store Tmp in min_reg.
[0176] 424, end.
[0177] It should be noted that for each row of elements to be selected as the Top_K elements, the process described above may be performed on each element in the row until the traversing of each element in the row is completed. The candidate elements stored in FIFO_TOP may be the Top_K elements (corresponding to the target elements in the aforementioned embodiment) .
[0178] Consistent with the present disclosure, first, a fast Top_K element selection hardware architecture based on the element stored in the minimum register (i.e., the first register min_reg) as the threshold is designed and applied to the processing flow of the self-attention mechanism to quickly complete Top_K screening while meeting the Top_K selection accuracy. Secondly, in the self-attention module, after performing matrix multiplication of the Q matrix and the K matrix, the Top_K selection logic can be embedded to realize the merging of the Top_K selection and the final calculation process steps while satisfying the normalized distribution of Softmax processing and keeping the calculated value corresponding to the target element in the attention weight matrix unchanged. Accordingly, the calculation process is simplified and the processing delay of the self-attention mechanism is reduced.
[0179] Consistent with the present disclosure, a faster Top_K search engine can be implemented to reduce the time complexity of Top_K selection to O (n*k) . In addition, a unified Top_K selection and Softmax calculation process is adopted, which eliminates the need to repeat the matrix multiplication processing and Softmax calculation of the query matrix and the key matrix, simplifies the processing flow and saves processing time.
[0180] The description of the above processor embodiment is similar to the description of the above method embodiment and has similar beneficial effects as the method embodiment. In some embodiments, the functions or modules included in the processor provided in the embodiments of the present disclosure can be used to execute the method described in the above method embodiment. For technical details not disclosed in the processor embodiment of the present disclosure, reference can be made to the description of the method embodiment of the present disclosure.
[0181] In the present disclosure, if the data processing method is implemented as a software functional module and sold or applied as an independent product, the method can be stored in a computer-readable storage medium. Thus, the technical solutions of the present disclosure or the parts contributing to the existing technology can essentially be embodied in the form of a software product. The software product can be stored on a storage medium and include multiple instructions for enabling an electronic device (e.g., a personal computer, a server, or a network device) to execute some or all of the methods of embodiments of the present disclosure. The storage medium can include various media capable of storing program codes, such as a USB drive, an external hard drive, a read-only memory (ROM) , a disk, or an optical disc. Therefore, embodiments of the present disclosure are not limited to any specific hardware, software or firmware, or any combination of hardware, software and firmware.
[0182] Embodiments of the present disclosure also provide a computer device having a memory and a processor. The memory stores a computer program that, when executed by the processor, causes the processor to implement the steps in the above data processing method; or, the processor is the processor described in the above embodiment.
[0183] Embodiments of the present disclosure also provide a computer-readable storage medium storing a computer program that, when executed by the processor, causes the processor to implement some or all of the steps in the method described above. The computer-readable storage medium can be transient or non-transient.
[0184] Embodiments of the present disclosure provide a computer program including computer-readable codes. When the computer readable code is executed in a computer device, a processor in the computer device executes some or all of the steps in the above method.
[0185] Embodiments of the present disclosure provide a computer program product, including a non-transient computer-readable storage medium storing the computer program that, when read and executed by a computer, causes the computer to perform some or all of the steps of the method above. The computer program product can be implemented by hardware, software, or a combination thereof. In some embodiments, the computer program product can be embodied as a computer storage medium. In some other embodiments, the computer program product can be embodied as a software product, such as a software development kit (SDK) .
[0186] Embodiments of the present disclosure provide a computer program product including a computer program or instructions, which, when executed by a processor, implements some or all of the steps in the above method.
[0187] The description of embodiments of the present disclosure tends to highlight the differences between embodiments of the present disclosure, and same or similar parts can be referred to each other. The description of the device, storage medium, computer program, and computer program product are similar to the description of the method embodiments and has similar beneficial effect as the method embodiments. For the technical details not described in embodiments of the device, storage medium, computer program, and computer program product of the present disclosure, reference can be made to the description of the method embodiments.
[0188] FIG. 12 is a schematic diagram showing a hardware entity of a computer device according to some embodiments of the present disclosure. As shown in FIG. 6, the hardware of the computer device 500 includes a processor 501, a communication interface 502, and a memory 503. The processor 501 can be configured to control the overall operation of the computer device 500. The communication interface 502 can be configured to allow the computer device to communicate with other terminals or servers through a network.
[0189] The memory 503 can be used to store instructions and applications that can be executed by the processor 501 and cache data to be processed or data that has been processed by the processor 501 and various modules of the computer device 500 (e.g., image data, audio data, voice communication data, and video communication data) . The memory 503 can include flash memory or random-access memory (RAM) . The processor 501, the communication interface 502, and the memory 503 can transfer data to each other through a bus 504.
[0190] The term “an embodiment” or “one embodiment” mentioned throughout the specification means that specific features, structures, or characteristics related to the embodiment are included in at least one embodiment of the present disclosure. Therefore, “in one embodiment” or “in an embodiment” in various parts of the specification do not necessarily refer to the same embodiment. In addition, these specific features, structures, or characteristics can be combined in various suitable methods in one or more embodiments. In various embodiments of the present disclosure, the numbered processes do not imply a specific order of execution. The execution order of the processes should be determined by their functionality and internal logic, and should not be considered as a limitation on the implementation processes of embodiments of the present disclosure. The numbering of embodiments of the present disclosure is merely for descriptive purposes and does not imply any superiority or preference of embodiments of the present disclosure.
[0191] The terms “comprise, ” “include, ” or any variations thereof are intended to encompass non-exclusive inclusion. Thus, a process, a method, an item, or a device that includes a series of elements can include additional elements not explicitly listed, or can include other elements inherent to the process, method, item, or device. When there is no more limitation, an element defined by “including an . . . ” does not exclude another identical element in the process, method, item, or device including the element.
[0192] In embodiments of the present disclosure, the disclosed device and method can be implemented in other methods. The device embodiments described above are illustrative. For example, the division of the units is merely a logical function division. In some embodiments, other division methods can exist, e.g., a plurality of units or assemblies can be combined or integrated in another system, or some features can be ignored or not be performed. In addition, the coupling, direct coupling, or communicative connection between the various components discussed or illustrated can be indirect coupling or communicative connection through some interfaces, devices, or units, and can be electrical, mechanical, or in another manner.
[0193] The units described as separate components may be or may not be physically separated. The components shown as the units may be or may not be physical units, and can be located in one place or distributed over a plurality of network units. Some or all of the units can be selected as needed to implement the purpose of the technical solution of embodiments of the present disclosure.
[0194] In addition, the functional units of embodiments of the present disclosure can be integrated in a processing unit or can be used as independent units, respectively. In some embodiments, two or more units can be integrated into one unit. The integrated unit can be implemented by hardware or a combination of hardware and a software functional unit.
[0195] Those of ordinary skill in the art can understand that all or some processes of method embodiments can be performed by the program instructing related hardware. The program can be stored in the computer-readable storage medium. When the program is executed, the processes of method embodiments can be performed. The storage medium can include various media capable of storing program codes, such as a mobile storage device, a read-only memory (ROM) , discs, or optical discs.
[0196] In some other embodiments, if the integrated unit is implemented as a software functional module and sold or used as an independent product, the integrated unit can also be stored in a computer-readable storage medium. Based on this understanding, the technical solutions disclosed here or parts contributing to the existing technology can be essentially embodied in the form of a software product. This computer software product can be stored on a storage medium and includes multiple instructions for enabling the electronic device (e.g., a personal computer, a server, or a network device) to execute some or all of the method of embodiments of the present disclosure. The storage medium can include various media capable of storing program codes, such as a mobile storage device, a ROM, a disk, or an optical disc.
[0197] The above description is only embodiments of the present disclosure, but the scope of the present disclosure is not limited to this. Those skilled in the art can easily think of modifications or replacements within the technical scope of the present disclosure. These modifications and replacements are within the scope of the present disclosure.
Claims
1.A data processing method comprising;determining a first similarity matrix based on a query matrix and a key matrix corresponding to a word vector sequence;filtering out a target element whose value meets a condition from each row of the first similarity matrix, and setting all elements other than the target element in each row to zero to obtain a second similarity matrix;normalizing the second similarity matrix to obtain an attention weight matrix; anddetermining a self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and a value matrix corresponding to the word vector sequence.2.The data processing method of claim 1, wherein filtering out the target element whose value meets the condition from each row of the first similarity matrix and setting all elements other than the target element in each row to zero to obtain the second similarity matrix includes:for each row in the first similarity matrix, selecting K elements with a largest value from the row as the target elements, K being a positive integer that is less than a total number of elements in the row; andsetting the elements other than the target element in each row of the first similarity matrix to zero to obtain the second similarity matrix.3.The data processing method of claim 2, wherein selecting K elements with the largest value from the row as the target elements include:traversing each element in the row in sequence, and performing the following operations on an ith element currently traversed to during the traversal process:if i<K+1, storing the ith element in a candidate element queue;if i>K, comparing the ith element with a smallest element in the candidate element queue, and replacing the smallest element in the candidate element queue with the ith element when the ith element is greater than the smallest element in the candidate element queue; anddetermining each element in the candidate element queue as the target element.4.The data processing method of claim 3 further comprising:temporarily storing the replaced smallest element as a latest first temporarily stored element when the smallest element in the candidate element queue is replaced; andcomparing the ith element with the smallest element in the candidate element queue includes:comparing the ith element with the current smallest element in the candidate element queue if the ith element is greater than the first temporarily stored element.5.The data processing method of claim 4, wherein:the candidate elements in the candidate element queue are arranged in order from largest to smallest; andreplacing the smallest element in the candidate element queue with the ith element when the ith element is greater than the smallest element in the candidate element queue includes:temporarily storing the ith element as a latest second temporarily stored element; andsequentially removing each candidate element in the candidate element queue, comparing the removed candidate element with the second temporarily stored element, storing the larger element in the candidate element queue, temporarily storing the smaller element as the latest second temporarily stored element, and temporarily storing the smaller element as the latest first temporarily stored element.6.A processor comprising:a first determination module, the first determination module being configured to determine a first similarity matrix based on a query matrix and a key matrix corresponding to a word vector sequence;a selection module, the selection module being configured to select a target element whose value meets a condition from each row of the first similarity matrix, and set other elements in each row other than the target element to zero to obtain a second similarity matrix;a normalization module, the normalization module being configured to normalize the second similarity matrix to obtain an attention weight matrix; anda second determination module, the second determination module being configured to determine a self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and a value matrix corresponding to the word vector sequence.7.The processor of claim 6, wherein the selection module includes:a selection submodule, the selection submodule being configured to select K elements with a largest value from each row in the first similarity matrix as the target elements, K being a positive integer that is less than a total number of elements in the row; anda clearing submodule the clearing submodule being configured to set all elements other than the target elements in each row of the first similarity matrix to zero to obtain the second similarity matrix.8.The processor of claim 7, wherein:the selection submodule includes a replacement unit, a bypass circuit and a storage queue, wherein:the bypass circuit is configured to bypass the replacement unit when it is turned on to store an ith element currently input in the row into the storage queue;the replacement unit is configured to compare ith element currently input in the row with a smallest element in the storage queue, and replace the smallest element in the storage queue with the ith element if the ith element is greater than the smallest element in the storage queue, wherein when i is less than K+1, the bypass circuit is turned on, and when i is greater than K, the bypass circuit is turned off to enable the replacement unit.9.The processor of claim 8, wherein:the replacement unit includes a first register, a comparator and a replacement subunit, wherein:the comparator includes a first input terminal, a second input terminal and a first output terminal, the first input terminal being configured to receive the ith element, the second input terminal being configured to receive a first temporarily stored element output by the first register, and the first output terminal being configured to output the ith element to the replacement subunit when the ith element is greater than the first temporarily stored element;the replacement subunit is configured to compare the ith element with the smallest element in the storage queue, replace the smallest element in the storage queue with the ith element when the ith element is greater than the smallest element in the storage queue, and temporarily store the replaced smallest element as the latest first temporarily stored element in the first register.10.The processor of claim 9, wherein:the replacement subunit includes a second register and a selector, wherein:the first output terminal is configured to temporarily store the ith element as a latest second temporarily stored element in the second register;the storage queue is configured to output the stored candidate elements to the selector in sequence;the selector is configured to receive the candidate elements output by the storage queue, select the larger element from the candidate element and the second temporarily stored element stored in the second register to store in the storage queue, select the smaller element as the latest second temporarily stored element to store in the second register, and store the smaller element as the latest first temporarily stored element in the first register.11.A computer device comprising:one or more processors; andone or more memories coupled to the one or more processors and storing a plurality of computer instructions that, when being executed, cause the one or more processors to:determine a first similarity matrix based on a query matrix and a key matrix corresponding to a word vector sequence;filter out a target element whose value meets a condition from each row of the first similarity matrix, and set all elements other than the target element in each row to zero to obtain a second similarity matrix;normalize the second similarity matrix to obtain an attention weight matrix; anddetermine a self-attention matrix corresponding to the word vector sequence based on the attention weight matrix and a value matrix corresponding to the word vector sequence.12.The computer device of claim 11, wherein the one or more processors are further configured to:for each row in the first similarity matrix, select K elements with a largest value from the row as the target elements, K being a positive integer that is less than a total number of elements in the row; andset the elements other than the target element in each row of the first similarity matrix to zero to obtain the second similarity matrix.13.The computer device of claim 12, the one or more processors are further configured to:traverse each element in the row in sequence, and perform the following operations on an ith element currently traversed to during the traversal process:if i<K+1, store the ith element in a candidate element queue;if i>K, compare the ith element with a smallest element in the candidate element queue, and replace the smallest element in the candidate element queue with the ith element when the ith element is greater than the smallest element in the candidate element queue; anddetermine each element in the candidate element queue as the target element.14.The computer device of claim 13, wherein the one or more processors are further configured to:temporarily store the replaced smallest element as a latest first temporarily stored element when the smallest element in the candidate element queue is replaced; andcompare the ith element with the current smallest element in the candidate element queue if the ith element is greater than the first temporarily stored element.15.The computer device of claim 14, wherein:the candidate elements in the candidate element queue are arranged in order from largest to smallest; andthe one or more processors are further configured to:temporarily store the ith element as a latest second temporarily stored element; andsequentially remove each candidate element in the candidate element queue, compare the removed candidate element with the second temporarily stored element, store the larger element in the candidate element queue, temporarily store the smaller element as the latest second temporarily stored element, and temporarily store the smaller element as the latest first temporarily stored element.