A coarse-grained and fine-grained neural network pruning method

By adopting a coarse- and fine-grained joint pruning method in the neural network, combined with two-norm sorting and knowledge distillation technology, the balance problem between hardware friendliness and accuracy of the pruning method in the existing technology is solved, and a more efficient model is achieved. Compression and inference efficiency, and verified model accuracy after pruning.

CN113850385BActive Publication Date: 2024-09-03BEIHANG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111187212.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-10-12
Publication Date
2024-09-03
Estimated Expiration
2041-10-12

AI Technical Summary

Technical Problem

It is difficult for existing technologies to find a balance between hardware friendliness and model accuracy, resulting in structured and unstructured pruning methods each suffering from accuracy loss or low hardware efficiency, especially in terms of model parameter variability and inference time compression.

Method used

A neural network pruning method combining coarse and fine granularity is used to select candidate filters and convolution kernels through second norm and layer-by-layer sorting, perform group sparse training and regularization compression, dynamically generate pattern sets, and combine knowledge distillation to model Retrain to restore accuracy.

Benefits of technology

It achieves higher hardware friendliness and model storage inference efficiency, and the compression ratio is better than the existing hybrid pruning method. At the same time, the model accuracy after pruning is ensured through mode discriminant function evaluation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113850385B
    Figure CN113850385B
Patent Text Reader

Abstract

The present invention discloses a neural network pruning method with coarse and fine granularity combination, including: group sparsification training of the selected candidate filters, pruning the candidate filters less than the threshold after a certain number of rounds; sorting the importance of the convolution kernel layer by layer, and obtaining the convolution kernel to be pruned layer by layer according to the predefined pruning rate; regularizing and compressing the convolution kernel in units of weight, and dynamically generating a pattern set that satisfies the pre-built pattern discriminant function during the compression process; matching each convolution kernel to the optimal pattern of the convolution kernel in the pattern set; performing convolution kernel pruning and pattern pruning; setting the parameters to be pruned to zero, and performing hard pruning on the model; retraining and fine-tuning the hard pruned model in combination with the knowledge distillation method to obtain the final pruned model. The present invention can give full play to the advantages of structured and unstructured pruning, improve the model storage and reasoning efficiency, and has higher hardware friendliness.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of embedded AI technology, and more specifically to a neural network pruning method combining coarse and fine granularity. Background Art

[0002] With the emergence of a series of embedded devices, it is very challenging to perform reasoning on deep neural networks considering the high computing and storage requirements. Pruning is a widely used method in model compression. It finds an effective means to judge the importance of parameters and prunes unimportant parameters to reduce the redundancy of the model. Existing pruning methods mainly include structured and unstructured pruning. Unstructured pruning has the characteristics of refinement and high precision, but is not hardware-friendly. Structured pruning has the characteristics of coarse granularity and high hardware efficiency, but has a large loss of precision.

[0003] For unstructured pruning, directly resetting the weights to zero does not change the model size or reduce the inference time. It is necessary to use indexes to sparsely encode the model, supplemented by a specific matrix multiplication strategy. Currently, there is less focus on weight pruning at any position. The reason is that although pruning at any position will bring a higher net parameter compression ratio, the index overhead is not negligible, and the efficiency of sparse multiplication is significantly lower than that of dense matrices. Therefore, weight pruning at any position no longer has an advantage in terms of parameter compression rate or inference time. The latest research proposes pattern pruning, which prunes a fixed number of weights in each convolution kernel and concentrates the remaining weights in a certain area to form a specific kernel pattern, generating an intermediate sparse type between unstructured pruning and structured pruning, which has better hardware adaptability. However, the current scheme directly applies the study of the importance of convolution kernel parameters to pattern selection. The greedy method selects the pattern, which cannot match all convolution kernels to the best pattern, and lacks comparative experimental demonstration of the effect of using convolution importance strategies (such as forcing the central element not to be pruned). Another solution mathematically deduces the optimal mode when the model parameters are fixed, but ignores the variability of the parameters during training. At the same time, single-shot mode generation consumes a lot of time compared to single-round training, and round-by-round mode generation is also undesirable.

[0004] Structured pruning can be divided into filter pruning and convolution kernel pruning. These schemes prune unimportant filters or convolution kernels by proposing a set of importance judgment criteria. Since pruning can change the size of the weight matrix, it can directly improve the inference speed. However, most of the current methods combining structured and unstructured pruning are to stack different pruning schemes in series, lacking analysis of the pruning order and the mutual influence of different pruning modes.

[0005] Therefore, how to provide a coarse-grained structured joint pruning method that can give full play to the advantages of structured and unstructured pruning and improve model storage and reasoning efficiency is an urgent problem that technical personnel in this field need to solve. Summary of the invention

[0006] In view of this, the present invention provides a coarse-grained and fine-grained combined neural network pruning method, which can give full play to the advantages of structured and unstructured pruning, improve model storage and reasoning efficiency, and has higher hardware friendliness.

[0007] In order to achieve the above object, the present invention adopts the following technical solution:

[0008] A coarse-grained and fine-grained combined neural network pruning method comprises the following steps:

[0009] According to the second norm and layer by layer, the candidate filters to be pruned in the original model are selected, and group sparse training is performed on the candidate filters. After a certain number of rounds, the candidate filters smaller than the threshold are pruned;

[0010] Sort the importance of convolution kernels layer by layer according to the binary norm, and obtain the convolution kernels that need to be pruned layer by layer according to the predefined pruning rate;

[0011] The convolution kernel is compressed and regularized in units of weights, and a pattern set that satisfies the pre-built pattern discrimination function is dynamically generated during the compression process;

[0012] Matching each convolution kernel to the optimal mode of the convolution kernel in the mode set;

[0013] Perform convolution kernel pruning and mode pruning;

[0014] Set the parameters to be pruned to zero and perform hard pruning on the model;

[0015] The hard pruned model is retrained and fine-tuned in combination with the knowledge distillation method to obtain the final pruned model.

[0016] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, the generation process of the pattern set is:

[0017] Traverse each convolution kernel of the model and select the four positions with the largest absolute weight values ​​to form the optimal mode of the convolution kernel;

[0018] Globally count the frequencies of occurrence of each of the optimal patterns, and select the k patterns with the highest frequencies as the pattern set generated in the current round;

[0019] The pattern discriminant function is used to judge the weight adjacency of the pattern set, the number of pattern set shapes and the convergence of the current model to evaluate whether the pattern set generated in the current round can be used as the final pattern set.

[0020] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, the judgment condition of the weighted adjacency of the pattern set is: all elements in a single pattern set are adjacent; specifically:

[0021] Traverse each non-zero element position (i, j) of the pattern set, and determine whether the four adjacent positions (i+1, j), (i-1, j), (i, j+1), and (i, j-1) are inside the convolution kernel; if they are inside the convolution kernel, determine whether the position is non-zero; if a non-zero element does not exist at any element neighbor position, then the pattern elements are not all adjacent. If this situation does not occur after the traversal is completed, then all are adjacent.

[0022] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, the judgment condition for the number of pattern set shapes is: pattern set shape ≤ k / 2; specifically:

[0023] For the convolution kernel weight index number, the number corresponding to the jth weight in the i-th row is i*kernel_size+j;

[0024] Use a dictionary to record the shape of the pattern set corresponding to the position combination. The key of the dictionary is the concatenation of numbers with non-zero weights from small to large.

[0025] Traverse each pattern set, query the corresponding number of its shape, use a set to record whether each number exists, if not, increase the number of pattern set shapes by 1, and add the number to the set.

[0026] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, the judgment condition for the convergence of the current model is: the current gradient mean is less than the threshold q; specifically:

[0027] The gradient of each position is obtained through the grad attribute of the tensor, and the mean is obtained through the mean function.

[0028] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, pruning candidate filters smaller than a threshold value includes:

[0029] Transform the convolution matrix and the mask matrix into the same matrix, calculate the bi-norm for each column, and record the columns less than the 0.1m threshold as the filters to be pruned;

[0030] Traverse the model layer by layer, save the input and output channels to be pruned in each layer. For the lth layer, the output channel to be discarded is the column index corresponding to the pruned filter of this layer, and the input channel is the row index corresponding to the pruned filter of the l-1th layer;

[0031] The rows and columns to be pruned are discarded through the index_select function.

[0032] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, the process of obtaining the convolution kernel to be pruned is:

[0033] Transform the convolution matrix and calculate the sum of the two norms of the convolution kernels one by one;

[0034] Sort the importance of all convolution kernels of each filter in each layer according to the sum of the two norms;

[0035] According to the predefined pruning rate, find the least important k convolution kernels and set the mask to 0 as the convolution kernels that need to be pruned.

[0036] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, matching each convolution kernel to the optimal pattern of the convolution kernel in the pattern set includes:

[0037] Traverse the final pattern sets one by one, calculate the sum of the absolute values ​​of the weights of the convolution kernels on each pattern set, and select the maximum sum index as the pattern set of the convolution kernel; after the allocation is completed, the shape of the pattern set of the convolution kernel no longer changes.

[0038] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, regularization training is performed on the weights that may be pruned in the filter pruning stage, the convolution kernel pruning stage and the pattern pruning stage, respectively, and the cross entropy is used to quantify the difference between the model prediction value and the true value.

[0039] Preferably, in the above-mentioned coarse-grained and fine-grained combined neural network pruning method, in the process of regularizing the weights that may be pruned in stages, the iterative method of SGD stochastic gradient descent is used to optimize the differentiable objective function, and the weights and bias terms are iteratively updated by calculating the gradient of the loss function on small batches of data, and the learning rate decay strategy MultiStepLR is used for compression pruning.

[0040] It can be seen from the above technical solutions that, compared with the prior art, the present invention discloses a coarse-grained and fine-grained joint neural network pruning method. Filter pruning, convolution kernel pruning and pattern pruning constitute a joint pruning method to compress the model. According to the structured and unstructured pruning schemes, it determines the position of the weights that need to be pruned, generates a mask layer, and gradually reduces the weights that need to be discarded through training with penalty terms. Actual pruning is performed through hard pruning, multiplying the original model parameters by the mask layer, and setting the discarded parameters to zero. The pruned model is then retrained and fine-tuned to restore accuracy. Through knowledge distillation, the model before pruning is used as a teacher, and the difference in predicted values ​​before and after pruning is used as the loss function for learning.

[0041] The present invention is triggered by the advantages and disadvantages of different pruning methods, and performs structured and unstructured pruning on the model. According to the pruning granularity, filters, convolution kernels and patterns are pruned in sequence. The pruning methods are not independent, but are performed in collaboration. The final model has higher hardware friendliness while having a slightly better compression ratio than the current hybrid pruning results. At the same time, the present invention uses a pattern discrimination function to determine whether the pattern set generated in the current round can be finally adopted, and through experiments on multiple pattern sets, the effectiveness of the evaluation function is proved, ensuring that the accuracy of the model after pattern pruning is higher than the current pattern pruning method. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.

[0043] Figure 1 The accompanying drawing is a flow chart of the coarse-grained and fine-grained combined neural network pruning method provided by the present invention;

[0044] Figure 2 The accompanying drawing is a schematic diagram of pattern generation and pruning provided by the present invention;

[0045] Figure 3 The accompanying drawings are comparison diagrams of filter pruning, convolution kernel pruning and pattern pruning provided by the present invention;

[0046] Figure 4 The accompanying drawing is a schematic diagram of filter pruning provided by the present invention;

[0047] Figure 5 The accompanying drawing is a schematic diagram of convolution kernel pruning provided by the present invention. DETAILED DESCRIPTION

[0048] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. 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.

[0049] like Figure 1 As shown, the embodiment of the present invention discloses a coarse-grained and fine-grained combined neural network pruning method, comprising the following steps:

[0050] S1. Filter pruning:

[0051] According to the second norm and layer by layer, candidate filters that need to be pruned in the original model are selected, and group sparse training is performed on the candidate filters. After a certain number of rounds, candidate filters that are smaller than a threshold are pruned.

[0052] S2, convolution kernel pruning:

[0053] The convolution kernels are sorted according to the binary norm and the importance of each layer, and the convolution kernels that need to be pruned are obtained layer by layer according to the predefined pruning rate.

[0054] S3, pattern set generation:

[0055] The convolution kernel is regularized and compressed in units of weights, and a pattern set that satisfies a pre-built pattern discriminant function is dynamically generated during the compression process.

[0056] S4. Assign a mode to each convolution kernel:

[0057] Match each convolution kernel to the optimal mode of the convolution kernel in the mode set.

[0058] S5. Perform convolution kernel pruning and pattern pruning.

[0059] S6, hard pruning: Set the parameters to be pruned to zero and perform hard pruning on the model.

[0060] S7. Fine-tuning and retraining:

[0061] The hard pruned model is retrained and fine-tuned in combination with the knowledge distillation method to obtain the final pruned model.

[0062] The following is a further description of each of the above steps:

[0063] S1. Filter pruning

[0064] The process is as follows Figure 4As shown. Due to the large pruning granularity, regularized training of too many parameters may cause the model to be over-pruned and the accuracy is difficult to restore. Therefore, we first find the candidate filters that may be pruned, perform group sparse training on these filters, and then actually prune the filters smaller than the threshold after a certain number of rounds. Specifically, transform the convolution matrix into (input_channel*kernel_size*kerne_size,output_channel), and sort the sum of the two norms of each column, as shown in formula (1). Find p% unimportant, and set the corresponding column mask of the filter smaller than the threshold m to 0.

[0065]

[0066] Since the convolutional layer after filter pruning is still a dense matrix, it will not have any impact on subsequent links, and filter pruning will reduce the size of the convolution matrix, making the subsequent compression speed faster, so unimportant filters are removed first. Specifically, transform both the convolution matrix and the mask matrix into (input_channel*kernel_size*kernel_size,output_channel), calculate the bi-norm for each column as above, and record the columns less than the 0.1m threshold as the filters to be pruned. Traverse the neural network layer by layer, save the input and output channels to be pruned in each layer, for the lth layer, the output channel to be discarded is the column index corresponding to the pruned filter of this layer (except the last convolution layer), and the input channel is the row index corresponding to the pruned filter of the l-1th layer (except the first convolution layer). The rows and columns to be pruned are discarded through the index_select function.

[0067] S2, convolution kernel pruning

[0068] The schematic diagram of convolution kernel pruning is as follows Figure 5 The specific implementation is as follows: after transforming the convolution matrix, the sum of the two norms of the convolution kernels is calculated one by one. The importance of the kth convolution kernel of the fth filter in each layer is shown in formula (2). The parameters are sorted within the layer, and the least important k convolution kernels are found according to the predefined pruning rate, and the mask is set to 0.

[0069]

[0070] S3. Pattern set generation

[0071] The parameters of the model are set as follows in the embodiment of the present invention: Assuming that the number of convolutional layers of the neural network is N, the number of input channels is K, the number of output channels is F, and the weight and offset of the nth layer are represented by W n 、b n For each layer's f-th output channel and k-th output channel, the corresponding convolution kernel is Z f,k,:,:, the weight of the i-th row and j-th column inside the convolution kernel is Z f,k,i,j .

[0072] As an unstructured pruning, pattern pruning has relatively good regularity and can be optimized by subsequent compilers to reduce the neural network inference time. Pattern set generation is divided into static and dynamic generation. Both generate different patterns through the importance evaluation method within the convolution kernel. The difference is that static generation generates patterns before pruning, while dynamic generation generates patterns during the pruning process.

[0073] For static pattern generation, the traditional solution is to select the maximum weight pattern containing the central element. The convolution is selected based on the mathematical derivation of the gradient, which has a rigorous theoretical derivation. However, since structured pruning and pattern pruning are performed simultaneously, the model weights cannot change significantly after removing redundant filters. This means that the model must be pre-trained and well-distributed, and the speed of pruning is greatly limited.

[0074] For dynamic pattern generation, the traditional method of dynamic pattern pool generation is to gradually generate candidate patterns and complete the pruning of unimportant cores. Although the relevant concept was proposed, the idea was not formalized, and because pattern generation is very time-consuming, the pattern is actually pre-generated in its code.

[0075] The embodiment of the present invention combines static and dynamic pattern set generation methods to construct a pattern discrimination function to determine whether the pattern set generated in the current round can be adopted. The core of the problem lies in how to determine whether a pattern set is a good pattern set.

[0076] Previous research on the importance of kernel values ​​has mainly proposed the following points:

[0077] (1) The center weight in the 3*3 kernel is crucial.

[0078] (2) Important weights within a convolution kernel are often adjacent.

[0079] (3) The larger the absolute value of an element, the more important it is.

[0080] It is worth noting that there are some differences between the importance of elements in the convolution kernel and the pattern generation standard. The individual for judging the importance of the kernel is a convolution kernel, while the pattern generation classifies the convolution kernel into a pattern through a clustering-like idea. Therefore, some convolution kernels cannot match their corresponding best patterns. Therefore, the diversity of convolution kernels is also an important criterion for measuring the quality of pattern sets.

[0081] Therefore, if Figure 2As shown, the present invention adopts a new pattern set generation method, setting each pattern to have 4 non-empty elements and the rest to null values. After every r rounds, all 3*3 convolution kernels in the neural network are subjected to pattern generation, and the number of patterns to be generated is set to k. Based on hardware friendliness and practical effects, the present invention limits the value of the hyperparameter k to between 2, 4, 8 and 12.

[0082] Specifically:

[0083] S31, traverse each convolution kernel of the model, and select the four positions with the largest absolute values ​​of weights to form the optimal mode of the convolution kernel;

[0084] S32, globally counting the frequencies of occurrence of each of the optimal patterns, and selecting k patterns with the highest frequencies as the pattern set generated in the current round;

[0085] S33. Use the pattern discriminant function to judge the weight adjacency of the pattern set, the number of pattern set shapes and the convergence of the current model to evaluate whether the pattern set generated in the current round can be used as the final pattern set.

[0086] The embodiment of the present invention provides three judgment conditions. If and only if all the conditions are met, the pattern set generation process ends. The conditions are as follows:

[0087] (1) All elements in a single pattern set are adjacent.

[0088] (2) The pattern set shape is less than or equal to k / 2.

[0089] (3) The current gradient mean is less than the threshold q.

[0090] The specific judgment process is as follows:

[0091] (1) To determine the adjacency of elements, traverse each non-zero element position (i, j) of the pattern and determine whether the four adjacent positions (i+1, j), (i-1, j), (i, j+1), and (i, j-1) are inside the convolution kernel. If they are inside the convolution kernel, determine whether the position is non-zero. If a non-zero element does not exist at any of the neighboring positions of an element, then the elements of the pattern are not all adjacent. If this situation does not occur after the traversal is completed, then all the elements are adjacent.

[0092] (2) As for the number of pattern set shapes, since the adjacency check is performed first, there is no need to consider non-adjacent pattern combinations. 9 4=126 situations, of which there are only 36 situations where all are adjacent, so the present invention performs an enumeration judgment. First, the convolution kernel weight index is numbered, and the number corresponding to the jth weight in the i-th row is i*kernel_size+j. Use a dictionary to record the pattern shape corresponding to the position combination, and the key of the dictionary is the numbering of non-zero weights from small to large. Since the convolution kernel processed by the present invention is 3*3, the maximum number is 9. When the numbers are spliced ​​together, there will be no ambiguity. For example, the key corresponding to the four positions 1, 3, 5, and 9 is "1359". If the size of the convolution kernel increases, other encoding methods need to be considered. The value of the dictionary is the pattern shape number. Traverse each pattern, query the number corresponding to its shape, and use a set to record whether each number exists. If not, the pattern shape number is increased by 1, and the number is added to the set.

[0093] (3) To determine the average gradient, the gradient at each position is obtained through the grad attribute of the tensor, and the mean is obtained through the mean function.

[0094] S4. Assign a mode to each convolution kernel:

[0095] Let the pattern set be S, and the optimization objective is shown in formula (3). Since the patterns of each convolution kernel are independent of each other, we traverse the pattern set one by one, calculate the sum of the absolute values ​​of the weights of the convolution kernels on the patterns, and select the maximum sum index as the pattern of the convolution kernel. After the allocation is completed, the shape of the pattern set of the convolution kernel will no longer change.

[0096]

[0097] S5, convolution kernel pruning and mode pruning

[0098] The present invention performs regularization training on the weights that may be pruned in stages. For each pruning method, a 0-1 mask matrix is ​​used to control whether the weight is retained. The pruned weight is masked to 0 and the retained weight is 1. k The corresponding filter, convolution kernel and pattern pruning mask matrices are I k_f ,I k_k ,I k_p , whose shape is the same as the convolution matrix. Let the regularization coefficients corresponding to the filter, convolution kernel, and pattern pruning stage be λ respectively. f , k , p , the loss function is shown in formula (4).

[0099]

[0100] Among them, cross entropy is used to characterize the difference between the model prediction value and the true value. Assume that the number of neural network output categories is n, the distribution of the true value of the neural network output is y_true, and the predicted value is y_pred, as shown in formula (5).

[0101]

[0102] For the optimizer, the present invention adopts SGD stochastic gradient descent as an iterative method, which is used to optimize the differentiable objective function. The weights and bias terms are iteratively updated by calculating the gradient of the loss function on small batches of data, and the learning rate decay strategy MultiStepLR is used for compression pruning.

[0103] At the beginning of the compression phase, the mask matrix is ​​initialized to all 1s, and then negated to 0. At this time, the loss function is the same as the normal training phase. Next, the values ​​of the three mask matrices are changed in stages, so that the compression granularity of the model can be controlled. The schematic diagram of the three pruning methods is as follows Figure 3 As shown in the figure, the filter pruning granularity is the largest, the convolution kernel pruning is in the middle, and the pattern pruning is the smallest. Since pruning should be a process from coarse to fine, the filter sparseness and pruning of the model are first performed, and then the convolution kernel and pattern granularity are pruned.

[0104] S6, hard pruning

[0105] Hard pruning actually prunes the internal convolution kernel of the model. At this time, the parameters to be pruned are already very close to 0. The weight parameters of each layer are traversed and the model parameters are updated by multiplying the convolution matrix with the mask matrix so that the weight to be pruned is 0. The updated output is set to Wk', as shown in formula (6).

[0106] W k '=W k ⊙I k_k ⊙I k_p (6).

[0107] S7. Fine-tuning and retraining

[0108] After hard pruning the model, the embodiment of the present invention retrains the model. The generated model is further fine-tuned by using the knowledge distillation method. Vanilla knowledge distillation is a classic accuracy recovery method in the field of image classification, which distills a large set of teacher models into a smaller student model without causing a large loss of accuracy. In this module, the distillation loss function is used to force similar logit outputs of the original network and the pruned network, as shown in formula (7). In the present invention, the fixed balance coefficient α is 0.4 and the temperature T is 4.

[0109]

[0110] In order to further prove the superiority of the pruning method of the present invention, the present invention conducted the following experimental verification:

[0111] The present invention selects cifar10 and cifar100 datasets, vgg16, resnet32, and resnet50 as the networks to be pruned, and conducts experiments on a general GPU platform. The contents and preliminary results of each module are shown below.

[0112] Experiment 1: Conduct multiple comparative experiments to explore whether the conclusions drawn from the study of the importance of convolution kernel parameters are applicable to model pruning. An experimental group and a control group are set up, and the accuracy of the pruned model is used to measure whether the pattern set is good. The specific experimental design and results are shown in Table 1.

[0113] Table 1 Comparison of different mode pruning methods

[0114]

[0115] Experiment 2: Conduct separate pruning experiments with pattern numbers of 2, 4, 8, and 12, and compare the accuracy with other existing results under the same pattern number. The results show that the present invention is superior to the existing results.

[0116] Experiment 3: For several indicators of the mode judgment function, the present invention verifies its correctness during the compression process, that is, under good training conditions, the model will gradually meet the judgment conditions of the mode judgment function. With the number of compression rounds as the horizontal axis, the gradient, the number of adjacent element patterns, and the number of pattern set shapes are plotted to verify the feasibility and accuracy of the evaluation conditions.

[0117] Experiment 4: Limit the number of patterns to 4, perform structured and unstructured pruning on the model, compare the model parameter compression ratio and accuracy, and compare with other hybrid pruning results. According to the current results, the present invention is slightly better than the current research and has higher hardware friendliness.

[0118] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.

[0119] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A coarse-grained and fine-grained neural network pruning method, It is characterized in that The following steps are involved: According to the second norm and layer by layer, the candidate filters to be pruned in the original model are selected, and group sparse training is performed on the candidate filters. After a certain number of rounds, the candidate filters smaller than the threshold are pruned; Sort the importance of convolution kernels layer by layer according to the binary norm, and obtain the convolution kernels that need to be pruned layer by layer according to the predefined pruning rate; The convolution kernel is compressed and regularized in units of weights, and a pattern set that satisfies the pre-built pattern discrimination function is dynamically generated during the compression process; Matching each convolution kernel to the optimal mode of the convolution kernel in the mode set; Perform convolution kernel pruning and mode pruning; Set the parameters to be pruned to zero and perform hard pruning on the model; Combine knowledge distillation method to retrain and fine-tune the hard pruned model to obtain the final pruned model; We selected the cifar10 and cifar100 datasets, vgg16, resnet32, and resnet50 as the networks to be pruned, and conducted experiments on a general-purpose GPU platform to evaluate the accuracy of the final model after pruning. The final pruned model is deployed on the embedded device.

2. A neural network pruning method combining coarse and fine granularity according to claim 1, It is characterized in that The generation process of the pattern set is: Traverse each convolution kernel of the model and select the four positions with the largest absolute weight values ​​to form the optimal mode of the convolution kernel; Globally count the frequencies of occurrence of each of the optimal patterns, and select the k patterns with the highest frequencies as the pattern set generated in the current round; The pattern discriminant function is used to judge the weight adjacency of the pattern set, the number of pattern set shapes and the convergence of the current model to evaluate whether the pattern set generated in the current round can be used as the final pattern set.

3. A coarse-grained and fine-grained combined neural network pruning method according to claim 2, It is characterized in that The judgment condition of the weighted adjacency of the pattern set is that all elements in a single pattern set are adjacent; specifically: Traverse each non-zero element position (i, j) of the pattern set and determine whether the four adjacent positions (i+1, j), (i-1, j), (i, j+1), and (i, j-1) are inside the convolution kernel; If it is inside the convolution kernel, determine whether the position is non-zero; if there is no non-zero element at the neighboring position of any element, then the pattern elements are not all adjacent. If this situation does not occur after the traversal is completed, then all are adjacent.

4. A coarse-grained and fine-grained combined neural network pruning method according to claim 2, It is characterized in that The judgment condition for the number of pattern set shapes is: pattern set shape ≤ k / 2; specifically: For the convolution kernel weight index number, the number corresponding to the jth weight in the i-th row is i*kernel_size+j; Use a dictionary to record the shape of the pattern set corresponding to the position combination. The key of the dictionary is the concatenation of numbers with non-zero weights from small to large. Traverse each pattern set, query the corresponding number of its shape, use a set to record whether each number exists, if not, increase the number of pattern set shapes by 1, and add the number to the set.

5. A coarse-grained and fine-grained combined neural network pruning method according to claim 2, It is characterized in that The judgment condition for the convergence of the current model is: the current gradient mean is less than the threshold q; specifically: The gradient of each position is obtained through the grad attribute of the tensor, and the mean is obtained through the mean function.

6. A coarse-grained and fine-grained combined neural network pruning method according to claim 1, It is characterized in that The step of pruning candidate filters smaller than a threshold value comprises: Transform the convolution matrix and the mask matrix into the same matrix, calculate the bi-norm for each column, and record the columns less than the 0.1m threshold as the filters to be pruned; Traverse the model layer by layer and save the input and output channels to be pruned in each layer. For the lth layer, the output channel to be discarded is the column index corresponding to the pruning filter of this layer, and the input channel is the row index corresponding to the pruning filter of the l-1th layer; The rows and columns to be pruned are discarded through the index_select function.

7. A coarse-grained and fine-grained combined neural network pruning method according to claim 1, It is characterized in that The process of obtaining the convolution kernel that needs to be pruned is: Transform the convolution matrix and calculate the sum of the two norms of the convolution kernels one by one; Sort the importance of all convolution kernels of each filter in each layer according to the sum of the two norms; According to the predefined pruning rate, find the least important k convolution kernels and set the mask to 0 as the convolution kernels that need to be pruned.

8. A coarse-grained and fine-grained combined neural network pruning method according to claim 1, It is characterized in that The matching each convolution kernel to the optimal mode of the convolution kernel in the mode set includes: Traverse the final pattern sets one by one, calculate the sum of the absolute values ​​of the weights of the convolution kernels on each pattern set, and select the maximum sum index as the pattern set of the convolution kernel; after the allocation is completed, the shape of the pattern set of the convolution kernel no longer changes.

9. The neural network pruning method of claim 1, It is characterized in that In the filter pruning stage, convolution kernel pruning stage and pattern pruning stage, the weights that may be pruned are regularized and trained respectively, and the cross entropy is used to quantify the difference between the model prediction value and the true value.

10. A coarse-grained and fine-grained combined neural network pruning method according to claim 9, It is characterized in that In the process of regularizing the training of weights that may be pruned in stages, the iterative method of SGD stochastic gradient descent is used to optimize the differentiable objective function. The weights and bias terms are iteratively updated by calculating the gradient of the loss function on small batches of data, and the learning rate decay strategy MultiStepLR is used for compression pruning.