End-side deep neural network model compression method based on fine tuning training
Through the deep neural network model compression method based on fine-tuning training, using weight segmentation quantization and sliding segmentation quantization technology, the problem of quantization accuracy loss during end-side compression of the deep neural network model is solved, and the efficient compression and inference effect of the model are achieved. improvement.
Patent Information
- Application Number
- CN202411100854.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-12
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2044-08-12
AI Technical Summary
When the existing deep neural network model is compressed on the end-side, the quantization accuracy loss is too large, making the model inference effect difficult to match the original floating-point model. Especially when 4-bit quantization, the loss is large, and the quantization loss needs to be compensated.
The end-side deep neural network model compression method based on fine-tuning training is adopted. The model weight matrix is segmented and restored through weight segmentation quantization and sliding segmentation quantization technology. Parameters are adjusted based on the tiny learning rate and the quantization factor is optimized to reduce Loss of accuracy.
On the premise of maintaining the model inference effect, the model is effectively compressed, the memory usage and calculation speed are reduced, the problem of low accuracy of quantization factor numerical dispersion caused by numerical fluctuations in the interval is reduced, and the accuracy loss caused by model quantization is reduced. .
Smart Images

Figure CN118643884A
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of artificial intelligence technology and relates to a speech neural network model, and specifically to an end-side deep neural network model compression method based on fine-tuning training. Background Art
[0002] When using a deep neural network model on the end, issues such as model end-side storage, running memory, device power consumption, inference speed, and hardware adaptation need to be considered. In order to maximize the capabilities of the neural network model on the end, it is necessary to perform 4-bit or 8-bit fixed-point quantization compression on the model without reducing the model effect as much as possible to maximize the effect on the end. The deep neural network server generally uses conventional precision training. Conventional precision models generally indicate that the model weight value format is FP32 (32-bit floating point, single precision) to ensure high accuracy of model parameters and good model inference effect.
[0003] For example, quantizing the model weight parameters from 32 bits to 8 bits can reduce the size of the model weight parameters by 75%, and quantizing to 4 bits can reduce the size of the model weight parameters by 87.5%, while reducing memory usage and increasing the speed of model reasoning calculations. It has been verified that 8-bit quantization has less damage and its impact on model reasoning effect is negligible, while 4-bit quantization has a larger loss and needs to take measures to compensate for the quantization loss.
[0004] Typical deep neural network models are Figure 5 As shown in the figure, it includes a linear layer, an activation function layer, and a normalization layer, among which the quantization of the weight matrix W in the linear layer has a higher benefit. The usual quantization scheme is to quantize layer by layer, calculate the layer maximum value by layer, set the number of bits to be quantized, calculate the layer quantization factor based on the maximum value and the number of bits, and use the same quantization factor for compression quantization and floating point restoration of the weights of each layer. However, when the weight matrix W is quantized by layer, the fluctuation range of the values of an entire layer is large, and the quantization accuracy loss is too large, and the model inference effect is difficult to reach the original floating point model. Summary of the invention
[0005] In view of the defects of the above-mentioned prior art, the present invention discloses an end-side deep neural network model compression method based on fine-tuning training.
[0006] The method for compressing a deep neural network model on the client side based on fine-tuning training of the present invention comprises the following steps: Step S1. Prepare a deep neural network model as an initial model; Step S2. The weight matrices of some layers in the initial model are quantized by weight segmentation; The weight segmentation quantization method is specifically as follows: Step S21. For the weight matrix W=[P1, P2], P1 and P2 represent the rows and columns of the matrix respectively, set the segment length to N, and the number of quantization bits to M; divide each column of the weight matrix W into K segments according to the segment number N; When the number of matrix columns P2 is divisible by N, each segment consists of N complete columns; When the number of matrix columns P2 cannot be divided by N, the number of columns of the previous segments except the last segment is N, and the last segment is the remaining number of columns; Step S22. Calculate the maximum value max(i) of the absolute values of all matrix elements in the i-th segment, i=1, 2…K; Step S23. Calculate the maximum value max(i) and the quantization bit number M to obtain the quantization factor S(i) of the i-th segment. The calculation formula is: S(i)=max(i) / [2(M-1)-1]; Step S24. Calculate the quantized value Q(i) of the i-th segment. The calculation formula is: Q(i)=round{[i,i+N] / S(i)}; round() represents the rounding function; The quantization matrix is obtained by combining the quantization values Q(i) of each segment: QR = (Q(1), Q(2)…Q(K)); replace the weight matrix W with the quantization matrix QR; Step S3. Replace the weight matrix in the initial model with the quantization matrix QR for training. During the training process, the layer parameters of the replaced weight matrix are fixed, and the layer parameters of other unreplaced weight matrices are adjusted. A small learning rate is set for parameter adjustment until the training target is reached.
[0007] Preferably, the specific form of the weighted piecewise quantization in step S2 is sliding piecewise quantization, and in the sliding piecewise quantization, step S21 is replaced by the following manner: Set the segment length to N and the number of quantization bits to M; Set the sliding start point to ST, the sliding step length to STEP, and the total number of sliding steps to SA; In the first set of segmentation methods, the column starting point of the first segment is ST. In the second set of segmentation methods, the column starting point of the first segment is ST+STEP. By analogy, we get the SA group segmentation method; In the last group, i.e., the SA group segmentation method, the column starting point of the first segment is ST+(SA-1)*STEP; In each group of segmentation, the segment length is N. When the number of remaining columns is less than N, The remaining column is the last paragraph; Then, for each group of segmentation modes, the quantization value Q(i) of the i-th segment is obtained according to steps S22 to S24; The step S2 further comprises a step S25: Step S25. For each group of segmentation methods, the restored segment is obtained by multiplying the quantization value Q(i) by the quantization factor S(i), and the restored segments are combined to obtain a restored weight matrix; The i-th segment after restoration is W(i)=Q(i)*S(i); i=1, 2…K; The restored weight matrix WR=(W(1), W(2)…W(K)); Finally, the SA group quantization factor and the SA group restored weight matrix are obtained, and the restored weight matrix is used to replace the weight matrix in the initial model to obtain the SA group quantization restoration model; The step S3 is specifically as follows: S31. Input the verification data into the SA group quantitative restoration model for verification, obtain the best set of SA group quantitative restoration models as the fine-tuning initial model, and record the quantitative factor QA corresponding to the fine-tuning initial model; S32. The layer LA whose weight matrix is replaced by the sliding piecewise quantization method in the fixed fine-tuning initial model is trained, and the weight matrix parameters of other layers except the layer LA in the fine-tuning initial model are adjusted during the training process until the training target is reached and the optimal floating-point model is output; S33. quantize and update the weight matrix WA of layer LA in the best floating point model, The updated matrix WB=WA / QA, the updated matrix WB is used to replace the weight matrix WA to obtain the optimal floating-point model for the first quantization; S34. The best floating-point model obtained by the first quantization is quantized in a weighted piecewise manner, the weight matrix WB of the layer LA is fixed, and secondary quantization is performed on other layers; the secondary quantized best floating-point model is obtained as the compressed model.
[0008] Preferably, the partial layers for which sliding segmentation quantization is performed in step S2 are linear layers in the model.
[0009] Preferably, when performing sliding segmented quantization, the number of quantization bits M=4, and when performing weighted segmented quantization, the number of quantization bits M is equal to 8.
[0010] The terminal-side deep neural network model compression method based on fine-tuning training described in the present invention has the following advantages: 1. The present invention compresses the deep neural network model and reduces the model reasoning effect as little as possible. It can achieve better model reasoning and recognition effect under the same computing and storage resources.
[0011] 2. The present invention can effectively reduce the problem of low dispersion accuracy of the obtained quantization factor values due to excessive fluctuation of the values within the interval through segmented quantization, thereby reducing the accuracy loss introduced by model quantization. BRIEF DESCRIPTION OF THE DRAWINGS
[0012] Figure 1 The figure shows a specific implementation flow diagram of the end-side deep neural network model compression method based on fine-tuning training according to the present invention; Figure 2 FIG. 1 is a schematic diagram of a specific implementation flow of step S1 in the compression method of the present invention; Figure 3 FIG. 1 is a schematic diagram of a specific implementation flow of step S3 in the compression method of the present invention; Figure 4 The figure is a schematic diagram of a specific implementation process of the embodiment 2 of the present invention; Figure 5 Shown is a schematic diagram of a specific implementation of a deep neural network model in the prior art. DETAILED DESCRIPTION
[0013] In order to more intuitively and clearly describe the specific details of the technical solution of the present invention, a detailed description will be given below in conjunction with embodiments and example drawings.
[0014] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in combination with the specific implementation methods of the present invention and the corresponding drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0015] The end-side deep neural network model compression method based on fine-tuning training described in the present invention takes 32-bit initial model compression as an example, and the specific implementation method is: first prepare rich corpus and train the model as the initial model for subsequent optimization steps; further, in order to compress the quantization model parameters, the linear layer weights in the model are 32-bit piecewise quantized to obtain a quantization factor group; during the piecewise quantization, a random sliding value is introduced to ensure that the optimal quantization factor group is found for the next step of fine-tuning training; finally, the initial model is combined with the sliding quantization factor group for fine-tuning training to obtain a fine-tuned model, 4-bit quantization is performed according to the optimal quantization factor group, and the remaining model layers are 8-bit quantized to output the final compressed model. Example 1
[0016] like Figure 1 As shown, the terminal-side deep neural network model compression method based on fine-tuning training of the present invention includes the following steps: Step S1. Prepare enough speech data sets, extract features, and set the initial deep neural network model structure and initial parameters. Train according to 32-bit single precision and output a 32-bit deep neural network model as the initial model. A specific process of step S1 is as follows Figure 2 As shown; Step S2. To reduce the loss of quantization accuracy, the weight matrices of some layers in the initial model are quantized in piecewise weights. The piecewise quantization method is as follows: Step S21. For the weight matrix W=[P1, P2], P1 and P2 represent the rows and columns of the matrix respectively, set the segment length to N, and the number of quantization bits to M; traverse the weight matrix W, and divide each column of the weight matrix W into K segments according to the number of segments N; When the number of matrix columns P2 is divisible by N, each segment consists of N complete columns, and K = P2 / N; When the number of matrix columns P2 cannot be divided by N, the number of columns of the previous segments except the last segment is N, and the last segment is the remaining number of columns; For example, if the matrix has 40 columns and N=7, then the first 5 segments can contain 7 columns each and the last segment can contain 5 columns; Step S22. Calculate the maximum value max(i) of the absolute values of all matrix elements in the i-th segment, i=1, 2…K; Step S23. Calculate the maximum value max(i) and the quantization bit number M to obtain the quantization factor S(i) of the i-th segment. The calculation formula is: S(i)=max(i) / [2(M-1)-1]; the number of quantization bits M determines the number of bits after compression. For example, if M=4, 4-bit quantization is performed, and if M=8, 8-bit quantization is performed. Step S24. Calculate the quantized value Q(i) of the i-th segment. The calculation formula is: Q(i)=round{[i,i+N] / S(i)}; round() represents the rounding function; Combine the quantization values Q(i) of each segment to obtain a quantization matrix QR = (Q(1), Q(2)…Q(K)); replace the weight matrix W with the quantization matrix QR; Step S3. Replace the weight matrix in the initial model with the quantization matrix QR for training. During the training process, the layer parameters of the replaced weight matrix are fixed, and the layer parameters of other unreplaced weight matrices are adjusted. A small learning rate is set to adjust the parameters until the training target is reached; the small learning rate is set according to the empirical value, and the general value is 0.0005-0.00005.
[0017] For example, if the layer of the weight matrix is replaced by a linear layer, a specific process of step S3 is as follows: Figure 3 shown. Example 2
[0018] The difference between this embodiment and embodiment 1 is that step S2 may also include another preferred implementation method, because it is found through subsequent model testing that when M=8 is set, the loss after 8-bit quantization is small and negligible, and the quantization of other layer parameters can be performed by weight segmented quantization in the manner described in step S2 above. However, when M=4 is set, the loss is large when 4-bit quantization is performed, and fine-tuning training is required to reduce the impact of quantization. For this reason, in step S21, when M=4, the linear layer can be quantized by sliding segmented quantization to obtain the best 4-bit quantization factor group.
[0019] The specific method of sliding segment quantization is: Set the sliding start point to ST, the sliding step length to STEP, and the total number of sliding steps to SA. When segmenting, the sliding start point ST is used as the column start point of the first segment, which is used as the segmentation method of the first group; According to the number of segments N, the first segment of the first group contains columns [ST, ST+N-1], In the second group of segmentation methods, ST+STEP is used as the column starting point of the first segment. As the segmentation method of the second group, the columns contained in the first segment of the second group are [ST+STEP, ST+STEP +N-1]; And so on, until the SA group segmentation method, ST+(SA-1)*STEP is used as the column starting point of the first segment, which is the SA group segmentation method; For example, set ST=5, STEP=3, total number of slides SA=6, N=7; In the first set of segmentation, the first segment includes 7 columns from column 5 to column 11, the second segment includes columns 12 to 18, the third segment includes columns 19 to 25, etc.; In the second group of segmentation, the starting point is moved forward by 3 columns. The first segment includes 7 columns from the 8th column to the 14th column, the second segment includes the 15th column to the 21st column, the third segment includes the 19th column to the 25th column, and so on. In the last group, i.e. the sixth group of segments, the first segment contains 7 columns from column 20 to column 26, the second segment contains columns 27 to 33, the third segment contains columns 34 to 40, etc.; In each group of segmentation methods, the segment length is N. When the number of remaining columns after segmentation is less than N, the remaining columns are taken as the last segment.
[0020] Then, according to steps S22 to S24, the quantized value Q(i) of the i-th segment is obtained. At this time, it is also necessary to perform inverse quantization floating point, that is, step S25. Figure 1 shown.
[0021] S25. Dequantization restores the floating point, specifically, by multiplying the quantization value Q(i) by the quantization factor S(i) to obtain the restored segment, and combining the restored segments to obtain the restored weight matrix; The i-th segment after restoration is W(i)=Q(i)*S(i); i=1, 2…K; The restored weight matrix WR=(W(1), W(2)…W(K)); After adopting sliding segmented quantization, an SA group segmentation method is generated, and finally the SA group quantization factor can be obtained, and further restored to obtain the SA group restored weight matrix.
[0022] At this time, in step S3, the restored weight matrix replaces the weight matrix in the initial model for training.
[0023] For the SA group 4-bit quantization factor and SA group quantization restoration model obtained by sliding segmented quantization, although the quantized model has achieved compressed size, the model loss is large. In order not to reduce the model reasoning effect, segmented quantization fine-tuning training is required based on the quantized model.
[0024] The segmented quantization fine-tuning training process is as follows: Figure 4 As shown, the following steps are included: S31. Input the data consisting of the test set and the validation set into the SA group quantitative restoration model for verification, obtain the best set of the SA group quantitative restoration model as the fine-tuning initial model, and record the quantitative factor QA corresponding to the fine-tuning initial model; The test set and validation set are pre-prepared speech datasets used as test models and validation models.
[0025] S32. The linear layer LA whose weight matrix is replaced by the sliding piecewise quantization method in the fixed fine-tuning initial model is trained, and during the training process, the weight matrix parameters of other layers except the layer LA in the fine-tuning initial model are adjusted, such as the weight matrix parameters of the activation layer and the normalization layer, until the training target is reached, and the optimal floating-point model is output; S33. quantize and update the weight matrix WA of the linear layer LA in the best floating point model. The updated matrix WB=WA / QA, WB is used to replace the weight matrix WA, and the optimal floating-point model for the first quantization is obtained; S34. The best floating-point model that has been quantized for the first time is quantized in accordance with the weight piecewise quantization method in step S2, the weight matrix WB of the linear layer LA is fixed, and the weight matrices in other layers in the model, such as the normalization layer and the activation function layer, are re-quantized. The weight matrices WC in other layers that are not fixed are subjected to weight piecewise quantization to obtain the quantized weight matrix WD, and the weight matrix WD is used to replace the weight matrix WC, so that the weight matrices of other layers are also updated, and finally the final compression model is obtained.
[0026] The flow chart of Example 2 is as follows Figure 4 shown. Example 3
[0027] Taking the speech recognition task in the air-conditioning field as an example, a sufficient data set is selected and, after preprocessing and data expansion, used as the input of the 32-bit deep neural network model model_32bit to be compressed.
[0028] The parameter quantities were set to 3M and 1.3M respectively, and the deep neural network model model_32bit was trained according to the traditional method, and two models model_32bit_3M and model_32bit_1.3M were obtained as the first control example and the second control example.
[0029] According to the method described in the present invention, the deep neural network model model_32bit is randomly quantized by sliding, and multiple groups of quantization factors and quantization restoration models are output. The above results are used as input training to screen out the best 4-bit quantization restoration model. Based on the 4-bit quantization restoration model, other layers are further quantized by 8-bit in a weighted segmented quantization manner as the output quantization model.
[0030] The output quantization model model_4bit_8bit_notfinetune is set to the third control example Continue to perform multiple rounds of quantization fine-tuning training on the model of the first control example, and finally output the secondary quantization optimal floating-point model model_4bit_8bit_finetune after fine-tuning training according to the segmented quantization fine-tuning training process described in Example 2.
[0031] The test results of the optimal floating-point model for secondary quantization obtained by the above three control examples and the method of the present invention are as follows in Table 1: Table 1 Test results comparison table
[0032] According to the results in the above table, after 4-bit and 8-bit quantization compression, the model size is effectively reduced by 56.67%. Compared with the original uncompressed model_32bit_3M model, the recognition and reasoning effect of the model model_4bit_8bit_finetune trained with quantization compression and fine-tuning is slightly reduced, about 1%~2%; compared with the model model_4bit_8bit_notfinetune trained with quantization compression but not fine-tuning, the effect of recovering the quantization compression loss is very obvious; compared with the original uncompressed model_32bit_1.3M model, the recognition is improved by about 2%~4%.
[0033] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0034] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0035] The foregoing describes various preferred embodiments of the present invention. Unless the preferred implementation modes in various preferred embodiments are obviously self-contradictory or based on a certain preferred implementation mode, various preferred implementation modes can be arbitrarily superimposed and used in combination. The embodiments and specific parameters in the embodiments are only for clearly describing the invention verification process of the inventor, and are not used to limit the patent protection scope of the present invention. The patent protection scope of the present invention shall still be based on its claims. All equivalent structural changes made using the contents of the description and drawings of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for compressing a deep neural network model on the client side based on fine-tuning training. Features , including the following steps: Step S1. Prepare a deep neural network model as an initial model; Step S2. The weight matrices of some layers in the initial model are quantized by weight segmentation; The weight segmentation quantization method is specifically as follows: Step S21. For the weight matrix W=[P1, P2], P1 and P2 represent the rows and columns of the matrix respectively, set the segment length to N, and the number of quantization bits to M; divide each column of the weight matrix W into K segments according to the segment number N; When the number of matrix columns P2 is divisible by N, each segment consists of N complete columns; When the number of matrix columns P2 cannot be divided by N, the number of columns of the previous segments except the last segment is N, and the last segment is the remaining number of columns; Step S22. Calculate the maximum value max(i) of the absolute values of all matrix elements in the i-th segment, i=1, 2…K; Step S23. Calculate the maximum value max(i) and the number of quantization bits M to obtain the quantization factor S(i) of the i-th segment. The calculation formula is: S(i)=max(i) / [2(M-1)-1]; Step S24. Calculate the quantized value Q(i) of the i-th segment. The calculation formula is: Q(i)=round{[i,i+N] / S(i)}; round() represents the rounding function; The quantization matrix is obtained by combining the quantization values Q(i) of each segment: QR = (Q(1), Q(2)…Q(K)); replace the weight matrix W with the quantization matrix QR; Step S3. Replace the weight matrix in the initial model with the quantization matrix QR for training. During the training process, the layer parameters of the replaced weight matrix are fixed, and the layer parameters of other unreplaced weight matrices are adjusted. A small learning rate is set for parameter adjustment until the training target is reached.
2. The method for compressing a deep neural network model on the end based on fine-tuning training as claimed in claim 1, It is characterized in that The specific form of the weighted piecewise quantization in step S2 is sliding piecewise quantization. In the sliding piecewise quantization, step S21 is replaced by the following method: Set the segment length to N and the number of quantization bits to M; Set the sliding start point to ST, the sliding step length to STEP, and the total number of sliding steps to SA; In the first set of segmentation methods, the column starting point of the first segment is ST. In the second set of segmentation methods, the column starting point of the first segment is ST+STEP. By analogy, we get the SA group segmentation method; In the last group, i.e., the SA group segmentation method, the column starting point of the first segment is ST+(SA-1)*STEP; In each group of segmentation, the segment length is N. When the number of remaining columns is less than N, The remaining column is the last paragraph; Then, for each group of segmentation modes, the quantization value Q(i) of the i-th segment is obtained according to steps S22 to S24; The step S2 further comprises a step S25: Step S25. For each group of segmentation methods, the restored segment is obtained by multiplying the quantization value Q(i) by the quantization factor S(i), and the restored segments are combined to obtain a restored weight matrix; The i-th segment after restoration is W(i)=Q(i)*S(i); i=1, 2…K; The restored weight matrix WR=(W(1), W(2)…W(K)); Finally, the SA group quantization factor and the SA group restored weight matrix are obtained, and the restored weight matrix is used to replace the weight matrix in the initial model to obtain the SA group quantization restoration model; The step S3 is specifically as follows: S31. Input the verification data into the SA group quantitative restoration model for verification, obtain the best set of SA group quantitative restoration models as the fine-tuning initial model, and record the quantitative factor QA corresponding to the fine-tuning initial model; S32. The layer LA whose weight matrix is replaced by the sliding piecewise quantization method in the fixed fine-tuning initial model is trained, and the weight matrix parameters of other layers except the layer LA in the fine-tuning initial model are adjusted during the training process until the training target is reached and the optimal floating-point model is output; S33. quantize and update the weight matrix WA of layer LA in the best floating point model, The updated matrix WB=WA / QA, the updated matrix WB is used to replace the weight matrix WA to obtain the optimal floating-point model for the first quantization; S34. The best floating-point model quantized for the first time is quantized in a weighted piecewise manner, the weight matrix WB of the fixed layer LA is fixed, and the secondary quantization of other layers is performed; The best floating-point model after secondary quantization is obtained as the compressed model.
3. The method for compressing a deep neural network model on the end based on fine-tuning training as claimed in claim 2, It is characterized in that The partial layers subjected to sliding piecewise quantization in step S2 are linear layers in the model.
4. The method for compressing a deep neural network model on the end based on fine-tuning training as claimed in claim 2, It is characterized in that When performing sliding segment quantization, the number of quantization bits M=4, and when performing weighted segment quantization, the number of quantization bits M is equal to 8.
Citation Information
Patent Citations
Data processing method and related product
CN113222097A
Model training method
CN114418088A
Data processing method and device, equipment and storage medium
CN114781650A
Voice recognition model compression method based on command word
CN114783427A
Low-rank adaptation quantization fine tuning method and device for large language model
CN118035624A
Cited By
Model parameter storage method and electronic equipment
CN121615727A
