Data processing method, computing device and storage medium
By grouping the three-dimensional vectors of the input sequence and utilizing multiple cores for calculation, the memory limitation problem of attention calculation for very long input sequences is solved and efficient attention calculation is achieved.
Patent Information
- Application Number
- CN202410954370.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-16
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2044-07-16
AI Technical Summary
Existing data processing methods are difficult to implement attention calculations on very long input sequences without increasing memory, and are limited by processor memory limitations.
By grouping the three-dimensional vectors (query vectors, key vectors, value vectors) of the input sequence into multiple three-dimensional vector groups, and using multiple kernels to perform attention-related calculations in sequence, using the process data of the previous kernel Initialize data for the next core to reduce data communication costs.
It achieves attention calculation for ultra-long input sequences without increasing the number of processors, reduces data communication costs, and improves computing efficiency.
Smart Images

Figure CN118503605B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention generally relate to the field of data processing, and more specifically to a data processing method, a computing device, and a storage medium. Background Art
[0002] Attention Mechanism is widely used in machine learning fields such as natural language processing (NLP) and computer vision (CV) to help models pay more attention to important information when processing complex tasks, thereby improving performance. Specifically, the attention mechanism enables the model to dynamically assign different weights to each element in the input sequence when processing the input sequence, so that the model can pay more attention to the information in the input sequence that is relevant to the current task.
[0003] However, in the attention mechanism, the length of the input sequence that the model can process is limited by the processor memory. In other words, without increasing the memory, existing data processing methods find it difficult to implement attention calculations for ultra-long input sequences. Summary of the invention
[0004] In response to the above problems, the present invention provides a data processing method, which enables attention calculation for ultra-long input sequences without increasing memory.
[0005] According to a first aspect of the present invention, a data processing method is provided, characterized in that it includes: determining a three-dimensional vector based on an input sequence, the three-dimensional vector including: a query vector, a key vector and a value vector; grouping at least one-dimensional vector in the three-dimensional vector so as to divide the three-dimensional vector into multiple three-dimensional vector groups; performing attention-related calculations using a current kernel and at least based on the current three-dimensional vector group to obtain current process data; and in response to obtaining the current process data, performing attention-related calculations using a next kernel and at least based on the current process data and the next three-dimensional vector group to obtain the next process data, until the final data is determined, so as to generate an attention output based on the final data, wherein the final data is the process data obtained by performing attention-related calculations based at least on the last three-dimensional vector group.
[0006] In some embodiments, grouping at least one-dimensional vectors in a three-dimensional vector includes: grouping at least one-dimensional vectors in a three-dimensional vector to obtain multiple sub-vectors of the at least one-dimensional vector; and grouping the three-dimensional vectors based at least on the multiple sub-vectors to obtain multiple three-dimensional vector groups.
[0007] In some embodiments, each three-dimensional vector grouping in the plurality of three-dimensional vector groups includes at least one sub-vector of each dimension of the grouped three-dimensional vector.
[0008] In some embodiments, each sub-vector is included in only one three-dimensional grouping of vectors among a plurality of three-dimensional groupings of vectors.
[0009] In some embodiments, the process data includes: first process data, second process data, and third process data. In these embodiments, obtaining the current process data includes: performing maximum value calculation on the query vector and the key vector in the current three-dimensional vector grouping to obtain the first process data; performing sum calculation on the query vector and the key vector in the current three-dimensional vector grouping to obtain the second process data; and performing product calculation on the query vector, the key vector, and the value vector in the current three-dimensional vector grouping to obtain the third process data.
[0010] In some embodiments, the process data includes: first process data, second process data and third process data, wherein obtaining the current process data includes: performing a maximum value calculation on the query vector and the key vector in the current three-dimensional vector grouping to obtain the first process data; performing a sum calculation on the query vector and the key vector in the current three-dimensional vector grouping to obtain the second process data; and performing a product calculation on the query vector, the key vector and the value vector in the current three-dimensional vector grouping to obtain the third process data.
[0011] In some embodiments, obtaining the current process data also includes: configuring the original process data in the current kernel, and initializing the original process data to obtain initialized original process data, wherein the initialized original process data includes: initialized first original process data, initialized second original process data, and initialized third original process data; and calculating the current process data based on the initialized original process data.
[0012] In some embodiments, obtaining the initialized original process data includes: in response to the current three-dimensional vector grouping being the first three-dimensional vector grouping, making the values of the initialized first original process data, the initialized second original process data and the initialized third original process data all 0; in response to the current three-dimensional vector grouping not being the first three-dimensional vector grouping, making the values of the initialized first original process data, the initialized second original process data and the initialized third original process data respectively equal to the values of the first process data, the second process data and the third process data of the previous process data.
[0013] In some embodiments, generating the attention output further comprises: calculating the gradient of each dimension of the three-dimensional vector for generating the attention output.
[0014] In some embodiments, calculating the gradient of each dimension of a three-dimensional vector includes: dividing a plurality of variables obtained based on an input sequence operation, the plurality of variables including: a gradient variable obtained by performing a gradient operation based on all three-dimensional vectors of the input sequence, and a correlation variable, wherein the correlation variable is determined based on the product of a query vector and a key vector via a predetermined function operation; and determining the gradient of each dimension of the three-dimensional vector based on the divided plurality of variables.
[0015] In some embodiments, dividing the multiple variables obtained based on the input sequence operation includes: dividing the gradient variables based on the number of sub-vectors of the query vector; and dividing the correlation variables based on the number of sub-vectors of the query vector and the number of sub-vectors of the key vector.
[0016] In some embodiments, the correlation variables include: a first correlation variable, which is determined by performing a normalized exponential function on the product of the query vector and the key vector; and a second correlation variable, which is determined by performing a transposition function based on the first correlation variable.
[0017] According to a second aspect of the present invention, a computing device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor so that the at least one processor can execute the method of the first aspect of the present invention.
[0018] According to a third aspect of the present invention, there is provided a non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to cause a computer to execute the method of the first aspect of the present invention.
[0019] According to a fourth aspect of the present invention, there is provided a computer program product tangibly stored on a non-transitory computer readable medium and comprising machine executable instructions which, when executed, cause a machine to perform the steps in the method of the first aspect of the present invention.
[0020] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] The above and other features, advantages and aspects of the embodiments of the present invention will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. In the accompanying drawings, the same or similar reference numerals represent the same or similar elements.
[0022] Figure 1A flow chart of a data processing method according to an embodiment of the present invention is shown.
[0023] FIG. 2A to FIG. 2C A schematic diagram showing an exemplary embodiment of a data processing method of the present invention.
[0024] Figure 3 A flow chart of a data processing method for reverse calculation according to an embodiment of the present invention is shown.
[0025] FIG. 4A to FIG. 4C A schematic diagram showing an exemplary embodiment of a data processing method of the present invention.
[0026] Figure 5 A block diagram of a computing device suitable for implementing embodiments of the present invention is schematically shown. DETAILED DESCRIPTION
[0027] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0028] As used herein, the term "including" and its variations mean open inclusion, i.e., "including but not limited to". The term "based on" means "based at least in part on". The terms "an example embodiment" and "an embodiment" mean "at least one example embodiment". The term "another embodiment" means "at least one additional embodiment". The terms "first", "second", etc. may refer to different or the same objects. Other explicit and implicit definitions may also be included below.
[0029] In the field of natural language processing, commonly used deep learning model architectures include: Transformer model based on the attention mechanism. Regarding the attention mechanism, it allows the model to focus on information at different positions in the input sequence when processing text, so as to help the model filter out information that is helpful to the model from a large amount of information. In other words, the core purpose of the attention mechanism is to filter out information that is more critical to the current task, so that attention can be focused on the filtered information.
[0030] According to the basic principle of the attention mechanism, a three-dimensional vector is usually generated based on the input sequence input into the model. The three-dimensional vector includes: the query vector Q , Key vector K , Value vector VOn this basis, based on the generated query vector Q , key vector K , value vector V , attention calculation can be performed according to the following formula (1) to calculate the corresponding attention weight and attention output;
[0031] (1)
[0032] in, Represents the key vector K Specifically, the query vector Q and key vector K Perform calculations such as scaling dot products to obtain the attention weight matrix, and then use the attention weight matrix to calculate the value vector V Perform weighted summation to obtain the attention output.
[0033] In practice, the aforementioned attention calculation can be performed on processors such as a graphics processing unit (GPU) and a general-purpose graphics processing unit (GPGPU). Taking the GPU as an example, the existing data processing method for attention calculation includes: reading the input data from the GPU's high bandwidth memory (HBM) to the GPU's static random access memory (SRAM), so as to perform attention calculation on the input data in the SRAM, and return the calculation result to the HBM. Usually, the above-mentioned data processing method for attention calculation is completed in one core of the GPU. Since a lot of time is consumed in this process to continuously exchange data between the GPU's HBM and SRAM, the calculation speed is slow and the efficiency is low. In addition, since the GPU's SRAM capacity is small, and the size of the memory space required for attention calculation increases quadratically with the growth of the length N of the input sequence, for input sequences exceeding the threshold length, the attention calculation cannot be realized due to the GPU memory limitation.
[0034] In summary, the shortcoming of existing data processing methods is that it is impossible to implement attention calculation for ultra-long input sequences without increasing memory.
[0035] In some embodiments, in order to be able to process input sequences exceeding a threshold length, the processor memory used for attention calculations can be increased by increasing the number of processors. That is, the attention calculation to be performed can be split into multiple parts, such as multiple parts equal to the number of processors (such as GPUs), so that each GPU is responsible for a part of the attention calculation, and then the final attention output is obtained based on the calculation results of each GPU. However, when the number of GPUs reaches the threshold number, due to the memory limitations of each GPU, the attention calculations that can be performed will also be limited, making it difficult to process longer input sequences, such as input sequences with a length of more than one million tokens.
[0036] In order to at least partially solve the above problems and one or more of other potential problems, an exemplary embodiment of the present invention proposes a data processing scheme. In the scheme, at least one-dimensional vectors in the three-dimensional vectors determined based on the input sequence are grouped so as to divide the three-dimensional vectors into multiple three-dimensional vector groups; the current kernel is used and at least based on the current three-dimensional vector grouping, the attention-related calculation is performed to obtain the current process data; and in response to obtaining the current process data, the next kernel is used and at least based on the current process data and the next three-dimensional vector grouping, the attention-related calculation is performed to obtain the next process data, so that multiple kernels of the same processor can be used to perform the attention-related calculation, so that each processor can process a longer input sequence, thereby realizing the execution of attention calculation for an ultra-long input sequence without increasing the number of processors.
[0037] The following will be combined Figures 1 to 4C The data processing scheme of the present invention is described. Figure 1 The flowchart of the data processing method 100 according to the embodiment of the present invention is shown. It should be understood that the method 100 may also include additional actions not shown and / or may omit the actions shown, and the scope of the present invention is not limited in this respect.
[0038] In step 102, a three-dimensional vector is determined based on an input sequence.
[0039] Regarding the three-dimensional vector, it refers to: query vector Q , key vector K Sum value vector V .
[0040] According to an embodiment of the present invention, a linear transformation may be performed on the input sequence to obtain a query vector corresponding to the input sequence. Q , key vector K Sum value vector V .
[0041] In step 104, at least one-dimensional vector in the three-dimensional vector is grouped so as to divide the three-dimensional vector into a plurality of three-dimensional vector groups.
[0042] Regarding grouping at least one-dimensional vectors in the three-dimensional vectors, it means that the query vector Q , key vector K Sum value vector V The at least one-dimensional vector in the three-dimensional vector is grouped to obtain multiple sub-vectors of the at least one-dimensional vector. For example, in some embodiments, only the query vector in the three-dimensional vector Q Group to get the query vector Q Multiple query subvectors of Q n In some other embodiments, the key vector in the three-dimensional vector may be K Sum value vector V Group to get the key vector K Multiple key subvectors of K n and the value vector V Multiple value subvectors of V n In some other embodiments, the query vector in the three-dimensional vector may also be Q , key vector K Sum value vector V are grouped to obtain multiple query sub-vectors Q n , multiple key subvectors K n and multiple value subvectors V n .
[0043] According to an embodiment of the present invention, the grouped vectors in the three-dimensional vector may include two or more sub-vectors. Q In some examples, the query vectors are grouped Q may include two query sub-vectors (such as the first query sub-vector Q 1 and the second query subvector Q 2 ); In yet another example, the grouped query vector Q may include three query sub-vectors (such as the first query sub-vector Q 1 , the second query subvector Q 2 and the third query subvector Q 3 ). The present invention does not limit this.
[0044] Further, according to an embodiment of the present invention, when grouping multidimensional vectors in a three-dimensional vector, the number of sub-vectors obtained by grouping each dimensional vector may be different or the same. K Sum value vector V In the grouping example, the key subvector obtained is K n The number of sub-vectors can be V n The same number of.
[0045] Regarding obtaining a plurality of three-dimensional vector groups, it may include: grouping the three-dimensional vectors based on at least a plurality of sub-vectors obtained by grouping at least one-dimensional vector in the three-dimensional vectors to obtain a plurality of three-dimensional vector groups.
[0046] For example, in the above example, only the query vector Q In the grouping example, suppose the query vector Q After grouping, we get the first query sub-vector Q 1 and the second query subvector Q 2 In this case, we can then base our query on the first subvector Q 1 and the second query subvector Q 2 The three-dimensional vectors are grouped to obtain a first three-dimensional vector group and a second three-dimensional vector group, wherein the first three-dimensional vector group includes: a first query subvector Q 1 , key vector K Sum value vector V ; The second three-dimensional vector grouping includes: the second query sub-vector Q 2 , key vector K Sum value vector V .
[0047] For example, in the above-mentioned three-dimensional vector key vector K Sum value vector V In another example of grouping, suppose that the key vector K After grouping, we get the first key sub-vector K 1 and the second bond subvector K 2 , the value vector V After grouping, we get the first value sub-vector V 1 and the second value subvector V 2In this case, we can then base our calculation on the first key subvector K 1 , the second key subvector K 2 , the first value subvector V 1 and the second value subvector V 2 The three-dimensional vectors are grouped and two three-dimensional vector groups are obtained, wherein the first three-dimensional vector group includes: the query vector Q , the first bond subvector K 1 and the first value subvector V 1 ; The second three-dimensional vector grouping includes: query vector Q , the second key subvector K 2 and the second value subvector V 2 .
[0048] From the above, it can be seen that in an embodiment of the present invention, each three-dimensional vector grouping may include at least one sub-vector of each dimensional vector grouped in the three-dimensional vector, and further, according to some embodiments of the present invention, each sub-vector is included in only one three-dimensional vector grouping among multiple three-dimensional vector groups.
[0049] In step 106, attention-related calculations are performed using the current kernel and based on at least the current three-dimensional vector grouping to obtain current process data.
[0050] Regarding process data, according to an embodiment of the present invention, it may include first process data (Max) calculated based on a maximum value, second process data (Sum) calculated based on a summation, and third process data (Q') calculated based on a vector product.
[0051] Specifically, according to an embodiment of the present invention, obtaining the current process data may include: performing a maximum value calculation on the query vector and the key vector in the current three-dimensional vector grouping so as to obtain the first process data; performing a sum calculation on the query vector and the key vector in the current three-dimensional vector grouping so as to obtain the second process data; and performing a product calculation on the query vector, the key vector and the value vector in the current three-dimensional vector grouping so as to obtain the third process data.
[0052] Group the current 3D vector into a group including the query vector Q , the first bond subvector K 1 and the first value subvector V 1As an example, according to an embodiment of the present invention, the process data of obtaining the current three-dimensional vector grouping may include: for the query vector Q and the first bond subvector K 1 Perform maximum value calculation to obtain the first process data Max1; for the query vector Q and the first bond subvector K 1 Perform a sum calculation to obtain the second process data Sum1; and for the query vector Q , the first bond subvector K 1 and the first value subvector V 1 The product calculation is performed to obtain the third process data Q1*. That is, in this example, for the query vector Q , the first bond subvector K 1 and the first value subvector V 1 A set of process data obtained by grouping three-dimensional vectors includes: based on the query vector Q and the first bond subvector K 1 The first process data Max1 is obtained by calculating the maximum value of the query vector Q and the first bond subvector K 1 The second process data Sum1 is obtained by summing up the query vector Q , the first bond subvector K 1 and the first value subvector V 1 The third process data Q1' is obtained by calculating the vector product of .
[0053] Further, in order to calculate the process data of the next three-dimensional vector grouping based on the process data calculated for the current three-dimensional vector grouping, according to the inventive concept of the present invention, in each kernel, original process data can be pre-configured and initialized, so that the current process parameters can then be calculated based on at least the original process data and the current three-dimensional vector grouping in the current kernel. Specifically, according to an embodiment of the present invention, obtaining the current process data may also include: configuring the original process data in the current kernel, and initializing the original process data to obtain initialized original process data; and calculating the current process data based on the initialized original process data.
[0054] As for the original process data, similar to the process data, it includes: first original process data (Max0), second original process data (Sum0) and third original process data (Q0').
[0055] The initialized raw process data includes: initialized first raw process data, initialized second raw process data and initialized third raw process data.
[0056] According to an embodiment of the present invention, in response to the current three-dimensional vector grouping being the first three-dimensional vector grouping, the values of the initialized first original process data, the initialized second original process data and the initialized third original process data are all 0; in response to the current three-dimensional vector grouping not being the first three-dimensional vector grouping, the values of the initialized first original process data, the initialized second original process data and the initialized third original process data are respectively equal to the values of the first original process data, the second original process data and the third original process data of the previous process data.
[0057] For example, assuming that the three-dimensional vector grouping in the current kernel is the first three-dimensional vector grouping among multiple three-dimensional vector groups, the original process data corresponding to the current kernel can be initialized so that the values of the initialized first original process data, the initialized second original process data, and the initialized third original process data are all initialized to 0, that is,
[0058] Max0 = 0
[0059] Sum0 = 0
[0060] Q0' = 0
[0061] Assuming that the three-dimensional vector grouping in the current kernel is not the first three-dimensional vector grouping among multiple three-dimensional vector groups, that is, before performing attention-related calculations on the three-dimensional vector grouping in the current kernel, attention-related calculations on at least one other three-dimensional vector grouping have been performed, the original process data of the current kernel can be initialized based on the calculated last process data. If the calculated last process data includes the first process data Max1, the second process data Sum1, and the third process data Q1', the values of the first original process data Max0, the second original process data Sum0, and the third original process data Q0' in the original process data of the current kernel can be initialized to be the same as the values of the first process data Max1, the second process data Sum1, and the third process data Q1' in the last process data, that is,
[0062] Max0 = Max1
[0063] Sum0 = Sum1
[0064] Q0' = Q1'
[0065] In summary, according to the inventive concept of the present invention, multiple sub-vectors can be obtained by grouping at least one-dimensional vectors in the three-dimensional vector, and the three-dimensional vector can be divided based on these sub-vectors, so that the three-dimensional vector can be divided into multiple three-dimensional vector groups. On this basis, these three-dimensional vector groups can then be assigned to different computing cores so as to perform attention-related calculations on these three-dimensional vector groups in turn, wherein, by loading and initializing process data in each core, it is avoided to load the complete three-dimensional vector and all intermediate data related to the input sequence in each core, thereby significantly reducing the data communication cost.
[0066] In step 108 , in response to obtaining the current process data, a calculation related to attention is performed using a next kernel and based on at least the current process data and the next three-dimensional vector grouping to obtain next process data.
[0067] According to an embodiment of the present invention, before using the next kernel and performing attention-related calculations based on the next three-dimensional vector grouping, as described above, the original process data of the next kernel needs to be initialized using the current process data, so that the values of the first original process data, the second original process data, and the third original process data in the original process data of the next kernel are respectively equal to the first process data, the second process data, and the third process data in the current process data, and then the attention-related calculations are performed based on the initialized original process data and the next three-dimensional vector grouping, so as to calculate and obtain the next process data based on the current process data. And so on, until all the attention-related calculations of the three-dimensional vector groupings are completed, and the last set of process data (hereinafter referred to as "final data") is obtained.
[0068] For example, in some embodiments, the three-dimensional vectors determined based on the input sequence are divided into M three-dimensional vector groups (M is an integer greater than 1), including the first three-dimensional vector group, the second three-dimensional vector group to the Mth three-dimensional vector group. According to the inventive concept of the present invention, the attention-related calculations can be performed based on the process data Max1, Sum1 and Q1' calculated for the first three-dimensional vector group and the second three-dimensional vector group to obtain the process data Max2, Sum2 and Q2'; similarly, the attention-related calculations can be performed based on the process data Max2, Sum2 and Q2' calculated for the second three-dimensional vector group and the third three-dimensional vector group to obtain the process data Max3, Sum3 and Q3', until the final data MaxM, SumM and QM' are calculated.
[0069] According to an embodiment of the present invention, final data may then be determined based on the last set of process data, so as to generate the attention output based on the final data.
[0070] As for the final data, as described above, it is process data obtained by performing calculations related to attention based on the last three-dimensional vector grouping. According to an embodiment of the present invention, the final data may include first data, second data, and third data, wherein the first data is first process data in process data obtained by performing calculations related to attention based on the last three-dimensional vector grouping, the second data is second process data in process data obtained by performing calculations related to attention based on the last three-dimensional vector grouping, and the third data is third process data in process data obtained by performing calculations related to attention based on the last three-dimensional vector grouping.
[0071] Regarding generating the attention output based on the final data, it may refer to calculating the final result (Final_R) related to the attention output based on the second data SumM and the third data QM' in the final data according to the following formula (2);
[0072] (2)
[0073] In summary, the present invention divides the three-dimensional vector determined based on the input sequence into multiple three-dimensional vector groups, and sequentially distributes these three-dimensional vector groups to different kernels in multiple kernels for attention-related calculations; and introduces process data in each kernel, initializes the original process data of the current kernel based on the process data calculated in the previous kernel, and uses the initialized process data and the three-dimensional vector groups in the current kernel to perform attention-related calculations, so that in the process of performing a complete attention calculation, no additional memory and additional merging operations for intermediate data are required, and the final result of the attention calculation can be obtained. Therefore, the present invention reasonably divides the ultra-long input sequence and uses multiple kernels of the same processor to perform attention-related calculations, so that a single processor can better process longer input sequences, thereby realizing the execution of attention calculations for ultra-long input sequences without increasing the number of processors.
[0074] As mentioned above, since the data needs to be read into SRAM for attention calculation, and the capacity of SRAM is usually small, according to some embodiments of the present invention, each vector in the three-dimensional vector grouping written into each core for attention-related calculation can be further tiled (Tiling), and then the three-dimensional vector grouping can be divided according to the tiled vectors to further divide the three-dimensional vector grouping into multiple small groups. On this basis, the process data of all small groups are traversed and calculated in an iterative manner, so as to calculate the process data corresponding to the three-dimensional vector grouping.
[0075] For example, in one example, based on the block operation on the vector, the three-dimensional vector group is divided into two groups, which are recorded as the first group and the second group, wherein each group includes a respective three-dimensional vector q , k , v For the sake of clarity, the three-dimensional vector of the first group is recorded as q 1 , k 1 , v 1 , the three-dimensional vector of the second group is recorded as q 2 , k 2 , v 2 In this example, according to the inventive concept of the present invention, the three-dimensional vectors of the first group can be firstly q 1 , k 1 , v 1 Performing attention-related calculations and determining first intermediate process data corresponding to the first group. In response to determining the intermediate process data corresponding to the first group, the three-dimensional vector based on the second group is then recorded as q 2 , k 2 , v 2 Perform calculations related to attention, and calculate the second intermediate process data based on the first intermediate process data, where the second intermediate process data can be regarded as process data corresponding to the three-dimensional vector grouping. The specific parameter calculation method can be found in the previous text, which will not be repeated here.
[0076] The following will be combined FIG. 2A to FIG. 2C Exemplary embodiments of the data processing scheme of the present invention are described.
[0077] FIG. 2A to FIG. 2CA schematic diagram showing an exemplary embodiment of a data processing method of the present invention.
[0078] like Figure 2A As shown, the three-dimensional vector determined based on the input sequence includes: the query vector Q , key vector K Sum value vector V . Further, in Figure 2A In the embodiment shown, the key vector K Sum value vector V are divided into two groups of sub-vectors, namely the key vectors K is divided into the first bond subvector K 1 and the second bond subvector K 2 , value vector V is divided into first value sub-vectors V 1 and the second value subvector V 2 . Further, the query vector Q , the first bond subvector K 1 and the first value subvector V 1 The three-dimensional vector grouping composed of Q , the second key subvector K 2 and the second value subvector V 2 The composed three-dimensional vector group is regarded as the second three-dimensional vector group.
[0079] like Figure 2B As shown, the first kernel kernel_1 can be used to group based on the first three-dimensional vector (ie, the query vector Q , the first bond subvector K 1 and the first value subvector V 1 ) calculates the first group of process data, namely Max1, Sum1 and Q1'. Specifically, according to an embodiment of the present invention, a group of original process data can be configured in the first kernel kernal_1, and the group of original process data includes first original process data Max0, second original process data Sum0 and third original process data Q0'. Considering that the three-dimensional vector grouping to be calculated related to attention in the first kernel kernel_1 is the first three-dimensional vector grouping, the original process data can be initialized so that Max0 = 0, Sum0 = 0, Q0' = 0. Then, based on the initialized original process data Max0, Sum0 and Q0' and the query vectorQ , the first bond subvector K 1 and the first value subvector V 1 Max1, Sum1 and Q1' are calculated.
[0080] like Figure 2C As shown, the second kernel kernal_2 can be used to group based on the second three-dimensional vector (ie, the query vector Q , the second key subvector K 2 and the second value subvector V 2 ) calculates the second group of process data, namely Max2, Sum2 and Q2'. Specifically, according to an embodiment of the present invention, a group of original process data can be configured in the second kernel kernal_2, and the group of original process data includes the first original process data Max0, the second original process data Sum0 and the third original process data Q0'. Since the three-dimensional vector grouping to be calculated related to attention in the second kernel kernal_2 is the second three-dimensional vector grouping, rather than the first three-dimensional vector grouping, the three-dimensional vector grouping is used in the second kernel kernal_2. Figure 2B The original process data is initialized with Max1, Sum1, and Q1' calculated in , so that Max0 = Max1, Sum0 = Sum1, and Q0' = Q1'. Then, based on the initialized original process data Max0, Sum0, and Q0' (equivalent to Max1, Sum1, and Q1') and the query vector Q , the second key subvector K 2 and the second value subvector V 2 Max2, Sum2 and Q2' are calculated.
[0081] Further, in FIG. 2A to FIG. 2C In the example shown, the second three-dimensional vector grouping is the last three-dimensional vector grouping, and therefore, the second set of process data Max2, Sum2 and Q2' is the final data. According to an embodiment of the present invention, the third kernel can then be used to generate an attention output based on the final data. Specifically, the final result (Final_R) for generating the attention output can be determined based on the second data in the final data (that is, the second process data Sum2 in the second set of process data) and the third data (that is, the third process data Q2' in the second set of process data) according to formula (2) shown above, that is,
[0082] Final_R = Q2' / Sum2
[0083] Generally, the above detailed description of the forward calculation part related to attention calculation based on the data processing scheme of the present invention is implemented. According to the inventive concept of the present invention, in such as Figure 1 Based on the data processing scheme of the data processing method 100 shown in FIG. 1 , the gradient of the three-dimensional vector determined based on the input sequence can also be calculated to implement the reverse calculation part related to the attention calculation. Specifically, according to an embodiment of the present invention, the gradient of each dimensional vector in the three-dimensional vector determined based on the input sequence can also be calculated to generate the attention output. Figure 3 Detailed description.
[0084] Figure 3 The flowchart of the data processing method 300 for reverse calculation according to the embodiment of the present invention is shown. It should be understood that the method 300 may also include additional actions not shown and / or may omit the actions shown, and the scope of the present invention is not limited in this respect.
[0085] In step 302, multiple variables obtained based on input sequence operations are divided, and the multiple variables include: gradient variables and correlation variables.
[0086] The gradient variable is a variable obtained by performing a gradient operation on all three-dimensional vectors of the input sequence.
[0087] Regarding the relevance variable, it is based on the query vector Q and key vector K The product of is determined by a predetermined function operation, where the query vector Q and key vector K The two-dimensional vectors in the three-dimensional vector are determined based on the input sequence. According to an embodiment of the present invention, the correlation variable further includes: a first correlation variable and a second correlation variable, the first correlation variable is for the query vector Q and key vector K The product of is determined by performing a normalized exponential function (also known as a Softmax function), and the second correlation variable is determined by performing a transposition function based on the first correlation variable.
[0088] According to an embodiment of the present invention, dividing the multiple variables obtained based on the input sequence operation may include: based on the query vector in the forward calculation Q The number of sub-vectors is used to divide the gradient variables; and based on the query vector in the forward calculation Q The number of subvectors and the key vector in the forward calculation K The number of sub-vectors is used to divide the relevant variables. For example, in the forward calculation process, the query vector Q is divided into two sub-vectors, namely the first query sub-vector Q 1and the second query subvector Q 2 , and the key vector K is divided into two sub-vectors, namely the first key sub-vector K 1 and the second bond subvector K 2 , then the gradient variable is divided into two parts, and the correlation variable is divided into four parts. In another example, if in the forward calculation process, the query vector Q is not grouped, and the key vector K is divided into two sub-vectors, the gradient variable is not divided, and the correlation variable is divided into two parts. Similarly, if in the forward calculation process, the query vector Q is divided into two sub-vectors, and the key vector K is divided into three sub-vectors, the gradient variable is divided into two parts, and the correlation variable is divided into six parts.
[0089] In step 304 , based on the divided plurality of variables, a gradient of each dimensional vector in the three-dimensional vector is determined.
[0090] According to an embodiment of the present invention, intermediate results related to the gradient of the same vector can be calculated based on different combinations of the divided multiple variables, and then at least one of addition and connection operations can be performed on the intermediate results to determine the gradient of the vector.
[0091] FIG. 4A to FIG. 4C FIG. 1 is a schematic diagram showing an exemplary embodiment of the data processing method of the present invention. It should be noted that in this embodiment, in the preceding forward calculation process, the query vector determined based on the input sequence Q and key vector K Were divided into two groups.
[0092] Figure 4A shows the calculated value vector V Schematic diagram of the gradient.
[0093] like Figure 4A As shown, the gradient variable 401A obtained based on the input sequence operation can be divided into two parts: O _1 and d O _2, and for the query vector Q and key vector K The product of (denoted as 402A) is divided into four parts QK_1, QK_2, QK_3 and QK_4. Then, based on the divided gradient variable 401A and the divided query vector Q and key vector K The product of can be calculated and the value vector VThe intermediate result d related to the gradient V _1, d V _2, d V _3 and d V _4.
[0094] On this basis, we can V _1, d V _2, d V _3 and d V _4 Perform at least one of an addition and a concatenation operation to determine a value vector V For example, we can calculate the gradient of d V _1 and d V _2 performs an addition operation to obtain the first addition result d V _12; for d V _3 and d V _4 performs an addition operation to obtain a second addition result d V _34; and the first addition result d V _12 and the second addition result d V _34 performs a connection operation to obtain a value vector V The gradient d V。
[0095] Figure 4B shows the calculation of the key vector K Schematic diagram of the gradient.
[0096] like Figure 4B As shown, the query vector Q (denoted as 401B) is divided into two parts Q _1 and Q _2, for the query vector Q and key vector K The correlation variable 402B determined by performing the Softmax function on the product of S _1, d S _2, d S _3 and d S _4. Then, based on the partitioned query vector Q and the partitioned correlation variable 402B, the resulting and value vector can be calculated K The intermediate result d related to the gradient K _1, d K _2, d K _3 and d K _4.
[0097] On this basis, we can K _1, d K _2, dK _3 and d K _4 Perform at least one of an addition and a concatenation operation to determine a value vector K For example, we can calculate the gradient of d K _1 and d K _2 performs an addition operation to obtain the first addition result d K _12; for d K _3 and d K _4 performs an addition operation to obtain a second addition result d K _34; and the first addition result d K _12 and the second addition result d K _34 performs a connection operation to obtain a value vector K The gradient d K。
[0098] Figure 4C The calculation of the query vector Q Schematic diagram of the gradient.
[0099] like Figure 4C As shown, the key vector K (denoted as 401C) is divided into two parts K _1 and K _2, for the query vector Q and key vector K The correlation variable 402C determined by performing the Softmax function on the product of and then performing the transposition function is divided into four parts d S’ _1, d S’ _2, d S’ _3 and d S’ _4. Then, based on the partitioned key vector K and the partitioned correlation variable 402C, the resulting and value vector can be calculated Q The intermediate result d related to the gradient Q _1, d Q _2, d Q _3 and d Q _4.
[0100] On this basis, we can Q _1, d Q _2, d Q _3 and d Q _4 Perform at least one of an addition and a concatenation operation to determine a value vector Q For example, we can calculate the gradient of d Q _1 and d Q _2 performs an addition operation to obtain the first addition result d Q _12; for d Q_3 and d Q _4 performs an addition operation to obtain a second addition result d Q _34; and the first addition result d Q _12 and the second addition result d Q _34 performs a connection operation to obtain a value vector Q The gradient d Q。
[0101] In summary, the data processing scheme of the present invention enables a single processor to handle attention calculations for ultra-long input sequences, reducing the communication cost between processors. In addition, the scheme does not require additional memory or additional merging operations for the kernel to realize forward and backward calculations of attention calculations.
[0102] Figure 5 The block diagram of a computing device 500 suitable for implementing an embodiment of the present invention is schematically shown. The device 500 may be a computer program product for implementing an embodiment of the present invention. Figure 1 The method 100 and Figure 3 The method 300 is shown in the apparatus. Figure 5 As shown, the device 500 includes a central processing unit (CPU) 501, which can perform various appropriate actions and processes according to computer program instructions stored in a read-only memory (ROM) 502 or computer program instructions loaded from a storage unit 508 to a random access memory (RAM) 503. In the RAM 503, various programs and data required for the operation of the device 500 can also be stored. The CPU 501, the ROM 502, and the RAM 503 are connected to each other via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0103] Multiple components in the device 500 are connected to the I / O interface 505, including: an input unit 506, an output unit 507, and a storage unit 508. The processing unit 501 performs the various methods and processes described above, such as performing method 100 or method 300. For example, in some embodiments, method 100 or method 300 can be implemented as a computer software program, which is stored in a machine-readable medium, such as storage unit 508. In some embodiments, part or all of the computer program can be loaded and / or installed on the device 500 via ROM 502 and / or communication unit 509. When the computer program is loaded into RAM 503 and executed by CPU 501, one or more operations of method 100 or method 300 described above can be performed. Alternatively, in other embodiments, CPU 501 can be configured to perform one or more actions of method 100 and / or method 300 in any other appropriate manner (for example, by means of firmware).
[0104] The computer-readable program instructions described herein can be downloaded from a computer-readable storage medium to each computing / processing device, or downloaded to an external computer or external storage device via a network, such as the Internet, a local area network, a wide area network, and / or a wireless network. The network can include copper transmission cables, optical fiber transmissions, wireless transmissions, routers, firewalls, switches, gateway computers, and / or edge servers. The network adapter card or network interface in each computing / processing device receives the computer-readable program instructions from the network and forwards the computer-readable program instructions for storage in the computer-readable storage medium in each computing / processing device.
[0105] The computer program instructions for performing the operation of the present invention may be assembly instructions, instruction set architecture (ISA) instructions, machine instructions, machine-dependent instructions, microcode, firmware instructions, state setting data, or source code or object code written in any combination of one or more programming languages, including object-oriented programming languages, such as Smalltalk, C++, etc., and conventional procedural programming languages, such as "C" language or similar programming languages. The computer-readable program instructions may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer, partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., using an Internet service provider to connect through the Internet). In some embodiments, by using the state information of the computer-readable program instructions to personalize an electronic circuit, such as a programmable logic circuit, a field programmable gate array (FPGA), or a programmable logic array (PLA), the electronic circuit may execute the computer-readable program instructions, thereby implementing various aspects of the present invention.
[0106] These computer-readable program instructions can be provided to a processor in a voice interaction device, a general-purpose computer, a special-purpose computer, or a processing unit of other programmable data processing devices, thereby producing a machine, so that when these instructions are executed by the processing unit of a computer or other programmable data processing device, a device that implements the functions / actions specified in one or more boxes in the flowchart and / or block diagram is generated. These computer-readable program instructions can also be stored in a computer-readable storage medium, and these instructions cause the computer, programmable data processing device, and / or other device to work in a specific manner.
[0107] The embodiments of the present invention have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The selection of terms used herein is intended to best explain the principles of the embodiments, practical applications, or technical improvements in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.
[0108] The above are only optional embodiments of the present invention and are not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A data processing method, characterized in that: include: Determine a three-dimensional vector based on the input sequence, the three-dimensional vector comprising: a query vector, a key vector, and a value vector; Grouping at least one-dimensional vector in the three-dimensional vector to obtain a plurality of sub-vectors of the at least one-dimensional vector; grouping the three-dimensional vectors based at least on the plurality of sub-vectors to obtain the plurality of three-dimensional vector groups, wherein each sub-vector is included in only one three-dimensional vector group among the plurality of three-dimensional vector groups; Performing attention-related calculations using the current kernel and based on at least the current three-dimensional vector grouping to obtain current process data; and In response to obtaining the current process data, attention-related calculations are performed using the next kernel and based at least on the current process data and the next three-dimensional vector grouping to obtain the next process data until the final data is determined, so as to generate the attention output based on the final data, wherein the final data is process data obtained by performing attention-related calculations based at least on the last three-dimensional vector grouping.
2. The method according to claim 1, characterized in that The process data includes: first process data, second process data and third process data, Wherein, obtaining the current process data includes: Performing maximum value calculation on the query vector and the key vector in the current three-dimensional vector grouping, so as to obtain the first process data; performing a sum calculation on the query vector and the key vector in the current three-dimensional vector group to obtain the second process data; and A product calculation is performed on the query vector, the key vector and the value vector in the current three-dimensional vector group to obtain the third process data.
3. The method according to claim 2, characterized in that Obtaining the current process data further includes: configuring original process data in the current kernel, and initializing the original process data to obtain initialized original process data, wherein the initialized original process data includes: initialized first original process data, initialized second original process data, and initialized third original process data; and The current process data is calculated based on the initialized original process data.
4. The method according to claim 3, characterized in that The initialized raw process data includes: In response to the current three-dimensional vector group being the first three-dimensional vector group, the values of the initialized first original process data, the initialized second original process data, and the initialized third original process data are all 0; In response to the current three-dimensional vector grouping not being the first three-dimensional vector grouping, the values of the initialized first original process data, the initialized second original process data, and the initialized third original process data are respectively equal to the values of the first process data, the second process data, and the third process data of the previous process data.
5. The method according to claim 1, characterized in that Generating the attention output further includes: The gradient of each dimension of the three-dimensional vector is calculated to generate the attention output.
6. The method according to claim 5, characterized in that Calculating the gradient of each dimension of the three-dimensional vector includes: Dividing a plurality of variables obtained by operation based on an input sequence, the plurality of variables comprising: a gradient variable obtained by performing a gradient operation based on all three-dimensional vectors of the input sequence, and a correlation variable, wherein the correlation variable is determined by performing a predetermined function operation based on the product of the query vector and the key vector; and Based on the divided plurality of variables, a gradient of each dimensional vector in the three-dimensional vector is determined.
7. The method according to claim 6, characterized in that Partitioning multiple variables based on input sequence operations includes: partitioning the gradient variable based on the number of subvectors of the query vector; and The relevance variable is partitioned based on the number of sub-vectors of the query vector and the number of sub-vectors of the key vector.
8. The method according to claim 6, characterized in that The correlation variables include: a first relevance variable, the first relevance variable being determined by performing a normalized exponential function on a product of the query vector and the key vector; and A second correlation variable is determined by executing a transposition function based on the first correlation variable.
9. A computing device comprising: at least one processor; as well as a memory communicatively coupled to the at least one processor; The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 8.
10. A non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to cause a computer to execute the method of any one of claims 1 to 8.
11. A computer program product, characterized in that The computer program product is tangibly stored on a non-transitory computer readable medium and comprises machine executable instructions which, when executed, cause a machine to perform the steps of the method according to any one of claims 1-8.
Citation Information
Patent Citations
Method and device for performing attention operation, and storage medium
CN117707791A
