Neural network model quantification method and device and electronic equipment

By processing nonlinear operators through quantization strategies and polynomial approximation methods for different operators, the problem of low quantification efficiency of nonlinear operators in the existing technology is solved, and the quantification accuracy and efficiency of the neural network model are improved, which is suitable for resource-limited applications. equipment.

CN118709732APending Publication Date: 2024-09-27VIVO MOBILE COMM CO LTD

Patent Information

Application Number
CN202410849291.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-27
Publication Date
2024-09-27

AI Technical Summary

Technical Problem

Existing neural network model quantification technology is difficult to effectively handle nonlinear operators, resulting in low quantification accuracy and efficiency. Especially in Transformer structure network models, traditional methods rely on linear properties and cannot make full use of hardware acceleration, and common PTQ The QAT method has accuracy loss when processing nonlinear operators.

Method used

By obtaining the information and hardware resource information of each operator, the quantization strategy for different operators is determined, the calibration sample set and the training sample set are used for quantization processing, the model parameters and quantization parameters are updated, and a polynomial approximation method is designed for nonlinear operators. , to achieve efficient quantification.

Benefits of technology

It improves the quantification accuracy and efficiency of the neural network model, is suitable for resource-constrained devices, enhances the compatibility and operating efficiency of the model on hardware, and reduces power consumption and storage requirements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118709732A
    Figure CN118709732A
Patent Text Reader

Abstract

The invention discloses a neural network model quantification method and device and electronic equipment, and belongs to the field of artificial intelligence. The method comprises the following steps: obtaining operator information of each operator in a first neural network model; based on the operator information of each operator and the resource information of the hardware resources of the to-be-deployed device, an operator quantification strategy corresponding to each type of operator is determined, and one type of operator comprises at least one operator; based on the operator quantification strategy corresponding to each type of operator, performing quantification processing on each operator in the first neural network model to obtain a second neural network model; based on a calibration sample set, quantifying the second neural network model to obtain a third neural network model, the calibration sample set comprising at least one calibration sample; and on the basis of the training sample set and the loss function, updating model parameters in the third neural network model and quantization parameters of operators to obtain a fourth neural network model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of artificial intelligence, and specifically relates to a neural network model quantization method, device and electronic device. Background Art

[0002] With the rapid development and widespread application of deep learning neural network models, their influence in various fields is becoming increasingly significant. In particular, in the fields of natural language processing and image recognition, deep learning technology has achieved remarkable results.

[0003] However, as the complexity of deep learning neural network models continues to increase, the computational workload and storage requirements of the models are also growing dramatically. This makes it increasingly difficult to deploy and implement the models on different hardware resources. Especially in resource-constrained environments, such as mobile electronic devices or embedded systems, the real-time and performance requirements of the models are more stringent. Therefore, the development of lightweight models has also become crucial. In order to solve this problem, researchers have begun to pay attention to and study model compression methods. Model compression methods can effectively reduce the hardware memory usage and increase the computing speed. Currently, the more common model compression algorithms include quantization, pruning, distillation, etc. Among them, quantization is a method that effectively reduces the computational workload and storage requirements of the model. By converting the model's weights and activation functions from high precision to low precision, the model's size and power consumption can be significantly reduced while maintaining the model's performance.

[0004] Since the quantization accuracy and effect achieved by current model quantization technology are not very impressive, how to further optimize model quantization technology and improve the performance and efficiency of the model remains a problem that needs to be solved urgently in this field. Summary of the invention

[0005] The purpose of the embodiments of the present application is to provide a neural network model quantization method, device and electronic device, which can improve the performance and efficiency of the neural network model.

[0006] In a first aspect, an embodiment of the present application provides a neural network model quantization method, which includes: obtaining operator information of each operator in a first neural network model; determining the operator quantization strategy corresponding to each type of operator based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, wherein one type of operator includes at least one operator; based on the operator quantization strategy corresponding to each type of operator, quantizing each operator in the first neural network model to obtain a second neural network model; based on a calibration sample set, quantizing the second neural network model to obtain a third neural network model, wherein the calibration sample set includes at least one calibration sample; based on the training sample set and the loss function, updating the model parameters and the quantization parameters of the operator in the third neural network model to obtain a fourth neural network model.

[0007] In the second aspect, an embodiment of the present application provides a neural network model quantization device, which includes: an acquisition module, a determination module, a quantization module and a processing module; the acquisition module is used to acquire the operator information of each operator in the first neural network model; the determination module is used to determine the operator quantization strategy corresponding to each type of operator based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, and one type of operator includes at least one operator; the quantization module is used to quantize each operator in the first neural network model based on the operator quantization strategy corresponding to each type of operator to obtain a second neural network model; the quantization module is also used to quantize the second neural network model based on a calibration sample set to obtain a third neural network model, and the calibration sample set includes at least one calibration sample; the processing module is used to update the model parameters and the quantization parameters of the operator in the third neural network model based on the training sample set and the loss function to obtain a fourth neural network model.

[0008] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor and a memory, wherein the memory stores programs or instructions that can be run on the processor, and when the program or instructions are executed by the processor, the steps of the method described in the first aspect are implemented.

[0009] In a fourth aspect, an embodiment of the present application provides a readable storage medium, on which a program or instruction is stored, and when the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.

[0010] In a fifth aspect, an embodiment of the present application provides a chip, comprising a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run a program or instruction to implement the method described in the first aspect.

[0011] In a sixth aspect, an embodiment of the present application provides a computer program product, which is stored in a storage medium and is executed by at least one processor to implement the method described in the first aspect.

[0012] In an embodiment of the present application, the operator information of each operator in the first neural network model is obtained; based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, the operator quantization strategy corresponding to each type of operator is determined, and one type of operator includes at least one operator; based on the operator quantization strategy corresponding to each type of operator, each operator in the first neural network model is quantized to obtain a second neural network model; based on the calibration sample set, the second neural network model is quantized to obtain a third neural network model, and the calibration sample set includes at least one calibration sample; based on the training sample set and the loss function, the model parameters and the quantization parameters of the operator in the third neural network model are updated to obtain a fourth neural network model. In this solution, the neural network model quantization device can determine different operator quantization strategies for different operators by analyzing the operator information of each operator and the resource information of the hardware resources of the device to be deployed, so that in the model quantization process, each operator can be quantized according to the characteristics of each operator, thereby obtaining an operator with higher quantization accuracy, thereby improving the performance and efficiency of the neural network model. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] Figure 1 This is one of the flow charts of a neural network model quantization method provided in an embodiment of the present application;

[0014] Figure 2 This is the second flow chart of a neural network model quantization method provided in an embodiment of the present application;

[0015] Figure 3 This is a third flow chart of a neural network model quantization method provided in an embodiment of the present application;

[0016] Figure 4 This is a fourth flow chart of a neural network model quantization method provided in an embodiment of the present application;

[0017] Figure 5 This is one of the structural schematic diagrams of a neural network model quantization device provided in an embodiment of the present application;

[0018] Figure 6 This is the second structural schematic diagram of a neural network model quantization device provided in an embodiment of the present application;

[0019] Figure 7 This is one of the hardware structure diagrams of an electronic device provided in an embodiment of the present application;

[0020] Figure 8 This is the second schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0021] The following will be combined with the drawings in the embodiments of the present application to clearly describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all the embodiments. All other embodiments obtained by ordinary technicians in this field based on the embodiments in the present application belong to the scope of protection of this application.

[0022] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally of one type, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally indicates that the objects associated with each other are in an "or" relationship.

[0023] It should be noted that the neural network model quantization method provided in the embodiments of the present application can be executed by electronic devices such as mobile phones, tablet computers, laptop computers, PDAs, and vehicle-mounted electronic devices. In some embodiments of the present application, the neural network model quantization method is executed by an electronic device as an example to illustrate the neural network model quantization method provided in the embodiments of the present application.

[0024] The following is an explanation of the technical terms involved in the embodiments of the present application:

[0025] 1) Model compression methods can effectively reduce hardware memory usage and improve computing speed. Currently, the more common model compression algorithms include quantization, pruning, distillation, etc. Among them, pruning and distillation achieve the purpose of model compression by reducing model parameters. The structure of the model after compression is usually very different from that before compression.

[0026] Quantization is an effective method to reduce the amount of computation and storage requirements of a model. By converting the model's weights and activation values ​​from high precision to low precision, the model's size and power consumption can be significantly reduced while maintaining the model's performance. This technology is of great significance for improving the deployment efficiency and real-time performance of the model on different hardware resources.

[0027] 2) Operator: refers to a set of operations built in the model to achieve specific functions. For example, Softmax, Gelu or FC. The model can contain multiple such operators, and these operators can be executed on the hardware execution unit on the acceleration unit.

[0028] 3) Post-training Quantization (PTQ)

[0029] PTQ is a quantization method for trained neural network models. PTQ is a quantization process performed after the model training is completed and the quantization conversion has been done. It aims to convert the floating-point weights and activation values ​​in the model into low-precision fixed-point representations to reduce the size and computational complexity of the model while maintaining the performance of the model as much as possible. PTQ does not require retraining the model, but quantizes based on the already trained model.

[0030] PTQ is widely used in the deployment and optimization of various neural network models, especially on resource-constrained devices. PTQ quantization can help models run efficiently on platforms such as mobile devices, embedded systems, and IoT devices, reducing power consumption and storage requirements. Through PTQ quantization, the size of the model can be significantly reduced, which is very beneficial for storing and transmitting the model. Low-precision fixed-point calculations are usually faster and consume less energy than floating-point calculations, so PTQ quantization can speed up the model's reasoning process and help reduce the model's power consumption.

[0031] 4) Quantization-Aware Training (QAT)

[0032] QAT is a quantization technology for neural network models. QAT is a neural network model training technology that aims to adapt neural network models to quantization operations so that they can maintain good performance when deployed on hardware with limited computing power. In the QAT process, the model simulates quantization operations during training, allowing the model to learn and adapt to the noise and precision loss introduced by quantization, ensuring that the performance loss of the model is minimized when it is actually quantized and deployed. Compared with PTQ, QAT can usually achieve better performance. Since current neural network models usually contain a large number of parameters and computing requirements, direct deployment on resource-constrained devices may be challenging. By simulating the quantization process, QAT adapts the model to the distribution of quantized weights and activation values, thereby improving the compatibility and operating efficiency of the model on specific hardware. Therefore, QAT quantization is widely used in scenarios such as mobile applications, embedded systems, and IoT devices to help models run efficiently on these platforms. QAT allows developers to adjust quantization schemes, such as quantization bit width and quantization strategy, according to the specific needs of the target platform to optimize the performance of the model.

[0033] It should be noted that QAT requires simulating quantization operations during training, which may increase training time and required computing resources.

[0034] At present, model quantization technology has made significant progress. From the earliest 8-bit quantization to the later mixed precision quantization and binary quantization, quantization technology has been constantly updated to provide effective solutions for lightweight models. However, with the rapid development of neural network models, such as the Transformer structure network model, and the continuous emergence of its application scenarios, the quantization methods based on CNN in the past have a strong demand for quantization-based hardware acceleration of the Transformer structure network. Therefore, the effect of supporting the performance and utilization of the Transformer is unsatisfactory.

[0035] At the same time, since the Transformer structure network model uses a large number of nonlinear operators, such as GELU, Softmax and LayerNorm, which are different from linear operators such as MatMul or piecewise linear operators such as ReLU, the quantization implementation of the nonlinear operators of the Transformer structure network model is not simple. The current quantization method relies on the linear properties of the operator for the quantization implementation of the model, and the nonlinear operators in the model are usually implemented in two ways: one is floating point mode, and the other is equivalent approximation.

[0036] The floating point method is to use floating point calculations for nonlinear operators, which makes it impossible for the module to achieve full quantization reasoning. The reasoning speed of the neural network model on the hardware that cannot be fully quantized will also be greatly reduced, and it cannot be used on hardware that does not support floating point operations.

[0037] Another equivalent approximation method is to quantize the nonlinear operator by using fixed-point approximate calculation or lookup table. Usually, replacing the nonlinear operator with an approximate operator in the model will result in a certain loss of accuracy.

[0038] The common PTQ and QAT quantization methods are currently for linear operators and cannot compensate for the precision loss caused by nonlinear operators. In the process of quantitative reasoning of the model, the calculation results of nonlinear operators often have a more significant impact on the results of the model, but there is currently a lack of an efficient, convenient and universal quantization method for nonlinear operators.

[0039] In related technologies, model quantization strategies can be divided into post-training quantization (PTQ) and training-aware quantization (QAT). PTQ directly quantizes the model parameters in the model after the neural network model is trained, that is, converting floating-point numbers into fixed-point numbers. QAT, on the other hand, reversely updates the floating-point parameters according to the loss function during the training of the neural network model.

[0040] Among them, the QAT method uses floating-point numbers in the model training process and simulates the impact of quantization operations at the same time, thereby estimating the loss after quantization and trying to offset this loss by adjusting the quantization parameters during training, so that the model can maintain a high model accuracy after quantization. QAT is an important neural network model quantization method, which is suitable for deploying neural network models on devices with limited hardware resources. However, the existing quantization methods may include non-differentiable operations for the quantization implementation of operators, which will make the model unable to execute normally during backpropagation (BP), and ultimately make QAT quantization unusable, thereby affecting the final quantization accuracy and effect of the model.

[0041] In the neural network model quantization method provided in the embodiment of the present application, the operator information of each operator in the first neural network model is first obtained; based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, the operator quantization strategy corresponding to each type of operator is determined, and one type of operator includes at least one operator; based on the operator quantization strategy corresponding to each type of operator, each operator in the first neural network model is quantized to obtain a second neural network model; based on the calibration sample set, the second neural network model is quantized to obtain a third neural network model, and the calibration sample set includes at least one calibration sample; based on the training sample set and the loss function, the model parameters in the third neural network model and the quantization parameters of the operator are updated to obtain a fourth neural network model. In this scheme, the neural network model quantization device can determine different operator quantization strategies for different operators by analyzing the operator information of each operator and the resource information of the hardware resources of the device to be deployed, so that in the model quantization process, each operator can be quantized according to the characteristics of each operator, thereby obtaining an operator with higher quantization accuracy, thereby improving the performance and efficiency of the neural network model.

[0042] The present application provides a neural network model quantization method. Figure 1 A flowchart of a neural network model quantization method provided by an embodiment of the present application is shown, and the method can be applied to electronic devices. Figure 1 As shown, the neural network model quantization method provided in the embodiment of the present application may include the following steps 201 to 205.

[0043] Step 201: The electronic device obtains operator information of each operator in the first neural network model.

[0044] In some embodiments of the present application, the above-mentioned neural network model includes multiple operators.

[0045] In some embodiments of the present application, the above operator refers to a set of operations constructed in the model to implement a specific function, for example, Softmax, Gelu or FC.

[0046] In some embodiments of the present application, the above-mentioned neural network model includes multiple layers of neural network layers, and each neural network layer is used to process corresponding data.

[0047] In some embodiments of the present application, the first neural network model can be any model used in the target field. The target field can be autonomous driving, computer vision, natural language processing, speech recognition, data processing and other fields. In other words, the algorithm for improving the accuracy of the neural network model after training provided by the present application can be applied to image target detection and segmentation, laser radar point cloud target detection, speech recognition, natural language processing and other fields.

[0048] In some embodiments of the present application, the above operator information may be the operator type, the linear property of the operator, the amount of operation of the operator, and the memory space required by each operator during execution.

[0049] Exemplarily, the above operator types may be convolution, pooling, full connection, or activation function.

[0050] Exemplarily, the linear property of the above operator may be a linear operator or a nonlinear operator.

[0051] In some embodiments of the present application, the electronic device obtains model information of the first neural network model by analyzing the structure of the first neural network model, for example, which layers the first neural network model includes, the type of each layer, and the connection relationship between the layers. Then, the operator information of the operators used in each neural network layer in the first neural network layer is further analyzed.

[0052] Step 202: The electronic device determines an operator quantization strategy corresponding to each type of operator based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed.

[0053] In some embodiments of the present application, an operator of one type includes at least one operator.

[0054] Exemplarily, the nonlinear type operators include at least one operator that is a nonlinear operator, and the linear type operators include at least one operator that is a linear operator.

[0055] In some embodiments of the present application, the above-mentioned device to be deployed can be any electronic device, such as a mobile device, an embedded system, an Internet of Things device, etc.

[0056] In some embodiments of the present application, the resource information of the above-mentioned hardware resources may include but is not limited to at least one of the following: the computing power of the device to be deployed, the computing type supported by the device to be deployed, the storage space of the device to be deployed, and the power consumption capacity of the device to be deployed.

[0057] In some embodiments of the present application, the above-mentioned operator quantization strategy may be a common quantization strategy, such as symmetric quantization, asymmetric quantization, mixed precision quantization, or quantization of different bit widths.

[0058] In some embodiments of the present application, the operator quantization strategy may also be a specific quantization strategy suitable for a certain operator calculated and analyzed based on the resource information.

[0059] It should be noted that the choice of quantization bit width in the above quantization strategies with different bit widths needs to be weighed based on the performance loss, hardware support, and deployment requirements of the model.

[0060] The following is a detailed description of the process of determining the operator quantization strategy through specific steps, which specifically include steps A to C:

[0061] Step A: The electronic device identifies each operator in the first neural model, and obtains resource information of hardware resources of the device to be deployed.

[0062] For example, the operator may include a linear operator, such as Conv, Linear, or FC, etc., and may also include a nonlinear operator, such as LayerNorm, Softmax, or Gelu, etc. The electronic device performs a detailed analysis on each operator, including the data type, value range, and computational complexity of its input and output, to obtain operator information of each operator.

[0063] Step B: The electronic device selects a suitable operator quantization strategy according to the operator information of each operator and the resource information of the hardware resources of the device to be deployed.

[0064] Exemplarily, for common operators, the electronic device can directly select a commonly used operator quantization strategy; for special operators, such as nonlinear operators, a specific operator quantization strategy can be designed according to the operator information of the operator.

[0065] Step C: The electronic device designs a specific operator quantization strategy according to the operator information of each operator.

[0066] Exemplarily, the above operator quantization strategy may include determining quantization parameters, such as quantization step size, zero point offset, etc., and a quantization algorithm.

[0067] For example, for asymmetric quantization, it is necessary to determine how to cluster the values ​​and how to select representative values ​​for each cluster; for mixed-precision quantization, it is necessary to determine which operators or layers can have their precision reduced and to what extent.

[0068] It is understandable that the electronic device can design different operator quantization strategies for different operators.

[0069] Step 203: The electronic device quantizes each operator in the first neural network model based on the operator quantization strategy corresponding to each type of operator to obtain a second neural network model.

[0070] In some embodiments of the present application, the electronic device modifies the computing logic of the original operator according to the operator quantization strategy of each operator to support low-precision integer operations, to complete the quantization conversion of each operator, and replaces each operator in the first neural network model with the corresponding quantized converted operator, and finally obtains the second neural network model.

[0071] Step 204: The electronic device quantizes the second neural network model based on the calibration sample set to obtain a third neural network model.

[0072] In some embodiments of the present application, the calibration sample set includes at least one calibration sample.

[0073] It should be noted that a sample set similar to the training sample set is selected as the calibration sample set to ensure that the data distribution in the calibration process is similar to the data of the training sample set.

[0074] In some embodiments of the present application, the electronic device inputs the calibration samples in the calibration sample set into the second neural network model, and outputs the results after processing, so as to obtain the weights and activation values ​​of each neural network layer of the second neural network model when processing the calibration samples, and then quantizes the second neural network model according to the weights and activation values ​​of each neural network layer to obtain a third neural network model.

[0075] Step 205: The electronic device updates the model parameters and the quantization parameters of the operators in the third neural network model based on the training sample set and the loss function to obtain a fourth neural network model.

[0076] In some embodiments of the present application, the training sample set includes at least one training sample.

[0077] In some embodiments of the present application, the training samples in the above training sample set are related to the tasks performed by the neural network model.

[0078] Exemplarily, the training sample set corresponding to the neural network model for recognizing the image field is an image sample, and the training sample set corresponding to the neural network model for recognizing the semantic field is a text sample set.

[0079] In some embodiments of the present application, the above processing results are related to the tasks performed by the neural network model.

[0080] Exemplarily, the processing result of the output corresponding to the neural network model for identifying the image field is image recognition information, such as image type, image name or image color, etc.; the processing result of the output corresponding to the neural network model for identifying the semantic field is the semantic interpretation corresponding to the input text.

[0081] In some embodiments of the present application, the above loss function may be a regression loss function or a classification loss function, which is not limited in the present application.

[0082] In some embodiments of the present application, the electronic device inputs the training sample set into the third neural network model and outputs the processing result. When the processing result does not match the training sample set, the electronic device trains the third neural network model based on the loss function to obtain the quantization parameter and model adjustment parameter of each operator in the third neural network model. Then, based on the quantization parameter and model adjustment parameter of each operator in the third neural network model, the third neural network model is quantized and converted to obtain a fourth neural network model.

[0083] In some embodiments of the present application, the above training process may adopt the QAT training method.

[0084] It should be noted that since the third neural network model mentioned above is a model whose operator parameters have been quantized and updated, as the initial model of QAT training, it can optimize the number of iterations of model training to a certain extent, thereby reducing the training time.

[0085] In addition, QAT is an effective neural network model quantization technology that can help the model adapt to quantization operations and achieve efficient operation on resource-constrained devices. However, in practical applications, it is necessary to select appropriate quantization strategies and solutions based on specific needs and constraints to ensure the best quantization effect.

[0086] In this way, electronic devices can continuously adjust the quantization parameters and model parameters of the operator through the training process, thereby improving the performance and accuracy of the neural network model.

[0087] Optionally, in some embodiments of the present application, after obtaining the fourth neural network model, the electronic device will combine the fourth neural network model with resource information of the hardware resources of the device to be deployed, perform quantization conversion, and obtain a final quantized neural network model.

[0088] In some embodiments of the present application, the above-mentioned quantization conversion of the fourth neural network model to obtain the final quantized neural network model is a necessary operation before the neural network model is deployed on hardware.

[0089] Exemplarily, after obtaining the fourth neural network model, it is first necessary to save the fourth network model that has undergone quantization conversion, which can usually be achieved through the functions provided by the deep learning framework. When saving the model, you can choose to save the structure and parameters of the entire model, or only save the state dictionary of the model, that is, the parameters of the model. Then, based on the resource information of the hardware resources of the device to be deployed, it may be necessary to convert the model to a specific format. For example, some devices to be deployed may support specific model formats or require specific optimizations. The conversion process can be completed using a model conversion tool or a conversion function provided by a deep learning framework.

[0090] In the neural network model quantization method provided in the embodiment of the present application, the operator information of each operator in the first neural network model is obtained; the operator quantization strategy corresponding to each operator is determined based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed; and each operator in the first neural network model is quantized based on the operator quantization strategy corresponding to each operator to obtain a second neural network model. In this solution, the electronic device can determine different operator quantization strategies for different operators by analyzing the operator information of each operator and the resource information of the hardware resources of the device to be deployed, so that in the process of model quantization, each operator can be quantized according to the characteristics of each operator, thereby obtaining an operator with higher quantization accuracy, thereby improving the performance and efficiency of the neural network model.

[0091] Optionally, in some embodiments of the present application, combined with Figure 1 ,like Figure 2 As shown, the above step 202 specifically includes steps 202a to 202c:

[0092] Step 202a: The electronic device determines a quantization target of the first neural network model according to the resource information.

[0093] In some embodiments of the present application, the above-mentioned quantization target may include at least one of the following: the size of the first neural network model after quantization, the range of accelerating the inference speed, the range of reducing power consumption, and the acceptable performance loss range.

[0094] Step 202b: The electronic device determines a quantization parameter and a quantization algorithm for each operator based on the operator information, quantization target, and resource information of each operator.

[0095] In some embodiments of the present application, the quantization parameter is a quantization bit width, such as 8 bits, 4 bits or lower, a quantization weight value, or a quantization activation value.

[0096] In some embodiments of the present application, the above quantization algorithm may be symmetric quantization, asymmetric quantization, mixed precision quantization, etc.

[0097] It is understandable that different quantization targets, quantization methods and quantization parameters correspond to different quantization design implementations in the quantization operator.

[0098] Step 202c: The electronic device determines an operator quantization strategy corresponding to each operator based on the quantization parameter and quantization algorithm of each operator.

[0099] In this way, electronic devices can design operator quantization strategies for operators in a targeted manner based on the results of analyzing each operator in the neural network model. Designing operator quantization strategies for operators in a neural network model is a key step in quantizing the neural network model, which involves converting operators in the model from floating-point representation to low-precision integer representation to reduce model size, accelerate reasoning, and reduce computing resource requirements.

[0100] Optionally, in some embodiments of the present application, combined with Figure 1 ,like Figure 3 As shown, the above step 203 specifically includes step 203a and step 203b:

[0101] Step 203a: The electronic device performs quantization conversion on each operator in the first neural network model based on the operator quantization strategy corresponding to each type of operator to obtain a quantized operator corresponding to each operator.

[0102] The following is an illustration of the quantization conversion process of linear operators and nonlinear operators.

[0103] Example 1:

[0104] Take the linear operator as Conv2d operator and the operator quantization strategy as the maximum and minimum quantization method as an example. The electronic device can realize the distribution statistics and quantization conversion of the input x, weights and bias of the Conv2d operator by constructing an observer and a quantizer. It should be noted that since the input x, weights and bias are all tensors, they are uniformly referred to as tensors in the embodiments of this application for the convenience of description.

[0105] First, the Observer obtains the maximum and minimum values ​​of each tensor by statistically analyzing the distribution of the values ​​corresponding to each tensor. The statistical methods include but are not limited to the maximum and minimum method. By obtaining the maximum and minimum values ​​xmax / xmin of the tensor and the maximum and minimum values ​​qmax / qmin required for quantization set in the operator quantization strategy, the quantization scale s and relative zero value z can be calculated by formula (1):

[0106]

[0107] Then, Quantizer implements the quantization conversion process of converting the tensor from floating point numbers to fixed point integers through the quantization parameters designed in the operator quantization strategy, such as the bit width parameter. Taking the quantization of unsigned numbers with a bit width of b as an example, the floating point to fixed point quantization conversion principle can be shown in formula (2):

[0108]

[0109] In the above formula, round is the integer operation, and clamp is the truncation operation.

[0110] Correspondingly, the principle of fixed-point to floating-point quantization conversion is shown in formula (3):

[0111]

[0112] Example 2:

[0113] Taking the nonlinear operator as an example, since the nonlinear operator itself carries more information and has higher precision requirements, it is difficult to directly use fixed-point numbers for quantitative reasoning. In the past, there are usually two strategies for operator quantization of such complex functions: one is to implement it through a lookup table, and the other is to approximate it with a simpler function. The lookup table method is to directly discretize the function interval and record the function value in a table at regular intervals. In this way, you can directly look up the table each time you calculate. However, this method requires a lot of memory to store the entire table, which is not suitable for low-power devices to be deployed. Therefore, in this example, considering the hardware resources of the device to be deployed, you can choose to use a polynomial function to effectively approximate this type of nonlinear function. In this way, we can convert the nonlinear function calculation into the implementation of a linear function, so that the nonlinear function can be directly quantified using fixed-point numbers for reasoning.

[0114] Specifically, the Gelu operator is shown in formula (4):

[0115]

[0116] The more difficult parts in the above formula (4) are all on the nonlinear function erf. Electronic devices can implement the quantization conversion of the Gelu operator by approximating the erf function. For example, a quadratic polynomial L(x) = a(x+b) can be used. 2 +c to approximate erf, so that a polynomial version of Gelu can be obtained, as shown in formula (5):

[0117]

[0118] Through the above polynomial approximation method, the quantization conversion of the nonlinear operator Gelu can be realized. Similarly, for other nonlinear operators such as Softmax and LayerNorm, we can also perform approximate calculations through a lightweight polynomial method to realize the quantization conversion of nonlinear operators.

[0119] It should be noted that the design of the operator quantization strategy is a complex process that requires comprehensive consideration of the performance of the model, the support capabilities of the hardware resources of the equipment to be deployed, and the deployment requirements. Different neural network models and equipment to be deployed have different characteristics and limitations, so adjustments and optimizations need to be made according to specific circumstances when performing operator analysis. In addition, with the continuous development of deep learning technology, new quantization methods and optimization technologies are also constantly emerging, so it is necessary to keep an eye on and learn new technologies and methods when performing operator quantization adaptation design.

[0120] Step 203b: The electronic device replaces each operator in the first neural network model with a corresponding quantization operator to obtain a second neural network model.

[0121] In some embodiments of the present application, the operator replacement process is a process of replacing the original operator in the model with the quantized operator according to the quantization target, the quantization algorithm and the quantization parameter.

[0122] Exemplarily, when replacing a quantized operator, for a linear operator, such as a Conv2d operator, when replacing the original operator, its corresponding quantization method and quantization parameters need to be configured; for a nonlinear operator, such as a Gelu operator, when replacing the original operator, it is mainly necessary to replace its corresponding quantized operator implementation.

[0123] Exemplarily, at the connection between each operator of the first neural network model, it is also necessary to determine whether to insert a quantization node for quantization and dequantization operations according to the quantization requirements of the output and input of the previous and next operators. After completing the above quantization operator replacement operation on the neural network model, the second neural network model is obtained.

[0124] It is understandable that the above second neural network model can be used for subsequent operations of model quantization.

[0125] In this way, the electronic device can obtain a neural network model with lower computing power and lower consumption by replacing the original operators in the first neural network model with quantized low-precision operators.

[0126] Optionally, in some embodiments of the present application, combined with Figure 1 ,like Figure 4 As shown, the above step 204 "the electronic device quantizes the second neural network model based on the calibration sample set to obtain the third neural network model" specifically includes step 204a and step 204b:

[0127] Step 204a: The electronic device calibrates the quantization parameters of each neural network layer in the second neural network model based on the calibration sample set.

[0128] In some embodiments of the present application, the electronic device inputs the calibration sample set into the second neural network model for processing, obtains the weights and activation values ​​of each neural network layer when the second neural network model processes the calibration sample set, and calibrates the quantization parameters of each neural network layer based on the weights and activation values.

[0129] Optionally, in some embodiments of the present application, the above step 204a specifically includes step 204a1 and step 204a2:

[0130] Step 204a1: The electronic device inputs the calibration sample set into the second neural network model for processing.

[0131] In some embodiments of the present application, the second neural network model includes multiple layers of neural network layers.

[0132] In some embodiments of the present application, at least one calibration sample in the calibration sample set is input into the second neural network model, and the at least one calibration sample is processed through each neural network layer.

[0133] Step 204a2: The electronic device calculates the quantization parameters of the first neural network layer based on the weights and activation values ​​of the first neural network layer of the processed second neural network model.

[0134] In some embodiments of the present application, the first neural network layer is one of the neural network layers in the second neural network model.

[0135] In some embodiments of the present application, the electronic device calculates the quantization parameters of each neural network layer based on the weights and activation values ​​of each neural network layer of the processed second neural network model.

[0136] Step 204b: The electronic device quantizes the operators in the second neural network model based on the calibrated quantization parameters of each neural network layer to obtain a third neural network model.

[0137] In some embodiments of the present application, after calibrating the quantization parameters of each neural network layer, the weights and activation values ​​in the operators of each neural network layer are quantized according to the calibrated quantization parameters of each neural network layer to obtain a third neural network model.

[0138] It should be noted that the above method for calibrating the neural network model can be PTQ. Since PTQ is performed after training, it may introduce a certain loss of accuracy. Although PTQ quantization is designed to minimize the loss of accuracy, in some cases, especially when the number of quantization bits is low, significant accuracy degradation may still occur. In practical applications, it is necessary to select appropriate quantization strategies and schemes based on specific needs and constraints to balance the relationship between accuracy and performance.

[0139] In this way, the electronic device can further improve the performance and efficiency of the quantized neural network model by calibrating the quantization parameters in the neural network model.

[0140] Optionally, in some embodiments of the present application, after the above step 204b, the neural network model quantization method provided in the embodiment of the present application further includes steps 301 to 303:

[0141] Step 301: The electronic device obtains a non-differentiable operator in a second neural network model after quantization processing.

[0142] In some embodiments of the present application, the non-differentiable operator is a function whose derivative cannot be calculated in a neural network model.

[0143] In some embodiments of the present application, the electronic device obtains the non-differentiable operator in the second neural network model by back-propagating the second neural network model.

[0144] Step 302: The electronic device converts a non-differentiable quantized operator into a differentiable quantized operator.

[0145] In some embodiments of the present application, for the calculation of the reverse propagation non-differentiable, the straight-through estimator (STE) formula (6) can be used to perform derivative approximation on the non-differentiable operator to convert the non-differentiable operator into a differentiable operator.

[0146]

[0147] It should be noted that in order to control the error of the derivative approximation as much as possible, the gradient of the input of formula (6) can be set to a threshold function equal to its output gradient, regardless of the actual derivative of the threshold function itself.

[0148] Step 303: The electronic device replaces each non-differentiable quantization operator in the second neural network model after quantization processing with a corresponding differentiable quantization operator to obtain a third neural network model.

[0149] In this way, since some operators cannot be trained on the model after quantization because they are not reversely differentiable, the process of converting non-differentiable operators into differentiable ones in this embodiment can effectively reduce the error rate and improve the efficiency of model quantization.

[0150] It can be understood that when designing the operator quantization strategy of the operator in the above steps of the embodiment of the present application, the resource information and limitations of the hardware resources of the device to be deployed will be taken into consideration. Therefore, after the model is converted into the final quantization model, better hardware adaptability can be obtained in theory.

[0151] It should be noted that the specific steps and processes may vary depending on the deep learning framework, hardware platform, and deployment environment used. Therefore, in actual operation, it is recommended to refer to the documentation and guidelines of the relevant framework and hardware platform to ensure that the model is saved and deployed correctly. In addition, for large-scale deployment or production environments, it may also be necessary to consider issues such as model version control, update strategy, security, and performance monitoring. These all need to be planned and implemented according to specific application scenarios and requirements.

[0152] In a possible embodiment, the above steps are applicable to the algorithm developer who has trained a floating-point neural network model in advance, and then performs PTQ calibration and QAT training according to the operators that need to be quantized in the floating-point neural network model, including linear operators and nonlinear operators, to obtain a fully quantized neural network model. However, further, before the training of the floating-point neural network model begins, the nonlinear operators in the neural network model can be replaced with approximate operators of equivalent hardware design, which are equipped with the properties of floating-point forward propagation and reverse propagation.

[0153] Specifically, first, the nonlinear operators in the original floating-point neural network model are replaced with hardware equivalent approximate operators to obtain a floating-point neural network model after the first quantization. Among them, the operator implementations of the original floating-point neural network model are all standard floating-point operators, and the weights of the model can be random or pre-trained on other data sets. The replaced floating-point neural network model refers to replacing the nonlinear operators in the original floating-point neural network model with equivalent approximate operators implemented in hardware, including but not limited to approximate operators implemented by polynomial approximation, piecewise approximation, etc.

[0154] Then, the replaced floating-point neural network model is trained again with the loss function to obtain the trained floating-point neural network model. The floating-point training process includes both the forward reasoning and back propagation of conventional linear operators and the forward reasoning and back propagation of nonlinear equivalent approximate operators, which is equivalent to letting the model use approximate operators for network fitting in advance.

[0155] Next, the trained floating-point neural network model is quantized according to the quantization method described above in the embodiment of the present application to obtain a fully quantized neural network model.

[0156] In this way, the difference between the floating-point neural network model and the quantized neural network model can be reduced, which helps to reduce the quantization accuracy loss in the PTQ process and improve the training convergence speed in the QAT stage. It can also reduce the deployment time of the quantized neural network model from the floating-point neural network model to the corresponding hardware.

[0157] It should be noted that the neural network model quantization method provided in the embodiment of the present application can be executed by a neural network model quantization device, or an electronic device, or a functional module or entity in an electronic device. In the embodiment of the present application, the method of executing model quantization by a neural network model quantization device is taken as an example to illustrate the model quantization device provided in the embodiment of the present application.

[0158] Figure 5 A possible structural diagram of a neural network model quantization device involved in an embodiment of the present application is shown. Figure 5 As shown, the neural network model quantization device 700 may include: an acquisition module 701, a determination module 702, a quantization module 703 and a processing module 704;

[0159] Among them, the above-mentioned acquisition module 701 is used to obtain the operator information of each operator in the first neural network model; the above-mentioned determination module 702 is used to determine the operator quantization strategy corresponding to each type of operator based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, and one type of operator includes at least one operator; the above-mentioned quantization module 703 is used to quantize each operator in the first neural network model based on the operator quantization strategy corresponding to each type of operator to obtain a second neural network model; the above-mentioned quantization module 703 is used to quantize the second neural network model based on the calibration sample set to obtain a third neural network model, and the calibration sample set includes at least one calibration sample; the above-mentioned processing module 704 is also used to update the model parameters and the quantization parameters of the operator in the third neural network model based on the training sample set and the loss function to obtain a fourth neural network model.

[0160] Optionally, in some embodiments of the present application, the above-mentioned determination module 702 is specifically used to:

[0161] Determine a quantitative target of the first neural network model according to the resource information;

[0162] Determine the quantization parameter and quantization algorithm of each operator based on the operator information, quantization target and resource information of each operator;

[0163] Based on the quantization parameters and quantization algorithm of each operator, the operator quantization strategy corresponding to each operator is determined.

[0164] Optionally, in some embodiments of the present application, the quantization module 703 is specifically used to:

[0165] Based on the operator quantization strategy corresponding to each type of operator, each operator in the first neural network model is quantized and converted to obtain a quantized operator corresponding to each operator;

[0166] Each operator in the first neural network model is replaced with a corresponding quantization operator to obtain a second neural network model.

[0167] Optionally, in some embodiments of the present application, the quantization module 703 is specifically used to:

[0168] Based on the calibration sample set, calibrate the quantization parameters of each neural network layer in the second neural network model;

[0169] Based on the calibrated quantization parameters of each neural network layer, the operators in the second neural network model are quantized to obtain a third neural network model.

[0170] Optionally, in some embodiments of the present application, the quantization module 703 is specifically used to:

[0171] inputting the calibration sample set into the second neural network model for processing;

[0172] Based on the weights and activation values ​​of the first neural network layer of the processed second neural network model, a quantization parameter of the first neural network layer is calculated, where the first neural network layer is one of the neural network layers in the second neural network model.

[0173] Optionally, in some embodiments of the present application, combined with Figure 5 ,like Figure 6As shown, the above-mentioned device also includes an acquisition module 705; the acquisition module 705 is used for the above-mentioned quantization module 703 to quantize the operators in the second neural network model based on the calibrated quantization parameters of each neural network layer, and obtain the non-differentiable operators in the second neural network model after quantization processing; the above-mentioned processing module 704 is also used to convert the non-differentiable operator into a differentiable operator; the above-mentioned processing module 704 is also used to replace each non-differentiable operator in the second neural network model after quantization processing with a corresponding differentiable operator to obtain a third neural network model.

[0174] In this scheme, the neural network model quantization device can determine different operator quantization strategies for different operators by analyzing the operator information of each operator and the resource information of the hardware resources of the device to be deployed, so that in the model quantization process, each operator can be quantized according to the characteristics of each operator, thereby obtaining an operator with higher quantization accuracy, thereby improving the performance and efficiency of the neural network model.

[0175] The module quantization device in the embodiment of the present application can be an electronic device, or a component in the electronic device, such as an integrated circuit or a chip. The electronic device can be a terminal, or other devices other than a terminal. Exemplarily, the electronic device can be a mobile phone, a tablet computer, a laptop computer, a PDA, a vehicle-mounted electronic device, a mobile Internet device (Mobile Internet Device, MID), an augmented reality (augmented reality, AR) / virtual reality (virtual reality, VR) device, a robot, a wearable device, an ultra-mobile personal computer (ultra-mobile personal computer, UMPC), a netbook or a personal digital assistant (personal digital assistant, PDA), etc., and can also be a server, a network attached storage (Network Attached Storage, NAS), a personal computer (personal computer, PC), a television (television, TV), a teller machine or a self-service machine, etc., which is not specifically limited in the embodiment of the present application.

[0176] The module quantization device in the embodiment of the present application may be a device having an operating system. The operating system may be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiment of the present application.

[0177] The module quantization device provided in the embodiment of the present application can implement each process implemented in the embodiment of the neural network model quantization method. To avoid repetition, it will not be repeated here.

[0178] Alternatively, if Figure 7 As shown, an embodiment of the present application also provides an electronic device 800, including a processor 801 and a memory 802, wherein the memory 802 stores a program or instruction that can be executed on the processor 801, and when the program or instruction is executed by the processor 801, each step of the above-mentioned module quantization method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.

[0179] It should be noted that the electronic devices in the embodiments of the present application include the mobile electronic devices and non-mobile electronic devices mentioned above.

[0180] Figure 8 A schematic diagram of the hardware structure of an electronic device to implement an embodiment of the present application.

[0181] The electronic device 100 includes but is not limited to components such as a radio frequency unit 101, a network module 102, an audio output unit 103, an input unit 104, a sensor 105, a display unit 106, a user input unit 107, an interface unit 108, a memory 109, and a processor 110.

[0182] Those skilled in the art will appreciate that the electronic device 100 may also include a power source (such as a battery) for supplying power to various components, and the power source may be logically connected to the processor 110 through a power management system, thereby implementing functions such as managing charging, discharging, and power consumption management through the power management system. Figure 8 The electronic device structure shown in the figure does not constitute a limitation on the electronic device. The electronic device may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently, which will not be described in detail here.

[0183] Among them, the above-mentioned processor 110 is used to obtain the operator information of each operator in the first neural network model; the above-mentioned processor 110 is used to determine the operator quantization strategy corresponding to each type of operator based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, and one type of operator includes at least one operator; the above-mentioned processor 110 is used to quantize each operator in the first neural network model based on the operator quantization strategy corresponding to each type of operator to obtain a second neural network model; the above-mentioned processor 110 is used to quantize the second neural network model based on the calibration sample set to obtain a third neural network model, and the calibration sample set includes at least one calibration sample; the above-mentioned processor 110 is also used to update the model parameters and the quantization parameters of the operator in the third neural network model based on the training sample set and the loss function to obtain a fourth neural network model.

[0184] Optionally, in some embodiments of the present application, the processor 110 is specifically configured to:

[0185] Determine a quantitative target of the first neural network model according to the resource information;

[0186] Determine the quantization parameter and quantization algorithm of each operator based on the operator information, quantization target and resource information of each operator;

[0187] Based on the quantization parameters and quantization algorithm of each operator, the operator quantization strategy corresponding to each operator is determined.

[0188] Optionally, in some embodiments of the present application, the processor 110 is specifically configured to:

[0189] Based on the operator quantization strategy corresponding to each type of operator, each operator in the first neural network model is quantized and converted to obtain a quantized operator corresponding to each operator;

[0190] Each operator in the first neural network model is replaced with a corresponding quantization operator to obtain a second neural network model.

[0191] Optionally, in some embodiments of the present application, the processor 110 is specifically configured to:

[0192] Based on the calibration sample set, calibrate the quantization parameters of each neural network layer in the second neural network model;

[0193] Based on the calibrated quantization parameters of each neural network layer, the operators in the second neural network model are quantized to obtain a third neural network model.

[0194] Optionally, in some embodiments of the present application, the processor 110 is specifically configured to:

[0195] inputting the calibration sample set into the second neural network model for processing;

[0196] Based on the weights and activation values ​​of the first neural network layer of the processed second neural network model, a quantization parameter of the first neural network layer is calculated, where the first neural network layer is one of the neural network layers in the second neural network model.

[0197] Optionally, in some embodiments of the present application, the processor 110 is further used to quantize the operators in the second neural network model based on the calibrated quantization parameters of each neural network layer, and obtain the non-differentiable operators in the second neural network model after quantization; the processor 110 is further used to convert the non-differentiable operators into differentiable operators; the processor 110 is further used to replace each non-differentiable operator in the second neural network model after quantization with a corresponding differentiable operator to obtain a third neural network model.

[0198] In this scheme, the neural network model quantization device can determine different operator quantization strategies for different operators by analyzing the operator information of each operator and the resource information of the hardware resources of the device to be deployed, so that in the model quantization process, each operator can be quantized according to the characteristics of each operator, thereby obtaining an operator with higher quantization accuracy, thereby improving the performance and efficiency of the neural network model.

[0199] It should be understood that in the embodiment of the present application, the input unit 104 may include a graphics processor (Graphics Processing Unit, GPU) 1041 and a microphone 1042, and the graphics processor 1041 processes the image data of a static picture or video obtained by an image capture device (such as a camera) in a video capture mode or an image capture mode. The display unit 106 may include a display panel 1061, and the display panel 1061 may be configured in the form of a liquid crystal display, an organic light emitting diode, etc. The user input unit 107 includes a touch panel 1071 and at least one of other input devices 1072. The touch panel 1071 is also called a touch screen. The touch panel 1071 may include two parts: a touch detection device and a touch controller. Other input devices 1072 may include, but are not limited to, a physical keyboard, function keys (such as volume control keys, switch keys, etc.), a trackball, a mouse, and a joystick, which will not be repeated here.

[0200] The memory 109 can be used to store software programs and various data. The memory 109 may mainly include a first storage area for storing programs or instructions and a second storage area for storing data, wherein the first storage area may store an operating system, an application program or instructions required for at least one function (such as a sound playback function, an image playback function, etc.), etc. In addition, the memory 109 may include a volatile memory or a non-volatile memory, or the memory 109 may include both volatile and non-volatile memories. Among them, the non-volatile memory may be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may be a random access memory (RAM), a static random access memory (SRAM), a dynamic random access memory (DRAM), a synchronous dynamic random access memory (SDRAM), a double data rate synchronous dynamic random access memory (DDRSDRAM), an enhanced synchronous dynamic random access memory (ESDRAM), a synchronous link dynamic random access memory (SLDRAM) and a direct memory bus random access memory (DRRAM). The memory 109 in the embodiment of the present application includes but is not limited to these and any other suitable types of memory.

[0201] The processor 110 may include one or more processing units; optionally, the processor 110 integrates an application processor and a modem processor, wherein the application processor mainly processes operations related to an operating system, a user interface, and application programs, and the modem processor mainly processes wireless communication signals, such as a baseband processor. It is understandable that the modem processor may not be integrated into the processor 110.

[0202] An embodiment of the present application also provides a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, each process of the above-mentioned neural network model quantization method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.

[0203] The processor is the processor in the electronic device described in the above embodiment. The readable storage medium includes a computer readable storage medium, such as a computer read-only memory ROM, a random access memory RAM, a magnetic disk or an optical disk.

[0204] An embodiment of the present application further provides a chip, which includes a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run programs or instructions to implement the various processes of the above-mentioned neural network model quantization method embodiment, and can achieve the same technical effect. To avoid repetition, it will not be repeated here.

[0205] It should be understood that the chip mentioned in the embodiments of the present application can also be called a system-level chip, a system chip, a chip system or a system-on-chip chip, etc.

[0206] An embodiment of the present application provides a computer program product, which is stored in a storage medium. The program product is executed by at least one processor to implement the various processes of the above-mentioned neural network model quantization method embodiment, and can achieve the same technical effect. To avoid repetition, it will not be repeated here.

[0207] It should be noted that, in this article, the terms "comprise", "include" or any other variant thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise one..." do not exclude the presence of other identical elements in the process, method, article or device including the element. In addition, it should be pointed out that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in reverse order according to the functions involved, for example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.

[0208] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a computer software product, which is stored in a storage medium (such as ROM / RAM, a disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present application.

[0209] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.

Claims

1. A neural network model quantization method, characterized in that: The method comprises: Obtaining operator information of each operator in the first neural network model; Determine, based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, an operator quantization strategy corresponding to each type of operator, where one type of operator includes at least one operator; Based on the operator quantization strategy corresponding to each type of operator, quantize each operator in the first neural network model to obtain a second neural network model; quantizing the second neural network model based on a calibration sample set to obtain a third neural network model, wherein the calibration sample set includes at least one calibration sample; Based on the training sample set and the loss function, the model parameters and the quantization parameters of the operators in the third neural network model are updated to obtain a fourth neural network model.

2. The method according to claim 1, characterized in that The determining, based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, the operator quantization strategy corresponding to each operator includes: Determining a quantization target of the first neural network model according to the resource information; Determining a quantization parameter and a quantization algorithm for each operator based on the operator information of each operator, the quantization target, and the resource information; Based on the quantization parameter and quantization algorithm of each operator, an operator quantization strategy corresponding to each operator is determined.

3. The method according to claim 1 or 2, characterized in that: The method of performing quantization processing on each operator in the first neural network model based on the operator quantization strategy corresponding to each type of operator to obtain a second neural network model includes: Based on the operator quantization strategy corresponding to each type of operator, each operator in the first neural network model is quantized and converted to obtain a quantized operator corresponding to each operator; Each operator in the first neural network model is replaced with a corresponding quantization operator to obtain the second neural network model.

4. The method according to claim 1, characterized in that The method of quantizing the second neural network model based on the calibration sample set to obtain a third neural network model includes: Based on the calibration sample set, calibrate the quantization parameters of each neural network layer in the second neural network model; Based on the calibrated quantization parameters of each neural network layer, the operators in the second neural network model are quantized to obtain a third neural network model.

5. The method according to claim 4, characterized in that The step of calibrating the quantization parameters of each neural network layer in the second neural network model based on the calibration sample set includes: Inputting the calibration sample set into the second neural network model for processing; Based on the processed weights and activation values ​​of the first neural network layer of the second neural network model, a quantization parameter of the first neural network layer is calculated, where the first neural network layer is one of the neural network layers in the second neural network model.

6. The method according to claim 4 or 5, characterized in that: After quantizing the operators in the second neural network model based on the calibrated quantization parameters of each neural network layer, the method further includes: Obtaining a non-differentiable operator in the second neural network model after quantization processing; converting the non-differentiable operator into a differentiable operator; Each non-differentiable operator in the second neural network model after the quantization process is replaced with a corresponding differentiable operator to obtain the third neural network model.

7. A neural network model quantization device, characterized in that: The device comprises: an acquisition module, a determination module, a quantification module and a processing module; The acquisition module is used to acquire operator information of each operator in the first neural network model; The determination module is used to determine the operator quantization strategy corresponding to each type of operator based on the operator information of each operator and the resource information of the hardware resources of the device to be deployed, where one type of operator includes at least one operator; The quantization module is used to quantize each operator in the first neural network model based on the operator quantization strategy corresponding to each type of operator to obtain a second neural network model; The quantization module is used to quantize the second neural network model based on a calibration sample set to obtain a third neural network model, wherein the calibration sample set includes at least one calibration sample; The processing module is also used to update the model parameters and quantization parameters of the operators in the third neural network model based on the training sample set and the loss function to obtain a fourth neural network model.

8. An electronic device, characterized in that: It includes a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the neural network model quantization method as described in any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a program or instruction, and when the program or instruction is executed by the processor, the steps of the neural network model quantization method as described in any one of claims 1 to 6 are implemented.

10. A chip, characterized in that: The chip includes a processor and a communication interface, the communication interface is coupled to the processor, and the processor is used to run programs or instructions to implement the steps of the neural network model quantization method as described in any one of claims 1 to 6.

Citation Information

Patent Citations

  • Model quantification method and device, equipment, storage medium and computer program product

    CN113780551A

  • Neural network model quantitative deployment method supporting multiple hardware platforms

    CN116362287A

  • Neural network training method and device, readable storage medium and chip

    CN116402122A

  • Model quantification method and device, computer equipment and readable storage medium

    CN117474050A

  • Loss-error-aware quantization of a low-bit neural network

    US20210019630A1

Cited By

  • Hardware system model generation method and device, equipment, medium and program product

    CN119150935A

  • Model optimization method, service processing method, server and terminal equipment

    CN119272822A

  • Model optimization method, business processing method, server and terminal equipment

    CN119272822B

  • Large model deployment method, device and equipment for heterogeneous platform and program product

    CN119415277A

  • Large model deployment method, device, equipment and program product for heterogeneous platforms

    CN119415277B