Method for setting quantization bit of neural network model
Optimizing the number of activation value quantization bits through data sets and integer linear programming methods solves the problem of selecting the number of activation value quantification bits in deep learning models, minimizing accuracy loss and improving hardware resource utilization under memory constraints.
Patent Information
- Application Number
- PCT/CN2023/100668
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-04-23
- Filing Date
- 2023-06-16
- Publication Date
- 2024-10-31
AI Technical Summary
Under memory constraints, the selection of the number of quantization bits for activation values in deep learning models is difficult to balance model accuracy and hardware resource utilization. Existing technologies often ignore the selection of the number of quantization bits for activation values or fail to effectively reduce model accuracy loss.
By preparing the data set, defining the quantization loss evaluation index and the number of model quantization bits, calculating the quantization loss for different quantization bits of the activation value, and using the integer linear programming method to optimize the selection of the quantization number of the activation value to minimize the number of quantization bits under the hardware memory limit. Loss of accuracy.
It achieves the selection of the optimal number of activation value quantization bits under memory constraints, reduces model accuracy loss, and improves hardware resource utilization efficiency.
Smart Images

Figure CN2023100668_31102024_PF_FP_ABST
Abstract
Description
A method for setting quantization bits of neural network models Technical Field
[0001] The present invention relates to the technical field of model quantization, and in particular to a method for setting quantization bits of a neural network model. Background Art
[0002] With the continuous development of deep learning technologies, neural network models have been widely used in many industries and scenarios. However, deep learning models have large parameter counts and computational complexity, requiring high hardware resources, which often conflicts with hardware resource limitations.
[0003] In the IoT, a large number of embedded devices exist. Due to the extremely limited hardware resources of these devices, deploying deep learning models is difficult. Furthermore, as deep learning models grow in size, the pressure of deploying these large models is increasing, even on servers or in the cloud. Memory limitations are a key factor hindering model deployment, determining whether the model can be deployed on a device.
[0004] However, during deep learning model inference, activation values typically occupy the majority of memory. Model quantization, particularly multi-precision model quantization, is an effective method for reducing model memory usage. However, compared to traditional quantization methods, multi-precision quantization often results in a significant loss of model accuracy. Choosing the right number of quantization bits for different weights and activation values is crucial for influencing this loss. Currently, some methods focus on multi-precision quantization of model weights without considering the number of activation bits. Conversely, some methods focus on selecting the number of activation bits, but fail to consider the objective of reducing model accuracy.
[0005] Summary of the Invention
[0006] The purpose of the present invention is to provide a method for setting the number of quantization bits of a neural network model to solve the problems raised in the above background technology.
[0007] To achieve the above object, the present invention provides the following technical solution: a method for setting the number of quantization bits of a neural network model, the method comprising the following steps:
[0008] Prepare a dataset containing a small amount of data;
[0009] Define quantitative loss assessment indicators;
[0010] Define the number of quantization bits for the model;
[0011] Get model-related memory limits;
[0012] Define the target neural network model;
[0013] Calculate the quantization loss of different quantization bits of activation value;
[0014] Analyze the model reasoning steps and the activation values in memory at each moment;
[0015] Optimization problems are constructed and solved using integer linear programming methods;
[0016] Model quantization and inference.
[0017] Preferably, when preparing a dataset containing a small amount of data, the dataset is used as a calibration dataset and a quantization loss calculation dataset.
[0018] Preferably, when defining the quantization loss evaluation index, the difference between the non-quantized activation value data and the quantized activation value data is calculated, and the difference is calculated using, but not limited to, mean square error, cosine similarity, etc., as the quantization loss.
[0019] Preferably, when defining the model quantization bit number, based on hardware support and model accuracy requirements, the model quantization bit number is but not limited to 2, 4, 6, or 8 bits, and Bitwidth is used to represent the quantization bit number set, Bitwidth = {2, 4, 6, 8}.
[0020] Preferably, when obtaining the model-related memory limit, during the model inference process, the inference-related memory usage includes the inference framework memory usage, code data memory usage, model weight memory usage, and activation value memory usage, wherein the model-related memory usage mainly includes the model weight memory usage and activation value memory usage; assuming that the hardware memory size is m total , the memory outside the model related memory is m other , remaining memory size m model , which is the model-related memory limit.
[0021] Preferably, when defining the target neural network model, the neural network model is a mainstream neural network model including but not limited to ResNet, MobileNet, and SSD.
[0022] Preferably, when calculating the quantization loss of different quantization bits of activation values, for each sample data in the loss calculation data set, use the pre-quantization model for inference, and obtain all activation value data. For all activation value data, use all quantization bits to calculate the quantization loss of different quantization bits of each activation value. After completing the inference of all sample data, summarize the quantization loss of different quantization bits of each activation value, and use but not limited to the mean method to calculate the quantization loss set Loss of different quantization bits of each activation value of all sample data; the formula is as follows, where loss ab Indicates the quantization loss of activation value data a using quantization bit b, Loss = {loss ab}, a∈Activations, b∈Bitwidth.
[0023] Preferably, when analyzing the model inference steps and the activation values in memory at each moment, during the model inference process, the model weights are always kept in memory, and the activation values that are no longer used are destroyed in time to release the occupied memory space; let Activations represent all the activation values of the model, activation_set t represents the set of activation values in memory at time t, so any are all subsets of Activations, and at different times The elements in will be repeated; reasoning step by step, we can get the activation value set Activation in memory at all times sets , the formula is as follows, Where T represents the set of all moments in the reasoning process.
[0024] Preferably, when constructing the optimization problem and solving it using the integer linear programming method, the optimization objective is to minimize the total quantization loss. The constraint is that the memory occupied by the activation value set at each moment is less than the activation value memory limit. Since the memory occupied by the model weight can be calculated in advance, it is assumed to be m weight , let m c Indicates the activation value memory limit, and m is calculated using the following formula c , m c =m model -m weight
[0025] Let total_loss represent the total quantization loss, m t Indicates the memory size occupied by the activation value set at time t, m a Indicates the memory size occupied by the activation value a, size a Indicates the number of elements of activation value a, b a Indicates the number of quantized bits of activation value a, From the Loss set, min total_loss m a =size a *b a
[0026] The optimization problem is an integer linear programming problem, and the optimization variable is the number of quantization bits b for each activation value. a , the integer linear programming method is used to solve and obtain the number of quantization bits for each activation value.
[0027] Preferably, during model quantization and inference, the optimized quantization bit number is used to perform model quantization and then perform model inference.
[0028] Compared with the prior art, the present invention has the following beneficial effects:
[0029] The method for setting the number of quantization bits for a neural network model proposed in this paper solves the problem of selecting the number of activation value quantization bits under memory constraints. First, the precision loss of activation values at different bit widths is analyzed. Second, the set of activation values in memory at different times is analyzed. An optimization problem is constructed. Under the condition of limiting the memory usage of activation values at all times, an integer linear programming method is used to optimize the optimal precision loss. This method can select the optimal number of activation value quantization bits under hardware memory constraints, minimizing precision loss, and has high practical and innovative value. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] FIG1 is a flow chart of the method of the present invention. DETAILED DESCRIPTION
[0031] In order to clearly and completely describe the objectives and technical solutions of the present invention and make the advantages more clearly understood, the embodiments of the present invention are further described in detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are part of the embodiments of the present invention, not all of them, and are only used to explain the embodiments of the present invention, not to limit the embodiments of the present invention. All other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.
[0032] Referring to FIG1 , the present invention provides a technical solution: a method for setting the number of quantization bits of a neural network model, the method comprising the following steps:
[0033] First, prepare a dataset containing a small amount of data. This dataset serves as a calibration dataset and a dataset for calculating the quantization loss.
[0034] Next, define the quantization loss evaluation metric. Quantization loss calculation involves calculating the difference between non-quantized activation data and quantized activation data. This difference can be calculated using, but is not limited to, mean square error (MSE) or cosine similarity, as the quantization loss. Non-quantized activation data refers to the use of floating-point weights and inputs (previous activation data) to calculate corresponding operators, such as convolution and full connection. The calculation result is the non-quantized activation data. Quantized activation data refers to quantizing the floating-point weights and input data separately, calculating the corresponding operators using the quantized data, and dequantizing the calculated results to obtain the quantized activation data.
[0035] Third, define the model quantization bit number. Based on hardware support and model accuracy requirements, the model quantization bit number can be, but is not limited to, 2, 4, 6, 8, etc. Bitwidth is used to represent the quantization bit number set. Bitwidth = {2, 4, 6, 8}
[0036] Fourth, obtain the model-related memory limit. During the model inference process, the inference-related memory usage includes the inference framework memory usage, code data memory usage, model weight memory usage, and activation value memory usage, among which the model-related memory usage mainly includes the model weight memory usage and activation value memory usage. Assuming the hardware memory size is m total , the memory outside the model related memory is m other , remaining memory size m model , that is, the model-related memory limit. m model =m total -m other
[0037] Fifth, define the target neural network model. Neural network models include but are not limited to mainstream neural network models such as ResNet, MobileNet, and SSD.
[0038] Sixth, calculate the quantization loss of different quantization bits of activation values. For each sample data in the loss calculation dataset, use the pre-quantization model for inference and obtain all activation value data. At the same time, for all activation value data, use all quantization bits to calculate the quantization loss of different quantization bits of each activation value. After completing the inference of all sample data, summarize the quantization loss of different quantization bits of each activation value, and use methods such as but not limited to the mean to calculate the quantization loss set Loss of different quantization bits of each activation value of all sample data. The formula is as follows, where loss ab Indicates the quantization loss of activation value data a using the quantization bit b. Loss = {loss ab}, a=Activations, b=Bitwidth
[0039] Seventh, analyze the model inference steps and the activation values in memory at each moment. During the model inference process, the model weights are always kept in memory, and the activation values that are no longer used will be destroyed in time to release the occupied memory space. Let Activations represent all the activation values of the model, activation_set t represents the set of activation values in memory at time t, so any are all subsets of Activations, and at different times The elements in may be repeated. Step by step reasoning, get the activation value set Activation in memory at all times sets , the formula is as follows, where T represents the set of all moments in the reasoning process.
[0040] Eighth, the optimization problem is constructed and solved using the integer linear programming method. Let the optimization objective be to minimize the total quantization loss, and the constraint condition is that the memory occupied by the activation value set at each moment is less than the activation value memory limit. Since the memory occupied by the model weight can be calculated in advance, it is assumed that m weight , let m c Indicates the activation value memory limit, and m is calculated using the following formula c . m c =m model -m weight
[0041] Let total_loss represent the total quantization loss, m t Indicates the memory size occupied by the activation value set at time t, m a Indicates the memory size occupied by the activation value a, size a Indicates the number of elements of activation value a, b a Indicates the number of quantized bits of the activation value a. Can be obtained from the Loss set. min total_loss m a =size a *b a
[0042] The above optimization problem is an integer linear programming problem, and the optimization variable is the number of quantization bits b for each activation value. a , can be solved by integer linear programming method to obtain the number of quantization bits for each activation value.
[0043] Ninth, model quantization and inference. Use the optimized quantization bit count to quantize the model and then perform model inference.
[0044] While embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions, and variations may be made to these embodiments without departing from the principles and spirit of the invention, and that the scope of the invention is defined by the appended claims and their equivalents.
Claims
1. A method for setting the number of quantization bits of a neural network model, characterized in that: The method comprises the following steps: Prepare a dataset containing a small amount of data; Define quantitative loss assessment indicators; Define the number of quantization bits of the model; Get model-related memory limits; Define the target neural network model; Calculate the quantization loss of different quantization bits of activation value; Analyze the model reasoning steps and the activation values in memory at each moment; Optimization problem construction and solving using integer linear programming methods; Model quantization and inference.
2. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: When preparing a dataset containing a small amount of data, the dataset is used as a calibration dataset as well as a dataset for quantization loss calculation.
3. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: When defining the quantization loss evaluation indicator, the difference between the non-quantized activation value data and the quantized activation value data is calculated. The difference is calculated using, but not limited to, mean square error, cosine similarity, etc. as the quantization loss.
4. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: When defining the model quantization bit number, based on hardware support and model accuracy requirements, the model quantization bit number is but not limited to 2, 4, 6, 8 bits, and Bitwidth is used to represent the quantization bit set, Bitwidth = {2, 4, 6, 8}.
5. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: When obtaining model-related memory limits, during model inference, the inference-related memory usage includes the inference framework memory usage, code data memory usage, model weight memory usage, and activation value memory usage. The model-related memory usage mainly includes the model weight memory usage and activation value memory usage. Assume that the hardware memory size is m total , the memory outside the model-related memory usage is m other , remaining memory size m model , which is the model-related memory limit.
6. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: When defining a target neural network model, the neural network model is a mainstream neural network model including but not limited to ResNet, MobileNet, and SSD.
7. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: When calculating the quantization loss of different quantization bits of activation values, for each sample data in the loss calculation dataset, use the pre-quantization model for inference and obtain all the activation value data. For all activation value data, all quantization bits are used to calculate the quantization loss of different quantization bits of each activation value. After completing the inference of all sample data, the quantization loss of different quantization bits of each activation value is summarized, and the quantization loss set Loss of different quantization bits of each activation value of all sample data is calculated by using methods such as but not limited to the mean; the formula is as follows, where loss ab Indicates the quantization loss of activation value data a using quantization bit b, Loss = {loss ab }, a∈Activations, b∈Bitwidth.
8. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: When analyzing the model inference steps and the activation values in memory at each moment, during the model inference process, the model weights are always kept in memory, and the activation values that are no longer used will be destroyed in time to release the occupied memory space; let Activations represent all the activation values of the model, activation_set t represents the set of activation values in memory at time t, so any are all subsets of Activations, and at different times The elements in will be repeated; step by step reasoning, get the activation value set Activation in memory at all times sets , the formula is as follows, Where T represents the set of all moments in the reasoning process.
9. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: When the optimization problem is constructed and solved using the integer linear programming method, the optimization goal is to minimize the total quantization loss. The constraint is that the memory occupied by the activation value set at each moment is less than the activation value memory limit. Since the memory occupied by the model weight can be calculated in advance, it is assumed to be m weight , let m c Indicates the activation value memory limit, and m is calculated using the following formula c , m c =m model -m weight Let total_loss represent the total quantization loss, m t Indicates the memory size occupied by the activation value set at time t, m a Indicates the memory size occupied by the activation value a, size a Indicates the number of elements with activation value a, b a Indicates the number of quantized bits of the activation value a, From the Loss set, min total_loss m a =size a *b a The optimization problem is an integer linear programming problem, and the optimization variable is the number of quantization bits b for each activation value. a , the integer linear programming method is used to solve and obtain the number of quantization bits for each activation value.
10. The method for setting the number of quantization bits of a neural network model according to claim 1, characterized in that: During model quantization and inference, the optimized number of quantization bits is used to quantize the model and then perform model inference.
Citation Information
Patent Citations
8-bit integer full-quantization reasoning method and device based on self-adaptive dynamic shifting
CN111950715A
Neural network model quantification method, system and device and computer readable medium
CN114021691A
Network model quantification method and device, electronic equipment and storage medium
CN114841339A
Neural network model quantification method and device, storage medium and electronic equipment
CN115879525A
Air conditioner for micro electric vehicle
KR1020230060157A
