Neural network model quantification method and device, equipment and storage medium
By gradually reducing the accuracy of the neural network model from 32-bit to 16-bit to 8-bit, combined with mixed-precision training, the problem of large quantization loss in the existing technology is solved, and efficient deployment and operation in embedded devices is achieved.
Patent Information
- Application Number
- CN202310333586.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-30
- Publication Date
- 2024-10-01
AI Technical Summary
Existing neural network model quantification technology suffers from sudden changes in accuracy and large performance losses, making it difficult to deploy efficiently in embedded devices.
Use 32-bit precision data to train the neural network model, gradually reduce the precision to 16-bit and 8-bit, reduce quantization loss through mixed precision training and multiple trainings, avoid sudden changes in accuracy, and improve the storage and operation efficiency of the model.
It realizes the possibility of efficiently deploying neural network models in embedded devices, reduces quantification losses, and improves the model's operating efficiency and storage capacity.
Smart Images

Figure CN118734908A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a neural network model quantization method, device, equipment and storage medium. Background Art
[0002] With the rapid application of deep learning technology in multiple fields, including speech, computer vision, natural language processing, etc., a large number of neural network models based on deep learning have emerged. These models are generally large in number of parameters and complex, making them difficult to deploy in embedded devices such as car computers and robots.
[0003] In order to solve this contradiction, model quantization came into being. It can compress the model with a small loss of precision, making it possible to apply these complex models to terminals such as car computers and robots. The current common quantization solution is to quantize the model weights and weight activation values from the original floating-point data type (float32) to the integer data type (int8), achieving smaller storage and faster running speed.
[0004] However, since neural network models generally have deep layers and undergo multiple rounds of data iteration, most of the model's weights are concentrated in a relatively small range with high precision. The existing quantization scheme directly quantizes float32 to int8, resulting in a sudden change in precision and causing large quantization losses. Summary of the invention
[0005] In order to solve the above technical problems, the present disclosure provides a neural network model quantization method, device, equipment and storage medium.
[0006] In a first aspect, an embodiment of the present disclosure provides a neural network model quantization method, comprising:
[0007] The neural network model is trained using 32-bit precision data to obtain a first neural network model;
[0008] Performing 16-bit precision training on the first neural network model to obtain a second neural network model;
[0009] The second neural network model is quantized with 8-bit precision to obtain a third neural network model.
[0010] In some embodiments, the neural network model is trained using 32-bit precision data to obtain a first neural network model, including:
[0011] Initialize the neural network model;
[0012] Get multiple sets of 32-bit precision data and preset convergence ranges;
[0013] Input each group of 32-bit precision data into the initialized neural network model for training, and obtain the output results corresponding to each group of 32-bit precision data;
[0014] The output results corresponding to each group of 32-bit precision data are converged to obtain the convergence value of the neural network model;
[0015] According to the deviation relationship between the convergence value of the neural network model and the preset convergence range, the parameters of the neural network model are updated to obtain a first neural network model.
[0016] In some embodiments, before updating the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain the first neural network model, the method further includes:
[0017] Determining whether the convergence value of the neural network model is within the preset convergence range;
[0018] If the convergence value of the neural network model is not within the preset convergence range, the deviation relationship between the convergence value of the neural network model and the preset convergence range is calculated.
[0019] In some embodiments, the updating of the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain the first neural network model includes:
[0020] Performing loss calculation on the convergence value of the neural network model based on the preset convergence range to obtain a loss calculation result of the neural network model;
[0021] Based on the loss calculation result of the neural network model, updating the model parameters of the neural network model;
[0022] If the convergence value of the updated neural network model is within the preset convergence range, a first neural network model is obtained.
[0023] In some embodiments, the performing 16-bit precision training on the first neural network model to obtain a second neural network model includes:
[0024] Mapping the 32-bit precision data to 16-bit precision data;
[0025] The first neural network model is trained using data with 16-bit precision to obtain a second neural network model.
[0026] In some embodiments, before performing 16-bit precision training on the first neural network model to obtain the second neural network model, the method further includes:
[0027] Performing mixed precision training of 32-bit precision and 16-bit precision on the first neural network model to obtain a fourth neural network model;
[0028] The step of performing 16-bit precision training on the first neural network model to obtain a second neural network model includes:
[0029] The fourth neural network model is trained with 16-bit precision to obtain a second neural network model.
[0030] In some embodiments, the performing mixed precision training of 32-bit precision and 16-bit precision on the first neural network model to obtain a fourth neural network model includes:
[0031] Using the 32-bit precision data to train the normalization layer and the activation layer of the first neural network model;
[0032] Mapping the 32-bit precision data to 16-bit precision data, and using the 16-bit precision data to train the convolutional layer and the linear layer of the first neural network model;
[0033] Converging an output result of the first neural network model to obtain a convergence value of the first neural network model;
[0034] Performing loss calculation on the convergence value of the first neural network model based on a preset convergence range to obtain a loss calculation result of the first neural network model;
[0035] Based on the loss calculation result of the first neural network model, updating the model parameters of the first neural network model;
[0036] If the updated convergence value of the first neural network model is within the preset convergence range, a fourth neural network model is obtained.
[0037] In some embodiments, the step of performing 8-bit precision quantization on the second neural network model to obtain a third neural network model includes:
[0038] Counting the weight value, activation value, maximum value, and minimum value in the second neural network model;
[0039] Mapping the weight value in the second neural network model to a weight value with 8-bit precision, mapping the activation value in the second neural network model to an activation value with 8-bit precision, mapping the maximum value in the second neural network model to a maximum value with 8-bit precision, and mapping the minimum value in the second neural network model to a minimum value with 8-bit precision;
[0040] The operator in the second neural network model is replaced with a quantization operator to obtain a third neural network model.
[0041] In a second aspect, an embodiment of the present disclosure provides a neural network model quantization device, comprising:
[0042] A first training module is used to train the neural network model using 32-bit precision data to obtain a first neural network model;
[0043] A second training module is used to perform 16-bit precision training on the first neural network model to obtain a second neural network model;
[0044] The quantization module is used to perform 8-bit precision quantization on the second neural network model to obtain a third neural network model.
[0045] In a third aspect, an embodiment of the present disclosure provides an electronic device, including:
[0046] Memory;
[0047] Processor; and
[0048] Computer programs;
[0049] The computer program is stored in the memory and is configured to be executed by the processor to implement the method as described in the first aspect.
[0050] In a fourth aspect, an embodiment of the present disclosure provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program is executed by a processor to implement the method as described in the first aspect.
[0051] In a fifth aspect, an embodiment of the present disclosure further provides a computer program product, which includes a computer program or instructions, and when the computer program or instructions are executed by a processor, the neural network model quantization method as described above is implemented.
[0052] The neural network model quantization method, device, equipment and storage medium provided in the embodiments of the present disclosure trains the neural network model with 32-bit precision data to obtain a first neural network model, trains the first neural network model with 16-bit precision to obtain a second neural network model, and quantizes the second neural network model with 8-bit precision to obtain a third neural network model. The present disclosure uses 32-bit precision data to train a neural network model with 32-bit precision, further trains the neural network model with 32-bit precision with 16-bit precision to obtain a neural network model with 16-bit precision, first trains the neural network model with 32-bit precision to 16-bit precision through multiple trainings, and then quantizes the neural network model with 16-bit precision to 8-bit precision to obtain a neural network model with 8-bit precision after quantization. Compared with the prior art that directly quantizes the neural network model with 32-bit precision to the neural network model with 8-bit precision, the present disclosure gradually reduces the precision from 32-bit precision to 16-bit precision, and there is no sudden change in precision, which can gradually reduce the precision of the model parameters, thereby reducing the performance loss of model quantization. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0054] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0055] Figure 1 A flow chart of a neural network model quantization method provided in an embodiment of the present disclosure;
[0056] Figure 2 A flowchart of a neural network model quantization method provided by another embodiment of the present disclosure;
[0057] Figure 3 A flowchart of a neural network model quantization method provided by another embodiment of the present disclosure;
[0058] Figure 4 A schematic diagram of the structure of a neural network model quantization device provided in an embodiment of the present disclosure;
[0059] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0060] In order to more clearly understand the above-mentioned objectives, features and advantages of the present disclosure, the scheme of the present disclosure will be further described below. It should be noted that the embodiments of the present disclosure and the features in the embodiments can be combined with each other without conflict.
[0061] In the following description, many specific details are set forth to facilitate a full understanding of the present disclosure, but the present disclosure may also be implemented in other ways different from those described herein; it is obvious that the embodiments in the specification are only part of the embodiments of the present disclosure, rather than all of the embodiments.
[0062] The present disclosure provides a neural network model quantization method, which is introduced below in conjunction with a specific embodiment.
[0063] Figure 1 A flow chart of the neural network model quantization method provided in the embodiment of the present disclosure. The method can be applied to electronic devices, which may be portable mobile devices such as smart phones, tablet computers, laptops, car navigation devices, smart sports equipment, etc.; they may also be fixed devices such as personal computers and smart home appliances. The method can be applied to the scenario of quantizing the neural network model. The method solves the problem of large quantization loss and low running efficiency of the quantized model in the prior art. The quantization loss can be reduced, and the entire model can be quantized, so that the neural network model can be deployed on the mobile terminal with smaller storage and faster running speed, so as to achieve the purpose of improving the product experience. It can be understood that the neural network model quantization method provided in the embodiment of the present disclosure can also be applied in other scenarios.
[0064] Below Figure 1 The neural network model quantization method shown in FIG. 1 is introduced, and the specific steps of the method are as follows:
[0065] S101. Use 32-bit precision data to train a neural network model to obtain a first neural network model.
[0066] In this step, the electronic device trains the neural network model based on 32-bit precision data. When the neural network model converges to a certain range, the training of the neural network model is completed, and the first neural network model is obtained, that is, the neural network model under 32-bit precision. The 32-bit precision data can be a floating point value without limitation.
[0067] S102: Perform 16-bit precision training on the first neural network model to obtain a second neural network model.
[0068] After obtaining the first neural network model, the electronic device performs 16-bit precision training on all layers of the first neural network model to obtain a second neural network model, i.e., a neural network model with 16-bit precision, which can gradually reduce the precision of model parameters and reduce the performance loss of model quantization.
[0069] S103. Perform 8-bit precision quantization on the second neural network model to obtain a third neural network model.
[0070] After the second neural network model is obtained through training, the electronic device quantizes the second neural network model with 8-bit precision to obtain a third neural network model, i.e., a neural network model with 8-bit precision after quantization.
[0071] Taking a deep learning architecture (Pytorch) as an example, the following steps are used for quantization: 1) Prepare the model: add QuantStub and DeQuantStub modules to specify where to explicitly quantize and dequantize activation values; 2) Module combination: fuse combined operations such as conv+relu or conv+batchnorm+relu together to improve the accuracy and performance of the model; 3) Specify quantization configuration: you can choose symmetric or asymmetric quantization and minimum maximum value (MinMax) or L2Norm calibration technology; 4) Calibration: perform forward inference on the model using a calibration dataset; 5) Model conversion: store the scaling factor and bias value to be used for each activation tensor, and replace the float operator with a quantization operator, etc.
[0072] The embodiment of the present disclosure uses 32-bit precision data to train the neural network model to obtain a first neural network model, performs mixed precision training of 32-bit precision and 16-bit precision on the first neural network model to obtain a fourth neural network model, performs 16-bit precision training on the first neural network model to obtain a second neural network model, and performs 8-bit precision quantization on the second neural network model to obtain a third neural network model. The present disclosure uses 32-bit precision data to train a neural network model with 32-bit precision, further performs 16-bit precision training on the neural network model with 32-bit precision to obtain a neural network model with 16-bit precision, first trains the neural network model with 32-bit precision to 16-bit precision through multiple trainings, and then quantizes the neural network model with 16-bit precision to 8-bit precision to obtain a neural network model with 8-bit precision after quantization. Compared with the prior art that directly quantizes the neural network model with 32-bit precision to the neural network model with 8-bit precision, the present disclosure gradually reduces the precision from 32-bit precision to 16-bit precision, and then to 8-bit precision, and there is no sudden change in precision, which can gradually reduce the precision of model parameters, thereby reducing the performance loss of model quantization.
[0073] On the basis of the above embodiment, before performing 16-bit precision training on the first neural network model to obtain the second neural network model, the method also includes: performing mixed precision training of 32-bit precision and 16-bit precision on the first neural network model to obtain a fourth neural network model; performing 16-bit precision training on the first neural network model to obtain the second neural network model includes: performing 16-bit precision training on the fourth neural network model to obtain the second neural network model.
[0074] Mixed precision training includes 32-bit precision training and 16-bit precision training. Since different layers of the neural network model have different sensitivities to precision, for example, normalization layers (such as layer normalization layernorm, batch normalization batchnorm), activation layers (such as softmax layers), etc. are more sensitive to precision, while convolutional layers (CNN), linear layers (Liner), etc. are less sensitive to precision, therefore, using different precisions for training different layers of the neural network model can reduce the performance loss of the neural network model. The electronic device performs mixed precision training of 32-bit precision and 16-bit precision on the first neural network model to obtain a fourth neural network model, that is, a neural network model under mixed precision. Further, the fourth neural network model is trained with 16-bit precision to obtain a second neural network model.
[0075] Compared with some other existing solutions, since the normalization layer, activation layer, etc. in the neural network model are particularly sensitive to precision, the quantization loss of these layers is generally large. Sometimes, for the sake of model use effect, the quantization of these layers will be abandoned, thus affecting the operating efficiency of the quantized model. Compared with the existing solution of quantizing layers that are not sensitive to precision, the embodiment of the present disclosure adopts mixed precision of 32-bit precision and 16-bit precision for training, that is, different precisions are used for training layers with different precision sensitivities, which can improve the operating efficiency of the quantized model while ensuring the use effect of the model. The present disclosure first adopts mixed precision training to obtain the fourth neural network model, and further, the fourth neural network model is trained with 16-bit precision to obtain the second neural network model, and then the entire second neural network model, that is, all layers of the model are quantized with 8-bit precision, which can reduce the storage of the neural network model, improve the running speed of the neural network model, and thus improve the running efficiency of the quantized model.
[0076] Figure 2 A flowchart of a neural network model quantization method provided by another embodiment of the present disclosure is shown in FIG. Figure 2 As shown, the method includes the following steps:
[0077] S201, initializing the neural network model.
[0078] In this step, the electronic device initializes the neural network model and trains the neural network model with initial parameters.
[0079] S202, obtaining multiple groups of 32-bit precision data and a preset convergence range.
[0080] The electronic device obtains multiple sets of 32-bit precision data and a preset convergence range, and the multiple sets of 32-bit precision data can be multiple sets of floating point values without limitation. The preset convergence range can be set by the user, and there is no limitation on the preset convergence range value.
[0081] S203, input each group of 32-bit precision data into the initialized neural network model for training, and obtain output results corresponding to each group of 32-bit precision data.
[0082] The electronic device trains the initialized neural network model based on multiple groups of 32-bit precision data to obtain output results corresponding to each group of 32-bit precision data. Specifically, the electronic device inputs each group of 32-bit precision data into the initialized neural network model for training, and obtains output results corresponding to each group of 32-bit precision data.
[0083] S204, converge the output results corresponding to each group of 32-bit precision data to obtain the convergence value of the neural network model.
[0084] After obtaining the output results corresponding to each group of 32-bit precision data, the electronic device converges the output results corresponding to each group of 32-bit precision data to obtain the convergence value of the neural network model. The more the neural network model converges, the more stable the neural network model tends to be.
[0085] S205. Update the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain a first neural network model.
[0086] The electronic device updates the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain a first neural network model. Specifically, when the convergence value of the neural network model deviates greatly from the preset convergence range, the parameters of the neural network model are updated and training is continued to obtain the first neural network model; when the convergence value of the neural network model deviates less from the preset convergence range, the neural network model training is completed, and the current parameters are used as the final parameters to obtain the first neural network model.
[0087] In some embodiments, before updating the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain the first neural network model, the method further includes: determining whether the convergence value of the neural network model is within the preset convergence range; if the convergence value of the neural network model is not within the preset convergence range, calculating the deviation relationship between the convergence value of the neural network model and the preset convergence range.
[0088] After obtaining the convergence value of the neural network model, the electronic device determines whether the convergence value of the neural network model is within the preset convergence range. If the convergence value of the neural network model is not within the preset convergence range, the deviation relationship between the convergence value of the neural network model and the preset convergence range is calculated; if the convergence value of the neural network model is within the preset convergence range, the neural network model training is completed, and the current parameters are used as the final parameters to obtain the first neural network model.
[0089] In some embodiments, S205 includes but is not limited to S2051, S2052, S2053:
[0090] S2051. Perform loss calculation on the convergence value of the neural network model based on the preset convergence range to obtain a loss calculation result of the neural network model.
[0091] The electronic device performs loss calculation on the convergence value of the neural network model according to the preset convergence range to obtain a loss calculation result of the neural network model. The smaller the loss calculation result is, the more accurate the output result is.
[0092] S2052. Based on the loss calculation result of the neural network model, update the model parameters of the neural network model.
[0093] The electronic device updates the model parameters of the neural network model according to the loss calculation result of the neural network model, thereby improving the accuracy of the neural network model. As the number of training times increases, the parameters of the model are continuously adjusted to make the output results more and more accurate.
[0094] S2053: If the convergence value of the updated neural network model is within the preset convergence range, a first neural network model is obtained.
[0095] When the convergence value of the updated neural network model is within the preset convergence range, the training of the neural network model is completed and a first neural network model is obtained.
[0096] S206. Map the 32-bit precision data to 16-bit precision data.
[0097] The electronic device maps the 32-bit precision data to 16-bit precision data. Specifically, the electronic device maps the 32-bit precision data to 16-bit precision data according to a mapping relationship between the 32-bit precision data and the 16-bit precision data.
[0098] S207. Use 16-bit precision data to train the first neural network model to obtain a second neural network model.
[0099] The electronic device trains the first neural network model based on 16-bit precision data. When the first neural network model converges to a certain range, the training of the first neural network model is completed, and the current parameters are used as the final parameters to obtain the second neural network model. Specifically, the electronic device inputs each group of 16-bit precision data into the first neural network model for training, obtains the output results corresponding to each group of 16-bit precision data, converges the output results corresponding to each group of 16-bit precision data, obtains the convergence value of the neural network model, calculates the loss of the convergence value of the first neural network model based on the preset convergence range, obtains the loss calculation result of the first neural network model, updates the model parameters of the first neural network model based on the loss calculation result of the first neural network model, and obtains the second neural network model if the convergence value of the updated first neural network model is within the preset convergence range.
[0100] S208. Perform 8-bit precision quantization on the second neural network model to obtain a third neural network model.
[0101] Specifically, the implementation process and principle of S208 and S103 are the same, and will not be repeated here.
[0102] The disclosed embodiment initializes the neural network model, obtains multiple groups of 32-bit precision data and a preset convergence range, inputs each group of 32-bit precision data into the initialized neural network model for training, obtains the output results corresponding to each group of 32-bit precision data, converges the output results corresponding to each group of 32-bit precision data, and obtains the convergence value of the neural network model. Further, according to the deviation relationship between the convergence value of the neural network model and the preset convergence range, the parameters of the neural network model are updated to obtain a first neural network model, the 32-bit precision data is mapped to 16-bit precision data, and the first neural network model is trained using the 16-bit precision data to obtain a second neural network model. Then, the second neural network model is quantized with 8-bit precision to obtain a third neural network model. The present invention uses 32-bit precision data to train a neural network model with 32-bit precision, and further trains the neural network model with 16-bit precision to obtain a neural network model with 16-bit precision. The neural network model with 32-bit precision is first trained multiple times to gradually be trained to 16-bit precision, and then the neural network model with 16-bit precision is quantized to 8-bit precision to obtain a neural network model with 8-bit precision after quantization. Compared with the prior art that directly quantizes the neural network model with 32-bit precision to the neural network model with 8-bit precision, the present invention gradually reduces the precision from 32-bit precision to 16-bit precision, and there is no sudden change in precision. The precision of the model parameters can be gradually reduced, thereby reducing the performance loss of model quantization.
[0103] Figure 3 A flowchart of a neural network model quantization method provided by another embodiment of the present disclosure is shown in FIG. Figure 3 As shown, the method includes the following steps:
[0104] S301. Use 32-bit precision data to train a neural network model to obtain a first neural network model.
[0105] Specifically, the implementation process and principle of S301 and S101 are the same, and will not be repeated here.
[0106] S302. Use the 32-bit precision data to train the normalization layer and activation layer of the first neural network model.
[0107] Since the normalization layer and activation layer in the neural network model are highly sensitive to precision, the electronic device uses the 32-bit precision data to train the normalization layer and activation layer of the first neural network model.
[0108] S303: Map the 32-bit precision data to 16-bit precision data, and use the 16-bit precision data to train the convolutional layer and the linear layer of the first neural network model.
[0109] Since the convolutional layer and linear layer in the neural network model are less sensitive to precision, the 16-bit precision data is used to train the convolutional layer and linear layer of the first neural network model. Specifically, the electronic device first maps the 32-bit precision data to 16-bit precision data, and further uses the 16-bit precision data to train the convolutional layer and linear layer of the first neural network model. By using different precisions for training different layers of the neural network model, the performance loss of the neural network model can be reduced.
[0110] S304: Converging the output result of the first neural network model to obtain a convergence value of the first neural network model.
[0111] Furthermore, the electronic device converges the output result of the first neural network model to obtain a convergence value of the first neural network model. The more the first neural network model converges, the more stable the first neural network model tends to be.
[0112] S305: Perform loss calculation on the convergence value of the first neural network model based on a preset convergence range to obtain a loss calculation result of the first neural network model.
[0113] After obtaining the convergence value of the first neural network model, the electronic device can perform loss calculation on the convergence value of the first neural network model according to the preset convergence range to obtain the loss calculation result of the first neural network model. The smaller the loss calculation result, the more accurate the output result.
[0114] S306. Based on the loss calculation result of the first neural network model, update the model parameters of the first neural network model.
[0115] The electronic device updates the model parameters of the first neural network model according to the loss calculation result of the first neural network model, thereby improving the accuracy of the first neural network model. As the number of training times increases, the parameters of the model are continuously adjusted to make the output results more and more accurate.
[0116] S307: If the updated convergence value of the first neural network model is within the preset convergence range, a fourth neural network model is obtained.
[0117] When the updated convergence value of the first neural network model is within the preset convergence range, the training of the first neural network model is completed, and the current parameters are used as the final parameters to obtain the fourth neural network model.
[0118] S308. Perform 16-bit precision training on the fourth neural network model to obtain a second neural network model.
[0119] After obtaining the fourth neural network model, the electronic device performs 16-bit precision training on all layers of the fourth neural network model to obtain a second neural network model, i.e., a neural network model with 16-bit precision, which can gradually reduce the precision of model parameters and reduce the performance loss of model quantization.
[0120] S309: Count the weight value, activation value, maximum value, and minimum value in the second neural network model.
[0121] After obtaining the second neural network model, the electronic device counts the weight value, activation value, maximum value, and minimum value in the second neural network model.
[0122] S310. Map the weight values in the second neural network model to weight values with 8-bit precision, map the activation values in the second neural network model to activation values with 8-bit precision, map the maximum value in the second neural network model to the maximum value with 8-bit precision, and map the minimum value in the second neural network model to the minimum value with 8-bit precision.
[0123] Furthermore, the electronic device maps the weight values in the second neural network model to weight values with 8-bit precision, maps the activation values in the second neural network model to activation values with 8-bit precision, maps the maximum value in the second neural network model to the maximum value with 8-bit precision, and maps the minimum value in the second neural network model to the minimum value with 8-bit precision. Since the entire model is fully quantized, the neural network model can be deployed on the mobile terminal with smaller storage and faster running speed, thereby improving the running efficiency of the quantized model.
[0124] S311. Replace the operator in the second neural network model with a quantization operator to obtain a third neural network model.
[0125] In this step, the electronic device replaces the operator in the second neural network model with a quantization operator to obtain a third neural network model. Specifically, the electronic device replaces the matrix transformation relationship in the second neural network model with a quantization matrix transformation relationship, thereby completing the quantization of the second neural network model and obtaining the third neural network model.
[0126] The disclosed embodiment trains the neural network model with 32-bit precision data to obtain a first neural network model, trains the normalization layer and activation layer of the first neural network model with the 32-bit precision data, maps the 32-bit precision data to 16-bit precision data, and trains the convolution layer and linear layer of the first neural network model with the 16-bit precision data. Further, the output result of the first neural network model is converged to obtain the convergence value of the first neural network model, and the loss calculation of the convergence value of the first neural network model is performed based on a preset convergence range to obtain the loss calculation result of the first neural network model. Based on the loss calculation result of the first neural network model, the model parameters of the first neural network model are updated. If the convergence value of the updated first neural network model is within the preset convergence range, a fourth neural network model is obtained. Then, the fourth neural network model is trained with 16-bit precision to obtain a second neural network model, and the weight value, activation value, maximum value, and minimum value in the second neural network model are counted. Then, the weight values in the second neural network model are mapped to weight values with 8-bit precision, the activation values in the second neural network model are mapped to activation values with 8-bit precision, the maximum value in the second neural network model is mapped to the maximum value with 8-bit precision, the minimum value in the second neural network model is mapped to the minimum value with 8-bit precision, and the operator in the second neural network model is replaced with a quantization operator to obtain a third neural network model. The present disclosure solves the problems of large quantization loss and low running efficiency of the quantized model in the prior art. By combining mixed precision training with 16-bit precision training, the accuracy of the model parameters can be gradually reduced, and the performance loss of the full quantization of the model can be reduced. The second neural network model is quantized with 8-bit precision, which can reduce the storage of the neural network model and increase the running speed of the neural network model, thereby improving the running efficiency of the quantized model.
[0127] Figure 4 The schematic diagram of the structure of the neural network model quantization device provided in the embodiment of the present disclosure. The neural network model quantization device may be the electronic device as described in the above embodiment, or the neural network model quantization device may be a component or assembly in the electronic device. The neural network model quantization device provided in the embodiment of the present disclosure may execute the processing flow provided in the embodiment of the neural network model quantization method, such as Figure 4As shown, the neural network model quantization device 40 includes: a first training module 41, a second training module 42, and a quantization module 43; wherein the first training module 41 is used to train the neural network model with 32-bit precision data to obtain a first neural network model; the second training module 42 is used to perform 16-bit precision training on the first neural network model to obtain a second neural network model; the quantization module 43 is used to perform 8-bit precision quantization on the second neural network model to obtain a third neural network model.
[0128] Optionally, the first training module 41 uses 32-bit precision data to train the neural network model, and when the first neural network model is obtained, it is specifically used to: initialize the neural network model; obtain multiple groups of 32-bit precision data and a preset convergence range; input each group of 32-bit precision data into the initialized neural network model for training, and obtain output results corresponding to each group of 32-bit precision data; converge the output results corresponding to each group of 32-bit precision data to obtain the convergence value of the neural network model; update the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain the first neural network model.
[0129] Optionally, before the first training module 41 updates the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain the first neural network model, the first training module 41 is also used to: determine whether the convergence value of the neural network model is within the preset convergence range; if the convergence value of the neural network model is not within the preset convergence range, calculate the deviation relationship between the convergence value of the neural network model and the preset convergence range.
[0130] Optionally, the first training module 41 updates the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range. When obtaining the first neural network model, it is specifically used to: perform loss calculation on the convergence value of the neural network model based on the preset convergence range to obtain the loss calculation result of the neural network model; update the model parameters of the neural network model based on the loss calculation result of the neural network model; if the convergence value of the updated neural network model is within the preset convergence range, the first neural network model is obtained.
[0131] Optionally, when the second training module 42 performs 16-bit precision training on the first neural network model to obtain a second neural network model, it is specifically used to: map the 32-bit precision data to 16-bit precision data; and train the first neural network model using 16-bit precision data to obtain a second neural network model.
[0132] Optionally, the device further includes: a third training module 44; the third training module 44 is used to perform mixed precision training of 32-bit precision and 16-bit precision on the first neural network model to obtain a fourth neural network model;
[0133] When the second training module 42 performs 16-bit precision training on the first neural network model to obtain a second neural network model, it is specifically used to perform 16-bit precision training on the fourth neural network model to obtain a second neural network model.
[0134] Optionally, when the second training module 42 performs mixed precision training of 32-bit precision and 16-bit precision on the first neural network model to obtain the fourth neural network model, it is specifically used to: use the 32-bit precision data to train the normalization layer and activation layer of the first neural network model; map the 32-bit precision data to 16-bit precision data, and use the 16-bit precision data to train the convolution layer and linear layer of the first neural network model; converge the output result of the first neural network model to obtain the convergence value of the first neural network model; perform loss calculation on the convergence value of the first neural network model based on a preset convergence range to obtain the loss calculation result of the first neural network model; update the model parameters of the first neural network model based on the loss calculation result of the first neural network model; if the convergence value of the updated first neural network model is within the preset convergence range, a fourth neural network model is obtained.
[0135] Optionally, when the quantization module 43 performs 8-bit precision quantization on the second neural network model to obtain the third neural network model, it is specifically used to: count the weight values, activation values, maximum values, and minimum values in the second neural network model; map the weight values in the second neural network model to weight values with 8-bit precision, map the activation values in the second neural network model to activation values with 8-bit precision, map the maximum value in the second neural network model to the maximum value with 8-bit precision, and map the minimum value in the second neural network model to the minimum value with 8-bit precision; replace the operator in the second neural network model with a quantization operator to obtain the third neural network model.
[0136] Figure 4 The neural network model quantization device of the illustrated embodiment can be used to execute the technical solution of the above-mentioned method embodiment. Its implementation principle and technical effects are similar and will not be repeated here.
[0137] Figure 5 Schematic diagram of the structure of an electronic device in the embodiment of the present disclosure. Figure 5, which shows a structural schematic diagram of an electronic device 600 suitable for implementing the embodiments of the present disclosure. Figure 5 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0138] like Figure 5 As shown, the electronic device 600 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 to a random access memory (RAM) 603 to implement the neural network model quantization method of the embodiment described in the present disclosure. In RAM 603, various programs and data required for the operation of the electronic device 600 are also stored. The processing device 601, ROM 602, and RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0139] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 600 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 5 The electronic device 600 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0140] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program contains a program code for executing the method shown in the flowchart, thereby implementing the neural network model quantization method as described above. In such an embodiment, the computer program can be downloaded and installed from the network through the communication device 609, or installed from the storage device 608, or installed from the ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.
[0141] It should be noted that the computer-readable medium disclosed above may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.
[0142] In addition, an embodiment of the present disclosure also provides a vehicle, comprising: a memory; a processor; and a computer program; wherein the computer program is stored in the memory and is configured to be executed by the processor to implement the neural network model quantization method as described above.
[0143] In some embodiments, the client and the server may communicate using any currently known or future developed network protocol such as HTTP (HyperText Transfer Protocol), and may be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.
[0144] The computer-readable medium may be included in the electronic device, or may exist independently without being installed in the electronic device.
[0145] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device:
[0146] The neural network model is trained using 32-bit precision data to obtain a first neural network model;
[0147] Performing 16-bit precision training on the first neural network model to obtain a second neural network model;
[0148] The second neural network model is quantized with 8-bit precision to obtain a third neural network model.
[0149] Optionally, when the above one or more programs are executed by the electronic device, the electronic device may also execute other steps described in the above embodiments.
[0150] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages or a combination thereof, including, but not limited to, object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0151] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0152] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a unit does not, in some cases, constitute a limitation on the unit itself.
[0153] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0154] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0155] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other to form a technical solution.
[0156] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.
[0157] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.
Claims
1. A neural network model quantization method, characterized in that: include: The neural network model is trained using 32-bit precision data to obtain a first neural network model; Performing 16-bit precision training on the first neural network model to obtain a second neural network model; The second neural network model is quantized with 8-bit precision to obtain a third neural network model.
2. The method according to claim 1, characterized in that The neural network model is trained using 32-bit precision data to obtain a first neural network model, including: Initialize the neural network model; Get multiple sets of 32-bit precision data and preset convergence ranges; Input each group of 32-bit precision data into the initialized neural network model for training, and obtain the output results corresponding to each group of 32-bit precision data; The output results corresponding to each group of 32-bit precision data are converged to obtain the convergence value of the neural network model; According to the deviation relationship between the convergence value of the neural network model and the preset convergence range, the parameters of the neural network model are updated to obtain a first neural network model.
3. The method according to claim 2, characterized in that Before updating the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain the first neural network model, the method further includes: Determining whether the convergence value of the neural network model is within the preset convergence range; If the convergence value of the neural network model is not within the preset convergence range, the deviation relationship between the convergence value of the neural network model and the preset convergence range is calculated.
4. The method according to claim 2, characterized in that: The updating of the parameters of the neural network model according to the deviation relationship between the convergence value of the neural network model and the preset convergence range to obtain the first neural network model includes: Performing loss calculation on the convergence value of the neural network model based on the preset convergence range to obtain a loss calculation result of the neural network model; Based on the loss calculation result of the neural network model, updating the model parameters of the neural network model; If the convergence value of the updated neural network model is within the preset convergence range, a first neural network model is obtained.
5. The method according to claim 1, characterized in that The step of performing 16-bit precision training on the first neural network model to obtain a second neural network model includes: Mapping the 32-bit precision data to 16-bit precision data; The first neural network model is trained using data with 16-bit precision to obtain a second neural network model.
6. The method according to claim 1, characterized in that Before performing 16-bit precision training on the first neural network model to obtain the second neural network model, the method further includes: Performing mixed precision training of 32-bit precision and 16-bit precision on the first neural network model to obtain a fourth neural network model; The step of performing 16-bit precision training on the first neural network model to obtain a second neural network model includes: The fourth neural network model is trained with 16-bit precision to obtain a second neural network model.
7. The method according to claim 6, characterized in that The mixed precision training of 32-bit precision and 16-bit precision is performed on the first neural network model to obtain a fourth neural network model, including: Using the 32-bit precision data to train the normalization layer and the activation layer of the first neural network model; Mapping the 32-bit precision data to 16-bit precision data, and using the 16-bit precision data to train the convolutional layer and the linear layer of the first neural network model; Converging an output result of the first neural network model to obtain a convergence value of the first neural network model; Performing loss calculation on the convergence value of the first neural network model based on a preset convergence range to obtain a loss calculation result of the first neural network model; Based on the loss calculation result of the first neural network model, updating the model parameters of the first neural network model; If the updated convergence value of the first neural network model is within the preset convergence range, a fourth neural network model is obtained.
8. The method according to claim 1, characterized in that: The step of quantizing the second neural network model with 8-bit precision to obtain a third neural network model includes: Counting the weight value, activation value, maximum value, and minimum value in the second neural network model; Mapping the weight value in the second neural network model to a weight value with 8-bit precision, mapping the activation value in the second neural network model to an activation value with 8-bit precision, mapping the maximum value in the second neural network model to a maximum value with 8-bit precision, and mapping the minimum value in the second neural network model to a minimum value with 8-bit precision; The operator in the second neural network model is replaced with a quantization operator to obtain a third neural network model.
9. A neural network model quantization device, characterized in that: include: A first training module is used to train the neural network model using 32-bit precision data to obtain a first neural network model; A second training module is used to perform 16-bit precision training on the first neural network model to obtain a second neural network model; The quantization module is used to perform 8-bit precision quantization on the second neural network model to obtain a third neural network model.
10. An electronic device, characterized in that: include: Memory; processor; as well as Computer programs; The computer program is stored in the memory and is configured to be executed by the processor to implement the method according to any one of claims 1 to 8.
11. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.
