A neural network quantization processing method, device, equipment and readable storage medium
By converting the quantization coefficient into a power of 2 form and merging the offset parameters and compensation terms, the convolution, addition and shift operations in the neural network quantization process are simplified, and the problem of high computational resource consumption in the neural network quantization process is solved. , improving the reliability of artificial intelligence equipment.
Patent Information
- Application Number
- CN202210467176.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-29
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2042-04-29
AI Technical Summary
In the neural network quantization process, the multiplication, addition, and shift operations on the INT32 convolution output during the inverse quantization and further quantization processes are too complex, consume a lot of computing and storage resources, and pose challenges to the artificial intelligence chip architecture design.
By converting the quantized coefficient into the nearest power form less than or equal to 2, only right shift processing is performed, integer multiplication calculation is omitted, and the quantized offset parameter and 2n- are added in the inverse quantization and further quantization process. The combined value of the compensation term of 1 is simplified into three steps: convolution, addition and shift.
It reduces the complexity of quantification processing, saves computing and storage resources, is suitable for artificial intelligence devices with limited computing power and resources, and improves the reliability of device deployment.
Smart Images

Figure CN114781618B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence data processing technology, and in particular to a neural network quantization processing method, device, equipment and readable storage medium. Background Art
[0002] In order to adapt to the development of artificial intelligence (AI) technology with large computational volume and high computational density, it is necessary to improve the poor-performing AI equipment and explore more efficient AI algorithms. Neural network quantization is an AI processing solution that simplifies network calculations, reduces computational volume and computational density, and accelerates neural network calculations without affecting the accuracy and effectiveness of AI algorithms.
[0003] Neural network quantization is to convert high-bit data that occupies more computing resources into low-bit data that occupies less computing resources. Currently, the most commonly used quantization is to convert FP32 to INT8, that is, to convert 32-bit floating-point data into 8-bit integer data, and use integers in the range of INT8 (-128 to 127) to represent FP32 data (generally between -1.0 and 1.0) and perform convolution calculations. Neural network quantization includes two quantization tasks: weight quantization and eigenvalue quantization. Weight quantization is to quantize the weight parameters of FP32 to the range of INT8 according to the maximum value of the weight parameters. Eigenvalue quantization requires dynamic acquisition of feature quantization coefficients based on the results of the data set. After the neural network quantization is completed, the quantization coefficients required for quantization calculations are obtained, including weight quantization coefficients and feature quantization coefficients. In the process of neural network calculations, the operator that needs to be quantized will perform INT8 or INT16 (16-bit integer data) calculations, and then the INT32 result will be obtained. At this time, the result needs to be dequantized to FP32, and finally quantized to INT8, and then the next layer of convolution calculations is performed.
[0004] In the above-mentioned neural network quantization process, during the dequantization and further quantization process, the convolution output of INT32 needs to be multiplied by a larger value, and then the compensation amount required for the shift is added, and then right-shifted. The whole process requires multiple different calculation forms: multiplication, addition and shift. For artificial intelligence chips with limited computing power and resources, the whole process is too complicated and consumes more computing and storage cycles and resources. At the same time, it poses great challenges to the architecture and design of artificial intelligence chips. Summary of the invention
[0005] The purpose of this application is to provide a neural network quantization processing method, device, equipment and readable storage medium, which are used to reduce the complexity of quantization processing, thereby reducing the requirements for computing power and resources, saving system resources and improving the reliability of artificial intelligence applications in device deployment.
[0006] In order to solve the above technical problems, the present application provides a neural network quantization processing method, comprising:
[0007] After performing a convolution operation on the model weights of the current layer convolution operation and the input feature values of the current layer convolution operation, an initial convolution result is obtained;
[0008] Adding a preset bias value to the initial convolution result to obtain a second convolution result;
[0009] Shifting the second convolution result right by n bits to obtain a third convolution result;
[0010] Adapting to the input requirements of the next layer of convolution operation, converting the third convolution result into the final output result of the current layer of convolution operation;
[0011] The preset bias value is obtained by combining the quantized bias parameter and the compensation item in the current layer convolution operation, and the data type of the preset bias value is the same as the data type of the initial convolution result;
[0012] The bias parameter is a bias value obtained by fusing the convolution operator of the current layer convolution operation and the normalization operator of the current layer convolution operation;
[0013] The compensation item is 2 n-1 , n=a+bc, a is the power value of the power of 2 to which the quantization coefficient of the model weight is closest and less than or equal to, b is the power value of the power of 2 to which the quantization coefficient of the input eigenvalue is closest and less than or equal to, c is the power value of the power of 2 to which the quantization coefficient of the output eigenvalue of the convolution operation of the current layer is closest and less than or equal to, and n, a, b, and c are all positive integers;
[0014] The model weights, the input eigenvalues, and the output eigenvalues are all quantized low-bit data.
[0015] Optionally, the current layer convolution operation includes a convolution operator with a bias term and a convolution operator without a bias term;
[0016] For the convolution operator with the bias term, the bias parameter is the sum of the bias term of the convolution operator with the bias term and the second weight of the normalization operator;
[0017] For the convolution operator without a bias term, the bias parameter is a second weight of the normalization operator;
[0018] The second weight of the normalization operator is another weight of the normalization operator except the coefficient of the input quantity.
[0019] Optionally, the current layer convolution operation includes a convolution operator with a bias term and a convolution operator without a bias term;
[0020] The bias parameters include a first bias parameter and a second bias parameter;
[0021] The first bias parameter is a bias term of the convolution operator having a bias term;
[0022] The second bias parameter is a second weight of the normalization operator;
[0023] The preset bias value is obtained by combining the quantized first bias parameter, the quantized second bias parameter and the compensation item;
[0024] The second weight of the normalization operator is another weight of the normalization operator except the coefficient of the input quantity.
[0025] Optionally, adapting to the input requirement of the next layer of convolution operation and converting the third convolution result into the final output result of the current layer of convolution operation specifically includes:
[0026] Performing a threshold operation on the third convolution result to obtain a threshold operation result;
[0027] Forcibly converting the cross-threshold operation result into a low-bit data operation to obtain a low-bit data type result;
[0028] The low-bit data type result is activated to obtain the final output result.
[0029] Optionally, performing a threshold operation on the third convolution result to obtain a threshold operation result specifically includes:
[0030] If the current layer convolution operation does not have an activation layer, converting the third convolution result to the over-threshold operation result in the range of (-127, 127);
[0031] If the current layer convolution operation has an activation layer, the third convolution result is converted to the threshold operation result in the range of (0, 255).
[0032] Optionally, the low-bit data type result is activated to obtain the final output result of the current layer convolution operation, which is specifically obtained by conversion according to the following formula:
[0033]
[0034] Among them, y f is the final output result, ReLU(x) is the activation function for x, and x is the low-bit data type result.
[0035] Optionally, the quantized low-bit data is specifically 8-bit integer data.
[0036] In order to solve the above technical problems, the present application also provides a neural network quantization processing device, comprising:
[0037] A convolution operation unit, used for performing a convolution operation on a model weight of a current layer convolution operation and an input feature value of the current layer convolution operation to obtain an initial convolution result;
[0038] A preprocessing unit, configured to add a preset bias value to the initial convolution result to obtain a second convolution result;
[0039] a shift processing unit, used for performing right shifting of the second convolution result by n bits to obtain a third convolution result;
[0040] An output conversion processing unit, used to adapt to the input requirements of the next layer of convolution operation, and convert the third convolution result into the final output result of the convolution operation of the current layer;
[0041] The preset bias value is obtained by combining the quantized bias parameter and the compensation item in the current layer convolution operation, and the data type of the preset bias value is the same as the data type of the initial convolution result;
[0042] The bias parameter is a bias value obtained by fusing the convolution operator of the current layer convolution operation and the normalization operator of the current layer convolution operation;
[0043] The compensation item is 2 n-1 , n=a+bc, a is the power value of the power of 2 to which the quantization coefficient of the model weight is closest and less than or equal to, b is the power value of the power of 2 to which the quantization coefficient of the input eigenvalue is closest and less than or equal to, c is the power value of the power of 2 to which the quantization coefficient of the output eigenvalue of the convolution operation of the current layer is closest and less than or equal to, and n, a, b, and c are all positive integers;
[0044] The model weights, the input eigenvalues, and the output eigenvalues are all quantized low-bit data.
[0045] In order to solve the above technical problems, the present application also provides a neural network quantization processing device, including:
[0046] Memory for storing computer programs;
[0047] A processor is used to execute the computer program, and when the computer program is executed by the processor, the steps of the neural network quantization processing method as described in any one of the above items are implemented.
[0048] In order to solve the above technical problems, the present application also provides a readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the neural network quantization processing method as described in any one of the above items are implemented.
[0049] The neural network quantization processing method provided by the present application, in the neural network quantization processing, by converting the quantization coefficients into a form less than and equal to the nearest power of 2, so that in the subsequent quantization and dequantization process, only right shift processing is required, and integer multiplication calculation is not required, thereby omitting a large number of calculation steps; in the process of dequantizing and further quantizing the initial convolution result of the current layer convolution operation, the initial convolution result is first added with the bias parameter quantized by the current layer and 2 n-1 The value of the compensation term combined, n is obtained by the quantization coefficient of the current layer; and then the quantization reasoning operation is performed, so that the quantization reasoning of the neural network is simplified to three steps of convolution, addition and shift, which can complete the calculations required for convolution, addition, multiplication, addition and shift in the prior art. It further simplifies the steps of quantization reasoning, saves a lot of computing resources and storage resources, and is suitable for deploying artificial intelligence applications on artificial intelligence devices with limited computing power and resources.
[0050] The present application also provides a neural network quantization processing device, equipment and readable storage medium, which have the above-mentioned beneficial effects and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] In order to more clearly illustrate the embodiments of the present application or the technical solutions of the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0052] Figure 1 A flowchart of a neural network quantization processing method provided in an embodiment of the present application;
[0053] Figure 2 A schematic diagram of weight quantization provided in an embodiment of the present application;
[0054] Figure 3 A schematic diagram of a quantization process provided in an embodiment of the present application;
[0055] Figure 4 A flowchart of a specific implementation of step S104 provided in an embodiment of the present application;
[0056] Figure 5 A flowchart of another neural network quantization processing method provided in an embodiment of the present application;
[0057] Figure 6 A schematic diagram of the structure of a neural network quantization processing device provided in an embodiment of the present application;
[0058] Figure 7 A schematic diagram of the structure of a neural network quantization processing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0059] The core of this application is to provide a neural network quantization processing method, device, equipment and readable storage medium, which are used to reduce the complexity of quantization processing, thereby reducing the requirements for computing power and resources, saving system resources and improving the reliability of artificial intelligence applications in device deployment.
[0060] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0061] Embodiment 1
[0062] Figure 1 A flowchart of a neural network quantization processing method provided in an embodiment of the present application; Figure 2 A schematic diagram of weight quantization provided in an embodiment of the present application; Figure 3 A schematic diagram of a quantization process provided in an embodiment of the present application.
[0063] Neural network quantization is to convert high-bit data that occupies more computing resources into low-bit data that occupies less computing resources. Currently, the most commonly used quantization is to convert FP32 (32-bit floating point data) into INT8 (8-bit integer data). In this application, the quantized low-bit data can all be 8-bit integer data.
[0064] Neural network quantization includes two parts: weight quantization and eigenvalue quantization.
[0065] Weight quantization is to quantize the FP32 weight parameters to the range of INT8, that is, -128 to 127, based on the maximum value of the weight parameters. Figure 2 As shown, the weight quantization coefficient scale obtained by quantization w (weights scale factor), convert the output into INT8 type data.
[0066] Weight quantization coefficient scale w It can be obtained by the following formula:
[0067]
[0068] Where ε = 10 -5 .
[0069] Feature quantization needs to dynamically obtain the feature quantization coefficient scale based on the results of the data set af .
[0070] Specifically, we need to find the optimal threshold of FP32 feature data in each layer of the convolutional network as the boundary of quantization. Assuming that the feature threshold of a layer in the convolutional neural network is a, then the feature quantization coefficient of this layer is All elements in this layer feature must be multiplied by this coefficient and rounded to integers, and data greater than 127 must be forced to be equal to 127, and data less than -128 must be forced to be equal to -128. This ensures that all data is converted to integers between -128 and 127, and when performing convolution calculations, the converted INT8 data is used for calculations.
[0071] Commonly used feature quantization methods include maximum value quantization, KL divergence quantization, percentage 99.9% quantization, percentage 99.99% quantization, percentage 99.999% quantization, etc. Different methods correspond to different feature quantization coefficients. First, randomly select a part of the data set (about 1 / 50 of the entire data set) from the data set for inference, obtain the results of each layer of output features (FP32 data type), and save them; then calculate the corresponding feature quantization coefficient scale according to the set quantization method af .
[0072] Neural network quantization includes two types: quantization during training and quantization after training. The quantization target of this application is INT8 type data, so only the post-training quantization method is used, which can meet the requirements of calculation accuracy. The final calculation result is basically consistent with the FP32 calculation result or the loss is very small, and there is no need to participate in the pre-training process.
[0073] After the neural network quantization is completed, the quantization coefficients required for quantization calculation can be obtained, including the weight quantization coefficient scale w and feature quantization coefficient scale af Generally, in the process of neural network calculation, operators that need to be quantized, such as the conv2D (two-dimensional convolution) operator, will perform INT8 or INT16 (16-bit integer data) calculations, and then get INT32 (32-bit integer data) results. At this time, the result needs to be dequantized to FP32, and finally quantized to INT8. However, in actual implementation, dequantization and quantization are often combined. Figure 3As shown in the figure, in the current layer convolution operation, the model weight W and the eigenvalue X are both INT8 type data. After the convolution operation, the convolution result of the INT32 type is obtained. The bias B is quantized to INT32 type data and then summed with the INT32 type convolution result to obtain the INT32 type result. The dequantization is performed to obtain FP32 type data, and then quantized to obtain INT8 type data.
[0074] However, in the quantization algorithm that implements full integer reasoning, the reasoning steps are too complicated to meet the reasoning accuracy requirements. In order to ensure that full integer calculations are used in the quantization reasoning process, the reasoning steps are simple and the calculation accuracy requirements of the neural network are met, such as Figure 1 As shown, the neural network quantization processing method provided in the embodiment of the present application includes:
[0075] S101: After performing a convolution operation on the model weights of the current layer convolution operation and the input feature values of the current layer convolution operation, an initial convolution result is obtained.
[0076] S102: Add a preset bias value to the initial convolution result to obtain a second convolution result.
[0077] S103: Shift the second convolution result right by n bits to obtain a third convolution result.
[0078] S104: Adapting to the input requirements of the next layer of convolution operation, converting the third convolution result into the final output result of the current layer of convolution operation.
[0079] The preset bias value is obtained by combining the quantized bias parameter and the compensation item in the current layer convolution operation, and the data type of the preset bias value is the same as the data type of the initial convolution result.
[0080] The bias parameter is the bias value of the convolution operator of the current layer convolution operation and the normalization operator of the current layer convolution operation fused together.
[0081] The compensation item is 2 n-1 , n=a+bc, a is the power value of the power of 2 that the quantization coefficient of the model weight is closest to and less than or equal to, b is the power value of the power of 2 that the quantization coefficient of the input eigenvalue is closest to and less than or equal to, c is the power value of the power of 2 that the quantization coefficient of the output eigenvalue of the current layer convolution operation is closest to and less than or equal to, n, a, b, c are all positive integers.
[0082] Model weights, input eigenvalues, and output eigenvalues are all quantized low-bit data.
[0083] Specifically, for step S101, in the current layer convolution operation, the calculation expression of the convolution layer (Conv) is generally as follows:
[0084] y=(∑wa)+(bias); (2)
[0085] Among them, y is the convolution calculation result of the current convolution operation; w is the model weight of the current convolution operation; a is the input value of the current convolution operation, called the feature value or activation value; bias is the bias value of the current convolution operation. For neural networks, some convolution layers may have bias, while others may not.
[0086] For the neural network, after the above convolution operation, there will be a normalization layer (Batch Norm) and an activation layer (ReLU layer) to normalize and activate the convolution operation results.
[0087] The normalized expression of the normalized layer is: y′=αy+β; (3)
[0088] Among them, y′ is the normalized result, y is the convolution calculation result of this layer, and α and β are normalization parameters.
[0089] The activation function of the activation layer is:
[0090] Among them, y f is the activation value, ReLU(x) is the activation function, and x is the output of the convolution operator.
[0091] After the above activation, y f ∈[0,+∞).
[0092] According to the operator type contained in the current layer convolution operation, the convolution operator and the normalization operator are fused and referenced Figure 3 In the quantization process shown, the INT8 type model weights and the INT8 type input feature values are first convolved, and an initial convolution result of the INT32 type is obtained. Then, the initial convolution result of the INT32 type needs to be dequantized and further quantized.
[0093] For step S102, in order to dequantize the initial convolution result of the INT32 type, it is necessary to divide it by the coefficient used in the convolution operation, as shown in the following formula:
[0094]
[0095] Among them, y dq is the dequantized value, y q is the activation value of type INT32 (i.e., the value after activating the initial convolution result according to expression (4)), scale w is the quantization coefficient of the model weight w, scale a is the quantization coefficient of the input eigenvalue a.dq It is a floating point value and needs to be further quantized, that is, multiplied by the quantization coefficient scale of the output eigenvalue of the current layer convolution operation yf , the output feature value is further quantized to the range of INT8 as the input feature value a of the next layer next_q , which is shown in the following formula:
[0096]
[0097] In actual AI chip operations, since AI chips may only support INT type calculations, the above Therefore, in the embodiment of the present application, the quantization coefficient scale of the output feature value is yf , quantization coefficient scale of model weights w , quantization coefficient scale of input eigenvalue a , are converted to the nearest power of 2 (round_power_2) that is less than or equal to it.
[0098] Then the quantization coefficient scale of the model weight w Obtained by the following formula:
[0099]
[0100] Quantization coefficient scale of input eigenvalue a By using expression (8) or (9), we can get:
[0101]
[0102]
[0103] Among them, if quantization is performed before activation, 127 is taken (ie, expression (8)); if quantization occurs after activation, 255 can be taken (ie, expression (9)).
[0104] If the round_power_2 process is not performed, the quantization coefficient itself can be any value. After the present application performs the round_power_2 process on the quantization coefficient, assuming that the quantization coefficient without the round_power_2 process is 1033.3, then the power of 2 (2 n format) is 1024 or 2 10 , then the final quantization coefficient used is 2 10 .
[0105] Then after the quantization coefficient scale of the output eigenvalue yf , quantization coefficient scale of model weightsw , quantization coefficient scale of input eigenvalue a After round_power_2 processing, expression (6) can be expressed as:
[0106] INT8(a next _ q )=y q >>Shift_value; (10)
[0107] Among them, Shift_value is the number of bits to be shifted right. The above operation is the same as are equivalent.
[0108] Then suppose that after quantization, the quantization coefficient scale of the model weight w =2 a , input eigenvalue quantization coefficient scale a =2 b , the quantization coefficient scale of the output eigenvalue yf =2 c ,So
[0109] Shift_value=a+bc. (11)
[0110] Generally, in the shift operation process, in order to compensate for the error caused by the shift calculation, it is necessary to perform a shift compensation calculation on the shift calculation. Specifically, a compensation addition is performed before the right shift. For expression (10), before the shift, the output of the convolution or activation must first be added with 2 a+b-c-1 . Then the entire quantitative reasoning calculation process can be expressed as:
[0111] a next_q =ReLU((INT32(∑INT8(w q ) INT8(a q ))+INT32(bias q )+2 Shift_value-1 )>>Shift_value)=ReLU((INT32(∑INT8(w q ) INT8(a q ))+INT32(bias q +2 Shift_ v alue-1 ))>>Shift_value)=ReLU((INT32(∑INT8(w q ) INT8(a q ))+INT32(new_bias q ))>>Shift_value). (12)
[0112] From expression (12), we can see that we can first convert the quantized bias parameter bias q and Compensation 2 Shift_value-1 Combined into a new expression new_bias q , and then perform quantitative reasoning operations.
[0113] Then in step S102, according to the quantization coefficient scale of the model weight w =2 a , input eigenvalue quantization coefficient scale a =2 b , the quantization coefficient scale of the output eigenvalue yf =2 c Determine a, b, c, set n = Shift_value = a + bc, and use 2 n-1 The initial convolution result of INT32 obtained in step S101 is added with the preset bias value new_bias obtained by combining the quantized bias parameter in the current layer convolution operation and the compensation term. q , get the second convolution result of INT32, and then perform quantized inference operation, that is, the processing process that originally required convolution, addition, multiplication, addition, and shift can be realized by only three steps of convolution, addition, and shift.
[0114] In step S103, referring to expression (12), the second convolution result of INT32 is right-shifted by n bits to obtain a third convolution result.
[0115] For step S104, according to the input requirements of the next layer of convolution operation, the third convolution result is converted into the final output result of the current layer of convolution operation. For example, in expressions (12) and (10), the third convolution result is activated and forcibly converted into INT8 type data to obtain the final output result.
[0116] In the neural network quantization processing method provided in the embodiment of the present application, in the neural network quantization processing, by converting the quantization coefficients into a form less than and equal to the nearest power of 2, in the subsequent quantization and dequantization process, only right shift processing is required, and integer multiplication calculation is not required, thereby omitting a large number of calculation steps; in the process of dequantizing and further quantizing the initial convolution result of the current layer convolution operation, the initial convolution result is first added with the bias parameter quantized by the current layer and 2 n-1The value of the compensation term combined, n is obtained by the quantization coefficient of the current layer; and then the quantization reasoning operation is performed, so that the quantization reasoning of the neural network is simplified to three steps of convolution, addition and shift, which can complete the calculations required for convolution, addition, multiplication, addition and shift in the prior art. It further simplifies the steps of quantization reasoning, saves a lot of computing resources and storage resources, and is suitable for deploying artificial intelligence applications on artificial intelligence devices with limited computing power and resources.
[0117] Embodiment 2
[0118] In the above embodiment, it is mentioned that for a neural network, some convolution layers may have bias and some may not. The expression of a convolution operator with a bias is shown in (2), and a convolution operator without a bias has no bias. Before performing the convolution operation, the convolution operator and the normalization operator are fused, which is equivalent to omitting the calculation of the weights in the normalization operator and the original convolution result. The convolution operator and the normalization operator are fused, that is, the weight w of the convolution operator and the weight α of the normalization operator are fused into a new weight w′, which is used as the model weight.
[0119] Then the expression after the convolution operator without bias and the normalization operator are fused is:
[0120] y′=(∑w′a)+β; (13)
[0121] The expression after the convolution operator with bias and the normalization operator are fused is:
[0122] y′=(∑w′a)+bias+β. (14)
[0123] It can be seen that the expression after the convolution operator without bias is fused with the normalization operator is different from the expression after the convolution operator with bias is fused with the normalization operator. In the quantization processing, quantization methods can be designed for these two cases respectively.
[0124] Then, on the basis of the above-mentioned embodiments, in the neural network quantization processing method provided in the embodiments of the present application, when the current layer convolution operation includes a convolution operator with a bias term and a convolution operator without a bias term, the bias parameter includes a first bias parameter and a second bias parameter; the first bias parameter is the bias term of the convolution operator with the bias term; the second bias parameter is the second weight of the normalization operator; the preset bias value is the quantized first bias parameter, the quantized second bias parameter and the compensation term combined; wherein the second weight of the normalization operator is another weight (i.e., β in expression (3)) of the normalization operator except the coefficient of the input quantity (i.e., α in expression (3)).
[0125] In the operation of the convolution layer after the convolution operator without bias is fused with the normalization operator, w′, β, and a are quantized respectively before the convolution operation is performed, and then the dequantization and further quantization process are performed.
[0126] In the operation of the convolution layer after the convolution operator with bias is fused with the normalization operator, w′, β, a, and bias are quantized respectively before the convolution operation is performed, and then the dequantization and further quantization process are performed. For the case where the convolution operator with bias is fused with the normalization operator, it is necessary to add a step to quantize the bias of the convolution operator of the current layer convolution operation.
[0127] Embodiment 3
[0128] The second embodiment of the present application describes the quantization method for the two cases where the convolution operator of the current layer convolution operation has a bias and does not have a bias. However, this method of separate quantization may lead to inconsistent quantization algorithms between layers in the neural network, and the algorithm implementation is relatively complicated. It can be seen from expressions (13) and (14) that the expression forms of the two are basically the same, and both can be regarded as the form of convolution + bias. Therefore, expressions (13) and (14) can be further integrated into a new form:
[0129] y′=(∑w′a)+bias′; (15)
[0130] Among them, w′ is the weight obtained by fusing the weight w of the convolution operator and the weight α of the normalization operator, and bias′ is the bias after the convolution operator and the normalization operator are fused.
[0131] Then, on the basis of the above-mentioned embodiment 1, in the neural network quantization processing method provided in the embodiment of the present application, when the current layer convolution operation includes a convolution operator with a bias term and a convolution operator without a bias term, for the convolution operator with a bias term, the bias parameter bias′ is the sum of the bias term of the convolution operator with the bias term and the second weight of the normalization operator; for the convolution operator without a bias term, the bias parameter bias′ is the second weight of the normalization operator; the second weight of the normalization operator is another weight (i.e., β in expression (3)) of the normalization operator except the coefficient of the input quantity (i.e., α in expression (3)).
[0132] After the above processing, for both the convolution operator with bias and the convolution operator without bias, the same process of quantization, dequantization and further quantization after fusion with the normalization operator can be performed, without separately quantizing the bias of the convolution operator.
[0133] Specifically, in the calculation of the original floating-point data, the value range of the quantities (y′, w′, a, bias′) involved in expression (15) is generally a floating-point value of (-1, 1). The convolution part ∑w′a is quantized to INT8 convolution calculation, and the part where the convolution result and bias′ are added is quantized to INT32 integer addition, that is:
[0134] y q =ReLU(INT32(∑INT8(w q ) INT8(a q ))+INT32(bias q )); (16)
[0135] Among them, floating point type w' to INT8 w q , needs to be multiplied by the quantization coefficient scale of the current layer weight w′ w , scale w The calculation method of is shown in expression (7). q , needs to be multiplied by the quantization coefficient scale of the current layer weight a a , scale a The calculation method of is shown in expression (8) or (9). Bias' of floating point type to bias of INT8 q , then multiply it by the quantization coefficient of the bias parameter bias′ of the current layer, that is:
[0136] bias q = bias′·scale w ·scale a = bias′·scale bias (17)
[0137] Among them, bias q is the quantized bias parameter, bias′ is the bias after the convolution operator and the normalization operator are fused, and scale bias is the quantization coefficient of the bias parameter.
[0138] After activation, the activation value y of INT32 is obtained. q For the next layer of convolution operation, the input data must also be INT8 values to ensure that the convolution calculation is INT8 convolution calculation. That is, the activation value y of INT32 needs to be q Dequantization and further quantization are performed, and the specific process can refer to the description of S102 and S103 in the first embodiment of the present application.
[0139] Embodiment 4
[0140] Figure 4A flowchart of a specific implementation of step S104 provided in an embodiment of the present application.
[0141] In the first embodiment of the present application, it is mentioned that according to the input requirements of the next layer of convolution operation, the third convolution result is converted into the final output result of the current layer of convolution operation. For example, in expressions (12) and (10), the third convolution result is activated and forcibly converted into INT8 type data to obtain the final output result. On this basis, in the neural network quantization processing method provided in the embodiment of the present application, step S104: adapting to the input requirements of the next layer of convolution operation, converting the third convolution result into the final output result of the current layer of convolution operation, specifically may include:
[0142] S401: Perform a threshold operation on the third convolution result to obtain a threshold operation result.
[0143] S402: Forcibly convert the over-threshold operation result into a low-bit data operation to obtain a low-bit data type result.
[0144] S403: Activate the low-bit data type result to obtain the final output result.
[0145] Specifically, step S401: performing a threshold operation on the third convolution result to obtain a threshold operation result may specifically include:
[0146] If the current convolution operation does not have an activation layer, the third convolution result is converted to a threshold operation result in the range of (-127, 127);
[0147] If the current convolution operation has an activation layer, the third convolution result is converted to a threshold operation result in the range of (0, 255);
[0148] For step S402, if the activation value y of INT32 is required to improve the accuracy in the above calculation, q The value is forcibly converted to INT64 and then dequantized. Here, the over-threshold operation result of INT64 or the over-threshold operation result of INT32 is forcibly converted to obtain a low-bit data type result of INT8 type.
[0149] For step S403, it can be obtained by converting the activation function provided by expression (4).
[0150] Embodiment 5
[0151] Figure 5 A flowchart of another neural network quantization processing method provided in an embodiment of the present application.
[0152] Based on the above embodiments, the present application provides a preferred neural network quantization processing method. Figure 5 As shown, the neural network quantization processing method provided in the embodiment of the present application includes:
[0153] S501: In the convolution operation of the current layer, a convolution calculation is performed on the INT8 type model weights and the INT8 type input feature values to obtain a convolution output value of INT32.
[0154] The algorithm is expressed as: conv(int8 input, int8 weight)→int32 conv_output.
[0155] S502: Add the convolution output value of INT32 to the preset bias value of INT32 to obtain a second convolution result of INT32 type.
[0156] The algorithm is expressed as: add(int32 conv_output, int32 bn_add_partion)→int32 add_output.
[0157] S503: Shift the second convolution result of INT32 right by n bits to obtain a third convolution result, where n is Shift_value.
[0158] The algorithm is expressed as: right_shift(int32 add_output, int32 shift_value)=add_output>>shift_value.
[0159] S504: Perform a threshold operation on the third convolution result and convert it into a threshold operation result of (-127, 127) or a threshold operation result of (0, 255). If the current layer has no activation layer, it is converted into a threshold operation result of (-127, 127); if the current layer has an activation layer, it is converted into a threshold operation result of (0, 255).
[0160] The algorithm is expressed as: clip: fixed_output→int32 clip_output(-127,127) or (0,255).
[0161] S505: Perform forced type conversion on the result of the over-threshold operation, and convert the INT64 value into a low-bit data type result of the INT8 type.
[0162] The algorithm is expressed as: cast: int8(clip_output)→int8 clip_output.
[0163] S506: Activate the low-bit data type result to obtain the INT8 activation value as the final output result of the INT8 of the current layer (the input feature value of the INT8 of the next convolutional layer).
[0164] The algorithm is expressed as: relu(int8 cast_output)→int8 relu_output.
[0165] The above describes in detail various embodiments corresponding to the neural network quantization processing method. On this basis, the present application also discloses a neural network quantization processing device, equipment and readable storage medium corresponding to the above method.
[0166] Embodiment 6
[0167] Figure 6 A schematic diagram of the structure of a neural network quantization processing device provided in an embodiment of the present application.
[0168] like Figure 6 As shown, the neural network quantization processing device provided in the embodiment of the present application includes:
[0169] A convolution operation unit 601 is used to perform a convolution operation on the model weight of the current layer convolution operation and the input feature value of the current layer convolution operation to obtain an initial convolution result;
[0170] A preprocessing unit 602, configured to add a preset bias value to the initial convolution result to obtain a second convolution result;
[0171] A shift processing unit 603 is used to perform a right shift of n bits on the second convolution result to obtain a third convolution result;
[0172] An output conversion processing unit 604 is used to adapt to the input requirements of the next layer of convolution operation and convert the third convolution result into the final output result of the current layer of convolution operation;
[0173] The preset bias value is obtained by combining the quantized bias parameter and the compensation term in the convolution operation of the current layer, and the data type of the preset bias value is the same as the data type of the initial convolution result;
[0174] The bias parameter is the bias value of the convolution operator of the current layer convolution operation and the normalization operator of the current layer convolution operation fused together;
[0175] The compensation item is 2 n-1 , n = a + bc, a is the power value of the power of 2 that the quantization coefficient of the model weight is closest to and is less than or equal to, b is the power value of the power of 2 that the quantization coefficient of the input eigenvalue is closest to and is less than or equal to, c is the power value of the power of 2 that the quantization coefficient of the output eigenvalue of the current layer convolution operation is closest to and is less than or equal to, n, a, b, c are all positive integers;
[0176] Model weights, input eigenvalues, and output eigenvalues are all quantized low-bit data.
[0177] Since the embodiments of the apparatus part correspond to the embodiments of the method part, please refer to the description of the embodiments of the method part for the embodiments of the apparatus part, which will not be repeated here.
[0178] Embodiment 7
[0179] Figure 7 A schematic diagram of the structure of a neural network quantization processing device provided in an embodiment of the present application.
[0180] like Figure 7 As shown, the neural network quantization processing device provided in the embodiment of the present application includes:
[0181] A memory 710 for storing a computer program 711;
[0182] The processor 720 is used to execute the computer program 711. When the computer program 711 is executed by the processor 720, the steps of the neural network quantization processing method described in any one of the above embodiments are implemented.
[0183] Among them, the processor 720 may include one or more processing cores, such as a 3-core processor, an 8-core processor, etc. The processor 720 may be implemented in at least one hardware form of a digital signal processing DSP (Digital Signal Processing), a field programmable gate array FPGA (Field-Programmable Gate Array), and a programmable logic array PLA (Programmable Logic Array). The processor 720 may also include a main processor and a coprocessor. The main processor is a processor for processing data in an awake state, also known as a central processing unit CPU (Central Processing Unit); the coprocessor is a low-power processor for processing data in a standby state. In some embodiments, the processor 720 may be integrated with a graphics processor GPU (Graphics Processing Unit), which is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 720 may also include an artificial intelligence AI (Artificial Intelligence) processor, which is used to process computing operations related to machine learning.
[0184] The memory 710 may include one or more readable storage media, which may be non-transitory. The memory 710 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 710 is at least used to store the following computer program 711, wherein the computer program 711, after being loaded and executed by the processor 720, can implement the relevant steps in the neural network quantization processing method disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 710 may also include an operating system 712 and data 713, etc., and the storage method may be short-term storage or permanent storage. Among them, the operating system 712 may be Windows. Data 713 may include, but is not limited to, the data involved in the above method.
[0185] In some embodiments, the neural network quantization processing device may also include a display screen 730 , a power supply 740 , a communication interface 750 , an input and output interface 760 , a sensor 770 , and a communication bus 780 .
[0186] Those skilled in the art will understand that Figure 7 The structure shown in the figure does not constitute a limitation on the neural network quantization processing device, and may include more or fewer components than those shown in the figure.
[0187] The neural network quantization processing device provided in the embodiment of the present application includes a memory and a processor. When the processor executes the program stored in the memory, it can implement the neural network quantization processing method as described above, and the effect is the same as above.
[0188] Embodiment 8
[0189] It should be noted that the above-described device and equipment embodiments are merely schematic. For example, the division of modules is merely a logical function division. There may be other division methods in actual implementation, such as multiple modules or components may be combined or integrated into another system, or some features may be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed may be through some interfaces, indirect coupling or communication connection of devices or modules, which may be electrical, mechanical or other forms. The modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network modules. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment.
[0190] In addition, each functional module in each embodiment of the present application can be integrated into a processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The above integrated modules can be implemented in the form of hardware or software functional modules.
[0191] If the integrated module is implemented in the form of a software function module and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium to execute all or part of the steps of the method described in each embodiment of the present application.
[0192] To this end, an embodiment of the present application also provides a readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the neural network quantization processing method are implemented.
[0193] The readable storage medium may include: a USB flash drive, a mobile hard disk, a read-only memory ROM (Read-Only Memory), a random access memory RAM (Random Access Memory), a magnetic disk or an optical disk, and other media that can store program codes.
[0194] The computer program contained in the readable storage medium provided in this embodiment can implement the steps of the neural network quantization processing method described above when executed by the processor, and the effect is the same as above.
[0195] The above is a detailed introduction to a neural network quantization processing method, device, equipment and readable storage medium provided by the present application. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the embodiments can refer to each other. For the devices, equipment and readable storage media disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple, and the relevant parts can refer to the method part description. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present application, several improvements and modifications can be made to the present application, and these improvements and modifications also fall within the scope of protection of the claims of the present application.
[0196] It should also be noted that, in this specification, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "comprise", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the statement "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.
Claims
1. A neural network quantization processing method, It is characterized in that include: After performing a convolution operation on the model weights of the current layer convolution operation and the input feature values of the current layer convolution operation, an initial convolution result is obtained; Adding a preset bias value to the initial convolution result to obtain a second convolution result; Shifting the second convolution result right by n bits to obtain a third convolution result; Adapting to the input requirements of the next layer of convolution operation, converting the third convolution result into the final output result of the current layer of convolution operation; The preset bias value is obtained by combining the quantized bias parameter and the compensation item in the current layer convolution operation, and the data type of the preset bias value is the same as the data type of the initial convolution result; The bias parameter is a bias value obtained by fusing the convolution operator of the current layer convolution operation and the normalization operator of the current layer convolution operation; The compensation item is 2 n-1 , n=a+bc, a is the power value of the power of 2 to which the quantization coefficient of the model weight is closest and less than or equal to, b is the power value of the power of 2 to which the quantization coefficient of the input eigenvalue is closest and less than or equal to, c is the power value of the power of 2 to which the quantization coefficient of the output eigenvalue of the convolution operation of the current layer is closest and less than or equal to, and n, a, b, and c are all positive integers; Neural network quantization converts high-bit data that occupies more computing resources into low-bit data that occupies less computing resources; the model weights, the input eigenvalues, and the output eigenvalues are all quantized low-bit data, which is suitable for deploying artificial intelligence applications on artificial intelligence devices with limited computing power and resources; Adapting to the input requirements of the next layer of convolution operation, converting the third convolution result into the final output result of the current layer of convolution operation specifically includes: Performing a threshold operation on the third convolution result to obtain a threshold operation result; Forcibly converting the cross-threshold operation result into a low-bit data operation to obtain a low-bit data type result; Activate the low-bit data type result to obtain the final output result; The low-bit data type result is activated to obtain the final output result of the current layer convolution operation, which is obtained by conversion according to the following formula: ; in, is the final output result, For x The activation function, x is the low bit data type result.
2. The neural network quantization processing method according to claim 1, It is characterized in that The current layer convolution operation includes a convolution operator with a bias term and a convolution operator without a bias term; For the convolution operator with the bias term, the bias parameter is the sum of the bias term of the convolution operator with the bias term and the second weight of the normalization operator; For the convolution operator without a bias term, the bias parameter is a second weight of the normalization operator; The second weight of the normalization operator is another weight of the normalization operator except the coefficient of the input quantity.
3. The neural network quantization processing method according to claim 1, It is characterized in that The current layer convolution operation includes a convolution operator with a bias term and a convolution operator without a bias term; The bias parameters include a first bias parameter and a second bias parameter; The first bias parameter is a bias term of the convolution operator having a bias term; The second bias parameter is a second weight of the normalization operator; The preset bias value is obtained by combining the quantized first bias parameter, the quantized second bias parameter and the compensation item; The second weight of the normalization operator is another weight of the normalization operator except the coefficient of the input quantity.
4. The neural network quantization processing method according to claim 1, It is characterized in that The performing a threshold operation on the third convolution result to obtain a threshold operation result specifically includes: If the current layer convolution operation does not have an activation layer, converting the third convolution result to the over-threshold operation result in the range of (-127, 127); If the current layer convolution operation has an activation layer, the third convolution result is converted to the threshold operation result in the range of (0, 255).
5. The neural network quantization processing method according to claim 1, It is characterized in that The quantized low-bit data is specifically 8-bit integer data.
6. A neural network quantization processing device, It is characterized in that include: A convolution operation unit, used for performing a convolution operation on a model weight of a current layer convolution operation and an input feature value of the current layer convolution operation to obtain an initial convolution result; A preprocessing unit, configured to add a preset bias value to the initial convolution result to obtain a second convolution result; a shift processing unit, used for performing right shifting of the second convolution result by n bits to obtain a third convolution result; An output conversion processing unit, used to adapt to the input requirements of the next layer of convolution operation, and convert the third convolution result into the final output result of the convolution operation of the current layer; The preset bias value is obtained by combining the quantized bias parameter and the compensation item in the current layer convolution operation, and the data type of the preset bias value is the same as the data type of the initial convolution result; The bias parameter is a bias value obtained by fusing the convolution operator of the current layer convolution operation and the normalization operator of the current layer convolution operation; The compensation item is 2 n-1 , n=a+bc, a is the power value of the power of 2 to which the quantization coefficient of the model weight is closest and less than or equal to, b is the power value of the power of 2 to which the quantization coefficient of the input eigenvalue is closest and less than or equal to, c is the power value of the power of 2 to which the quantization coefficient of the output eigenvalue of the convolution operation of the current layer is closest and less than or equal to, and n, a, b, and c are all positive integers; Neural network quantization converts high-bit data that occupies more computing resources into low-bit data that occupies less computing resources; the model weights, the input eigenvalues, and the output eigenvalues are all quantized low-bit data, which is suitable for deploying artificial intelligence applications on artificial intelligence devices with limited computing power and resources; Adapting to the input requirements of the next layer of convolution operation, converting the third convolution result into the final output result of the current layer of convolution operation specifically includes: Performing a threshold operation on the third convolution result to obtain a threshold operation result; Forcibly converting the cross-threshold operation result into a low-bit data operation to obtain a low-bit data type result; Activate the low-bit data type result to obtain the final output result; The low-bit data type result is activated to obtain the final output result of the current layer convolution operation, which is obtained by conversion according to the following formula: ; in, is the final output result, For x The activation function, x is the low bit data type result.
7. A neural network quantization processing device, It is characterized in that include: Memory for storing computer programs; A processor is used to execute the computer program, and when the computer program is executed by the processor, the steps of the neural network quantization processing method as described in any one of claims 1 to 5 are implemented.
8. A readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, the steps of the neural network quantization processing method according to any one of claims 1 to 5 are implemented.
