A neural network training method and computing device for sparse matrix multiplication using different mask blocks
By dividing the weight matrix into B weight blocks and using a sparse mask set and random number index to generate a mask matrix for sparse matrix multiplication, the problem of long training time in existing technologies is solved, and more efficient neural network training is achieved.
Patent Information
- Application Number
- CN202480091454.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-02
- Publication Date
- 2026-11-27
Smart Images

Figure CN123187150A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, and in particular to a neural network training method and computing device. Background Technology
[0002] In recent years, deep learning has made significant breakthroughs in many practical problems, such as computer vision (e.g., object detection, segmentation, and face recognition), natural language processing, and speech recognition. In fact, neural networks, the primary tool of deep learning, are now ubiquitous. They are used for searching information, translating speech and text, driving vehicles, controlling product quality in manufacturing plants, forecasting the weather, improving photo quality, recommending products, and performing many other everyday activities.
[0003] Although neural networks are widely used, they are usually based on one or more of the following basic operations: convolution, multi-head self attention (MHSA), and fully connected layers (such as multi-layer perceptrons, or MLPs).
[0004] All the above operations can be implemented using matrix multiplication. Therefore, to accelerate model training, matrix multiplication needs to be performed as efficiently as possible. In this regard, an N:M sparse method for matrix multiplication is proposed, where N and M are both positive integers and M > N. For example, N:M can be 2:4 or 4:8. Figure 1 As shown, in every 4 (M=4) consecutive elements of the left-hand matrix row (or possibly the right-hand matrix column), 2 (N=2) elements can be set to zero. Theoretically, this assumption can speed up matrix multiplication by a factor of 2 (M / N). However, a key limitation of this method is the need for a densely pre-trained model, which also requires fine-tuning for the N:M sparsity constraint.
[0005] like Figure 2 As shown, a transposeable N:M sparse mask is proposed, which can leverage the structured sparsity of hardware to accelerate model training. Furthermore, a 2-approximation method for finding the optimal sparse mask is also proposed, but this method requires additional training time. Moreover, the model needs fine-tuning for the N:M sparsity constraint, which fails to fully utilize hardware features to accelerate model training. Summary of the Invention
[0006] This invention provides a neural network training method and computing device that eliminates the need for pre-training a large-scale dense weight matrix and for fine-tuning the trained model to meet N:M sparse constraints, thereby saving a significant amount of training time and improving training efficiency.
[0007] Firstly, a neural network training method is provided. The method includes: dividing a weight matrix into B weight blocks, wherein the B weight blocks are of equal size and defining B sparse mask sets, each sparse mask set including D mask blocks satisfying an N:M sparsity constraint, where N, M, B, and D are all positive integers, and N...<M、B> 1 and D>1; generate B random number indices corresponding to the B weight blocks, wherein the random range of the B random number indices is [1,D]; use the B random number indices and the B sparse mask sets to determine B mask blocks, wherein the B mask blocks correspond to the B weight blocks; use the B mask blocks to generate a mask matrix; use the mask matrix to perform sparse matrix multiplication.
[0008] Based on the neural network training method provided in the first aspect, the weight matrix can be divided into B weight blocks of the same size that satisfy the N:M sparsity constraint. Then, using B sparse mask sets defined for each of the B weight blocks and B random number indices generated for each of the B weight blocks, a mask matrix for implementing sparse matrix multiplication operations can be generated. This eliminates the need for pre-training a large-scale dense weight matrix and for fine-tuning the trained model to meet the N:M sparsity constraint, thus saving significant training time and improving training efficiency.
[0009] Here, satisfying the N:M sparsity constraint can include all mask blocks in the B sparse mask sets satisfying the N:M sparsity constraint by row and / or column, which is not limited in this embodiment of the invention. Optionally, the N:M sparsity constraint can be that all mask blocks satisfy the N:M sparsity constraint by row and column. Therefore, in the training process of both forward and backward neural networks, the mask matrix can be used to reduce the number of weights actually used for training in the weight matrix, thereby improving training efficiency.
[0010] Any of the mask blocks mentioned above can be composed of elements with values of 0 or 1 (e.g., binary numbers 0 or 1). Thus, during neural network training, the elements with values of 1 in the mask matrix are actually used to select a subset of weights from the weight matrix (i.e., the weights that satisfy the N:M sparse constraint) for training, thereby improving training efficiency by approximately M / N times.
[0011] Specifically, the following scheme can be used to generate B mask blocks for B weight blocks, and then generate a mask matrix based on the B mask blocks.
[0012] In Scheme 1 (i.e., the fully randomized scheme), in one possible design, determining the B mask blocks using the B random number indices and the B sparse mask sets includes: randomly selecting B mask blocks from the B sparse mask sets for each of the B weight blocks based on the B random number indices; generating the mask matrix using the B mask blocks includes: generating the mask matrix using the B mask blocks selected from the B sparse mask sets, wherein the position of the i-th mask block in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B. Thus, a fully randomized approach is used to generate B mask blocks for each of the B weight blocks, thereby generating the mask matrix. This eliminates the need for pre-training a large-scale dense weight matrix and for fine-tuning the trained model again for the N:M sparse constraint, saving significant training time and improving training efficiency.
[0013] In Scheme 2 (i.e., the partial replacement scheme), in another possible design, determining the B mask blocks using the B random number indices and the B sparse mask sets includes: randomly selecting B mask blocks from the B sparse mask sets for the B weight blocks based on the B random number indices; obtaining X additional mask blocks, wherein the X additional mask blocks are the mask blocks that give the highest score to the X weight blocks the most times within one or more predefined time periods, where X is an integer and 0≤X≤B; updating the B mask blocks by replacing X mask blocks from the B mask blocks selected from the B sparse mask sets with the X additional mask blocks; generating the mask matrix using the B mask blocks includes: generating the mask matrix using the updated B mask blocks, wherein the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B. Therefore, X additional mask blocks that provide the highest scores to X weight blocks out of B weight blocks within one or more predefined time periods can be used to replace X mask blocks randomly generated from the X weight blocks. In other words, some or all of the mask blocks with long-term optimal performance in terms of training effect can be used to replace mask blocks with unknown training effect, thereby improving the overall quality of the B mask blocks actually used to generate the mask matrix and thus improving the training effect.
[0014] Optionally, the i-th mask block in the mask matrix generated using the updated B mask blocks satisfies: Ai = Fi + (1 – sign(Fi)) Ri Where Ai is the index of the i-th mask block in the mask matrix generated using the updated B mask blocks, Ri is the index of the i-th mask block among the B mask blocks selected from the B sparse mask sets, Fi is the index of the mask block corresponding to the i-th weight block among the X additional mask blocks, and sign(Fi) is the index sign function that satisfies the following condition:
[0015] in, This indicates that the mask block corresponding to the i-th weight block among the B weight blocks has not been determined. This indicates that the mask block corresponding to the i-th weight block among the B weight blocks is determined as the mask block with the specified index Fi in the i-th sparse mask set.
[0016] In other words, by using the index of a previously determined and stored mask block (the initial value of which is less than 1 or greater than D, and the updated value of which is greater than or equal to 1 and less than or equal to D), X randomly generated mask blocks from B mask blocks can be replaced with X previously determined additional mask blocks. For example, the "X determined additional mask blocks" could be mask blocks as described above that most frequently give the highest score to the X weight blocks out of B weight blocks, thereby improving the quality of the mask matrix and enhancing training performance.
[0017] In Scheme 3 (i.e., the merging scheme), in another possible design, determining the B mask blocks using the B random number indices and the B sparse mask sets includes: randomly selecting B–X mask blocks from the B–X sparse mask sets for each of the B–X weight blocks based on the B–X random number indices, where X is an integer and 0≤X≤B; obtaining X additional mask blocks, where the X additional mask blocks are the mask blocks that give the highest score to the X weight blocks the most times within one or more predefined time periods; updating the B mask blocks by merging the B–X mask blocks and the X additional mask blocks; generating the mask matrix using the B mask blocks includes: generating the mask matrix using the updated B mask blocks, where the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B. In other words, compared with the above replacement scheme, if it is known that there are X additional mask blocks that give the highest score the most times among the X weight blocks of B weight blocks, then B mask blocks can be obtained to generate a mask matrix by directly using these X additional mask blocks and merging these X additional mask blocks with B–X mask blocks randomly generated for the other B–X weight blocks. This reduces the workload of randomly generating and replacing some mask blocks and improves training efficiency.
[0018] In one possible design, obtaining the X additional mask blocks includes: obtaining the X additional mask blocks from X sparse mask sets in the B sparse mask sets based on X specified indices.
[0019] Optionally, the method further includes: before obtaining the X additional mask blocks from the X sparse mask sets in the B sparse mask sets based on the X specified indices, determining the X specified indices for the X weight blocks in the B weight blocks respectively within one or more predefined time periods. That is, the X specified indices can be determined over a relatively long period; that is, the X specified indices are the indices of mask blocks that perform well over a relatively long period in terms of the training effect of the X weight blocks in the B weight blocks, thereby ensuring the quality of the mask matrix and the training effect.
[0020] In addition, determining the X specified indices for the X weight blocks among the B weight blocks within the one or more predefined durations respectively includes: determining E weight blocks and E additional mask blocks within one predefined duration of the one or more predefined durations, wherein the E additional mask blocks are mask blocks that give the highest score to the E weight blocks the most times within the predefined duration, and E is an integer satisfying 0≤E≤X; determining the X weight blocks and the X additional mask blocks within the one or more predefined durations; and allocating the X specified indices of the X additional mask blocks to the X weight blocks respectively. That is, the X specified indices may be all determined within one predefined duration (E=X), or may be determined in batches within more predefined durations (E<X). For example, the quantity (for example, X / total number of predefined durations) or proportion of weight blocks and mask blocks to be determined within each predefined duration may be set, which is not limited in the embodiments of the present invention.
[0021] In addition, determining the E weight blocks and the E additional mask blocks includes: setting all elements in a counter matrix of size B×D and all elements in an index array of size B to 0, wherein a counter at the i-th row and the j-th column in the counter matrix is used to count when the j-th mask block in the i-th sparse mask set gives the highest score to the i-th weight block; the i-th element in the index array is used to store the specified index of the mask block corresponding to the i-th weight block, and j is a positive integer satisfying 1≤j≤D; every certain number of training steps, determining the score given by the j-th mask block in the i-th sparse mask set to the i-th weight block; finding an additional mask block that gives the highest score to the i-th weight block, and increasing the value of the counter corresponding to the i-th weight block and the additional mask block by 1; determining weight blocks and mask blocks corresponding to E counters with the E highest values in the counter matrix as the E weight blocks and the E additional mask blocks, and storing specified indices of the E additional mask blocks.
[0022] Specifically, scores given by all mask blocks in the i-th sparse mask set corresponding to the i-th weight block to the i-th weight block may be calculated by using the following two methods.
[0023] Optionally, the B weight blocks are all sub-weight matrices of size K×P, where K and P are positive integers, and K=M and / or P=M; and a score given by the j-th mask block in the i-th sparse mask set to the i-th weight block satisfies:
[0024] wherein, The score given to the j-th mask block in the i-th sparse mask set as the score of the i-th weight block. This refers to the nth weight value in the i-th weight block. Let n be the nth mask value of the jth mask block in the i-th sparse mask set, where n is a positive integer and 1 ≤ n ≤ K. P.
[0025] Alternatively, the B weight blocks are all sub-weight matrices of size K×P, where K and P are positive integers, and K=M and / or P=M; the B sparse mask sets are all first sparse mask sets. Determining the score given by the j-th mask block in the i-th sparse mask set as the i-th weight block includes: generating a reconstruction matrix using the B weight blocks, where the size of the reconstruction matrix is B×(K... P) or (K) P)×B, where the i-th row or i-th column of the reconstructed matrix includes the absolute values of all weights of the i-th weight block in the B weight blocks; a full mask matrix is generated using the first sparse mask set, wherein the size of the full mask matrix is (K)×B. P)×D or D×(K) P), the j-th column or j-th row of the full mask matrix includes all mask values of the j-th mask block in the first sparse mask set, and the positions of all mask values of the j-th mask block in the first sparse mask set in the full mask matrix coincide with the positions of the absolute values of all weights of the i-th weight block in the reconstruction matrix; a score matrix is obtained by performing matrix multiplication on the reconstruction matrix and the full mask matrix, wherein the size of the score matrix is B×D or D×B, the i-th row or i-th column of the score matrix stores D scores, and the j-th score is the matrix product of all weights in the i-th weight block and the absolute values of all mask values in the j-th mask block.
[0026] As can be seen, since any element in the mask block can take the value of 0 or 1, the score given by all mask blocks in the i-th sparse mask set corresponding to the i-th weight block is the sum of the absolute values of the weights selected from the i-th weight block using the elements with the value of 1 in all mask blocks in the i-th sparse mask set. The higher this score, the better the sparse training effect.
[0027] In a second aspect, a computing device is provided, including a memory and at least one processor coupled to the memory. The memory stores computer instructions that, when executed by the at least one processor, cause the computing device to perform the following operations: dividing a weight matrix into B weight blocks, wherein the B weight blocks are of equal size and defining B sparse mask sets, each sparse mask set including D mask blocks satisfying an N:M sparsity constraint, where N, M, B, and D are all positive integers, and N...<M、B> 1 and D>1; generate B random number indices corresponding to the B weight blocks, wherein the random range of the B random number indices is [1,D]; use the B random number indices and the B sparse mask sets to determine B mask blocks, wherein the B mask blocks correspond to the B weight blocks; use the B mask blocks to generate a mask matrix; use the mask matrix to perform sparse matrix multiplication.
[0028] In one possible design, when the computer instructions are executed by the at least one processor, the computing device performs the following operations: randomly selects B mask blocks from the B sparse mask sets for the B weight blocks based on the B random number indices; generates the mask matrix using the B mask blocks selected from the B sparse mask sets, wherein the position of the i-th mask block in the mask matrix coincides with the position of the i-th weight block among the B mask blocks in the weight matrix, where i is a positive integer and 1≤i≤B.
[0029] In another possible design, when the computer instructions are executed by the at least one processor, the computing device performs the following operations: randomly selects B mask blocks from the B sparse mask sets for the B weight blocks based on the B random number indices; obtains X additional mask blocks, wherein the X additional mask blocks are the mask blocks that give the highest score to the X weight blocks among the B weight blocks the most times within one or more predefined time intervals, where X is an integer and 0≤X≤B; updates the B mask blocks by replacing the X mask blocks among the B mask blocks selected from the B sparse mask sets with the X additional mask blocks; generates the mask matrix using the updated B mask blocks, wherein the position of the i-th mask block among the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
[0030] Optionally, the i-th mask block in the mask matrix generated using the updated B mask blocks satisfies: Ai = Fi + (1 – sign(Fi)) Ri Where Ai is the index of the i-th mask block in the mask matrix generated using the updated B mask blocks, Ri is the index of the i-th mask block among the B mask blocks selected from the B sparse mask sets, Fi is the index of the mask block corresponding to the i-th weight block among the X additional mask blocks, and sign(Fi) is the index sign function that satisfies the following condition:
[0031] in, This indicates that the mask block corresponding to the i-th weight block among the B weight blocks has not been determined. This indicates that the mask block corresponding to the i-th weight block among the B weight blocks is determined as the mask block with the specified index Fi in the i-th sparse mask set.
[0032] In another possible design, when the computer instructions are executed by the at least one processor, the computing device performs the following operations: randomly selects B–X mask blocks from B–X sparse mask sets for B–X weight blocks based on B–X random number indices, where X is an integer and 0≤X≤B; obtains X additional mask blocks, wherein the X additional mask blocks are the mask blocks that give the highest score to the X weight blocks among the B weight blocks the most times within one or more predefined time periods; updates the B mask blocks by merging the B–X mask blocks and the X additional mask blocks; and generates the mask matrix using the updated B mask blocks, wherein the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
[0033] In one possible design, when the computer instructions are executed by the at least one processor, the computing device performs the following operation: obtains the X additional mask blocks from the X sparse mask sets of the B sparse mask sets based on X specified indices.
[0034] Optionally, when executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: before obtaining the X additional mask blocks from the X sparse mask sets in the B sparse mask sets based on the X specified indices, determining the X specified indices for the X weight blocks in the B weight blocks respectively within the one or more predefined time periods.
[0035] Furthermore, when executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: determine E weight blocks and E additional mask blocks within one of the one or more predefined durations, wherein the E additional mask blocks are the mask blocks that give the highest score to the E weight blocks the most times within the predefined durations, where E is an integer and 0≤E≤X; determine X weight blocks and X additional mask blocks within the one or more predefined durations; and assign the X specified indices of the X additional mask blocks to the X weight blocks respectively.
[0036] Furthermore, when executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: setting all elements of a B×D counter matrix and all elements of a B-sized index array to 0, wherein the counter in the i-th row and j-th column of the counter matrix is used to count when the j-th mask block in the i-th sparse mask set gives the highest score to the i-th weight block; the i-th element of the index array is used to store a specified index of the mask block corresponding to the i-th weight block, where j is... Positive integers and 1≤j≤D; every certain number of training steps, determine the score given by the j-th mask block in the i-th sparse mask set for the i-th weight block; find the additional mask block that gives the highest score for the i-th weight block, and increment the value of the counter corresponding to the i-th weight block and the additional mask block by 1; determine the weight block and mask block corresponding to the E counters with E highest values in the counter matrix as the E weight blocks and the E additional mask blocks, and store the specified index of the E additional mask blocks.
[0037] Optionally, each of the B weight blocks is a sub-weight matrix of size K×P, where K and P are positive integers, and K=M and / or P=M; the j-th mask block in the i-th sparse mask set is given by the score of the i-th weight block satisfying:
[0038] in, The score given to the j-th mask block in the i-th sparse mask set as the score of the i-th weight block. This refers to the nth weight value in the i-th weight block. Let n be the nth mask value of the jth mask block in the i-th sparse mask set, where n is a positive integer and 1 ≤ n ≤ K. P.
[0039] Optionally, the B weight blocks are all sub-weight matrices of size K×P, where K and P are positive integers, and K=M and / or P=M; the B sparse mask sets are all first sparse mask sets. When executed by the at least one processor, the computer instructions cause the computing device to perform the following operation: generate a reconstruction matrix using the B weight blocks, wherein the size of the reconstruction matrix is B×(K… P) or (K) P)×B, where the i-th row or i-th column of the reconstructed matrix includes the absolute values of all weights of the i-th weight block in the B weight blocks; a full mask matrix is generated using the first sparse mask set, wherein the size of the full mask matrix is (K)×B. P)×D or D×(K) P), the j-th column or j-th row of the full mask matrix includes all mask values of the j-th mask block in the first sparse mask set, and the positions of all mask values of the j-th mask block in the first sparse mask set in the full mask matrix coincide with the positions of the absolute values of all weights of the i-th weight block in the reconstruction matrix; a score matrix is obtained by performing matrix multiplication on the reconstruction matrix and the full mask matrix, wherein the size of the score matrix is B×D or D×B, the i-th row or i-th column of the score matrix stores D scores, and the j-th score is the matrix product of all weights in the i-th weight block and the absolute values of all mask values in the j-th mask block.
[0040] Thirdly, a computer-readable storage medium is provided having computer instructions stored thereon, which, when executed by a computer, cause the computer to implement the method provided in any implementation of the first aspect.
[0041] Fourthly, a computer program product is provided, comprising instructions that, when executed by a computer, cause the computer to implement the method provided in any implementation of the first aspect.
[0042] Fifthly, a chip system is provided. The chip system includes processing circuitry and a storage medium.
[0043] The storage medium stores computer instructions that, when executed by the processing circuit, cause the chip system to implement the method provided by any of the implementations of the first aspect. Attached Figure Description
[0044] Figure 1 This is a schematic diagram of the 2:4 sparse method used for matrix multiplication; Figure 2 This is a schematic diagram of a transposeable N:M sparse mask; Figure 3This is a schematic diagram of the structure of a computing device according to an embodiment of the present invention; Figure 4 This is a schematic flowchart of a neural network training method according to an embodiment of the present invention; Figure 5 This is a schematic flowchart of a fully randomized scheme according to an embodiment of the present invention; Figure 6 An example of a fully randomized scheme according to an embodiment of the present invention is shown; Figure 7 This is a schematic flowchart illustrating a partial replacement scheme according to an embodiment of the present invention; Figure 8 An example of a partial replacement scheme according to an embodiment of the present invention is shown; Figure 9 This is a schematic flowchart of a merging scheme according to an embodiment of the present invention; Figure 10 An example of a merging scheme according to an embodiment of the present invention is shown; Figure 11 This is a schematic flowchart of a method for determining a specified index for a weight block according to an embodiment of the present invention; Figure 12 An example of a counter matrix and a mask block index array according to an embodiment of the present invention is shown; Figure 13 An example is shown that, according to an embodiment of the present invention, B weight blocks are given by D mask blocks in a first sparse mask set; Figure 14 An example of determining E specified indices for E weight blocks is shown according to an embodiment of the present invention. Detailed Implementation
[0045] The implementation of the embodiments of the present invention will now be described in detail with reference to the accompanying drawings.
[0046] Figure 3 This is a schematic diagram of the structure of a computing device provided in an embodiment of the present invention. The computing device can be an electronic device with computing capabilities, such as a server, supercomputer, mainframe computer, network device, terminal device, personal computer (PC), or a chip or system-on-chip (SoC) from the above devices. Figure 3 As shown, the computing device 300 includes a first processor 301, a second processor 302, and a communication line 303.
[0047] The first processor 301 can be a general-purpose processor, such as an advanced RISC machine (ARM) processor or an x86 processor, primarily used to run processes to control software. The software may include a series of instructions that can invoke the second processor 302 to perform composite operations (e.g., matrix multiplication). The second processor 302 can be a dedicated processor for neural network training, such as a graphics processing unit (GPU) or a tensor processing unit (TPU). The communication line 303 can be a bus or an electrically coupled line that enables communication between the first processor 301 and the second processor 302.
[0048] Here, the first processor 301 can execute the sparse computing scheme provided in the embodiments of the present invention to search for mask blocks that satisfy the N:M sparse constraint by row and / or by column, and provide the mask blocks to the second processor 302 through the communication line 303; the second processor 302 can generate a mask matrix based on the mask blocks provided by the first processor 301, and use the generated mask matrix to perform sparse mask training to improve training efficiency.
[0049] In some embodiments, the first processor 301 may also be referred to as a host or controller, and the second processor 302 may also be referred to as an accelerator or hardware accelerator. The embodiments of the present invention do not limit this.
[0050] Optionally, the computing device 300 may also include one or more of the following: a memory 304, a communication interface 305, and an input / output interface 306, wherein the first processor 301, the second processor 302, the memory 304, the communication interface 305, and the input / output interface 306 can communicate with each other through the communication line 303.
[0051] The communication interface 305 is used by the computing device 300 to communicate with other devices or communication networks. Other communication networks may be Ethernet, radio access network (RAN), wireless local area network (WLAN), etc. The communication interface 305 may be any device, component, assembly, or subsystem capable of communication, such as a module, circuit, or transceiver.
[0052] The communication line 303 is used to transmit information between the various components included in the computing device 300.
[0053] The memory 304 is used to store instructions and data, wherein the instructions may be computer programs and the data may include input data, output data and intermediate data.
[0054] The memory 304 may be a read-only memory (ROM) or other type of static storage device capable of storing static information and / or instructions; or, the memory 304 may be a random access memory (RAM) or other type of dynamic storage device capable of storing information and / or instructions; or, the memory 304 may be an electrically erasable programmable read-only memory (EEPROM), a compact disc read-only memory (CD-ROM), or other CD storage, optical storage (including optical discs, laser discs, optical discs, digital multifunction optical discs, and Blu-ray discs), magnetic disk storage media, or other magnetic storage devices, etc., without limitation.
[0055] It should be noted that the memory 304 may exist independently of the first processor 301 and / or the second processor 302, or it may be integrated with the first processor 301 and / or the second processor 302. The memory 304 may be located inside or outside the computing device 300, and there is no limitation on this.
[0056] In one example, the first processor 301 and / or the second processor 302 may each include one or more CPUs, for example, Figure 3 CPU0 and CPU1 in the CPU.
[0057] As an optional implementation, the computing device 300 may include a plurality of first processors 301 and / or a plurality of second processors 302, and the embodiments of the present invention do not limit this.
[0058] The input / output interface 306 is used to connect various input / output devices such as keyboard, mouse, microphone, monitor, and speaker. These input / output devices can receive user input commands and / or output calculation results.
[0059] It should be noted that the computing device 300 can be a desktop computer, portable computer, network server, cellular phone, tablet computer, wireless terminal, embedded device, system-on-a-chip, or have similar functions. Figure 3 Devices with a similar structure as shown.
[0060] in addition, Figure 3 The composition and structure shown do not constitute a limitation on the computing device 300. Except... Figure 3 In addition to the components shown, computing device 300 may include more or fewer components than those shown in the figure, or combine certain components, or use different arrangements of components.
[0061] Furthermore, the operations, terms, etc., involved in the various embodiments of this invention can be referenced interchangeably without limitation. In the embodiments of this invention, the message names and parameter names in the messages used for interaction between various devices are merely examples, and other names may be used in specific implementations. The embodiments of this invention do not limit this.
[0062] The following is combined Figures 4 to 14 This invention provides a detailed description of the neural network training method provided in the embodiments of the present invention.
[0063] Figure 4 This is a schematic flowchart of the neural network training method provided in an embodiment of the present invention. Figure 4 As shown, the method may include the following steps S401 to S405.
[0064] S401: Divide the weight matrix into B weight blocks, where the B weight blocks are of equal size, and define B sparse mask sets. Each sparse mask set includes D mask blocks satisfying the N:M sparsity constraint, where N, M, B, and D are all positive integers.<M、B> 1 and D>1.
[0065] S402: Generate B random number indices corresponding to B weight blocks, where the random range of the B random number indices is [1, D].
[0066] S403: Use B random number indices and B sparse mask sets to determine B mask blocks, where the B mask blocks correspond to B weight blocks.
[0067] S404: Generate a mask matrix using B mask blocks.
[0068] S405: Implement sparse matrix multiplication using a mask matrix.
[0069] Based on the neural network training method provided in this invention, the weight matrix can be divided into B weight blocks of the same size that satisfy the N:M sparse constraint. Then, B sparse mask sets defined for each of the B weight blocks and B random number indices generated for each of the B weight blocks are used to generate a mask matrix for implementing sparse matrix multiplication. This eliminates the need for pre-training a large-scale dense weight matrix and for fine-tuning the trained model to meet the N:M sparse constraint, thus saving significant training time and improving training efficiency.
[0070] Here, satisfying the N:M sparsity constraint can include all mask blocks in the B sparse mask sets satisfying the N:M sparsity constraint by rows and / or columns. Optionally, the N:M sparsity constraint can be that all mask blocks satisfy the N:M sparsity constraint by both rows and columns. Therefore, during the training of both forward and backward neural networks, the mask matrix can be used to reduce the number of weights actually used for training in the weight matrix, thereby improving training efficiency.
[0071] Any of the mask blocks mentioned above can be composed of elements with values of 0 or 1 (e.g., binary numbers 0 or 1). Thus, during neural network training, the elements with values of 1 in the mask matrix are actually used to select a subset of weights from the weight matrix (i.e., the weights that satisfy the N:M sparse constraint) for training, thereby improving training efficiency by approximately M / N times.
[0072] Specifically, the following scheme can be used to generate B mask blocks for B weight blocks, and then generate a mask matrix based on the B mask blocks.
[0073] Option 1 (Fully Randomized Option) In one possible design, such as Figure 5 As shown, S403 (i.e., determining B mask blocks using B random number indices and B sparse mask sets) includes: S4031: Based on B random number indices, randomly select B mask blocks from B sparse mask sets for B weight blocks.
[0074] Here, the B random number indices can be generated by a random function with a random range of [1, D].
[0075] Accordingly, S404 (i.e., generating a mask matrix using B mask blocks) includes: S4041: Generate a mask matrix using B mask blocks selected from B sparse mask sets, wherein the position of the i-th mask block in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
[0076] The term "overlapping positions" here refers to the fact that, when performing sparse matrix multiplication, all elements in the i-th weight block of the weight matrix can be multiplied one by one with all elements in the i-th mask block of the mask matrix without adjusting their positions or order.
[0077] For example, such as Figure 6 As shown in (a), assume the weight matrix is divided into 8 weight blocks, numbered from 1 to 8. Then, as... Figure 6As shown in (b), by using a fully randomized method, eight random number indices for the mask blocks can be generated for each of the eight weight blocks, and each index satisfies the N:M sparsity constraint. The generated random number indices are as follows: Figure 6 The numbers are shown in italics. Then, based on these 8 random number indices, 8 mask blocks can be selected from the corresponding 8 sparse mask sets, and a mask matrix can be generated using the selected 8 mask blocks, as shown below. Figure 6 As shown in (c) in the diagram. Here, the positions of the eight selected mask blocks in the mask matrix coincide with the positions of the eight weight blocks in the weight matrix.
[0078] It should be noted that, for weight blocks and mask blocks with defined sizes (the size of the mask block corresponds to the size of the weight block), the maximum number of mask block types that can satisfy the N:M sparsity constraint can also be determined. For example, suppose... Figure 6 The weighted block size is 4×4, and the sparse constraint of N:M=2:4 is satisfied in both rows and columns. There are a total of 90 mask blocks, corresponding to indices 1 to 90. Accordingly, the random range of the generated mask block index can be [1,D], where D is a positive integer and D=90.
[0079] Furthermore, if the training performance of some mask blocks is known to be consistently good, the range of random number index values can be narrowed down to a subset composed of the indices of these mask blocks. For ease of operation, continuous indices can also be regenerated for these mask blocks; however, this embodiment of the invention does not limit this approach.
[0080] Therefore, a fully randomized method is used to generate B mask blocks for each of the B weight blocks, thereby generating a mask matrix. This eliminates the need for pre-training a large-scale dense weight matrix and for fine-tuning the trained model to meet the N:M sparse constraint, thus saving significant training time and improving training efficiency.
[0081] Option 2 (Partial Replacement) In another possible design, such as Figure 7 As shown, S403 (i.e., determining B mask blocks using B random number indices and B sparse mask sets) includes: S4032: Based on B random number indices, randomly select B mask blocks from B sparse mask sets for B weight blocks respectively.
[0082] S4033: Obtain X additional mask blocks, where the X additional mask blocks are the mask blocks that give the highest score to the X weight blocks the most times within one or more predefined time periods, where X is an integer and 0≤X≤B.
[0083] S4034: Update B mask blocks by replacing X mask blocks from B mask blocks selected from B sparse mask sets with X additional mask blocks.
[0084] Accordingly, S404 (i.e., generating a mask matrix using B mask blocks) includes: S4042: Generate a mask matrix using the updated B mask blocks, wherein the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
[0085] Therefore, X additional mask blocks with better training performance can be used to replace X mask blocks randomly generated from these X weight blocks (since they are randomly generated, their training performance is unknown). In other words, some or all of the mask blocks with long-term optimal performance in terms of training performance can be used to replace the mask blocks with unknown training performance, thereby improving the overall quality of the B mask blocks actually used to generate the mask matrix and thus improving the training performance.
[0086] Optionally, the i-th mask block in the mask matrix generated using the updated B mask blocks satisfies: Ai = Fi + (1 – sign(Fi)) Ri Where Ai is the index of the i-th mask block in the mask matrix generated using the updated B mask blocks, Ri is the index of the i-th mask block among the B mask blocks selected from the B sparse mask sets, Fi is the index of the mask block corresponding to the i-th weight block among the X additional mask blocks, and sign(Fi) is the index sign function that satisfies the following condition:
[0087] in, This indicates that the mask block corresponding to the i-th weight block among B weight blocks has not been determined. This means that the mask block corresponding to the i-th weight block in B weight blocks is determined as the mask block with the specified index Fi in the i-th sparse mask set.
[0088] In other words, by using the index of a previously determined and stored mask block (the initial value of which is less than 1 or greater than D, and the updated value of which is greater than or equal to 1 and less than or equal to D), X randomly generated mask blocks from B mask blocks can be replaced with X previously determined additional mask blocks. For example, the "X determined additional mask blocks" could be mask blocks as described above that most frequently give the highest score to the X weight blocks out of B weight blocks, thereby improving the quality of the mask matrix and enhancing training performance.
[0089] For example, combining Figure 6 as well as Figure 8 As shown in (a) and (b), eight random number indices are randomly generated for the eight weight blocks. The random number indices of the mask blocks corresponding to the 1st and 6th weight blocks are 85 and 59, respectively. It is known that for the 1st and 6th weight blocks, the training performance of the mask blocks with specified indices of 48 and 75 is stable and efficient in the long term, i.e., B=8 and X=2. Then, as shown in (a) and (b), the training performance of the mask blocks with specified indices of 48 and 75 is stable and efficient in the long term. Figure 8 As shown in (c), during the process of generating the mask matrix for these 8 weight blocks, the mask blocks with indices 48 and 75 are used to replace the mask blocks with random number indices 85 and 59, respectively. For ease of distinction, Figure 8 The indices of the 1st and 6th weight blocks, as well as the random number index and specified index of the corresponding mask block, are shown in bold.
[0090] Option 3 (Merger Option) In another possible design, such as Figure 9 As shown, S403 (i.e., determining B mask blocks using B random number indices and B sparse mask sets) includes: S4035: Based on B–X random number indices, randomly select B–X mask blocks from B–X sparse mask sets for B–X weight blocks, where X is an integer and 0≤X≤B; S4036: Obtain X additional mask blocks, wherein the X additional mask blocks are the mask blocks that give the highest score the most times to the X weight blocks within one or more predefined time periods; S4037: Update B mask blocks by merging B–X mask blocks and X additional mask blocks.
[0091] Accordingly, S404 (i.e., generating a mask matrix using B mask blocks) includes: S4043: Generate a mask matrix using the updated B mask blocks, wherein the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
[0092] For example, combining Figure 8 as well as Figure 10 As shown in (a) and (b), since it is known that the training performance of mask blocks with specified indices of 48 and 75, respectively, is stable and efficient in the long run for the 1st and 6th weight blocks, it is possible to generate 6 random number indices only for the other 6 weight blocks out of these 8 weight blocks. Therefore, as... Figure 10 As shown in (c), a mask matrix is generated using mask blocks with specified indices 48 and 75 and other mask blocks corresponding to 6 random number indices. For clarity, Figure 10The indices of the 1st and 6th weight blocks, as well as the specified indices of the corresponding mask blocks, are indicated in bold.
[0093] It should be noted that the variable used to store the mask block index can be initialized, and this initial value can be 0 (e.g., ...). Figure 10 The initial value can be any value (as shown in (b)) or other values (e.g., 91 or 95), as long as the initial value is not within the random range [1, D] of the mask block index. This embodiment of the invention does not limit this.
[0094] In other words, compared to the partial replacement scheme described above, if it is known that there are X additional mask blocks that most frequently give the highest scores to X weight blocks out of B weight blocks, then B mask blocks can be obtained to generate a mask matrix by directly using these X additional mask blocks and merging them with B–X mask blocks randomly generated for the other B–X weight blocks. This eliminates the need to repeatedly generate random number indices for these X weight blocks, thereby reducing the workload of randomly generating and replacing some mask blocks and improving training efficiency.
[0095] In one possible design, obtaining the X additional mask blocks from S4033 and S4036 above includes: Based on X specified indices, obtain X additional mask blocks from X sparse mask sets in B sparse mask sets.
[0096] Optionally, the method further includes: before obtaining X additional mask blocks from X sparse mask sets in B sparse mask sets based on X specified indices, Within one or more predefined time periods, determine X specified indices for X weight blocks out of B weight blocks.
[0097] Here, the predefined duration can be set according to actual needs, such as a period of time, one or more training cycles (rounds), or one or more training steps, etc., and this embodiment of the invention does not limit this. That is to say, the X specified indices can be determined over a relatively long period of time, that is, the X specified indices are the indices of the mask blocks that perform well over a relatively long period of time in terms of the training effect of X weight blocks out of B weight blocks, thereby ensuring the quality of the mask matrix and the training effect.
[0098] In addition, such as Figure 11 As shown, determining X specified indices for X weight blocks out of B weight blocks within one or more predefined time periods includes the following steps S1101 to S1103.
[0099] S1101: Determine E weight blocks and E additional mask blocks within one of a set of predefined durations, wherein the E additional mask blocks are the mask blocks that give the highest score to the E weight blocks the most times within the predefined duration, where E is an integer and 0≤E≤X.
[0100] In addition, the above S1101 (i.e., determining E weight blocks and E additional mask blocks) includes the following steps 1 to 4.
[0101] Step 1: Set all elements of the B×D counter matrix and all elements of the B-size index array to 0. For example... Figure 12 As shown in (a), the counter in the i-th row and j-th column of the counter matrix is used to count when the j-th mask block in the i-th sparse mask set gives the highest score to the i-th weight block; as Figure 12 As shown in (b), the i-th element in the index array is used to store the specified index of the mask block corresponding to the i-th weight block, where j is a positive integer and 1≤j≤D.
[0102] Step 2: Every certain number of training steps, determine the j-th mask block in the i-th sparse mask set as the score given by the i-th weight block.
[0103] Specifically, the score given to the i-th weight block by all mask blocks in the i-th sparse mask set corresponding to the i-th weight block can be calculated using the following two methods.
[0104] In Method 1, each of the B weight blocks is a sub-weight matrix of size K×P, where K and P are positive integers, and K=M and / or P=M; the j-th mask block in the i-th sparse mask set satisfies the following score given by the i-th weight block:
[0105] in, The score given to the j-th mask block in the i-th sparse mask set as the score given to the i-th weight block. This represents the nth weight value within the i-th weight block. Let n be the nth mask value of the jth mask block in the i-th sparse mask set, where n is a positive integer and 1 ≤ n ≤ K. P.
[0106] In Method 2, it is assumed that the B weight blocks are all sub-weight matrices of size K×P, where K and P are positive integers, and K=M and / or P=M; the B sparse mask sets are all the first sparse mask set. For example... Figure 13 As shown, step 2 above (i.e., determining the score given by the i-th weight block as the j-th mask block in the i-th sparse mask set) includes: Step (a): Generate a reconstruction matrix using B weight blocks, where the size of the reconstruction matrix is B×(K). P) or (K) P)×B, the i-th row or i-th column of the reconstructed matrix includes the absolute values of all weights of the i-th weight block in the B weight blocks; Step (b) generates a full mask matrix using the first sparse mask set, where the size of the full mask matrix is (K... P)×D or D×(K) P), the j-th column or j-th row of the full mask matrix includes all the mask values of the j-th mask block in the first sparse mask set, and the positions of all the mask values of the j-th mask block in the first sparse mask set in the full mask matrix coincide with the positions of the absolute values of all the weights of the i-th weight block in the reconstruction matrix. Step (c): Obtain the score matrix by performing matrix multiplication on the reconstructed matrix and the full mask matrix. The score matrix is of size B×D or D×B. The i-th row or i-th column of the score matrix stores D scores. The j-th score is the matrix product of the absolute values of all weights in the i-th weight block and all mask values in the j-th mask block.
[0107] Steps (a) through (c) can be repeated until all mask blocks in the first sparse mask set are determined to be the i-th weight block and a score is given.
[0108] As can be seen, since any element in the mask block can take the value of 0 or 1, the score given by all mask blocks in the i-th sparse mask set corresponding to the i-th weight block is the sum of the absolute values of the weights selected from the i-th weight block using the elements with the value of 1 in all mask blocks in the i-th sparse mask set. The higher this score, the better the sparse training effect.
[0109] Step 3: Find the additional mask block that gives the highest score to the i-th weight block, and increment the counter corresponding to the i-th weight block and the additional mask block by 1.
[0110] Step 4: Determine the weight blocks and mask blocks corresponding to the E counters with the E highest values in the counter matrix as E weight blocks and E additional mask blocks, and store the specified indices of the E additional mask blocks.
[0111] S1102: Determine X weight blocks and X additional mask blocks within one or more predefined durations.
[0112] S1103: Assign the X specified indices of the X additional mask blocks to the X weight blocks respectively.
[0113] That is, the X specified indexes may be determined all within one predefined duration (E=X), or determined in batches within more predefined durations (E<X). For example, the number (for example, X / total number of predefined durations) or proportion of weight blocks and mask blocks to be determined in each predefined duration may be set, which is not limited in this embodiment of the present invention.
[0114] The foregoing S1101 to S1104 may be repeatedly performed until B specified indexes are determined for the B weight blocks respectively, where an i-th specified index is an index of a mask block that is in an i-th sparse mask set and is given a highest score for an i-th weight block for most times.
[0115] It should be noted that, if a specified index has been determined for the i-th weight block, it may be considered that the i-th weight block has been frozen, or the i-th weight block has been bound to the mask block corresponding to the specified index, or a mapping relationship or correspondence has been established between them, that is, in a sparse matrix multiplication operation, the mask block corresponding to the specified index may be directly used to generate a full mask matrix, thereby reducing a workload of matrix multiplication and improving training efficiency.
[0116] In addition, if E=X=0 in the foregoing solutions 1 to 3, the solutions 2 and 3 are essentially the same as the solution 1.
[0117] The following describes with reference to an example shown in Figure 14 .
[0118] In the foregoing step 1, it is assumed that all weight blocks have a size of 4×4 (K=P=4, that is, all weight blocks are square), and all weight blocks correspond to a first mask set. Then there are 90 mask blocks in the first sparse mask set, which satisfy a 2:4 (N=2 and M=4) sparsity constraint both row-wise and column-wise. As shown in (a) and (b) in Figure 14 , a weight matrix is divided into B weight blocks (B=8), where a first weight block and a sixth weight block respectively correspond to mask blocks with specified indexes 48 and 75.
[0119] In the foregoing step 4, as shown in (c) in Figure 14 , in a counter matrix, except for a first row and a sixth row (which respectively correspond to the first weight block and the sixth weight block), in remaining six rows, a counter at a fourth row and a first column and a counter at a seventh row and an 88th column have highest count values (6 and 5 respectively), then as shown in (d) in Figure 14 , it is determined that a specified index of a mask block corresponding to a fourth weight block and a specified index of a mask block corresponding to a seventh weight block are 1 and 88 respectively.
[0120] It should be noted that, in the embodiments of the present invention, the random number indices generated separately for different weight blocks and the multiple random number indices generated for the same weight block are completely independent of each other, and the random range is [1, D].
[0121] This invention provides a computer program product including instructions that, when executed by a computing device, cause the computing device to implement the method described in the above-described method embodiments.
[0122] This invention provides a computer-readable storage medium storing a computer program or instructions, which, when executed on a computing device, causes the computing device to perform the methods described in the above-described method embodiments.
[0123] This invention provides a chip system including a processing circuit and a storage medium. The storage medium stores computer program instructions, which, when executed by the processing circuit, cause the chip system to implement the methods described in any of the above embodiments.
[0124] It should be understood that, in the embodiments of the present invention, the processor may be a central processing unit (CPU); or, the processor may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.
[0125] It should also be understood that the memory in the embodiments of the present invention can be volatile (transient) memory or non-volatile (non-transient) memory, or can include both volatile and non-volatile memory. Non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Volatile memory can be random access memory (RAM), which is used as an external cache. As can be seen from the exemplary but non-limiting description, there are various forms of random access memory (RAM), such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM), and direct rambus RAM (DR RAM).
[0126] The above embodiments can be implemented, in whole or in part, by software, hardware (e.g., circuitry), firmware, or any other combination thereof. When implemented in software, the above embodiments can be implemented, in whole or in part, as a computer program product. A computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer programs are loaded or executed on a computer, all or part of the processes or functions according to the embodiments of the present invention are implemented. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. Computer instructions can be stored in a computer-readable storage medium or transferred from one computer-readable storage medium to another. For example, computer instructions can be transferred from one website, computer, server, or data center to another via wired or wireless means (e.g., infrared, Wi-Fi, microwave, etc.). A computer-readable storage medium can be any available medium accessible to a computer, or a data storage device containing a collection of one or more available media, such as a server, data center, etc. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, and magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium, wherein the semiconductor medium can be a solid-state drive.
[0127] It should be understood that the term "and / or" in this document is used only to describe the relationship between related objects. It indicates that there may be three relationships: for example, A and / or B can mean that only A exists, A and B exist simultaneously, or only B exists, where A and B can be singular or plural. In addition, the character " / " in this document generally indicates that the related objects before and after the character are in an "or" relationship, but it can also mean an "and / or" relationship. The specific meaning should be understood in conjunction with the context.
[0128] In embodiments of the present invention, "at least one" refers to one or more, and "multiple" refers to two or more. The expression "at least one of the following" or similar expressions refer to any combination of these items, including any combination of a singular item (one) or a plural item (multiple). For example, at least one of a, b, or c can be represented as: a; b; c; a and b; a and c; b and c; or a, b, and c, wherein a, b, and c can each be singular or plural.
[0129] In embodiments of the present invention, the terms "exemplary," "for example," etc., are used to indicate examples, illustrations, or descriptions. Any embodiment or design described as "exemplary" in the present invention should not be construed as superior to other embodiments or designs. Rather, the term "for example" is used to present a concept in a specific manner.
[0130] It should be understood that in the various embodiments of the present invention, the sequence number of the above process does not indicate the order of execution. The order of execution should be determined by its function and inherent logic, and should not constitute any limitation on the process used to implement the embodiments of the present invention.
[0131] Those skilled in the art will recognize that the units and algorithm steps in the various examples described in conjunction with the embodiments disclosed herein can be implemented by electronic hardware or by a combination of computer software and electronic hardware. Whether these functions are performed by hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the invention.
[0132] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the working process of the above-described systems, devices, and modules can be referred to the corresponding process in the above-described method embodiments, and will not be repeated here.
[0133] In the embodiments provided by this invention, it should be understood that the disclosed systems, devices, and methods can be implemented in other ways. For example, the embodiments of the devices described above are merely exemplary. For example, the division of units is only a logical functional division. In actual implementation, there are other division methods. For example, multiple devices or components can be merged or integrated into another system, or some features can be ignored or not performed. Furthermore, the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through certain interfaces, devices, or units, and can take electrical, mechanical, or other forms.
[0134] The units described as individual components may or may not be physically separate. A component shown as a unit may or may not be a single physical unit; that is, the component may be located in one place or distributed across multiple different network units. Some or all of the units can be selected to achieve the purpose of the embodiment scheme according to actual needs.
[0135] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0136] When implemented as a software functional unit and sold or used as an independent product, the described functions can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention is essentially either a contribution to the prior art, or a part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the embodiments of the present invention. The aforementioned storage medium includes a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or optical disk, and other media capable of storing program code.
[0137] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be conceived by those skilled in the art within the technical scope of the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A neural network training method, characterized in that, The method includes: The weight matrix is divided into B weight blocks, each of which is of equal size. B sparse mask sets are defined, each consisting of D mask blocks satisfying the N:M sparsity constraint, where N, M, B, and D are all positive integers.<M、B> 1 and D>1; Generate B random number indices corresponding to the B weight blocks, wherein the random range of the B random number indices is [1, D]. B mask blocks are determined using the B random number indices and the B sparse mask sets, wherein the B mask blocks correspond to the B weight blocks; Use the B mask blocks to generate a mask matrix; The mask matrix is used to perform sparse matrix multiplication.
2. The method according to claim 1, characterized in that, Determining the B mask blocks using the B random number indices and the B sparse mask set includes: Based on the B random number indices, B mask blocks are randomly selected from the B sparse mask sets for the B weight blocks; Obtain X additional mask blocks, wherein the X additional mask blocks are the mask blocks that give the highest score the most times to the X weight blocks among the B weight blocks within one or more predefined time periods, where X is an integer and 0≤X≤B; The B mask blocks are updated by replacing X mask blocks from the B mask blocks selected from the B sparse mask sets with the X additional mask blocks; Generating the mask matrix using the B mask blocks includes: The updated B mask blocks are used to generate the mask matrix, wherein the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
3. The method according to claim 2, characterized in that, The i-th mask block in the mask matrix generated using the updated B mask blocks satisfies: Ai=Fi+(1–sign(Fi)) countries Where Ai is the index of the i-th mask block in the mask matrix generated using the updated B mask blocks, Ri is the index of the i-th mask block among the B mask blocks selected from the B sparse mask sets, Fi is the index of the mask block corresponding to the i-th weight block among the X additional mask blocks, and sign(Fi) is the index sign function that satisfies the following condition: in, This indicates that the mask block corresponding to the i-th weight block among the B weight blocks has not been determined. This indicates that the mask block corresponding to the i-th weight block among the B weight blocks is determined as the mask block with the specified index Fi in the i-th sparse mask set.
4. The method according to claim 1, characterized in that, Determining the B mask blocks using the B random number indices and the B sparse mask set includes: Based on B–X random number indices, B–X mask blocks are randomly selected from B–X sparse mask sets for B–X weight blocks, where X is an integer and 0≤X≤B; Obtain X additional mask blocks, wherein the X additional mask blocks are the mask blocks that give the highest score the most times to the X weight blocks among the B weight blocks within one or more predefined time periods; The B mask blocks are updated by merging the B–X mask blocks and the X additional mask blocks; Generating the mask matrix using the B mask blocks includes: The updated B mask blocks are used to generate the mask matrix, wherein the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
5. The method according to claim 2 or 4, characterized in that, Obtaining the X additional mask blocks includes: The X additional mask blocks are obtained from the X sparse mask sets in the B sparse mask sets based on the X specified indexes.
6. The method according to claim 5, characterized in that, The method further includes: before obtaining the X additional mask blocks from the X sparse mask sets in the B sparse mask sets based on the X specified indices. Within one or more predefined time periods, determine the X specified indices for the X weight blocks in the B weight blocks respectively.
7. The method according to claim 6, characterized in that, Determining the X specified indices for the X weight blocks in the B weight blocks within the one or more predefined time periods includes: Within one of the one or more predefined durations, determine E weight blocks and E additional mask blocks, wherein the E additional mask blocks are the mask blocks that give the highest score to the E weight blocks the most times within the predefined duration, where E is an integer and 0≤E≤X; Determine the X weight blocks and the X additional mask blocks within one or more predefined time periods; The X specified indices of the X additional mask blocks are respectively assigned to the X weight blocks.
8. The method according to claim 7, characterized in that, Determining the E weight blocks and the E additional mask blocks includes: All elements in a counter matrix of size B×D and all elements in an index array of size B are set to 0. The counter in the i-th row and j-th column of the counter matrix is used to count when the j-th mask block in the i-th sparse mask set gives the highest score to the i-th weight block. The i-th element in the index array is used to store the specified index of the mask block corresponding to the i-th weight block, where j is a positive integer and 1≤j≤D. Every certain number of training steps, the j-th mask block in the i-th sparse mask set is determined as the score given by the i-th weight block; Find the additional mask block that gives the highest score for the i-th weight block, and increment the value of the counter corresponding to the i-th weight block and the additional mask block by 1; The weight blocks and mask blocks corresponding to the E counters with the E highest values in the counter matrix are determined as the E weight blocks and the E additional mask blocks, and the specified indices of the E additional mask blocks are stored.
9. The method according to claim 8, characterized in that, The B weight blocks are all sub-weight matrices of size K×P, where K and P are positive integers, and K=M and / or P=M; The B sparse mask sets are all the first sparse mask set; The score given for determining the j-th mask block in the i-th sparse mask set as the i-th weight block includes: A reconstruction matrix is generated using the B weight blocks, wherein the size of the reconstruction matrix is B×(K). P) or (K) P)×B, where the i-th row or i-th column of the reconstruction matrix includes the absolute values of all weights of the i-th weight block in the B weight blocks; A full mask matrix is generated using the first sparse mask set, wherein the size of the full mask matrix is (K... P)×D or D×(K) P), the j-th column or j-th row of the full mask matrix includes all the mask values of the j-th mask block in the first sparse mask set, and the positions of all the mask values of the j-th mask block in the first sparse mask set in the full mask matrix coincide with the positions of the absolute values of all the weights of the i-th weight block in the reconstruction matrix; A score matrix is obtained by performing matrix multiplication on the reconstructed matrix and the full mask matrix, wherein the size of the score matrix is B×D or D×B, the i-th row or i-th column of the score matrix stores D scores, and the j-th score is the matrix product of the absolute values of all weights in the i-th weight block and all mask values in the j-th mask block.
10. The method according to claim 1, characterized in that, Determining the B mask blocks using the B random number indices and the B sparse mask set includes: Based on the B random number indices, B mask blocks are randomly selected from the B sparse mask sets for the B weight blocks; Generating the mask matrix using the B mask blocks includes: The mask matrix is generated using the B mask blocks selected from the B sparse mask sets, wherein the position of the i-th mask block in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
11. A computing device, characterized in that, include: Memory; At least one processor coupled to the memory, wherein, The memory is used to store computer instructions that, when executed by the at least one processor, cause the computing device to perform the following operations: The weight matrix is divided into B weight blocks, each of which is of equal size. B sparse mask sets are defined, each consisting of D mask blocks satisfying the N:M sparsity constraint, where N, M, B, and D are all positive integers.<M、B> 1 and D>1; Generate B random number indices corresponding to the B weight blocks, wherein the random range of the B random number indices is [1, D]. B mask blocks are determined using the B random number indices and the B sparse mask sets, wherein the B mask blocks correspond to the B weight blocks; Use the B mask blocks to generate a mask matrix; The mask matrix is used to perform sparse matrix multiplication.
12. The computing device according to claim 11, characterized in that, When executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: Based on the B random number indices, B mask blocks are randomly selected from the B sparse mask sets for the B weight blocks; Obtain X additional mask blocks, wherein the X additional mask blocks are the mask blocks that give the highest score the most times to the X weight blocks among the B weight blocks within one or more predefined time periods, where X is an integer and 0≤X≤B; The B mask blocks are updated by replacing X mask blocks from the B mask blocks selected from the B sparse mask sets with the X additional mask blocks; The updated B mask blocks are used to generate the mask matrix, wherein the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
13. The computing device according to claim 12, characterized in that, The i-th mask block in the mask matrix generated using the updated B mask blocks satisfies: Ai=Fi+(1–sign(Fi)) countries Where Ai is the index of the i-th mask block in the mask matrix generated using the updated B mask blocks, Ri is the index of the i-th mask block among the B mask blocks selected from the B sparse mask sets, Fi is the index of the mask block corresponding to the i-th weight block among the X additional mask blocks, and sign(Fi) is the index sign function that satisfies the following condition: in, This indicates that the mask block corresponding to the i-th weight block among the B weight blocks has not been determined. This indicates that the mask block corresponding to the i-th weight block among the B weight blocks is determined as the mask block with the specified index Fi in the i-th sparse mask set.
14. The computing device according to claim 11, characterized in that, When executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: Based on B–X random number indices, B–X mask blocks are randomly selected from B–X sparse mask sets for B–X weight blocks, where X is an integer and 0≤X≤B; Obtain X additional mask blocks, wherein the X additional mask blocks are the mask blocks that give the highest score the most times to the X weight blocks among the B weight blocks within one or more predefined time periods; The B mask blocks are updated by merging the B–X mask blocks and the X additional mask blocks; The updated B mask blocks are used to generate the mask matrix, wherein the position of the i-th mask block in the updated B mask blocks in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
15. The computing device according to claim 12 or 14, characterized in that, When executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: The X additional mask blocks are obtained from the X sparse mask sets in the B sparse mask sets based on the X specified indexes.
16. The computing device according to claim 15, characterized in that, When executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: Before obtaining the X additional mask blocks from the X sparse mask sets in the B sparse mask sets based on the X specified indices, the X specified indices are determined for the X weight blocks in the B weight blocks respectively within one or more predefined time periods.
17. The computing device according to claim 16, characterized in that, When executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: Within one of the one or more predefined durations, determine E weight blocks and E additional mask blocks, wherein the E additional mask blocks are the mask blocks that give the highest score to the E weight blocks the most times within the predefined duration, where E is an integer and 0≤E≤X; Determine the X weight blocks and the X additional mask blocks within one or more predefined time periods; The X specified indices of the X additional mask blocks are respectively assigned to the X weight blocks.
18. The computing device according to claim 16, characterized in that, When executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: All elements in a counter matrix of size B×D and all elements in an index array of size B are set to 0. The counter in the i-th row and j-th column of the counter matrix is used to count when the j-th mask block in the i-th sparse mask set gives the highest score to the i-th weight block. The i-th element in the index array is used to store the specified index of the mask block corresponding to the i-th weight block, where j is a positive integer and 1≤j≤D. Every certain number of training steps, the j-th mask block in the i-th sparse mask set is determined as the score given by the i-th weight block; Find the additional mask block that gives the highest score for the i-th weight block, and increment the value of the counter corresponding to the i-th weight block and the additional mask block by 1; The weight blocks and mask blocks corresponding to the E counters with the E highest values in the counter matrix are determined as the E weight blocks and the E additional mask blocks, and the specified indices of the E additional mask blocks are stored.
19. The computing device according to claim 18, characterized in that, The B weight blocks are all sub-weight matrices of size K×P, where K and P are positive integers, and K=M and / or P=M; The B sparse mask sets are all the first sparse mask set; When executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: A reconstruction matrix is generated using the B weight blocks, wherein the size of the reconstruction matrix is B×(K). P) or (K) P)×B, where the i-th row or i-th column of the reconstruction matrix includes the absolute values of all weights of the i-th weight block in the B weight blocks; A full mask matrix is generated using the first sparse mask set, wherein the size of the full mask matrix is (K... P)×D or D×(K) P), the j-th column or j-th row of the full mask matrix includes all the mask values of the j-th mask block in the first sparse mask set, and the positions of all the mask values of the j-th mask block in the first sparse mask set in the full mask matrix coincide with the positions of the absolute values of all the weights of the i-th weight block in the reconstruction matrix; A score matrix is obtained by performing matrix multiplication on the reconstructed matrix and the full mask matrix, wherein the size of the score matrix is B×D or D×B, the i-th row or i-th column of the score matrix stores D scores, and the j-th score is the matrix product of the absolute values of all weights in the i-th weight block and all mask values in the j-th mask block.
20. The computing device according to claim 11, characterized in that, When executed by the at least one processor, the computer instructions cause the computing device to perform the following operations: Based on the B random number indices, B mask blocks are randomly selected from the B sparse mask sets for the B weight blocks; The mask matrix is generated using the B mask blocks selected from the B sparse mask sets, wherein the position of the i-th mask block in the mask matrix coincides with the position of the i-th weight block in the weight matrix, where i is a positive integer and 1≤i≤B.
21. A computer-readable storage medium storing computer instructions, characterized in that, When executed by a computer, the computer instructions cause the computer to perform the method according to any one of claims 1 to 10.
22. A computer program product comprising instructions, characterized in that, When executed by a computer, the instructions cause the computer to implement the method according to any one of claims 1 to 10.
23. A chip system, characterized in that, The system includes processing circuitry and a storage medium, wherein the storage medium stores computer instructions that, when executed by the processing circuitry, cause the chip system to implement the method according to any one of claims 1 to 10.
