A self-attention mechanism calculation method and reasoning method
By optimizing matrix operations in the calculation of the self-attention mechanism and reducing the number of divisions, the problem of low computational efficiency in the existing technology is solved, and more efficient hardware resource utilization and reasoning task efficiency are achieved.
Patent Information
- Application Number
- CN202410852330.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-27
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2044-06-27
AI Technical Summary
In the existing technology, there are many division operations in the calculation process of the self-attention mechanism, which results in large hardware resource usage and low computing efficiency, which affects the efficiency of reasoning tasks such as text translation and abnormal behavior detection.
After determining the query matrix, key matrix and value matrix, calculate the product of the transposed key matrix and the query matrix, and use the natural constant e as the base to perform exponential operations to reduce the number of division operations. If the number of columns of the value matrix is less than the number of columns of the product matrix , then the matrix product is calculated to reduce the number of division operations.
It reduces hardware resource usage, improves computing efficiency, and improves the efficiency of reasoning tasks based on the self-attention mechanism.
Smart Images

Figure CN118396039B
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a self-attention mechanism calculation method and reasoning method. Background Art
[0002] Currently, the self-attention mechanism is often required in the implementation of reasoning tasks such as text translation and abnormal behavior detection. However, since there are many division operations in the calculation process of the self-attention mechanism, it occupies a large amount of hardware resources and has low computational efficiency. Therefore, the implementation efficiency of reasoning tasks such as text translation and abnormal behavior detection is low. That is, the efficiency of reasoning tasks based on the self-attention mechanism is low. Summary of the invention
[0003] The purpose of the embodiments of the present application is to provide a self-attention mechanism calculation method and a reasoning method to improve the efficiency of reasoning tasks based on the self-attention mechanism. The specific technical solution is as follows:
[0004] In a first aspect, an embodiment of the present application provides a method for calculating a self-attention mechanism, the method comprising:
[0005] Determine a query matrix, a key matrix, and a value matrix based on an object to be processed; the object to be processed includes any one or more of an image, a text, a video, an audio, and a signal;
[0006] Transposing the key matrix to obtain a transposed key matrix; and calculating the product of the query matrix and the transposed key matrix as a first product matrix;
[0007] Taking the natural constant e as the base and the first product matrix as the exponent, a second product matrix is obtained;
[0008] If the number of columns of the value matrix is less than the number of columns of the first product matrix, calculating the product of the second product matrix and the value matrix as a third product matrix;
[0009] The sum of the second product matrix is calculated as the matrix sum; and the ratio of the third product matrix to the matrix sum is calculated as the output feature obtained based on the self-attention mechanism.
[0010] In some embodiments, the method further comprises:
[0011] If the number of columns of the value matrix is greater than the number of columns of the first product matrix, calculating the sum of the second product matrix as the matrix sum; and calculating the ratio of the second product matrix to the matrix sum as the fourth product matrix;
[0012] The product of the fourth product matrix and the value matrix is calculated as the output feature obtained based on the self-attention mechanism.
[0013] In some embodiments, the object to be processed is an image.
[0014] The step of determining a query matrix, a key matrix, and a value matrix based on the object to be processed includes:
[0015] Segmenting the image to obtain a plurality of image blocks; and extracting features of each image block from a plurality of feature dimensions as features of each image block;
[0016] Based on the image blocks and the features of the image blocks, an input matrix is determined; the input matrix is an m×n matrix, and the a-th row and the b-th column of the input matrix represent the features of the a-th image block in the b-th feature dimension, wherein m is the number of the image blocks, n is the number of features corresponding to the image blocks, a is any positive integer from 1 to m, and b is any positive integer from 1 to n;
[0017] Based on the input matrix and a preset weight matrix, the query matrix, the key matrix, and the value matrix are determined; wherein the preset weight matrix includes a weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix.
[0018] In some embodiments, the object to be processed is audio.
[0019] The step of determining a query matrix, a key matrix, and a value matrix based on the object to be processed includes:
[0020] Segmenting the audio to obtain multiple audio frames; and extracting features of each audio frame from multiple feature dimensions as features of each audio frame;
[0021] Based on the audio frames and the features of the audio frames, an input matrix is determined; the input matrix is a p×q matrix, and the cth row and dth column of the input matrix represent the features of the cth audio frame in the dth feature dimension, wherein p is the number of the audio frames, q is the number of features corresponding to the audio frames, c is any positive integer from 1 to p, and d is any positive integer from 1 to q;
[0022] Based on the input matrix and a preset weight matrix, the query matrix, the key matrix, and the value matrix are determined; wherein the preset weight matrix includes a weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix.
[0023] In a second aspect, an embodiment of the present application further provides an inference method, the method comprising:
[0024] Determine a query matrix, a key matrix, and a value matrix based on an object to be processed; the object to be processed includes any one or more of an image, a text, a video, an audio, and a signal;
[0025] Transposing the key matrix to obtain a transposed key matrix; and calculating the product of the query matrix and the transposed key matrix as a first product matrix;
[0026] Taking the natural constant e as the base and the first product matrix as the exponent, a second product matrix is obtained;
[0027] If the number of columns of the value matrix is less than the number of columns of the first product matrix, calculating the product of the second product matrix and the value matrix as a third product matrix;
[0028] Calculating the sum of the second product matrix as a matrix sum; and calculating the ratio of the third product matrix to the matrix sum as an output feature obtained based on the self-attention mechanism;
[0029] Based on the output features, an inference result is determined.
[0030] In some embodiments, the object to be processed is an image of a region to be detected, and the inference result is whether there is abnormal behavior and / or abnormal objects in the region to be detected;
[0031] Alternatively, the object to be processed is a text, and the inference result is a translation of a character in the text;
[0032] Alternatively, the object to be processed is a video, and the inference result is whether there is abnormal behavior in the video;
[0033] Alternatively, the object to be processed is audio, and the inference result is whether there is abnormal language in the audio;
[0034] Alternatively, the object to be processed is a plurality of images to be processed, and the inference result is a video generated based on the plurality of images to be processed.
[0035] In a third aspect, an embodiment of the present application further provides a self-attention mechanism computing device, the device comprising:
[0036] Matrix determination module: used to determine the query matrix, key matrix, and value matrix based on the object to be processed; the object to be processed is any one or more of an image, text, video, audio, and signal;
[0037] A matrix calculation module is used to transpose the key matrix to obtain a transposed key matrix; and calculate the product of the transposed key matrix and the query matrix as a first product matrix; and obtain a second product matrix using the natural constant e as the base and the first product matrix as the exponent;
[0038] The matrix calculation module is also used to calculate the product of the second product matrix and the value matrix as a third product matrix if the number of columns of the value matrix is less than the number of columns of the first product matrix; calculate the sum of the second product matrices as a matrix sum; and calculate the ratio of the third product matrix to the matrix sum as an output feature obtained based on the self-attention mechanism.
[0039] In some embodiments, if the number of columns of the value matrix is greater than the number of columns of the first product matrix,
[0040] The matrix calculation module is further configured to calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the second product matrix to the matrix sum as a fourth product matrix;
[0041] The product of the fourth product matrix and the value matrix is calculated as the output feature obtained based on the self-attention mechanism.
[0042] In some embodiments, the object to be processed is an image, and the matrix determination module is specifically used to:
[0043] Segmenting the image to obtain a plurality of image blocks; and extracting features of each image block from a plurality of feature dimensions as features of each image block;
[0044] Based on the image blocks and the features of the image blocks, an input matrix is determined; the input matrix is an m×n matrix, and the a-th row and the b-th column of the input matrix represent the features of the a-th image block in the b-th feature dimension, wherein m is the number of the image blocks, n is the number of features corresponding to the image blocks, a is any positive integer from 1 to m, and b is any positive integer from 1 to n;
[0045] Based on the input matrix and a preset weight matrix, the query matrix, the key matrix, and the value matrix are determined; wherein the preset weight matrix includes a weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix.
[0046] In some embodiments, the object to be processed is audio, and the matrix determination module is specifically used to:
[0047] Segmenting the audio to obtain multiple audio frames; and extracting features of each audio frame from multiple feature dimensions as features of each audio frame;
[0048] Based on the audio frames and the features of the audio frames, an input matrix is determined; the input matrix is a p×q matrix, and the cth row and dth column of the input matrix represent the features of the cth audio frame in the dth feature dimension, wherein p is the number of the audio frames, q is the number of features corresponding to the audio frames, c is any positive integer from 1 to p, and d is any positive integer from 1 to q;
[0049] Based on the input matrix and a preset weight matrix, the query matrix, the key matrix, and the value matrix are determined; wherein the preset weight matrix includes a weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix.
[0050] In a fourth aspect, an embodiment of the present application further provides an inference device, wherein the inference device includes a first operation unit and a second operation unit.
[0051] The first operation unit is used to determine a query matrix, a key matrix, and a value matrix based on the object to be processed; transpose the key matrix to obtain a transposed key matrix; and calculate the product of the query matrix and the transposed key matrix as a first product matrix; and send the first product matrix to the second operation unit; wherein the object to be processed includes any one or more of an image, text, video, audio, and signal;
[0052] The second operation unit is used to receive the first product matrix sent by the first operation unit; obtain a second product matrix with a natural constant e as a base and the first product matrix as an exponent; and send the second product matrix to the first operation unit;
[0053] The first operation unit is further used to receive the second product matrix sent by the second operation unit; if the number of columns of the value matrix is less than the number of columns of the first product matrix, calculate the product of the second product matrix and the value matrix as a third product matrix; and send the third product matrix to the second operation unit;
[0054] The second operation unit is also used to receive the third product matrix sent by the first operation unit; calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the third product matrix to the matrix sum as the output feature obtained based on the self-attention mechanism.
[0055] In a fifth aspect, an embodiment of the present application further provides an electronic device, including:
[0056] Memory, used to store computer programs;
[0057] A processor is used to implement any of the above-mentioned self-attention mechanism calculation methods when executing a program stored in a memory.
[0058] In a sixth aspect, an embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the computer program implements any of the above-mentioned self-attention mechanism calculation methods.
[0059] In the seventh aspect, an embodiment of the present application also provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute any of the self-attention mechanism calculation methods described above.
[0060] Beneficial effects of the embodiments of the present application:
[0061] In the technical solution provided by the embodiment of the present application, after determining the query matrix, key matrix, and value matrix based on the object to be processed, the product of the transposed key matrix and the query matrix is calculated to obtain a first product matrix; then the natural constant e is used as the base, and the first product matrix is used as the exponent to obtain the second product matrix; at this time, in the related art, the ratio of the sum of the second product matrix and the first product matrix is calculated, and then the product of the ratio and the value matrix is calculated as the output feature, so that when the division operation is used to calculate the ratio, the number of division operations performed is consistent with the number of elements in the first product matrix; and in the present application, if the number of columns of the value matrix is less than the first product matrix The number of columns of the second product matrix and the value matrix is calculated as the third product matrix; at this time, the number of rows of the third product matrix is the same as the number of rows of the first product matrix, and the number of columns of the third product matrix is the same as the number of columns of the value matrix. In this way, the number of elements in the third product matrix is less than the number of elements in the first product matrix. Furthermore, when the ratio of the sum of the third product matrix and the second product matrix is subsequently calculated, the number of division operations performed is less than the number of division operations performed in the related art. Therefore, the self-attention mechanism calculation method provided in the embodiment of the present application occupies fewer hardware resources and has higher calculation efficiency, and thus the efficiency of the reasoning task based on the self-attention mechanism is higher.
[0062] Of course, implementing any product or method of the present application does not necessarily require achieving all of the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other embodiments can also be obtained based on these drawings.
[0064] Figure 1 A flowchart of a self-attention mechanism calculation method in related technologies;
[0065] Figure 2 Another flowchart of a self-attention mechanism calculation method in related technologies;
[0066] Figure 3 A flowchart of a method for calculating a self-attention mechanism when the number of columns of the value matrix provided in an embodiment of the present application is less than the number of columns of the first product matrix;
[0067] Figure 4 Provided for the embodiments of this application Figure 3 An example diagram of the calculation method of the self-attention mechanism shown;
[0068] Figure 5 A flowchart of a method for calculating a self-attention mechanism when the number of columns of the value matrix provided in an embodiment of the present application is greater than the number of columns of the first product matrix;
[0069] Figure 6 The embodiments of the present application provide Figure 3 and Figure 5 A flowchart of the combination of the self-attention mechanism calculation method shown;
[0070] Figure 7 A flowchart of the reasoning method provided in the embodiment of the present application;
[0071] Figure 8 Another flowchart of the reasoning method provided in the embodiment of the present application;
[0072] Fig. 9 A flowchart of a method for calculating a self-attention mechanism in an inference method provided in an embodiment of the present application;
[0073] Fig.10 A schematic diagram of a structure of a self-attention mechanism computing device provided in an embodiment of the present application;
[0074] Fig.11 A schematic diagram of the structure of the inference device provided in the embodiment of the present application;
[0075] Fig.12 Provided for the embodiments of this application Fig.11An example diagram of an inference device is shown;
[0076] Fig.13 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0077] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field based on the present application belong to the scope of protection of the present application.
[0078] Below, the terms in the embodiments of the present application are explained:
[0079] Large Language Model (LLM): LLM is a deep learning model trained on a large amount of text data. It usually adopts the structure of the Transformer model, which is a neural network model based on the self-attention mechanism.
[0080] Currently, the self-attention mechanism is often required in the implementation of reasoning tasks such as text translation and abnormal behavior detection. However, since there are many division operations in the calculation process of the self-attention mechanism, it occupies a large amount of hardware resources and has low computational efficiency. Therefore, the implementation efficiency of reasoning tasks such as text translation and abnormal behavior detection is low. That is, the efficiency of reasoning tasks based on the self-attention mechanism is low.
[0081] Next, first Figure 1 and Figure 2 The example shown illustrates the calculation method of the self-attention mechanism in the related art.
[0082] See also Figure 1 , Figure 1 A flowchart of a self-attention mechanism calculation method in related technologies. Figure 1 In , Q represents the query matrix of the input self-attention mechanism, K represents the key matrix of the input self-attention mechanism, and V represents the value matrix of the input self-attention mechanism, where Q, K, and V are all N×C matrices.
[0083] In the calculation process of the self-attention mechanism, the query matrix is first multiplied by the transposed key matrix to obtain a new matrix, which is recorded as X, that is, X=QK T, at this time, X is an N×N matrix; then, the normalized exponential function (i.e., softmax function) is used to process X, and a new matrix is obtained again, and the matrix is recorded as Y, that is, Y=softmax(X)= softmax(QK T ), at this time, Y is an N×N matrix; then multiply Y by the value matrix to obtain a new matrix, and record the new matrix as Z, that is, Z= YV= softmax(QK T )V, at this time, Z is an N×C matrix, thus, the calculation of the self-attention mechanism is completed, and Z can be understood as the output feature obtained based on the self-attention mechanism.
[0084] When the softmax function is used to process X, the formula Y=softmax(X) can also be expressed as shown in the following formula (1):
[0085] (1)
[0086] Among them, y i It can be understood as the output of the i-th data in the matrix X after being processed by the softmax function; i represents the i-th data in the matrix X; e represents a natural constant.
[0087] It can be seen that the above formula (1) can be used to process each data in the matrix X, that is, to obtain the matrix Y. However, this method of calculating the matrix Y requires a division operation to be performed on each data in the matrix X, that is, N×N division operations are required, which consumes a lot of hardware resources, resulting in low computational efficiency of the self-attention mechanism, and further leads to low efficiency of the reasoning process based on the self-attention mechanism.
[0088] See also Figure 2 , Figure 2 Another flowchart of a self-attention mechanism calculation method in the related art is shown, including the following steps:
[0089] Step S21: vector segmentation;
[0090] Step S22: Find the maximum value of each sub-vector;
[0091] Step S23: subtract the exponential term from the maximum value element by element to obtain f(x);
[0092] Step S24: summing the subtracted vectors to obtain I(x);
[0093] Step S25: Calculate subvector softmax = f(x) / I(x);
[0094] Step S26: through iterative calculation, the normalized exponential function (softmax) of the sub-vector is used to update the global normalized exponential function.
[0095] It can be seen that in this scheme, the vector is first divided into blocks, and then each sub-vector is processed separately, and then the processing results are integrated, which improves the computational efficiency of the self-attention mechanism in a parallel computing manner. However, in this scheme, in order to obtain the processing results of each sub-vector, a softmax process must be performed on each sub-vector. At the same time, in the subsequent process of integrating the softmax processing results of each sub-vector, a softmax process must be performed on each sub-vector. It can be seen that Figure 2 In the scheme shown, each sub-vector needs to perform multiple softmax processes. It can be seen that relative to Figure 1 The scheme shown uses Figure 2 In the scheme shown, the division operation that the electronic device needs to perform not only does not decrease but increases, and the division consumes a lot of hardware resources. Figure 2 Although the scheme shown can improve the computational efficiency of the self-attention mechanism, it consumes a lot of hardware resources.
[0096] To solve the above problems, the present application embodiment provides a self-attention mechanism calculation method. Figure 1 In the method shown, when N < C, in the embodiment of the present application, after obtaining the matrix X, the softmax function is not directly used to process the matrix X, but the natural constant e is used as the base and the matrix X is used as the exponent to calculate an N×N matrix, which is recorded as the matrix Y. Then, the product of the matrix Y and the matrix V is calculated, thereby obtaining an N×C matrix, which is recorded as the matrix Z. Then, the ratio of the sum of the matrix Z and the matrix Y is calculated. At this time, the number of division operations to be performed is N×C times, and the above Figure 1 The number of division operations to be performed in the illustrated scheme is N×N times. It can be seen that the self-attention mechanism calculation method provided in the embodiment of the present application occupies fewer hardware resources and has higher calculation efficiency, and thus the efficiency of the reasoning task based on the self-attention mechanism is higher. That is, the technical solution provided in the embodiment of the present application can reduce the consumption of hardware resources and improve the efficiency of the reasoning task based on the self-attention mechanism.
[0097] It can be understood that the calculation method of the softmax function can be converted into the form shown in the above formula (1). Therefore, the method of calculating the output features in the embodiment of the present application is: first, using e as the base and matrix X as the exponent to calculate matrix Y, then calculating the product of matrix Y and matrix V to obtain matrix Z, and then calculating the ratio of the sum of matrix Z and matrix Y; Figure 1The method of calculating the output features in the method shown is: first use the softmax function to process the matrix X to obtain the matrix Y, and then calculate the product of the matrix Y and the matrix V; the principle is the same, the calculation result is the same, that is, the embodiment of the present application = softmax(X)×V in related technology = ×V, it should be noted that Y in this application is different from Y in the related art, and Y in this application = e X , while Y=softmax(X)= .
[0098] Below, through specific examples, the self-attention mechanism calculation method provided in the embodiment of the present application is described in detail. At the same time, for the convenience of description, the module used to implement the self-attention mechanism in the model based on the self-attention mechanism similar to LLM is referred to as the self-attention mechanism.
[0099] The self-attention mechanism calculation method provided in the embodiment of the present application can be applied to electronic devices. Specifically, the electronic device can be a desktop computer, a portable computer, a smart mobile terminal, etc.
[0100] See also Figure 3 , Figure 3 A flow chart of a method for calculating a self-attention mechanism when the number of columns of the value matrix provided in an embodiment of the present application is less than the number of columns of the first product matrix, comprising the following steps:
[0101] Step S31: determining a query matrix, a key matrix, and a value matrix based on the object to be processed;
[0102] Step S32: determine whether the number of columns of the value matrix is less than the number of columns of the first product matrix; if the number of columns of the value matrix is less than the number of columns of the first product matrix, execute the following steps S33 to S36;
[0103] Step S33: transpose the key matrix to obtain a transposed key matrix; and calculate the product of the transposed key matrix and the query matrix as a first product matrix;
[0104] Step S34: Taking the natural constant e as the base and the first product matrix as the exponent, a second product matrix is obtained;
[0105] Step S35: Calculate the product of the second product matrix and the value matrix as the third product matrix;
[0106] Step S36: Calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the third product matrix to the matrix sum as the output feature obtained based on the self-attention mechanism.
[0107] In the technical solution provided by the embodiment of the present application, after determining the query matrix, key matrix, and value matrix based on the object to be processed, the product of the transposed key matrix and the query matrix is calculated to obtain a first product matrix; then the natural constant e is used as the base, and the first product matrix is used as the exponent to obtain the second product matrix; at this time, in the related art, the ratio of the sum of the second product matrix and the first product matrix is calculated, and then the product of the ratio and the value matrix is calculated as the output feature, so that when the division operation is used to calculate the ratio, the number of division operations performed is consistent with the number of elements in the first product matrix; and in the present application, if the number of columns of the value matrix is less than the first product matrix , then the product of the second product matrix and the value matrix is calculated as the third product matrix; at this time, the number of rows of the third product matrix is the same as the number of rows of the first product matrix, and the number of columns of the third product matrix is the same as the number of columns of the value matrix, so that the number of elements in the third product matrix is less than the number of elements in the first product matrix, and then, when the ratio of the sum of the third product matrix and the second product matrix is subsequently calculated, the number of division operations performed is less than the number of division operations performed in the related art, and thus, the self-attention mechanism calculation method provided in the embodiment of the present application occupies less hardware resources and has higher calculation efficiency, and thus the efficiency of the reasoning task based on the self-attention mechanism is higher. That is, in the technical solution provided in the embodiment of the present application, it can reduce the consumption of hardware resources and improve the efficiency of the reasoning task based on the self-attention mechanism.
[0108] In the above step S31, the object to be processed is an object that needs to be input into the self-attention mechanism for processing, and the object to be processed may be any one or more of an image, text, audio, and signal, wherein the signal may be a millimeter wave signal, a radar signal, etc. The electronic device may determine a query matrix, a key matrix, and a value matrix based on the object to be processed.
[0109] In some embodiments, when the object to be processed is an image, the electronic device may segment the image to obtain multiple image blocks. For example, the electronic device may segment a 100×100 image into multiple 2×2 image blocks, or 4×4 image blocks, etc. The specific segmentation method and the size of the image blocks after segmentation may be set according to actual conditions. Thereafter, features of each image block are extracted from multiple feature dimensions as features of each image block. Among them, multiple feature dimensions may be set based on user needs. For example, if the user's needs may be to detect whether there are abnormal objects or abnormal behaviors in the image, the extracted feature dimensions may be set according to these needs. The extracted feature dimensions may be the pixel mean of the image block, the pixel variance of the image block, etc.
[0110] Based on each image block and the features of each image block, the electronic device can determine an input matrix, which can be an m×n matrix. The a-th row and b-th column of the input matrix represent the features of the a-th image block in the b-th feature dimension, wherein m is the number of image blocks, n is the number of features corresponding to each image block, a is any positive integer from 1 to m, and b is any positive integer from 1 to n.
[0111] A weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix are pre-set, and these weight matrices are collectively referred to as preset weight matrices; the query matrix can be obtained by multiplying the input matrix with the weight matrix corresponding to the query matrix; the key matrix can be obtained by multiplying the input matrix with the weight matrix corresponding to the key matrix, and the value matrix can be obtained by multiplying the input matrix with the weight matrix corresponding to the value matrix.
[0112] In other embodiments, when the object to be processed is audio, the electronic device can segment the audio to obtain multiple audio frames. For example, the electronic device can divide the audio into multiple audio frames according to a preset time. The preset time can be 1 second (S), 2S, 3S, etc. The specific segmentation method and the size of the audio frame after segmentation can be set according to actual conditions; thereafter, the features of each audio frame are extracted from multiple feature dimensions as the features of each audio frame, wherein the multiple feature dimensions can be set based on user needs. For example, the user's needs may be to detect whether there are abnormalities in the audio, or to convert the audio into text, etc., then the extracted feature dimensions can be set according to these needs, and the extracted feature dimensions may be the pitch, bandwidth, etc. of the audio frame.
[0113] Based on each audio frame and the features of each audio frame, the electronic device can determine the input matrix, the input matrix is a p×q matrix, the cth row and dth column of the input matrix represent the features of the cth audio frame on the dth feature dimension, wherein p is the number of audio frames, q is the number of features corresponding to each audio frame, c is any positive integer from 1 to p, and d is any positive integer from 1 to q. Afterwards, based on the input matrix and the preset weight matrix, the electronic device can determine the query matrix, the key matrix, and the predicted value matrix; wherein the method of determining the query matrix, the key matrix, and the value matrix is the same as the method of determining the query matrix, the key matrix, and the value matrix when the image is the object to be processed, and will not be repeated here.
[0114] Among them, it can be understood that in the self-attention mechanism, the product of the query matrix and the transposed key matrix needs to be calculated, so the number of columns of the query matrix is the same as the number of rows of the transposed key matrix, that is, the number of columns of the query matrix key matrix must be the same as the number of columns of the key matrix, and then, the number of columns of the weight matrix corresponding to the query matrix must be the same as the number of columns of the weight matrix corresponding to the key matrix. At the same time, the query matrix, key matrix, and value matrix are all obtained by multiplying the input matrix with the preset weight matrix. Therefore, the number of rows of the query matrix, key matrix, and value matrix are all the same, which is the number of rows of the input matrix.
[0115] When the object to be processed is text, video, signal, multiple texts, multiple images, audio and text, text and video, etc., the principle of determining the query matrix, key matrix, and value matrix corresponding to the object to be processed is the same as the principle of determining the query matrix, key matrix, and value matrix when the object to be processed is image and audio, and will not be repeated here.
[0116] In the above step S32, the electronic device determines whether the number of columns of the value matrix is less than the number of columns of the first product matrix. When the number of columns of the value matrix is less than the number of columns of the first product matrix, the subsequent steps S33 to S36 are executed; when the number of columns of the value matrix is greater than the number of columns of the first product matrix, the subsequent steps S33 to S36 can be referred to. Figure 5 The method shown, or the above Figure 2 The self-attention mechanism is calculated by methods in related technologies. The description of the first product matrix can refer to the content in the following step S33, which will not be repeated here.
[0117] Among them, since the number of rows and columns of the first product matrix is the same as the number of rows of the input matrix, it is determined whether the number of columns of the value matrix is less than the number of columns of the first product matrix, that is, it is determined whether the number of columns of the value matrix is less than the number of rows of the input matrix, or it is determined whether the number of columns of the weight matrix corresponding to the value matrix is less than the number of rows of the input matrix. In some embodiments, when the number of columns of the value matrix is consistent with the number of columns of the query matrix and the key matrix, it is determined whether the number of columns of the value matrix is less than the number of columns of the first product matrix, that is, it is determined whether the number of columns of the preset weight matrix is less than the number of rows of the input matrix.
[0118] In the above step S33, the electronic device transposes the key matrix to obtain a transposed key matrix; and then calculates the product of the query matrix and the transposed key matrix to obtain a first product matrix.
[0119] It can be understood that the first product matrix is the product of the query matrix and the transposed key matrix. Therefore, the number of rows of the first product matrix is the same as the number of rows of the query matrix, and the number of columns of the first product matrix is the same as the number of columns of the transposed key matrix. At the same time, since the number of rows of the key matrix is the same as the number of rows of the input matrix, the number of columns of the transposed key matrix is the same as the number of rows of the input matrix, and the number of rows of the query matrix is the same as the number of rows of the input matrix. Therefore, the number of rows and columns of the first product matrix are the same, both of which are the number of rows of the input matrix.
[0120] For example, if the input matrix is an A×B matrix, the number of rows of the query matrix and the key matrix are both A, and the number of columns of the query matrix and the key matrix must be the same. Therefore, the query matrix and the key matrix can be A×F matrices. In this way, the transposed key matrix is an F×A matrix, and the first product matrix obtained by multiplying the query matrix and the transposed key matrix is an A×A matrix.
[0121] In the above step S34, the electronic device uses the natural number e as the base and the first product matrix as the exponent to obtain the second product matrix. At this time, the number of rows and columns of the second product matrix are the same as the number of rows and columns of the first product matrix. For example, if the first product matrix is an A×A matrix, then the second product matrix is also an A×A matrix.
[0122] In the above step S35, the third product matrix is the product of the second product matrix and the value matrix. Therefore, the number of rows of the third product matrix is consistent with the number of rows of the second product matrix and the number of columns of the first product matrix, which are the number of rows of the input matrix; the number of columns of the third product matrix is consistent with the number of columns of the value matrix. If the second product matrix is an A×A matrix and the value matrix is an A×H matrix, the third product matrix is an A×H matrix.
[0123] In the above step S36, the output feature obtained based on the self-attention mechanism is the ratio of the third product matrix to the matrix sum value, wherein the matrix sum value is the sum value obtained by summing the second product matrix.
[0124] It can be understood that in this step, the number of division operations to be performed when calculating the ratio is the same as the number of data in the third product matrix; and if the above Figure 1The related technology shown in the figure is used for calculation, and the number of division operations to be performed is the same as the number of data in the matrix X; however, the number of rows of the matrix X is the number of rows of the input matrix, and the number of columns of the matrix X is the number of columns of the input matrix, while the number of rows of the third product matrix in the embodiment of the present application is the number of rows of the input matrix, and the number of columns of the third product matrix is the number of columns of the value matrix, but the number of columns of the value matrix is less than the number of columns of the first product matrix, that is, the number of columns of the value matrix is less than the number of columns of the input matrix. Therefore, for the same input matrix, when the number of columns of the value matrix is less than the number of columns of the first product matrix, the number of data in the matrix X is greater than the number of data in the third product matrix in the embodiment of the present application. It can be seen that for the same input matrix, when the number of columns of the value matrix is less than the number of columns of the first product matrix, the calculation method provided in the embodiment of the present application is more efficient than the calculation method provided in the embodiment of the present application for calculating the self-attention mechanism. Figure 1 The calculation method in the related technology shown requires fewer division operations to calculate the self-attention mechanism, consumes less hardware resources, and the efficiency of the self-attention mechanism is higher. The reasoning method based on the self-attention mechanism is also higher in efficiency.
[0125] The following is a specific example of the above Figure 3 For example, Q represents the query matrix of the input self-attention mechanism, K represents the key matrix of the input self-attention mechanism, and V represents the value matrix of the input self-attention mechanism, where Q, K, and V are all N×C matrices, and N is less than C, that is, the number of columns of the value matrix is less than the number of columns of the first product matrix. Figure 4 , Figure 4 A second flow chart of the self-attention mechanism calculation method provided in an embodiment of the present application.
[0126] Figure 4 In the process, the electronic device first calculates the query matrix (Q) and the transposed key matrix (K T ) to obtain the matrix X, that is, X = QK T , the matrix X is the first product matrix shown in the above step S33. At this time, the matrix X is an N×N matrix. Then, with e as the base and the first product matrix as the exponent, the matrix Y is calculated, that is, Y=exp(X), where exp is the natural constant e, and the matrix Y is the second product matrix shown in the above step S34. At this time, the matrix Y is an N×N matrix. The product of the second product matrix and the value matrix is calculated to obtain the matrix O, that is, O=YV= exp(X)V, and the matrix O is the third product matrix shown in the above step S35. At this time, the matrix O is an N×C matrix. The ratio of the third product matrix to the matrix sum value is calculated to obtain the matrix Z. The matrix Z can be regarded as the output feature obtained based on the self-attention mechanism, where, if D represents the sum of the second product matrix, then , ∑ represents the summation symbol, at this time, Z=O / D= exp(X)V / D= exp(QK T )V / D, at this time, the matrix Z is an N×C matrix.
[0127] It can be seen that compared with the above Figure 1 The method shown, when N is less than C, Figure 1 N×N division operations are required, while the embodiment of the present application performs N×C division operations. Therefore, less hardware resources are consumed. The calculation method based on the self-attention mechanism provided in the embodiment of the present application can improve the efficiency of the reasoning method.
[0128] See also Figure 5 , Figure 5 A flow chart of a method for calculating a self-attention mechanism when the number of columns of the value matrix provided in the embodiment of the present application is greater than the number of columns of the first product matrix may include the following steps:
[0129] Step S51: determining a query matrix, a key matrix, and a value matrix based on the object to be processed;
[0130] Step S52: determine whether the number of columns of the value matrix is less than the number of columns of the first product matrix; if the number of columns of the value matrix is greater than the number of columns of the first product matrix, execute the following steps S53 to S56;
[0131] Step S53: transpose the key matrix to obtain a transposed key matrix; and calculate the product of the transposed key matrix and the query matrix as a first product matrix;
[0132] Step S54: using the natural constant e as the base and the first product matrix as the exponent to obtain a second product matrix;
[0133] Step S55: Calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the second product matrix to the matrix sum as the fourth product matrix;
[0134] Step S56: Calculate the product of the fourth product matrix and the value matrix as the output feature obtained based on the self-attention mechanism.
[0135] In the embodiment of the present application, step S51, step S53-step S54 are respectively Figure 3 In the illustrated embodiment, step S31, step S33 and step S34 are the same, and therefore, they will not be described in detail later.
[0136] In the technical solution provided in the embodiment of the present application, when the number of columns of the value matrix is greater than the number of columns of the first product matrix, a division operation is directly performed based on the second product matrix to obtain a fourth product matrix, and then the product of the fourth product matrix and the value matrix is used as the output feature. Compared with the above Figure 3The method shown performs fewer division operations, occupies less hardware resources, and has higher computational efficiency, thereby making the reasoning tasks based on the self-attention mechanism more efficient.
[0137] In the above step S52, the electronic device determines whether the number of columns of the value matrix is less than the number of columns of the first product matrix. When the number of columns of the value matrix is greater than the number of columns of the first product matrix, the subsequent steps S53 to S56 are executed; when the number of columns of the value matrix is less than the number of columns of the first product matrix, the above steps can be referred to. Figure 3 The method shown is not described in detail here. This step can also refer to the description in the above step S32.
[0138] In the above step S55, the fourth product matrix is the ratio of the second product matrix to the matrix sum, wherein the matrix sum is the sum of the second product matrix. Here, the number of rows and columns of the calculated fourth product matrix are the same as the number of rows and columns of the second product matrix and the number of rows and columns of the first product matrix, that is, if the first product matrix is an A×A matrix, then the second product matrix is also an A×A matrix, and the fourth product matrix is also an A×A matrix. In the embodiment of the present application, the method of calculating the fourth product matrix can also be regarded as taking the second product matrix as input, inputting the second product matrix into the softmax function, and the calculation result obtained is the fourth product matrix.
[0139] In the above step S56, the output feature obtained based on the self-attention mechanism is the product of the fourth product matrix and the value matrix.
[0140] It can be understood that when the number of columns of the value matrix is greater than the number of columns of the first product matrix, if A represents the number of columns of the first product matrix and H represents the number of columns of the value matrix, then according to the above Figure 3 The method shown calculates the self-attention mechanism, Figure 3 The matrix that needs to be divided is the third product matrix, and the third product matrix is a matrix of A×H; if the self-attention mechanism is calculated according to the method shown in the embodiment of the present application, the matrix that needs to be divided is the second product matrix, and the second product matrix is a matrix of A times A. Since H>A, at this time, use Figure 3 The method shown in the figure needs to perform more division operations to calculate the self-attention mechanism. Therefore, in order to improve the efficiency of the reasoning process based on the self-attention mechanism, when the number of columns of the value matrix is greater than the number of columns of the first product matrix, Figure 5 The method shown computes the self-attention mechanism.
[0141] Below, Figure 6 For example, for the above Figure 3 and Figure 5 The relationship between the calculation methods of the self-attention mechanism is explained. Figure 6 The following steps may be included:
[0142] Step S61: Input the object to be processed into the self-attention mechanism so that the self-attention mechanism can obtain the query matrix, key matrix, and value matrix; for details, please refer to the description in Step S31 above;
[0143] Step S62: Determine whether N is less than C, where N can represent the number of columns of the first product matrix and C can represent the number of columns of the value matrix; when N < C is true, execute the subsequent Step S63; when N < C is false, execute the subsequent Step S64.
[0144] Step S63: Execute the normal calculation process, such as the Figure 5 calculation process shown above;
[0145] Step S64: Execute the normalization exponential function division separation calculation process (i.e., softmax division separation calculation process), that is, the Figure 3 calculation process shown above;
[0146] Step S65: Output, where the output can be understood as the output features obtained based on the self-attention mechanism. For details, please refer to the description in Step S36 or Step S56 above.
[0147] In the technical solution provided by the embodiments of the present application, a more optimal calculation method can be adaptively selected according to the actual situation, saving the hardware resource overhead; moreover, the self-attention mechanism calculation method provided by the embodiments of the present application can be widely applied to various models that need to implement corresponding functions based on the self-attention mechanism, such as the Vision Transformer (VIT) model, LLM model, etc.
[0148] Corresponding to the above self-attention calculation mechanism calculation method, the embodiments of the present application also provide an inference method, as Figure 7 shown, which may include the following steps:
[0149] Step S71: Determine the query matrix, key matrix, and value matrix based on the object to be processed; the object to be processed includes any one or more of images, texts, videos, audios, signals;
[0150] Step S72: Determine whether the number of columns of the value matrix is less than the number of columns of the first product matrix; if the number of columns of the value matrix is less than the number of columns of the first product matrix, then execute the following Steps S73 - S76;
[0151] Step S73: Transpose the key matrix to obtain the transposed key matrix; and calculate the product of the transposed key matrix and the query matrix as the first product matrix;
[0152] Step S74: Use the natural constant e as the base and the first product matrix as the exponent to obtain the second product matrix;
[0153] Step S75: Calculate the product of the second product matrix and the value matrix as the third product matrix;
[0154] Step S76: Calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the third product matrix to the matrix sum as the output feature obtained based on the self-attention mechanism;
[0155] The above steps S71 to S76 correspond to the above Figure 3 Steps S31 to S36 in the example shown are not described in detail here.
[0156] Step S77: Determine the inference result based on the output features.
[0157] In the present application embodiment, based on the above Figure 3 The method shown in the figure calculates the self-attention mechanism, which improves the computational efficiency of the self-attention calculation value, that is, it can obtain the output features more efficiently, and then use the above in the reasoning process. Figure 3 The method shown calculates the self-attention mechanism to improve the efficiency of obtaining reasoning results.
[0158] The application scenario of the embodiments of the present application may be any intelligent scenario, such as any scenario based on a transformer (Transformer-based) model, or other large model scenarios.
[0159] In some embodiments, the object to be processed may be an image of the area to be detected, and the inference result may be whether there is abnormal behavior and / or abnormal objects in the area to be detected. For example, if the object to be processed is an image of a kitchen, the inference result may be whether the chef's movements are standard, whether the chef is wearing a chef's hat, whether the trash can is overflowing, and whether there are animals such as cockroaches and mice in the kitchen. For another example, if the object to be processed is a portrait image, the inference result may be whether the portrait in the image is a portrait in the target database, and the target database may be a pre-established portrait database, that is, the self-attention mechanism may be used for portrait recognition.
[0160] Alternatively, in some embodiments, the object to be processed may be a text, and the inference result may be a translation of a word in the text. For example, a self-attention mechanism is used for machine translation.
[0161] Alternatively, in some embodiments, the object to be processed may be a video, and the inference result may be whether there is abnormal behavior in the video. For example, the object to be processed may be a video of a swimming pool, and the inference result may be whether there is a drowning incident; or the object to be processed may be a surveillance video on the road, and the inference result may be whether there are red light running, wrong-way driving, and other incidents; or the object to be processed may be a surveillance video of a community, and the inference result may be whether there is a high-altitude throwing incident.
[0162] Alternatively, in some embodiments, the object to be processed may be audio, and the inference result may be whether there is an anomaly in the audio. For example, the object to be processed may be a customer service conversation, and the inference result may be whether there is impolite language in the conversation.
[0163] Or, in some embodiments, the object to be processed may be multiple images to be processed, and the inference result may be a video generated based on the images to be processed, that is, the self-attention mechanism is used to automatically generate a video.
[0164] Taking a specific example as an example, the above Figure 7 inferred method shown in the present application will be described. For example, in an intelligent recognition system in a kitchen, anomalies need to be monitored in real time. For example, whether the actions of the chef are standardized, whether the chef is wearing a chef's hat, whether the trash can is overflowing with garbage, whether there are animals such as cockroaches and mice, see Figure 8 , Figure 8 which is the second process schematic diagram of the inference method provided by the embodiment of the present application. First, a real-time scene picture and user-defined requirements are input. Here, the real-time scene picture is also a picture of the kitchen, and the user-defined requirements may be to detect mice, etc. Here, inputting the user-defined requirements can be understood as determining the direction of processing the real-time scene picture based on the user-defined requirements, such as extracting features of the real-time scene picture from which dimensions; serializing and splicing based on the user-defined requirements and the real-time scene picture into an input token, and then inputting the token into an LLM stacked by self-attention modules. The LLM includes multiple self-attention modules (i.e., self-attention mechanisms), such as Figure 8 the LLM (i.e., the large language model) in
[0165] is stacked by 4 self-attention modules. The calculation method of each self-attention module is the same. The output features obtained based on the previous self-attention module can be input into the next self-attention module for further processing to obtain output features again. Then, an inference is made based on the output features to obtain an output result. Among them, a token can be understood as a vector. In the embodiment of the present application, a token can be used to represent multiple image blocks obtained by segmenting a real-time scene picture. Figure 8 In the above Fig. 9 shown example, the calculation process of the self-attention module is as follows
[0166] Step S91: Receive the concatenated token;
[0167] Step S92: Determine whether N is less than C, where N can represent the number of columns of the first product matrix, and C can represent the number of columns of the value matrix; when N < C is yes, execute the subsequent step S93; when N < C is no, execute the subsequent step S94.
[0168] Step S93: Execute the normal calculation process, as described above Figure 5 The calculation process shown;
[0169] Step S94: Execute the normalized exponential function division separation calculation process (i.e., the softmax division separation calculation process), i.e., Figure 3 The calculation process shown;
[0170] Step S95: Output, where the output can be understood as the output features obtained based on the self-attention module.
[0171] Corresponding to the above-mentioned self-attention mechanism calculation method, the embodiment of the present application also provides a self-attention mechanism calculation device, such as Fig.10 As shown, the computing device includes:
[0172] Matrix determination module 101: used to determine a query matrix, a key matrix, and a value matrix based on an object to be processed; the object to be processed is any one or more of an image, a text, a video, an audio, and a signal;
[0173] Matrix calculation module 102: used to transpose the key matrix to obtain a transposed key matrix; and calculate the product of the transposed key matrix and the query matrix as a first product matrix; using the natural constant e as the base and the first product matrix as the exponent to obtain a second product matrix;
[0174] Matrix calculation module 102: is also used to calculate the product of the second product matrix and the value matrix as the third product matrix if the number of columns of the value matrix is less than the number of columns of the first product matrix; calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the third product matrix to the matrix sum as the output feature obtained based on the self-attention mechanism.
[0175] In the technical solution provided by the embodiment of the present application, after determining the query matrix, key matrix, and value matrix based on the object to be processed, the product of the transposed key matrix and the query matrix is calculated to obtain a first product matrix; then the natural constant e is used as the base, and the first product matrix is used as the exponent to obtain the second product matrix; at this time, in the related art, the ratio of the sum of the second product matrix and the first product matrix is calculated, and then the product of the ratio and the value matrix is calculated as the output feature, so that when the division operation is used to calculate the ratio, the number of division operations performed is consistent with the number of elements in the first product matrix; and in the present application, if the number of columns of the value matrix is less than the first product matrix The number of columns of the second product matrix and the value matrix is calculated as the third product matrix; at this time, the number of rows of the third product matrix is the same as the number of rows of the first product matrix, and the number of columns of the third product matrix is the same as the number of columns of the value matrix. In this way, the number of elements in the third product matrix is less than the number of elements in the first product matrix. Furthermore, when the ratio of the sum of the third product matrix and the second product matrix is subsequently calculated, the number of division operations performed is less than the number of division operations performed in the related art. Therefore, the self-attention mechanism calculation method provided in the embodiment of the present application occupies fewer hardware resources and has higher calculation efficiency, and thus the efficiency of the reasoning task based on the self-attention mechanism is higher.
[0176] In some embodiments, if the number of columns of the value matrix is greater than the number of columns of the first product matrix,
[0177] The matrix calculation module 102 is also used to calculate the sum of the second product matrix as the matrix sum value; and calculate the ratio of the second product matrix to the matrix sum value as the fourth product matrix; calculate the product of the fourth product matrix and the value matrix as the output feature obtained based on the self-attention mechanism.
[0178] In some embodiments, the object to be processed is an image, and the matrix determination module 101 is specifically used to:
[0179] The image is segmented to obtain multiple image blocks; and the features of each image block are extracted from multiple feature dimensions as the features of each image block;
[0180] Based on each image block and the features of each image block, an input matrix is determined; the input matrix is an m×n matrix, and the a-th row and b-th column of the input matrix represent the features of the a-th image block in the b-th feature dimension, wherein m is the number of the image blocks, n is the number of features corresponding to each image block, a is any positive integer from 1 to m, and b is any positive integer from 1 to n;
[0181] Based on the input matrix and the preset weight matrix, the query matrix, key matrix, and value matrix are determined; wherein the preset weight matrix includes a weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix.
[0182] In some embodiments, the object to be processed is audio, and the matrix determination module 101 is specifically used for:
[0183] The audio is segmented to obtain multiple audio frames; and features of each audio frame are extracted from multiple feature dimensions as features of each audio frame;
[0184] Based on each audio frame and the features of each audio frame, an input matrix is determined; the input matrix is a p×q matrix, and the cth row and dth column of the input matrix represent the features of the cth audio frame in the dth feature dimension, wherein p is the number of audio frames, q is the number of features corresponding to each audio frame, c is any positive integer from 1 to p, and d is any positive integer from 1 to q;
[0185] Based on the input matrix and the preset weight matrix, the query matrix, key matrix, and value matrix are determined; wherein the preset weight matrix includes a weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix.
[0186] Corresponding to the above reasoning method, the embodiment of the present application provides a reasoning device, such as Fig.11 As shown, the inference device includes a first computing unit 111 and a second computing unit 112, wherein:
[0187] The first operation unit 111 is used to determine the query matrix, the key matrix, and the value matrix based on the object to be processed; transpose the key matrix to obtain the transposed key matrix; and calculate the product of the query matrix and the transposed key matrix as the first product matrix; and send the first product matrix to the second operation unit 112; wherein the object to be processed includes any one or more of an image, text, video, audio, and signal;
[0188] The second operation unit 112 is used to receive the first product matrix sent by the first operation unit 111; use the natural constant e as the base and the first product matrix as the exponent to obtain a second product matrix; and send the second product matrix to the first operation unit 111;
[0189] The first operation unit 111 is further used to receive the second product matrix sent by the second operation unit 112; if the number of columns of the value matrix is less than the number of columns of the first product matrix, calculate the product of the second product matrix and the value matrix as a third product matrix; and send the third product matrix to the second operation unit 112;
[0190] The second operation unit 112 is also used to receive the third product matrix sent by the first operation unit 111; calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the third product matrix to the matrix sum as the output feature obtained based on the self-attention mechanism.
[0191] In some examples, the inference device may further include a memory, and the calculation results such as the first product matrix, the second product matrix, and the third product matrix obtained above may be stored in the memory.
[0192] The above reasoning device is further explained by taking an example. Fig.12 As shown, Fig.12 The inference device shown in the figure includes a main central processing unit (CPU) 121, an external memory 122, and a neural network processor 123. The neural network processor 123 includes a storage unit access controller 124, an input memory 125, a vector calculation unit 126, a unified memory 127, a weight memory 128, an operation circuit 129, an accumulator 1210, a controller 1211, an instruction fetch memory 1212, and a bus interface unit 1213. The vector calculation unit 126 is equivalent to the above Fig.11 The second operation unit in the operation circuit 129 is equivalent to the above Fig.11 The calculation results of the first product matrix, the second product matrix and the third product matrix obtained based on the vector calculation unit 126 and the operation circuit 129 can be stored in the input memory 125, the unified memory 127, or the instruction memory 1212.
[0193] visible, Fig.12 In the inference device shown, the vector calculation unit 126 and the operation circuit 129 are used to implement the above-mentioned Figure 3-Figure 6 Any of the self-attention mechanism calculation methods described in, and wherein the vector calculation unit 126 is used to calculate the ratio of the third product matrix to the matrix sum value, that is, the vector calculation unit 126 is used to perform a division operation. Therefore, the technical solution provided in the embodiment of the present application reduces the division operation, that is, reduces the occupation of the computing resources of the vector calculation unit 126; the other units in the inference device except the vector calculation unit 126 and the operation circuit 129 are used to realize the inference task to be performed by the neural network processor 123 according to the output features obtained by the vector calculation unit 126 and the operation circuit 129.
[0194] The present application also provides an electronic device, such as Fig.13 As shown, including:
[0195] A memory 131, used for storing computer programs;
[0196] The processor 132 is used to implement any of the above-mentioned self-attention mechanism calculation methods when executing the program stored in the memory 131.
[0197] Furthermore, the electronic device may further include a communication bus and / or a communication interface, and the processor 132, the communication interface, and the memory 131 communicate with each other via the communication bus.
[0198] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0199] The communication interface is used for communication between the above electronic device and other devices.
[0200] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0201] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0202] In another embodiment provided in the present application, a computer-readable storage medium is also provided, which stores a computer program, and when the computer program is executed by a processor, the steps of any of the above-mentioned self-attention mechanism calculation methods are implemented.
[0203] In another embodiment provided in the present application, a computer program product comprising instructions is also provided, which, when executed on a computer, enables the computer to execute any of the self-attention mechanism calculation methods in the above embodiments.
[0204] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a solid-state hard disk (SSD), etc.
[0205] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0206] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the reasoning method, self-attention mechanism computing device, reasoning device, electronic device, computer-readable storage medium, and computer program product embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments.
[0207] The above description is only a preferred embodiment of the present application and is not intended to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application are included in the protection scope of the present application.
Claims
1. A self-attention mechanism calculation method, characterized in that: The method comprises: Determine a query matrix, a key matrix, and a value matrix based on an object to be processed; the object to be processed includes any one or more of an image, text, video, audio, millimeter wave signal, and radar signal; Transposing the key matrix to obtain a transposed key matrix; and calculating the product of the query matrix and the transposed key matrix as a first product matrix; Taking the natural constant e as the base and the first product matrix as the exponent, a second product matrix is obtained; If the number of columns of the value matrix is less than the number of columns of the first product matrix, the product of the second product matrix and the value matrix is calculated as a third product matrix; the sum of the second product matrices is calculated as a matrix sum; and the ratio of the third product matrix to the matrix sum is calculated as an output feature obtained based on the self-attention mechanism; If the number of columns of the value matrix is greater than the number of columns of the first product matrix, the sum of the second product matrix is calculated as the matrix sum; and the ratio of the second product matrix to the matrix sum is calculated as the fourth product matrix; and the product of the fourth product matrix and the value matrix is calculated as the output feature obtained based on the self-attention mechanism.
2. The method according to claim 1, characterized in that The object to be processed is an image, The determining of the query matrix, the key matrix, and the value matrix based on the object to be processed includes: Segmenting the image to obtain a plurality of image blocks; and extracting features of each image block from a plurality of feature dimensions as features of each image block; Based on the image blocks and the features of the image blocks, an input matrix is determined; the input matrix is an m×n matrix, and the a-th row and the b-th column of the input matrix represent the features of the a-th image block in the b-th feature dimension, wherein m is the number of the image blocks, n is the number of features corresponding to the image blocks, a is any positive integer from 1 to m, and b is any positive integer from 1 to n; Based on the input matrix and a preset weight matrix, the query matrix, the key matrix, and the value matrix are determined; wherein the preset weight matrix includes a weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix.
3. The method according to claim 1, characterized in that The object to be processed is audio, The determining of the query matrix, the key matrix, and the value matrix based on the object to be processed includes: Segmenting the audio to obtain multiple audio frames; and extracting features of each audio frame from multiple feature dimensions as features of each audio frame; Based on the audio frames and the features of the audio frames, an input matrix is determined; the input matrix is a p×q matrix, and the cth row and dth column of the input matrix represent the features of the cth audio frame in the dth feature dimension, wherein p is the number of the audio frames, q is the number of features corresponding to the audio frames, c is any positive integer from 1 to p, and d is any positive integer from 1 to q; Based on the input matrix and a preset weight matrix, the query matrix, the key matrix, and the value matrix are determined; wherein the preset weight matrix includes a weight matrix corresponding to the query matrix, a weight matrix corresponding to the key matrix, and a weight matrix corresponding to the value matrix.
4. A reasoning method, characterized in that The method comprises: Determine a query matrix, a key matrix, and a value matrix based on an object to be processed; the object to be processed includes any one or more of an image, text, video, audio, millimeter wave signal, and radar signal; Transposing the key matrix to obtain a transposed key matrix; and calculating the product of the query matrix and the transposed key matrix as a first product matrix; Taking the natural constant e as the base and the first product matrix as the exponent, a second product matrix is obtained; If the number of columns of the value matrix is less than the number of columns of the first product matrix, calculate the product of the second product matrix and the value matrix as a third product matrix; calculate the sum of the second product matrices as a matrix sum; and calculate the ratio of the third product matrix to the matrix sum as an output feature obtained based on the self-attention mechanism; If the number of columns of the value matrix is greater than the number of columns of the first product matrix, the sum of the second product matrix is calculated as the matrix sum; and the ratio of the second product matrix to the matrix sum is calculated as a fourth product matrix; the product of the fourth product matrix and the value matrix is calculated as the output feature obtained based on the self-attention mechanism; Based on the output features, an inference result is determined.
5. The method according to claim 4, characterized in that The object to be processed is an image of the area to be detected, and the inference result is whether there is abnormal behavior and / or abnormal object in the area to be detected; Alternatively, the object to be processed is a text, and the inference result is a translation of a character in the text; Alternatively, the object to be processed is a video, and the inference result is whether there is abnormal behavior in the video; Alternatively, the object to be processed is audio, and the inference result is whether there is abnormal language in the audio; Alternatively, the object to be processed is a plurality of images to be processed, and the inference result is a video generated based on the plurality of images to be processed.
6. A self-attention mechanism computing device, characterized in that: The device comprises: Matrix determination module: used to determine the query matrix, key matrix, and value matrix based on the object to be processed; the object to be processed is any one or more of an image, text, video, audio, millimeter wave signal, and radar signal; A matrix calculation module is used to transpose the key matrix to obtain a transposed key matrix; and calculate the product of the transposed key matrix and the query matrix as a first product matrix; and obtain a second product matrix using the natural constant e as the base and the first product matrix as the exponent; The matrix calculation module is further configured to, if the number of columns of the value matrix is less than the number of columns of the first product matrix, calculate the product of the second product matrix and the value matrix as a third product matrix; calculate the sum of the second product matrices as a matrix sum; and calculate the ratio of the third product matrix to the matrix sum as an output feature obtained based on the self-attention mechanism; If the number of columns of the value matrix is greater than the number of columns of the first product matrix, the sum of the second product matrix is calculated as the matrix sum; and the ratio of the second product matrix to the matrix sum is calculated as the fourth product matrix; and the product of the fourth product matrix and the value matrix is calculated as the output feature obtained based on the self-attention mechanism.
7. An inference device, characterized in that: The inference device comprises a first computing unit and a second computing unit. The first operation unit is used to determine a query matrix, a key matrix, and a value matrix based on an object to be processed; transpose the key matrix to obtain a transposed key matrix; and calculate the product of the query matrix and the transposed key matrix as a first product matrix; and send the first product matrix to the second operation unit; wherein the object to be processed includes any one or more of an image, text, video, audio, millimeter wave signal, and radar signal; The second operation unit is used to receive the first product matrix sent by the first operation unit; obtain a second product matrix with the natural constant e as the base and the first product matrix as the exponent; if the number of columns of the value matrix is greater than the number of columns of the first product matrix, calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the second product matrix to the matrix sum as a fourth product matrix, and send the fourth product matrix to the first operation unit; if the number of columns of the value matrix is less than the number of columns of the first product matrix, send the second product matrix to the first operation unit; The first operation unit is further used to receive the fourth product matrix sent by the second operation unit, calculate the product of the fourth product matrix and the value matrix as the output feature obtained based on the self-attention mechanism; or receive the second product matrix sent by the second operation unit; calculate the product of the second product matrix and the value matrix as a third product matrix; and send the third product matrix to the second operation unit; The second operation unit is also used to receive the third product matrix sent by the first operation unit; calculate the sum of the second product matrix as the matrix sum; and calculate the ratio of the third product matrix to the matrix sum as the output feature obtained based on the self-attention mechanism.
8. An electronic device, characterized in that: include: Memory, used to store computer programs; A processor, for implementing any of the methods described in claims 1-3 when executing a program stored in a memory.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 3 is implemented.
