Neural network model retraining methods and related products
Through hierarchical quantitative iterative training and a method that combines floating-point and fixed-point operations, the quantification accuracy of the neural network model is improved, the problem of low accuracy in the existing technology is solved, and resources are saved.
Patent Information
- Application Number
- CN202010113322.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-02-24
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2040-02-24
AI Technical Summary
Existing neural network quantification technology results in low accuracy of the quantized network, making it difficult to reduce computing resource requirements while maintaining model accuracy.
The iterative training method of hierarchical quantification is used to first quantify some network layers in the neural network model, then update the weights based on the training results, improve the quantification accuracy layer by layer, and combine floating point and fixed point operations to optimize the model.
The prediction accuracy of the quantized neural network model is improved, the consumption of computing resources is reduced, and a balance between accuracy and resources is achieved.
Smart Images

Figure CN113298224B
Abstract
Description
Technical Field
[0001] The present application relates to the field of neural network technology, and in particular to a neural network model quantization method and related products. Background Art
[0002] With the development of neural network technology, neural network quantization technology has emerged, which mainly compresses the weights in each network layer (such as convolutional layer and fully connected layer) in the neural network and reduces the bit width of the weights, so as to achieve the purpose of compressing the data volume of the neural network model and reducing the computing resource requirements of the neural network model in the prediction process.
[0003] At present, the commonly used neural network quantization scheme is to quantize the weights of each network layer after the neural network is trained. This method has low network accuracy. Therefore, it is necessary to study a quantization scheme that can make the network accuracy of the quantized neural network higher. Summary of the invention
[0004] The embodiments of the present application disclose a retraining method for a neural network model and related products.
[0005] In a first aspect, an embodiment of the present application provides a method for retraining a neural network model, the method comprising: performing a first iteration based on a first neural network model to obtain a second neural network model, wherein, in the first iteration, the weight of at least one first network layer included in the first neural network model is quantized, and the weights of multiple second network layers included in the first neural network model are floating point numbers; performing a second iteration based on the second neural network model to obtain a third neural network model, wherein, in the second iteration, the weights of the at least one first network layer and at least one second network layer adjacent to the at least one first network layer among the multiple second network layers are quantized.
[0006] In the embodiment of the present application, iterative training is performed in a layered quantization manner to ensure the prediction accuracy of the neural network model.
[0007] In an optional implementation, the network depth of the at least one first network layer is greater than the network depth of the at least one second network layer.
[0008] Experience shows that most neural network models are more sensitive to accuracy the closer they are to the input end, so retraining neural network models can be done layer by layer in a back-to-front order. For example, if there are 50 network layers in a neural network model, fine-tuning can be done in multiple rounds based on the training convergence. For example, the network parameters of 5 to 10 network layers are quantized in each round, and other network layers are expressed in floating points until all network layers of the neural network model are quantized and the fine-tuning process ends.
[0009] In this implementation, the efficiency of retraining can be improved.
[0010] In an optional implementation, performing a first iteration based on the first neural network model to obtain the second neural network model includes: performing a first prediction process on the first sample through the first neural network model to obtain a first prediction result; based on the first prediction result, updating the quantized weight data of the at least one first network layer and the floating-point weight data of the multiple second network layers to obtain the second neural network model.
[0011] In this implementation, the weight to be updated of at least one first network layer can be quantified accurately and quickly.
[0012] In an optional implementation, the quantization of the floating-point weight data of the first network layer includes: determining a first reference value based on the floating-point weight data of the first network layer; determining multiple numerical intervals based on the first reference value; each of the multiple numerical intervals corresponds to a numerical value that can be represented by F bits, and F is an integer greater than 1 and less than 8; based on the multiple numerical intervals, performing a first quantization process on the weights in the floating-point weight data to obtain the quantized weight data of the first network layer.
[0013] In this implementation, the weights can be accurately and quickly quantized into a value represented by F bits.
[0014] In an optional implementation, before determining the first reference value based on the floating-point weight data of the first network layer, the method further includes: determining the number of bits F of the first quantization processing based on the distribution discreteness of multiple numerical values included in the floating-point weight data of the first network layer; determining multiple numerical intervals based on the first reference value includes: determining R numerical intervals based on the first reference value and F.
[0015] In this implementation, the number of bits F for the first quantization process is determined based on the distribution discreteness of multiple values included in the first weight data to be quantized; the number of bits occupied by the quantized data can be reduced, thereby saving computing resources.
[0016] In an optional implementation, after performing a second iteration based on the second neural network model to obtain a third neural network model, the method further includes: in response to the weights of all network layers included in the second neural network model having been quantized, performing a third iteration based on the third neural network model to obtain a fourth neural network model, wherein, in the third iteration, activation data of at least one third network layer of the third neural network model is quantized and input into the next network layer, and activation data of at least one fourth network layer of the third neural network model is not quantized.
[0017] In this implementation, the activation data is quantized, and the weight data is updated using a second prediction result obtained based on the quantized activation data, so that accurate prediction results can be obtained when the obtained third neural network model is used for prediction processing and the activation data is quantized.
[0018] In an optional implementation, after obtaining the fourth neural network model, the method further includes:
[0019] A fourth iteration is performed based on the fourth neural network model to obtain a target neural network model; wherein, in the fourth iteration, the activation data of the at least one third network layer and the activation data of the at least one fourth network layer are both quantized and input into the next network layer.
[0020] In this implementation, the weight data can be optimized quickly by using a layered quantization activation data approach.
[0021] In an optional implementation, the third iteration based on the third neural network model to obtain the fourth neural network model includes: obtaining a first operation parameter required for a first operation processing, the first operation processing being an operation processing obtained by merging a network model processing performed by at least one third network layer in the first neural network model and a second quantization processing for quantizing activation data of the at least one third network layer, the first operation parameter including a quantization parameter corresponding to the second quantization processing; performing fixed-point processing on the floating-point numbers in the first operation parameter to obtain a second operation parameter; performing the first operation processing based on the second operation parameter to obtain a processing result of the at least one third network layer; and obtaining the fourth neural network model based on the processing result of the at least one third network layer.
[0022] In this implementation, the entire process is implemented using fixed-point numbers, which can reduce the overhead of computing resources.
[0023] In an optional implementation, the fixed-point processing of the floating-point numbers in the first operation parameter to obtain the second operation parameter includes: expanding each floating-point number included in the first operation parameter by K times to obtain an expanded value, where K is an integer greater than 1; and converting the expanded value into a fixed-point number to obtain the second operation parameter.
[0024] In this implementation, the floating-point number in the first operation parameter is first expanded by K times, and then the floating-point number expanded by K times is converted into a fixed-point number, so that the accuracy of the first operation processing can be guaranteed.
[0025] In an optional implementation, the quantization parameter corresponding to the second quantization processing is obtained based on a quantization maximum value and a quantization minimum value, and the quantization maximum value and the quantization minimum value are obtained by clustering multiple values included in the activation data obtained from the sample image.
[0026] In an optional implementation, in the first iteration, activation data of at least one fourth network layer included in the first neural network model is quantized, and activation data of multiple fifth network layers included in the first neural network model are floating point numbers;
[0027] In the second iteration, activation data of the at least one fourth network layer and at least one fifth network layer of the plurality of fifth network layers adjacent to the at least one fourth network layer are quantized.
[0028] In an optional implementation, in the first prediction process, activation data of at least one sixth network layer included in the first neural network model is quantized.
[0029] In this implementation, activation data of at least one sixth network layer is quantized in the first prediction process, and accurate prediction results can be obtained when the obtained target neural network model is used for prediction processing and the activation data is quantized.
[0030] In a second aspect, an embodiment of the present application provides a data processing device, comprising: a first iteration unit, configured to perform a first iteration based on a first neural network model to obtain a second neural network model, wherein, in the first iteration, the weight of at least one first network layer included in the first neural network model is quantized, and the weights of multiple second network layers included in the first neural network model are floating point numbers; a second iteration unit, configured to perform a second iteration based on the second neural network model to obtain a third neural network model, wherein, in the second iteration, the weights of the at least one first network layer and at least one second network layer adjacent to the at least one first network layer among the multiple second network layers are quantized.
[0031] In an optional implementation, the network depth of the at least one first network layer is greater than the network depth of the at least one second network layer.
[0032] In an optional implementation, the first iteration unit is specifically used to perform a first prediction process on the first sample through the first neural network model to obtain a first prediction result; based on the first prediction result, the quantization weight data of the at least one first network layer and the floating-point weight data of the multiple second network layers are updated to obtain the second neural network model.
[0033] In an optional implementation, the device also includes: a quantization unit, used to determine a first reference value based on the floating-point weight data of the first network layer; based on the first reference value, determine multiple numerical intervals; each of the multiple numerical intervals corresponds to a numerical value that can be represented by F bits, and F is an integer greater than 1 and less than 8; based on the multiple numerical intervals, perform a first quantization processing on the weights in the floating-point weight data to obtain the quantized weight data of the first network layer.
[0034] In an optional implementation, the quantization unit is also used to determine the number of bits F of the first quantization processing based on the distribution discreteness of multiple numerical values included in the floating point of the first network layer; the quantization unit is specifically used to determine R numerical intervals based on the first reference value and the F.
[0035] In an optional implementation, the device also includes: a third iteration unit, used to perform a third iteration based on the third neural network model in response to the weights of all network layers included in the second neural network model having been quantized, so as to obtain a fourth neural network model, wherein, in the third iteration, the activation data of at least one third network layer of the third neural network model is quantized and input to the next network layer, and the activation data of at least one fourth network layer of the third neural network model is not quantized.
[0036] In an optional implementation, the device also includes: a fourth iteration unit, used to perform a fourth iteration based on the fourth neural network model to obtain a target neural network model; wherein, in the fourth iteration, the activation data of the at least one third network layer and the activation data of the at least one fourth network layer are both quantized and input into the next network layer.
[0037] In an optional implementation, the first iteration unit is specifically used to obtain first operation parameters required for a first operation processing, wherein the first operation processing is an operation processing obtained by merging the network model processing performed by at least one third network layer in the first neural network model and the second quantization processing for quantizing the activation data of the at least one third network layer, and the first operation parameters include the quantization parameters corresponding to the second quantization processing; the floating-point numbers in the first operation parameters are fixed-point processed to obtain second operation parameters; based on the second operation parameters, the first operation processing is performed to obtain the processing result of the at least one third network layer; based on the processing result of the at least one third network layer, the fourth neural network model is obtained.
[0038] In an optional implementation, the first iteration unit is specifically used to expand each floating-point number included in the first operation parameter by K times to obtain an expanded value, where K is an integer greater than 1; and convert the expanded value into a fixed-point number to obtain the second operation parameter.
[0039] In an optional implementation, the quantization parameter corresponding to the second quantization processing is obtained based on a quantization maximum value and a quantization minimum value, and the quantization maximum value and the quantization minimum value are obtained by clustering multiple values included in the activation data obtained from the sample image.
[0040] In an optional implementation, in the first iteration, the activation data of at least one fourth network layer included in the first neural network model is quantized, and the activation data of multiple fifth network layers included in the first neural network model are floating point numbers; in the second iteration, the activation data of at least one fourth network layer and at least one fifth network layer adjacent to the at least one fourth network layer among the multiple fifth network layers are quantized.
[0041] In an optional implementation, in the first prediction process, activation data of at least one fifth network layer included in the first neural network model is quantized.
[0042] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a memory for storing programs; a processor for executing the programs stored in the memory, and when the program is executed, the processor is used to execute the method as described in the first aspect and any optional implementation method mentioned above.
[0043] In a fourth aspect, an embodiment of the present application provides a chip, which includes a processor and a data interface. The processor reads instructions stored in a memory through the data interface to execute the method of the first aspect and any optional implementation method described above.
[0044] In a fifth aspect, an embodiment of the present application provides a computer-readable storage medium, which stores a computer program, and the computer program includes program instructions, which, when executed by a processor, enable the processor to execute the method of the above-mentioned first aspect and any optional implementation method.
[0045] In a sixth aspect, an embodiment of the present application provides a computer program product, which includes program instructions, and when the program instructions are executed by a processor, the processor executes the method of the above-mentioned first aspect and any optional implementation method. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the background technology, the drawings required for use in the embodiments of the present application or the background technology will be described below.
[0047] Figure 1 A flowchart of a retraining method for a neural network model provided in an embodiment of the present application;
[0048] Figure 2 A flowchart of a neural network model quantization method provided in an embodiment of the present application;
[0049] Figure 3 A flow chart of another method for retraining a neural network model provided in an embodiment of the present application;
[0050] Figure 4 A flow chart of a data processing method provided in an embodiment of the present application;
[0051] Figure 5 A flow chart of another data processing method provided in an embodiment of the present application;
[0052] Figure 6 A flowchart of a retraining method for a neural network model provided in an embodiment of the present application;
[0053] Figure 7 A schematic diagram of the structure of a data processing device provided in an embodiment of the present application;
[0054] Figure 8 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0055] Fig. 9 A schematic diagram of the structure of a server provided in an embodiment of the present application. DETAILED DESCRIPTION
[0056] The terms "first", "second", and "third" etc. in the specification embodiments and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequential order. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, including a series of steps or units. Methods, systems, products or equipment are not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or equipment. "And / or" is used to represent the selection of one or all between the two objects connected thereto. For example, "A and / or B" means A, B or A+B.
[0057] Figure 1 A flowchart of a retraining method for a neural network model provided in an embodiment of the present application. Figure 1 As shown, the method may include:
[0058] 101. The data processing device performs a first iteration based on the first neural network model to obtain a second neural network model.
[0059] Wherein, in the above-mentioned first iteration, the weight of at least one first network layer included in the above-mentioned first neural network model is quantized, and the weights of multiple second network layers included in the above-mentioned first neural network model are floating point numbers. The above-mentioned data processing device can be a terminal device such as a mobile phone, a tablet computer, a wearable device, a laptop computer, a desktop computer, or a server. The first neural network model can be any one of a deep neural network model, a gated recurrent unit (GRU), a long short-term memory network (LSTM), etc., which is not limited in this application. The above-mentioned at least one first network layer can be any network layer in the above-mentioned first neural network model; it can also be multiple adjacent network layers in the first neural network model; it can also include any network layer in the first neural network model; this application does not limit it.
[0060] Optionally, the data processing device performs a first iteration based on the first neural network model to obtain the second neural network model, which may be: performing a first prediction process on the first sample through the first neural network model to obtain a first prediction result; obtaining first weight data based on the first prediction result; the first weight data includes the weight to be updated of the at least one first network layer; performing a first quantization process on the first weight data to obtain second weight data; the second weight data includes the quantized weight of the at least one first network layer. It should be understood that the data processing device may quantize the updated weight data of a part of the network layers, but not quantize the updated weight data of another part of the network layers during the first iteration of the first neural network model. For example, the neural network model includes 50 network layers, wherein the data processing device quantizes the weight data of the 41st to 50th network layers of the first neural network model during the first iteration of the first neural network model, but does not quantize the weight data of the 1st to 40th network layers, that is, keeps them as floating point numbers, and sequentially processes the input sample data using the floating point weight data of the 1st to 40th layers and the quantized weight data of the 41st to 50th layers to obtain the processing results, and then updates the floating point weight data of the 1st to 40th layers and the quantized weight data of the 41st to 50th layers based on the processing results to obtain the second neural network model. In some embodiments, the data processing device performs the first iteration based on the first neural network model, which may be that the data processing device uses one or more sample images to perform one or more iterations on the first neural network model, and should not be understood as only using the first prediction result to update the first neural network model. For example, the data processing device performs the first iteration based on the first neural network model to obtain the second neural network model, which may be that the data processing device performs one iteration update on the first neural network model or performs multiple iteration updates in sequence to obtain the second neural network model.
[0061] 102. Perform a second iteration based on the second neural network model to obtain a third neural network model.
[0062] Wherein, in the above-mentioned second iteration, the weights of at least one of the above-mentioned at least one first network layer and at least one of the above-mentioned multiple second network layers adjacent to the above-mentioned at least one first network layer are quantized, wherein the number of at least one second network layer and the number of at least one first network layer may be the same or different. Continuing with the previous example, after the data processing device completes the first iteration to obtain the second neural network model, the weight data of the 31st to 40th network layers of the second neural network model are further quantized. At this time, the weights of the 31st to 50th layers of the second neural network model are quantized, that is, quantized weights, while the weights of the 1st to 30th network layers remain as floating point numbers. The processing of the input sample data in the second neural network model may include processing based on the floating point weight data of the 1st to 30th layers and the quantized weight data of the 31st to 50th layers in sequence. Accordingly, the parameter update of the second neural network model may include updating processing of the floating point weight data of the 1st to 30th layers and updating processing of the quantized weight data of the 31st to 50th layers. In some embodiments, the data processing device may perform the second iteration based on the second neural network model once or more times. For example, the number of times the data processing device performs the second iteration may be the same as or different from the number of times the data processing device performs the first iteration. This is not limited in the embodiments of the present disclosure.
[0063] Optionally, the network depth of the at least one first network layer is greater than the network depth of the at least one second network layer. Experience shows that most neural network models are more sensitive to accuracy the closer they are to the input end, so retraining the neural network model can be performed layer by layer in a back-to-front order to improve training efficiency.
[0064] Optionally, if all weights in the obtained third neural network model are quantized, it can be determined whether to stop iterative updating based on the model training cutoff condition, which can include, but is not limited to, that the accuracy of the model obtained by retraining is close to the accuracy of the original neural network model. Optionally, if the obtained third neural network model also includes one or more network layers whose weights are not quantized, the weights of one or more network layers adjacent to at least one second network layer in the third neural network model can continue to be quantized, and one or more iterations can be performed until the weights of all network layers of the neural network model are quantized.
[0065] Optionally, the number of iterative updates included in the first iteration and / or the second iteration can also be determined based on the model accuracy, for example, the difference between the prediction accuracy of the second neural network model and the original prediction accuracy of the first neural network model or the prediction accuracy of the original neural network model is less than the first threshold; the difference between the prediction accuracy of the third neural network model and the original prediction accuracy of the first neural network model is less than the first threshold. The original prediction accuracy is the prediction accuracy of the first neural network model, for example, 85%, 90%, 95%, etc. The first threshold can be 10%, 8%, 5%, 3%, 1%, etc. The second iteration based on the second neural network model to obtain the third neural network model can be: the data processing device performs prediction processing on one or more sample images through the second neural network model to obtain one or more prediction results; based on the one or more prediction results, the second neural network model is updated; until the difference between the prediction accuracy of the second neural network model and the original prediction accuracy is less than the first threshold, the third neural network model is obtained.
[0066] Since the network parameters in the quantized neural network model occupy fewer bits, the computing resources and storage resources consumed by using the quantized neural network model to perform prediction processing tasks will be reduced. Figure 1 The neural network model quantization scheme in compresses the neural network model, which can maintain the prediction accuracy of the neural network model and reduce resource consumption.
[0067] In the embodiment of the present application, iterative training is performed in a layered quantization manner to ensure the prediction accuracy of the neural network model.
[0068] The above embodiments do not describe in detail the method of quantizing the weights in the neural network model. The following takes the quantization of the weights of at least one first network layer in the first neural network model as an example to introduce the implementation method of quantizing the weights in the neural network model.
[0069] Figure 2 A flowchart of a neural network model quantization method provided in an embodiment of the present application. Figure 2 As shown, the method may include:
[0070] 201. The data processing device determines a first reference value based on first weight data in a first neural network model.
[0071] Optionally, the first weight data includes the weights to be updated of at least one first network layer of the first neural network model. Optionally, the data processing device uses the average value of each value in the first weight data as the first reference value. Optionally, the data processing device uses the median of each value in the first weight data as the first reference value. The data processing device can also determine the first reference value in other ways, which is not limited in the embodiments of the present application.
[0072] 202. Determine R numerical intervals based on the first reference value and the number of bits F of the first quantization process.
[0073] Optionally, the above R is 2 to the Fth power. In some embodiments, the data processing device may pre-store the number of bits F of the first quantization process. Accordingly, when the data processing device needs to determine the quantization parameter, it obtains the number of bits F corresponding to the first quantization process stored from the memory. In some embodiments, before executing step 202, the data processing device may perform the following operations: based on the distribution discreteness of the multiple values included in the above first weight data, determine the number of bits F of the first quantization process. Optionally, the above number of bits F is positively correlated with the distribution discreteness of the multiple values included in the above first weight data. That is, the more dispersed the values included in the above first weight data, the larger the number of bits F of the first quantization process. The above F is an integer greater than 0, such as 1, 2, 3, 4, 6, etc. Exemplarily, the distribution discreteness of the multiple values included in the above first weight data is any one of the variance, standard deviation or range of the multiple values. For example, if the distribution discreteness of the multiple values included in the above first weight data is not greater than the discrete threshold, the number of bits F of the first quantization process is determined to be 2; otherwise, the number of bits F of the first quantization process is determined to be 4. The implementation of step 202 will be further described later and will not be expanded here. The discrete threshold value may be 10, 20, etc., and is not limited in the embodiment of the present application.
[0074] 203. Based on the R numerical intervals, quantize the numerical values in the first weight data.
[0075] Exemplarily, the value in the first weight data is quantized into a value of F bits. For example, alpha1 is a first reference value, W is any weight in the network parameter, and based on the first reference value, the four value intervals determined are as follows:
[0076] When W>=alpha1, W≈alpha1*3 / 2;
[0077] When 0 <W<alpha1,W≈alpha1 / 2;
[0078] When W <= -alpha1, W ≈ alpha1*(-3) / 2;
[0079] When -alpha1 <W<=0,W≈alpha1*(-1) / 2。
[0080] Since alpha1 is a common factor, the quantized values after extracting alpha1 / 2 are 3, 1, -1, -3, so 2 bits can be used to express it. When storing in hardware, -3, -1, 1, 3 can be mapped to 0, 1, 2, 3 through a mapping relationship. In other words, weights greater than alpha1 in the first weight data are quantized to the product of alpha1 / 2 and 3; weights greater than 0 and less than alpha1 in the first weight data are quantized to the product of alpha1 / 2 and 1; weights less than or equal to -alpha1 in the first weight data are quantized to the product of alpha1 / 2 and -3; weights greater than -alpha1 and less than 0 in the first weight data are quantized to the product of alpha1 / 2 and -1. The above example is a 2-bit quantization method. The embodiments of the present application also derive 3-bit, 4-bit, 5-bit and other quantization methods. The main idea is to use binary division to divide the quantization order. For 3-bit quantization, the quantization order is further expanded compared to 2-bit. When the weight is greater than 0, alpha1 / 2 and alpha1*3 / 2 are replaced by alpha1 / 4, alpha1*3 / 4, alpha1*5 / 4 and alpha1*7 / 4. When the weight is less than 0, -alpha1 / 2 and -alpha1*3 / 2 are replaced by -alpha1 / 4, -alpha1*3 / 4, -alpha1*5 / 4 and -alpha1*7 / 4. Similarly, 4-bit quantization and higher-bit quantization also use binary division to perform a more detailed split of the quantization order. In other words, the data processing device can use different bits to quantize the weights according to the characteristics of the neural network model. In addition, the weights of different layers in the neural network model can be quantized using different bits. For example, the data processing device uses 2 bits to quantize the weights of at least one first network layer in the first neural network model, and uses 4 bits to quantize the weights of at least one second network layer in the first neural network model.
[0081] The neural network model quantization method proposed in the embodiment of the present application can adjust the bit width of the weight (i.e., the bit position of the quantized weight) according to the characteristics of the neural network model, thereby reducing the network performance loss caused by quantization.
[0082] The following describes an implementation method for determining the number of bits F for the first quantization process based on the distribution discreteness of the multiple values included in the first weight data.
[0083] Exemplarily, the data processing device determines the number of bits F of the first quantization processing based on the variance of the multiple values included in the first weight data (corresponding to the distribution dispersion); the number of bits F is positively correlated with the variance of the multiple values included in the first weight data. For example, the variance can be compared with one or more thresholds to determine the number of bits F. When the variance of the multiple values included in the first weight data is less than the first threshold, the data processing device determines that the number of bits F of the first quantization processing is 2; when the variance of the multiple values included in the first weight data is not less than the first threshold, the data processing device determines that the number of bits F of the first quantization processing is 4. The first threshold can be 0.016, 0.2, 1.2, 10, 100, etc., and this application does not limit it. For another example, multiple segmented intervals may be set, and when the variance of the multiple values included in the first weight data is less than the second threshold and greater than the third threshold, the data processing device determines that the number of bits F of the first quantization processing is 4; when the variance of the multiple values included in the first weight data is not less than the second threshold, the number of bits F of the first quantization processing is determined to be 6; when the variance of the multiple values included in the first weight data is not greater than the third threshold, the number of bits F of the first quantization processing is determined to be 2. The third threshold may be 0.016, 0.2, 1.2, etc., and the second threshold may be 10, 25, etc., which are not limited in this application.
[0084] Exemplarily, the data processing device determines the number of bits F of the first quantization processing based on the range of multiple values included in the above-mentioned first weight data; the above-mentioned number of bits F is positively correlated with the range of multiple values included in the above-mentioned first weight data. The range of multiple values included in the above-mentioned first weight data is the difference between the maximum value and the minimum value included in the multiple values. For example, when the range of multiple values included in the above-mentioned first weight data is less than a fourth threshold, the data processing device determines that the number of bits F of the first quantization processing is 3; when the range of multiple values included in the above-mentioned first weight data is not less than the fourth threshold, the data processing device determines that the number of bits M of the first quantization processing is 4. The fourth threshold value can be 1.2, 10, 100, etc., and is not limited in this application. For another example, when the range of the multiple values included in the first weight data is less than the fifth threshold and greater than the sixth threshold, the data processing device determines that the number of bits F of the first quantization process is 4; when the range of the multiple values included in the first weight data is not less than the fifth threshold, the data processing device determines that the number of bits F of the first quantization process is 6; when the range of the multiple values included in the first weight data is not greater than the sixth threshold, the data processing device determines that the number of bits M of the first quantization process is 2. The sixth threshold can be 0.016, 0.2, 1.2, etc., and the fifth threshold can be 10, 25, etc., which is not limited in the embodiments of the present application.
[0085] Exemplarily, the data processing device determines the number of bits F of the first quantization processing based on the standard deviation of the multiple values included in the first weight data; the number of bits F is positively correlated with the standard deviation of the multiple values included in the first weight data. For example, when the standard deviation of the multiple values included in the first weight data is less than the seventh threshold, the data processing device determines that the number of bits F of the first quantization processing is 3; when the standard deviation of the multiple values included in the first weight data is not less than the seventh threshold, the data processing device determines that the number of bits F of the first quantization processing is 4. The seventh threshold can be 0.04, 0.2, 1.2, 10, etc., and is not limited in the embodiments of the present application.
[0086] Understandably, Figure 2 This is an example of quantizing some network parameters in the first neural network model. The data processing device can quantize the network parameters of each network layer in the first neural network model in a similar manner.
[0087] In this implementation, the data processing device determines the number of bits F for the first quantization processing based on the distribution of multiple numerical values included in the first weight data; it is able to reduce the bits used for quantization, thereby reducing the computing resources required for predictive processing using the first neural network model.
[0088] Not detailed above Figure 1 The implementation of step 101 and step 102 in the embodiment of the present invention. The following takes the first iteration based on the first neural network model to obtain the second neural network model as an example to describe how to retrain the neural network model.
[0089] Figure 3 This is a flow chart of another retraining method of a neural network model provided in an embodiment of the present application. Figure 3 As shown, the method may include:
[0090] 301. A data processing device performs a first prediction process on a first sample through a first neural network model to obtain a first prediction result.
[0091] The first sample may be image data, speech data, or data obtained by preprocessing image data, etc. The first prediction process may be image classification, image text recognition, speech recognition, and the like.
[0092] 302. Based on the first prediction result, obtain first weight data.
[0093] The first weight data includes the weights to be updated of at least one first network layer of the first neural network model. In some embodiments, the first weight data is a floating-point weight to be updated of at least one first network layer of the first neural network model, that is, the weight data of at least one first network layer is to be updated to floating-point first weight data.
[0094] 303. Perform a first quantization process on the first weight data to obtain second weight data.
[0095] The second weight data includes the quantized weight of the at least one first network layer. Figure 2 The method flow is not described in detail here. It should be understood that Figure 3 An example of using a first sample to train a first neural network model is provided. In practical applications, the data processing device can use multiple samples to adopt similar Figure 3 The first neural network model is trained according to the method flow in until the difference between the prediction accuracy of the second neural network model obtained by training the first neural network model and the original prediction accuracy is less than the first threshold. Figure 3 The method flow in is only an example of training the first neural network model. It should be understood that the data processing device can train the neural network model in a similar manner.
[0096] In the embodiment of the present application, the weight to be updated of at least one first network layer can be quantified accurately and quickly.
[0097] In some embodiments, the data processing device may convert the operation processing performed by the neural network model into fixed-point operations during the training process, thereby reducing the consumption of computing resources and storage resources. An optional implementation of step 301 is described below, in which the data processing device may convert at least part of the floating-point operations into fixed-point operations.
[0098] Figure 4 A flow chart of a data processing method provided in an embodiment of the present application. Figure 4 As shown, the method may include:
[0099] 401. A data processing device obtains a first operation parameter required for a first operation.
[0100] The above-mentioned first operation processing is an operation processing obtained by merging the network model processing performed by at least one third network layer and the second quantization processing for quantizing the activation data, and the above-mentioned first operation parameter includes the quantization parameter corresponding to the above-mentioned second quantization processing. The activation data of a network layer in the neural network model may refer to the output data of the network layer (i.e., the output feature map). The above-mentioned at least one third network layer may be a network layer in the first neural network model. The above-mentioned network model processing may include at least one of convolution processing, pooling processing, batch normalization processing, etc., which is not limited in this application.
[0101] Optionally, the quantization parameter corresponding to the second quantization process is determined based on the maximum quantization value and the minimum quantization value; the maximum quantization value and the minimum quantization value are obtained by clustering multiple values included in the reference data, and the reference data is the activation data obtained by the third network layer based on the training image or the test image. The third network layer can be any network layer except the last layer in the first neural network model. The second quantization process can be a quantization process performed on the activation data of the at least one third network layer. For example, the operation process performed by at least one third network layer is: Qout1 = x*A+y*B; wherein A and B are matrices of the same size, and x and y are floating point numbers; the second quantization process corresponding to the at least one third network layer is: Qout2 = ceil[(Qout1-Q / 2) / Q], wherein Qout1 represents the activation data output by the at least one third network layer, and Qout2 represents the quantized activation data; the first operation process performed by the at least one third network layer is: Qout = ceil[(x*A+y*BQ / 2) / Q]. ceil is the name of a function that returns the smallest integer greater than or equal to the specified expression. ceil(x) is the smallest integer greater than or equal to x.
[0102] 402. Perform fixed-point processing on the floating-point number in the first operation parameter to obtain a second operation parameter.
[0103] Optionally, a method for implementing the second operation parameter by performing fixed-point processing on the floating-point number in the first operation parameter is as follows: each floating-point number included in the first operation parameter is enlarged by K times to obtain an enlarged value, where K is an integer greater than 1; the enlarged value is converted into a fixed-point number to obtain the second operation parameter. For example, x is a floating-point number in the first operation parameter, and x is enlarged by K times to obtain x*K; x*K is converted into a fixed-point number to obtain round(x*K). The round function rounds a number to a specified number of digits, and its function formula is: round(number to be rounded, number of digits to be retained). For example, round(3.1415926,2)=3.14, round(3.1415926,3)=3.142. In some embodiments, the round function can be a function that rounds a floating-point number to an integer. For example, round(3.1415926,0)=3, round(7.76)=8. It should be understood that the data processing device can convert each floating-point number included in the first operation parameter into a fixed-point number in a similar manner.
[0104] 403. Based on the second operation parameter, perform the first operation to obtain a processing result of the at least one third network layer.
[0105] For example, the first operation process is: Qout=ceil[(x*A+y*BQ / 2) / Q]; the first operation parameters include x, y, Q, A and B, wherein x, y and Q are all floating point numbers; the second operation parameters include round(x*K), round(y*K)round(Q / 2*K), round(Q*K), A and B; based on the second operation parameters, the first operation process is performed as: Out=ceil[(round(x*K)*A+round(y*K)-round(Q / 2*K)) / round(Q*K)]. It should be understood that Figure 4 This is only an example of a data processing device implementing the first operation process in the process of training the first neural network model. In some embodiments, the data processing device can implement the operation process and quantization process of each network layer in a similar manner in the process of training the second neural network model.
[0106] 404. Obtain a first prediction result based on the processing result of the at least one third network layer.
[0107] It should be understood that the data processing device can obtain the processing results of each layer in the second neural network model in a similar manner. Optionally, the data processing device obtains the final prediction result based on the processing results of each layer in the second neural network model.
[0108] In an embodiment of the present application, the data processing device uses fixed-point numbers to implement the entire process of executing the prediction processing task using the first neural network model, which can reduce the overhead of computing resources.
[0109] The above embodiments do not describe in detail the implementation of step 402 and step 403. The following describes the application of step 402 and step 403 in some embodiments.
[0110] Embodiment 1
[0111] The network model processing performed by the Lth layer (Eltwise layer) in the first neural network model is: Out = x*A+y*B, where x and y are both floating point numbers, and A and B are two input feature maps of the Eltwise layer; the quantization processing performed by the Lth layer is as follows: Qout = ceil[(Qin-Q / 2) / Q], where Qin represents the activation data output by the Lth layer, that is, the quantized input; Qout represents the quantized activation data, that is, the quantized output, and Q represents the quantization parameter; the first operation processing performed by the Lth layer is: Qout = ceil[(x*A+y*BQ / 2) / Q]. Exemplarily, the data processing device converts the first operation parameter required for the Lth layer to perform the first operation processing into a fixed-point number to obtain a second operation parameter, and the calculation formula corresponding to performing the above-mentioned first operation processing based on the above-mentioned second operation parameter is as follows: Out = ceil[(round(x*K)*A+round(y*K)-round(Q / 2*K)) / round(Q*K)].
[0112] Embodiment 2
[0113] The network model processing (convolution operation) performed by the Lth layer (convolution layer) in the first neural network model is: Conv_out = W × A, where W is the weight matrix (weight) of the Lth layer, and A is the input feature map (ie, activation) of the Lth layer. Assume that the current quantization adopts 2W4A, that is, the bit width of each element in the weight matrix W after quantization is 2 bits, and the bit width of each element in the input feature map A after quantization is 4 bits. W = alpha*B, A = Qparam*M, where B = {-3, -1, 1, 3}, M = {0-shift, 1-shift, ..., 15-shift}, alpha (floating point number) represents the common factor corresponding to the weight matrix, B represents the quantized weight matrix, Qparam (floating point number) represents the common factor corresponding to the input feature map, and M represents the quantized input feature map. Conv_out=W×A=alpha*Qparam*(B×M)=scale*(B*M), so the convolution operation can extract the common factor scale (i.e., alpha*Qparam), and fixed-point convolution operation is performed by B and M during hardware operation. The calculation formula for quantizing the activation data output by the data processing device of the Lth layer is as follows: Qout=ceil[(Qin-Q / 2) / Q], where Q represents the quantization parameter of the Lth layer. It should be understood that the calculation formula for the first operation processing performed by the Lth layer is expressed as follows: Qout=ceil[(scale*(B*M)-Q / 2) / Q]. Exemplarily, the data processing device converts the floating-point numbers in the first operation parameters required for the Lth layer to perform the first operation processing into fixed-point numbers to obtain the second operation parameters, and the calculation formula corresponding to performing the above-mentioned first operation processing based on the above-mentioned second operation parameters is as follows: Qout=ceil[(round(scale*K)*(B*M)-round(Q / 2*K)) / round(Q / 2*K)].
[0114] Embodiment 3
[0115] The network model processing performed by the Lth layer (convolutional layer) in the first neural network model is: Conv_out = W × A, where W is the weight matrix (weight) of the Lth layer, A represents the input feature map of the Lth layer, W = alpha*B, A = Qparam*M, alpha (floating point number) represents the common factor corresponding to the weight matrix, B represents the quantized weight matrix, Qparam (floating point number) represents the common factor corresponding to the input feature map, and M represents the quantized input feature map. Conv_out = W × A = alpha*Qparam*(B×M) = scale*(B*M). Assume that part of the network structure in the neural network model is convolution layer (i.e., Lth layer) -> batch normalization layer (i.e., BN layer) -> activation layer (i.e., relu layer). The operation processing performed by the convolution layer, the operation processing (linear operation) processing performed by the batch normalization layer, and the operation processing (linear operation) processing performed by the activation layer are combined to obtain the operation processing: BNout = scale1*A+bias, scale1 is the floating point number after the merger (including alpha, Qparam, etc.), and A is the output result of the convolution layer. Next, BNout is quantized. The quantization function can be: Qout = ceil[(Qin-Q / 2) / Q], Qin is BNout, Q / 2 can be merged into bias, and finally the first operation processing performed by conv->BN->relu can be simplified to: Qout = ceil[(scale1*A+bias) / Q], where scale1, bias, and Q are all floating point numbers. Next, the floating point number elimination is performed. Exemplarily, the data processing device converts the floating-point numbers in the first operation parameters required for the Lth layer to perform the first operation processing into fixed-point numbers to obtain second operation parameters, and the corresponding calculation formula for performing the above-mentioned first operation processing based on the above-mentioned second operation parameters is as follows: Qout = ceil[(round(N*scale)*A+round(N*bias)) / round(N*Q)] = ceil[(scale_new*A+bias_new) / Q_new], where scale_new, bias_new and Q_new are all fixed-point numbers.
[0116] When the data processing device uses the neural network model to perform prediction processing tasks, the calculations performed will contain floating-point numbers, such as batch normalization, scaling operations, etc. In addition, the quantization process will also introduce floating-point numbers, such as the quantization parameter Q, etc. Taking into account the complexity, area, computing resources and other factors of the neural network accelerator hardware implementation, the method provided in the embodiment of the present application can eliminate all floating-point parameters and floating-point operations in the neural network model, and the entire process uses fixed-point operations, which can improve computing efficiency.
[0117] In some embodiments, before quantizing the first neural network model obtained through training, the data processing device may first determine the quantization parameters of the quantization processing corresponding to each network layer in the first neural network model. The following is an example of first determining the quantization parameters of each network layer in the neural network model and then quantizing the weights in the neural network model.
[0118] It should be understood that when the data processing device is executing Figure 4 Before the method flow in the above, it is necessary to obtain the quantization parameters used to quantize the activation data. The following describes the method for determining the quantization parameters used to quantize the activation data.
[0119] Figure 5 Another data processing method flow chart provided in the embodiment of the present application is a method flow chart for determining the quantization parameters of the activation data obtained by the first neural network model. Figure 5 As shown, the method may include:
[0120] 501. A data processing device obtains data to be quantized outputted by the Lth layer of a first neural network model.
[0121] The data to be quantized (i.e., activation data) includes at least one value represented by M or more bits, L is an integer greater than 0, and M is an integer greater than 1. M can be 6, 8, 12, 16, etc. Optionally, the data to be quantized is the activation data output by the Lth layer of the neural network model when the data processing device inputs a training sample into the neural network model for prediction processing.
[0122] 502. Cluster multiple values included in the data to be quantified to obtain a clustering result.
[0123] 503. Taking the maximum value of at least two cluster centers included in the clustering result as the quantized maximum value, and taking the minimum value of at least two cluster centers as the quantized minimum value.
[0124] 504. Determine a first arithmetic progression that has the minimum quantization value as the first item and the maximum quantization value as the last item and includes N values.
[0125] In some embodiments, before executing step 504, the data processing device may perform the following operation: obtain the number M of bits to be used to quantize the numerical values included in the above-mentioned data to be quantized. The number of bits M may be preset, for example, the data processing device or other device may pre-store the number of bits to be used to quantize the numerical values included in the above-mentioned data to be quantized. Accordingly, when it is necessary to determine the quantization parameter, the stored one is obtained from the memory. In other embodiments, the number of bits M may also be determined based on the data to be quantized, for example, based on the distribution discreteness of multiple numerical values included in the above-mentioned data to be quantized, the number M of bits to be used to quantize the numerical values included in the above-mentioned data to be quantized is determined. Alternatively, the number of bits M may also be determined based on other distribution characteristics or parameters of the data to be quantized. Optionally, the above-mentioned N is 2 to the Mth power.
[0126] 505. Use the common factors of each item in the first arithmetic progression as the quantization parameters of the Lth layer of the neural network model, and store the quantization parameters.
[0127] For example, the maximum quantization value (max) is 1.3, the minimum quantization value (min) is -0.2, and M is 4; determine the first arithmetic progression {-0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8. 0.9, 1.0, 1.1, 1.2, 1.3} with -0.2 as the first item and 1.3 as the last item, and include 16 values; extract the common factor 0.1 of each item in the first arithmetic progression as the quantization parameter.
[0128] It can be understood that steps 501 to 506 are a method flow for the data processing device to determine the quantization parameters of the Lth layer in the neural network model. In practical applications, the data processing device can use a similar method flow to determine the quantization parameters of each layer in the neural network model, and store the quantization parameters of each layer, so that when the neural network model is used to perform a prediction processing task, the quantization parameters of each layer are used to quantize the feature maps (i.e., activation data) output by each layer.
[0129] In some embodiments, the data processing device uses Figure 1 After quantizing the weights in the neural network model, you can also use layered quantization of activation data to further optimize the weights in the neural network model. The following describes how to use layered quantization of activation data to further optimize the weights in the neural network model.
[0130] Figure 6 A flowchart of a retraining method for a neural network model provided in an embodiment of the present application. Figure 6 As shown, the method may include:
[0131] 601. The data processing device performs a second prediction process on the second sample through a third neural network model to obtain a second prediction result.
[0132] In the second prediction process, the activation data of at least one fourth network layer of the third neural network model is quantized, and the activation data of at least one fifth network layer of the third neural network model is not quantized. The third neural network model may be executed by the data processing device. Figure 1 That is, the data processing device executes Figure 6 Before the method flow in Figure 1 The method flow in the method flow is used to obtain a third neural network model. For example, when the data processing device performs the second prediction processing on the second sample through the third neural network model, the activation data of the 41st network layer to the 50th network layer of the third neural network model is quantized, and the activation data of the 1st network layer to the 40th network layer is not quantized, to obtain the second prediction result; wherein the third neural network model includes 50 network layers.
[0133] 602. Based on the second prediction result, update the weight data in the third neural network model to obtain a fourth neural network model.
[0134] 603. Perform a third prediction process on the third sample using the fourth neural network model to obtain a third prediction result.
[0135] Among them, the activation data of at least one fourth network layer is quantized, the activation data of at least one fifth network layer is quantized, and the activation data of at least one sixth network layer of the fourth neural network model is not quantized. The implementation method of step 603 may be similar to the implementation method of step 601. Continuing with the previous example, when the data processing device performs the third prediction processing on the third sample through the fourth neural network model, the activation data of the 31st network layer to the 50th network layer of the fourth neural network model is quantized, and the activation data of the 1st network layer to the 30th network layer is not quantized, to obtain the third prediction result; wherein, the fourth neural network model includes 50 network layers.
[0136] 604. Based on the third prediction result, update the weight data in the fourth neural network model to obtain a target neural network model.
[0137] In the embodiment of the present application, the weight data can be quickly optimized by adopting a layered quantization activation data approach.
[0138] In some embodiments, the data processing device uses Figure 1In the process of quantizing the weights in the neural network model, the activation data output by at least some of the network layers in the neural network model may be quantized. Exemplarily, in the first prediction process, the activation data of at least one seventh network layer included in the first neural network model is quantized.
[0139] Figure 7 A schematic diagram of the structure of a data processing device provided in an embodiment of the present application. Figure 7 As shown, the data processing device includes:
[0140] A first iteration unit 701 is used to perform a first iteration based on the first neural network model to obtain a second neural network model, wherein in the first iteration, the weight of at least one first network layer included in the first neural network model is quantized, and the weights of multiple second network layers included in the first neural network model are floating point numbers;
[0141] The second iteration unit 702 is used to perform a second iteration based on the second neural network model to obtain a third neural network model, wherein in the second iteration, the weights of the at least one first network layer and at least one second network layer adjacent to the at least one first network layer among the multiple second network layers are quantized.
[0142] In an optional implementation, the network depth of the at least one first network layer is greater than the network depth of the at least one second network layer.
[0143] In an optional implementation, the first iteration unit 701 is specifically used to perform a first prediction process on the first sample through the first neural network model to obtain a first prediction result; based on the first prediction result, the quantization weight data of the at least one first network layer and the floating-point weight data of the multiple second network layers are updated to obtain the second neural network model.
[0144] In an optional implementation, the device further includes:
[0145] The quantization unit 703 is used to determine a first reference value based on the floating-point weight data of the first network layer; determine multiple numerical intervals based on the first reference value; each numerical interval in the multiple numerical intervals corresponds to a numerical value that can be represented by F bits, and F is an integer greater than 1 and less than 8; based on the multiple numerical intervals, perform a first quantization processing on the weights in the floating-point weight data to obtain the quantized weight data of the first network layer.
[0146] In an optional implementation, the quantization unit 703 is further configured to determine the number of bits F of the first quantization processing based on the distribution discreteness of multiple values included in the floating-point weight data of the network layer;
[0147] The quantization unit 703 is specifically configured to determine R numerical intervals based on the first reference value and F.
[0148] In an optional implementation, the above device further includes:
[0149] The third iteration unit 704 is used for performing a third iteration based on the third neural network model to obtain a fourth neural network model in response to the weights of all network layers included in the second neural network model being quantized, wherein in the third iteration, the activation data of at least one third network layer of the third neural network model is quantized and then input to the next network layer, and the activation data of at least one fourth network layer of the third neural network model is not quantized.
[0150] Optionally, the first iteration unit 701, the second iteration unit 702 and the third iteration unit 703 are the same unit or different units.
[0151] In an optional implementation, the device further includes:
[0152] The fourth iteration unit 705 is used to perform a fourth iteration based on the fourth neural network model to obtain a target neural network model; wherein, in the fourth iteration, the activation data of the at least one third network layer and the activation data of the at least one fourth network layer are both quantized and input into the next network layer.
[0153] In an optional implementation, the first iteration unit 701 is specifically used to obtain first operation parameters required for a first operation process, wherein the first operation process is an operation process obtained by merging a network model process performed by at least one third network layer and a second quantization process for quantizing activation data, and the first operation parameters include quantization parameters corresponding to the second quantization process; performing fixed-point processing on the floating-point numbers in the first operation parameters to obtain second operation parameters; performing the first operation process based on the second operation parameters to obtain the processing result of the at least one third network layer; and obtaining the first prediction result based on the processing result of the at least one third network layer.
[0154] In an optional implementation, the first iteration unit 701 is specifically used to expand each floating-point number included in the above-mentioned first operation parameter by K times to obtain an expanded value, where the above-mentioned K is an integer greater than 1; and convert the above-mentioned expanded value into a fixed-point number to obtain the above-mentioned second operation parameter.
[0155] In an optional implementation, the quantization parameter corresponding to the second quantization processing is obtained based on a quantization maximum value and a quantization minimum value, and the quantization maximum value and the quantization minimum value are obtained by clustering multiple values included in the activation data obtained from the sample image.
[0156] In an optional implementation, in the first iteration, the activation data of at least one fourth network layer included in the first neural network model is quantized, and the activation data of multiple fifth network layers included in the first neural network model are floating point numbers; in the second iteration, the activation data of at least one fourth network layer and at least one fifth network layer adjacent to the at least one fourth network layer among the multiple fifth network layers are quantized.
[0157] In an optional implementation, in the above-mentioned first prediction process, activation data of at least one fifth network layer included in the above-mentioned first neural network model is quantized.
[0158] It should be understood that the division of the various units of the above data processing device is only a division of logical functions. In actual implementation, they can be fully or partially integrated into one physical entity, or they can be physically separated. For example, the above units can be separately established processing elements, or they can be integrated into the same chip for implementation. In addition, they can also be stored in the storage element of the controller in the form of program code, and called and executed by a certain processing element of the processor. The functions of the above units. In addition, the various units can be integrated together or implemented independently. The processing element here can be an integrated circuit chip with signal processing capabilities. In the implementation process, the steps of the above method or the above units can be completed by hardware integrated logic circuits in the processor element or software instructions. The processing element can be a general-purpose processor, such as a central processing unit (CPU), or one or more integrated circuits configured to implement the above method, such as one or more application-specific integrated circuits (ASICs), or one or more microprocessors (DSPs), or one or more field programmable gate arrays (FPGAs).
[0159] Figure 8 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Figure 8As shown, the electronic device 80 includes a processor 801, a memory 802 and a communication interface 803; the processor 801, the memory 802 and the communication interface 803 are interconnected via a bus. Figure 8 The electronic device in the embodiment may be the data processing device in the foregoing embodiment.
[0160] The memory 802 includes, but is not limited to, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), or a portable read-only memory (CDROM), and the memory 802 is used for related instructions and data. The communication interface 803 is used to receive and send data.
[0161] The processor 1001 may be one or more central processing units (CPUs). When the processor 801 is a CPU, the CPU may be a single-core CPU or a multi-core CPU. Figure 8 Specifically, the processor 801 can implement Figure 7 The functions of each unit in it.
[0162] The processor 801 in the electronic device 80 is used to read the program code stored in the memory 802 and execute the data processing method in the above-mentioned embodiment.
[0163] Fig. 9 : is a structural diagram of a server 900 provided in an embodiment of the present application. The server 900 may have relatively large differences due to different configurations or performances, and may include one or more central processing units (CPU) 922 (for example, one or more processors) and a memory 932, and one or more storage media 930 (for example, one or more mass storage devices) storing application programs 942 or data 944. Among them, the memory 932 and the storage medium 930 may be temporary storage or permanent storage. The program stored in the storage medium 930 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations on the server. Furthermore, the central processing unit 922 may be configured to communicate with the storage medium 930 and execute a series of instruction operations in the storage medium 930 on the server 900. The server 900 may be a data processing device provided in an embodiment of the present application.
[0164] The server 900 may also include one or more power supplies 926, one or more wired or wireless network interfaces 950, one or more input and output interfaces 958, and / or one or more operating systems 941, such as Windows ServerTM, Mac OS XTM, UnixTM, LinuxTM, FreeBSDTM, etc.
[0165] The steps performed by the data processing device in the above embodiment can be based on the Fig. 9 Specifically, the central processor 922 can implement Figure 7 The functions of each unit in it.
[0166] In an embodiment of the present application, a computer-readable storage medium is provided. The computer-readable storage medium stores a computer program. When the computer program is executed by a processor, the method provided in the above embodiment is implemented.
[0167] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present application, and these modifications or replacements should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A retraining method for a neural network model, characterized in that: include: Performing a first iteration on the first neural network model using one or more sample images to obtain a second neural network model, wherein in the first iteration, the weight of at least one first network layer included in the first neural network model is quantized, and the weights of multiple second network layers included in the first neural network model are floating point numbers; Performing a second iteration based on the second neural network model to obtain a third neural network model, wherein in the second iteration, weights of the at least one first network layer and at least one second network layer adjacent to the at least one first network layer among the plurality of second network layers are quantized; In the first iteration, activation data of at least one fourth network layer included in the first neural network model is quantized, and activation data of multiple fifth network layers included in the first neural network model are floating point numbers; In the second iteration, activation data of the at least one fourth network layer and at least one fifth network layer of the plurality of fifth network layers adjacent to the at least one fourth network layer are quantized.
2. The method according to claim 1, characterized in that The network depth of the at least one first network layer is greater than the network depth of the at least one second network layer.
3. The method according to claim 1 or 2, characterized in that: The first neural network model is first iterated using one or more sample images to obtain a second neural network model, comprising: Performing a first prediction process on the first sample by using the first neural network model to obtain a first prediction result; Based on the first prediction result, the quantized weight data of the at least one first network layer and the floating-point weight data of the multiple second network layers are updated to obtain the second neural network model.
4. The method according to claim 1 or 2, characterized in that: The quantization of the floating point weight data of the first network layer includes: Determining a first reference value based on the floating-point weight data of the first network layer; Based on the first reference value, determine a plurality of numerical intervals; each of the plurality of numerical intervals corresponds to a numerical value that can be represented by F bits, where F is an integer greater than 1 and less than 8; Based on the multiple numerical intervals, a first quantization process is performed on the weights in the floating-point weight data to obtain quantized weight data of the first network layer.
5. The method according to claim 4, characterized in that Before determining the first reference value based on the floating-point weight data of the first network layer, the method further includes: Determining the number of bits F of the first quantization processing based on the distribution discreteness of multiple values included in the floating-point weight data of the first network layer; The determining a plurality of numerical intervals based on the first reference value comprises: Based on the first reference value and the F, R numerical intervals are determined.
6. A retraining method for a neural network model, characterized in that: include: Performing a first iteration on the first neural network model using one or more sample images to obtain a second neural network model, wherein in the first iteration, the weight of at least one first network layer included in the first neural network model is quantized, and the weights of multiple second network layers included in the first neural network model are floating point numbers; Performing a second iteration based on the second neural network model to obtain a third neural network model, wherein in the second iteration, weights of the at least one first network layer and at least one second network layer adjacent to the at least one first network layer among the plurality of second network layers are quantized; After performing a second iteration based on the second neural network model to obtain a third neural network model, the method further includes: In response to the fact that the weights of all network layers included in the second neural network model have been quantized, a third iteration is performed based on the third neural network model to obtain a fourth neural network model, wherein, in the third iteration, the activation data of at least one third network layer of the third neural network model is quantized, and the activation data of at least one fourth network layer of the third neural network model is not quantized.
7. The method according to claim 6, characterized in that After obtaining the fourth neural network model, the method further includes: A fourth iteration is performed based on the fourth neural network model to obtain a target neural network model; wherein, in the fourth iteration, the activation data of the at least one third network layer and the activation data of the at least one fourth network layer are both quantized.
8. The method according to claim 6 or 7, characterized in that: The performing a third iteration based on the third neural network model to obtain a fourth neural network model includes: Obtaining first operation parameters required for a first operation process, wherein the first operation process is an operation process obtained by merging a network model process performed by at least one third network layer in the first neural network model and a second quantization process for quantizing activation data of the at least one third network layer, and the first operation parameters include a quantization parameter corresponding to the second quantization process; Performing fixed-point processing on the floating-point number in the first operation parameter to obtain a second operation parameter; Based on the second operation parameter, perform the first operation processing to obtain the processing result of the at least one third network layer; Based on the processing result of the at least one third network layer, the fourth neural network model is obtained.
9. The method according to claim 8, characterized in that The performing fixed-point processing on the floating-point number in the first operation parameter to obtain the second operation parameter comprises: Expand each floating-point number included in the first operation parameter by K times to obtain an expanded value, where K is an integer greater than 1; The expanded value is converted into a fixed-point number to obtain the second operation parameter.
10. The method according to claim 6 or 7, characterized in that: The quantization parameter used for quantizing the activation data to be quantized is obtained based on a maximum quantization value and a minimum quantization value, and the maximum quantization value and the minimum quantization value are obtained based on clustering multiple values included in the activation data to be quantized.
11. A data processing method, characterized in that: include: Get the image to be processed; The image to be processed is processed by a target neural network model to obtain an image processing result, wherein the target neural network model is trained by the retraining method according to any one of claims 1 to 10.
12. A data processing device, characterized in that: include: A first iteration unit is used to perform a first iteration on the first neural network model using one or more sample images to obtain a second neural network model, wherein in the first iteration, the weight of at least one first network layer included in the first neural network model is quantized, and the weights of multiple second network layers included in the first neural network model are floating point numbers; a second iteration unit, configured to perform a second iteration based on the second neural network model to obtain a third neural network model, wherein in the second iteration, weights of the at least one first network layer and at least one second network layer adjacent to the at least one first network layer among the plurality of second network layers are quantized; In the first iteration, activation data of at least one fourth network layer included in the first neural network model is quantized, and activation data of multiple fifth network layers included in the first neural network model are floating point numbers; In the second iteration, activation data of the at least one fourth network layer and at least one fifth network layer of the plurality of fifth network layers adjacent to the at least one fourth network layer are quantized; Alternatively, the device also includes: a third iteration unit, used to perform a third iteration based on the third neural network model in response to the weights of all network layers included in the second neural network model having been quantized, so as to obtain a fourth neural network model, wherein, in the third iteration, activation data of at least one third network layer of the third neural network model is quantized and input to the next network layer, and activation data of at least one fourth network layer of the third neural network model is not quantized.
13. The device according to claim 12, characterized in that The network depth of the at least one first network layer is greater than the network depth of the at least one second network layer.
14. The device according to claim 12 or 13, characterized in that The first iteration unit is specifically used to perform a first prediction process on the first sample through the first neural network model to obtain a first prediction result; based on the first prediction result, the quantized weight data of the at least one first network layer and the floating-point weight data of the multiple second network layers are updated to obtain the second neural network model.
15. The device according to claim 12 or 13, characterized in that The device also includes: a quantization unit, configured to determine a first reference value based on the floating-point weight data of the first network layer; Based on the first reference value, determine a plurality of numerical intervals; each of the plurality of numerical intervals corresponds to a numerical value that can be represented by F bits, where F is an integer greater than 1 and less than 8; Based on the multiple numerical intervals, a first quantization process is performed on the weights in the floating-point weight data to obtain quantized weight data of the first network layer.
16. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions. When the program instructions are executed by a processor, the processor executes the method according to any one of claims 1 to 11.
17. An electronic device, characterized in that: include: Memory, used to store programs; A processor, configured to execute the program stored in the memory; when the program is executed, the processor is configured to execute the method according to any one of claims 1 to 11.
18. A chip, characterized in that: include: A processor and a data interface, wherein the processor reads instructions stored in a memory through the data interface to execute the method according to any one of claims 1 to 11.
