System and method for sparse matrix multiplication
By combining fine-grained and coarse-grained balanced sparsity in deep neural networks to prune matrix blocks, the problem of sparsity pruning in the existing technology being difficult to achieve high sparsity and accuracy is solved, and efficient calculation of matrix multiplication is achieved. .
Patent Information
- Application Number
- CN202380027149.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2022-04-04
- Filing Date
- 2023-01-24
- Publication Date
- 2024-10-29
AI Technical Summary
When existing technologies reduce computational costs through sparsity pruning in deep neural networks, it is difficult to achieve high sparsity levels without loss of accuracy, and may result in information loss or no reduction in computational costs when implemented on hardware.
Prune the first block using fine-grained balanced sparsity, and prune the second block using coarse-grained balanced sparsity, maintaining consistent combinatorial sparsity in matrix multiplication through a combination of different sparsity modes and avoiding excessive sparsity ization and computational costs are not reduced.
Consistent combinatorial sparsity in matrix multiplication is achieved without increasing computational burden, reducing computational resource usage and maintaining model accuracy.
Smart Images

Figure CN118871902A_ABST
Abstract
Description
Background Art
[0001] Deep neural networks (DNNs) can be used in machine learning to build artificial intelligence models. A deep learning workload includes input data, a weight matrix learned during supervised training, and an activation matrix calculated from the input data and the weight matrix. As computing resources expand, larger data sets can be processed, so DNNs need to be scaled up. Sparsity can be used as a tool to reduce the amount of computation and / or memory consumed for operations required during training of the DNN and / or during reasoning when deploying the trained DNN. Summary of the invention
[0002] This summary is provided to introduce in simplified form some concepts further described in the following detailed description. This summary is not intended to identify key features or essential features of the claimed subject matter, nor is it intended to be used to limit the scope of the claimed subject matter. Furthermore, the claimed subject matter is not limited to implementations that solve any or all of the disadvantages mentioned in any part of this disclosure.
[0003] A method for sparse matrix multiplication includes receiving a first block having M elements in a first dimension and parsing the first block of M elements into a first set of B sub-blocks including M / B elements in the first dimension. Applying a first sparsity mask having S% sparsity to the first block of elements so that each sub-block in the first set of B sub-blocks has S% sparsity. Receive a second block having M elements in a second dimension. Parsing the second block of elements into a second set of B sub-blocks including M / B elements in the second dimension. Applying a second sparsity mask having S'% sparsity to the second block of elements so that S'% of the second set of S'% sub-blocks has 100% sparsity and (100-S')% of the second set of B sub-blocks has 0% sparsity. Then matrix multiplication is performed on the first block and the second block. BRIEF DESCRIPTION OF THE DRAWINGS
[0004] Figure 1 An example system for training a neural network is schematically shown.
[0005] Figure 2 An example of intensive training of a neural network is schematically shown.
[0006] Figure 3 An example of sparse training of a neural network is schematically shown.
[0007] Figure 4 A simple matrix sparsification is schematically shown.
[0008] Figure 5An unstructured sparsity mask and a balanced sparsity mask are schematically shown.
[0009] Figure 6 The method for matrix multiplication is schematically shown.
[0010] Figure 7 The method for sparse matrix multiplication is schematically shown.
[0011] Figure 8 A flow chart of a method for sparse matrix multiplication is shown.
[0012] Fig. 9 The method for sparse matrix multiplication of the present disclosure is schematically illustrated.
[0013] Fig.10 An example computing system is schematically depicted. DETAILED DESCRIPTION
[0014] Over the past few years, the size of deep neural networks has grown exponentially to achieve greater accuracy. These large models result in high computational costs during both training and inference. Sparsity is a common technique used to prune models to reduce the number of parameters and thus reduce their computational cost.
[0015] Sparsity can be implemented as structured sparsity or unstructured sparsity. Unstructured sparsity allows high degrees of freedom for pruning, but is generally not hardware-friendly. On the other hand, structured sparsity can be implemented efficiently in hardware, but may result in a significant reduction in model accuracy.
[0016] Balanced sparsity is a specific kind of structured sparsity that provides a balance between structured sparsity and unstructured sparsity. For example, balanced sparsity can include simply taking each row in a matrix and then applying percentage sparsity to the elements in a row-by-row manner.
[0017] For fine-grained balanced sparsity, the tensor can first be flattened into multiple blocks of size "B" each; (e.g., each row of the tensor matrix is divided into multiple smaller blocks of equal number of elements). Then, within each block, the same percentage of sparsity is applied so as to prune the same percentage of elements within each block. In this way, sparsity is balanced across all blocks in each row. For inference, one-dimensional blocks (e.g., rows / columns) are typically used. In training, blocks can be two-dimensional because the weight matrix needs to be transposed for backpropagation. Multiple rows can be grouped together, where the same mask pattern is applied to each row of the group, or a mask can be created for each row individually, which is then divided into multiple blocks.
[0018] In order to achieve higher sparsity levels without a significant loss in accuracy, and to reduce imbalance when loading tensors, it may be necessary to prune both the weight tensors and the activation tensors. For example, 50% sparsity may be applied to the weight matrix, and 50% sparsity may be applied independently to the corresponding activation matrix to achieve an average combined sparsity of 75% during a matrix-matrix multiplication (matmul) operation.
[0019] In this example, while the combined sparsity of the resulting matrix averages 75% across each block, the local block sparsity for each block varies between 50% and 100% depending on the amount of overlap between the pruning mask of the weight matrix and the pruning mask of the activation matrix.
[0020] When the combined sparsity is much higher than the expected average within a block (e.g., close to 100%), a significant amount of information may be lost without any additional improvement in computational cost in hardware. This can result in a significant loss of accuracy. Conversely, when the combined sparsity is lower than the expected average, some of the additional non-zeros are eventually intentionally dropped from the computation by the hardware to keep the computational cost within the allocated budget. Therefore, it is desirable to keep the sparsity level within each block consistently close to the average.
[0021] To reduce variability and achieve more consistent sparsity, systems and methods are presented herein in which a first block is pruned using fine-grained balanced sparsity and a second block is pruned using coarse-grained balanced sparsity. In this way, the resulting combined sparsity is consistently achieved without any additional computational burden. For coarse-grained sparsity, the applied sparsity percentage is applied at the level of sub-blocks, rather than at the level of individual elements. By combining these together, the patterns of the two blocks complement each other in such a way that the desired percentage of elements is maintained from each block without the risk of over-sparseness.
[0022] Figure 1 An example system 100 for training a neural network 102 is shown. In this example, training data 104 is used to train parameters of the neural network 102, such as weights and / or gradients of the neural network 102. The training data 104 may be processed over multiple epochs to obtain a final set of trained model parameters. As used herein, an "epoch" occurs when a complete set of training data 104 has been processed once.
[0023] The neural network 102 includes an input layer 110, one or more hidden layers 112, and an output layer 114. Each layer includes a plurality of nodes 120. A training supervisor 122 may provide training data 104 to the input layer 110 of the neural network 102. In some examples, the training data 104 may be divided into small batches and / or shards for distribution to subsets of the input. The training supervisor 122 may include one or more network-accessible computing devices that are programmed to provide services responsible for managing resources for training jobs. The training supervisor 122 may also provide information and instructions about the training process to each node 120.
[0024] In this example, nodes 120 of the model receive input values on input layer 110 and produce output results on output layer 114 during forward processing, or inference (125). During training, data flows in the opposite direction during backpropagation (127), where the error between the network's results and the expected results is determined at the output, and weights are updated layer by layer from output layer 114 to input layer 110.
[0025] Each node 120 may include one or more agents 130 configured to supervise one or more workers 132. In general, each node 120 includes multiple workers 132, and the agent 130 can monitor multiple workers. Each node may also include multiple agents 130. The node 120 may be implemented using a central processing unit (CPU), a graphics processing unit (GPU), a combination of a CPU and a GPU, or any combination of CPU, GPU, ASIC, and / or other computer programmable hardware. The agents 130 and workers 132 in a public node 120 may share certain resources, such as one or more local networks, storage subsystems, local services, etc.
[0026] Each agent 130 may include an agent processing unit 134, a training process 136, and an agent memory 138. Each worker 132 may include a worker processing unit 142 and a worker memory 144. In general, the agent processing unit 134 is described as being implemented using a CPU, while the worker processing unit 142 is implemented using a GPU. However, other configurations are possible. For example, some or all aspects may be additionally or alternatively implemented in a cloud computing environment. A cloud computing environment may include a model for enabling on-demand network access to a shared pool of configurable computing resources. Such a shared pool of configurable computing resources can be quickly provisioned via virtualization and then scaled accordingly. The cloud computing model may consist of various features, such as on-demand self-service, extensive network access, resource pooling, rapid elasticity, measured services, and the like.
[0027] A deep learning model (or "network") comprises a graph of parameterizable layers (or "operators") that together implement complex nonlinear functions. The network can be trained via a training dataset that includes pairs of input examples (x) and outputs (y). The desired output is a learned function that can be parameterized by weights (w) such that given an input (x), the prediction f(x; w) is close to (y).
[0028] Applying a function f(x; w) to generate an output is performed by transforming the input (x) layer by layer - a process known as inference. In a training setting, this is known as the forward pass. Providing a network to solve a specific task involves two stages - designing the network structure and training the network's weights. Once designed, the network structure as a whole is not changed during the training process.
[0029] The training iterations begin with a forward pass, which is similar to inference, but in which the inputs to each layer are stored. The quality of the result of the forward pass f(x; w) is evaluated using a loss function l to estimate the accuracy of the prediction. The following backward pass propagates the loss (e.g., error) from the last layer in the opposite direction. At each parameter (e.g., learnable) layer, the backward pass uses the adjoint of the forward operation to calculate the gradient g and updates the parameters or weights using a learning rule to reduce l. This process is repeated iteratively for many examples until the function f(x; w) provides the desired accuracy.
[0030] As an example, Figure 2 A multi-layer neural network 200 is schematically shown, including an input layer (x0) 202, two hidden layers (x1) 204 and (x2) 206, and an output layer (x3) 208. In this example, the input layer 202 includes 5 neurons (210, 211, 212, 213, 214), the first hidden layer 204 includes 3 neurons (220, 221, 222), the second hidden layer 206 includes 4 neurons (230, 231, 232, 233), and the output layer 208 includes 3 neurons (241, 242, 243).
[0031] The neural network 200 includes an activation function, such as a rectified linear unit (not shown). The neural network 200 can be parameterized by a weight matrix w1 250, a weight matrix w2 251, and a weight matrix w3 252 and a bias vector (not shown). Each weight matrix includes weights for each connection between two adjacent layers. The forward pass can include a series of matrix-vector products f(x0; w), where x0 is an input vector or feature vector.
[0032] The size of deep neural networks such as network 200 rapidly outstrips the ability of hardware to quickly store and train the neural network. Sparsity can be applied by pruning edges from the base topology to reduce the number of network parameters before, during, and after training. Figure 3 A sparsified version 300 of network 200 is shown, including an input layer (x0') 302, a hidden layer (x1') 304 and a hidden layer (x2') 306, and an output layer (x3') 308. In this example, the third input feature 212 and all of its adjacent weights are removed from the input layer (x0') 302 (dashed lines). In addition, hidden neurons 222 and hidden neurons 232 and their weights are removed from hidden layers (x1') 304 and hidden layers (x2') 306, respectively. Various other weights are removed from sparsified version 300, yielding weight matrices (w1') 350, weight matrices (w2') 351, and weight matrices (w3') 352. In this manner, neurons or input features are removed corresponding to rows or columns in the removed layer weight matrices. Individual weights are removed corresponding to removing individual elements of the weight matrices. Sparsity can be induced or occur naturally, and can be applied to other tensors and matrices, such as matrices for activations, errors, biases, etc.
[0033] For activations, turning off activations for a node essentially generates zero outputs. Sparsity applied to activations can work the same way, for example, activations of higher magnitude have higher value to the network and are retained. In some examples, activations naturally approach sparsity, so real sparsity can be added with a modest effect. During inference, as new data is introduced into the neural network, the activation matrix changes during each pass. In this way, a pruning metric can be applied during each pass, and then a new mask is calculated based on this operation.
[0034] Sparsifying the weight matrix or other matrices or tensors effectively reduces the complexity of matrix multiplication events that utilize the matrix. In general, the speed of matrix multiplication is directly related to the sparsity of the matrix. Applying 75% sparsity to the weight matrix and 0% sparsity to the activation can increase the speed of the process by 4 times. Another way to achieve a 4-fold speed increase is to apply 50% sparsity to the activation and 50% sparsity to the weight. Therefore, a balance can be made by allocating sparsity between weights and activations.
[0035] For example, in Figure 4In [the figure], a heatmap 410 of an 8×8 weight matrix to be sparsified is shown. Brighter shaded blocks represent higher values. A simple high-pass filter can be applied to take the highest values to form a sparse matrix 420. However, using a simple filter like this leaves unbalanced rows and columns. Thus, while effectively reducing the complexity of any subsequent matrix multiplication, a more thoughtful scheme for sparsity can simplify the matrix even more, allowing for more targeted matrix compression.
[0036] For unstructured sparsity, the mask has few constraints and can essentially be configured in any random pattern. In Figure 5 [the figure], mask 510 is an example of unstructured sparsity. Each black square masks the underlying value to 0. Each white square allows the underlying value to be non-zero (e.g., the assigned value). The numbers on the grid axes are the count for that row or column - e.g., how many non-zero values are present in that dimension. For example, the top row of mask 510 has one white square (non-zero value), and the second column from the left of mask 510 has two white squares (non-zero values). This convention is used throughout the present disclosure.
[0037] Unstructured sparsity is generally applied after the network is trained, but can also be applied during training under some conditions. Unstructured sparsity is the least constrained form of sparsity, but its inherent randomness makes it difficult to accelerate at the hardware level. The size of each sparsity block is equal to the size of the tensor. As the block size increases, the fidelity also increases because more flexibility can be exploited to represent different configurations. However, as the block size increases beyond a threshold, the gains diminish.
[0038] The most common constraint for balanced sparsity is N out of M constraints. That is, for a column or row with M values, only N (N < M) can be non-zero. For example, mask 520 is an example of balanced sparsity with N = 1 value. Each row of mask 520 has one white square (non-zero value). The columns of mask 520 range from 0 to 2 non-zero values.
[0039] Thus, balanced sparsity is more constrained than unstructured sparsity, but is easier to accelerate using hardware because the hardware can anticipate what to expect from each constrained row or constrained column. The known constraints can be pre-loaded into the hardware. For balanced random fine-grained sparsity, "fine-grained" means that only a portion of the tensor is sparsified, while "balanced" means that all blocks (e.g., rows, columns) have the same level of sparsity, but within each block, the pattern is random.
[0040] Pruning the matrix saves computation and memory during many matrix multiplications performed during the execution of a neural network (whether during training, fine-tuning, or inference). Figure 6 A method 600 for matrix multiplication is schematically shown. A first matrix (A) 602 is multiplied by a second matrix (B) 604 to produce a third matrix (C) 606 .
[0041] The first matrix (A) 602 is characterized by a height 610 and a width 612 based on the number of matrix elements. The second matrix (B) 604 has a height 620 and a width 622. In general, the internal dimensions (here, the width 612 of the first matrix (A) and the height 620 of the second matrix (B)) are set to an equal number of matrix elements so that multiplying the first matrix (A) 602 with the second matrix (B) 604 produces a third matrix (C) 606 having a height 630 and a width 632 that is equal in dimension to the width 612 of the first matrix (A) 602 and the height 620 of the second matrix (B) 604. The height 610 of the first matrix (A) 602 and the width 622 of the second matrix (B) 604 are not constrained to be equal dimensions. The first matrix (A) 602 and the second matrix (B) 604 may represent an activation matrix and a weight matrix, or other combinations of matrices.
[0042] It is common to divide matrices into smaller, more consistent sub-matrices for implementing matmul in hardware. As shown, the first matrix (A) 602 includes at least a first sub-block A (1,0) 640 and the second sub-block A (1,1) 642, and the second matrix (B) 604 includes at least the first sub-block B (0,1) 644 and the second sub-block B (1,1) 646, each of the sub-blocks has a block size 650. In this example, the sub-blocks are square, with equal height and width. However, as will be further described herein, the sub-blocks may alternatively be rectangular or linear.
[0043] Thus, when performing matrix multiplication, the first sub-block A (1,0) 640 multiplied by the first sub-block B (0,1) 644, and the sub-block C of the third matrix (C) 606 (1,1) 652 is updated. During the next iteration, the second sub-block A (1,1) 642 multiplied by the second sub-block B (1,1) 646, and the sub-block C of the third matrix (C) 606 (1,1) 652 was further updated.
[0044] This particular blocking scheme is not specific to sparsity; instead, the blocking scheme can be implemented in the hardware itself. Additional levels of blocking can be used to achieve sparsity, where each sub-block is divided into smaller sparsity blocks for masking.
[0045] As an example, Figure 7 A scenario 700 for matrix multiplication is shown. A first sparsity mask 702 is shown for a first block 704 of elements, and a second sparsity mask 706 is shown for a second block 708 of elements and a third block 710 of elements. As indicated at 712, each block of elements has a block size (M) of 16. In this example, the blocks of elements are one-dimensional, but in other examples, the blocks of elements may be two-dimensional, three-dimensional, or have greater dimensions, for example, if derived from a multi-dimensional tensor.
[0046] Then, each element block can be divided into a second level of block for the application of sparsity. The amount of hardware overhead for achieving sparsity is proportional to the sparsity block size (B). Thus, the sparsity block size (B) is generally smaller than the block size (M). In general, the block size (M) is an integer multiple of the sparsity block size (B). In this example, the sparsity block size is set to B=4. Thus, the first block 704 of the element is divided into 4 sparsity blocks of size 4-sparse block 720, sparse block 721, sparse block 722, and sparse block 723. Similarly, the second block 708 of elements is divided into 4 sparse blocks of size 4 - sparse block 730, sparse block 731, sparse block 732, and sparse block 733, and the third block 710 of elements is divided into 4 sparse blocks of size 4 - sparse block 740, sparse block 741, sparse block 742, and sparse block 743.
[0047] In this example, 50% sparsity is applied to each sparsity block on an element-by-element basis (eg, fine-grained balanced sparsity). Thus, each sparsity block includes two zero elements (black blocks) that prune the base value and two non-zero elements (white blocks) that maintain the base value.
[0048] Applying 50% sparsity to two blocks of elements in this manner averages 75% sparsity of the matmul product given the random distribution of zero elements within each mask, as shown at 750 for the product of the first block 704 of elements and the third block 710 of elements. However, when two blocks are masked in this manner and then multiplied together, all information is lost whenever there are zero values in either block. Thus, if the two blocks are fully complementary, such as the first block 704 of elements and the second block 708 of elements, each multiplication includes zero elements, and thus the resulting product is 100% sparse, as shown at 752.
[0049] Thus, the actual resulting sparsity may be far greater than the target sparsity, or even lower than the target sparsity. This eliminates a significant amount of information that cannot be recovered, resulting in a loss of accuracy in downstream calculations. In this example, the target sparsity is 75%, but if the pattern of the two blocks is exactly the same, the resulting sparsity will be 50%. The random distribution of values means that the result may be a resulting sparsity anywhere from 50% to 100%, and it is impossible to control this distribution.
[0050] Furthermore, there is no computational or performance advantage to over-sparseness. If the hardware is specifically designed to exploit 50% sparsity, it will not have the logic to dynamically determine if the computation is 100% sparse. Rather than eliminating any matrix multiplications, it will still load a 0 here and a non-0 there, do the actual multiplication and then return zero regardless. Thus, the overall computational cost remains the same even at 100% sparsity.
[0051] In order to generate and maintain a consistent level of combined sparsity within each block of a matmul computation, two different sparsity modes can be applied to the two components of the computation. Figure 7 As shown, one component is pruned using a fine-grained balanced sparsity pattern. Alternatively, a second component may be pruned using a coarse-grained balanced sparsity pattern using a different level of granularity. This allows the desired combined level of sparsity to be achieved while also ensuring that some non-zero data is preserved within each block.
[0052] Figure 8 A method 800 for sparse matrix multiplication is shown. The method 800 can be performed by one or more computing systems (such as, system 100 and / or system 200). Thus, the method 800 can be implemented as part of training a neural network, fine-tuning a neural network, performing an inference operation using a trained neural network, as part of a self-attention layer of a transformer language model, and / or during any computing process in which a block of elements derived from a matrix is pruned prior to performing a matmul operation. By using masks with different sparsity patterns (e.g., different granularities) for the components of the matmul operation, the combined sparsity after the matmul operation can be consistent at the block level. The technical effect of implementing such a method is to reduce the use of computing resources.
[0053] At 810, method 800 includes receiving a first block of elements, the first block of elements having M elements in a first dimension, where M is an integer. For example, a matrix containing one or more blocks of M elements can be loaded from main memory to a suitable cache. For the purposes of this example, the first block of elements will be described as belonging to a weight matrix, but may alternatively be a block of activation matrix elements, gradient matrix elements, bias matrix elements, or other matrix elements. The block of elements can be one-dimensional, two-dimensional, or three-dimensional or more. In this example, the block of elements will be described as one-dimensional, such as rows of elements, columns of elements, and / or partial rows or columns of elements, such as with respect to Figure 6 and Figure 7 as described.
[0054] At 820, method 800 includes parsing a first block of elements into a first B set of sub-blocks, where B is an integer <= M, and where each sub-block in the first set of B sub-blocks includes M / B elements in a first dimension. In most cases, M is an integer multiple of B. In general, once a block size M and a sparsity block size B are selected, the hardware is designed to operate on the selected block size. However, M and B are not necessarily fixed and can be changed during runtime for inference or training, particularly when implementing a virtual machine.
[0055] At 830, a first sparsity mask having S% sparsity over M elements is applied to a first block of elements such that each subblock in the first B sets of subblocks has S% sparsity. Thus, the first sparsity mask can be a fine-grained balanced sparsity mask. To determine which elements to prune for sparsity, a pruning metric can be applied. In one example, the S% with the lowest L1 norm for each set of M / B elements can be pruned. Additionally or alternatively, the absolute magnitude of each corresponding set of elements can be determined, and the lowest S% can be pruned.
[0056] At 840, method 800 includes receiving a second block of elements, the second block of elements having M elements in a second dimension different from the first dimension, where M is an integer, generally the same as the integer M described at 810. For example, the first dimension can be columns and the second dimension can be rows, or vice versa. Continuing with the example, where the first block of elements is derived from a weight matrix, the second block of elements can be an activation matrix. Continuing at 850, method 800 includes parsing the second block of elements into a second set of B sub-blocks, each sub-block in the second set of B sub-blocks including M / B elements in the second dimension. In this example, the sub-blocks are of equal size and equal number, but in other examples, one element block may be subdivided into a different sub-block pattern than other element blocks.
[0057] At 860, method 800 includes applying a second sparsity mask having S'% sparsity over the M elements to a second block of elements such that S'% of the second set of B sub-blocks has 100% sparsity (e.g., pruned) and (100-S')% of the second set of B sub-blocks has 0% sparsity (e.g., fully dense) (e.g., coarse-grained balanced sparsity). In some examples, S' may be equal to S, but in other examples they are different. The metric used to prune the S'% of the second block of elements may be the same metric as the metric used for S, but in other examples the metric may be specifically determined for the matrix type, the expected distribution within the block, etc. S and S' may be determined based on a desired combined sparsity. For example, a desired combined sparsity of 75% may be produced by applying 50% sparsity to both the first block and the second block.
[0058] At 870, method 800 includes matrix multiplying the first block with the second block. By applying fine-grained sparsity to the first block (e.g., weights) and coarse-grained sparsity to the second block (e.g., activations), the first block and the second block will have completely different sparsity patterns. Although each corresponding pair of sub-blocks can have different levels of sparsity, the different patterns generate a combined sparsity in the matmul product that is deterministically consistent throughout the product (e.g., the same for each block or within a threshold similarity) without increasing any computational cost, thereby resulting in increased model accuracy at the same cost.
[0059] In this way, different levels of sparsity granularity can be applied to the two matrices that are multiplied, therefore guaranteeing the desired level of the total sparsity of the matmul product as a result. This allows the sparsity generation of the software level to be adjusted to the hardware configuration to generate an effective matmul operation, while still maintaining the relatively cheap calculation for pruning the elements of a given percentage. In other words, other sparse patterns that reach similar results can be applied, but significant calculations can be taken to generate two masks that are complementary in this way. On the contrary, the method is fast, cheap, globally applicable and adjustable.
[0060] As an example, Fig. 9A scenario 900 for sparse matrix multiplication is shown. A first sparsity mask 902 is shown for a first block 904 of elements derived from a first matrix and a second block 906 of elements. A second sparsity mask 908 is shown for a third block 910 of elements derived from a second matrix and a fourth block 912 of elements. Each element block has a block size (M) of 16, as shown at 915. Each element block is then divided into a second horizontal block (B) for the application of sparsity. In this example, the sparsity block size is set to B=4. Thus, the first block 904 of elements is divided into 4 sparsity blocks of size 4 - sparsity block 920, sparsity block 921, sparsity block 922, and sparsity block 923. Similarly, the second block 906 of elements is divided into 4 sparse blocks of size 4 - sparse block 930, sparse block 931, sparse block 932, and sparse block 933; the third element block 910 is divided into 4 sparse blocks of size 4 - sparse block 940, sparse block 941, sparse block 942, and sparse block 943; the fourth element block 912 is divided into 4 sparse blocks of size 4 - sparse block 950, sparse block 951, sparse block 952, and sparse block 953.
[0061] In this example, a first sparsity mask 902 is used to apply 50% sparsity to each of the sparsity blocks of the first block 904 of elements and the second block 906 of elements on an element-by-element basis (e.g., fine-grained balanced sparsity). Thus, each of the sparsity blocks 920, 921, 922, 923, 930, 931, 932, and 933 includes two zero elements (black blocks) that prune the base value and two non-zero elements (white blocks) that maintain the base value.
[0062] In contrast, the second sparsity mask 908 is used to apply 50% sparsity to the third element block 910 and the fourth element block 912 on a sparsity block-by-block basis (e.g., coarse-grained balanced sparsity). Thus, sparsity block 940, sparsity block 943, sparsity block 952, and sparsity block 953 each include four zero elements, pruning the base value of each sparsity block, while sparsity block 941, sparsity block 942, sparsity block 950, and sparsity block 951 each include four non-zero elements, maintaining the base value of the sparsity block.
[0063] By masking in this manner, when the first block 904 of elements and the second block 906 of elements are matrix multiplied with the third block 910 of elements and the fourth block 912 of elements, the resulting combined sparsity for each pair of element blocks is exactly 75%. For example, when the first block 904 of elements is matrix multiplied by the third block 910 of elements, the matrix multiplication of subblock 940 with subblock 920 and the matrix multiplication of subblock 943 with subblock 923 can be completely eliminated because subblock 940 and subblock 943 are completely zero. In addition, since subblock 921 and subblock 922 are 50% sparse, the matrix multiplication of subblock 941 with subblock 921 and the matrix multiplication of subblock 942 with subblock 922 only involve 50% of the calculations. In total, only 4 of the 16 pairs of elements in block 910 and block 904 must be multiplied to obtain the result value in block 960, providing a combined sparsity of 75%.
[0064] In effect, each sparsity block of the first block of elements is multiplied by either the zero or non-zero value of the corresponding sparsity block from the second block of elements. Thus, the relative sparsity can be averaged over the size of the first block of elements and the size of the second block of elements. In the example of 50% activation sparsity and 50% weight sparsity, each matmul block achieves a combined sparsity of exactly 75%. Overall, when a fine-grained balanced sparsity of x% is applied to one of the two matrices being multiplied together, and a coarse-grained sparsity of y% is applied to the other matrix, the combined sparsity within each matmul block is exactly (x+y-((x*y) / 100)%).
[0065] During training, both the activation matrix and the weight matrix change dynamically, e.g., during each forward phase, there will be new elements in the activation matrix, and each backpropagation updates the weight matrix. The overall sparsity level can be set to a constant, or can be changed step by step with training (e.g., gradually reduced based on model performance).
[0066] However, during inference, the weight matrix is fixed based on training. The activation matrix, which depends on the user input, is newly calculated for each forward stage based on the newly input data. The dimensions and size of the activation matrix can remain essentially the same, but the individual elements are different for each forward stage. Thus, during inference, when calculating the sparsity mask, the mask for the weight matrix can be reused or maintained (e.g., static), but the mask for the activation matrix can be dynamically recalculated for each forward stage (e.g., dynamic).
[0067] This sparsity pattern generally applies to all matrix multiplications. Thus, in a neural network, this approach also applies to cases where both matrices include activations (e.g., a self-attention layer in a Transformer language model). A fine-grained sparsity mask can be applied to one activation matrix, and a coarse-grained sparsity mask can be applied to the other activation matrix. As another example, during back-propagation iterations during training, one matrix can be a gradient matrix, and the second matrix can be an activation matrix or a weight matrix.
[0068] In general, the examples of this paper describe activation as receiving coarse-grained sparsity, and weights as receiving fine-grained sparsity, but in terms of hardware performance, this pattern can be reversed without significant impact. However, in practice, particularly for language modeling tasks, it has been noted that for activation, usually continuous elements have very similar magnitudes. In other words, low magnitude elements are clustered together (e.g., continuous elements in a row), and higher magnitude elements are clustered together elsewhere. On the contrary, weights have a more random distribution. In this way, it may be more advantageous to apply coarse-grained sparsity for activation and fine-grained sparsity for weights in this particular pattern. However, other applications may have opposite patterns. In this way, the conditions of the application can be learned over time, so the sparsity pattern can be determined at the beginning of the process and then maintained from beginning to end.
[0069] It has been shown that the accuracy loss due to sparsity can be reduced by minimizing the one-norm of the pruned values. One method to achieve this for structured sparsity includes calculating a permutation matrix that minimizes the pruned one-norm of each corresponding weight matrix using a greedy reordering technique. The weight matrix can then be permuted using the permutation matrix. Structured sparsity can then be applied to the top of the permuted weight matrix. This process can be applied to both fine-grained balanced sparsity modes and coarse-grained balanced sparsity modes to further improve pruning accuracy. Therefore, the matrix elements can be shuffled so that they are randomly distributed.
[0070] This may not be necessary, or may be solved by other means, when the matrix has a known pattern and distribution. However, there may be situations where the weight matrix is random overall, but has a different pattern in a layer or a portion of a layer. In this case, it may be beneficial to implement some form of element shuffling to make the matrix pattern random and uniform throughout. An inverse function or similar function may be maintained to return the matrix to the previous configuration after the permutation.
[0071] In some embodiments, the methods and processes described herein may be bound to a computing system of one or more computing devices. In particular, such methods and processes may be implemented as a computer application or computer application service, an application program interface (API), a library, and / or other computer program products.
[0072] Fig.10 A non-limiting embodiment of a computing system 1000 that can perform one or more of the methods and processes described above is schematically shown. The computing system 1000 is shown in a simplified form. The computing system 1000 can take the form of one or more personal computers, server computers, tablet computers, home entertainment computers, network computing devices, gaming devices, mobile computing devices, mobile communication devices (e.g., smart phones), and / or other computing devices. Systems 100, 200, and 300 can be examples of computing system 1000.
[0073] The computing system 1000 includes a logic machine 1010 and a storage machine 1020. The computing system 1000 may optionally include a display subsystem 1030, an input subsystem 1040, a communication subsystem 1050, and / or Fig.10 Other components not shown.
[0074] The logic machine 1010 includes one or more physical devices configured to execute instructions. For example, the logic machine can be configured to execute instructions that are part of one or more applications, services, programs, routines, libraries, objects, components, data structures, or other logical constructs. Such instructions can be implemented to perform a task, implement a data type, transform the state of one or more components, achieve a technical effect, or otherwise derive a desired result.
[0075] The logic machine may include one or more processors configured to execute software instructions. Additionally or alternatively, the logic machine may include one or more hardware logic machines or firmware logic machines configured to execute hardware instructions or firmware instructions. The processors of the logic machine may be single-core or multi-core, and the instructions executed thereon may be configured for sequential processing, parallel processing, and / or distributed processing. Individual components of the logic machine may optionally be distributed among two or more separate devices, which may be remotely located and / or configured for collaborative processing. Aspects of the logic machine may be virtualized and executed by a remotely accessible networked computing device configured in a cloud computing configuration.
[0076] In addition to the one or more GPUs 1054, the logic subsystem may include one or more CPUs 1052, and the one or more CPUs 1052 may be configured to send executable instructions and / or data to the one or more GPUs 1054. In response to processing of the instructions and / or data by the one or more GPUs 1054, the CPUs 1052 may receive result data from the one or more GPUs 1054. In this way, the logic subsystem may perform a large number of calculations in parallel via the GPUs. In particular, the logic subsystem may efficiently perform Figure 8 Method 800.
[0077] The present disclosure refers to the GPU as a computing device that is well suited for distributed learning processes because the GPU is configured to execute a very large number of multiple replicated instances of the same program (e.g., GPU kernel) in parallel, where each instance of the program receives different input data and works on that data. However, it should be understood that other aspects of the logic subsystem can be configured to provide the same or similar benefits. As such, it should be understood that any discussion of the GPU also applies to other suitable computing components, and the present disclosure is by no means limited to executing method 800 or any other aspect of training a machine learning model on a GPU to the exclusion of other suitable computing devices.
[0078] Storage machine 1020 includes one or more physical devices configured to hold instructions executable by a logical processor to implement the methods and processes described herein. In implementing such methods and processes, the state of storage machine 1020 may be transformed—for example, to hold different data.
[0079] The storage machine 1020 may include removable and / or built-in devices. The storage machine 1020 may include optical storage (e.g., CD, DVD, HD-DVD, Blu-ray disc, etc.), semiconductor storage (e.g., RAM, EPROM, EEPROM, etc.), and / or magnetic storage (e.g., hard disk drive, floppy disk drive, tape drive, MRAM, etc.), etc. The storage machine 1020 may include volatile devices, non-volatile devices, dynamic devices, static devices, read / write devices, read-only devices, sequential access devices, location addressable devices, file addressable devices, and / or content addressable devices.
[0080] It should be understood that storage machine 1020 includes one or more physical devices. However, aspects of the instructions described herein may alternatively be propagated by a communication medium (eg, electromagnetic signal, optical signal, etc.) that is not held by a physical device for a limited duration.
[0081] Aspects of the logic machine 1010 and the storage machine 1020 may be integrated together into one or more hardware logic components. Such hardware logic components may include, for example, field programmable gate arrays (FPGAs), program specific integrated circuits and application specific integrated circuits (PASIC / ASIC), program specific standard products and application specific standard products (PSSP / ASSP), systems on chips (SOCs), and complex programmable logic devices (CPLDs).
[0082] The terms "module", "program", and "engine" may be used to describe aspects of the computing system 1000 that are implemented to perform specific functions. In some cases, a module, program, or engine may be instantiated via a logic machine 1010 that executes instructions held by a storage machine 1020. It should be understood that different modules, programs, and / or engines may be instantiated from the same application, service, code block, object, library, routine, API, function, etc. Similarly, the same module, program, and / or engine may be instantiated by different applications, services, code blocks, objects, routines, APIs, functions, etc. The terms "module", "program", and "engine" may cover individuals or groups of executable files, data files, libraries, drivers, scripts, database records, and the like.
[0083] It should be understood that a "service" as used herein is an application executable across multiple user sessions. A service may be available for one or more system components, programs, and / or other services. In some implementations, a service may run on one or more server computing devices.
[0084] When display subsystem 1030 is included, display subsystem 1030 can be used to present a visual representation of the data held by storage machine 1020. The visual representation can take the form of a graphical user interface (GUI). As the methods and processes described herein change the data held by the storage machine, and thus transform the state of the storage machine, the state of display subsystem 1030 can be transformed to visually represent the changes in the underlying data. Display subsystem 1030 can include one or more display devices utilizing virtually any type of technology. Such a display device can be combined in a shared enclosure with logic machine 1010 and / or storage machine 1020, or such a display device can be a peripheral display device.
[0085] When input subsystem 1040 is included, input subsystem 1040 may include or interface with one or more user input devices, such as a keyboard, mouse, touch screen, or game controller. In some embodiments, the input subsystem may include selected natural user input (NUI) components or interface with NUI. Such components may be integrated or peripheral, and the conversion and / or processing of input actions may be manipulated on-board or off-board. Example NUI components may include microphones for speech recognition and / or voice recognition; infrared cameras, color cameras, stereo cameras, and / or depth cameras for machine vision and / or gesture recognition; head trackers, eye trackers, accelerometers, and / or gyroscopes for motion detection and / or intent recognition; and electric field sensing components for assessing brain activity.
[0086] When including the communication subsystem 1050, the communication subsystem 1050 can be configured to communicatively couple the computing system 1000 with one or more other computing devices. The communication subsystem 1050 may include wired communication devices and / or wireless communication devices compatible with one or more different communication protocols. As a non-limiting example, the communication subsystem can be configured for communication via a wireless telephone network, or a wired or wireless local area network or wide area network. In some embodiments, the communication subsystem can allow the computing system 500 to send messages to other devices and / or receive messages from other devices via a network (such as the Internet).
[0087] In one example, a method for sparse matrix multiplication includes receiving a first block of elements, the first block of elements having M elements in a first dimension, where M is an integer; parsing the first block of elements into a first set of B sub-blocks, where B is an integer <= M, and where each sub-block in the first set of B sub-blocks includes M / B elements in the first dimension; applying a first sparsity mask having S% sparsity on the M elements to the first block of elements, such that each sub-block in the first set of B sub-blocks has S% sparsity; receiving the elements A second block of elements having M elements in a second dimension different from the first dimension; parsing the second block of elements into a second set of B sub-blocks, each sub-block in the second set of B sub-blocks including M / B elements in the second dimension; applying a second sparsity mask having S'% sparsity on the M elements to the second block of elements such that S'% of the second set of B sub-blocks has 100% sparsity and (100-S')% of the second set of B sub-blocks has 0% sparsity; and matrix multiplying the first block by the second block. In such an example or any other example, S is additionally or alternatively equal to S'. In any of the preceding examples or any other example, one or more of the first sparsity mask and the second sparsity mask are additionally or alternatively generated based on a lowest one-norm set for the corresponding set of M / B elements. In any or any other example of the preceding examples, one or more of the first and second sparsity masks are additionally or alternatively generated based on the absolute modulus for the corresponding M / B element sets. In any or any other example of the preceding examples, the first block of elements is additionally or alternatively derived from the weight matrix, and the second block of elements is additionally or alternatively derived from the activation matrix. In any or any other example of the preceding examples, sparse matrix multiplication occurs additionally or alternatively during the training of the neural network. In any or any other example of the preceding examples, the first and second sparsity masks are additionally or alternatively recalculated dynamically for each iteration of the training of the neural network. In any or any other example of the preceding examples, sparse matrix multiplication occurs additionally or alternatively during the inference operation of the trained neural network. In any or any other example of the preceding examples, the first sparsity mask is additionally or alternatively maintained during each iteration of the inference operation, and the second sparsity mask is additionally or alternatively recalculated dynamically for each forward stage of the inference operation. In any of the previous examples or any other example, the sparse matrix multiplication additionally or alternatively occurs within a self-attention layer of the Transformer language model, and where both the first block of elements and the second block of elements are derived from the activation matrix.The technical effect of implementing the method is an improvement in the use of computing resources.
[0088] In another example, a computing system for implementing a deep neural network includes, one or more logic machines; and one or more storage machines, each storage machine holding instructions that, when executed by the one or more logic machines, cause the computing system to receive a first block of elements, the first block of elements having M elements in a first dimension, where M is an integer; parse the first block of elements into a first set of B sub-blocks, where B is an integer <= M, and where each sub-block in the first set of B sub-blocks includes M / B elements in the first dimension; apply a first sparsity mask having S% sparsity on the M elements to the first set of elements; A method of generating a matrix of elements comprising: generating a matrix of elements comprising a plurality of elements, wherein each of the plurality of elements comprises ... In any of the preceding examples or any other examples, the first block of elements is additionally or alternatively derived from a weight matrix, and the second block of elements is additionally or alternatively derived from an activation matrix, the weight matrix and the activation matrix being used as inputs to a sparse matrix multiplication. In any of the preceding examples or any other examples, the sparse matrix multiplication additionally or alternatively occurs during the training of the neural network. In any of the preceding examples or any other examples, the first sparsity mask and the second sparsity mask are additionally or alternatively dynamically recalculated for each iteration of the training of the neural network. In any of the preceding examples or any other examples, the sparse matrix multiplication additionally or alternatively occurs during an inference operation of the trained neural network. In any of the preceding examples or any other examples, the first sparsity mask is additionally or alternatively maintained during each iteration of the inference operation, and the second sparsity mask is additionally or alternatively dynamically recalculated for each forward stage of the inference operation. In any of the previous examples or any other example, the sparse matrix multiplication additionally or alternatively occurs within the self-attention layer of the Transformer language model, and both the first block of elements and the second block of elements are additionally or alternatively derived from the activation matrix.The technical effect of implementing this computing system is to reduce the computational costs in training and machine learning models.
[0089] In yet another example, a method for training a deep neural network includes receiving a first block of elements derived from a weight matrix, the first block of elements having M elements in a first dimension, where M is an integer; parsing the first block of elements into a first set of B sub-blocks, where B is an integer <= M, and wherein each sub-block in the first set of B sub-blocks includes M / B elements in the first dimension; applying a first sparsity mask having S% sparsity on the M elements to the first block of elements such that each sub-block in the first set of B sub-blocks has S% sparsity; receiving a second block of elements derived from an activation matrix, the elements A second block of elements has M elements in a second dimension different from the first dimension; parsing the second block of elements into a second set of B sub-blocks, each sub-block in the second set of B sub-blocks including M / B elements in the second dimension; applying a second sparsity mask having S'% sparsity over M elements to the second block of elements such that S'% of the second set of B sub-blocks has 100% sparsity and (100-S')% of the second set of B sub-blocks has 0% sparsity; matrix multiplying the first block by the second block; and dynamically recalculating the first sparsity mask and the second sparsity mask for each iteration of training of the neural network. The technical effect of implementing such a method is to reduce the amount of computational resources utilized in training the neural network.
[0090] It should be understood that the configuration and / or scheme described herein are exemplary in nature, and this specific embodiment or example should not be considered as restrictive, because numerous variations are possible. Specific routines or methods described herein can represent one or more processing strategies in any number of processing strategies. Therefore, the various actions illustrated and / or described can be performed in the illustrated and / or described order, in other order, in parallel, or omitted. Similarly, the order of the process described above can be changed.
[0091] The subject matter of the present disclosure includes all novel and nonobvious combinations and subcombinations of the various processes, systems and configurations, and other features, functions, acts, and / or properties disclosed herein, as well as any and all equivalents thereof.
Claims
1. A method for sparse matrix multiplication, comprising: receiving a first block of elements, the first block of elements having M elements in a first dimension, where M is an integer; Parsing the first block of elements into a first set of B sub-blocks, where B is an integer <= M, and where each sub-block in the first set of B sub-blocks includes M / B elements in the first dimension; applying a first sparsity mask having S% sparsity over M elements to the first block of elements such that each subblock in the first set of B subblocks has S% sparsity; receiving a second block of elements, the second block of elements having M elements in a second dimension different from the first dimension; parsing the second block of elements into a second set of B sub-blocks, each sub-block in the second set of B sub-blocks comprising M / B elements in the second dimension; applying a second sparsity mask having S'% sparsity over M elements to the second block of elements such that S'% of the second set of B sub-blocks have 100% sparsity and (100-S')% of the second set of B sub-blocks have 0% sparsity; as well as Matrix multiplication is performed on the first block and the second block. The method according to claim 1 , wherein S=S′. 3 . The method of claim 1 , wherein one or more of the first sparsity mask and the second sparsity mask are generated based on a lowest one-norm set for a corresponding set of M / B elements. 4 . The method of claim 1 , wherein one or more of the first sparsity mask and the second sparsity mask are generated based on absolute modulus lengths for corresponding sets of M / B elements.
5. The method of claim 1, wherein the first block of elements is derived from a weight matrix, and wherein the second block of elements is derived from an activation matrix.
6. The method of claim 5, wherein the sparse matrix multiplication occurs during training of a neural network.
7. The method of claim 6, wherein the first sparsity mask and the second sparsity mask are dynamically recomputed for each iteration of training of the neural network.
8. The method of claim 5, wherein the sparse matrix multiplication occurs during inference operations of a trained neural network.
9. The method of claim 8, wherein the first sparsity mask is maintained during each iteration of the inference operation, and wherein the second sparsity mask is dynamically recomputed for each forward stage of the inference operation.
10. The method of claim 1, wherein the sparse matrix multiplication occurs within a self-attention layer of a Transformer language model, and wherein both the first block of elements and the second block of elements are derived from an activation matrix.
11. A computing system for implementing a deep neural network, comprising: one or more logical machines; as well as one or more storage machines, each storage machine holding instructions that, when executed by the one or more logic machines, cause the computing system to: receiving a first block of elements, the first block of elements having M elements in a first dimension, where M is an integer; Parsing the first block of elements into a first set of B sub-blocks, where B is an integer <= M, and where each sub-block in the first set of B sub-blocks includes M / B elements in the first dimension; applying a first sparsity mask having S% sparsity over M elements to the first block of elements such that each subblock in the first set of B subblocks has S% sparsity; receiving a second block of elements, the second block of elements having M elements in a second dimension different from the first dimension; parsing the second block of elements into a second set of B sub-blocks, each sub-block in the second set of B sub-blocks comprising M / B elements in the second dimension; applying a second sparsity mask having S'% sparsity over M elements to the second block of elements such that S'% of the second set of B sub-blocks have 100% sparsity and (100-S')% of the second set of B sub-blocks have 0% sparsity; as well as Matrix multiplication is performed on the first block and the second block.
12. The computing system of claim 11, wherein S=S'. 13 . The computing system of claim 11 , wherein one or more of the first sparsity mask and the second sparsity mask are generated based on a lowest one-norm set for a corresponding set of M / B elements.
14. The computing system of claim 11, wherein the first block of elements is derived from a weight matrix, and wherein the second block of elements is derived from an activation matrix, the weight matrix and the activation matrix being used as inputs to a sparse matrix multiplication.
15. The computing system of claim 14, wherein the sparse matrix multiplication occurs during training of a neural network.
Citation Information
Cited By
Matrix operation system supporting mask processing, operation method, electronic equipment and readable storage medium
CN120705456A
A matrix operation system supporting mask processing, a running method, an electronic device and a readable storage medium
CN120705456B
