Method for reducing low-bit convolutional neural network reasoning operation complexity
Technical Field
The invention relates to the technical field of neural network acceleration, in particular to a method for reducing the reasoning operation complexity of a low-bit convolutional neural network.
Background
In recent years, with rapid development of technology, a large data age has come. Deep learning takes a Deep Neural Network (DNN) as a model, and has quite remarkable results in many key fields of artificial intelligence, such as image recognition, reinforcement learning, semantic analysis and the like. The Convolutional Neural Network (CNN) is used as a typical DNN structure, can effectively extract hidden layer characteristics of images, accurately classifies the images, and is widely applied to the fields of image recognition and detection in recent years.
In particular, multiplying the shift achieves 32 bits quantization as low bits: and storing the result obtained by the quantized convolution operation as 32bit shaping, and then performing multiplication and shift operation according to the pre-calculated parameters to realize conversion from 32bit to low bit.
However, when the 32 bits are quantized into the low bits in the prior art, since the accuracy after quantization needs to be ensured, a series of addition and comparison operations need to be performed in the quantization process, so that the computational complexity and the computational resource are greatly increased, and the cost is often too high especially when the quantization is performed to 2 bits.
Furthermore, the common terminology in the prior art is as follows:
Convolutional neural network (Convolutional Neural Networks, CNN): is a type of feedforward neural network that includes convolution calculations and has a depth structure.
Quantification: quantization refers to the process of approximating a continuous value (or a large number of possible discrete values) of a signal to a finite number (or fewer) discrete values.
Low bits: the data is quantized to 8bit,4bit or 2bit wide data.
Reasoning: and after the neural network training is finished, performing operation by using the stored data.
Disclosure of Invention
The application provides a method for reducing the inference operation complexity of a low-bit convolutional neural network, which aims to overcome the defects in the prior art and solve the problems of high computation complexity and high computation resource requirement in the existing low-bit model inference process.
Specifically, the invention provides a method for reducing the inference operation complexity of a low-bit convolutional neural network, which comprises the following steps:
S1, after the neural network training is finished, the stored data is used for quantization,
Let the quantization of the i-th layer be as follows:
Wherein delta i is an activation function, Q A is a quantization formula of feature map, and Q w is a quantization formula of weight;
s2, when the parameters of the formula in S1 meet the following conditions:
1)、 Representing i.e./>, with floating point scalar scaled fixed point numbers W int is a fixed point number expressed in an integer;
2)、 Representing i.e./>, with floating point scalar scaled fixed point numbers X int is a fixed point number expressed in an integer;
3) Delta i is a monotonic function;
then, quantize Obtained by fixed point number operation, namely:
s3, determining a threshold value from quantization of feature map:
The quantization formula of feature map is:
The threshold value (0.5, 1.5 … (2 k -0.5)) can be directly deduced from the quantization formula of the feature map above, where k is the quantized bit width;
since the distance between the thresholds is 1.0, only the preservation is needed in the final quantization WhereinThresholdWhere k is the quantized bit width;
s4, since the value of the feature map after quantization is already determined when the quantization is low-bit, and Q A is uniformly quantized, δi(swsxsBN(wint·xint+bi/(swsxsBN))) in S2 obtains the final quantization result by comparing with a series of the thresholds (T 1,T2…Tn) in step S3.
And when the quantization is carried out to the low bit 2bit in the step S2, the quantized feature map has the values of 0,1,2 and 3.
Since δ i is a monotonic function in step S2, S wsx > 0, it is also possible to pass (w int·xint+bi/(swsxsBN)) andComparison between them to obtain quantized results.
In the step S4, since S BN are different for each channel, one for each channel is required to be saved when the threshold is saved.
Thus, the present application has the advantages that:
1. the 32 bits are quantized into low bits directly through threshold comparison, so that the complexity of operation is reduced;
2. The overall running time of the quantization model is reduced;
3. the demand of operation resources is reduced;
The operation of 64 bits by 64 bits is avoided.
Drawings
The accompanying drawings, which are included to provide a further understanding of the application and are incorporated in and constitute a part of this specification, illustrate and together with the description serve to explain the application.
FIG. 1 is a schematic flow chart of the method of the present invention.
Detailed Description
In order that the technical content and advantages of the present invention may be more clearly understood, a further detailed description of the present invention will now be made with reference to the accompanying drawings.
As shown in fig. 1, the method for reducing the inference operation complexity of the low-bit convolutional neural network comprises the following steps:
S1, after the neural network training is finished, the stored data is used for quantization,
Let the quantization of the i-th layer be as follows:
Wherein delta i is an activation function, Q A is a quantization formula of feature map, and Q w is a quantization formula of weight;
s2, when the parameters of the formula in S1 meet the following conditions:
1)、 Representing i.e./>, with floating point scalar scaled fixed point numbers W int is a fixed point number expressed in an integer;
2)、 Representing i.e./>, with floating point scalar scaled fixed point numbers X int is a fixed point number expressed in an integer;
3) Delta i is a monotonic function;
then, quantize Obtained by fixed point number operation, namely:
s3, determining a threshold value from quantization of feature map:
The quantization formula of feature map is:
The threshold value (0.5, 1.5 … (2 k -0.5)) can be directly deduced from the quantization formula of the feature map above, where k is the quantized bit width;
since the distance between the thresholds is 1.0, only the preservation is needed in the final quantization WhereinThresholdWhere k is the quantized bit width;
S4, since the value of the feature map after quantization is already determined when the quantization is low-bit, and Q A is uniformly quantized, δi(swsxsBN(wint·xint+bi/(swsxsBN))) in S2 obtains the final quantization result by comparing with a series of the thresholds (T 1,t2…Tn) in step S3.
In particular, the method of the application can also be expressed as follows:
suppose the quantization calculation for the i-th layer is as follows:
Wherein delta i is an activation function, Q A is a quantization formula of feature map, and Q w is a quantization formula of weight
The parameters in the above formula meet the following conditions:
1、 i.e./>, can be represented by fixed point numbers scaled with floating point scalar W int is the fixed point number expressed in integers
2、I.e./>, can be represented by fixed point numbers scaled with floating point scalarX int is the fixed point number expressed in integers
3. Delta i is a monotonic function
So calculate the finalThe method can be obtained by calculating the fixed point number:
Since the value of the quantized feature map is actually determined (taking 2 bits as an example, the feature map takes 0,1,2, 3) and Q A is uniformly quantized when quantized to low bits, δi(swsxsBN(wint·xint+bi/(swsxsBN))) can be compared with a series of thresholds (T 1,T2…Tn) to obtain a quantized result, and since δ i is a monotonic function, s wsx > 0, it can also be obtained by (w int·xint+bi/(swsxsBN) and (d) a quantization result Comparison between them to obtain quantized results.
The determination of the threshold needs to start with the quantization formula of the feature map.
The quantization formula of feature map is:
from the above equation, the threshold value can be directly deduced to be (0.5, 1.5 … (2 k -0.5)), where k is the quantized bit width. Since the distance between the thresholds is 1.0, we only need to preserve at the final quantization Wherein the method comprises the steps ofThreshold value Where k is the quantized bit width; since s BN are different for each channel, one per channel is required to save the threshold.
The above description is only of the preferred embodiments of the present invention and is not intended to limit the present invention, and various modifications and variations can be made to the embodiments of the present invention by those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.