Method and system for reducing quantization error based on secondary quantization

By introducing 2-bit quantization and secondary quantization into the binary neural network, combined with symbolic functions and decomposition calculation encoding formats, the problem of binary network accuracy loss and edge device deployment is solved, and performance higher than full accuracy on small data sets is achieved. and higher accuracy than existing binary networks.

CN118643869APending Publication Date: 2024-09-13HANGZHOU DIANZI UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410783677.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-18
Publication Date
2024-09-13

AI Technical Summary

Technical Problem

While binary neural networks reduce the amount of model parameters and calculations, they also lead to a decrease in model capacity and representation capabilities, and serious loss of accuracy, and the computing and storage requirements are still not met when deployed on edge devices.

Method used

2-bit quantized activation data is used, and through the combination of primary quantization and secondary quantization, the symbolic function is used for quantization, a quantization encoding format suitable for decomposition calculation is introduced, and the convolution step calculation method is used to accelerate the calculation.

Benefits of technology

It reduces the activation quantization error, retains more feature information, improves the model prediction accuracy, and achieves slightly lower performance than the full-precision neural network on small data sets through the encoding format, while at the same time higher than the performance on CIFAR10 and CIFA100 data sets. Accuracy of existing binary neural networks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118643869A
    Figure CN118643869A
Patent Text Reader

Abstract

The invention discloses a method and a system for reducing quantization errors based on secondary quantization. The method comprises the following steps: S1, activating data and preprocessing the activated data; s2, performing secondary quantization on the preprocessed activation data; s3, encoding the quantized data; and S4, step-by-step accelerated convolution calculation is carried out on the coded data. According to the method, traditional binary neural network activation quantization is improved, secondary quantization is introduced into activated quantization, errors caused by quantization are reduced, more feature information is reserved, and richer information is provided for model training. Finally, the prediction precision of the model is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of artificial intelligence technology, and in particular relates to a method and system for reducing quantization error based on secondary quantization. Background Art

[0002] In recent years, deep neural networks (DNNs) have achieved great success in computer vision, natural language processing and other fields. In order to obtain higher accuracy, deep neural networks usually have the characteristics of large number of parameters and high computational complexity. For example, ResNet-50 has more than 25M parameters, and one inference requires 4.1G floating-point calculations. At the same time, deploying such a large model requires powerful computing resources and massive storage. However, with the development of mobile phones and Internet of Things (IoT) devices, the demand for deploying neural networks on edge devices is increasing. On these devices with limited storage and computing power, it is impossible to carry models with too many parameters and computational requirements. To solve this problem, researchers have proposed various model compression and acceleration methods, such as model quantization, pruning, distillation, etc. Among them, binary neural networks (BNNs) compress both weights and activation values ​​to 1 bit, greatly reducing the size and computational complexity of the model. At the same time, the most commonly used multiplication and accumulation operations in the inference process can be replaced by bit-by-bit operations such as XNOR and Bitcount, which are faster and more power-saving than ordinary floating-point operations.

[0003] However, while reducing the number of model parameters and computation, BNN also greatly limits the model capacity and representation ability, resulting in a huge decrease in network accuracy. For example, the groundbreaking XNORNet only achieved a classification accuracy of 51.2% on the ImageNet dataset, which is 18% lower than the accuracy of the full-precision network on the same dataset. In order to improve the accuracy of binary networks, researchers subsequently proposed minimizing the binarization error, designing a binarization-friendly network architecture, and better training algorithms. After introducing a series of methods to reduce binary quantization errors, there is still a large gap between binary neural networks and full-precision binary neural networks. At the same time, the XNORNet paper also proposed a binary weight network (BWN), which reduces the model size and computation by binarizing the weight parameters to +1 or -1, while retaining full-precision activations and using accumulation operations to approximate convolution operations, thereby achieving efficient calculations. On the ImageNet classification task, the Top-1 accuracy of BWN is within the accuracy loss range of about 5% compared to traditional CNN. However, BWN still requires a large number of floating-point operations, and is still unfriendly to edge devices with limited storage and computing and difficult to deploy. Summary of the invention

[0004] In order to reduce the negative impact of binary quantization of the model on the accuracy of the model, the present invention discloses a method and system for reducing quantization error based on secondary quantization. Specifically, the influence of activation quantization error on accuracy in binary neural network is more significant. In order to reduce the quantization error of activation, the present invention uses 2bit to quantize activation; in order to reduce the amount of calculation caused by introducing 2bit quantization, the present invention decomposes 2bit quantization into primary quantization and secondary quantization, and both quantizations are quantized using a sign function, wherein the primary quantization is the high bit of data and the secondary quantization is the low bit of data; in order to solve the problem that the weight and activation cannot be directly accelerated by XNOR and Bitcount due to the introduction of secondary quantization, the present invention introduces a quantization encoding format suitable for decomposition calculation; in order to utilize the advantage of binary quantization that only uses XOR calculation, a convolution step calculation method is proposed to solve the problem that the convolution operation between 1bit and 2bit data cannot be accelerated by XNOR and BitCount.

[0005] The technical solution of the present invention is as follows:

[0006] A method for reducing quantization error based on secondary quantization, comprising the following steps:

[0007] S1, activate data and preprocess the activated data;

[0008] S2, re-quantize the pre-processed activation data;

[0009] S3, encoding the quantized data;

[0010] S4. The encoded data is subjected to step-by-step accelerated convolution calculation.

[0011] Preferably, step S1 is specifically as follows:

[0012] S11. Preprocess the activation of the kth layer to obtain the mean of the activation data of this layer, as follows:

[0013]

[0014] In the above formula, μ k is the mean activation of the kth layer, x is the full-precision activation tensor data of the kth layer, c is the number of channels of the activation tensor, h is the height of the activation tensor, w is the width of the activation tensor, and m, j, i are the subscripts of the activation channels, height, and width, respectively.

[0015] S12: Preprocess the activation of the kth layer to obtain the relative mean μ of the activation data of this layer. k The distance is as follows:

[0016]

[0017] In the above formula, δk is the relative mean μ of the activation data of the kth layer k distance.

[0018] S13, calculating the secondary quantization reference threshold t, as follows:

[0019]

[0020] Preferably, in step S2, the activation data is quantized twice. Specifically, the steps are as follows:

[0021] S21, perform the first quantization on each activation data x to obtain b h , as follows:

[0022] b h =Sign(x-μ)

[0023] In the above formula, Sign(·) is the sign function, and μ is the mean value of the activation of this layer.

[0024] Specifically, when x>=μ, b h = +1; otherwise, b h =-1.

[0025] S22, perform a second quantization on each activation data x to obtain b l , as follows:

[0026] b l =Sign(x-μ-b h ×t)

[0027] Specifically, when x>=μ+b h ×t, b l = +1; otherwise, b l =-1.

[0028] Preferably, step S3, encoding the quantized data is as follows:

[0029] The quantization is encoded using the encoding table shown in the following table.

[0030] <![CDATA[Quantification result (b h , b l )]]> (-1,-1) (-1,+1) (+1,-1) (+1,+1) Binary encoding 00 01 10 11 Decimal result -2 -1 1 2 Actual physical meaning x<-t+μ -t+μ≤x<μ μ≤x<t+μ t+μ≤x

[0031] Preferably, step S4, stepwise acceleration of convolution calculation is as follows:

[0032] Since the high and low positions represent different weights, they cannot be directly convolved and added. Assuming the high and low position ratio coefficients are α and β respectively, the distribution law of convolution gives:

[0033]

[0034] In the above formula, is an XNOR operation, w b is the convolution kernel, and the above coding table shows:

[0035]

[0036] The solution is α=1.5, β=0.5. Similarly, when the input data x<0, the same result can be obtained. Then perform step-by-step XNOR and BitCount calculation on the binary data according to the low and high bits to obtain out h ,out l as follows:

[0037]

[0038] In order to further reduce the quantization error, since the secondary quantization uses t as the threshold quantization, the output result is multiplied by the proportional coefficient t, and the result is as follows:

[0039] out=t×(out h +out l )

[0040] In the above formula, out is the final convolution result, which realizes the use of XNOR and Bitcount to accelerate the convolution operation between non-single-bit data.

[0041] The present invention also discloses a system for reducing quantization error based on secondary quantization, based on the above method, including the following modules:

[0042] Preprocessing module: activates data and preprocesses the activated data;

[0043] Secondary quantization module: secondary quantization of preprocessed activation data;

[0044] Encoding module: encodes the quantized data;

[0045] Step-by-step accelerated convolution calculation module: The encoded data is subjected to step-by-step accelerated convolution calculation.

[0046] The beneficial effects of the present invention are:

[0047] The present invention discloses a method and system for reducing quantization error based on secondary quantization. The present invention (1) improves the quantization of activation of a traditional binary neural network, introduces secondary quantization in the quantization of activation, reduces the error caused by quantization, and retains more feature information, providing richer information for model training. Finally, the prediction accuracy of the model is improved. (2) The present invention introduces a coding format that is easy to split high and low bits, which is convenient for subsequent convolution operations of data to use XNOR and BitCount to accelerate calculation. At the same time, the introduced coding format allows the binary quantization of the original data to be retained and utilized, and then the secondary quantization has the characteristic of being easy to insert into different types of binary neural network models. (3) The present invention uses the introduced coding format to implement two-step XNOR and BitCount operations to obtain the same result of a 1-bit convolution kernel and a 2-bit activation convolution, greatly reducing the amount of calculation. Finally, under the same order of magnitude of calculation as the original binary neural network, a prediction accuracy slightly lower than that of a full-precision neural network is obtained under a small data set, and at the same time, the prediction accuracy is higher than that of the current binary neural network on the CIFAR10 and CIFA100 data sets. BRIEF DESCRIPTION OF THE DRAWINGS

[0048] Figure 1 The present invention is a flowchart of a method for reducing quantization error based on secondary quantization according to a preferred embodiment of the present invention.

[0049] Figure 2 Quantize the error of traditional binary neural network.

[0050] Figure 3 Accelerate 1-bit data convolution for XNOR and BitCount.

[0051] Figure 4 It is the secondary quantization process and encoding format.

[0052] Figure 5 To accelerate the convolution calculation process step by step.

[0053] Figure 6 The present invention is a block diagram of a system for reducing quantization error based on secondary quantization in a preferred embodiment of the present invention. DETAILED DESCRIPTION

[0054] The present invention will be further described below in conjunction with the accompanying drawings and specific implementation cases, but it should be noted that the present invention is not limited to the following implementation cases. Those skilled in the art should recognize that various replacements and modifications are possible without departing from the spirit and scope of the present invention and the appended claims. Therefore, the protection scope of the present invention should not be limited to the contents disclosed in the embodiments.

[0055] The application environment of the present invention is as follows: CUDA11.7, deep learning framework pytorch2.0.0, Intel core i9-13900KS CPU, 128G memory, GPU is NVIDIA GeForce RTX 4090, and video memory is 24G. The technical solution of the present invention is specifically introduced in conjunction with the accompanying drawings.

[0056] like Figure 1 As shown, this embodiment discloses a method for reducing quantization error based on secondary quantization, and the specific steps are as follows:

[0057] S1. Data preprocessing.

[0058] First, the present invention is verified on the datasets CIFAR10 and CIFAR100. CIFAR10 is relatively small in scale and has diverse categories. Its dataset contains 60,000 32x32 pixel color photos in 10 categories. CIFAR100 has richer categories, and the categories of the dataset are more specific and fine-grained, which can better explore the generalization ability of the model.

[0059] For a traditional binary neural network, given an input a∈R c×h×w and the weight matrix w∈R n×c×k×k (where a is a c×h×w matrix and w is a n×c×k×k matrix), then the feature output y∈R can be extracted through the convolution operation c ×h′×w′ , where the convolution operation consists of a large number of floating-point multiplication and accumulation operations. The convolution operation is as follows:

[0060] y=Conv(a,w)

[0061] In the above formula, Conv is the convolution calculation, a is the activation, and w is the convolution kernel.

[0062] S11. Evenly distributing data on both sides of the quantization threshold can reduce quantization error and avoid quantizing data to the same value when they are distributed on the same side of zero. Preprocess the activation of the kth layer to obtain the mean of the activation data of this layer, as follows:

[0063]

[0064] In the above formula, μ k is the mean activation of the kth layer, x is the full-precision activation tensor data of the kth layer, c is the number of channels of the activation tensor, h is the height of the activation tensor, w is the width of the activation tensor, and m, j, i are the subscripts of the activation channels, height, and width, respectively.

[0065] Because traditional binary quantization cannot retain the difference between data on the same side of the quantization threshold due to data width limitations, it leads to large quantization errors. When the data is near zero, the data greater than zero and less than zero have higher similarity than the data far from zero, such as Figure 2 As shown in the circled data, there is a big difference between the data far from zero and the data close to zero on the same side of the zero point, such as Figure 2 The triangle marked data shown is shown. Figure 2 Real represents the real input data of the quantization module, and Binary represents the binary quantization output data. Therefore, the present invention introduces secondary quantization to better preserve the similarities and differences between data, and secondary quantization also requires a threshold value suitable for the data set.

[0066] S12. In order to better represent the similarities and differences between data, and considering the discreteness of the overall data distribution, the mean of the distance between the input data and the quantization center point can well represent the degree to which the overall data deviates from the quantization center point. The activation of the kth layer is preprocessed to obtain the relative mean μ of the activation data of this layer. k The average distance is as follows:

[0067]

[0068] In the above formula, δ k is the relative mean μ of the activation data of the kth layer k distance.

[0069] S13, the average distance δ of the above different layers k Then the mean value can take into account the data of all layers, and the secondary quantization reference threshold t can be obtained, as follows:

[0070]

[0071] In the above formula, K is the number of iterations of model training.

[0072] S2. Perform secondary quantization on the activation data.

[0073] In order to speed up the time-consuming convolution calculation, the traditional binary neural network uses a sign function to directly quantize the input data and the convolution kernel into two extreme results +1 and -1. The sign function is as follows:

[0074]

[0075] S21, perform the first quantization on each activation data x to obtain b h , as follows:

[0076] b h =Sign(x-μ)

[0077] In the above formula, Sign(·) is the sign function, and μ is the mean activation value of the layer. Specifically, when x>=μ, b h = +1; otherwise, b h =-1.

[0078] S22. Data secondary quantization.

[0079] After the input data is quantized for the first time, binary data b is obtained. h , which has a large quantization error, mainly due to the 1-bit width limitation of binary data, which cannot represent the difference between data with the same symbol. Here, secondary quantization is introduced to further subdivide the data. Perform a second quantization on each activation data x to obtain b l , as follows:

[0080] b l =Sign(x-μ-b h ×t)

[0081] Specifically, when x>=μ+b h ×t, b l = +1; otherwise, b l =-1.

[0082] S3. Encode the quantized data.

[0083] The coding table shown in the following table is used to encode the quantization. The quantization coding process is as follows: Figure 4 shown.

[0084] <![CDATA[Quantification result (b h , b l )]]> (-1,-1) (-1,+1) (+1,-1) (+1,+1) Binary encoding 00 01 10 11 Decimal result -2 -1 1 2 Actual physical meaning x<-t+μ -t+μ≤x<μ μ≤x<t+μ t+μ≤x

[0085] As shown in the table above, double quantization divides the full-precision data into four segments, two positive and two negative segments. In computer storage, -1 is represented by 0 and +1 is represented by 1.

[0086] S4, step-by-step acceleration of convolution calculation.

[0087] Figure 5 The convolution process is shown in step by step. The convolution calculation used to extract features can be operated by bitwise XNOR (symbol ) and BitCount operations, where XNOR is a bitwise exclusive OR operation and BitCount is a bitwise sum operation, such as Figure 3 As shown, feature extraction can be achieved with less computational overhead and lower latency. The specific function is as follows:

[0088]

[0089] The introduced secondary quantization quantizes the activation into 2-bit data and performs bitwise XNOR on the data (with the symbol Indicates), BitCount operation can only speed up the convolution operation between 1-bit data. In order to speed up the convolution operation between 1-bit data and 2-bit data, the distribution law of convolution is obtained:

[0090] [f1(k)+f2(k)]*f3(k)=f1(k)*f3(k)+f2(k)*f3(k)

[0091] In the above formula, * indicates convolution calculation. According to the secondary quantization encoding format, 0 is represented by -1 and 1 is represented by +1, such as Figure 4 As shown in the figure, the high bit is used to indicate the sign of the data, and the low bit is used to further subdivide the size of the data. Since the high bit and the low bit represent different weights, they cannot be directly convolved and added. In order to achieve the final output result equivalent to the result of the convolution of 1-bit data and 2-bit data, the out h and out l Perform weighted summation. Let the high-order weight be α and the low-order weight be β. According to the convolution distribution law, we can get:

[0092]

[0093] Because b h , b l All are 1-bit matrices. The present invention introduces step-by-step convolution calculation, and convolves the 2-bit activation data with the 1-bit convolution kernel according to the high and low bits respectively. The calculation process is as follows: Figure 5 As shown in the second step, the calculation formula is as follows:

[0094]

[0095] From the above secondary quantization coding table, when the quantized input data x≥0, the binary linear equation can be obtained as follows:

[0096]

[0097] The solution is α = 1.5, β = 0.5. Similarly, when the quantized input data x < 0, the same result can be obtained. In order to further reduce the quantization error, since the secondary quantization uses t as the threshold quantization, the output result is multiplied by the proportional coefficient t, and the weighted output expression can be obtained as follows:

[0098] out=t×(out h +out l )

[0099] like Figure 5As shown in the third step, XNOR and BitCount are implemented to accelerate the convolution calculation while the activation is quantized to 2 bits. The verification accuracy is significantly improved with a slight increase in the model calculation amount and storage space.

[0100] like Figure 6 As shown, this embodiment discloses a system for reducing quantization error based on secondary quantization, based on the above method embodiment, including the following modules:

[0101] Preprocessing module: activates data and preprocesses the activated data;

[0102] Secondary quantization module: secondary quantization of preprocessed activation data;

[0103] Encoding module: encodes the quantized data;

[0104] Step-by-step accelerated convolution calculation module: The encoded data is subjected to step-by-step accelerated convolution calculation.

[0105] For other contents of this embodiment, please refer to the above method embodiment.

[0106] In order to solve the problem of edge device deployment in the prior art, the present invention introduces secondary quantization by activation to further reduce the quantization error of activation, and introduces a step-by-step convolution operation method to accelerate the convolution calculation, so as to solve the problem that the weights and activations cannot be directly accelerated by XNOR and Bitcount due to the introduction of secondary quantization.

[0107] The present invention improves the quantization of activation of traditional binary neural network, introduces secondary quantization in the quantization of activation, reduces the error caused by quantization, and retains more feature information, providing richer information for model training. Finally, the prediction accuracy of the model is improved. Secondly, the present invention introduces a coding format that is easy to split high and low bits, which is convenient for the convolution operation of subsequent data to use XNOR and BitCount for accelerated calculation. At the same time, the introduced coding format allows the binary quantization of the original data to be retained and utilized, and then the secondary quantization has the characteristic of being easy to insert different types of binary neural network models. Finally, the present invention uses the introduced coding format to realize the two-step XNOR and BitCount operations to obtain the same result of 1bit convolution kernel and 2bit activation convolution, greatly reducing the amount of calculation. Finally, it is achieved that under the same order of magnitude of calculation as the original binary neural network, the prediction accuracy slightly lower than the full-precision neural network under a small data set is obtained, and at the same time, the prediction accuracy of the current binary neural network is higher than that of the current binary neural network on the CIFAR10 and CIFA100 data sets.

[0108] The above description is only a detailed description of the preferred embodiments and principles of the present invention. For ordinary technicians in this field, according to the ideas provided by the present invention, there will be changes in the specific implementation methods, and these changes should also be regarded as the protection scope of the present invention.

Claims

1. A method for reducing quantization error based on secondary quantization, characterized in that: Follow these steps: S1, activating data and preprocessing the activated data; S2, re-quantize the pre-processed activation data; S3, encoding the quantized data; S4. The encoded data is subjected to step-by-step accelerated convolution calculation.

2. A method for reducing quantization error based on secondary quantization as claimed in claim 1, characterized in that: Step S1 is specifically as follows: S11. Preprocess the activation of the kth layer to obtain the mean of the activation data of this layer, as follows: In the above formula, μ k is the mean value of the activation of the kth layer, x is the full-precision activation tensor data of the kth layer, c is the number of channels of the activation tensor, h is the height of the activation tensor, w is the width of the activation tensor, and m, j, i are the subscripts of the activation channels, height, and width respectively; S12: Preprocess the activation of the kth layer to obtain the relative mean μ of the activation data of this layer. k The distance is as follows: In the above formula, δ k is the relative mean μ of the activation data of the kth layer k distance; S13, calculating the secondary quantization reference threshold t, as follows: Here, K is the number of iterations of model training.

3. A method for reducing quantization error based on secondary quantization as claimed in claim 2, characterized in that: Step S2 is specifically as follows: S21, perform the first quantization on each activation data x to obtain b h , as follows: b h =Sign(x-μ) In the above formula, Sign(·) is the sign function, and μ is the mean of the activation layer; When x>=μ, b h = +1; otherwise, b h = -1; S22, perform a second quantization on each activation data x to obtain b l , as follows: b l =Sign(x-μ-b h ×t) When x>=μ+b h ×t, b l = +1; otherwise, b l =-1.

4. A method for reducing quantization error based on secondary quantization as claimed in claim 3, characterized in that: Step S3, encode the quantized data using the following encoding table: 。 5. A method for reducing quantization error based on secondary quantization as claimed in claim 4, characterized in that: Step S4 is specifically as follows: Assume that the high-order and low-order proportional coefficients are α and β respectively, and the distribution law of convolution gives: In the above formula, is an XNOR operation, w b Is the convolution kernel, obtained from the encoding table: The solution is α=1.5, β=0.5; similarly, when the input data x<0, the same result is obtained; then the binary data is divided into low and high bits and the BitCount is calculated to get out h ,out l as follows: Multiply the output result by the proportional coefficient t, and the result is as follows: out=t×(out h +out l ) In the above formula, out is the final convolution result.

6. A system for reducing quantization error based on secondary quantization, based on the method according to any one of claims 1 to 5, characterized in that: Includes the following modules: Preprocessing module: activates data and preprocesses the activated data; Secondary quantization module: secondary quantization of preprocessed activation data; Encoding module: encodes the quantized data; Step-by-step accelerated convolution calculation module: The encoded data is subjected to step-by-step accelerated convolution calculation.

Citation Information

Patent Citations

  • Neural network quantification method

    CN111985639A

  • Method for retraining and compensating activation function of low-bit quantization network

    CN113743601A