Quantization method for effectively improving precision of 4-bit model
By analyzing the minmax distribution of each layer in the 4-bit model and setting it to 8 bit, a 4/8-bit hybrid model is formed, which solves the problem of accuracy loss of the 4-bit model and significantly improves the quantization accuracy. It is especially suitable for networks with large differences in default values.
Patent Information
- Application Number
- CN202310366730.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-07
- Publication Date
- 2024-10-15
AI Technical Summary
During the quantization process, the accuracy loss of the 4-bit model is difficult to avoid, especially after training, the problem of accuracy loss still exists.
By analyzing the minmax distribution of each layer in the 4-bit model, setting the larger value layer to 8-bit, a 4/8-bit hybrid model is formed, and quantization-aware training is performed, and the quantization range is recalculated to reduce accuracy loss.
It effectively reduces the accuracy loss caused by the quantization model and improves the low-bit quantization accuracy, especially in networks with large differences in default values.
Smart Images

Figure CN118780332A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of neural network, and in particular relates to a quantization method for effectively improving the accuracy of a 4-bit model. Background Art
[0002] With the popularization of artificial intelligence applications in mobile phones and IoT, the computing performance and storage capacity of terminal hardware are relatively weak due to the limitations of energy consumption and device size. This has brought new challenges to artificial intelligence models - the models need to be smaller, faster and stronger. Quantization is one of the important means. Therefore, quantization has become a hot research direction in academia and industry in recent years. Model quantization, in simple terms, is to replace the original floating-point data (32 bits) with lower-bit data (such as 8-bit integers). 8-bit quantization of weight: data_type=qint8, data range is [-128,127]; 8-bit quantization of activation: data_type=quint8, data range is [0,255]; bias is generally not quantized and still maintains the float32 data type. There is one more thing that needs to be explained in advance. The weight is generally fixed after the floating-point model training converges, so it can be directly quantized according to the original data. However, the activation will cause the data range to be different each time due to the different input data. Therefore, to address this problem, there will be a special calibration process in the quantization process, that is, prepare a small calibration data set in advance, and record the data range of each activation when testing this calibration data set, and then determine a fixed range based on the recorded value.
[0003] For the inference process, weight is a constant tensor with a fixed dynamic range, while the dynamic range of activation varies. Its actual dynamic range must be obtained through sampling (this process is generally called data calibration).
[0004] When converting floating-point quantization to fixed-point, the minimum / maximum value is adjusted (range adjustment), that is, the dynamic range of the floating-point number is selected. The selection of the dynamic range directly determines the distribution of the quantized data. The data outside the dynamic range will be mapped to the boundary points of the quantized data, that is, the selection of the range directly determines the quantization error.
[0005] First of all, the benefits of quantification are:
[0006] Quantization is to store floating point numbers that were previously expressed in 32 bits or 64 bits in a form that takes up less memory space, such as 8 bits, 16 bits, or even 1 bit or 2 bits. The benefits of this are:
[0007] 1. Reduce the model size and reduce the model storage space requirements;
[0008] 2. Reduce memory bandwidth requirements;
[0009] 3. Accelerated computing. Modern mobile devices basically support SIMD (Single Instruction Multiple Data). Taking a 128-bit register as an example, a single instruction can simultaneously calculate four 32-bit single-precision floating points, or eight 16-bit integers, or sixteen 8-bit integers. Obviously, with the support of SIMD, the calculation rate of 8-bit integers is faster.
[0010] Secondly, the problem faced by quantification is:
[0011] The core challenge of quantization is how to reduce the model data bit width while maintaining the accuracy of the model, that is, to make a trade-off between compression rate and accuracy loss. This leads to many interesting sub-problems, such as what is the quantization object (weight, activation, gradient), how many bits are quantized (8 bits, 4 bits, 2 bits, 1 bit), which quantization parameters are selected (such as moving rate, clipping value), whether the quantization parameters can be automatically optimized, whether different layers require different quantization parameters, how to restore accuracy after quantization or consider quantization during training, etc.
[0012] Model quantization mainly includes two parts: one is the quantization of weights, and the other is the quantization of activation values. Only by quantizing both parts at the same time can the maximum computational efficiency be achieved. Weights can be distributed as compactly as possible to reduce outliers and uneven distributions with the help of network regularization and other means, but there is still a lack of effective means for activation values.
[0013] In addition, the commonly used technical terms in the prior art are:
[0014] 1. Quantization-aware training: During the quantization process, the network is trained so that the network parameters can better adapt to the information loss caused by quantization. This is called quantization-aware training, in which the network's forward (forward) simulates INT8 calculations, and the backward (backward) is still FP32. See Figure 1(1) and Figure 1(2).
[0015] 2. Asymmetric quantization: Map the minimum / maximum values in the floating point range to the minimum / maximum values in the integer range. This is done by using a zero point (also called quantization error, or offset) in addition to the scaling factor. See Figure 2(1).
[0016] 3. Symmetric quantization: Instead of mapping the exact minimum / maximum value of the floating-point range to the quantization range, the maximum absolute value between the minimum / maximum values is selected. In addition, the zero point is not used, that is, the floating-point range of effective quantization is symmetric with respect to zero, and so is the quantization range. As shown in Figure 2(2). Summary of the invention
[0017] In order to solve the above problems, the purpose of this application is: because the 8-bit accuracy in most quantizations can be guaranteed, and even no training is required to meet the requirements (direct post-quantization), but for the 4-bit training quantization, sometimes there will still be accuracy loss even after training. The present invention mainly proposes a method that can effectively improve the accuracy of the 4-bit bit model.
[0018] Specifically, the present invention provides a quantization method for effectively improving the accuracy of a 4-bit model, the method comprising the following steps:
[0019] S1, training floating-point model: training the floating-point model to obtain a floating-point model with acceptable accuracy;
[0020] S2, prepare a calibration set: the calibration set uses a part of the training set or a picture similar to the test scene;
[0021] S3, perform 4-bit quantization statistics minmax and save:
[0022] Perform statistics to obtain the minmax required for quantization to determine the dynamic range of floating-point numbers;
[0023] If every FP32 value in the quantization process is within this actual dynamic range, the dynamic range refers to the range from the minimum value to the maximum value calculated previously, which is called an unsaturated state; conversely, if an FP32 value is not within this actual dynamic range, it is called a saturated state;
[0024] S4, 4-bit model training: load the floating-point model to convert it to a 4-bit model and then perform quantization-aware training; after the training is completed, perform an accuracy test on the 4-bit model. If the accuracy meets the requirement, the training ends directly; if it does not meet the requirement, go to step S5;
[0025] S5, analyzing the distribution of minmax, setting the minmax range to a value outside (-6, 6), the range can be adjusted according to the actual situation, and the layer is 8 bits; further comprising:
[0026] S5.1, analyze the distribution of minmax of each layer;
[0027] S5.2, for some layers with relatively large values, set to 8 bits.
[0028] Assumption: The minmax statistics of a feature of a 40-layer network are as follows:
[0029] min:
[0030] -9.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 2 11 10 7 4 3 2 1
[0031] max:
[0032] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 9.5 2 3 3 6 4 10 8 2
[0033] As can be seen from the above table, most of the min values are within the range of -2.0-1.0, and only a few layers are outside the range of -9.5; in addition, most of the max values are within the range of 0.0-3.0, and only a few layers are outside the range of 9.5. At this time, these layers with values outside the range are set to 8 bits, that is, both weight and feature are set to 8 bits to ensure accuracy, and then minmax is recalculated according to the set bit, that is, the maximum value Rmax and minimum value Rmin of the floating point range are obtained, and the maximum value Qmax and minimum value Qmin of the quantization range are obtained, and then linear mapping is performed through the scale s=(Rmax-Rmin) / (Qmax-Qmin), z=Qmax-Rmax / s: q=r / s+z and then the following process is continued;
[0034] S6, 4 / 8bit mixed model training: It is a 4-bit model, but some layers are set to 8-bit in the previous step, so the entire network has 4-bit and 8-bit, which is a 4 / 8-bit mixed model for training;
[0035] S7, determine whether it has converged. If yes, then end directly; if no, then repeat step S5.
[0036] The step S4 does not meet the requirement, including a difference of 3-5 points.
[0037] The step S4 further comprises:
[0038] S4.1, determine whether it has converged? If not, return to quantization-aware training; if yes, continue to step S4.2;
[0039] S4.2, determine whether there is precision loss? If yes, end; if no, go to step S5.
[0040] Therefore, the advantage of the present application is that the method can reduce the accuracy loss caused by the subsequent quantization model. Experiments have shown that this method has a practical effect on networks that are significantly different from the default values and can greatly improve the low-bit quantization accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] The drawings described herein are used to provide a further understanding of the present invention, constitute a part of this application, and do not constitute a limitation of the present invention.
[0042] Figure 1(1) is a schematic diagram of reasoning with only integer operations.
[0043] Figure 1(2) is a schematic diagram of simulated quantization training.
[0044] Figure 2(1) is a schematic diagram of asymmetric quantization.
[0045] Figure 2(2) is a schematic diagram of symmetric quantization.
[0046] Figure 3 It is a flow chart of the method of the present application. DETAILED DESCRIPTION
[0047] In order to more clearly understand the technical content and advantages of the present invention, the present invention is now further described in detail in conjunction with the accompanying drawings.
[0048] The present application relates to a quantization method for effectively improving the accuracy of a 4-bit model. According to the distribution of calibration values of a full-precision model under 4 bits, an appropriate threshold is selected, such as (-6, 6), which can be adjusted according to actual conditions. Layers with larger fixed values obtained by calibration are set to 8 bits, and the model is targetedly changed to a 4 / 8 hybrid, thereby reducing the accuracy loss caused by the quantization model. Experiments have shown that this method can better improve the model accuracy and ensure the quantization quality for some networks when pure 4 bits cannot meet the target accuracy requirements.
[0049] Furthermore, if Figure 3 As shown, the method comprises the following steps:
[0050] S1. Training floating-point model: Training a floating-point model. This does not involve a specific network. This method can be applied to classification, detection or segmentation network models. A floating-point model with acceptable accuracy is obtained by default. Here, the acceptable accuracy is expected, and the main focus is on the accuracy after quantization. By default, a floating-point model with acceptable accuracy is obtained.
[0051] S2. Prepare a calibration set: It can be part of the training set or a picture with similar test scenes. There is not much difference between the two. For example, for human detection, the training set and validation set are mostly public data sets with more diverse scenes, while the actual collection is slightly more single.
[0052] S3. Perform 4-bit quantization statistics minmax and save: Perform statistics to obtain the minmax required for quantization to determine the dynamic range of floating-point numbers. Currently, the most commonly used methods in major deep learning frameworks and inference frameworks of the three major platforms are the maximum and minimum values (MinMax), the moving average maximum and minimum values (MovingAverageMinMax) and the KL distance (Kullback-Leiblerdivergence). All three methods are applicable, but the maximum and minimum value method is used in the experiment here;
[0053] If every FP32 value in the quantization process is within this actual dynamic range, it is generally called an unsaturated state; conversely, if some FP32 values are not within this actual dynamic range, it is called a saturated state.
[0054] S4.4-bit model training: load the floating-point model and convert it to a 4-bit model, then perform quantization-aware training. After training, perform an accuracy test on the 4-bit model. If the accuracy meets the requirement, the training can be terminated directly. If the requirement is not met by a few points, the training can be transferred to step S5.
[0055] S4.1, determine whether it has converged? If not, return to quantization-aware training; if yes, then S4.2, determine whether there is precision loss? If yes, end; if not, go to step S5; S5. Analyze the distribution of minmax, set the layer with a larger minmax range value to 8bit, and further include:
[0056] S5.1, analyze the distribution of minmax of each layer;
[0057] S5.2, for some layers with relatively large values, set to 8 bits.
[0058] For example:
[0059] The minmax statistics of a feature of a 40-layer network are as follows:
[0060] min:
[0061] -9.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 2 11 10 7 4 3 2 1
[0062] max:
[0063] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 9.5 2 3 3 6 4 10 8 2
[0064] As can be seen from the above table, most of the min values are in the range of -2.0-1.0, and only a few (two layers) are in the range of -9.5, which is larger; in addition, most of the max values are in the range of 0.0-3.0, and only a few (two layers) are around 9.5. At this time, you can set the layers with these outer values to 8 bits. Generally, both weight and feature are set to 8 bits to ensure accuracy, and then recalculate minmax according to the set bit, and then continue with the subsequent process.
[0065] S6, 4 / 8bit mixed model training;
[0066] S7, determine whether it has converged. If yes, then end directly; if no, then repeat step S5.2.
[0067] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the embodiments of the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A quantization method for effectively improving the accuracy of a 4-bit model, characterized in that: The method comprises the following steps: S1, training floating-point model: training the floating-point model to obtain a floating-point model with acceptable accuracy; S2, prepare a calibration set: the calibration set uses a part of the training set or a picture similar to the test scene; S3, perform 4-bit quantization statistics minmax and save: Perform statistics to obtain the minmax required for quantization to determine the dynamic range of floating-point numbers; If every FP32 value in the quantization process is within the actual dynamic range, the dynamic range refers to the range from the minimum value to the maximum value calculated previously, which is called an unsaturated state; on the contrary, if the FP32 value is not within the actual dynamic range, it is called a saturated state; S4, 4-bit model training: load the floating-point model to convert it to a 4-bit model and then perform quantization-aware training; after the training is completed, perform an accuracy test on the 4-bit model. If the accuracy meets the requirement, the training ends directly; if it does not meet the requirement, go to step S5; S5, analyze the distribution of minmax, set the minmax range to a large value outside (-6,6), the range can be adjusted according to the actual situation, the layer is 8bit, further including: S5.1, analyze the distribution of minmax of each layer; S5.2, for some layers with large range of values, set to 8 bits. Assumption: The minmax statistics of a feature of a 40-layer network are as follows: min: max: As can be seen from the above table, most of the min values are within the range of -2.0-1.0, and only a few layers are outside the range of -9.5; in addition, most of the max values are within the range of 0.0-3.0, and only a few layers are outside the range of 9.
5. At this time, these layers with values outside the range are set to 8 bits, that is, both weight and feature are set to 8 bits to ensure accuracy, and then minmax is recalculated according to the set bit. Because the calculation of minmax is related to the current bit position, when the bit position of a certain layer is modified, it is necessary to recalculate according to the minmax method, that is, to obtain the maximum value Rmax and minimum value Rmin of the floating point range, and the maximum value Qmax and minimum value Qmin of the quantization range, and then linearly map through the scale s=(Rmax-Rmin) / (Qmax-Qmin), z=Qmax-Rmax / s: q=r / s+z, and then continue with the following process; S6, 4 / 8bit mixed model training: It is a 4-bit model, but some layers are set to 8-bit in the previous step, so the entire network has 4-bit and 8-bit, which is a 4 / 8-bit mixed model for training; S7, determine whether it has converged. If yes, then end directly; if no, then repeat step S5.
2. A quantization method for effectively improving the accuracy of a 4-bit model according to claim 1, characterized in that: The step S4 does not meet the requirement, including a difference of 3-5 points.
3. A quantization method for effectively improving the accuracy of a 4-bit model according to claim 1, characterized in that: The step S4 further comprises: S4.1, determine whether it has converged? If not, return to quantization-aware training; if yes, continue to step S4.2; S4.2, determine whether there is precision loss? If yes, end; if no, go to step S5.
