A neural network quantization method and device with adaptive bit width

Through the adaptive bit-width neural network quantization method, the problem of high resource consumption of neural networks on portable devices is solved, the storage and computing resources are reduced, and the quantification training time is shortened, making it suitable for deployment on embedded devices.

CN112686384BActive Publication Date: 2024-08-27NANJING UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202011630360.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-12-31
Publication Date
2024-08-27
Estimated Expiration
2040-12-31

AI Technical Summary

Technical Problem

When existing neural networks are deployed on portable devices, memory, computing resources and power consumption are too high, and multi-bit binary quantization training is time-consuming and has a large loss of accuracy, leaving room for optimization.

Method used

Adopt the adaptive bit width neural network quantization method, establish a lookup table by minimizing the expected mean square error of quantization, initialize and adjust the number of quantization bits of weight parameters, perform multi-bit quantization and backpropagation, accumulate sensitivity and adjust the quantization bits number, and finally deploy a multi-bit binary network.

Benefits of technology

It significantly reduces the storage and computing resource consumption of neural networks, reduces quantitative training time, is suitable for deployment on embedded devices, and effectively reduces inference time, memory consumption and power consumption.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112686384B
    Figure CN112686384B
Patent Text Reader

Abstract

The present invention discloses a neural network quantization method and device with adaptive bit width. The steps of the method are: obtaining a lookup table of quantization strategies under different quantization bit numbers by minimizing the expected mean square error of quantization under multi-bit binary constraints; using the lookup table to quantize different weight parameters during quantization training; after the forward process and the reverse process, using the gradient of the quantization weight parameter to calculate the sensitivity of each weight parameter to the loss function, and adjusting the quantization bit number of each weight parameter according to the accumulated sensitivity after a certain number of iterations. The device of the present invention includes a multi-bit quantization module and a quantization bit number adjustment module. The method and device of the present invention can not only greatly reduce the storage space and calculation amount occupied by the neural network parameters, but also reduce the training time, so that it can be conveniently deployed in embedded devices, mobile devices or other terminals.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of neural network compression, and in particular to a quantization method and device for a neural network with adaptive bit width. Background Art

[0002] In recent years, deep neural networks have made significant progress in many fields, such as object recognition, image restoration, and semantic segmentation. However, a neural network layer may contain tens of thousands of parameters and generate millions of calculations in one iteration. Due to the limitations of computing power, memory, and power consumption, neural network algorithms are difficult to deploy on portable devices such as smartphones, smart wearable devices, and drones.

[0003] In neural network algorithms, the data being operated is usually composed of 32-bit floating-point numbers, and the main operations are floating-point multiplication and addition. These operations and floating-point numbers consume most of the computing resources and storage resources. Therefore, using fewer bits to represent these floating-point data and using operations that require less computing resources to replace floating-point calculations can greatly reduce the amount of calculation, storage, and power consumption.

[0004] Multi-bit binary network is a special type of network that binarizes network parameters, where each quantized parameter is composed of multiple bits represented by binary values ​​({0, 1} or {-1, 1}). By binarizing the parameters of the neural network with multiple bits, the original floating-point operations can be replaced by binary operations (AND, OR, NOT, XOR, etc.). However, the previous quantization training method of binarizing neural networks with multiple bits is usually very time-consuming, and the accuracy loss caused by quantization still has room for further optimization. Summary of the invention

[0005] The present invention mainly aims at the problems of large memory and computing resource consumption and high power consumption of existing convolutional neural networks, and proposes a multi-bit neural network quantization method and device with adaptive bit width.

[0006] The technical solution adopted by the method of the present invention is as follows:

[0007] A neural network quantization method with adaptive bit width comprises the following steps:

[0008] (1) Minimize the expected mean square error of quantization, obtain the quantization strategy under different quantization bit numbers, and establish a lookup table of quantization level with respect to quantization bit numbers;

[0009] (2) Initialize the weight parameters and quantization bit number of the neural network;

[0010] (3) performing multi-bit quantization on the weight parameters of the neural network using the lookup table and quantization bit number of step (1);

[0011] (4) Use the quantized weight parameters for forward and backward propagation;

[0012] (5) Calculate the quantitative sensitivity and accumulate it to obtain the cumulative sensitivity;

[0013] (6) using the accumulated sensitivity to adjust the number of quantization bits of the weight parameter, and repeating steps (3) to (6) until the quantization training is completed;

[0014] (7) Deploy the multi-bit binary network after completing quantization training.

[0015] Furthermore, in the step (1), a brute force search algorithm is used to solve the quantization strategy, and the specific steps are: under the assumption of Gaussian distribution and Laplace distribution, under different quantization bits, possible solutions of the proportional factor are traversed, thereby calculating the quantization level and quantization interval, using the quantization level, quantization interval and the probability density function of the assumed distribution to integrate to obtain the quantization expected mean square error, find the quantization level corresponding to the minimum quantization expected mean square error, and finally establish a lookup table for different distributions and different quantization bit numbers.

[0016] Furthermore, in step (2), all quantization bit numbers are uniformly initialized to a constant value.

[0017] Furthermore, in the step (3), according to the number of quantization bits of the weight parameter, the quantization level corresponding to the number of quantization bits is found by searching the lookup table, and then the weight parameter is standardized, and then quantization projection is performed according to the quantization level, and finally de-standardization is performed.

[0018] Furthermore, in step (4), the quantized weight parameters are used for forward propagation, and the gradient of the weight parameters is obtained through back propagation, and the gradient is used to update the original weight parameters.

[0019] Furthermore, in step (5), the original weight parameter is subtracted from the quantized weight parameter, and then a dot product operation is performed with the gradient of the weight parameter. Finally, the absolute value is taken and divided by the number of parameters to represent the quantization sensitivity of the weight parameter to the loss function.

[0020] Furthermore, in step (6), the cumulative sensitivities are first sorted in ascending order, and then the weight parameters with the smallest cumulative sensitivity are found according to a certain percentage r. If the number of quantization bits of this part of the weight parameters is greater than 0, it is reduced by 1. When the number of quantization bits of the weight parameters drops to 0, this part of the weight parameters is permanently removed from the neural network.

[0021] Furthermore, in the step (7), according to the one-to-one correspondence between the quantization level and the scaling factor and the binary weight, the quantization weight parameter is decomposed into the binary weight and the scaling factor for deployment of the multi-bit binary network.

[0022] The present invention discloses a neural network quantization device with adaptive bit width, comprising: a multi-bit quantization module, which establishes a lookup table of quantization levels with respect to quantization bit numbers based on Gaussian distribution and Laplace distribution assumptions, and performs multi-bit quantization on weight parameters during quantization training; and a quantization bit number adjustment module, which is used to obtain and accumulate the sensitivity of the quantization weight parameters with respect to a loss function, and adjust the quantization bit number of the weight parameters according to the accumulated sensitivity during quantization training.

[0023] The method of the present invention decomposes the weight parameters of the neural network into multi-bit binary weights, and the original 32-bit floating-point weight parameters are replaced by M (M < < 32)-bit binary numbers, and the storage space occupied is reduced to the original The storage and memory usage of neural networks can be greatly reduced. In the process of calculating with activation parameters, floating-point operations can be replaced by bit operations, which greatly reduces the consumption of computing resources, and is therefore more conducive to deployment on portable devices and embedded devices. By pre-establishing a lookup table, optimization and solving in each quantization training iteration is avoided, which greatly reduces the computational complexity of quantization training, thereby significantly reducing the time of quantization training, making it possible to conveniently deploy it on embedded devices, mobile devices or other terminals. In summary, compared with neural networks based on floating-point data and floating-point operations, the present invention can greatly reduce the time of inference, memory consumption and power consumption; compared with previous multi-bit neural network quantization methods, the present invention can effectively reduce the quantization training time. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] Figure 1 is a flow chart of a method in an embodiment of the present invention;

[0025] Figure 2 It is a schematic diagram of the framework of the device of the present invention;

[0026] Figure 3 This is a relationship diagram of the scale factor, binary weight and quantization level taking 3 bits as an example in the present invention. DETAILED DESCRIPTION

[0027] The present invention will be described in detail below with reference to the accompanying drawings and embodiments.

[0028] Example 1

[0029] This embodiment provides a neural network quantization method with adaptive bit width, and the specific steps are as follows: Figure 1In this embodiment, the weight parameters of the neural network are divided into groups as basic units. Generally, a layer or a channel is taken as a group. The layer refers to the weight parameters represented by a whole layer of convolutional network or fully connected network. The shape of this tensor is generally C o ×C i ×K×K (convolutional layer) or F o ×F i (fully connected layer); the channel refers to C o or F o The weight data of the dimension, that is, C i ×K×K (convolutional layer) or F i The data of the (fully connected layer) dimension is considered as a whole. The weight parameters in the same group are quantized to the same number of quantization bits, and the weight parameters in different groups may be quantized to different numbers of quantization bits.

[0030] In this embodiment, any set of quantized parameters is modeled as a standard Laplace distribution or a standard Gaussian distribution. A standard Gaussian distribution refers to a distribution whose probability density function satisfies The standard Laplace distribution is one whose probability density function satisfies Where e is the natural exponent. Floating-point data will be quantized into multi-bit binary data, so the quantized parameters will obey the multi-bit binary constraint. The multi-bit binary constraint means that for any quantized parameter X, it can be expressed as the cumulative multiplication of multiple binary weights and scale factors: Where M represents the number of bits used, α j represents the scale factor, B j represents binary weight, Represents the quantized parameters of X.

[0031] Scaling factor α (a = [α1, α2, ..., α M ]) can be used to determine a set of 2M quantization levels:

[0032]

[0033] Use q i (a) represents the i-th quantization level in Q(α) arranged from small to large. i (a), s i+1 (a)] is quantized to q i (a), in order to minimize the quantization error, X is quantized to the nearest quantization level, so the quantization margin is defined as:

[0034]

[0035] Step 1: By minimizing the expected mean square error of quantization, the quantization strategy (i.e., quantization level) under different quantization bit numbers is obtained at one time, and a lookup table is established. Minimizing the expected mean square error of quantization can be expressed as the following mathematical formula:

[0036]

[0037] In the case of standard Gaussian distribution and standard Laplace distribution, the probability density function and Substituting into formula (3), we can get the following expression:

[0038]

[0039] Where Ψ(·) is the original function of the integrand in formula (3). For the standard Gaussian distribution,

[0040]

[0041] Where erf(·) represents the Gaussian Error Function. For the standard Laplace distribution,

[0042]

[0043] Since q in formula (4) i (a) and s i (a) Since a is discontinuous, a closed-form solution cannot be directly obtained. Therefore, a brute force search algorithm is used to solve the quantization strategy. The quantization strategy refers to a and its corresponding set of quantization levels under different quantization bit numbers. The main operation of the brute force search algorithm is: for different quantization bit numbers M (from 1 to 7), traverse the possible values ​​of a, and bring them into formula (4) for calculation, find the quantization strategy corresponding to the minimum quantization mean square expected error, and finally establish a lookup table of quantization level Q(a) with respect to the quantization bit number M. In this embodiment, any a j The search space of (j=1, 2, ..., M) is from 0.1 to 10, with a resolution of 0.01. That is, from 0.1 to 10, a is taken every 0.01. j The sampled a is first used in formula (1) to calculate the quantization level Q(a) and obtain q i (a), and then calculate s by formula (2) i(a), and finally calculate the minimization target by formula (4). Thus, the search space of a is traversed until the quantization level Q(a) corresponding to the minimum optimization target is found. In this embodiment, the lookup table established by minimizing the target function under different quantization bit numbers based on the standard Gaussian distribution is defined as a Gaussian lookup table, and the lookup table established by minimizing the target function under different bit numbers based on the standard Laplace distribution is defined as a Laplace lookup table.

[0044] Step 2, initialize the weight parameters and quantization bits of the neural network. The weight parameters of the neural network can be randomly initialized or initialized using a model that has been trained under floating-point precision. In this embodiment, the weight parameters of any scale in the neural network are quantized as a whole based on the group as the basic unit (that is, the weight parameters in the same group are quantized to the same number of quantization bits, and the weight parameters in different groups may be quantized to different numbers of quantization bits). Before quantization training, the quantization bit numbers of all layers (or channels) are uniformly initialized to a fixed value, which is generally an integer between 4 and 8.

[0045] Step 3: Multi-bit quantization of the weight parameters of the neural network. The training data is first input into the neural network. During the forward propagation process, if the weight parameters are modeled as standard Gaussian distribution, the mean and standard deviation of each group of weight parameters will be calculated based on the aforementioned groups. The specific calculation method is:

[0046]

[0047] In the formula, w k represents the kth value of the group of weight parameters W, and N represents the number of weight parameters in the group. If the weight parameters are modeled as standard Laplace distribution, the mean and scale parameters of each group of weight parameters will be calculated based on the aforementioned group. The specific calculation method is:

[0048]

[0049] In the formula, w kRepresents the kth value of the group of weight parameters W, and |·| represents the absolute value. According to the number of quantization bits M of the group of weight parameters, by looking up the Gaussian lookup table, find the quantization level Q(a) corresponding to the number of quantization bits, and then standardize the weight parameters, and then perform quantization projection according to the quantization level, and finally destandardize. Standardization here means that in the case of Gaussian distribution, the weight parameters of each group are subtracted from their mean and then divided by their standard deviation; in the case of Laplace distribution, the weight parameters of each group are subtracted from their mean and then divided by their scale parameter. Destandardization here means that in the case of Gaussian distribution, the weight parameters of each group are multiplied by the standard deviation and then added to the mean; in the case of Laplace distribution, the weight parameters of the basic unit are multiplied by the scale parameter and then added to the mean. In the case of Gaussian distribution, the process can be expressed by the following mathematical formula,

[0050]

[0051] In the formula, Π Q(a) (·) means projecting the parameter in the brackets to the nearest value q in the quantization level Q(a) i (a), the formula is defined as a multi-bit Gaussian quantization formula. In the case of Laplace distribution, the process can be expressed by the following mathematical formula:

[0052]

[0053] In the formula, Π Q(a) (·) means projecting the data in brackets to the nearest value q in the quantization level Q(a) i (a), the formula is defined as the multibit Laplace quantization formula.

[0054] Step 4: Perform forward and backward propagation. Use quantized weight parameters Perform forward propagation and obtain through back propagation The gradient g is used The gradient g of W is updated.

[0055] Step 5: Calculate the quantization sensitivity and accumulate it. Taking the aforementioned group as a unit, add the weight parameter W and the quantization weight parameter Subtract, then dot product operation with gradient g, and finally take the absolute value and divide it by the number of parameters N to represent the sensitivity of this group of parameters to the loss function. The dot product operation refers to multiplying the two parts involved in the operation element by element and then taking the sum. The above process can be expressed by the following mathematical formula:

[0056]

[0057] in Describes the dot product operation, g represents the quantization weight parameter The gradient of Δl′ is used to characterize the influence of the group of weight parameters on the loss function before and after quantization (i.e., sensitivity). As the quantization training is iterated, the sensitivity is accumulated with the number of training iterations to eliminate the influence of unstable input data. In this embodiment, the number of accumulations is the number of iterations of a complete traversal of the training data set.

[0058] Step 6, adjust the number of quantization bits of each group of weight parameters. First, sort the cumulative sensitivity of each group of quantization weight parameters in ascending order, and then find the weight parameter with the smallest cumulative sensitivity (in groups) according to a certain percentage r. If the number of quantization bits of this part of weight parameters is greater than 0, subtract 1 from it, and then clear the cumulative sensitivity for the next adjustment. In this embodiment, the percentage is fixed at 12%. In particular, when the number of quantization bits of a group of weight parameters drops to 0, this part of the parameters will be permanently removed from the neural network, thereby producing a more streamlined network structure. The adjustment method ends when the target average number of bits is reached or the quantization training ends. Step 7, decompose the quantization weight parameters into binary weights and proportional factors for deployment in embedded devices and portable devices. After the quantization training is completed, each group of quantization weight parameters and its corresponding number of quantization bits and quantization level will be fixed. The relationship between the quantization level, proportional factor and binary weight is shown in formula (1), Figure 3 The relationship between the three in the case of 3 bits is given. It can be seen that when the scale factor is determined, the binary weight corresponds to the quantization level one by one. The quantization weight parameter is the projection of the weight parameter on the quantization level, so the corresponding binary weight can be obtained by reverse search through the corresponding relationship. When all weight parameters are decomposed into binary weights and scale factors, most operations in the neural network can be replaced by bit operations, so the reasoning process is accelerated.

[0059] Example 2

[0060] This embodiment provides a neural network quantization device with adaptive bit width. Figure 2 As shown, it includes a multi-bit quantization module and a quantization bit number adjustment module.

[0061] The multi-bit quantization module, under the multi-bit quantization constraint, optimizes the expected mean square error of quantization by using a brute force search algorithm under the standard Gaussian distribution or the standard Laplace distribution, and establishes a Gaussian lookup table and a Laplace lookup table for the quantization strategy (i.e., quantization level) with respect to different quantization bit numbers. In the case of Gaussian distribution, a Gaussian lookup table and a multi-bit Gaussian quantization formula are used to perform multi-bit quantization on each group of weight parameters during the quantization training process. The specific quantization process is to first search the Gaussian lookup table based on the number of quantization bits of the group of weight parameters to find the corresponding quantization level, then subtract the mean value of each group of weight parameters, divide it by its standard deviation, project it to the quantization level closest to it, multiply it by the standard deviation, and finally add the mean value, as shown in formula (9); In the case of Laplace distribution, a Laplace lookup table and a multi-bit Laplace quantization formula are used to perform multi-bit quantization on each group of weight parameters. The specific quantization process is to first search the Laplace lookup table based on the number of quantization bits of the group of weight parameters to find the corresponding quantization level, then subtract the mean value of each group of weight parameters, divide it by the scale parameter, project it to the quantization level closest to it, then multiply it by the scale parameter, and finally add the mean value, as shown in formula (10).

[0062] The quantization bit number adjustment module, after each iteration of back propagation, performs the operation of formula (11) on the weight parameter, the quantization weight parameter and the corresponding gradient to obtain the sensitivity of the quantization weight parameter to the loss function, and the sensitivity is accumulated with the number of iterations or the accumulated sensitivity. After a certain number of iterations, the accumulated sensitivity is sorted in ascending order, and then the quantization bit number of each group of weight parameters with the highest sorting is adjusted according to a certain percentage. The adjustment means that if the quantization bit number of the selected weight parameter is greater than 0, it is reduced by 1. The adjusted quantization bit number will be used in the subsequent quantization training or deployment stage.

Claims

1. A method for quantizing a neural network with adaptive bit width, wherein the neural network is a neural network suitable for target recognition, image restoration or semantic segmentation, characterized in that: The following steps are involved: (1) Minimize the expected mean square error of quantization, obtain the quantization strategy under different quantization bit numbers, and establish a lookup table of quantization level with respect to the number of quantization bits; wherein, the quantization strategy is solved by using a brute force search algorithm, and the specific steps are: under the assumption of Gaussian distribution and Laplace distribution, traverse the possible solutions of the proportional factor under different quantization bits, thereby calculating the quantization level and quantization interval, using the quantization level, quantization interval and the probability density function of the assumed distribution to integrate to obtain the expected mean square error of quantization, find the quantization level corresponding to the minimum expected mean square error of quantization, and finally establish a lookup table under different distributions and different quantization bit numbers; (2) Initialize the weight parameters and quantization bit number of the neural network; (3) performing multi-bit quantization on the weight parameters of the neural network using the lookup table and quantization bit number of step (1); (4) Use the quantized weight parameters for forward and backward propagation; (5) Calculate the quantitative sensitivity and accumulate it to obtain the cumulative sensitivity; (6) using the accumulated sensitivity to adjust the number of quantization bits of the weight parameter, and repeating steps (3) to (6) until the quantization training is completed; (7) Deploy the multi-bit binary network after completing quantization training.

2. The method for adaptive bit width neural network quantization according to claim 1, characterized in that: In the step (2), all quantization bit numbers are uniformly initialized to a constant value.

3. The method for adaptive bit width neural network quantization according to claim 1, characterized in that: In the step (3), according to the quantization bit number of the weight parameter, the quantization level corresponding to the quantization bit number is found by searching the lookup table, and then the weight parameter is standardized, and then quantization projection is performed according to the quantization level, and finally de-standardization is performed.

4. The method for adaptive bit width neural network quantization according to claim 1, characterized in that: In step (4), the quantized weight parameters are used for forward propagation, and the gradient of the weight parameters is obtained through back propagation, and the gradient is used to update the original weight parameters.

5. The method for adaptive bit width neural network quantization according to claim 1, characterized in that: In the step (5), the original weight parameter is subtracted from the quantized weight parameter, and then a dot product operation is performed with the gradient of the weight parameter. Finally, the absolute value is taken and divided by the number of parameters to represent the quantization sensitivity of the weight parameter with respect to the loss function.

6. The method for adaptive bit width neural network quantization according to claim 1, characterized in that: In the step (6), the cumulative sensitivities are first sorted in ascending order, and then the weight parameters with the smallest cumulative sensitivity are found according to a certain percentage r. If the number of quantization bits of this part of the weight parameters is greater than 0, it is reduced by 1. When the number of quantization bits of the weight parameters drops to 0, this part of the weight parameters is permanently removed from the neural network.

7. The method for adaptive bit width neural network quantization according to claim 1, characterized in that: In the step (7), according to the one-to-one correspondence between the quantization level and the proportional factor and the binary weight, the quantization weight parameter is decomposed into the binary weight and the proportional factor, which are used to deploy the multi-bit binary network.

8. A neural network quantization device with adaptive bit width, wherein the neural network is a neural network suitable for target recognition, image restoration or semantic segmentation, characterized in that: include: The multi-bit quantization module establishes a lookup table of quantization levels with respect to the number of quantization bits based on the Gaussian distribution and Laplace distribution assumptions, and performs multi-bit quantization on weight parameters during the quantization training process; wherein, the quantization strategy is solved using a brute force search algorithm, and the specific steps are: under the assumptions of Gaussian distribution and Laplace distribution, under different quantization bits, traverse the possible solutions of the proportional factor, thereby calculating the quantization level and quantization interval, using the quantization level, quantization interval and the probability density function of the assumed distribution to integrate to obtain the quantization expected mean square error, find the quantization level corresponding to the minimum quantization expected mean square error, and finally establish a lookup table under different distributions and different quantization bit numbers; The quantization bit number adjustment module is used to obtain and accumulate the sensitivity of the quantization weight parameter to the loss function, and adjust the quantization bit number of the weight parameter according to the accumulated sensitivity during the quantization training process.