A mixed precision quantization method, device, medium and equipment
By adjusting the quantization bit width of the deep neural network model and building a quantization knowledge base, the application difficulties of the mixed-precision quantization method in unlabeled or small sample scenarios are solved, and the efficient deployment of the deep neural network model and the shortening of the response time are achieved.
Patent Information
- Application Number
- CN202411153835.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-21
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2044-08-21
AI Technical Summary
Mixed-precision quantification methods are difficult to apply in unlabeled or small-sample scenarios, resulting in high deployment costs and long response times for deep neural network models.
By determining the quantization bit width of each layer in the deep neural network model, a quantization knowledge base is constructed, and the evolutionary algorithm is used to adjust the quantization bit width and determine the candidate quantization model until the evolution end conditions are met to achieve the determination of the target quantization model.
The practicality and versatility of the mixed-precision quantification method are improved, enabling it to accurately determine the target quantification model of the deep neural network model under small sample or unlabeled conditions, reducing deployment costs and response time.
Smart Images

Figure CN118673959B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a mixed precision quantization method, device, medium and equipment. Background Art
[0002] In recent years, with the development of Internet technology, deep neural network models have become more and more mature, and their application fields have become more and more numerous. In order to avoid the deployment cost hindering the application of deep neural network models, the model quantization method can be used to reduce the deployment difficulty of deep neural network models. Among various model quantization methods, the mixed precision quantization method can quantize each layer in the model to different bit widths, reducing the loss of model accuracy on the basis of reducing deployment costs, and has become a key technology for the deployment of deep neural network models. For example, in image processing tasks, due to the relatively large amount of image data, the corresponding image processing model structure and model parameters are also large, so it is usually deployed in the server. Not only does it require additional front-end and back-end transmission time, but the model processing time is also long, resulting in slow task response. And due to the deployment requirements of the model itself, it is also difficult to deploy on the front end. Therefore, by quantizing the model, it is possible to deploy the image processing model on the front end to reduce the task response time.
[0003] However, the mixed precision quantization method usually requires a certain number of labeled samples to be input into the model in order to quantize each layer of the deep neural network model. Therefore, the mixed precision quantization method is difficult to apply in unlabeled or small sample scenarios.
[0004] To this end, this specification provides a mixed precision quantization method. Summary of the invention
[0005] The present application provides a mixed precision quantization method to partially solve the above-mentioned problems existing in the prior art.
[0006] This application provides a mixed precision quantization method, including:
[0007] The trained deep neural network model is used as a standard model, and for each parameter quantization layer in the standard model, an output quantization layer corresponding to the parameter quantization layer is determined, where different parameter quantization layers correspond to different output quantization layers;
[0008] Determine, according to a preset quantization bit width range, a first quantization bit width of the parameter quantization layer, and determine a second quantization bit width of the output quantization layer corresponding to the parameter quantization layer, and quantize the standard model according to the determined first quantization bit width and second quantization bit width to determine a candidate quantization model;
[0009] Input samples into the standard model and the candidate quantization model respectively, and determine the fitness of the candidate quantization model according to the difference between the output results of the standard model and the candidate quantization model;
[0010] Within the quantization bit width range, adjust the quantization bit width of the parameter quantization layer in the standard model, determine each priori model, and determine the quantization error of the standard model of each priori model, establish each corresponding relationship between the quantization bit width and the quantization error of each priori model, and construct a quantization knowledge base according to each corresponding relationship;
[0011] According to the preset evolutionary algorithm, based on the fitness of the candidate quantization model and the quantization knowledge base, the first quantization bit width and the second quantization bit width are adjusted, and the candidate quantization model is re-determined until the evolution end condition is met, and the target quantization model of the standard model is determined from the obtained candidate quantization models.
[0012] Optionally, samples are input into the standard model and the candidate quantization model respectively, and the fitness of the candidate quantization model is determined according to the difference between the output results of the standard model and the candidate quantization model, specifically including:
[0013] Inputting the image samples to be processed into the standard model and the candidate quantization model respectively, obtaining the standard output of the standard model, determining the standard time consumption for obtaining the standard output, obtaining the candidate output of the candidate quantization model, and determining the candidate time consumption for obtaining the candidate output;
[0014] Determine a quantization error as a fitness parameter according to a difference between the standard output and the candidate output;
[0015] Determine a speedup ratio as a fitness parameter according to the difference between the standard time consumption and the candidate time consumption;
[0016] Determine the compression effect as a fitness parameter according to the proportion of the first mean value of the first quantization bit width of each parameter quantization layer in the quantization range and the proportion of the second mean value of the second quantization bit width of each output quantization layer in the quantization range;
[0017] The fitness of the candidate quantization model is determined according to the fitness parameters.
[0018] Optionally, within the quantization bit width range, adjusting the quantization bit width of the parameter quantization layer in the standard model, determining each priori model, and determining the quantization error of the standard model of each priori model, establishing each corresponding relationship between the quantization bit width and the quantization error of each priori model, and constructing a quantization knowledge base according to each corresponding relationship, specifically including:
[0019] Determine a priori value in the quantization bit width range, and quantize each parameter quantization layer of the standard model according to the priori value to obtain a priori model;
[0020] Determining the quantization error between each prior model and the standard model;
[0021] Determine each sensitivity of each prior model based on each quantization error determined by each prior model, and construct a quantization knowledge base according to each sensitivity of each prior model;
[0022] According to the fitness of the candidate quantization model and the quantization knowledge base, adjusting the first quantization bit width and the second quantization bit width specifically includes:
[0023] According to the fitness of the candidate quantization model, and in comparison with the sensitivity of each parameter quantization layer in the quantization knowledge base, the first quantization bit width and the second quantization bit width are adjusted.
[0024] Optionally, adjusting the first quantization bit width and the second quantization bit width according to the fitness of the candidate quantization model and the quantization knowledge base specifically includes:
[0025] Determining a variation parameter of the first quantization bit width and a variation parameter of the second quantization bit width according to each quantization error in the quantization knowledge base;
[0026] According to the fitness of the candidate quantization model and each variation parameter, and with reference to the standard model, the first quantization bit width and the second quantization bit width are adjusted.
[0027] Optionally, determining a first quantization bit width of the parameter quantization layer according to a preset quantization bit width range, and determining a second quantization bit width of an output quantization layer corresponding to the parameter quantization layer specifically includes:
[0028] Determine, according to a preset third bit width of the parameter quantization layer, a first quantization bit width that is smaller than the third bit width;
[0029] An output quantization layer corresponding to the parameter quantization layer is determined, and according to a preset fourth bit width of the output quantization layer, a second quantization bit width that is smaller than the fourth bit width is determined.
[0030] Optionally, determining the compression effect according to a proportion of a first mean value of a first quantization bit width of each parameter quantization layer in the quantization range and a proportion of a second mean value of a second quantization bit width of each output quantization layer in the quantization range specifically includes:
[0031] Determine a first mean value of the candidate quantization model according to a first quantization bit width of each parameter quantization layer in the candidate quantization model, and determine a second mean value of the candidate quantization model according to a second quantization bit width of each output quantization layer in the candidate quantization model;
[0032] Calculating a ratio of the first mean value to an upper limit of the quantization range to determine a first ratio, and calculating a ratio of the second mean value to an upper limit of the quantization range to determine a second ratio;
[0033] A compression effect is determined according to the sum of the first ratio and the second ratio.
[0034] Optionally, the samples are a small amount of unlabeled samples.
[0035] A mixed precision quantization device, comprising:
[0036] A setting module uses the trained deep neural network model as a standard model, and determines, for each parameter quantization layer in the standard model, an output quantization layer corresponding to the parameter quantization layer, where different parameter quantization layers correspond to different output quantization layers;
[0037] A quantization module, which determines a first quantization bit width of the parameter quantization layer according to a preset quantization bit width range, and determines a second quantization bit width of the output quantization layer corresponding to the parameter quantization layer, and quantizes the standard model according to the determined first quantization bit width and second quantization bit width to determine a candidate quantization model;
[0038] A comparison module, inputting samples into the standard model and the candidate quantization model respectively, and determining the fitness of the candidate quantization model according to the difference between the output results of the standard model and the candidate quantization model;
[0039] A construction module, within the quantization bit width range, adjusts the quantization bit width of the parameter quantization layer in the standard model, determines each priori model, determines the quantization error of the standard model of each priori model, establishes each corresponding relationship between the quantization bit width and the quantization error of each priori model, and constructs a quantization knowledge base according to each corresponding relationship;
[0040] The evolution module adjusts the first quantization bit width and the second quantization bit width according to the fitness of the candidate quantization model and the quantization knowledge base in accordance with a preset evolution algorithm, and redetermines the candidate quantization model until the evolution end condition is met, and determines the target quantization model of the standard model from the obtained candidate quantization models.
[0041] An electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned mixed precision quantization method when executing the program.
[0042] At least one of the above technical solutions adopted in this specification can achieve the following beneficial effects:
[0043] A mixed precision quantization method is provided in this specification. By determining the quantization bit width of each layer in the deep neural network model, the deep neural network model is quantized, and a candidate quantization model is determined. Samples are input into the standard model and the candidate quantization model respectively to determine the quantization error, acceleration ratio and compression effect of the standard model and the candidate quantization model, and then the fitness of the candidate quantization model is determined. Finally, the target quantization model of the standard model is determined according to the preset evolutionary algorithm and the fitness of the candidate quantization model.
[0044] It can be seen from the above method that by repeatedly determining new candidate quantization models through fitness, the mixed precision quantization method can accurately determine the target quantization model of the deep neural network model based on small samples or unlabeled samples, which greatly improves the practicality and versatility of the mixed precision quantization method. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] The drawings described herein are used to provide a further understanding of this specification and constitute a part of this specification. The illustrative embodiments and descriptions of this specification are used to explain this specification and do not constitute an improper limitation on this specification. In the drawings:
[0046] Figure 1 A flow chart of a mixed precision quantization method provided in this specification;
[0047] Figure 2 A schematic diagram of a distillation process of a mixed precision quantization method provided in this specification;
[0048] Figure 3 A schematic diagram of a mixed precision quantization device provided in this specification;
[0049] Figure 4 A schematic diagram of an electronic device provided in this manual. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical solutions and advantages of this specification more clear, the technical solutions of this specification will be clearly and completely described below in combination with the specific embodiments of this specification and the corresponding drawings. Obviously, the described embodiments are only part of the embodiments of this specification, not all of the embodiments. Based on the embodiments in this specification, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0051] It should be noted that the mixed precision quantization method provided in this specification can be executed by a computer or a server, and the specific subject of the execution method is not limited in this specification. And for the convenience of description in this specification, this specification takes the execution of the mixed precision quantization method by a server as an example for description.
[0052] The technical solutions provided by the embodiments of this specification are described in detail below in conjunction with the accompanying drawings.
[0053] Figure 1 A flow chart of a mixed precision quantization method provided for this specification:
[0054] S101: Use the trained deep neural network model as the standard model, and for each parameter quantization layer in the standard model, determine the output quantization layer corresponding to the parameter quantization layer of the standard model, where different parameter quantization layers correspond to different output quantization layers.
[0055] Currently, the deployment of deep neural network models usually requires the focus on the deployment platform, and the training of deep neural network models is often handled by different departments, so the samples used for model deployment usually need to be obtained separately. The mixed precision quantization method in model deployment needs to determine the sensitivity of each layer in the model, and quantize each layer in the model to different bit widths according to the sensitivity of each layer, which requires a large number of labeled samples.
[0056] In one or more embodiments of the present specification, the trained deep neural network model is used as the standard model. Each parameter quantization layer in the standard model is determined, and each output quantization layer in the standard model is determined non-repeatedly, and multiple output quantization layers corresponding to each parameter quantization layer are determined according to the preset correspondence between the parameter quantization layer and the output quantization layer. Wherein, the bit width of the standard model is floating point data.
[0057] In addition, in the embodiments of this specification, the trained deep neural network model is constructed and trained based on the requirements of deployment in the second terminal, where the second terminal is usually a server, or a computing center composed of distributed servers. The most important target quantization model obtained in the embodiments of this specification is a model that needs to be deployed in the first terminal, and image processing is performed in the first terminal. In addition, the performance of the first terminal is lower than that of the second terminal, which is usually a personal computer or a mobile terminal.
[0058] Each neuron layer in the trained deep neural network model can weight and calculate the input data and input the calculation results into the subsequent neuron layer. When the model is quantized and used as a standard model, each neuron layer is used as a parameter quantization layer, and an output quantization layer is added after each parameter quantization layer. Each output quantization layer quantizes the output of the parameter quantization layer before it. At present, due to quantization, the amount of data processing is relatively small compared to the weighting and calculation of the neuron layer, and even the amount of calculation for bit width adjustment can be approximately ignored. In other words, each parameter quantization layer outputs the image features of the input image sample, and the output quantization layer quantizes the image features again, but fundamentally it is still the image features of the sample image. In the embodiment of this specification, it is necessary to reduce the amount of calculation while ensuring the accuracy of the final output result of the model through subsequent steps and quantization of the model parameters and the data transmitted in the middle of the model.
[0059] It should be noted that this standard model is a model for image processing, which includes at least one of image classification, object detection, semantic segmentation and image recognition. Image classification refers to determining which of the predefined categories a picture belongs to. For example, given a photo of a dog, determine whether it is a corgi or a husky. Object detection refers to not only identifying the category of objects in the image, but also giving the precise location of these objects in the image, usually represented by a bounding box. For example, in a picture containing multiple people, find the location of each person and mark each person. Semantic segmentation refers to classifying each pixel in the image to determine which category it belongs to. For example, in a picture of a city street, distinguish each part such as roads, sidewalks, buildings, trees, etc. Image recognition can generally be understood as identifying meaningful information from an image, which may include classification, detection or more complex understanding tasks. For example, in a picture containing handwritten numbers, identify what each number is.
[0060] In the embodiments of this specification, there is no restriction on what specific image processing tasks the standard model is used for, nor is there any restriction on what samples are used in the aforementioned training process to train the standard model. However, it is certain that the standard model is used for image processing, and its input is image data, and its output is the image processing result of the corresponding task, such as classification results, bounding boxes for target recognition, etc.
[0061] S103: Determine the first quantization bit width of the parameter quantization layer and the second quantization bit width of the output quantization layer corresponding to the parameter quantization layer according to a preset quantization bit width range, and quantize the standard model according to the determined first quantization bit width and second quantization bit width to determine the candidate quantization model.
[0062] Through the quantization target of the standard model, that is, the preset upper limit bit width, one or more quantization strategies of the standard model are determined, and the quantization results corresponding to each strategy are determined, so that each strategy can be evaluated according to the parameters of the quantization results in subsequent steps.
[0063] In one or more embodiments of the present specification, the server determines a first quantization bit width smaller than the third bit width according to a third bit width of a preset parameter quantization layer, determines an output quantization layer corresponding to the parameter quantization layer, and determines a second quantization bit width smaller than the fourth bit width according to a fourth bit width of the preset output quantization layer.
[0064] Specifically, first determine the number of layers of the standard model as , and then determine multiple minimum quantization units consisting of each parameter quantization layer and the output quantization layer corresponding to the parameter quantization layer. Specify the upper limit of the optimized bit width of the parameter quantization layer and the output quantization layer corresponding to the parameter quantization layer as the third optimized bit width and the fourth optimized bit width , repeat the random initialization operation under the constraint, and get the population size The initial population:
[0065]
[0066] Among them, the superscript represents the population generation number, Indicates the first generation of the population i Individuals:
[0067]
[0068] For example, when the quantization bit width is 3, 4, 5, 6, 7, or 8, the first bit width of the parameter quantization layer and the second bit width of the output quantization layer satisfy the following constraints:
[0069]
[0070]
[0071] in, and Represents the average quantization bit width of weight or activation, respectively. and Respectively represent l The number of weight parameters and activation parameters of the layer.
[0072] In addition, a folding operation is performed on the standard model, the batch normalization layer is merged into the convolutional layer or the linear layer, and then a quantization operation is performed on the standard model according to the quantization strategy to obtain each candidate quantization model. The quantization operation method includes but is not limited to symmetric quantization, asymmetric quantization, and exponential quantization. Generally, asymmetric quantization is used for the first bit width, and symmetric quantization is used for the second bit width.
[0073] S105: Input the image samples to be processed into the standard model and the candidate quantization model respectively, and determine the fitness of the candidate quantization model according to the difference between the image processing results of the standard model and the candidate quantization model. The fitness is used to characterize the accuracy of the image processing results of the candidate quantization model.
[0074] The image samples to be processed are input into the standard model and the candidate quantization model respectively, and the fitness of the candidate quantization model is determined by obtaining the difference between the image processing results of the standard model and the candidate quantization model, so as to evaluate each candidate quantization model. The fitness is used to characterize the accuracy of the image processing results of the candidate quantization model.
[0075] For example, if the standard model is an image classification model, the image samples are input into the standard model to obtain the image classification results of the standard model, and the image samples are input into the candidate quantization model to obtain the image classification results of the candidate quantization model. The greater the difference between the image classification results output by the candidate quantization model and the standard model, the lower the fitness of the obtained candidate quantization model.
[0076] Of course, the embodiments of this specification also provide a more detailed fitness calculation method, which can be specifically described in the subsequent description. This specification does not limit the specific method used to calculate the fitness, as long as it can characterize the accuracy of the image processing results.
[0077] S107: Within the quantization bit width range, adjust the quantization bit width of the parameter quantization layer in the standard model, determine each priori model, and determine the quantization error of the standard model of each priori model, establish each corresponding relationship between the quantization bit width and the quantization error of each priori model, and construct a quantization knowledge base based on the each corresponding relationship.
[0078] In order to obtain the required quantization model more quickly in the subsequent steps, the quantization construction knowledge base for reference is first determined, and then the evolution conditions are determined based on the constructed quantization knowledge base, so as to more quickly determine the target quantization model from the candidate quantization models. The construction of the quantization knowledge base is completed by determining the impact of the quantization error on the candidate quantization model.
[0079] It should be noted that the knowledge base, or knowledge base system, is a system that interprets input data based on the data stored therein, or generates and verifies hypotheses based on the input data. By interpreting or verifying the knowledge base, each variation parameter can be quickly determined to obtain the target quantitative model from the candidate quantitative models more quickly.
[0080] S109: According to the preset evolutionary algorithm, based on the fitness of the candidate quantization model and comparing the sensitivity of each parameter quantization layer in the quantization knowledge base, the first quantization bit width and the second quantization bit width are adjusted, and the candidate quantization model is re-determined until the evolution end condition is met, and the target quantization model of the standard model is determined from the obtained candidate quantization models.
[0081] The server uses a preset evolutionary algorithm to compare the sensitivity of each parameter quantization layer in the quantization knowledge base, re-determines the candidate quantization model through the fitness of each candidate quantization model, and obtains the optimal target quantization model after multiple iterations. Wherein, the sensitivity is a set of quantization errors. When the bit width is adjusted by sensitivity, the server can increase the first quantization bit width and the second quantization bit width when the set of quantization errors represented by the sensitivity increases to ensure the model accuracy, and reduce the first quantization bit width and the second quantization bit width when the set of quantization errors represented by the sensitivity decreases to reduce the model parameters and minimize the model deployment cost.
[0082] In one or more embodiments of this specification, a selected As a target quantitative strategy, As the base quantitative strategy, and randomly select and Calculate the difference vector and get the mutation quantization strategy:
[0083]
[0084] in, , p, q are different from each other and are related to different, is the scaling factor, usually 0.5, is a rounding function constructed using the quantization knowledge base. The above rounding function can greatly improve the search efficiency. Specifically, according to the current quantization strategy, the quantization knowledge base is searched to obtain the quantization sensitivity information of different layers under the current quantization strategy. All layers are sorted according to the size of the quantization sensitivity. k The layer quantization bit width is rounded up, and then k The quantized bit width of the layer is rounded down, and the remaining layers are randomly rounded up or down. k Can be set to 3.
[0085] The variation quantification strategy and target quantitative strategy Perform crossover to obtain a crossover quantization strategy:
[0086]
[0087] Among them, rand(j) represents a uniformly distributed random number between [0,1], CR is the crossover probability in the range [0,1], Represents a random vector between [1,2L]; CR can be set to a fixed value, such as 0.75, or a random value. The specific formula is as follows:
[0088] CR=0.5(1 + rand())
[0089] The fitness of the cross quantization strategy is calculated by using the evaluation module. If its fitness is higher than that of the selected quantization strategy, it is used as an individual of the new population, and the selected quantization strategy is removed to obtain a new generation of population.
[0090] It should be noted that the number of iterations is greatly affected by the number of layers in the neural network. That is, the more layers there are in the network, the larger the search space is, and the more search time is required. However, in most cases, 1,000 iterations are sufficient to complete the optimization, and the corresponding entire search process can be completed within half an hour.
[0091] based on Figure 1 The provided mixed precision quantization method enables the mixed precision quantization method to quickly determine the target quantization model of the deep neural network model based on small samples or unlabeled samples, greatly improving the practicality and versatility of the mixed precision quantization method.
[0092] In step S105, in order to more accurately determine the fitness, first, the image samples to be processed are input into the standard model and the candidate quantization model respectively, and the standard output of the standard model, that is, the standard image processing result, is obtained, and the standard time consumption for obtaining the standard output is determined, and the candidate output of the candidate quantization model, that is, the image processing result, is obtained, and the candidate time consumption for obtaining the candidate output is determined. Secondly, according to the difference between the standard output and the candidate output, the quantization error is determined as a fitness parameter. Thirdly, according to the difference between the standard time consumption and the candidate time consumption, the acceleration ratio is determined as a fitness parameter. Thirdly, according to the proportion of the first mean value of the first quantization bit width of each parameter quantization layer in the quantization range, and the proportion of the second mean value of the second quantization bit width of each output quantization layer in the quantization range, the compression effect is determined as a fitness parameter. Finally, according to each fitness parameter, the fitness of the candidate quantization model is determined.
[0093] Specifically, each fitness parameter is determined according to the output results of the standard model and the candidate quantization model, that is, the quantization error between the output results of the standard model and the candidate quantization model is first determined. Then the speedup ratio between the output time of the standard model and the candidate quantization model is determined. Finally, the compression effect is determined according to the proportion of the first mean value of the first quantization bit width of each activated layer in the quantization range and the proportion of the second mean value of the second quantization bit width of each inactive layer in the quantization range, wherein the compression effect represents the degree of compression of the activated layer.
[0094] In one or more embodiments of this specification, for each candidate quantization model, the formula for calculating the quantization error is as follows:
[0095]
[0096] Where n is the number of samples, represents the intermediate layer features or outputs of the original neural network model, Represents the intermediate layer features or outputs of the initial quantization model.
[0097] It should be noted here that the number of samples n in this method is about 50 to achieve a quantization effect with less loss.
[0098] Each candidate quantization model is deployed on a specified hardware platform, and the formula for calculating the speedup ratio is as follows:
[0099]
[0100] in, and Represent the hardware delays of the candidate quantization model and the standard model respectively.
[0101] For each candidate quantization model, the formula for calculating the compression effect is as follows:
[0102]
[0103] Correspondingly, the fitness is determined according to the quantization error, the acceleration ratio, and the compression effect as fitness parameters.
[0104] Specifically, for each candidate quantization model, the formula for calculating fitness is as follows:
[0105]
[0106] in, represents the weight of the quantization error, represents the weight of the compression effect, Indicates the weight of hardware delay, for example, the values can be 1, 0.2, and 0.5 respectively.
[0107] In step S107, in order to build a more complete quantization knowledge base, first, a priori value is determined within the quantization bit width range, and for each parameter quantization layer of the standard model, the parameter quantization layer is quantized according to the priori value to obtain a priori model. Secondly, the quantization error between each priori model and the standard model is determined. Finally, the sensitivity of each priori model is determined based on each quantization error determined by each priori model, and a quantization knowledge base is constructed according to each sensitivity of each priori model.
[0108] In order to obtain the required quantitative model more quickly in the subsequent steps, the server first determines the quantitative construction knowledge base for reference, and then determines the evolution conditions based on the constructed quantitative knowledge base, so as to more quickly determine the target quantitative model from each candidate two-household model.
[0109] Specifically, first, for each layer in the standard model, the bit width of the layer is independently determined, and the standard model is quantized to obtain each prior model. And each quantization error between each prior model and the standard model is determined, and according to each quantization error, a quantization knowledge base is constructed, that is, the quantization error between each prior model and the standard model is determined.
[0110] Secondly, the preset samples are respectively input into the standard model and each prior model to obtain the calibration output of the standard model and each reference output of the prior model output. According to the difference between each reference output and the calibration output, the value corresponding to each reference output is determined as the sensitivity of the standard model.
[0111] Then, according to the sensitivity of each layer in the standard model, the knowledge base of the standard model is determined, wherein the index of the quantization knowledge base is composed of the first bit width and the third bit width. Only the corresponding network layer is quantized to obtain a layer quantization model, and a small amount of unlabeled data is used as input. The quantization sensitivity is obtained by calculating the output difference between the standard model and the layer quantization model. Then, according to the quantization knowledge base, the network layer or network block is used as the first index, and the quantization bit width is used as the second index.
[0112] Correspondingly, in step S109, in order to make full use of the constructed quantization knowledge base, the server adjusts the first quantization bit width and the second quantization bit width according to the fitness of the candidate quantization model and the sensitivity of each parameter quantization layer in the quantization knowledge base.
[0113] In step S103, in order to more accurately determine the first bit width and the second bit width, the server determines a first quantization bit width that is smaller than the third bit width based on the preset third bit width of the parameter quantization layer, and determines an output quantization layer corresponding to the parameter quantization layer, and determines a second quantization bit width that is smaller than the fourth bit width based on the preset fourth bit width of the output quantization layer.
[0114] In step S107, in order to more accurately determine the compression effect, the server determines the first mean of the candidate quantization model according to the first quantization bit width of each parameter quantization layer in the candidate quantization model, and determines the second mean of the candidate quantization model according to the second quantization bit width of each output quantization layer in the candidate quantization model; calculates the ratio of the first mean to the upper limit of the quantization range to determine the first ratio, calculates the ratio of the second mean to the upper limit of the quantization range to determine the second ratio; determines the compression effect according to the sum of the first ratio and the second ratio.
[0115] In step S105, the method can obtain a candidate quantization model based on a small number of samples or unlabeled samples. The server only needs to input a small sample into the standard model and the candidate quantization model, or input an unlabeled sample into the standard model and the candidate quantization model.
[0116] In one or more embodiments of the present specification, before determining the target quantization model in step S117, a method such as Figure 2 The distillation algorithm shown distills the selected quantization model to obtain a target quantization model with smaller loss.
[0117] Specifically, the teacher-student framework is used, with the standard model as the teacher and the quantized model as the student. Unlabeled data is used as input, and the parameters of the quantized model are optimized by optimizing the intermediate layer and the output difference. The optimization is performed according to the target loss function. The specific formula of the target function is as follows:
[0118]
[0119] in, represents the target loss function, represents the number of network layers used for optimization, Indicates The impact factor of the selected middle layer, represents the intermediate layer features or outputs of the standard model, represents the intermediate layer features or outputs of the initial quantization model, the second term is the regularization term, W represents the parameters of the standard model, represents the parameters of the quantized model, is the regularization parameter, and its value can be set to 10 -4 .
[0120] This specification also provides a schematic diagram of a mixed precision quantization device, wherein the standard model used for quantization by the device is a model for image processing, wherein the image processing includes at least one of image classification, object detection, semantic segmentation, and image recognition, such as Figure 3 As shown:
[0121] The setting module 301 uses the trained deep neural network model as a standard model, and determines the output quantization layer corresponding to each parameter quantization layer in the standard model, where different parameter quantization layers correspond to different output quantization layers;
[0122] The quantization module 303 determines the first quantization bit width of the parameter quantization layer according to a preset quantization bit width range, and determines the second quantization bit width of the output quantization layer corresponding to the parameter quantization layer, and quantizes the standard model according to the determined first quantization bit width and second quantization bit width to determine the candidate quantization model;
[0123] The comparison module 305 inputs the image sample to be processed into the standard model and the candidate quantization model respectively, and determines the fitness of the candidate quantization model according to the difference between the image processing results of the standard model and the candidate quantization model, wherein the fitness is at least used to characterize the accuracy of the image processing result of the candidate quantization model;
[0124] A construction module 307 is configured to adjust the quantization bit width of the parameter quantization layer in the standard model within the quantization bit width range, determine each priori model, determine the quantization error of the standard model of each priori model, establish each corresponding relationship between the quantization bit width and the quantization error of each priori model, and construct a quantization knowledge base according to each corresponding relationship;
[0125] The evolution module 309 adjusts the first quantization bit width and the second quantization bit width according to the fitness of the candidate quantization model and the quantization knowledge base in accordance with the preset evolution algorithm, and re-determines the candidate quantization model until the evolution end condition is met, and determines the target quantization model of the standard model from the obtained candidate quantization models, and the target quantization model is used to be deployed in the first terminal for image processing, and the performance of the first terminal is lower than that of the second terminal where the standard model is deployed.
[0126] Optionally, a comparison module 305 is used to input the image samples to be processed into the standard model and the candidate quantization model respectively, to obtain a standard image processing result of the standard model, to determine a standard time consumption for obtaining the standard image processing result, to obtain a candidate image processing result of the candidate quantization model, to determine a candidate time consumption for obtaining the candidate image processing result, to determine a quantization error based on a difference between the standard image processing result and the candidate image processing result, to determine an acceleration ratio based on a difference between the standard time consumption and the candidate time consumption, to determine a compression effect based on a proportion of a first mean value of a first quantization bit width of each parameter quantization layer in the quantization range and a proportion of a second mean value of a second quantization bit width of each output quantization layer in the quantization range, to use the quantization error, the acceleration ratio and the compression effect as different fitness parameters, respectively, and to determine the fitness of the candidate quantization model based on the determined fitness parameters.
[0127] Correspondingly, construction module 307 is used to determine the prior value within the quantization bit width range, and for each parameter quantization layer of the standard model, quantize the parameter quantization layer according to the prior value to obtain the prior model, determine the quantization error between each prior model and the standard model, determine the sensitivity of each prior model based on the quantization errors determined by each prior model, and construct a quantization knowledge base according to the sensitivity of each prior model.
[0128] Correspondingly, the evolution module 309 is used to adjust the first quantization bit width and the second quantization bit width according to the fitness of the candidate quantization model and the sensitivity of each parameter quantization layer in the quantization knowledge base.
[0129] Optionally, the quantization module 303 is used to determine the variation parameters of the first quantization bit width and the variation parameters of the second quantization bit width according to the quantization errors in the quantization knowledge base, and adjust the first quantization bit width and the second quantization bit width according to the fitness of the candidate quantization model and the variation parameters with reference to the standard model.
[0130] Optionally, the comparison module 305 is used to determine the first mean of the candidate quantization model according to the first quantization bit width of each parameter quantization layer in the candidate quantization model, and determine the second mean of the candidate quantization model according to the second quantization bit width of each output quantization layer in the candidate quantization model, calculate the ratio of the first mean to the upper limit of the quantization range to determine the first ratio, calculate the ratio of the second mean to the upper limit of the quantization range to determine the second ratio, and determine the compression effect according to the sum of the first ratio and the second ratio.
[0131] Optionally, the image samples to be processed used by each module are a small number of unlabeled samples.
[0132] The present specification also provides a computer-readable storage medium, which stores a computer program. The computer program can be used to execute the above hybrid quantization method.
[0133] This manual also provides Figure 4 The one shown corresponds to Figure 1 A schematic diagram of the electronic device. Figure 4 At the hardware level, the electronic device includes a processor, an internal bus, a network interface, a memory, and a non-volatile memory, and may also include other hardware required for the business. The processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it to achieve the above Figure 1 Of course, in addition to the software implementation, this specification does not exclude other implementations, such as logic devices or a combination of software and hardware, etc., that is, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0134] In the 1990s, it was very clear whether the improvement of a technology was hardware improvement (for example, improvement of the circuit structure of diodes, transistors, switches, etc.) or software improvement (improvement of the method flow). However, with the development of technology, many improvements of the method flow today can be regarded as direct improvements of the hardware circuit structure. Designers almost always obtain the corresponding hardware circuit structure by programming the improved method flow into the hardware circuit. Therefore, it cannot be said that the improvement of a method flow cannot be implemented with hardware entity modules. For example, a programmable logic device (PLD) (such as a field programmable gate array (FPGA)) is such an integrated circuit whose logical function is determined by the user's programming of the device. Designers can "integrate" a digital system on a PLD by programming it themselves, without having to ask chip manufacturers to design and make dedicated integrated circuit chips. Moreover, nowadays, instead of manually making integrated circuit chips, this kind of programming is mostly implemented by "logic compiler" software, which is similar to the software compiler used when developing and writing programs, and the original code before compilation must also be written in a specific programming language, which is called hardware description language (HDL). There is not only one kind of HDL, but many kinds, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc. The most commonly used ones are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should also know that it is only necessary to program the method flow slightly in the above-mentioned hardware description languages and program it into the integrated circuit, and then it is easy to obtain the hardware circuit that implements the logic method flow.
[0135] The controller may be implemented in any suitable manner, for example, the controller may take the form of a microprocessor or processor and a computer-readable medium storing a computer-readable program code (e.g., software or firmware) executable by the (micro)processor, a logic gate, a switch, an application-specific integrated circuit (ASIC), a programmable logic controller, and an embedded microcontroller, examples of which include but are not limited to the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320, and the memory controller may also be implemented as part of the control logic of the memory. It is also known to those skilled in the art that, in addition to implementing the controller in a purely computer-readable program code manner, the controller may be implemented in the form of a logic gate, a switch, an application-specific integrated circuit, a programmable logic controller, and an embedded microcontroller by logically programming the method steps. Therefore, such a controller may be considered as a hardware component, and the devices for implementing various functions included therein may also be considered as structures within the hardware component. Or even, the devices for implementing various functions may be considered as both software modules for implementing the method and structures within the hardware component.
[0136] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a sensor phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0137] For the convenience of description, the above device is described in various units according to their functions. Of course, when implementing this specification, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0138] Those skilled in the art will appreciate that the embodiments of this specification may be provided as methods, systems, or computer program products. Therefore, this specification may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, this specification may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0139] This specification is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of this specification. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0140] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0141] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0142] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0143] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0144] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
[0145] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0146] It should be understood by those skilled in the art that the embodiments of this specification may be provided as methods, systems or computer program products. Therefore, this specification may take the form of a complete hardware embodiment, a complete software embodiment or an embodiment combining software and hardware. Moreover, this specification may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0147] This specification may be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. This specification may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules may be located in local and remote computer storage media, including storage devices.
[0148] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0149] The above description is only an embodiment of the present specification and is not intended to limit the present specification. For those skilled in the art, the present specification may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present specification shall be included in the scope of the claims of the present specification.
Claims
1. A mixed precision quantization method, It is characterized in that The standard model used for quantification in the method is a model for image processing, wherein the image processing includes at least one of image classification, object detection, semantic segmentation, and image recognition, and the method includes: The trained deep neural network model is used as a standard model, and for each parameter quantization layer in the standard model, an output quantization layer corresponding to the parameter quantization layer is determined, where different parameter quantization layers correspond to different output quantization layers; Determine, according to a preset quantization bit width range, a first quantization bit width of the parameter quantization layer, and determine a second quantization bit width of the output quantization layer corresponding to the parameter quantization layer, and quantize the standard model according to the determined first quantization bit width and second quantization bit width to determine a candidate quantization model; Inputting the image samples to be processed into the standard model and the candidate quantization model respectively, and determining the fitness of the candidate quantization model according to the difference between the image processing results of the standard model and the candidate quantization model, wherein the fitness is used to characterize the accuracy of the image processing results of the candidate quantization model; Within the quantization bit width range, adjust the quantization bit width of the parameter quantization layer in the standard model, determine each priori model, and determine the quantization error of the standard model of each priori model, establish each corresponding relationship between the quantization bit width and the quantization error of each priori model, and construct a quantization knowledge base according to each corresponding relationship; According to a preset evolutionary algorithm, based on the fitness of the candidate quantization model and the quantization knowledge base, the first quantization bit width and the second quantization bit width are adjusted, and the candidate quantization model is re-determined until the evolution end condition is met, and the target quantization model of the standard model is determined from the obtained candidate quantization models, and the target quantization model is used to be deployed in a first terminal for image processing, and the performance of the first terminal is lower than that of a second terminal where the standard model is deployed.
2. The method according to claim 1, It is characterized in that Inputting the image samples to be processed into the standard model and the candidate quantization model respectively, and determining the fitness of the candidate quantization model according to the difference between the image processing results of the standard model and the candidate quantization model, specifically including: Inputting the image samples to be processed into the standard model and the candidate quantization model respectively, obtaining the standard image processing result of the standard model, determining the standard time consumption for obtaining the standard image processing result, obtaining the candidate image processing result of the candidate quantization model, and determining the candidate time consumption for obtaining the candidate image processing result; Determining a quantization error according to a difference between the standard image processing result and the candidate image processing result; Determine a speedup ratio according to a difference between the standard time consumption and the candidate time consumption; Determine the compression effect according to the proportion of the first mean value of the first quantization bit width of each parameter quantization layer in the quantization range and the proportion of the second mean value of the second quantization bit width of each output quantization layer in the quantization range; The quantization error, the acceleration ratio and the compression effect are respectively used as different fitness parameters, and the fitness of the candidate quantization model is determined according to the determined fitness parameters.
3. The method according to claim 1, It is characterized in that Within the quantization bit width range, adjusting the quantization bit width of the parameter quantization layer in the standard model, determining each priori model, and determining the quantization error of the standard model of each priori model, establishing each corresponding relationship between the quantization bit width and the quantization error of each priori model, and constructing a quantization knowledge base according to each corresponding relationship, specifically including: Determine a priori value in the quantization bit width range, and quantize each parameter quantization layer of the standard model according to the priori value to obtain a priori model; Determining the quantization error between each prior model and the standard model; Determine each sensitivity of each prior model based on each quantization error determined by each prior model, and construct a quantization knowledge base according to each sensitivity of each prior model; According to the fitness of the candidate quantization model and the quantization knowledge base, adjusting the first quantization bit width and the second quantization bit width specifically includes: According to the fitness of the candidate quantization model, and in comparison with the sensitivity of each parameter quantization layer in the quantization knowledge base, the first quantization bit width and the second quantization bit width are adjusted.
4. The method according to claim 1, It is characterized in that According to the fitness of the candidate quantization model and the quantization knowledge base, adjusting the first quantization bit width and the second quantization bit width specifically includes: Determining a variation parameter of the first quantization bit width and a variation parameter of the second quantization bit width according to each quantization error in the quantization knowledge base; According to the fitness of the candidate quantization model and each variation parameter, and with reference to the standard model, the first quantization bit width and the second quantization bit width are adjusted.
5. The method according to claim 1, It is characterized in that Determining a first quantization bit width of the parameter quantization layer according to a preset quantization bit width range, and determining a second quantization bit width of an output quantization layer corresponding to the parameter quantization layer, specifically includes: Determine, according to a preset third bit width of the parameter quantization layer, a first quantization bit width that is smaller than the third bit width; An output quantization layer corresponding to the parameter quantization layer is determined, and according to a preset fourth bit width of the output quantization layer, a second quantization bit width that is smaller than the fourth bit width is determined.
6. The method according to claim 2, It is characterized in that Determining the compression effect according to the proportion of the first mean value of the first quantization bit width of each parameter quantization layer in the quantization range and the proportion of the second mean value of the second quantization bit width of each output quantization layer in the quantization range specifically includes: Determine a first mean value of the candidate quantization model according to a first quantization bit width of each parameter quantization layer in the candidate quantization model, and determine a second mean value of the candidate quantization model according to a second quantization bit width of each output quantization layer in the candidate quantization model; Calculating a ratio of the first mean value to an upper limit of the quantization range to determine a first ratio, and calculating a ratio of the second mean value to an upper limit of the quantization range to determine a second ratio; A compression effect is determined according to the sum of the first ratio and the second ratio.
7. The method according to any one of claims 1 to 6, It is characterized in that The image samples to be processed are a small number of unlabeled samples.
8. A mixed precision quantization device, It is characterized in that The standard model used for quantification by the device is a model for image processing, wherein the image processing includes at least one of image classification, object detection, semantic segmentation, and image recognition, and the device includes: A setting module uses the trained deep neural network model as a standard model, and determines, for each parameter quantization layer in the standard model, an output quantization layer corresponding to the parameter quantization layer, where different parameter quantization layers correspond to different output quantization layers; A quantization module, which determines a first quantization bit width of the parameter quantization layer according to a preset quantization bit width range, and determines a second quantization bit width of the output quantization layer corresponding to the parameter quantization layer, and quantizes the standard model according to the determined first quantization bit width and second quantization bit width to determine a candidate quantization model; A comparison module, inputting the image samples to be processed into the standard model and the candidate quantization model respectively, and determining the fitness of the candidate quantization model according to the difference between the image processing results of the standard model and the candidate quantization model, wherein the fitness is at least used to characterize the accuracy of the image processing results of the candidate quantization model; A construction module, within the quantization bit width range, adjusts the quantization bit width of the parameter quantization layer in the standard model, determines each priori model, determines the quantization error of the standard model of each priori model, establishes each corresponding relationship between the quantization bit width and the quantization error of each priori model, and constructs a quantization knowledge base according to each corresponding relationship; The evolution module adjusts the first quantization bit width and the second quantization bit width according to the fitness of the candidate quantization model and the quantization knowledge base in accordance with a preset evolutionary algorithm, and redetermines the candidate quantization model until the evolution end condition is met, and determines the target quantization model of the standard model from the obtained candidate quantization models, the target quantization model is used to be deployed in a first terminal for image processing, and the performance of the first terminal is lower than that of a second terminal where the standard model is deployed.
9. A computer storage medium, It is characterized in that, The storage medium stores a computer program, and when the computer program is executed by a processor, the method described in any one of claims 1 to 7 is implemented.
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the program, the method described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Method for performing multi-bit-width quantization on deep convolutional neural network
CN113762489A
Target detection method and device, electronic equipment, medium and product
CN114897062A
Method and device for generating in-memory computing neural network model and medium
CN117077726A
Model quantification method, device and equipment based on resistive random access memory
CN117787358A
IMAGE RECOGNITION METHOD, APPARATUS, DEVICE AND STORAGE MEDIUM
JP7459357B1
