Method, computing device, medium and program product for matrix reduction computing
By dividing the input matrix into sub-matrices and allocating threads to calculate contribution values, the problem of low computational efficiency of traditional matrix reduction is solved and the performance of matrix operators is significantly improved.
Patent Information
- Application Number
- CN202410947021.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-15
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2044-07-15
AI Technical Summary
The traditional matrix reduction calculation method is inefficient and difficult to significantly improve the performance of matrix operators, especially when the matrix scaling ratio is large, which will lead to performance degradation.
Divide the input matrix into multiple input sub-matrices, map each output matrix element to a corner position of the corresponding input sub-matrix, allocate threads to calculate the contribution values of all elements in each input sub-matrix, and store the contribution values in the shared Different locations in memory are accumulated for the same output matrix elements.
It significantly reduces the number of reads of input matrix elements, avoids thread read and write conflicts and performance losses caused by atomic instructions, and improves the efficiency of matrix reduction calculations and the performance of matrix operators.
Smart Images

Figure CN118484322B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention generally relate to the field of artificial intelligence computing, and more specifically to a method, computing device, computer-readable storage medium, and computer program product for matrix reduction computing. Background Art
[0002] In the process of multi-threaded programming of AI operators on the GPU platform, it is very common to reduce multiple data to one data. For example, in matrix calculations, reduction operations are usually required, that is, reducing the value of an input matrix to an element of the output matrix. In particular, due to the characteristics of matrix operations, these input matrices that need to be reduced are usually more regular. It should be understood that matrix reduction calculations usually cause competition problems between multiple threads, that is, multiple threads need to operate the same resource at the same time. Since the above operations themselves are non-atomic, data errors will occur.
[0003] To avoid the above problems, the traditional method for matrix reduction calculations provides some atomic instructions through hardware to ensure the atomicity of the operation to a certain extent, in order to avoid competition between multiple threads. However, these atomic instructions are usually limited, and in order to ensure the atomicity of the operation, the efficiency of atomic instructions is much lower than that of conventional memory access instructions.
[0004] Take the common upsample operator as an example. This operator is mainly used for matrix scaling. Among them, the forward (FWD) calculation is usually reflected as enlarging the matrix, and the backward (BWD) calculation is usually reflected as reducing the matrix. Take the case of direct scaling as an example. There are two traditional methods for matrix reduction calculation. The first method mainly includes: each thread reads the value of the element of the input matrix, calculates the contribution to each element in the range of the output matrix, and then accumulates the contribution value to the output matrix element through atomic instructions. The second method mainly includes: each thread traverses based on the elements of the output matrix, reads the value of the element of the input matrix in the range, and after calculating the contribution, accumulates it to the element of the output matrix. The first method will result in low efficiency of matrix reduction calculation, and affect the performance improvement of matrix operators because the application of atomic instructions is limited. The second method will result in the efficiency of matrix reduction calculation being reduced due to the delay caused by reading the elements of the input matrix from the global memory multiple times, especially when the matrix scaling ratio is large, which will cause a significant performance degradation of the matrix operator.
[0005] In summary, the shortcomings of traditional methods for matrix reduction calculations are that the efficiency of matrix reduction calculations is low and it is difficult to significantly improve the performance of matrix operators. Summary of the invention
[0006] The present invention provides a method, a computing device, a computer-readable storage medium and a computer program product for matrix reduction computing, which can improve the efficiency of matrix reduction computing and significantly enhance the performance of matrix operators.
[0007] According to a first aspect of the present invention, a method for matrix reduction calculation is provided. The method comprises: dividing an input matrix into a plurality of input sub-matrices, so that each output matrix element is mapped to an angular position of the corresponding input sub-matrix, and each input sub-matrix contains a plurality of input matrix elements within its range; allocating threads based on the divided plurality of input sub-matrices, so that each thread calculates the contribution values of all input matrix elements in an input sub-matrix to the output matrix elements corresponding to the four angular positions of the input sub-matrix, thereby storing the plurality of contribution values generated for each output matrix element to different storage locations in a shared memory; accumulating the plurality of contribution values for the same output matrix element to generate a contribution cumulative value for the output matrix element; and storing the contribution cumulative value for the output matrix element to the storage location of the shared memory corresponding to the output matrix element.
[0008] According to a second aspect of the present invention, a computing device is also provided. The computing device comprises: at least one processor; and a memory in communication connection with the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the computing device can perform the method of the first aspect of the present invention.
[0009] According to a third aspect of the present invention, a computer-readable storage medium is provided, wherein a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a machine, the method of the first aspect of the present invention is executed.
[0010] According to a fourth aspect of the present invention, there is further provided a computer program product, comprising a computer program, wherein when the computer program is executed by a machine, the method of the first aspect of the present invention is performed.
[0011] In some embodiments, accumulating multiple contribution values for the same output matrix element to generate a contribution accumulation value for the output matrix element includes: determining whether a predetermined condition is met, the predetermined condition including any one of the following: the shared memory is full; or the contribution values are calculated for a predetermined number of input sub-matrices; in response to the predetermined condition being met, performing thread synchronization; and reallocating thread tasks in units of output matrix elements so that each thread will accumulate multiple contribution values for the same output matrix element, thereby generating a contribution accumulation value for the output matrix element.
[0012] In some embodiments, each thread accumulates multiple contribution values for the same output matrix element, including: traversing each output matrix element through multiple threads so that each thread reads multiple contribution values of an output matrix element from a shared memory; and accumulating the read multiple contribution values to generate a contribution accumulation value for the output matrix element.
[0013] In some embodiments, the predetermined number is the number of all input sub-matrices, or the number of part of the input sub-matrices.
[0014] In some embodiments, dividing the input matrix into a plurality of input sub-matrices includes: dividing the output matrix into a plurality of output sub-matrices so that each output matrix element is located at a corner position of the output sub-matrix; and dividing the input matrix into a plurality of input sub-matrices based on the division manner of the output sub-matrix so that each output matrix element is mapped to a corner position of the corresponding input sub-matrix.
[0015] In some embodiments, the multiple contribution values generated for each output matrix element include: a plurality of contribution values respectively calculated for input matrix elements of a plurality of adjacent input sub-matrices with each output matrix element as a common corner point relative to the output matrix element.
[0016] In some embodiments, the plurality of contribution values are four contribution values for a same output matrix element and are stored in four different storage locations in the shared memory.
[0017] 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
[0018] 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.
[0019] Figure 1The figure schematically shows a schematic diagram of a computing device implementing a method for matrix reduction computing according to an embodiment of the present invention.
[0020] Figure 2 A flow chart of a method for matrix reduction calculation according to an embodiment of the present invention is shown.
[0021] Figure 3 A schematic diagram of an input sub-matrix according to an embodiment of the present invention is shown.
[0022] Figure 4 A schematic diagram for calculating multiple contribution values of output matrix elements according to an embodiment of the present invention is shown.
[0023] Figure 5 A flow chart of a method for generating a contribution accumulation value for an output matrix element according to an embodiment of the present invention is shown.
[0024] In the various drawings, the same or corresponding reference numerals represent the same or corresponding parts. DETAILED DESCRIPTION
[0025] The preferred embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although the preferred embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided to make the present invention more thorough and complete, and to fully convey the scope of the present invention to those skilled in the art.
[0026] As used herein, the term "including" and its variations mean open inclusion, i.e., "including but not limited to". Unless otherwise stated, the term "or" means "and / or". 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.
[0027] As described above, there are mainly two traditional methods for matrix reduction calculation.
[0028] In the first method, the use of atomic instructions to accumulate contributions to the output matrix elements can avoid the problem of competition between multiple threads; however, it should be understood that the efficiency of atomic instructions is lower than the normal memory access efficiency, and the calculations that support atomic instructions are relatively limited. For example, it does not support multiplication and needs to be completed by converting logarithms into additions. Therefore, the first method will result in low efficiency in matrix reduction calculations and affect the performance improvement of matrix operators due to limited applicable scenarios.
[0029] In the second method, since one thread reads the values of the input matrix elements within the range, calculates the contribution, and accumulates the values for each element of the output matrix, the competition problem between multiple threads can be avoided. However, since the input matrix data needs to be read multiple times, for example, most of the elements of the input matrix that do not fall on the output matrix elements will be read repeatedly multiple times (for example, 4 times), and the input matrix is large, and it is usually stored in the global memory space. It should be understood that the memory access efficiency of the global memory is low, and the input matrix stored in the global memory has many elements and is read multiple times, so the efficiency of the matrix reduction calculation is difficult to improve due to the delay of reading, especially when the matrix scaling ratio is large, it will cause obvious performance degradation.
[0030] In summary, the shortcomings of traditional matrix reduction calculation methods are: the efficiency of matrix reduction calculation is low, and it is difficult to significantly improve the performance of matrix operators.
[0031] In order to at least partially solve one or more of the above problems and other potential problems, an exemplary embodiment of the present invention proposes a method for matrix reduction calculation. In the method, by dividing the input matrix into multiple input sub-matrices, each output matrix element is mapped to an angular position of the corresponding input sub-matrix, and each input sub-matrix contains multiple input matrix elements within its range; and threads are allocated based on the multiple divided input sub-matrices, so that each thread calculates the contribution values of all input matrix elements in an input sub-matrix to the output matrix elements corresponding to the four angular positions of the input sub-matrix, thereby storing multiple contribution values generated for each output matrix element in different storage bits in the shared memory; since all elements of each input sub-matrix of the present invention are read by one thread, each input matrix element is read once, thereby significantly reducing the number of reads for the input matrix elements; in addition, by separating multiple contribution values for each output matrix element through shared memory, the present invention not only avoids possible thread read-write conflicts, but also avoids performance losses caused by using atomic instructions. Furthermore, by accumulating multiple contribution values of the same output matrix element to generate a contribution accumulation value for the output matrix element; and storing the contribution accumulation value for the output matrix element to a storage location of a shared memory corresponding to the output matrix element, the present invention significantly reduces the read and write operations on a global memory with low memory access efficiency, and improves the performance of a matrix operator. Therefore, the present invention can improve the efficiency of matrix reduction calculations and significantly improve the performance of matrix operators.
[0032] Figure 1 A schematic diagram of a computing device 100 for implementing a method for matrix reduction computing according to an embodiment of the present invention is shown schematically. Figure 1As shown, the computing device 100 may have one or more processing units, including special processing units such as a graphics processing unit (GPU), a field programmable gate array (FPGA), an application-specific integrated circuit (ASIC), a general-purpose graphics processing unit (GPGPU), etc., and a general-purpose processing unit such as a CPU. The computing device 100 also includes at least: an input sub-matrix partitioning unit 102, a plurality of contribution value generating units 104 of output matrix elements, and a contribution cumulative value generating unit 106. A shared memory storage contribution cumulative value unit 108.
[0033] The input sub-matrix division unit 102 is used to divide the input matrix into multiple input sub-matrices, so that each output matrix element is mapped to an angular position of the corresponding input sub-matrix, and each input sub-matrix contains multiple input matrix elements within its range.
[0034] Regarding the multiple contribution value generation unit 104 for the output matrix elements, it is used to allocate threads based on the divided multiple input sub-matrices, so that each thread calculates the contribution values of all input matrix elements in an input sub-matrix for the output matrix elements corresponding to the four corner positions of the input sub-matrix, thereby storing the multiple contribution values generated for each output matrix element to different storage locations in the shared memory.
[0035] The contribution accumulated value generating unit 106 is used to accumulate multiple contribution values of the same output matrix element to generate a contribution accumulated value of the output matrix element.
[0036] The shared memory storage contribution accumulated value unit 108 is used to store the contribution accumulated value of the output matrix element into the storage position of the shared memory corresponding to the output matrix element.
[0037] The following will be combined Figure 2 , Figure 3 and Figure 4 A method 200 for matrix reduction calculation according to an embodiment of the present invention is described. Figure 2 A flow chart of a method 200 for matrix reduction calculation according to an embodiment of the present invention is shown. Figure 3 A schematic diagram of an input sub-matrix according to an embodiment of the present invention is shown. Figure 4FIG. 2 is a schematic diagram showing a method for calculating multiple contribution values of output matrix elements according to an embodiment of the present invention. It should be understood that the method 200 may be used, for example, in Figure 1 The method 200 is executed at the described computing device 100. The method 200 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] At step 202, the computing device 100 divides the input matrix into a plurality of input sub-matrices, such that each output matrix element is mapped to an angular position of the corresponding input sub-matrix, and each input sub-matrix contains a plurality of input matrix elements within its range.
[0039] Regarding the method of dividing an input matrix into multiple input sub-matrices, it for example includes: the computing device 100 divides the output matrix into multiple output sub-matrices so that each output matrix element is located at a corner position of the output sub-matrix; and based on the division method of the output sub-matrix, the input matrix is divided into multiple input sub-matrices so that each output matrix element is mapped to a corner position of the corresponding input sub-matrix.
[0040] Regarding the matrix partitioning method, the following is an example of combining the upsample and backward (BWD) operators. The following expressions (1)-(4) illustrate the range of four adjacent input sub-matrices:
[0041] (1)
[0042] (2)
[0043] (3)
[0044] (4)
[0045] In the above expressions (1) to (4), x represents the abscissa of the output matrix element, and (y) represents the ordinate of the output matrix element. Represents the horizontal coordinate of the output matrix element mapped to the input matrix. , Represents the scaling ratio in the x direction. Represents the ordinate of the output matrix element that is mapped to the input matrix. , Represents the scaling ratio in the y direction. Expression (1) represents the output matrix element located in the mapping The lower left corner of the submatrix range is , , , is the corner point. Expression (2) represents , , , is the output submatrix range of the corner points. The output submatrix is located at the output matrix element of the mapping The lower right corner of . Expression (3) represents , , , is the output submatrix range of the corner points, which is located at the output matrix element of the mapping The upper left corner of . Expression (4) represents , , , is the output submatrix range of the corner points. The output submatrix is located at the output matrix element of the mapping .
[0046] As shown in the figure, Figure 3 The solid dots in the figure indicate the elements in the input matrix, referred to as "input matrix elements", and the hollow dots indicate the output matrix elements mapped to the input matrix coordinates. Mark 310, for example, indicates an output matrix element. Mark 312, for example, indicates the input submatrix at the upper left corner of the output matrix element 310. Mark 314, for example, indicates the input submatrix at the upper right corner of the output matrix element 310. Mark 316, for example, indicates the input submatrix at the lower left corner of the output matrix element 310. Mark 318, for example, indicates the input submatrix at the lower right corner of the output matrix element 310. Figure 3 It can be seen that each mapped output matrix element is located at a corner point of the corresponding input sub-matrix, and the range of the input sub-matrix contains multiple input matrix elements.
[0047] It should be understood that partitioning the input matrix also requires distinguishing whether matrix scaling is performed for alignment. Figure 3 The left half of indicates the case where an integer number of complete input sub-matrices are divided (or the division under the "aligned scaling mode"). Figure 3 The right half of indicates the situation where there are some incomplete input sub-matrices (as indicated by mark 324) (or the division under "non-aligned scaling"). When the aligned scaling is adopted, the division of the input sub-matrix will be just aligned with the boundary of the output matrix, so the number of sub-matrices will be less than the number of elements of the output matrix. When the non-aligned scaling is adopted, there are some input matrix elements outside the coordinates after the output matrix is mapped, and these input matrix elements will also affect the values of the output matrix elements. Therefore, more input sub-matrices need to be divided (for example, these more input sub-matrices are, for example, incomplete input sub-matrices).
[0048] At step 204, the computing device 100 allocates threads based on the divided multiple input sub-matrices so that each thread calculates the contribution values of all input matrix elements within an input sub-matrix to the output matrix elements corresponding to the four corner positions of the input sub-matrix, thereby storing the multiple contribution values generated for each output matrix element to different storage locations in the shared memory.
[0049] Regarding the method of generating multiple contribution values for each output matrix element, it includes: several contribution values calculated respectively relative to the output matrix element by the input matrix elements of several adjacent input sub-matrices with each output matrix element as a common corner point. In some embodiments, several contribution values are 4 contribution values of 4 different storage locations stored in the shared memory about the same output matrix element. In some embodiments, for example, an incomplete matrix sub-matrix under the "non-aligned scaling mode", several contribution values can be, for example, 2 contribution values of 2 different storage locations stored in the shared memory about the same output matrix element. It should be understood that in some embodiments, 4 contribution values can also be still calculated for the output matrix elements of the common corner points of the incomplete matrix sub-matrix under the "non-aligned scaling mode", but 2 of the contribution values are, for example, defaulted to "0".
[0050] It should be understood that, since the multiple contribution values generated for each output matrix element of the present invention are stored in different storage bits in the shared memory, there will be no competition among threads. In the traditional multi-threaded computing, if two threads read an address at the same time for operation and then store it back to the original address, it is possible that both threads read the original data, which leads to the omission of the operation. Therefore, the present invention does not have the problem of thread competition in the traditional method.
[0051] It should be understood that the output matrix elements of each output matrix are just affected by the input matrix elements of the four input sub-matrices adjacent to the point after the coordinate mapping of the output matrix element. Conversely, all elements in each input sub-matrix will affect the corresponding output matrix elements at the four corner points of the input sub-matrix. Therefore, after completing the division of the input sub-matrix, you can choose to enumerate all sub-matrices by thread to calculate the contribution of all elements in each sub-matrix to the output matrix elements at the four corners.
[0052] like Figure 4As shown, each output matrix element, such as the output matrix element indicated by label 424, is located at a common angular position of multiple adjacent input sub-matrices. Through step 204, all input matrix elements in 4 adjacent input sub-matrices respectively calculate 4 contribution values for the output matrix element 424, and the 4 contribution values are stored in different storage locations in the shared memory. For example, Figure 4 The four identifiers "0", "1", "2" and "3" associated with the output matrix element 424 in the shared memory respectively indicate four different (isolated from each other) storage locations in the shared memory. Among them, the shared memory storage location indicated by the identifier "0" is used, for example, to store the contribution values of all input matrix elements in the input sub-matrix 410 with the output matrix element 424 as the lower right corner point to the output matrix element 424. The shared memory storage location indicated by the identifier "1" is used, for example, to store the contribution values of all input matrix elements in the input sub-matrix 430 with the output matrix element 424 as the lower left corner point to the output matrix element 424. The shared memory storage location indicated by the identifier "2" is used, for example, to store the contribution values of all input matrix elements in the input sub-matrix 450 with the output matrix element 424 as the upper right corner point to the output matrix element 424. The shared memory storage location indicated by the identifier "3" is used, for example, to store the contribution values of all input matrix elements in the input sub-matrix 440 with the output matrix element 424 as the upper left corner point to the output matrix element 424. In some embodiments, assuming that the array of the shared memory is Shared[index_x)[index_y)[4), taking the number "3" as an example, the calculated contribution values of all input matrix elements in the input submatrix 440 with the output matrix element 424 as the upper left corner point to the output matrix element 424 will be placed in Shared[0)[0)[3).
[0053] There are many methods for calculating the contribution value. For example, the contribution value of the output matrix element is calculated based on the distance between each input matrix element in the input submatrix and the output matrix element corresponding to the four corner positions of the input submatrix. The following is an algorithm for calculating the contribution value of the input matrix element within the input submatrix to the output matrix element corresponding to the mapping at the four corners of the input submatrix, in combination with formula (5):
[0054] (5)
[0055] In the above formula (5), Represents the contribution function. and They represent the distances of the input matrix elements from the boundaries of the output matrix elements. Represents the contribution of the input matrix elements to the output matrix elements. Represents the coordinates of the mapped output matrix elements corresponding to the four corners of the input submatrix. Coordinates of the input matrix elements that represent the input submatrices. represent Middle distance The horizontal coordinate of the closer boundary. Similarly, represent Middle distance It should be understood that Middle distance The closer they are, the greater the contribution value.
[0056] The following table 1 schematically shows schematic codes for generating multiple contribution values for each output matrix element. For example, the code indicates: traverse each input sub-matrix; calculate the mapping coordinates of the output matrix element coordinates in the input matrix; traverse each input matrix element (x_in, y_in), initialize multiple contribution values for each output matrix element; calculate multiple contribution values for each output matrix element; store multiple contribution values for each output matrix element in multiple locations in the shared memory. Wherein, when generating each of the multiple contribution values, the contribution value of each input matrix element in the corresponding input sub-matrix with the output matrix element (x_out, y_out) as the corner point is accumulated using a register to generate a corresponding contribution accumulated value. It should be understood that if the input sub-matrices with the output matrix element (x_out, y_out) as the corner point are, for example, four adjacent input sub-matrices, then four contribution accumulated values can be generated for the output matrix element (x_out, y_out), for example, val_00, val_01, val_10 and val_11 involved in the schematic code.
[0057] Table 1
[0058]
[0059] At step 206 , the computing device 100 accumulates multiple contribution values for the same output matrix element to generate a contribution accumulation value for the output matrix element.
[0060] A method for accumulating multiple contribution values for the same output matrix element, for example, includes: the computing device 100 determines whether a predetermined condition is met, and the predetermined condition includes any one of the following: the shared memory is full; or the contribution values are calculated for a predetermined number of input sub-matrices; in response to the predetermined condition being met, thread synchronization is performed; and thread tasks are reallocated in units of output matrix elements so that each thread will accumulate multiple contribution values for the same output matrix element, thereby generating a contribution accumulation value for the output matrix element.
[0061] If the computing device 100 determines that the contribution values have been calculated for all input sub-matrices of the entire input matrix (in some embodiments, it can also be determined that the contribution values have been calculated for a row or a predetermined number of input sub-matrices, where the size of the predetermined number is related to the size of the storage space of the shared memory), synchronization is performed. Then, a thread is assigned to each matrix output element, and for each output matrix element, multiple contribution values (for example, 4 contribution values, as indicated by the four identifiers "0", "1", "2", and "3") of multiple (for example, but not limited to 4) adjacent input sub-matrices with it as corner points are calculated and accumulated to generate a contribution accumulation value for the output matrix element (for example, the 4 contribution values indicated by the four identifiers "0", "1", "2", and "3" around the output matrix element 424 are accumulated to generate a contribution accumulation value for the output matrix element 424).
[0062] Regarding the manner of accumulating multiple contribution values for the same output matrix element, in some embodiments, it includes, for example: accumulating multiple contribution values to the "contribution accumulation value initial value" for the output matrix element. If all the multiple contribution values are accumulated through one round of calculation, the "contribution accumulation value initial value" for the output matrix element at this time is "0". If all the multiple contribution values are accumulated through multiple rounds of calculation, for example, thread synchronization is performed after calculating the contribution values of a part of the input sub-matrix but not all of the input sub-matrix, at this time, for example, the accumulation calculation of 4 contribution values of the output matrix element located at the boundary of a part of the input sub-matrix may need to be completed through 2 or more rounds of calculation. Take 2 rounds of calculation as an example, if in the first round of calculation, the 4 contribution values indicated by the four identifiers "0", "1", "2", and "3" are accumulated, for example, the 2 contribution values indicated by the two identifiers "0" and "1" are the calculated contribution values, and the 2 contribution values indicated by the two identifiers "2" and "3" are still the default value "0", then the "initial value of the contribution accumulation value" is updated with the contribution accumulation value of the first round of calculation of the output matrix element; at the same time, the 2 contribution values indicated by the two identifiers "0" and "1" that have completed the first round of accumulation calculation are updated to "0". When the second round of accumulation calculation is performed on the same output matrix element, the 4 contribution values indicated by the four identifiers "0", "1", "2", and "3" are accumulated, among which the values of the two identifiers "0" and "1" have been updated to "0", and the 2 contribution values indicated by the two identifiers "2" and "3" are the calculated contribution values, and the "initial value of the contribution accumulation value" is the accumulated value of the contributions of the two identifiers "0" and "1" in the first round. In the second round of cumulative calculation, the four values indicated by the four identifiers "0", "1", "2" and "3" are added to the "initial value of the contribution cumulative value", thereby obtaining the final contribution cumulative value. Therefore, even if the accumulation operation of multiple contribution values for output matrix elements involves multiple rounds of calculation due to limited shared memory space, the present invention will not cause calculation errors of the final contribution cumulative value.
[0063] At step 208 , the computing device 100 stores the contribution accumulation value of the output matrix element to the storage location of the shared memory corresponding to the output matrix element.
[0064] It should be understood that shared memory is a common memory space in GPUs, which is usually located closer to the computing unit, so its memory access efficiency is higher than that of global memory. Therefore, by storing the accumulated value in the shared memory corresponding to the output matrix element, it is helpful to speed up the operation. In addition, only the memory within the thread group is visible, and different thread groups see different shared memories.
[0065] In the above scheme, since all elements of each input submatrix of the present invention are read by one thread, each input matrix element is read once, thereby significantly reducing the number of times the input matrix elements are read; in addition, by separating multiple contribution values of each output matrix element through shared memory, the present invention not only avoids possible thread read and write conflicts, but also avoids performance loss caused by using atomic instructions. Furthermore, by accumulating multiple contribution values of the same output matrix element to generate a contribution accumulation value for the output matrix element; and storing the contribution accumulation value for the output matrix element to the storage location of the shared memory corresponding to the output matrix element, the present invention significantly reduces the read and write operations of the global memory with low memory access efficiency, thereby improving the performance of the matrix operator. Therefore, the present invention can improve the efficiency of matrix reduction calculation and significantly improve the performance of the matrix operator.
[0066] The following will be combined Figure 5 A method 500 for generating contribution accumulation values for the output matrix elements according to an embodiment of the present invention is described. Figure 5 FIG. 5 is a flow chart of a method 500 for generating a contribution accumulation value for an output matrix element according to an embodiment of the present invention. It should be understood that the method 500 may be implemented, for example, in Figure 1 The method 500 is executed at the described computing device 100. The method 500 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.
[0067] At step 502, the computing device 100 determines whether a predetermined condition is satisfied, wherein the predetermined condition includes any one of the following: the shared memory is full; or the contribution values are calculated for a predetermined number of input sub-matrices.
[0068] Regarding the predetermined number, it is, for example, the number of all input sub-matrices, or the number of a portion of the input sub-matrices.
[0069] In some embodiments, for example, if the shared memory in the GPU has sufficient space, the computing device 100 determines whether all input sub-matrices in the input matrix have completed calculating the contribution values of the output matrix elements. If it is confirmed that all input sub-matrices have completed calculating the contribution values of the output matrix elements, it is determined that the predetermined condition is met.
[0070] It should be understood that the shared memory is an on-chip memory, and its space is limited, so the contribution value can also be calculated in blocks. For example, in some embodiments, if the computing device 100 determines that a row or a portion of the input sub-matrix has completed the contribution value of the output matrix element, it is determined that the predetermined condition is satisfied.
[0071] At step 504, if the computing device 100 determines that the predetermined condition is satisfied, thread synchronization is performed. Otherwise, at step 502, waiting is performed, for example, waiting to continue calculating the contribution value of the output matrix element.
[0072] For example, if it is confirmed that all input sub-matrices have calculated the contribution values of the output matrix elements, synchronization is performed for the threads to reallocate the thread tasks. It should be understood that the frequency of synchronization is relatively low, for example, synchronization is performed only after all the contribution values of the input sub-matrices have been calculated, so its efficiency is significantly improved relative to traditional atomic instructions.
[0073] At step 506, the computing device 100 reallocates thread tasks in units of output matrix elements so that each thread accumulates multiple contribution values for the same output matrix element, thereby generating a contribution accumulation value for the output matrix element.
[0074] A method for accumulating multiple contribution values calculated for the same output matrix element by each thread in a register, for example, includes: traversing each output matrix element through multiple threads so that each thread reads multiple contribution values of an output matrix element from a shared memory; and accumulating the read multiple contribution values to generate a contribution accumulation value for the output matrix element.
[0075] It should be understood that after the contribution value calculation is completed or the shared memory is full, the threads are reallocated in units of output matrix elements, and the output matrix elements are traversed through multiple threads, so that the elements of each output matrix will have a contribution value read from the corresponding shared memory position by one thread, and then the read contribution values are accumulated and stored in the shared memory position corresponding to the output matrix element. The present invention can effectively avoid memory access conflicts and improve memory access efficiency.
[0076] The following table 2 schematically shows a schematic code for synchronizing and generating the contribution accumulation value of the output matrix element. In the schematic code, OutData[x_out][y_out] represents the contribution accumulation value of the output matrix element (x_out, y_out). SharedData[x_out][y_out][0] represents 1 contribution value indicated by the "0" mark; SharedData[x_out][y_out][1] represents 1 contribution value indicated by the "1" mark; SharedData[x_out][y_out][2] represents 1 contribution value indicated by the "2" mark; SharedData[x_out][y_out][3] represents 1 contribution value indicated by the "3" mark.
[0077] Table 2
[0078]
[0079] By adopting the above-mentioned means, the present invention can effectively avoid memory access conflicts in matrix scaling calculations and improve the efficiency of matrix reduction calculations.
[0080] The various processes and processing described above, such as methods 200 and 500, may be performed at a computing device. The computing device, for example, includes: at least one processor (at least one graphics processor and at least one central processing unit); and a memory connected to the at least one processor in communication; wherein the memory stores instructions that can be executed by at least one processor, and the instructions are executed by at least one processor. In some embodiments, methods 200 and 500 may be implemented as a computer software program or program product, which is tangibly contained in a machine-readable medium. In some embodiments, part or all of the computer program may be loaded and / or installed on a computing device via a read-only memory (ROM) and / or a communication unit. When the computer program is loaded into a random-access memory (RAM) and executed by a GPU and a CPU, one or more actions of methods 200 and 500 described above may be performed.
[0081] The present invention may be a method, an apparatus, a system and / or a computer program product. The computer program product may include a computer-readable storage medium having computer-readable program instructions for executing various aspects of the present invention. The computer-readable storage medium may be a tangible device that can hold and store instructions used by an instruction execution device. The computer-readable storage medium may be, for example, but not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination thereof.
[0082] The computer-readable program instructions described herein can be downloaded from a computer-readable storage medium to various computing / processing devices, 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. Various aspects of the present invention are described herein with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each box of the flowchart and / or block diagram, and the combination of boxes in the flowchart and / or block diagram, can be implemented by computer-readable program instructions.
[0083] These computer-readable program instructions can be provided to a central processing unit of a general-purpose computer, a special-purpose computer, or other programmable data processing device, thereby producing a machine, so that when these instructions are executed by the central processing unit of the 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 equipment to work in a specific manner, so that the computer-readable medium storing the instructions includes a manufactured product, which includes instructions for implementing various aspects of the functions / actions specified in one or more boxes in the flowchart and / or block diagram.
[0084] The flow chart and block diagram in the accompanying drawings show the possible architecture, function and operation of the system, method and computer program product according to multiple embodiments of the present invention. In this regard, each square box in the flow chart or block diagram can represent a part of a module, program segment or instruction, and a part of a module, program segment or instruction includes one or more executable instructions for realizing the logical function of the specification. In some alternative implementations, the function marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two continuous square boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the function involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be realized by a special hardware-based system that performs the function or action of the specification, or can be realized by a combination of special hardware and computer instructions.
[0085] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps loaded in this application can be executed in parallel, sequentially or in different orders, as long as the expected results of the technical solution disclosed in this application can be achieved, and this document is not limited here.
[0086] The above specific implementations do not constitute a limitation on the protection scope of the present application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may be made according to design requirements and other factors.
Claims
1. A method for matrix reduction calculation, characterized in that: The method comprises: Divide the input matrix into a plurality of input sub-matrices, such that each output matrix element is mapped to an angular position of the corresponding input sub-matrix, and each input sub-matrix contains a plurality of input matrix elements within its range; Allocating threads based on the divided multiple input sub-matrices so that each thread calculates contribution values of all input matrix elements in an input sub-matrix to output matrix elements corresponding to four corner positions of the input sub-matrix, thereby storing multiple contribution values generated for each output matrix element in different storage locations in the shared memory; Accumulating multiple contribution values for the same output matrix element to generate a contribution accumulation value for the output matrix element, wherein the thread tasks are redistributed according to the output matrix element as a unit so that each thread accumulates multiple contribution values for the same output matrix element to generate the contribution accumulation value for the output matrix element; and The contribution accumulation value of the output matrix element is stored in a storage location of the shared memory corresponding to the output matrix element.
2. The method according to claim 1, characterized in that: Before redistributing the thread tasks according to the output matrix elements as units, generating the contribution accumulation value about the output matrix elements further includes: Determine whether a predetermined condition is satisfied, the predetermined condition including any one of the following: the shared memory is full; or After calculating the contribution values for a predetermined number of input sub-matrices and in response to a predetermined condition being met, thread synchronization is performed.
3. The method according to claim 2, characterized in that Each thread will accumulate multiple contributions to the same output matrix element including: Iterate through each output matrix element through multiple threads, so that each thread reads multiple contribution values of an output matrix element from the shared memory; and Accumulation is performed on the plurality of contribution values read so as to generate a contribution accumulation value related to the output matrix element.
4. The method according to claim 2, characterized in that: The predetermined number is the number of all input sub-matrices or the number of part of the input sub-matrices.
5. The method according to claim 1, characterized in that Dividing the input matrix into multiple input sub-matrices includes: dividing the output matrix into a plurality of output sub-matrices such that each output matrix element is located at a corner position of the output sub-matrix; and Based on the partitioning method of the output sub-matrix, the input matrix is divided into a plurality of input sub-matrices, so that each output matrix element is mapped to an angular position of the corresponding input sub-matrix.
6. The method according to claim 1, characterized in that The multiple contribution values generated for each output matrix element include: a plurality of contribution values respectively calculated for input matrix elements of a plurality of adjacent input sub-matrices with each output matrix element as a common corner point relative to the output matrix element.
7. The method according to claim 6, characterized in that The plurality of contribution values are four contribution values regarding a same output matrix element and stored in four different storage locations in the shared memory.
8. A computing device, characterized in that: include: at least one processor; as well as a memory communicatively coupled to the at least one processor; in 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 7.
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 machine, the method according to any one of claims 1 to 7 is performed.
10. A computer program product, characterized in that The invention comprises a computer program, which, when executed by a machine, performs the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Matrix-factorization based gradient compression
CN111753979A
Data processing method and device, equipment and medium
CN116127260A
Multidimensional fast Fourier transform acceleration method based on matrix operation
CN117633418A
Processor, reduction calculation method and electronic equipment
CN117785480A
Information processing apparatus, multithread matrix operation method, and multithread matrix operation program
US20180341517A1
