Training deep neural networks with 4-bit integers
Patent Information
- Application Number
- PCT/CN2023/089753
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Filing Date
- 2023-04-21
- Publication Date
- 2024-10-24
Smart Images

Figure CN2023089753_24102024_PF_FP_ABST
Abstract
Description
TRAINING DEEP NEURAL NETWORKS WITH 4-BIT INTEGERSFIELD
[0001] Aspects of the present disclosure relate generally to artificial intelligence (AI) , and more particularly, to training deep neural networks with 4-bit integers.BACKGROUND
[0002] Artificial neural networks (ANNs) , usually simply called neural networks (NNs) , as computing systems that were inspired by information processing and distributed communication nodes in biological neural network systems, have been studied for several decades. A deep neural network (DNN) , which is an artificial neural network with multiple layers between the input and output layers, has been experiencing a tremendous development in recent years with the great advances in both machine learning algorithms and models and computer hardware performance. Current state-of-the-art models of deep neural networks, such as Transformers introduced in 2017 by Ashish Vaswani et al., have been extensively applied to a wide variety of fields including natural language processing, machine translation, speech recognition, computer vision, and etc., where they have produced results demonstrating remarkable performance.
[0003] There is always a need for accelerating operations of deep neural networks, especially considering the operating environments of mobile computing, in-vehicle computing, edge computing, and etc., which each have a relatively low computing capacity. The fundamental idea of quantization, as a type of method for accelerating neural networks, is to replace expensive high-precision representation and computation (e.g., 32-bit floating point (FP32) , 16-bit floating point (FP16) ) with cheaper low-precision (e.g., 8-bit integer (INT8) ) for use within the neural works. Quantization can effectively improve computing efficiency, reduce both memory footprint and storage space, and accordingly, lower the power consumption.
[0004] Training neural networks are computationally demanding. Quantizing the activations, weights, and gradients to ultra-low precision is promising to accelerate neural network training. Current research in this area aims to reduce the numerical precision, without sacrificing much model accuracy. Although a series of works have been proposed, there still exist significant optimization challenges to train neural networks at an extremely low 4-bit level.
[0005] SUMMARY
[0006] This summary is provided to introduce a selection of concepts in a simplified form that are further described below in the detailed description. This summary is not intended to identify any key features or essential features of the claimed subject matter, nor is it intended to be used as an aid in determining the scope of the claimed subject matter.
[0007] According to an embodiment of the present disclosure, a computer-implemented method for accelerating operations of a deep neural network is provided. The method comprises: for each of a plurality of matrix multiplications during forward propagation, obtaining a first matrix by multiplying an activation matrix, which an input of the matrix multiplication, with a block-diagonal Hadamard transform matrix; obtaining a second matrix by multiplying a weight matrix, a transpose of which is another input of the matrix multiplication, with the block-diagonal Hadamard transform matrix; quantizing the first matrix and the second matrix into 4-bit integer matrices; obtaining a resultant matrix by multiplying the quantized first matrix and the transpose of the quantized second matrix; and dequantizing the resultant matrix to a matrix of floating-point representation.
[0008] According to another embodiment of the present disclosure, a computing device is provided. The computing device comprises memory for storing instructions and one or more processing units coupled to the memory, wherein the instructions, when executed on the one or more processing units, cause the one or more processing units to: for each of a plurality of matrix multiplications during forward propagation, obtain a first matrix by multiplying an activation matrix, which an input of the matrix multiplication, with a block-diagonal Hadamard transform matrix; obtain a second matrix by multiplying a weight matrix, a transpose of which is another input of the matrix multiplication, with the block-diagonal Hadamard transform matrix; quantize the first matrix and the second matrix into 4-bit integer matrices; obtain a resultant matrix by multiplying the quantized first matrix and the transpose of the quantized second matrix; and dequantize the resultant matrix to a matrix of floating-point representation.
[0009] According to a further embodiment of the present disclosure, a non-transitory computer-readable storage medium is provided, which has stored thereon instructions that, when executed by one or more processing units, cause the one or more processing units to: for each of a plurality of matrix multiplications during forward propagation, obtain a first matrix by multiplying an activation matrix, which an input of the matrix multiplication, with a block-diagonal Hadamard transform matrix; obtain a second matrix by multiplying a weight matrix, a transpose of which is another input of the matrix multiplication, with the block-diagonal Hadamard transform matrix; quantize the first matrix and the second matrix into 4-bit integer matrices; obtain a resultant matrix by multiplying the quantized first matrix and the transpose of the quantized second matrix; and dequantize the resultant matrix to a matrix of floating-point representation.
[0010] According to a still further embodiment of the present disclosure, a computer program product is provided, which comprises instructions that, when executed by one or more processing units, cause the one or more processing units to: for each of a plurality of matrix multiplications during forward propagation, obtain a first matrix by multiplying an activation matrix, which an input of the matrix multiplication, with a block-diagonal Hadamard transform matrix; obtain a second matrix by multiplying a weight matrix, a transpose of which is another input of the matrix multiplication, with the block-diagonal Hadamard transform matrix; quantize the first matrix and the second matrix into 4-bit integer matrices; obtain a resultant matrix by multiplying the quantized first matrix and the transpose of the quantized second matrix; and dequantize the resultant matrix to a matrix of floating-point representation.BRIEF DESCRIPTION OF THE DRAWINGS
[0011] Embodiments of the present disclosure are illustrated by way of example, and not by way of limitation, in the figures of the accompanying drawings in which like references numerals refers to identical or similar elements and in which:
[0012] Fig. 1 illustrates a typical architecture of an exemplary neural network;
[0013] Fig. 2A is a histogram illustrating original activation distribution of an exemplary transformer model;
[0014] Fig. 2B is a histogram illustrating activation distribution of an exemplary transformer model, after Hadamard transformation in accordance with some embodiments of the present disclosure;
[0015] Fig. 3 is a chart illustrating performance variations associated with different group sizes, in accordance with some embodiments of the present disclosure.
[0016] Fig. 4 is a flow chart of an exemplary computer-implemented method in accordance with some embodiments of the present disclosure;
[0017] Fig. 5 is a flow chart of an exemplary matrix multiplication procedure in accordance with some embodiments of the present disclosure;
[0018] Fig. 6 is a flow chart of an exemplary matrix multiplication procedure in accordance with some embodiments of the present disclosure; and
[0019] Fig. 7 is a block diagram of an exemplary computing device in accordance with some embodiments of the disclosure.DETAILED DESCRIPTION
[0020] In the following description, numerous specific details are set forth for the purposes of explanation. It should be understood that, however, embodiments of the present disclosure may be practiced without these specific details. In other instances, well-known circuits, structures and techniques have not been shown in detail in order not to obscure the understanding of the disclosure.
[0021] References to “one embodiment” , “an embodiment” , “exemplary embodiment” , “some embodiments” , “various embodiments” or the like throughout the description indicate that the embodiment (s) of the present disclosure so described may include particular features, structures or characteristics, but it is not necessarily for every embodiment to include the particular features, structures or characteristics. Further, some embodiments may have some, all or none of the features described for other embodiments.
[0022] In the following description and claims, the terms “coupled” and “connected” , along with their derivatives, may be used. It should be understood that these terms are not intended as synonyms for each other. Rather, in particular embodiments, “connected” is used to indicate that two or more elements are in direct physical or electrical contact with each other, while “coupled” is used to indicate that two or more elements co-operate or interact with each other, but they may or may not be in direct physical or electrical contact.
[0023] FIG. 1 illustrates a typical architecture of an exemplary neural network, which has an input layer, and output layer, and two hidden layers. In this exemplary neural network, there are four neurons (or “nodes” ) in the input layer, three neurons in the first hidden layer, two neuron for in second hidden layer, and one neuron in the output layer. As shown herein, a neuron or node in one of the layers in the exemplary neural network receives, from those neurons in the previous layer, input signals that are passed via connections with different weights. An activation function (denoted as “f”) associated with the neuron is then applied to the weight sum (denoted as “Σ” ) of input signals received by the neuron, to generate the output of the neuron, called “activation” , which will act as an input to neuron (s) in the next layer. It can be understood that for the neurons of the input layers, the corresponding activations are equal to the respective input values.
[0024] Deep neural network (DNN) is a neural network with multiple layers (that means, very deep) between the input and output layers. Transformer, as a state-of-the art model of deep neural network, is increasingly becoming the model of choice for various tasks such as natural language processing (NLP) , and etc. For NLP applications, the deep neural network takes vector representations for words generated based on word-embedding, in some typical scenarios. A typical transformer adopts an encoder-decoder architecture, with the use of an attention mechanism, wherein the encoder consists of encoding layers to generate encoding information about which parts of the inputs are relevant to each other, which is then passed to the next encoding layer as input, and wherein the decoder consists of decoding layers that take all the encoding information and use their incorporated context information to generate an output sequence. Although in the following discussion, transformers are referred to as exemplary models of deep neural networks, it should be noted that the inventive concept proposed by the present disclosure could also be applied to other types of deep neural networks.
[0025] The training process of a deep neural network, a typical example of which is transformer, generally consists of two stages: forward propagation and backpropagation.
[0026] Forward Propagation
[0027] The forward propagation of a neural network can be formulated as a composition of linear operations (matrix multiplications) and non-linear operations (GeLU, normalization, softmax, …) . According to some embodiments of the present disclosure, all the linear operations, i.e., matrix multiplications, are to be accelerated with 4-bit integer (INT4) arithmetic. For those less-computationally-intensive non-linear operations, they can be left in the original floating-point (FP) format, such as 16-bit floating-point format (FP16) . It is to be noted that in the following discussion the FP16 is usually referred to as an example of floating-point representation, and other floating-point formats with higher or lower precision, such as FP32, could also be adopted in implementations of the present disclosure.
[0028] All linear operations in a deep neural network, a typical and most-popular instantiation of which is transformer, can be written in the form of matrix multiplication (MM) , which is to be accelerated according to some embodiments of the present disclosure. For ease of presentation, the matrix multiplication can be expressed as below: Y=XWT, (1)
[0029] where and The most predominant use case of such matrix multiplication is the fully-connected layer. Consider a transformer with an input shape of (batch size S, sequence length T, dimensionality D) . A fully-connected layer can be written as Equation (1) , where X is the activation matrix for N=ST tokens, and W is the weight matrix.
[0030] Given a FP matrix X, a typical quantization method called learned step size quantization (LSQ) can quantize X to integer with:
[0031] where sX is a learnable floating-point scalar parameter, called step size; the clamp function restricts its input into the range [-QN, QP] ; operator represents a rounding operation to the nearest integer, and X / sX is to be computed elementwise. The resultant matrix takes values from {-QN, -QN+1, …, QP} . According to some embodiments of the present disclosure, since the computation is to be performed with 4-bit integer (INT4) , it may be preferable that QN=QP=7, especially considering symmetric quantization.
[0032] The integer matrix can be dequantized back to floating-point representation:
[0033] Similarly, the FP weight matrix W can be quantized as:
[0034] where sW is a learnable floating-point scalar parameter for the weight matrix.
[0035] Therefore, the above Equation (1) can be computed approximately as:
[0036] Here, the INT4 matrix multiplication can be implemented efficiently on hardware such as tensor-core CPUs.
[0037] It has been found that, however, directly utilizing the above Equations such as Equation (2) for accelerating training with integer arithmetic may lead to major accuracy degradation. More particularly, the above-mentioned quantization method suffers from the outlier problem. As shown in Fig. 2A which is a histogram illustrating original activation distribution of an exemplary transformer model, activations have some outlier entries, which are much larger in magnitude than other entries. In this case, the step size sX poses a trade-off between quantization granularity and representable numerical range. If sX is large, the outliers can ben represented well at the expense of representing most other entries in a very coarse manner. On the other hand, if sX is small, the entries outside the range [-QNsX, QPsX] have to be truncated. Transformers tend to store information in these outliers, and such truncation would seriously harm accuracy. The outlier problem is particularly significant when the training task is to fine-tune a pre-trained model on some new downstream tasks.
[0038] The outliers in an activation matrix X form a feature-wise structure. They are typically concentrated on a few dimensions, i.e., only a few columns of X are significantly larger than others. According to some embodiments of the present disclosure, Hadamard transform, which is a linear transformation, is introduced herein to solve the outlier problem.
[0039] Specifically, the Hadamard transform Hk is a 2k×2k matrix, where
[0040] Hadamard matrices are orthogonal and symmetric: so According to some embodiments of the present disclosure, a block-diagonal matrix is used, where D is a multiple of 2k: H=BlockDiag (Hk, …, Hk) , (7)
[0041] To suppress outliers, a transformed version of the activation matrix X can be quantized as:
[0042] The practical effect of the Hadamard transformation on suppressing outliers, in a quantization-friendly manner, is demonstrated in Fig. 2B. The Hadamard transformation spreads the information carried in outliers to its nearby entries of the matrix and thus reduce the numerical range of the outliers. The quantization with Hadamard transformation, also called Hadamard quantization (HQ) herein, successfully quantizes the matrix in another linear space which has fewer outliers.
[0043] For Hadamard quantization, there is a group size hyperparameter M=2k, as defined in Equation (7) . The group size controls a trade-off between accuracy and time complexity. With a larger group size, outlier can be amortized to more entries, at the cost of higher time complexity. Fig. 3 is a chart illustrating performance variations associated with different group sizes, in accordance with some embodiments of the present disclosure. As shown in Fig. 3, a larger group size steadily improves the accuracy, proving the effectiveness of Hadamard quantization, and the improvement begins to saturate at a group size M=25=32. Therefore, according to some embodiments of the present disclosure, the value of k is chosen as k=5.
[0044] Similarly, the weight matrix W can be quantized with another block diagonal Hadamard transform matrix
[0045] Combining the quantized matrices, the following can be obtained:
[0046] Therefore, according to some embodiments of the present disclosure, a computer-implemented method for accelerating operations of a deep neural network is disclosed, which can be used during a training process of the deep neural network. Fig. 4 is a flow chart of an exemplary method 400 in accordance with some embodiments of the present disclosure.
[0047] For each of a plurality of matrix multiplications during forward propagation, the exemplary method 400 begins with Step 410, obtaining a first matrix (XH) by multiplying an activation matrix (X) , which an input of the matrix multiplication, with a block-diagonal Hadamard transform matrix (H) . It can be understood that the obtained first matrix in this step is still in a floating-point format, such as FP16.
[0048] Similarly, the method 400 executes Step 420, obtaining a second matrix (WH) by multiplying a weight matrix (W) , a transpose of which is another input of the matrix multiplication, with the block-diagonal Hadamard transform matrix (H) . Following the above example, the obtained second matrix is also in FP16.
[0049] Then, the method 400 proceeds to Step 430, quantizing the first matrix and the second matrix into 4-bit integer matrices. The quantizing can be implemented using the above-discussed quantization method, learned step size quantization. In some embodiments of the present disclosure, the quantizing may include applying a clamp function to the first matrix and the second matrix, respectively, to restrict each of elements in the first matrix and the second matrix into a range representable by 4-bit integers. Here, a first step size for the activation matrix and a second step size for the weight matrix, each being a parameter of the clamp function, are floating-point scalars learnable during a training process of the deep neural network. In this case, with reference to the above equations, the quantized first matrix may correspond to in Equation (8) , while the quantized second matrix may correspond to in Equation (9) .
[0050] The method 400 continues to execute Step 440, obtaining a resultant matrix by multiplying the quantized first matrix and the transpose of the quantized second matrix. It can bee seen that the matrix multiplication in this step is performed on two 4-bit integer (INT4) matrices, in lieu the original computationally-expensive FP16 matrix multiplication.
[0051] Then, in Step 450, dequantizing the resultant matrix to a matrix of floating-point representation. In some embodiments of the present disclosure, with reference to Equation (10) , the dequantizing may include multiplying the resultant matrix, which is an integer matrix as described above, with the first step size and the second step size that each are floating-point scalars.
[0052] It should be noted that the sequence from Step 410 to step 450 as discussed above does not mean, in any way, that the exemplary method 400 can only be performed in this particular sequential order. Instead, it could be readily appreciated that some of the operations may be performed simultaneously, in parallel, or in a different order. As an example, Steps 410 and 420 may be performed simultaneously. Likewise, other method steps and procedures disclosed in the present disclosure are not necessarily to be performed in the described sequential order.
[0053] For time complexity, Step 410 and Step 420 takes O (2kN (D+C) ) FP16 multiply-accumulates (MACs) ; Step 430 and Step 450 takes O (N (D+C) ) FP 16 MACs in total; and Step 440 takes O (NDC) INT MACs. Comparing with directly using the above-discussed learned step size quantization (LSQ) by referring to Equations (2) - (5) , the amount of FP16 MACs increases by 2k times, from O (N (D+C) ) to O (2kN (D+C) ) . However, the Hadamard quantization proposed herein is still much cheaper than an FP16 matrix multiplication given 2k<<D, 2k<<C.
[0054] It is to be noted that although the above discussions are made under the context of the training process of the deep neural network, the technique discussed herein could also be applied to accelerate the inference process that uses a trained neural network to make predictions on new data, which is also a type of forward propagation.
[0055] Backpropagation
[0056] The backpropagation stage of the training process for the deep neural network refers to a procedure that feeds the loss (or cost) , calculated by using a loss function (e.g., cross entropy) , back through the neural network layers to adjust parameters (e.g., weights) within the neural network, by using some form of gradient descent. Here, the loss function is to measure the difference between the predicted output of the deep neural network and the expected output (or the ground truth) , and the backpropagation aims to minimize the loss function.
[0057] For accelerating the backpropagation of the training process, now consider the linear operation of matrix multiplication defined in Equation (10) , which has four inputs: activation X, weight W, and step sizes sX, sW. Given the gradient (for simplicity, hereinafter denoted as ) of the loss function with regard to the output Y of the matrix multiplication, the gradients for all of the four inputs are to be computed.
[0058] First, by the straight-through estimator and the chain rule, the weight gradient and the activation gradient can be calculated as below:
[0059] where and where and are 0 / 1 matrices, and operator represents element-size multiplication.
[0060] According to some embodiments of the present disclosure, among the others, the matrix multiplication of the FP16 gradient matrix with an INT4 matrix or i.e., two matrix multiplications and in the above Equation, will be accelerated by quantizing to INT4.
[0061] It has been found that, the gradient matrix tends to be very sparse along the training process, and the sparsity has a structure: few rows (i.e., tokens) of have large entries, while most of the other rows are close to an all-zero vector. Such a structural sparsity arises from the heavy overparameterization of modern neural networks, and it is found that for pretraining tasks, such a structural sparsity quickly emerges after only a few training epochs, while for fine-tuning tasks, the gradient is always sparse during the whole training process.
[0062] As described above, may rows of the gradient are so small that they have little impact on the parameter gradient, yet they waste abundant computation. On the other hand, the large rows may not be accurately represented with 4-bit integers (INT4) .
[0063] In view of the above problems, for computations associated with the FP16 gradient matrix this matrix is firstly to be split into higher 4 bits and lower 4 bits:
[0064] where s↑, s↓ are two floating-point scalars, and are INT4 matrices representing the higher and lower 4 bits of respectively.
[0065] According to some embodiments of the present disclosure, this bit splitting (BS) can be implemented by first quantizing the FP16 gradient matrix into the first (higher) INT4 gradient matrix as and then, quantizing the residual, i.e., the difference between and into the second (lower) INT4 gradient matrix as
[0066] The bit splitting can be viewed as an INT8 representation of the original gradient matrix where and are the higher and lower 4 bits of the INT8 representation, respectively.
[0067] Next, discussions will be made regarding the computation of with the weight gradient and the activation gradient.
[0068] Weight Gradient
[0069] As discussed earlier, weight gradient involves the matrix multiplication where and is an N×D INT4 matrix. This matrix multiplication can be approximated as:
[0070] where and which is to be an 2N×D INT4 matrix.
[0071] Equation (13) represents that the matrix multiplication of an FP16 matrix, the transpose of the gradient matrix and the INT4 matrix can be approximated by two INT4 matrix multiplications and More specifically, according to some embodiments of the present disclosure, the matrix multiplication discussed herein can be implemented by the following steps: obtaining a matrix by multiplying the transpose of the first (higher) INT4 gradient matrix and the INT4 matrix obtaining a matrix by multiplying the transpose of the second (lower) INT4 gradient matrix and the INT4 matrix dequantizing the obtained two matrices into matrices of floating-point representation (e.g., FP16) ; and then, obtaining a resultant matrix by summing up the dequantized two matrices.
[0072] Such matrix multiplication in Equation (13) is accurate since the gradient matrix is represented with 8 bits, effectively avoiding the situations where, if an element in the gradient matrix is quantized into one 4-bit integer only, the imprecise gradients may adversely slow down the training process and even cause the training to be unstable or diverge.
[0073] Further, comparing to a quantization of to INT4, the bit splitting described above may double the amount of INT4 operations for matrix multiplication. According to some embodiments of the present disclosure, leverage score sampling (LSS) is proposed herein to cut the operations of Equation (13) by half, to the same amount as the matrix multiplication That means, by leveraging structural sparsity, some small rows of the gradient matrix may be dropped and the saved computation could therefore be used to represent large rows more accurately.
[0074] The matrix multiplication of Equation (13) can be written as the sum of 2N matrix matrices:
[0075] where Due to the sparsity of the matrices differ in magnitude and small matrices can be discarded without having a big influence on the result.
[0076] According to some embodiments of the present disclosure, in obtaining the matrix and the matrix as discussed above, rows in the first (higher) INT4 gradient matrix and the second (lower) INT4 gradient matrix may be sampled, with per-row random masks (mi) that follow Bernoulli distribution with corresponding per-row probabilities (pi) . More specifically, each is assigned a probability pi∈ [0, 1] , i=1, …, 2N, that satisfies Random masks mi are defined to follow the Bernoulli distribution with the parameter pi, denoted as mi ~ Bern (pi) , and a diagonal matrix is defined as So, can be unbiasedly approximated as below:
[0077] In expectation, there are only N nonzero mis. Therefore, with the leverage score sampling, the cost of matrix multiplication is reduced by half. A per-row coefficient, called herein the leverage score for weight gradient, is which can be easily computed in low time complexity. When pi∝ci, the variance for achieves its minimum.
[0078] Here, M↑ is defined to be the top-left N×N submatrix of M and M↓ is defined to be the bottom-right N×N submatrix of M. Therefore,
[0079] which can be implemented by two INT4 matrix multiplications with sampled rows / columns.
[0080] Therefore, with the above equations, a matrix multiplication procedure 500 for computing the weight gradient may be as follows, as shown in Fig. 5:
[0081] Step 510: Quantizing with bit splitting to obtain and in INT4;
[0082] Step 520: Computing the leverage score in FP16;
[0083] Step 530: Sampling the masks mi that follow the Bernoulli distribution with the parameter pi, where pi∝ci;
[0084] Step 540: Computing INT4 matrix multiplications and
[0085] Step 550: Dequantizing the resultant matrices into FP16 matrices; and
[0086] Step 560: Summing up the FP16 matrices to obtain the FP16 result
[0087] The two INT4 matrix multiplications in Step 540 take about 2NCD INT4 MACs in expectation. The overhead of all the other steps is O (NC+ND) in total.
[0088] Activation Gradient
[0089] Similar to the discussion of the weight gradient, the matrix multiplication in the calculation of the activation gradient can be written as:
[0090] where as described above, and which is to be an N×2N INT4 matrix, I being a N×N identity matrix.
[0091] Equation (17) represent that the matrix multiplication of the FP16 gradient matrix and the INT4 matrix can also be approximated by two INT4 matrix multiplications and More specifically, according to some embodiments of the present disclosure, the matrix multiplication discussed herein can be implemented by the following steps: obtaining a matrix by multiplying the first (higher) INT4 integer gradient matrix and the INT4 matrix obtaining a matrix by multiplying the second (lower) INT4 gradient matrix and the INT4 matrix dequantizing the obtained two matrices into matrices of floating-point representation (e.g., FP16) ; and then, obtaining a resultant matrix by summing up the dequantized two matrices.
[0092] But different from the weight gradient, now the focus is on in Eq. (17) and leverage score sampling is done on this matrix multiplication. This matrix multiplication can similarly be written as the sum of 2N smaller multiplications:
[0093] where
[0094] According to some embodiments of the present disclosure, in obtaining the matrix and the matrix as discussed above, rows in the first (higher) INT4 gradient matrix and the second (lower) INT4 gradient matrix may be sampled, with per-row random masks mi that follow Bernoulli distribution with corresponding per-row probabilities pi. More specifically, each is assigned a probability pi∈ [0, 1] , i=1, …, 2N, that satisfies Random masks mi are defined to follow the Bernoulli distribution with the parameter pi, denoted as mi ~ Bern (pi) , and a diagonal matrix is defined as So, can be unbiasedly approximated as below:
[0095] Here, the leverage score for activation gradient would be and the variance for achieves its minimum when pi∝ci.
[0096] Again, M↑ is defined to be the top-left N×N submatrix of M and M↓ is defined to be the bottom-right N×N submatrix of M, and the matrix multiplication Equation (17) can be decomposed as two INT4 matrix multiplications:
[0097] In this case, and both only have parts of its rows being non-zero, and the rest rows are zeros since they are discarded. Then, when multiplying it by there are half of rows being zeros in and so there is no need to calculate them, and half of the computation is cut off in this case.
[0098] Therefore, with the above equations, a matrix multiplication procedure 600 for computing the activation gradient may be as follows, as shown in Fig. 6:
[0099] Step 610: Quantizing with bit splitting to obtain and in INT4;
[0100] Step 620: Computing the leverage score in FP16;
[0101] Step 630: Sampling the masks mi that follow the Bernoulli distribution with the parameter pi, where pi∝ci;
[0102] Step 640: Computing and by discarding some of its rows;
[0103] Step 650: Computing INT4 matrix multiplications and
[0104] Step 660: Dequantizing the resultant matrices into FP16 matrices; and
[0105] Step 670: Summing up the FP16 matrices to obtain the FP16 result
[0106] The two matrix multiplications in Step 640 take about 2NCD INT4 MACs in expectation.
[0107] Learning Quantizer Parameters
[0108] The following is to discuss how to learn or optimize the quantization step sizes sX, sW. Following the chain rule, the gradients for the step sizes sX, sW can be written as:
[0109] where NW and NX the number of elements of weight and activation, and
[0110] For computing and the most expensive matrix multiplications are and which however have already been calculated during previous calculations, so it does not require extra computation. The elementwise multiplication with δW (sW) and δX (sX) require minor computation.
[0111] Further, according to some embodiments of the present disclosure, whether or not to start optimizing each of the first step size and the second step size is to be determined, when the rate of change of the corresponding step size over one or more iterations in the training process is less than a pre-determined threshold, or when a pre-determined number of epochs has been trained in the training process. Thereby the cold start problem, which happens when the neural network is trained from scratch, can be effectively avoided.
[0112] Combining quantization techniques for forward and backward propagation, the mechanism proposed in the present disclosure uses INT4 matrix multiplications for all linear operations in deep neural networks, such as transformers. The mechanism disclosed herein is evaluated for training transformers on a wide variety of tasks, including natural language understanding, question answering, machine translation, image classification, and etc., achieving competitive or superior accuracy compared with existing works. Training deep neural networks at an extremely low 4-bit level, with the mechanism disclosed herein, can effectively reduce the demand for computer hardware resources including, but not limited to, arithmetic units, registers, memory and storage, compared to those solutions with expensive high-precision representation and computation, and accordingly, it significantly improves computing efficiency and lowers the power consumption. More, the mechanism disclosed herein is compatible with contemporary hardware like GPUs, since it does not require custom numerical formats that are needed by existing works. Still further, a prototypical implementation of the mechanism disclosed herein demonstrates superior speed that is 1.7-2.9 times faster than the FP16 matrix multiplication baseline.
[0113] Turning now to Fig. 7, a block diagram of an exemplary computing device 700 in accordance with some embodiments of the present disclosure is illustrated. As illustrated herein, the computing device 700 may comprise one or more processing units 710 and memory 720. The one or more processing units 710 may include any type of general-purpose processing units / cores (for example, but not limited to CPU, GPU) , or application-specific processing units, cores, circuits, controllers or the like. The memory 720, coupled to the one or more processing units 710, may include any type of medium that may be used to store data. The memory 720 is configured to store instructions that, when executed, cause the one or more processing units 710 to perform operations of any method described herein, including but not limited to the exemplary method 400, the exemplary procedure 500, the exemplary procedure 600.
[0114] Various embodiments described herein may include, or may operate on, a number of components, elements, units, modules, instances, or mechanisms, which may be implemented using hardware, software, firmware, or any combination thereof. Examples of hardware may include, but not be limited to, devices, processors, microprocessors, circuits, circuit elements (e.g., transistors, resistors, capacitors, inductors, and so forth) , integrated circuits, application specific integrated circuits (ASIC) , programmable logic devices (PLD) , digital signal processors (DSP) , field programmable gate array (FPGA) , memory units, logic gates, registers, semiconductor devices, chips, microchips, chip sets, and so forth. Examples of software may include, but not be limited to, software components, programs, applications, computer programs, application programs, system programs, machine programs, operating system software, middleware, software modules, routines, subroutines, functions, methods, procedures, software interfaces, application programming interfaces (API) , instruction sets, computer code, computer code segments, words, values, symbols, or any combination thereof. Determining whether an embodiment is implemented using hardware, software and / or firmware may vary in accordance with any number of factors, such as desired computational rate, power levels, heat tolerances, processing cycle budget, input data rates, output data rates, memory resources, data bus speeds and other design or performance constraints, as desired for a given embodiment.
[0115] Some embodiments described herein may comprise an article of manufacture. An article of manufacture may comprise a storage medium. Examples of storage medium may include volatile and nonvolatile, removable and non-removable media implemented in any method or technology for storage of information, such as computer readable instructions, data structures, program modules, or other data. Storage medium may include, but not be limited to, random-access memory (RAM) , read-only memory (ROM) , programmable read-only memory (PROM) , erasable programmable read-only memory (EPROM) , electrically erasable programmable read-only memory (EEPROM) , flash memory or other memory technology, compact disc (CD) , digital versatile disk (DVD) or other optical storage, magnetic cassette, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other medium which can be used to store information. In some embodiments, an article of manufacture may store executable computer program instructions that, when executed by one or more processing units, cause the processing units to perform operations described herein. The executable computer program instructions may include any suitable type of code, such as source code, compiled code, interpreted code, executable code, static code, dynamic code, and the like. The executable computer program instructions may be implemented using any suitable high-level, low-level, object-oriented, visual, compiled and / or interpreted programming language.
[0116] Some examples of the present disclosure described herein are given below.
[0117] Example 1 may include a computer-implemented method for accelerating operations of a deep neural network, comprising: for each of a plurality of matrix multiplications during forward propagation, obtaining a first matrix by multiplying an activation matrix, which an input of the matrix multiplication, with a block-diagonal Hadamard transform matrix; obtaining a second matrix by multiplying a weight matrix, a transpose of which is another input of the matrix multiplication, with the block-diagonal Hadamard transform matrix; quantizing the first matrix and the second matrix into 4-bit integer matrices; obtaining a resultant matrix by multiplying the quantized first matrix and the transpose of the quantized second matrix; and dequantizing the resultant matrix to a matrix of floating-point representation.
[0118] Example 2 may include the subject matter of Example 1, wherein the method is to be performed during a training process of the deep neural network.
[0119] Example 3 may include the subject matter of Example 1, wherein the block-diagonal Hadamard transform matrix is a block-diagonal matrix consisting of multiple Hadamard transform matrices Hk, wherein k=5.
[0120] Example 4 may include the subject matter of Example 1, wherein the quantizing includes: applying a clamp function to the first matrix and the second matrix, respectively, to restrict each of elements in the first matrix and the second matrix into a range representable by 4-bit integers, wherein a first step size for the activation matrix and a second step size for the weight matrix, each being a parameter of the clamp function, are floating-point scalars learnable during a training process of the deep neural network.
[0121] Example 5 may include the subject matter of Example 4, wherein the dequantizing includes: multiplying the resultant matrix with the first step size and the second step size.
[0122] Example 6 may include the subject matter of Example 4, wherein whether or not to start optimizing each of the first step size and the second step size is to be determined when the rate of change of the corresponding step size over one or more iterations in the training process is less than a pre-determined threshold, or when a pre-determined number of epochs has been trained in the training process.
[0123] Example 7 may include the subject matter of Example 1, wherein the method further comprises, during backpropagation in a training process of the deep neural network: for computations associated with a gradient matrix representing gradient of a loss function with regard to the output of the matrix multiplication, splitting the gradient matrix into a first 4-bit integer gradient matrix and a second 4-bit integer gradient matrix, wherein the first 4-bit integer gradient matrix and the second 4-bit integer gradient matrix represent higher 4 bits and lower 4 bits of the gradient matrix, respectively.
[0124] Example 8 may include the subject matter of Example 7, wherein the splitting includes: quantizing the gradient matrix into the first 4-bit integer gradient matrix associated with a first floating-point scalar; and quantizing the difference between the gradient matrix and a product of the first 4-bit integer gradient matrix and the first floating-point scalar into the second 4-bit integer gradient matrix associated with a second floating-point scalar.
[0125] Example 9 may include the subject matter of Example 8, wherein the method further comprises, for computation of weight gradient that involves a second matrix multiplication of a transpose of the gradient matrix with the quantized first matrix: obtaining a third matrix by multiplying the transpose of the first 4-bit integer gradient matrix and the quantized first matrix; obtaining a fourth matrix by multiplying the transpose of the second 4-bit integer gradient matrix and the quantized first matrix; dequantizing the third matrix and the fourth matrix into matrices of floating-point representation; and obtaining a resultant fifth matrix by summing up the dequantized third matrix and the dequantized fourth matrix.
[0126] Example 10 may include the subject matter of Example 9, wherein in obtaining the third matrix and obtaining the fourth matrix, rows in the first 4-bit integer gradient matrix and the second 4-bit integer gradient matrix are sampled, with per-row random masks that follow Bernoulli distribution with corresponding per-row probabilities.
[0127] Example 11 may include the subject matter of Example 10, wherein each of the per-row probabilities is proportional to a corresponding per-row coefficient being equal to a product of a first norm associated with the quantized gradient matrix and a second norm associated with the quantized first matrix.
[0128] Example 12 may include the subject matter of Example 7, wherein the method further comprises, for computation of activation gradient that involves a third matrix multiplication of the gradient matrix with the quantized second matrix, obtaining a sixth matrix by multiplying the first 4-bit integer gradient matrix and the quantized second matrix; obtaining a seventh matrix by multiplying the second 4-bit integer gradient matrix and the quantized second matrix; dequantizing the sixth matrix and the seventh matrix into matrices of floating-point representation; and obtaining a resultant eighth matrix by summing up the dequantized six matrix and the dequantized seven matrix.
[0129] Example 13 may include the subject matter of Example 12, wherein in obtaining the sixth matrix and obtaining the seventh matrix, rows in the first 4-bit integer gradient matrix and the second 4-bit integer gradient matrix are sampled, with per-row random masks that follow Bernoulli distribution with corresponding per-row probabilities.
[0130] Example 14 may include the subject matter of Example 13, wherein each of the per-row probabilities is proportional to a corresponding per-row coefficient being equal to a third norm associated with the quantized gradient matrix.
[0131] Example 15 may include the subject matter of Example 4, wherein during backpropagation in the training process of the deep neural network, the gradient of a loss function with regard to the first step size is determined based on the maximum of the range and the number of elements of the activation matrix, and wherein the gradient of the loss function with regard to the second step size is determined based on the maximum of the range and the number of elements of the weight matrix.
[0132] Example 16 may include the subject matter of Example 1, wherein the deep neural network includes a transformer.
[0133] Example 17 may include the subject matter of Example 1, wherein the deep neural network is to be used for natural language processing, wherein the deep neural network takes, as its inputs, vector representations for words generated based on word-embedding.
[0134] Example 18 may include a computing device, comprising: memory for storing instructions; and one or more processing units coupled to the memory, wherein the instructions, when executed by the one or more processing units, cause the one or more processing units to perform any of the methods disclosed herein.
[0135] Example 19 may include a non-transitory computer-readable storage medium having stored thereon instructions that, when executed by one or more processing units, cause the one or more processing units to perform any of the methods disclosed herein.
[0136] Example 20 may include a computer program product comprising instructions that, when executed by one or more processing units, cause the one or more processing units to perform any of the methods disclosed herein.
[0137] What has been described above includes examples of the disclosed architecture. It is, of course, not possible to describe every conceivable combination of components and / or methodologies, but one of ordinary skill in the art may recognize that many further combinations and permutations are possible. Accordingly, the novel architecture is intended to embrace all such alterations, modifications and variations that fall within the spirit and scope of the appended claims.
Claims
1.A computer-implemented method for accelerating operations of a deep neural network, comprising:for each of a plurality of matrix multiplications during forward propagation,obtaining a first matrix by multiplying an activation matrix, which an input of the matrix multiplication, with a block-diagonal Hadamard transform matrix;obtaining a second matrix by multiplying a weight matrix, a transpose of which is another input of the matrix multiplication, with the block-diagonal Hadamard transform matrix;quantizing the first matrix and the second matrix into 4-bit integer matrices;obtaining a resultant matrix by multiplying the quantized first matrix and the transpose of the quantized second matrix; anddequantizing the resultant matrix to a matrix of floating-point representation.2.The computer-implemented method of claim 1, wherein the method is to be performed during a training process of the deep neural network.3.The computer-implemented method of claim 1, wherein the block-diagonal Hadamard transform matrix is a block-diagonal matrix consisting of multiple Hadamard transform matrices Hk, wherein k=5.4.The computer-implemented method of claim 1, wherein the quantizing includes:applying a clamp function to the first matrix and the second matrix, respectively, to restrict each of elements in the first matrix and the second matrix into a range representable by 4-bit integers,wherein a first step size for the activation matrix and a second step size for the weight matrix, each being a parameter of the clamp function, are floating-point scalars learnable during a training process of the deep neural network.5.The computer-implemented method of claim 4, wherein the dequantizing includes: multiplying the resultant matrix with the first step size and the second step size.6.The computer-implemented method of claim 4, wherein whether or not to start optimizing each of the first step size and the second step size is to be determined when the rate of change of the corresponding step size over one or more iterations in the training process is less than a pre-determined threshold, or when a pre-determined number of epochs has been trained in the training process.7.The computer-implemented method of claim 1, further comprising, during backpropagation in a training process of the deep neural network:for computations associated with a gradient matrix representing gradient of a loss function with regard to the output of the matrix multiplication, splitting the gradient matrix into a first 4-bit integer gradient matrix and a second 4-bit integer gradient matrix, wherein the first 4-bit integer gradient matrix and the second 4-bit integer gradient matrix represent higher 4 bits and lower 4 bits of the gradient matrix, respectively.8.The computer-implemented method of claim 7, wherein the splitting includes:quantizing the gradient matrix into the first 4-bit integer gradient matrix associated with a first floating-point scalar; andquantizing the difference between the gradient matrix and a product of the first 4-bit integer gradient matrix and the first floating-point scalar into the second 4-bit integer gradient matrix associated with a second floating-point scalar.9.The computer-implemented method of claim 8, further comprising, for computation of weight gradient that involves a second matrix multiplication of a transpose of the gradient matrix with the quantized first matrix:obtaining a third matrix by multiplying the transpose of the first 4-bit integer gradient matrix and the quantized first matrix;obtaining a fourth matrix by multiplying the transpose of the second 4-bit integer gradient matrix and the quantized first matrix;dequantizing the third matrix and the fourth matrix into matrices of floating-point representation; andobtaining a resultant fifth matrix by summing up the dequantized third matrix and the dequantized fourth matrix.10.The computer-implemented method of claim 9, wherein in obtaining the third matrix and obtaining the fourth matrix, rows in the first 4-bit integer gradient matrix and the second 4-bit integer gradient matrix are sampled, with per-row random masks that follow Bernoulli distribution with corresponding per-row probabilities.11.The computer-implemented method of claim 10, wherein each of the per-row probabilities is proportional to a corresponding per-row coefficient being equal to a product of a first norm associated with the quantized gradient matrix and a second norm associated with the quantized first matrix.12.The computer-implemented method of claim 7, further comprising, for computation of activation gradient that involves a third matrix multiplication of the gradient matrix with the quantized second matrix,obtaining a sixth matrix by multiplying the first 4-bit integer gradient matrix and the quantized second matrix;obtaining a seventh matrix by multiplying the second 4-bit integer gradient matrix and the quantized second matrix;dequantizing the sixth matrix and the seventh matrix into matrices of floating-point representation; andobtaining a resultant eighth matrix by summing up the dequantized six matrix and the dequantized seven matrix.13.The computer-implemented method of claim 12, wherein in obtaining the sixth matrix and obtaining the seventh matrix, rows in the first 4-bit integer gradient matrix and the second 4-bit integer gradient matrix are sampled, with per-row random masks that follow Bernoulli distribution with corresponding per-row probabilities.14.The computer-implemented method of claim 13, wherein each of the per-row probabilities is proportional to a corresponding per-row coefficient being equal to a third norm associated with the quantized gradient matrix.15.The computer-implemented method of claim 4, wherein during backpropagation in the training process of the deep neural network, the gradient of a loss function with regard to the first step size is determined based on the maximum of the range and the number of elements of the activation matrix, and wherein the gradient of the loss function with regard to the second step size is determined based on the maximum of the range and the number of elements of the weight matrix.16.The computer-implemented method of claim 1, wherein the deep neural network includes a transformer.17.The computer-implemented method of claim 1, wherein the deep neural network is to be used for natural language processing, wherein the deep neural network takes, as its inputs, vector representations for words generated based on word-embedding.18.A computing device, comprising:memory for storing instructions; andone or more processing units coupled to the memory, wherein the instructions, when executed by the one or more processing units, cause the one or more processing units to perform the method of any one of claims 1-17.19.A non-transitory computer-readable storage medium having stored thereon instructions that, when executed by one or more processing units, cause the one or more processing units to perform the method of any one of claims 1-17.20.A computer program product comprising instructions that, when executed by one or more processing units, cause the one or more processing units to perform the method of any one of claims 1-17.
Citation Information
Patent Citations
Convolutional neural network processing method and device, equipment and storage medium
CN111382854A
Fast convolution method and device suitable for microprocessor
CN112199636A
Reasoning method based on deep learning model
CN113869517A
Performing network congestion control with reinforcement learning
CN114827032A
Neural network quantization method and apparatus, and computer-readable storage medium
WO2022151505A1
Cited By
Method and apparatus for computation performed on a model
WO2026178834A1
