Structured sparsity and image classification method for network model, and related apparatus
By performing structured sparse processing on the network layer to be sparse in the deep learning network model, the problem of high computational overhead of the deep learning network model is solved, and the computational overhead is reduced and the inference efficiency is improved while ensuring accuracy.
Patent Information
- Application Number
- PCT/CN2024/083269
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-03-28
- Filing Date
- 2024-03-22
- Publication Date
- 2024-10-03
AI Technical Summary
Deep learning network models have a large computational overhead, resulting in long inference times. There is an urgent need for a method that can reduce computational overhead while ensuring model accuracy.
By performing structured sparse processing on the network layer to be sparse in the deep learning network model, the number of input matrix elements of the target operator of each network layer is reduced. The input matrix is pruned using the overall sparse method and the partition sparse method, and is processed through matrix processing. The operator maintains the isomorphism of the output matrix and optimizes the sparse ratio and scaling ratio to improve model accuracy.
It effectively reduces the computational overhead of the deep learning network model and improves the inference efficiency. At the same time, when the initial sparse model accuracy does not reach the target accuracy, the model accuracy is further improved by optimizing the sparse ratio and scaling factor.
Smart Images

Figure CN2024083269_03102024_PF_FP_ABST
Abstract
Description
Structured sparse network model, image classification method and related device
[0001] This application claims priority to Chinese patent application No. 202310350886.1 filed on March 28, 2023, entitled “Structured sparsity of network models, image classification methods and related devices,” the entire contents of which are incorporated herein by reference. Technical Field
[0002] The present application relates to the field of artificial intelligence technology, and in particular to a structured sparse network model, an image classification method and related devices. Background Art
[0003] With the rapid development of deep learning technology, deep learning network models have become increasingly large in size and have a growing number of parameters. This has led to high computational overhead in inference on deep learning network models, meaning that inference on deep learning network models takes a significant amount of time. Therefore, there is an urgent need for a structured sparse method for deep learning network models that can reduce computational overhead while ensuring model inference accuracy.
[0004] Summary of the Invention
[0005] This application provides a structured sparse network model, an image classification method and related devices, which can solve the problem of high model inference cost in related technologies. The technical solution is as follows:
[0006] In a first aspect, a method for structured sparsification of a network model is provided. The method comprises: performing initial structured sparsification on each of a plurality of network layers to be sparsified, including a deep learning network model, according to a target sparsification method and a target sparsification ratio, to obtain an initial sparse network model. Based on a test sample set, the accuracy of the initial sparse network model is determined. If the accuracy of the initial sparse network model meets the target accuracy, the initial sparse network model is determined as the deep learning network model after structured sparsification.
[0007] By performing initial structured sparsification on each of the multiple layers to be sparsified in a deep learning network model, the number of elements in the input matrix of the target operator of each layer is reduced, thus resolving the problem of the time-consuming inference of the target operator of each layer. Since the inference cost of a deep learning network model is related to the inference cost of each layer, reducing the time consumption of the target operator of each layer further reduces the computational overhead of the deep learning network model.
[0008] Among them, the number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model.
[0009] Optionally, the deep learning network model is applicable to any type of task scenario among semantic segmentation, image classification, object detection, image generation, language modeling, question answering, machine translation, and sentiment analysis.
[0010] Optionally, the multiple network layers all include modules that use a self-attention mechanism, and the target operator is a softmax operator. It should be noted that in practical applications, in addition to including multiple network layers to be sparse, the deep learning network model may also include other types of network layers. Moreover, the multiple network layers may also include modules that use other mechanisms.
[0011] Since the process of performing initial structured sparseness on each of the multiple network layers is the same, any one of the multiple network layers is used as the target network layer. The process of performing initial structured sparseness on the target network layer is described below using the target network layer as an example. The process of performing initial structured sparseness on the other network layers in the multiple network layers can refer to the process of performing initial structured sparseness on the target network layer.
[0012] Optionally, perform initial structured sparsification on the target network layer according to the following steps (1)-(2).
[0013] (1) A target sparse operator is added to the target network layer according to the target sparse mode and target sparse ratio. The target sparse operator is used to prune the elements of the input matrix of the target operator of the target network layer.
[0014] In other words, the target network layer is initially structured and sparsely populated using the target sparse operator, reducing the number of elements in the input matrix of the target operator of the target network layer. This solves the problem of the target operator inference taking a long time, thereby reducing the computational overhead of the deep learning network model.
[0015] Target sparsity methods include global sparsity and partitioned sparsity. In different cases, the target sparsity operator is added to the target network layer in different ways according to the target sparsity method and target sparsity ratio. Therefore, the following two cases are divided into the following sections for explanation.
[0016] In the first case, the target sparsity mode is the overall sparsity mode, and the target sparsity ratio is M%, where M is greater than 0 and less than 100. In this case, a first sparsity operator is added to the target network layer according to the target sparsity mode and the target sparsity ratio. The first sparsity operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top 1-M% of the elements with the largest values in the target dimension.
[0017] In the second case, the target sparsity mode is the partitioned sparsity mode, the target sparsity ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R. In this case, a second sparse operator is added to the target network layer according to the target sparsity mode and the target sparsity ratio. The second sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top N elements with the largest values in each R elements of the target dimension.
[0018] (2) Adding a matrix processing operator to the target network layer, the matrix processing operator is used to process the mapping matrix of the target network layer according to the pruning result of the target sparse operator, so that the dimension of the output matrix of the target network layer is the same before and after the initial structured sparse operation of the target network layer.
[0019] The pruning result of the target sparse operator includes a position matrix. Based on the position matrix, the matrix processing operator determines the value element corresponding to each row element in the normalized matrix from the mapping matrix of the target network layer according to the relevant algorithm. The position matrix indicates the position of each element in the feature matrix after pruning in the feature matrix before pruning. The mapping matrix of the target network layer is obtained by mapping the data to be inferred by the target network layer according to the relevant algorithm. The normalized matrix is obtained by processing the pruned feature matrix by the target operator.
[0020] That is, the target network layer is initially structured and sparsified through the matrix processing operator to ensure the isomorphism between the output matrices before and after the initial structured sparsification of the target network layer, so that the target network layer with the initial structured sparsification can still be applied to the original network structure.
[0021] The test sample set includes multiple groups of sample data, each of which includes test samples and sample annotation results. The test samples are input into the initial sparse network model to obtain sample test results output by the initial sparse network model. Based on the sample test results and sample annotation results, the accuracy of the initial sparse network model is determined according to the relevant algorithm.
[0022] Optionally, the multiple groups of sample data include text type data, image type data, video type data, audio type data, etc.
[0023] Optionally, the deep learning network model is applicable to different types of task scenarios such as semantic segmentation, image classification, language modeling, question answering, etc.
[0024] Optionally, if the task scenario for the deep learning network model is image classification, the test sample set includes test images and classification annotation results. If the task scenario for the deep learning network model is question answering, the test sample set includes test questions in text format and answer annotation results.
[0025] Of course, in practical applications, the accuracy of the initial sparse network model may not meet the target accuracy. In this case, based on the target accuracy, target sparsity ratio, and test sample set, the sparsity ratio of each network layer in the initial sparse network model is optimized to obtain a deep learning network model after structured sparseness. In other words, when the accuracy of the initial sparse network model cannot meet the user's usage requirements, the accuracy of the initial sparse network model can be further improved by optimizing the sparsity ratio of each network layer in the initial sparse network model.
[0026] Optionally, the sparsity ratio of each network layer in the initial sparse network model is optimized according to the following steps (1)-(5).
[0027] (1) Based on the target accuracy and the test sample set, determine the sensitivity of each network layer in the initial sparse network model.
[0028] Based on the test sample set, the accuracy of each network layer in the initial sparse network model is determined. Based on the target accuracy and the accuracy of each network layer, the sensitivity of each network layer is determined.
[0029] (2) Determine the optimized sparsity ratio of each network layer based on the sensitivity of each network layer, the number of the multiple network layers, the total number of elements in the target dimension, and the target sparsity ratio.
[0030] (3) Based on the optimized sparse ratio of each network layer, the sparse ratio of each network layer in the initial sparse network model is optimized to obtain an optimized network model.
[0031] (4) Based on the test sample set, determine the accuracy of the optimized network model.
[0032] (5) When the accuracy of the optimized network model reaches the target accuracy, the optimized network model is determined as a deep learning network model after structured sparsification.
[0033] It should be noted that, according to the target sparsification method and target sparsification ratio, initial structured sparsification is first performed on each of the multiple network layers to obtain an initial sparse network model. The sparsification ratios of each of the multiple network layers included in the initial sparse network model are the same, namely, the target sparsification ratio. To further improve the accuracy of the initial sparse network model, the sparsification ratio of each network layer in the initial sparse network model is optimized to obtain an optimized network model. Therefore, while ensuring that the average of the optimized sparsification ratios of the multiple network layers is the same as the target sparsification ratio, the optimized network model may contain network layers with an optimized sparsification ratio of 0%. In other words, the optimized network model may contain network layers that do not require structured sparsification. If the accuracy of the optimized network model meets the target accuracy, the optimized network model is directly determined as the deep learning network model after structured sparsification. In other words, only some of the multiple network layers to be sparsified, included in the deep learning network model, need to be structured sparsified to obtain the deep learning network model after structured sparsification.
[0034] Of course, in practical applications, the accuracy of the optimized network model may not reach the target accuracy. In this case, an element scaling operator is added to each network layer in the optimized network model. The element scaling operator is used to scale the elements in the output matrix of the target operator in the network layer. Based on the initial scaling range and multiple granularities, the scaling factor of the element scaling operator in each network layer in the optimized network model is optimized to obtain a deep learning network model after structured sparseness.
[0035] Specifically, if the accuracy of the initial sparse network model fails to meet user requirements, the sparsity ratio of each network layer in the initial sparse network model is optimized to obtain an optimized network model. If the accuracy of the optimized network model still fails to meet user requirements, the scaling factor of the element scaling operator of each network layer in the optimized network model is optimized to further improve the accuracy of the optimized network model, thereby obtaining a structured sparse deep learning network model.
[0036] It should be noted that when the accuracy of the initial sparse network model cannot meet the user's usage requirements, further improving the accuracy of the initial sparse network model by optimizing the sparsity ratio of each network layer alone, or by combining the optimization of the sparsity ratio of each network layer and the optimization of the scaling factor of the element scaling operator of each network layer is only an example. Moreover, the above method of improving model accuracy is only applicable when the target sparsity method is the overall sparsity method. When the target sparsity method is the partitioned sparsity method, the target sparsity ratio of each network layer is fixed to N:R, and the optimized sparsity ratio of each network layer cannot be adaptively determined. Therefore, a model accuracy optimization method that is applicable to both the overall sparsity method and the partitioned sparsity method is proposed. That is, an element scaling operator is added to each network layer in the initial sparse network model, and the element scaling operator is used to scale the elements in the output matrix of the target operator of the network layer. Based on the initial ratio range and multiple granularities, the scaling factor of the element scaling operator of each network layer in the initial sparse network model is optimized to obtain a deep learning network model after structured sparseness.
[0037] That is to say, when the target sparsity mode is the overall sparsity mode, the optimized network model is obtained by optimizing the sparsity ratio of each network layer alone. Alternatively, the optimized network model is obtained by optimizing the scaling factor of the element scaling operator of each network layer alone. Alternatively, the optimized network model is obtained by optimizing the sparsity ratio of each network layer and optimizing the scaling factor of the element scaling operator of each network layer. When the target sparsity mode is the partitioned sparsity mode, the optimized network model can only be obtained by optimizing the scaling factor of the element scaling operator of each network layer.
[0038] For example, according to the following steps (1)-(3), the scaling factor of the element scaling operator of each network layer in the initial sparse network model is optimized by a multi-granularity parameter search method.
[0039] (1) Let i = 1, take the initial magnification range as the i-th magnification range, select the i-th granularity from multiple granularities in descending order, and search for the reference scaling factor corresponding to each network layer within the i-th scaling range according to the i-th granularity. The reference scaling factor is the scaling factor when the accuracy of the corresponding network layer is the maximum.
[0040] (2) When i is equal to the total number of the multiple granularities, the scaling factor of the element scaling operator of each network layer is set to the network model of the reference scaling factor, and the network model is determined as the deep learning network model after structured sparsification.
[0041] (3) When i is less than the total number of the multiple granularities, let i=i+1, and determine the i-1th magnification range corresponding to each network layer based on the i-1th granularity and the i-th granularity in order from large to small among the multiple granularities, and the reference scaling factor corresponding to each network layer, and return to the step of searching for the reference scaling factor corresponding to each network layer from the i-th scaling factor range according to the i-th granularity.
[0042] This solution optimizes the scaling factor of the element scaling operator of each network layer through a cyclic iteration to obtain an optimized network model. If i equals the total number of granularities, indicating that the current optimized network model is reliable, the iterative process is stopped and the current optimized network model is determined as the deep learning network model after structured sparsification. If i is less than the total number of granularities, indicating that the current optimized network model is unreliable, the network model processed in the i-th iteration is used as the network model processed in the i+1-th iteration, and the i+1-th iteration is continued.
[0043] In a second aspect, a structured sparse method for a network model is provided, which is applied to a server, and the method includes: receiving a target sparse mode, a target sparse ratio, a deep learning network model, a test sample set, and a target accuracy sent by a terminal. According to the target sparse mode and the target sparse ratio, initial structured sparseness is performed on each of the multiple network layers to be sparse included in the deep learning network model to obtain an initial sparse network model. Based on the test sample set, the accuracy of the initial sparse network model is determined. When the accuracy of the initial sparse network model reaches the target accuracy, the initial sparse network model is determined as the deep learning network model after structured sparseness, and the deep learning network model after structured sparseness is sent to the terminal.
[0044] Among them, the number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model.
[0045] In a third aspect, an image classification method is provided, the method comprising: inputting a target image to be classified into an image classification model, the image classification model comprising a plurality of sparse network layers. Based on a mapping matrix of a target network layer, a feature matrix is determined, the target network layer being any one of the plurality of network layers. Elements in the feature matrix are pruned to obtain a pruned feature matrix and a position matrix, the position matrix indicating the position of each element in the pruned feature matrix in the feature matrix before pruning. The pruned feature matrix is input into a target operator of a target network layer to obtain a normalized matrix. Based on the position matrix and the normalized matrix, an output matrix of the target network layer is determined. Based on the output matrix of the target network layer, a classification result output by the image classification model is determined.
[0046] The target image to be classified is input into the image classification model, which then processes the target image through the multiple, already sparse, network layers included in the image classification model. By pruning elements in the feature matrix through the target network layer, the time required for the target operator in the target network layer to infer the normalized matrix can be reduced. Since the inference cost of the image classification model is related to the inference cost of the target operator, reducing the time spent on the target operator further reduces the computational overhead of the image classification model, allowing the image classification model to output classification results more quickly.
[0047] The target network layer includes a target sparse operator having a target sparse mode and a target sparse ratio. According to the target sparse mode and the target sparse ratio, the target sparse operator is used to prune elements in the feature matrix to obtain a pruned feature matrix and a position matrix.
[0048] Target sparsity methods include global sparsity and partition sparsity. In each case, the target sparsity operator prunes elements in the feature matrix in different ways, depending on the target sparsity method and target sparsity ratio. Therefore, the following two cases are explained separately.
[0049] In the first case, the target sparsity mode is the global sparsity mode, the target sparsity ratio is M%, M is greater than 0 and less than 100, and the target sparsity operator is the first sparsity operator. In this case, the first sparsity operator is used to retain the top 1-M% of the elements with the largest values in the target dimension in the feature matrix, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0050] That is, when the target sparsity mode is the overall sparsity mode, the values of the elements in the target dimension in the feature matrix are directly compared, so as to retain the top 1-M% elements with the largest values to obtain the pruned feature matrix.
[0051] In the second case, the target sparsity mode is partitioned sparsity mode, the target sparsity ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R. The target sparsity operator is the second sparsity operator. In this case, the second sparsity operator is used to retain the first N elements with the largest values among every R elements in the feature matrix located in the target dimension, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0052] That is, when the target sparsity mode is partitioned sparsity, each element in the feature matrix located in the target dimension is first divided into multiple regions, each of which includes R adjacent elements. Then, for each of the multiple regions, the values of the R elements included in the region are compared, and the top N elements with the largest values in the region are retained to obtain the pruned feature matrix.
[0053] Optionally, the target network layer also includes an element scaling operator, and after the pruned feature matrix is input into the target operator of the target network layer to obtain a normalized matrix, the elements in the normalized matrix can be scaled by the element scaling operator. Then, the above-mentioned determination of the output matrix of the target network layer directly based on the position matrix and the normalized matrix is only an example. Optionally, the output matrix of the target network layer can also be determined based on the position matrix and the scaled normalized matrix, thereby further improving the accuracy of the output matrix of the target network layer.
[0054] In a fourth aspect, a structured sparse device for a network model is provided, wherein the structured sparse device for a network model has the function of implementing the structured sparse method for a network model described in the first aspect. The structured sparse device for a network model includes at least one module, which is configured to implement the structured sparse method for a network model described in the first aspect.
[0055] In a fifth aspect, a structured sparse device for a network model is provided, wherein the structured sparse device for a network model has the function of implementing the structured sparse method for a network model described in the second aspect. The structured sparse device for a network model includes at least one module, which is configured to implement the structured sparse method for a network model described in the second aspect.
[0056] In a sixth aspect, a structured sparse system of a network model is provided, wherein the structured sparse system of the network model includes a terminal and a server;
[0057] The terminal is used to send the target sparsity mode, target sparsity ratio, deep learning network model, test sample set and target accuracy to the server;
[0058] The server is configured to receive a target sparsification method, a target sparsification ratio, a deep learning network model, a test sample set, and a target accuracy sent by the terminal, and perform initial structured sparsification on each of a plurality of network layers to be sparsified included in the deep learning network model according to the target sparsification method and the target sparsification ratio to obtain an initial sparse network model;
[0059] The server is further configured to determine the accuracy of the initial sparse network model based on the test sample set. If the accuracy of the initial sparse network model reaches the target accuracy, the initial sparse network model is determined as the deep learning network model after structured sparseness, and the structured sparse deep learning network model is sent to the terminal.
[0060] In a seventh aspect, an image classification device is provided, wherein the image classification device has the function of implementing the image classification method of the third aspect. The image classification device includes at least one module, wherein the at least one module is used to implement the image classification method provided in the third aspect.
[0061] In an eighth aspect, a computer device is provided, comprising a processor and a memory, wherein the memory is configured to store a computer program for executing the method provided in the first, second, or third aspect. The processor is configured to execute the computer program stored in the memory to implement the method provided in the first, second, or third aspect.
[0062] Optionally, the computer device may further include a communication bus, which is used to establish a connection between the processor and the memory.
[0063] In a ninth aspect, a computer-readable storage medium is provided, wherein the storage medium stores instructions. When the instructions are executed on a computer, the computer executes the steps of the method described in the first, second or third aspect above.
[0064] In a tenth aspect, a computer program product comprising instructions is provided. When the instructions are executed on a computer, the computer is caused to perform the steps of the method described in the first, second, or third aspects. Alternatively, a computer program is provided. When the computer program is executed on a computer, the computer is caused to perform the steps of the method described in the first, second, or third aspects.
[0065] In the eleventh aspect, a chip is provided, which includes a programmable logic circuit and program instructions, and when the chip runs on a computer, it is used to implement the steps of the method described in the first aspect, the second aspect or the third aspect.
[0066] The technical effects obtained in the above-mentioned second to eleventh aspects are similar to the technical effects obtained by the corresponding technical means in the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0067] FIG1 is a schematic diagram of a network layer processing inference data including a self-attention module provided by an embodiment of the present application;
[0068] Figure 2 shows the data distribution of each element in the normalized matrix in a shallow network layer with a smaller network level;
[0069] Figure 3 shows the data distribution of each element in the normalized matrix in a deep network layer with a larger network level;
[0070] FIG4 is a schematic diagram of the architecture of a computer device provided in an embodiment of the present application;
[0071] FIG5 is a flow chart of a structured sparse method for a network model provided by an embodiment of the present application;
[0072] FIG6 is a flow chart of another structured sparse method for a network model provided in an embodiment of the present application;
[0073] FIG7 is a flowchart of an image classification method provided in an embodiment of the present application;
[0074] FIG8 is a schematic diagram of pruning elements in a feature matrix provided by an embodiment of the present application;
[0075] FIG9 is a schematic diagram of another method for pruning elements in a feature matrix provided by an embodiment of the present application;
[0076] FIG10 is a flowchart of a question answering method provided in an embodiment of the present application;
[0077] FIG11 is a schematic diagram of the structure of a structured sparse device for a network model provided in an embodiment of the present application;
[0078] FIG12 is a schematic diagram of the structure of a structured sparse device of another network model provided in an embodiment of the present application;
[0079] FIG13 is a schematic structural diagram of an image classification device provided in an embodiment of the present application;
[0080] FIG14 is a schematic structural diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0081] In order to make the objectives, technical solutions and advantages of this application clearer, the implementation methods of this application will be further described in detail below with reference to the accompanying drawings.
[0082] Before explaining in detail the structured sparse method for the network model and the image classification method provided in the embodiments of the present application, the terminology, business scenarios and system architecture involved in the embodiments of the present application are first introduced.
[0083] To facilitate understanding, the terms involved in the embodiments of the present application are first explained.
[0084] Normalized Exponential (Softmax) Operator: As a generalization of the logistic function, this operator normalizes the logarithm of the gradient of a finite discrete probability distribution. The softmax operator is widely used in probabilistic classification problems, such as multinomial logistic regression, multinomial linear discriminant analysis, naive Bayes classifiers, and artificial neural networks.
[0085] Visual transformer (ViT): A transformer whose architecture is designed for visual processing tasks such as image classification.
[0086] Shifted Windows Transformer (Swin): A widely used transformer in vision that enhances the perception of high variation between pixels in high-resolution images by shifting windows.
[0087] Bidirectional encoder representations from transformers (BERT): A pre-trained transformer for natural language processing, it is the most important baseline in the field of deep learning natural language processing in recent years.
[0088] Next, the business scenarios involved in the embodiments of this application are introduced.
[0089] Deep learning network models can be applied to a variety of fields, including computer vision and natural language processing. In computer vision, deep learning network models are suitable for different task scenarios, such as semantic segmentation, image classification, object detection, and image generation. In natural language processing, deep learning network models are suitable for different task scenarios, such as language modeling, question answering, machine translation, and sentiment analysis.
[0090] The widespread application of deep learning network models depends on the multiple network layers they include. For example, after receiving inference data, any one of the multiple network layers maps the inference data according to a relevant algorithm to obtain a mapping matrix. A feature matrix is determined based on the mapping matrix. The feature matrix is input into the target operator of the network layer to obtain a normalized matrix. Then, based on the normalized matrix and the mapping matrix, the output matrix of the network layer is determined, and the inference result of the deep learning network model is determined based on the output matrix of the network layer.
[0091] Optionally, the multiple network layers all include a module that adopts a self-attention mechanism, and the target operator is a softmax operator. For ease of description, the module that adopts the self-attention mechanism is referred to as a self-attention module. That is, the wide application of deep learning network models is achieved by including multiple network layers of self-attention modules. For example, please refer to Figure 1, which is a schematic diagram of a network layer processing inference data including a self-attention module provided by an embodiment of the present application. In Figure 1, after the network layer receives the inference data, it maps the inference data into three mapping matrices, namely, a query matrix, a key matrix, and a value matrix, according to a relevant algorithm. The query matrix, the key matrix, and the value matrix all include elements of three dimensions. Based on the same two dimensions in the query matrix and the key matrix, a feature matrix is determined. The feature matrix is input into the softmax operator of the self-attention module to obtain a normalized matrix. Then, based on the normalized matrix and the value matrix, the output matrix of the network layer is determined.
[0092] The elements in the normalized matrix are used to indicate the probability distribution of the inference data, and the sum of the elements in the same row of the normalized matrix is always 1. For example, in Figure 1, the degree of lightness or darkness of each element indicates the probability: darker elements correspond to greater probabilities, and lighter elements correspond to smaller probabilities.
[0093] The elements in the normalized matrix are converted into the data distribution diagrams shown in Figures 2 and 3. Figure 2 shows the data distribution of each element in the normalized matrix in a shallow network layer with a smaller network layer, while Figure 3 shows the data distribution of each element in the normalized matrix in a deep network layer with a larger network layer. As can be seen from Figures 2 and 3, for both shallow and deep network layers, 90% of the elements in the normalized matrix have values less than 0.2. This means that more than 90% of the elements in the normalized matrix contribute little to 1, and the elements in the normalized matrix are naturally sparse. Therefore, these 90% elements are dispensable in the subsequent calculation of inference results and have little impact on the inference results output by the deep learning network model. However, when inputting the feature matrix into the softmax operator of the self-attention module to obtain the normalized matrix, the softmax operator of the self-attention module still needs to process these 90% elements, resulting in a high computational overhead for inference in the deep learning network model.
[0094] Since the normalized matrix is the output matrix of the softmax operator, and given the characteristics of the softmax operator, when the output matrix of the softmax operator is sparse, the input matrix of the softmax operator is also sparse. Therefore, when performing inference through a deep learning network model, in order to reduce the computational overhead of the deep learning network model, structured sparseness can be performed on each of the multiple network layers to be sparsed included in the deep learning network model according to the method provided in the embodiments of the present application.
[0095] Finally, the system architecture involved in the embodiments of this application is introduced.
[0096] The execution subject of the structured sparse method of the network model provided in the embodiment of the present application is a computer device. Please refer to Figure 4, which is a schematic diagram of the architecture of a computer device provided in the embodiment of the present application. The computer device includes an input module 401, a structured sparse module 402, an accuracy test module 403, a sparse parameter optimization module 404 and an output module 405. The user inputs the deep learning network model, the target sparse mode and the target sparse ratio through the input module 401. The input module 401 sends the deep learning network model, the target sparse mode and the target sparse ratio to the structured sparse module 402. The structured sparse module 402 receives the deep learning network model, the target sparse mode and the target sparse ratio sent by the input module 401, and performs initial structured sparse on each of the multiple network layers to be sparsed included in the deep learning network model according to the target sparse mode and the target sparse ratio to obtain an initial sparse network model. The structured sparse module 402 sends the initial sparse network model to the accuracy test module 403. The user also inputs a test sample set and a target accuracy through the input module 401, and the input module 401 sends the test sample set and the target accuracy to the accuracy testing module 403. The accuracy testing module 403 receives the initial sparse network model sent by the structured sparse module 402, as well as the test sample set and the target accuracy sent by the input module 401, and then determines the accuracy of the initial sparse network model through an artificial intelligence (AI) chip. The sparse parameter optimization module 404 is used to optimize the sparse ratio of each network layer in the initial sparse network model when the accuracy of the initial sparse network model does not reach the target accuracy, so as to obtain an optimized network model. Then, the accuracy of the optimized network model is determined by the accuracy testing module 403, and the accuracy of the optimized network model reaches the target accuracy. The output module 405 is used to output the optimized network model whose accuracy reaches the target accuracy.
[0097] That is to say, according to the structured sparse method of the network model provided in the embodiment of the present application, the user only needs to input the deep learning network model, the target sparsity method, the target sparsity ratio, the test sample set and the target accuracy into the computer device. Subsequently, through the interaction between the various modules included in the computer device, the structured sparse deep learning network model can be obtained, thereby realizing low-overhead and low-perception model reasoning.
[0098] A computer device can be any electronic product that can interact with a user through one or more methods such as a keyboard, touchpad, touch screen, remote control, voice interaction or handwriting device, such as a personal computer (PC), mobile phone, smart phone, personal digital assistant (PDA), wearable device, pocket PC (PPC), tablet computer, smart screen, car speaker, etc.
[0099] Those skilled in the art should understand that the above-mentioned computer devices are only examples, and other existing or future computer devices that are applicable to the embodiments of the present application should also be included in the scope of protection of the embodiments of the present application and are included here by reference.
[0100] It should be noted that the business scenarios and system architectures described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application, and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. Ordinary technicians in this field can know that with the emergence of new business scenarios and the evolution of system architecture, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0101] Next, the structured sparse method of the network model provided in the embodiment of the present application is explained in detail.
[0102] FIG5 is a flow chart of a structured sparse method for a network model provided by an embodiment of the present application. Referring to FIG5 , the method includes the following steps.
[0103] Step 501: Perform initial structured sparseness on each of the multiple network layers to be sparsed included in the deep learning network model according to the target sparseness mode and the target sparseness ratio to obtain an initial sparse network model.
[0104] Among them, the number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model.
[0105] Optionally, the multiple network layers all include modules that adopt a self-attention mechanism, and the target operator is a softmax operator. Based on the above description, the input matrix of the softmax operator is sparse, and the softmax operator is a parameter of the self-attention module. It can be seen that the self-attention module included in the network layer is sparse, which is equivalent to the sparsity of the network layer including the self-attention module in the deep learning network model. Therefore, by performing initial structured sparseness on each network layer including a self-attention module in the deep learning network model, it is equivalent to performing initial structured sparseness on the self-attention module included in each network layer.
[0106] It should be noted that in practical applications, in addition to including multiple network layers to be sparsed, the deep learning network model may also include other types of network layers. Moreover, the multiple network layers may also include modules using other mechanisms, which is not limited in the embodiments of the present application.
[0107] Since the process of performing initial structured sparseness on each of the multiple network layers is the same, any one of the multiple network layers is used as the target network layer. The process of performing initial structured sparseness on the target network layer is described below using the target network layer as an example. The process of performing initial structured sparseness on the other network layers in the multiple network layers can refer to the process of performing initial structured sparseness on the target network layer.
[0108] In some embodiments, the target network layer is initially structured sparsified according to the following steps (1)-(2).
[0109] (1) A target sparse operator is added to the target network layer according to the target sparse mode and target sparse ratio. The target sparse operator is used to prune the elements of the input matrix of the target operator of the target network layer.
[0110] In other words, the target network layer is initially structured and sparsely populated using the target sparse operator, reducing the number of elements in the input matrix of the target operator of the target network layer. This solves the problem of the target operator inference taking a long time, thereby reducing the computational overhead of the deep learning network model.
[0111] Target sparsity methods include global sparsity and partitioned sparsity. In different cases, the target sparsity operator is added to the target network layer in different ways according to the target sparsity method and target sparsity ratio. Therefore, the following two cases are divided into the following sections for explanation.
[0112] In the first case, the target sparsity mode is the overall sparsity mode, and the target sparsity ratio is M%, where M is greater than 0 and less than 100. In this case, a first sparsity operator is added to the target network layer according to the target sparsity mode and the target sparsity ratio. The first sparsity operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top 1-M% of the elements with the largest values in the target dimension.
[0113] In other words, when the target sparsity mode is global sparsity, a first sparsity operator is added to the target network layer. The parameters corresponding to the first sparsity operator are the global sparsity mode and the target sparsity ratio. In this way, the first sparsity operator can be used to uniformly prune the elements of the target dimension in the input matrix of the target operator of the target network layer, thereby improving the efficiency of element pruning.
[0114] Among them, the target dimension includes any one of a matrix row dimension and a matrix column dimension. When the target dimension is a matrix row dimension, the elements in the input matrix of the target operator of the target network layer are pruned row by row through the first sparse operator. When the target dimension is a matrix column dimension, the elements in the input matrix of the target operator of the target network layer are pruned column by column through the first sparse operator. Of course, in actual applications, the input matrix of the target operator may also include other dimensions, such as matrix channel dimensions, etc., which is not limited in the embodiments of the present application.
[0115] In the second case, the target sparsity mode is the partitioned sparsity mode, the target sparsity ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R. In this case, a second sparse operator is added to the target network layer according to the target sparsity mode and the target sparsity ratio. The second sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top N elements with the largest values in each R elements of the target dimension.
[0116] That is to say, when the target sparsity mode is a partitioned sparsity mode, a second sparse operator is added to the target network layer, and the parameters corresponding to the second sparse operator are the partitioned sparsity mode and the target sparsity ratio. Due to the limitations of the hardware settings of the computer device, the bandwidth of the computer device for processing data streams is usually very small, which makes it impossible for the computer device to uniformly prune the elements of the target dimension in the input matrix of the target operator at the same time. Therefore, first, according to the bandwidth of the computer device for processing data streams, the elements of the target dimension in the input matrix of the target operator are divided into multiple regions, and each of the multiple regions includes R adjacent elements. Then, the second sparse operator is used to partition and prune the elements of the target dimension in the input matrix of the target operator of the target network layer. Among them, R is set in advance according to the bandwidth of the computer device for processing data streams, and R is small relative to the total number of elements of the target dimension in the input matrix of the target operator.
[0117] (2) Adding a matrix processing operator to the target network layer, the matrix processing operator is used to process the mapping matrix of the target network layer according to the pruning result of the target sparse operator, so that the dimension of the output matrix of the target network layer is the same before and after the initial structured sparse operation of the target network layer.
[0118] The pruning result of the target sparse operator includes a position matrix. Based on the position matrix, the matrix processing operator determines the value element corresponding to each row element in the normalized matrix from the mapping matrix of the target network layer according to the relevant algorithm. The position matrix indicates the position of each element in the feature matrix after pruning in the feature matrix before pruning. The mapping matrix of the target network layer is obtained by mapping the data to be inferred by the target network layer according to the relevant algorithm. The normalized matrix is obtained by processing the pruned feature matrix by the target operator.
[0119] That is, the target network layer is initially structured and sparsified through the matrix processing operator to ensure the isomorphism between the output matrices before and after the initial structured sparsification of the target network layer, so that the target network layer with the initial structured sparsification can still be applied to the original network structure.
[0120] Step 502: Determine the accuracy of the initial sparse network model based on the test sample set.
[0121] The test sample set includes multiple groups of sample data, each of which includes test samples and sample annotation results. The test samples are input into the initial sparse network model to obtain sample test results output by the initial sparse network model. Based on the sample test results and sample annotation results, the accuracy of the initial sparse network model is determined according to the relevant algorithm.
[0122] In some embodiments, the multiple groups of sample data include text type data, image type data, video type data, audio type data, etc.
[0123] Based on the above description, the deep learning network model is applicable to different types of task scenarios, such as semantic segmentation, image classification, language modeling, and question answering. In some embodiments, when the task scenario applicable to the deep learning network model is image classification, the test sample set includes test images and classification annotation results. When the task scenario applicable to the deep learning network model is question answering, the test sample set includes test questions in text format and answer annotation results.
[0124] Next, we will take image classification as an example to fully explain how to determine the accuracy of the initial sparse network model based on the test sample set.
[0125] When deep learning network models are applied to image classification, the test sample set includes multiple sets of sample data, each of which includes a test image and classification annotation results. The test image is input into the initial sparse network model to obtain the classification results output by the initial sparse network model. Then, based on the classification results and classification annotation results output by the initial sparse network model, the accuracy of the initial sparse network model is calculated using the relevant algorithm.
[0126] Step 503: When the accuracy of the initial sparse network model reaches the target accuracy, the initial sparse network model is determined as the deep learning network model after structured sparseness.
[0127] Since the target accuracy is set by the user in advance according to usage requirements, if the accuracy of the initial sparse network model reaches the target accuracy, it means that the accuracy of the initial sparse network model obtained through initial structured sparseness can meet the user's usage requirements. Therefore, the initial sparse network model is directly determined as the deep learning network model after structured sparseness.
[0128] Of course, in practical applications, the accuracy of the initial sparse network model may not meet the target accuracy. In this case, based on the target accuracy, target sparsity ratio, and test sample set, the sparsity ratio of each network layer in the initial sparse network model is optimized to obtain a deep learning network model after structured sparseness. In other words, when the accuracy of the initial sparse network model cannot meet the user's usage requirements, the accuracy of the initial sparse network model can be further improved by optimizing the sparsity ratio of each network layer in the initial sparse network model.
[0129] In some embodiments, the sparsity ratio of each network layer in the initial sparse network model is optimized according to the following steps (1)-(5).
[0130] (1) Based on the target accuracy and the test sample set, determine the sensitivity of each network layer in the initial sparse network model.
[0131] Based on the test sample set, the accuracy of each network layer in the initial sparse network model is determined. Based on the target accuracy and the accuracy of each network layer, the sensitivity of each network layer is determined.
[0132] The sparse ratio of each network layer in the initial sparse network model is the same and is the target sparse ratio. For any network layer in the initial sparse network model, the sparse ratio of the network layer is kept fixed at the target sparse ratio, and the sparse ratios of other network layers are adjusted to 0 to obtain the test model corresponding to the network layer. Based on the above description, the test sample set includes multiple groups of sample data, each group of sample data in the multiple groups of sample data includes test samples and sample annotation results, and the test samples are input into the test model corresponding to the network layer to obtain the sample test results output by the test model corresponding to the network layer. Based on the sample test results and the sample annotation results, the accuracy of the test model corresponding to the network layer is determined according to the relevant algorithm, and then the accuracy of the test model corresponding to the network layer is determined as the accuracy of the network layer. That is, the accuracy of each network layer in the initial sparse network model refers to the accuracy of the initial sparse network model when the corresponding network layer is only initially structured sparsified according to the target sparsity ratio and other network layers are not initially structured sparsified.
[0133] As an example, based on the target accuracy and the accuracy of each network layer, the sensitivity of each network layer is determined according to the following formula (1).
[0134] Among them, in the above formula (1), Sense c represents the sensitivity of the c-th network layer, Represents the accuracy deviation between the accuracy of the c-th network layer and the target accuracy, Represents the maximum accuracy deviation among the accuracy deviations corresponding to the multiple network layers, ACC baseline Represents target accuracy, ACC c Represents the accuracy of the c-th network layer, that is, the accuracy of the initial sparse network model when the sparse ratios of other network layers are 0 and only the sparse ratio of the c-th network layer is the target sparse ratio.
[0135] Because the sparsity of the target operator varies across each network layer, after initial structured sparsification is performed on each layer using the same target sparsity ratio, each layer has a different sensitivity to sparsity. The sensitivity of a network layer indicates the accuracy loss of that layer; a higher sensitivity indicates a more severe accuracy loss for that layer.
[0136] (2) Determine the optimized sparsity ratio of each network layer based on the sensitivity of each network layer, the number of the multiple network layers, the total number of elements in the target dimension, and the target sparsity ratio.
[0137] As an example, the optimized sparsity ratio of each network layer is determined according to the following formulas (2) and (3).
[0138] In the above formulas (2) and (3), L represents the number of the multiple network layers, W represents the total number of elements in the target dimension, and Y represents the intermediate variable to be solved. represents the target sparsity ratio, Represents the optimized sparsity ratio of the c-th network layer.
[0139] That is, under the premise of ensuring that the average value of the optimized sparse ratio of the multiple network layers is the same as the target sparse ratio, the sensitivity of each network layer is firstly calculated. c , the number of network layers L, the total number of elements in the target dimension W and the target sparsity ratio According to the above formula (2), the intermediate variable Y is calculated. Then, the sensitivity Sense of each network layer is calculated. c , the intermediate variable Y, and the total number of elements W in the target dimension are substituted into the above formula (3) to obtain the optimized sparse ratio of each network layer The optimized sparse ratio of each network layer is adaptively determined by combining the sensitivity of each network layer, while ensuring that the average optimized sparse ratio of the multiple network layers is the same as the target sparse ratio.
[0140] (3) Based on the optimized sparse ratio of each network layer, the sparse ratio of each network layer in the initial sparse network model is optimized to obtain an optimized network model.
[0141] After determining the optimized sparse ratio of each network layer according to the above step (2), the sparse ratio M% of each network layer in the initial sparse network model is replaced with the optimized sparse ratio of the corresponding network layer to obtain an optimized network model. The sparse ratio of each network layer in the optimized network model is different. That is, when the target sparse mode is the overall sparse mode, for each network layer in the initial sparse network model, the target sparse ratio M% corresponding to the first sparse operator in the network layer is replaced with the optimized sparse ratio of the network layer. In this way, each network layer in the optimized network model can adaptively prune the elements of the target dimension in the input matrix of the target operator according to the corresponding optimized sparse ratio, control the accuracy loss of the model within a smaller range, and improve the accuracy of the optimized network model.
[0142] Assume that the initial sparse network model includes 6 network layers, and the sparse ratio of each network layer is the same and the target sparse ratio is 80%. Based on the target accuracy, target sparse ratio, and test sample set, the sparse ratio of each network layer in the initial sparse network model is optimized. The relevant information of the optimized network model is shown in Table 1. In Table 1, the optimized network model includes 6 network layers, and the sparse ratios of these 6 network layers are 80%, 90%, 82%, 75%, 73%, and 80%, respectively. The average optimized sparse ratio of these 6 network layers is still 80%. As can be seen from Table 1, the higher the sensitivity of the network layer, the smaller the optimized sparse ratio of the network layer. The lower the sensitivity of the network layer, the larger the optimized sparse ratio of the network layer.
[0143] Table 1
[0144] (4) Based on the test sample set, determine the accuracy of the optimized network model.
[0145] The process of determining the accuracy of the optimized network model based on the test sample set is similar to the process of determining the accuracy of the initial sparse network model based on the test sample set in the above step 502. Therefore, the relevant content of the above step 502 can be referred to and will not be repeated here.
[0146] (5) When the accuracy of the optimized network model reaches the target accuracy, the optimized network model is determined as a deep learning network model after structured sparsification.
[0147] When the accuracy of the optimized network model reaches the target accuracy, it indicates that the optimized network model can meet the user's usage requirements. Therefore, the optimized network model is directly determined as the deep learning network model after structured sparseness.
[0148] It should be noted that, according to the target sparsification method and target sparsification ratio, initial structured sparsification is first performed on each of the multiple network layers to obtain an initial sparse network model. The sparsification ratios of each of the multiple network layers included in the initial sparse network model are the same, namely, the target sparsification ratio. To further improve the accuracy of the initial sparse network model, the sparsification ratio of each network layer in the initial sparse network model is optimized to obtain an optimized network model. Therefore, while ensuring that the average of the optimized sparsification ratios of the multiple network layers is the same as the target sparsification ratio, the optimized network model may contain network layers with an optimized sparsification ratio of 0%. In other words, the optimized network model may contain network layers that do not require structured sparsification. If the accuracy of the optimized network model meets the target accuracy, the optimized network model is directly determined as the deep learning network model after structured sparsification. In other words, only some of the multiple network layers to be sparsified, included in the deep learning network model, need to be structured sparsified to obtain the deep learning network model after structured sparsification.
[0149] Of course, in practical applications, the accuracy of the optimized network model may not reach the target accuracy. In this case, an element scaling operator is added to each network layer in the optimized network model. The element scaling operator is used to scale the elements in the output matrix of the target operator in the network layer. Based on the initial scaling range and multiple granularities, the scaling factor of the element scaling operator in each network layer in the optimized network model is optimized to obtain a deep learning network model after structured sparseness.
[0150] Specifically, if the accuracy of the initial sparse network model fails to meet user requirements, the sparsity ratio of each network layer in the initial sparse network model is optimized to obtain an optimized network model. If the accuracy of the optimized network model still fails to meet user requirements, the scaling factor of the element scaling operator of each network layer in the optimized network model is optimized to further improve the accuracy of the optimized network model, thereby obtaining a structured sparse deep learning network model.
[0151] Among them, the process of optimizing the scaling ratio of the element scaling operator of each network layer in the optimized network model is similar to the process of optimizing the scaling ratio of the element scaling operator of each network layer in the initial sparse network model below. Therefore, you can refer to the relevant content below and will not repeat it here.
[0152] It should be noted that when the accuracy of the initial sparse network model cannot meet the user's usage requirements, further improving the accuracy of the initial sparse network model by optimizing the sparse ratio of each network layer alone, or by combining the optimization of the sparse ratio of each network layer and the optimization of the scaling factor of the element scaling operator of each network layer is only an example. Moreover, the above method for improving model accuracy is only applicable when the target sparsity method is the overall sparse method. When the target sparsity method is the partitioned sparse method, the target sparsity ratio of each network layer is fixed to N:R, and the optimized sparsity ratio of each network layer cannot be adaptively determined. Therefore, in other embodiments, a model accuracy optimization method that is applicable to both the overall sparse method and the partitioned sparse method is proposed. That is, an element scaling operator is added to each network layer in the initial sparse network model, and the element scaling operator is used to scale the elements in the output matrix of the target operator of the network layer. Based on the initial ratio range and multiple granularities, the scaling factor of the element scaling operator of each network layer in the initial sparse network model is optimized to obtain a deep learning network model after structured sparseness.
[0153] That is to say, when the target sparsity mode is the overall sparsity mode, the optimized network model is obtained by optimizing the sparsity ratio of each network layer alone. Alternatively, the optimized network model is obtained by optimizing the scaling factor of the element scaling operator of each network layer alone. Alternatively, the optimized network model is obtained by optimizing the sparsity ratio of each network layer and optimizing the scaling factor of the element scaling operator of each network layer. When the target sparsity mode is the partitioned sparsity mode, the optimized network model can only be obtained by optimizing the scaling factor of the element scaling operator of each network layer.
[0154] The sum of all elements in the same row of the target operator's output matrix is constant at 1. After the target sparse operator prunes the elements of the target operator's input matrix, the elements in the target operator's input matrix are significantly enlarged compared to before pruning. Therefore, when the pruned input matrix is input into the target operator to obtain the target operator's output matrix, some elements with relatively small values are inevitably omitted from the target operator's output matrix, affecting the accuracy of the initial sparse network model. To mitigate this negative effect of pruning the input matrix of the target operator, an element scaling operator is added to each network layer and its scaling factor is optimized to further improve the accuracy of the initial sparse network model.
[0155] For example, according to the following steps (1)-(3), the scaling factor of the element scaling operator of each network layer in the initial sparse network model is optimized by a multi-granularity parameter search method.
[0156] (1) Let i = 1, take the initial magnification range as the i-th magnification range, select the i-th granularity from multiple granularities in descending order, and search for the reference scaling factor corresponding to each network layer within the i-th scaling range according to the i-th granularity. The reference scaling factor is the scaling factor when the accuracy of the corresponding network layer is the maximum.
[0157] One granularity corresponds to a magnification range, and each granularity is selected in order from large to small. For any granularity among the multiple granularities, starting from the lower limit of the magnification range corresponding to the granularity, multiple zoom magnifications are determined according to the size of the granularity, and the difference between two adjacent zoom magnifications in the multiple zoom magnifications is equal to the granularity. Then, starting from the first network layer among the multiple network layers, the reference zoom magnification of the first network layer within the magnification range corresponding to the granularity is determined. The reference zoom magnification corresponding to the first network layer is fixed, and the reference zoom magnification of the second network layer within the magnification range corresponding to the granularity is determined. The above steps are repeated until the reference zoom magnification of each network layer within the magnification range corresponding to the granularity is determined. That is, within the same magnification range corresponding to the same granularity, the reference zoom magnification corresponding to each network layer is determined in sequence according to the order of the multiple network layers.
[0158] The implementation process of determining the reference scaling factor of the first network layer within the scaling factor range corresponding to the granularity includes: maintaining the scaling factors corresponding to other network layers unchanged, calculating the accuracy of the initial sparse network model of the first network layer at the multiple scaling factors according to the relevant algorithm, to obtain multiple accuracies, wherein the multiple accuracies correspond one-to-one to the multiple scaling factors. The scaling factor with the highest accuracy among the multiple scaling factors is determined as the reference scaling factor of the first network layer within the scaling factor range corresponding to the granularity.
[0159] Assume that the first granularity is 0.3 and the first magnification range corresponding to the first granularity is [0.1, positive infinity], that is, the initial magnification range is [0.1, positive infinity]. Starting from the lower limit of the first magnification range, 0.1, and according to the first granularity of 0.3, multiple magnifications such as 0.1, 0.4, 0.7, 1.00, and 1.30 are determined. The accuracy of the initial sparse network model is calculated for each network layer at different magnifications of 0.1, 0.4, 0.7, 1.00, and 1.30. For each network layer, the magnification with the highest accuracy among these multiple magnifications is determined as the reference magnification for each network layer within the first magnification range.
[0160] Among them, the lower limit and upper limit of the initial magnification range are set in advance, for example, the lower limit of the initial magnification range is 0.1, and the upper limit of the initial magnification range is positive infinity. Moreover, the lower limit and upper limit of the initial magnification range can also be adjusted according to different needs. It should be noted that when the upper limit of the initial magnification range is positive infinity, the reference scaling factor of each network layer within the initial magnification range is the scaling factor with the largest accuracy. When the upper limit of the initial magnification range is not positive infinity, the reference scaling factor of each network layer within the initial magnification range is the scaling factor with the largest accuracy in the entire initial magnification range.
[0161] (2) When i is equal to the total number of the multiple granularities, the scaling factor of the element scaling operator of each network layer is set to the network model of the reference scaling factor, and the network model is determined as the deep learning network model after structured sparsification.
[0162] (3) When i is less than the total number of the multiple granularities, let i=i+1, and determine the i-1th magnification range corresponding to each network layer based on the i-1th granularity and the i-th granularity in order from large to small among the multiple granularities, and the reference scaling factor corresponding to each network layer, and return to the step of searching for the reference scaling factor corresponding to each network layer from the i-th scaling factor range according to the i-th granularity.
[0163] The embodiment of the present application optimizes the scaling factor of the element scaling operator of each network layer in a cyclic iteration to obtain an optimized network model. When i is equal to the total number of the multiple granularities, it indicates that the current optimized network model is reliable, then the iterative process is stopped, and the current optimized network model is determined as a deep learning network model after structured sparsification. Since the embodiment of the present application determines the reference scaling factor corresponding to each network layer in sequence according to the order of the multiple network layers. After determining the reference scaling factor corresponding to each network layer, the scaling factor of the element scaling operator of each network layer is replaced with the corresponding reference scaling factor in real time. Therefore, when i is equal to the total number of the multiple granularities, the current optimized network model obtained by the reference scaling factor corresponding to the last network layer in the multiple network layers is the deep learning network model after structured sparsification, and there is no need to uniformly replace the scaling factor of the element scaling operator of each network layer with the corresponding reference scaling factor. When i is less than the total number of the multiple granularities, it indicates that the current optimized network model is unreliable, then the network model processed by the i-th iteration is used as the network model processed by the i+1-th iteration, and the i+1-th iteration is continued.
[0164] The total number of the plurality of granularities is pre-set, for example, the total number of the plurality of granularities is 3. Moreover, the total number of the plurality of granularities can also be adjusted according to different requirements, which is not limited in the embodiment of the present application.
[0165] Since the process of determining the i-th multiplier range corresponding to each network layer is the same, one of the multiple network layers is used as the target network layer. Next, taking the target network layer as an example, the process of determining the i-th multiplier range corresponding to the target network layer is introduced.
[0166] In some embodiments, the difference between the i-1th granularity and the i-th granularity is determined. The reference scaling factor corresponding to the target network layer is subtracted by the difference to obtain the lower limit of the i-th scaling factor range corresponding to the target network layer, and the reference scaling factor corresponding to the target network layer is increased by the difference to obtain the upper limit of the i-th scaling factor range corresponding to the target network layer. In this way, the i-th scaling factor range can compensate for the single-peak blind spot of the i-1th scaling factor range, thereby more accurately determining the reference scaling factor corresponding to each network layer by combining the i-1th scaling factor range and the i-th scaling factor range.
[0167] Of course, in practical applications, the i-th multiplier range corresponding to the target network layer can also be determined in other ways, and the embodiments of the present application do not limit this. For example, the lower limit of the i-th multiplier range corresponding to the target network layer is the difference between the i-th granularity and the first value, and the upper limit of the i-th multiplier range corresponding to the target network layer is the sum of the i-th granularity and the second value. The first value and the second value are pre-set, and the first value and the second value can be the same or different.
[0168] Assume that the multiple granularities include three granularities, from large to small: 0.3, 0.05, and 0.01, and the initial magnification range is [0.1, positive infinity]. Taking the first network layer as an example, based on the first granularity of 0.3, the accuracy of the initial sparse network model of the first network layer at various magnifications is determined within the first magnification range [0.1, positive infinity]. Assume that the accuracy of the initial sparse network model is maximized when the magnification of the first network layer is 1.30, then the reference magnification of the first network layer within the first magnification range is 1.30.
[0169] The difference between the second granularity of 0.05 and the first granularity of 0.3 is 0.25. Since the reference scaling factor of the first network layer within the first scaling factor range is 1.30, the lower limit of the second scaling factor range corresponding to the first network layer is 1.30-0.25, and the upper limit of the second scaling factor range is 1.30+0.25. In other words, the second scaling factor range corresponding to the first network layer is [1.05, 1.55]. Then, the accuracy of the initial sparse network model of the first network layer at each scaling factor within the second scaling factor range of [1.05, 1.55] is determined based on the second granularity of 0.05. Assuming that the accuracy of the initial sparse network model is maximized when the scaling factor of the first network layer is 1.35, the reference scaling factor of the first network layer within the second scaling factor range is 1.35.
[0170] The difference between the third granularity of 0.01 and the second granularity of 0.05 is 0.04. Since the reference scaling factor of the first network layer within the second scaling factor range is 1.35, the lower limit of the third scaling factor range corresponding to the first network layer is 1.35-0.04, and the upper limit of the third scaling factor range is 1.35+0.04. In other words, the third scaling factor range corresponding to the first network layer is [1.31, 1.39]. Then, the accuracy of the initial sparse network model of the first network layer at each scaling factor within the third scaling factor range [1.31, 1.39] is determined based on the third granularity of 0.01. Assuming that the accuracy of the initial sparse network model is maximized at a scaling factor of 1.37 for the first network layer, the reference scaling factor of the first network layer within the third scaling factor range is 1.37.
[0171] For example, based on the initial magnification range and multiple granularities, the scaling factor of the element scaling operator of each network layer is optimized, and the relevant information of the deep learning network model after structured sparseness is shown in Tables 2 to 4. As can be seen from Table 2, the reference scaling factors corresponding to the six network layers in the first magnification range are 1.30, 1.00, 0.40, 0.70, 0.70, and 1.00, respectively. As can be seen from Table 3, the reference scaling factors corresponding to the six network layers in the second magnification range are 1.35, 0.85, 0.45, 0.75, 0.55, and 0.90, respectively. As can be seen from Table 4, the reference scaling factors corresponding to the six network layers in the third magnification range are 1.37, 0.83, 0.42, 0.76, 0.55, and 0.88, respectively. In this way, the reference scaling factors corresponding to the six network layers within the third scaling range are determined as the scaling factors of the corresponding network layers in the optimized network model to obtain a deep learning network model after structured sparsification.
[0172] Table 2
[0173] Table 3
[0174] Table 4
[0175] It should be noted that the structured sparse network model methods provided in the embodiments of this application have achieved excellent results for deep learning network models applied in different fields. For example, the experimental results obtained by performing initial structured sparse on deep learning network models using the overall sparse method are shown in Table 5, and the experimental results obtained by performing initial structured sparse on deep learning network models using the partitioned sparse method are shown in Table 6.
[0176] Tables 5 and 6 both include two different types of deep learning network models: image classification models and question answering models. The image classification models include ViT1, ViT2, Swin1, and Swin2, while the question answering models include BERT1 and BERT2.
[0177] Taking ViT1 as an example, Table 5 shows that by performing initial structured sparsification on ViT1 at an overall sparsity ratio of 70%, the resulting initial sparse network model achieves an accuracy of 72.60%, a decrease of 1.26% compared to the target accuracy of 73.86%. By optimizing the sparsity ratio of each network layer in the initial sparse network model, the resulting optimized network model achieves an accuracy of 72.70%. By optimizing the scaling factor of the element scaling operator of each network layer in the initial sparse network model, the resulting optimized network model achieves an accuracy of 73.68%. By optimizing the sparsity ratio of each network layer and the scaling factor of the element scaling operator of each network layer, the resulting optimized network model achieves an accuracy of 73.74%, a decrease of only 0.120% compared to the target accuracy of 73.86%.
[0178] Taking ViT1 as an example, Table 6 shows that initial structured sparsification of ViT1 with a partition sparsity ratio of 2:4 results in an initial sparse network model with an accuracy of 73.44%, a decrease of 0.42% compared to the target accuracy of 73.86%. By optimizing the scaling factor of the element scaling operator of each network layer in the initial sparse network model, the optimized network model achieves an accuracy of 74.01%, an improvement of 0.150% compared to the target accuracy of 73.86%.
[0179] Table 5
[0180] Table 6
[0181] In an embodiment of the present application, by performing initial structured sparseness on each of the multiple network layers to be sparsed included in a deep learning network model, the number of elements in the input matrix of the target operator of each network layer is reduced, thereby solving the problem of the target operator of each network layer taking a long time to infer. Since the inference cost of the deep learning network model is related to the inference cost of each network layer, by reducing the time consumption of the target operator of each network layer, the computational overhead of the deep learning network model is further reduced.
[0182] In addition, when the accuracy of the initial sparse network model does not reach the target accuracy, if the target sparsity method is the overall sparse method, the accuracy of the initial sparse network model can be further improved by optimizing the sparsity ratio of each network layer and / or by optimizing the scaling factor of the element scaling operator of each network layer. If the target sparsity method is the partitioned sparsity method, the accuracy of the initial sparse network model can be further improved by optimizing the scaling factor of the element scaling operator of each network layer. That is, for different target sparsity methods, the corresponding model accuracy compensation method is used to reduce the computational overhead while ensuring the inference accuracy of the model.
[0183] It should be noted that the above content is based on the example of a computer device performing structured sparseness on a deep learning network model in real time through steps 501-503 to obtain a deep learning network model after structured sparseness. The computer device is a terminal or a server, that is, the terminal and the server do not interact, and the terminal or the server performs structured sparseness on the deep learning network model alone. Of course, in actual applications, the deep learning network model can also be structured sparsely through the interaction between the terminal and the server to obtain a deep learning network model after structured sparseness. For example, please refer to Figure 6, which is a flow chart of another structured sparse method for a network model provided in an embodiment of the present application, and the method comprises the following steps.
[0184] Step 601: The terminal sends the target sparsity mode, target sparsity ratio, deep learning network model, test sample set and target accuracy to the server.
[0185] Step 602: The server receives the target sparsification method, target sparsification ratio, deep learning network model, test sample set, and target accuracy from the terminal. Initial structured sparsification is performed on each of the multiple network layers to be sparsified in the deep learning network model according to the target sparsification method and target sparsification ratio to obtain an initial sparse network model.
[0186] Among them, the number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model.
[0187] Since the process of performing initial structured sparseness on each of the multiple network layers is the same, any one of the multiple network layers is used as the target network layer. The process of performing initial structured sparseness on the target network layer is described below using the target network layer as an example. The process of performing initial structured sparseness on the other network layers in the multiple network layers can refer to the process of performing initial structured sparseness on the target network layer.
[0188] In some embodiments, the target network layer is initially structured sparsified according to the following steps (1)-(2).
[0189] (1) A target sparse operator is added to the target network layer according to the target sparse mode and target sparse ratio. The target sparse operator is used to prune the elements of the input matrix of the target operator of the target network layer.
[0190] In other words, the target network layer is initially structured and sparsely populated using the target sparse operator, reducing the number of elements in the input matrix of the target operator of the target network layer. This solves the problem of the target operator inference taking a long time, thereby reducing the computational overhead of the deep learning network model.
[0191] Target sparsity methods include global sparsity and partitioned sparsity. In different cases, the target sparsity operator is added to the target network layer in different ways according to the target sparsity method and target sparsity ratio. Therefore, the following two cases are divided into the following sections for explanation.
[0192] In the first case, the target sparsity mode is the overall sparsity mode, and the target sparsity ratio is M%, where M is greater than 0 and less than 100. In this case, a first sparsity operator is added to the target network layer according to the target sparsity mode and the target sparsity ratio. The first sparsity operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top 1-M% of the elements with the largest values in the target dimension.
[0193] In the second case, the target sparsity mode is the partitioned sparsity mode, the target sparsity ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R. In this case, a second sparse operator is added to the target network layer according to the target sparsity mode and the target sparsity ratio. The second sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top N elements with the largest values in each R elements of the target dimension.
[0194] (2) Adding a matrix processing operator to the target network layer, the matrix processing operator is used to process the mapping matrix of the target network layer according to the pruning result of the target sparse operator, so that the dimension of the output matrix of the target network layer is the same before and after the initial structured sparse operation of the target network layer.
[0195] The pruning result of the target sparse operator includes a position matrix. Based on the position matrix, the matrix processing operator determines the value element corresponding to each row element in the normalized matrix from the mapping matrix of the target network layer according to the relevant algorithm. The position matrix indicates the position of each element in the feature matrix after pruning in the feature matrix before pruning. The mapping matrix of the target network layer is obtained by mapping the data to be inferred by the target network layer according to the relevant algorithm. The normalized matrix is obtained by processing the pruned feature matrix by the target operator.
[0196] That is, the target network layer is initially structured and sparsified through the matrix processing operator to ensure the isomorphism between the output matrices before and after the initial structured sparsification of the target network layer, so that the target network layer with the initial structured sparsification can still be applied to the original network structure.
[0197] Step 603: The server determines the accuracy of the initial sparse network model based on the test sample set.
[0198] The test sample set includes multiple groups of sample data, each of which includes test samples and sample annotation results. The test samples are input into the initial sparse network model to obtain sample test results output by the initial sparse network model. Based on the sample test results and sample annotation results, the accuracy of the initial sparse network model is determined according to the relevant algorithm.
[0199] Step 604: When the accuracy of the initial sparse network model reaches the target accuracy, the server determines the initial sparse network model as the deep learning network model after structured sparseness, and sends the deep learning network model after structured sparseness to the terminal.
[0200] It should be noted that the content in the embodiment of the present application is similar to the content in the embodiment shown in Figure 5 above. For specific implementation details, please refer to the content in the embodiment shown in Figure 5 above.
[0201] In an embodiment of the present application, by performing initial structured sparseness on each of the multiple network layers to be sparsed included in a deep learning network model, the number of elements in the input matrix of the target operator of each network layer is reduced, thereby solving the problem of the target operator of each network layer taking a long time to infer. Since the inference cost of the deep learning network model is related to the inference cost of each network layer, by reducing the time consumption of the target operator of each network layer, the computational overhead of the deep learning network model is further reduced.
[0202] FIG7 is a flow chart of an image classification method provided in an embodiment of the present application. Referring to FIG7 , the method includes the following steps.
[0203] Step 701: Input the target image to be classified into an image classification model, where the image classification model includes multiple sparse network layers.
[0204] The image classification model is obtained by performing structured sparseness on the deep learning network model in real time by a computer device. Alternatively, the image classification model is obtained directly by the terminal from the server. In some embodiments, the terminal sends the target sparsity method, target sparsity ratio, deep learning network model, test sample set and target accuracy to the server. The server receives the target sparsity method, target sparsity ratio, deep learning network model, test sample set and target accuracy sent by the terminal, and performs a structured sparse method on the deep learning network model according to the relevant algorithm to obtain a deep learning network model after structured sparseness. Then, the deep learning network model after structured sparseness is sent to the terminal as the image classification model.
[0205] In other embodiments, the server stores a deep learning network model before structured sparsification and a deep learning network model after structured sparsification, both of which are applicable to image classification task scenarios. The terminal sends a model request parameter to the server, and the model request parameter is used to indicate the terminal's ability to infer data. The server receives the model request parameter sent by the terminal, determines an image classification model based on the model request parameter, and sends the image classification model to the terminal.
[0206] If the model request parameters indicate that the terminal has strong data reasoning capabilities, the server sends the deep learning network model before structured sparsification as the image classification model to the terminal. If the model request parameters indicate that the terminal has weak data reasoning capabilities, the server sends the deep learning network model after structured sparsification as the image classification model to the terminal.
[0207] Of course, in actual applications, the terminal can also obtain the image classification model from the server in other ways, and the embodiments of the present application do not limit this.
[0208] After the target image is input into the image classification model, the processing process of the target image in multiple sparse networks in the image classification model is similar. The following is an example of the target network layer.
[0209] Step 702: Determine a feature matrix based on a mapping matrix of a target network layer, where the target network layer is any one of the multiple network layers, and the mapping matrix of the target network layer is determined based on a target image.
[0210] After receiving the target image to be classified, the target network layer maps the target image according to the relevant algorithm to obtain a mapping matrix. The mapping matrix includes elements of multiple dimensions. Based on the mapping matrix, the feature matrix is determined.
[0211] Optionally, when the target network layer includes a module that uses a self-attention mechanism, the target network layer maps the target image into three mapping matrices: a query matrix, a key matrix, and a value matrix. The query matrix, the key matrix, and the value matrix each include elements of multiple dimensions, and the query matrix, the key matrix, and the value matrix have the same dimensions. The query matrix is used to indicate each pixel in the target image, the key matrix is used to indicate the correlation between each pixel, and the value matrix is used to indicate the candidate classification result corresponding to each pixel.
[0212] As an example, based on the query matrix and key matrix of the target network layer, the feature matrix is determined according to the following formula (4): P = Q × K T (4)
[0213] In the above formula (4), P represents the feature matrix, Q represents the query matrix, and K T Represents the transpose of the bond matrix.
[0214] Step 703: prune the elements in the feature matrix to obtain a pruned feature matrix and a position matrix, where the position matrix indicates the position of each element in the pruned feature matrix in the feature matrix before pruning.
[0215] The target network layer includes a target sparse operator having a target sparse mode and a target sparse ratio. According to the target sparse mode and the target sparse ratio, the target sparse operator is used to prune elements in the feature matrix to obtain a pruned feature matrix and a position matrix.
[0216] As an example, the elements in the feature matrix are pruned according to the following formula (5) by using the target sparse operator. P`, U = sparse(Q × K T ,dim=-1) (5)
[0217] In the above formula (5), P' represents the feature matrix after pruning, U represents the position matrix, sparse represents the target sparse operator, and dim represents the target dimension of the feature matrix for element pruning.
[0218] For example, dim = -1 represents pruning the elements of the feature matrix row by row, i.e., the target dimension is the matrix row dimension. dim = +1 represents pruning the elements of the feature matrix column by column, i.e., the target dimension is the matrix column dimension. Of course, in practical applications, the target dimension for pruning elements of the feature matrix can also be expressed in other ways, and this embodiment of the application is not limited to this.
[0219] Based on the description above, target sparsity methods include global sparsity and partition sparsity. In each case, the target sparsity operator prunes elements in the feature matrix in different ways, depending on the target sparsity method and target sparsity ratio. Therefore, the following two cases are explained separately.
[0220] In the first case, the target sparsity mode is the global sparsity mode, the target sparsity ratio is M%, M is greater than 0 and less than 100, and the target sparsity operator is the first sparsity operator. In this case, the first sparsity operator is used to retain the top 1-M% of the elements with the largest values in the target dimension in the feature matrix, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0221] That is, when the target sparsity mode is the overall sparsity mode, the values of the elements in the target dimension in the feature matrix are directly compared, so as to retain the top 1-M% elements with the largest values to obtain the pruned feature matrix.
[0222] For example, please refer to Figure 8, which is a schematic diagram of pruning elements in a feature matrix provided by an embodiment of the present application. In Figure 8, the query matrix and the key matrix are both 4*3 matrices, and the feature matrix determined based on the query matrix and the key matrix is a 4*4 matrix. That is, the feature matrix includes 4 rows of elements, each row includes 4 elements, and the position numbers corresponding to the 4 elements in the same row are 0, 1, 2, and 3 respectively. When the target dimension is the matrix row dimension and the target sparsity ratio is 50%, since the values of the two elements with position numbers 1 and 2 in the 4 elements included in the first row are the largest, the values of the two elements with position numbers 0 and 3 in the 4 elements included in the second row are the largest, the values of the two elements with position numbers 0 and 2 in the 4 elements included in the third row are the largest, and the values of the two elements with position numbers 1 and 3 in the 4 elements included in the fourth row are the largest. Therefore, the first two elements with the largest values in the 4 elements in the same row of the feature matrix are retained, and the pruned feature matrix is shown in Figure 8. The pruned feature matrix consists of 4 rows of elements, each row consisting of 2 elements. At the same time, the position of each retained element is determined to obtain a position matrix.
[0223] In the second case, the target sparsity mode is partitioned sparsity mode, the target sparsity ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R. The target sparsity operator is the second sparsity operator. In this case, the second sparsity operator is used to retain the first N elements with the largest values among every R elements in the feature matrix located in the target dimension, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0224] That is, when the target sparsity mode is partitioned sparsity, each element in the feature matrix located in the target dimension is first divided into multiple regions, each of which includes R adjacent elements. Then, for each of the multiple regions, the values of the R elements included in the region are compared, and the top N elements with the largest values in the region are retained to obtain the pruned feature matrix.
[0225] For example, please refer to Figure 9, which is a schematic diagram of another method for pruning elements in a feature matrix provided by an embodiment of the present application. In Figure 9, the query matrix and the key matrix are both 8*3 matrices, and the feature matrix determined based on the query matrix and the key matrix is an 8*8 matrix. That is, the feature matrix includes 8 rows of elements, each row includes 8 elements, and the position numbers corresponding to the 8 elements in the same row are 0, 1, 2, 3, 4, 5, 6 and 7 respectively. When the target dimension is the matrix row dimension and the target sparsity ratio is 2:4, the 8 elements in the same row in the feature matrix are first divided into 2 regions, region 1 and region 2, and each row in region 1 and region 2 includes 4 adjacent elements. Then, the first 2 elements with the largest values among the 4 elements in the same row of region 1 and the first 2 elements with the largest values among the 4 elements in the same row of region 2 are retained respectively to obtain the pruned feature matrix. The pruned feature matrix includes 8 rows of elements, and each row includes 4 elements. At the same time, the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0226] Step 704: Input the pruned feature matrix into the target operator of the target network layer to obtain a normalized matrix.
[0227] When the target network layer includes a module using the self-attention mechanism, the target operator is the softmax operator. In this case, the normalized matrix is obtained according to the following formula (6). S = softmax(P`) (6)
[0228] In the above formula (6), S represents the normalized matrix, softmax represents the softmax operator, and P' represents the feature matrix after pruning.
[0229] Optionally, the target network layer further includes an element scaling operator. After the pruned feature matrix is input into the target operator of the target network layer to obtain a normalized matrix, the elements in the normalized matrix can be scaled by the element scaling operator.
[0230] As an example, the elements in the normalized matrix are scaled according to the following formula (7): S' = β × softmax(P') (7)
[0231] In the above formula (7), S' represents the scaled normalized matrix, and β represents the element scaling operator.
[0232] Step 705: Determine the output matrix of the target network layer based on the position matrix and the normalized matrix.
[0233] In some embodiments, when the target network layer includes a module that adopts a self-attention mechanism and the target operator is a softmax operator, the output matrix of the target network layer is determined according to the following steps (1)-(2).
[0234] (1) Based on the position matrix, determine the value element corresponding to each row element in the normalized matrix from the value matrix of the target network layer.
[0235] Among them, the value matrix of the target network layer is obtained by mapping the target image by the target network layer according to the relevant algorithm.
[0236] Based on the above description, the normalized matrix is obtained by inputting the pruned feature matrix into the target operator of the target network layer, so in fact, the number of elements in the target dimension in the normalized matrix is also reduced. In order to ensure that the dimension of the output matrix of the target network layer is the same as the dimension of the query matrix and the key matrix, it is also necessary to determine the value elements corresponding to each row element in the normalized matrix. For example, the value elements corresponding to each row element in the normalized matrix are determined from the value matrix of the target network layer based on the position matrix through the bit field extraction method. For each row element in the normalized matrix, according to the position indicated by the elements in the same row in the position matrix, the elements at the corresponding position are selected from the value matrix of the target network layer to obtain the value elements corresponding to each row element in the normalized matrix.
[0237] Assume that the elements in the feature matrix are pruned to obtain the pruned feature matrix and position matrix as shown in Figure 8 above. Since the pruned feature matrix includes 4 rows of elements, each row includes 2 elements. Then, the pruned feature matrix is input into the target operator of the target network layer, and the resulting normalized matrix also includes 4 rows of elements, each row includes 2 elements. Taking the first row of elements in the normalized matrix as an example, the elements in the first row of the position matrix are 1 and 2. Therefore, two rows of elements with row numbers 1 and 2 are selected from the value matrix of the target network layer and used as the value elements corresponding to the first row elements in the normalized matrix.
[0238] Of course, in practical applications, it is also possible to determine the value elements corresponding to each row element in the normalized matrix from the value matrix of the target network layer based on the position matrix in other ways, and the embodiments of the present application do not limit this.
[0239] (2) Based on the normalized matrix and the value elements corresponding to each row element in the normalized matrix, determine the output matrix of the target network layer.
[0240] As an example, the output matrix of the target network layer is determined according to the following formula (8).
[0241] In the above formula (8), Z represents the output matrix of the target network layer, S represents the normalized matrix, V' represents the value element corresponding to each row element in the normalized matrix, and Z [a,b] Represents the values of each element in the output matrix of the target network layer.
[0242] Optionally, based on the above description, the target network layer also includes an element scaling operator. After obtaining the normalized matrix, the elements in the normalized matrix can be scaled by the element scaling operator to obtain the scaled normalized matrix. Therefore, the above-mentioned determination of the output matrix of the target network layer directly based on the normalized matrix and the value elements corresponding to each row of elements in the normalized matrix is only an example. In other embodiments, the output matrix of the target network layer can also be determined based on the scaled normalized matrix and the value elements corresponding to each row of elements in the normalized matrix, thereby further improving the accuracy of the output matrix of the target network layer.
[0243] Step 706: Based on the output matrix of the target network layer, determine the classification result output by the image classification model.
[0244] When the target network layer is the last network layer among the multiple network layers, the output matrix of the target network layer is processed according to the relevant algorithm to obtain the classification result output by the image classification model.
[0245] If the target network layer is not the last network layer in the multiple network layers, the output matrix of the target network layer is input to the next network layer adjacent to the target network layer. The next network layer receives the output matrix of the target network layer and serves as the new target network layer, and repeats steps 702-705 until the last network layer in the multiple network layers is reached.
[0246] In an embodiment of the present application, a target image to be classified is input into an image classification model, and the target image to be classified is processed by multiple sparse network layers included in the image classification model. By pruning the elements in the feature matrix through the target network layer, the time spent by the target operator of the target network layer to perform inference to obtain a normalized matrix can be reduced. Since the inference cost of the image classification model is related to the inference cost of the target operator, by reducing the time consumption of the target operator, the computational overhead of the image classification model is further reduced, so that the image classification model can output classification results more quickly.
[0247] FIG10 is a flow chart of a question answering method provided in an embodiment of the present application. Referring to FIG10 , the method includes the following steps.
[0248] Step 1001: Input a target question to be answered into a question answering model, where the question answering model includes multiple sparse network layers.
[0249] The question answering model is obtained by performing structured sparse analysis on the deep learning network model in real time by the computer device. Alternatively, the question answering model is obtained directly from the server by the terminal. The detailed implementation process can be referred to the relevant description of step 701 above and will not be repeated here.
[0250] Step 1002: determining a characteristic matrix based on a mapping matrix of a target network layer, where the target network layer is any one of the multiple network layers, and the mapping matrix of the target network layer is determined based on a target problem.
[0251] After receiving the target question to be answered, the target network layer maps the target question according to the relevant algorithm to obtain a mapping matrix. The mapping matrix includes elements of multiple dimensions. Based on the mapping matrix, the feature matrix is determined.
[0252] Optionally, when the target network layer includes a module that uses a self-attention mechanism, the target network layer maps the target question into three mapping matrices: a query matrix, a key matrix, and a value matrix. The query matrix, the key matrix, and the value matrix each include elements of multiple dimensions, and the dimensions of the query matrix, the key matrix, and the value matrix are the same. The query matrix is used to indicate the various phrases in the target question, the key matrix is used to indicate the correlation between the various phrases, and the value matrix is used to indicate the candidate answers corresponding to the various phrases.
[0253] Step 1003: pruning the elements in the feature matrix to obtain a pruned feature matrix and a position matrix, where the position matrix indicates the position of each element in the pruned feature matrix in the feature matrix before pruning.
[0254] The target network layer includes a target sparse operator having a target sparse mode and a target sparse ratio. According to the target sparse mode and the target sparse ratio, the target sparse operator is used to prune elements in the feature matrix to obtain a pruned feature matrix and a position matrix.
[0255] Based on the description above, target sparsity methods include global sparsity and partition sparsity. In each case, the target sparsity operator prunes elements in the feature matrix in different ways, depending on the target sparsity method and target sparsity ratio. Therefore, the following two cases are explained separately.
[0256] In the first case, the target sparsity mode is the global sparsity mode, the target sparsity ratio is M%, M is greater than 0 and less than 100, and the target sparsity operator is the first sparsity operator. In this case, the first sparsity operator is used to retain the top 1-M% of the elements with the largest values in the target dimension in the feature matrix, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0257] That is, when the target sparsity mode is the overall sparsity mode, the values of the elements in the target dimension in the feature matrix are directly compared, so as to retain the top 1-M% elements with the largest values to obtain the pruned feature matrix.
[0258] In the second case, the target sparsity mode is partitioned sparsity mode, the target sparsity ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R. The target sparsity operator is the second sparsity operator. In this case, the second sparsity operator is used to retain the first N elements with the largest values among every R elements in the feature matrix located in the target dimension, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0259] That is, when the target sparsity mode is partitioned sparsity, each element in the feature matrix located in the target dimension is first divided into multiple regions, each of which includes R adjacent elements. Then, for each of the multiple regions, the values of the R elements included in the region are compared, and the top N elements with the largest values in the region are retained to obtain the pruned feature matrix.
[0260] Step 1004: Input the pruned feature matrix into the target operator of the target network layer to obtain a normalized matrix.
[0261] If the target network layer includes a module using a self-attention mechanism, the target operator is a softmax operator. In this case, the pruned feature matrix is input to the softmax operator of the target network layer to obtain a normalized matrix. The detailed implementation process can be found in the description of step 704 above and will not be repeated here.
[0262] Optionally, the target network layer further includes an element scaling operator. After the pruned feature matrix is input into the target operator of the target network layer to obtain a normalized matrix, the elements in the normalized matrix can be scaled by the element scaling operator.
[0263] Step 1005: Determine the output matrix of the target network layer based on the position matrix and the normalization matrix.
[0264] Based on the position matrix, the value elements corresponding to each row element in the normalized matrix are determined from the value matrix of the target network layer. Based on the normalized matrix and the value elements corresponding to each row element in the normalized matrix, the output matrix of the target network layer is determined. The detailed implementation process can be referred to the relevant description of step 705 above and will not be repeated here.
[0265] Optionally, based on the above description, the target network layer also includes an element scaling operator. After obtaining the normalized matrix, the elements in the normalized matrix can be scaled by the element scaling operator to obtain the scaled normalized matrix. Therefore, the above-mentioned determination of the output matrix of the target network layer directly based on the normalized matrix and the value elements corresponding to each row of elements in the normalized matrix is only an example. In other embodiments, the output matrix of the target network layer can also be determined based on the scaled normalized matrix and the value elements corresponding to each row of elements in the normalized matrix, thereby further improving the accuracy of the output matrix of the target network layer.
[0266] Step 1006: Based on the output matrix of the target network layer, determine the answer output by the question answering model.
[0267] When the target network layer is the last network layer among the multiple network layers, the output matrix of the target network layer is processed according to the relevant algorithm to obtain the answer output by the question answering model.
[0268] If the target network layer is not the last network layer in the multiple network layers, the output matrix of the target network layer is input to the next network layer adjacent to the target network layer. The next network layer receives the output matrix of the target network layer and serves as the new target network layer, and the above steps 1002-1005 are repeatedly performed until the last network layer in the multiple network layers is reached.
[0269] In an embodiment of the present application, the target question to be answered is input into the question answering model, and the target question to be answered is processed by the sparse multiple network layers included in the question answering model. By pruning the elements in the feature matrix through the target network layer, the time spent by the target operator of the target network layer to reason and obtain the normalized matrix can be reduced. Since the reasoning cost of the question answering model is related to the reasoning cost of the target operator, by reducing the time consumption of the target operator, the computational overhead of the question answering model is further reduced, so that the question answering model can output the answer more quickly.
[0270] It should be noted that, in the case where the deep learning network model includes multiple sparse network layers, performing image classification or question answering using the deep learning network model is only an example. In actual applications, the deep learning network model including multiple sparse network layers can also be applied to other task scenarios. The process of performing data reasoning by the deep learning network model in other task scenarios is similar to the process of performing image classification or question answering, and will not be repeated here.
[0271] FIG11 is a schematic diagram of a structured sparse network model apparatus according to an embodiment of the present application. The structured sparse network model apparatus can be implemented as part or all of a computer device using software, hardware, or a combination of both. Referring to FIG11 , the apparatus includes a structured sparse network model module 1101, an accuracy testing module 1102, and a determination module 1103.
[0272] The structured sparse module 1101 is configured to perform initial structured sparse processing on each of the multiple network layers to be sparsed in the deep learning network model according to the target sparse mode and target sparse ratio, thereby obtaining an initial sparse network model. The detailed implementation process is described in the corresponding embodiments above and will not be repeated here.
[0273] Among them, the number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model.
[0274] The accuracy test module 1102 is used to determine the accuracy of the initial sparse network model based on the test sample set. The detailed implementation process is referred to the corresponding content of each embodiment above and will not be repeated here.
[0275] Determination module 1103 is used to determine the initial sparse network model as a deep learning network model after structured sparseness when the accuracy of the initial sparse network model reaches the target accuracy. The detailed implementation process refers to the corresponding content in the above embodiments and will not be repeated here.
[0276] Optionally, the structured sparse module 1101 includes:
[0277] a first adding unit, configured to add a target sparse operator in a target network layer according to a target sparse mode and a target sparse ratio, wherein the target sparse operator is configured to prune elements of an input matrix of a target operator of the target network layer, wherein the target network layer is any one of the multiple network layers;
[0278] The second adding unit is used to add a matrix processing operator to the target network layer, and the matrix processing operator is used to process the mapping matrix of the target network layer according to the pruning result of the target sparse operator so that the dimension of the output matrix of the target network layer is the same before and after the initial structured sparseness of the target network layer.
[0279] Optionally, the target thinning mode is an overall thinning mode, and the target thinning ratio is M%, where M is greater than 0 and less than 100;
[0280] The first adding unit is specifically used for:
[0281] A first sparse operator is added to the target network layer according to the target sparsity mode and the target sparsity ratio. The first sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top 1-M% elements with the largest values in the target dimension.
[0282] Optionally, the target sparse mode is a partitioned sparse mode, and the target sparse ratio is N:R, where N and R are both integers greater than or equal to 1, and N is less than R;
[0283] The first adding unit is specifically used for:
[0284] A second sparse operator is added to the target network layer according to the target sparsity mode and the target sparsity ratio. The second sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top N elements with the largest values in every R elements of the target dimension.
[0285] Optionally, the device further comprises:
[0286] The first sparse parameter optimization module is used to optimize the sparse ratio of each network layer in the initial sparse network model based on the target accuracy, target sparsity ratio and test sample set when the accuracy of the initial sparse network model does not reach the target accuracy, so as to obtain a deep learning network model after structured sparseness.
[0287] Optionally, the first sparse parameter optimization module includes:
[0288] A first determining unit is used to determine the sensitivity of each network layer in the initial sparse network model based on the target accuracy and the test sample set;
[0289] A second determining unit is configured to determine an optimized sparse ratio for each network layer based on the sensitivity of each network layer, the number of the plurality of network layers, the total number of elements in the target dimension, and the target sparse ratio;
[0290] A first optimization unit is configured to optimize the sparse ratio of each network layer in the initial sparse network model based on the optimized sparse ratio of each network layer to obtain an optimized network model;
[0291] A third determining unit is used to determine the accuracy of the optimized network model based on the test sample set;
[0292] The fourth determining unit is used to determine the optimized network model as a deep learning network model after structured sparseness when the accuracy of the optimized network model reaches the target accuracy.
[0293] Optionally, the first determining unit is specifically configured to:
[0294] Determine the accuracy of each network layer in the initial sparse network model based on the test sample set;
[0295] Determine the sensitivity of each network layer based on the target accuracy and the accuracy of each network layer.
[0296] Optionally, the first sparse parameter optimization module further includes:
[0297] An adding unit is used to add an element scaling operator to each network layer in the optimized network model when the accuracy of the optimized network model does not reach the target accuracy. The element scaling operator is used to scale the elements in the output matrix of the target operator of the network layer.
[0298] The second optimization unit is used to optimize the scaling factor of the element scaling operator of each network layer in the optimized network model based on the initial scaling factor range and multiple granularities to obtain a deep learning network model after structured sparseness.
[0299] Optionally, the device further comprises:
[0300] A module is added for adding an element scaling operator to each network layer in the initial sparse network model when the accuracy of the initial sparse network model does not reach the target accuracy. The element scaling operator is used to scale the elements in the output matrix of the target operator of the network layer.
[0301] The second sparse parameter optimization module is used to optimize the scaling factor of the element scaling operator of each network layer in the initial sparse network model based on the initial scaling factor range and multiple granularities to obtain a deep learning network model after structured sparseness.
[0302] Optionally, the second sparse parameter optimization module includes:
[0303] a search unit, configured to set i=1, take the initial magnification range as the i-th magnification range, select the i-th granularity from the multiple granularities in descending order, and search for a reference scaling factor corresponding to each network layer within the i-th scaling range according to the i-th granularity, where the reference scaling factor is the scaling factor when the accuracy of the corresponding network layer is maximized;
[0304] A first determining unit is configured to, when i is equal to the total number of the multiple granularities, determine a network model whose scaling factor of an element scaling operator of each network layer is equal to a reference scaling factor as a deep learning network model after structured sparsification;
[0305] The second determination unit is used to set i=i+1 when i is less than the total number of the multiple granularities, determine the i-th magnification range corresponding to each network layer based on the i-1th granularity and the i-th granularity in order from large to small, and the reference scaling factor corresponding to each network layer, and return to the step of searching for the reference scaling factor corresponding to each network layer from the i-th scaling factor range according to the i-th granularity.
[0306] Optionally, the second determining unit is specifically configured to:
[0307] Determine the difference between the i-1th particle size and the i-th particle size;
[0308] Subtract the difference from the reference scaling factor corresponding to the target network layer to obtain the lower limit of the i-th scaling range corresponding to the target network layer, and add the difference to the reference scaling factor corresponding to the target network layer to obtain the upper limit of the i-th scaling range corresponding to the target network layer. The target network layer is any one of the multiple network layers.
[0309] Optionally, the multiple network layers all include a module that adopts a self-attention mechanism, and the target operator is a softmax operator.
[0310] Optionally, the deep learning network model is applicable to any type of task scenario among semantic segmentation, image classification, object detection, image generation, language modeling, question answering, machine translation, and sentiment analysis.
[0311] Optionally, when the deep learning network model is applicable to an image classification task scenario, the test sample set includes a test image and classification annotation results;
[0312] In the case where the deep learning network model is applicable to the question answering task scenario, the test sample set includes test questions in text format and answer annotation results.
[0313] In an embodiment of the present application, by performing initial structured sparseness on each of the multiple network layers to be sparsed included in a deep learning network model, the number of elements in the input matrix of the target operator of each network layer is reduced, thereby solving the problem of the target operator of each network layer taking a long time to infer. Since the inference cost of the deep learning network model is related to the inference cost of each network layer, by reducing the time consumption of the target operator of each network layer, the computational overhead of the deep learning network model is further reduced.
[0314] In addition, when the accuracy of the initial sparse network model does not reach the target accuracy, if the target sparsity method is the overall sparse method, the accuracy of the initial sparse network model can be further improved by optimizing the sparsity ratio of each network layer and / or by optimizing the scaling factor of the element scaling operator of each network layer. If the target sparsity method is the partitioned sparsity method, the accuracy of the initial sparse network model can be further improved by optimizing the scaling factor of the element scaling operator of each network layer. That is, for different target sparsity methods, the corresponding model accuracy compensation method is used to reduce the computational overhead while ensuring the inference accuracy of the model.
[0315] It should be noted that: the structured sparse device for the network model provided in the above embodiment only uses the division of the above functional modules as an example to illustrate when performing structured sparseness on the network model. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the structured sparse device for the network model provided in the above embodiment and the structured sparse method embodiment of the network model belong to the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0316] Figure 12 is a schematic diagram of the structure of a structured sparse data processing device for another network model provided by an embodiment of the present application. The structured sparse data processing device for this network model can be implemented as part or all of a server using software, hardware, or a combination of both. Referring to Figure 12 , the device includes a receiving module 1201, a structured sparse data processing module 1202, an accuracy testing module 1203, a determination module 1204, and a sending module 1205.
[0317] The receiving module 1201 is used to receive the target sparsity mode, target sparsity ratio, deep learning network model, test sample set and target accuracy sent by the terminal. The detailed implementation process refers to the corresponding content in the above embodiments and will not be repeated here.
[0318] The structured sparse module 1202 is configured to perform initial structured sparse processing on each of the multiple network layers to be sparsed in the deep learning network model according to the target sparse mode and target sparse ratio to obtain an initial sparse network model. The detailed implementation process is referred to the corresponding content in the above embodiments and will not be repeated here.
[0319] Among them, the number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model.
[0320] The accuracy test module 1203 is used to determine the accuracy of the initial sparse network model based on the test sample set. The detailed implementation process is referred to the corresponding content of each embodiment above and will not be repeated here.
[0321] Determination module 1204 is used to determine the initial sparse network model as a deep learning network model after structured sparseness when the accuracy of the initial sparse network model reaches the target accuracy. The detailed implementation process refers to the corresponding content in the above embodiments and will not be repeated here.
[0322] The sending module 1205 is used to send the structured sparse deep learning network model to the terminal. The detailed implementation process refers to the corresponding content in the above embodiments and will not be repeated here.
[0323] In an embodiment of the present application, by performing initial structured sparseness on each of the multiple network layers to be sparsed included in a deep learning network model, the number of elements in the input matrix of the target operator of each network layer is reduced, thereby solving the problem of the target operator of each network layer taking a long time to infer. Since the inference cost of the deep learning network model is related to the inference cost of each network layer, by reducing the time consumption of the target operator of each network layer, the computational overhead of the deep learning network model is further reduced.
[0324] It should be noted that: the structured sparse device for the network model provided in the above embodiment only uses the division of the above functional modules as an example to illustrate when performing structured sparseness on the network model. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the structured sparse device for the network model provided in the above embodiment and the structured sparse method embodiment of the network model belong to the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0325] Figure 13 is a schematic diagram of the structure of an image classification device provided in an embodiment of the present application. The image classification device can be implemented as part or all of a computer device using software, hardware, or a combination of both. Referring to Figure 13 , the device includes: a first input module 1301, a first determination module 1302, a pruning module 1303, a second input module 1304, a second determination module 1305, and a third determination module 1306.
[0326] The first input module 1301 is used to input the target image to be classified into the image classification model, which includes multiple sparse network layers. The detailed implementation process refers to the corresponding content in the above embodiments and will not be repeated here.
[0327] The first determining module 1302 is configured to determine a characteristic matrix based on a mapping matrix of a target network layer, where the target network layer is any one of the multiple network layers. Detailed implementation procedures are described in detail in the above embodiments and will not be repeated here.
[0328] Pruning module 1303 is configured to prune elements from the feature matrix to obtain a pruned feature matrix and a position matrix. The position matrix indicates the position of each element in the pruned feature matrix in the feature matrix before pruning. The detailed implementation process is described in the above embodiments and will not be repeated here.
[0329] The second input module 1304 is used to input the pruned feature matrix into the target operator of the target network layer to obtain a normalized matrix. The detailed implementation process is referred to the corresponding content of each embodiment above and will not be repeated here.
[0330] The second determining module 1305 is used to determine the output matrix of the target network layer based on the position matrix and the normalized matrix. The detailed implementation process is referred to the corresponding content of each embodiment above and will not be repeated here.
[0331] The third determining module 1306 is used to determine the classification result output by the image classification model based on the output matrix of the target network layer. The detailed implementation process is referred to the corresponding content of each embodiment above and will not be repeated here.
[0332] Optionally, the target network layer includes a target sparse operator, and the target sparse operator has a target sparse mode and a target sparse ratio;
[0333] The pruning module 1303 is specifically used for:
[0334] According to the target sparsity mode and the target sparsity ratio, the elements in the feature matrix are pruned by the target sparsity operator to obtain the pruned feature matrix and position matrix.
[0335] Optionally, the target sparse mode is an overall sparse mode, the target sparse ratio is M%, M is greater than 0 and less than 100, and the target sparse operator is a first sparse operator;
[0336] The pruning module 1303 is specifically used for:
[0337] The first 1-M% of the elements with the largest values in the target dimension in the feature matrix are retained by the first sparse operator, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0338] Optionally, the target sparse mode is a partitioned sparse mode, the target sparse ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R, and the target sparse operator is a second sparse operator;
[0339] The pruning module 1303 is specifically used for:
[0340] The second sparse operator is used to retain the first N elements with the largest values in each R elements in the target dimension in the feature matrix, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
[0341] Optionally, the target network layer further includes an element scaling operator, and the apparatus further includes:
[0342] A scaling module, used to scale the elements in the normalized matrix using an element scaling operator;
[0343] The second determining module 1305 is specifically configured to:
[0344] Based on the position matrix and the scaled normalized matrix, the output matrix of the target network layer is determined.
[0345] In an embodiment of the present application, a target image to be classified is input into an image classification model, and the target image to be classified is processed by multiple sparse network layers included in the image classification model. By pruning the elements in the feature matrix through the target network layer, the time spent by the target operator of the target network layer to perform inference to obtain a normalized matrix can be reduced. Since the inference cost of the image classification model is related to the inference cost of the target operator, by reducing the time consumption of the target operator, the computational overhead of the image classification model is further reduced, so that the image classification model can output classification results more quickly.
[0346] It should be noted that the image classification device provided in the above embodiment only uses the division of the above functional modules as an example to illustrate image classification. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the image classification device provided in the above embodiment and the image classification method embodiment are based on the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0347] Please refer to Figure 14, which is a schematic diagram of the structure of a computer device according to an embodiment of the present application. The computer device can be the terminal mentioned above or the server mentioned above. The computer device includes at least one processor 1401, a communication bus 1402, a memory 1403, and at least one communication interface 1404.
[0348] Processor 1401 may be a general-purpose central processing unit (CPU), a network processor (NP), a microprocessor, or one or more integrated circuits for implementing the solution of the present application, such as an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. The PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0349] Communication bus 1402 is used to transmit information between the above components. Communication bus 1402 can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used in the figure, but this does not mean that there is only one bus or one type of bus.
[0350] The memory 1403 may be a read-only memory (ROM), a random access memory (RAM), an electrically erasable programmable read-only memory (EEPROM), an optical disc (including a compact disc read-only memory (CD-ROM), a compact disc, a laser disc, a digital versatile disc, a Blu-ray disc, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 1403 may exist independently and be connected to the processor 1401 via the communication bus 1402. The memory 1403 may also be integrated with the processor 1401.
[0351] Communication interface 1404 uses any transceiver or other device for communicating with other devices or communication networks. Communication interface 1404 includes a wired communication interface and may also include a wireless communication interface. The wired communication interface may be, for example, an Ethernet interface. The Ethernet interface may be an optical interface, an electrical interface, or a combination thereof. The wireless communication interface may be a wireless local area network (WLAN) interface, a cellular network communication interface, or a combination thereof.
[0352] In a specific implementation, as an embodiment, the processor 1401 may include one or more CPUs, such as CPU0 and CPU1 shown in FIG14 .
[0353] In a specific implementation, as an embodiment, a computer device may include multiple processors, such as processor 1401 and processor 1405 shown in Figure 14. Each of these processors may be a single-core processor or a multi-core processor. The processor herein may refer to one or more devices, circuits, and / or processing cores for processing data (such as computer program instructions).
[0354] In a specific implementation, as an embodiment, the computer device may further include an output device 1406 and an input device 1407 (not shown). The output device 1406 communicates with the processor 1401 and can display information in a variety of ways. For example, the output device 1406 can be a liquid crystal display (LCD), a light emitting diode (LED) display device, a cathode ray tube (CRT) display device, or a projector. The input device 1407 communicates with the processor 1401 and can receive user input in a variety of ways. For example, the input device 1407 can be a mouse, a keyboard, a touch screen device, or a sensor device.
[0355] In some embodiments, the memory 1403 is used to store program code 1410 for executing the solution of the present application, and the processor 1401 can execute the program code 1410 stored in the memory 1403. The program code 1410 may include one or more software modules, and the computer device can implement the structured sparse method of the network model provided in the embodiment of Figure 5 above through the processor 1401 and the program code 1410 in the memory 1403.
[0356] In the above embodiments, all or part of the embodiments can be implemented by software, hardware, firmware, or any combination thereof. When implemented using software, all or part of the embodiments can be implemented in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that can be accessed by a computer, or a data storage device such as a server or data center that includes one or more available media integrated therein. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, or a magnetic tape), an optical medium (e.g., a digital versatile disc (DVD)), or a semiconductor medium (e.g., a solid state disk (SSD)). It is worth noting that the computer-readable storage medium mentioned in the embodiments of the present application may be a non-volatile storage medium, in other words, a non-transient storage medium.
[0357] That is, an embodiment of the present application also provides a computer-readable storage medium, which stores instructions. When the instructions are run on a computer, the computer executes the steps of the structured sparse method, image classification method or question answering method of the above-mentioned network model.
[0358] The present application also provides a computer program product comprising instructions that, when executed on a computer, cause the computer to execute the steps of the structured sparse method, image classification method, or question answering method for the aforementioned network model. Alternatively, a computer program is provided that, when executed on a computer, causes the computer to execute the steps of the structured sparse method, image classification method, or question answering method for the aforementioned network model.
[0359] It should be understood that the "plurality" mentioned herein refers to two or more. In the description of the embodiments of the present application, unless otherwise specified, " / " means or, for example, A / B can mean A or B; "and / or" in this article is merely a description of the association relationship of associated objects, indicating that there can be three relationships, for example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. In addition, in order to facilitate a clear description of the technical solutions of the embodiments of the present application, in the embodiments of the present application, words such as "first" and "second" are used to distinguish between identical or similar items with substantially the same functions and effects. Those skilled in the art will understand that words such as "first" and "second" do not limit the quantity and execution order, and words such as "first" and "second" do not necessarily limit them to be different.
[0360] It should be noted that the information (including but not limited to user device information, user personal information, etc.), data (including but not limited to data used for analysis, stored data, displayed data, etc.) and signals involved in the embodiments of this application are all authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions. For example, the target sparsity method, target sparsity ratio, deep learning network model, test sample set and target accuracy involved in the embodiments of this application are all obtained with full authorization.
[0361] The above description is an embodiment provided for this application and is not intended to limit this application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of this application should be included in the scope of protection of this application.
Claims
1. A structured sparse method for a network model, characterized in that: The method comprises: According to the target sparsifying mode and the target sparsifying ratio, initial structured sparsification is performed on each network layer of the multiple network layers to be sparsified included in the deep learning network model to obtain an initial sparse network model; The number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model; Determining the accuracy of the initial sparse network model based on the test sample set; When the accuracy of the initial sparse network model reaches the target accuracy, the initial sparse network model is determined as the deep learning network model after structured sparseness.
2. The method according to claim 1, characterized in that The method of performing initial structured sparseness on each of the multiple network layers to be sparsed included in the deep learning network model according to the target sparseness mode and the target sparseness ratio includes: Adding a target sparse operator in a target network layer according to the target sparse mode and the target sparse ratio, wherein the target sparse operator is used to prune elements of an input matrix of a target operator of the target network layer, wherein the target network layer is any one of the multiple network layers; A matrix processing operator is added to the target network layer, and the matrix processing operator is used to process the mapping matrix of the target network layer according to the pruning result of the target sparse operator, so that the dimension of the output matrix of the target network layer is the same before and after the initial structured sparseness is performed on the target network layer.
3. The method according to claim 2, characterized in that The target sparse mode is an overall sparse mode, and the target sparse ratio is M%, where M is greater than 0 and less than 100; The adding a target sparse operator in the target network layer according to the target sparse mode and the target sparse ratio includes: A first sparse operator is added to the target network layer according to the target sparse mode and the target sparse ratio, and the first sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top 1-M% elements with the largest values in the target dimension.
4. The method according to claim 2, characterized in that The target sparse mode is a partition sparse mode, the target sparse ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R; The adding a target sparse operator in the target network layer according to the target sparse mode and the target sparse ratio includes: A second sparse operator is added to the target network layer according to the target sparse mode and the target sparse ratio, and the second sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the first N elements with the largest values in every R elements of the target dimension.
5. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: When the accuracy of the initial sparse network model does not reach the target accuracy, the sparse ratio of each network layer in the initial sparse network model is optimized based on the target accuracy, the target sparse ratio and the test sample set to obtain a deep learning network model after structured sparseness.
6. The method according to claim 5, characterized in that The step of optimizing the sparse ratio of each network layer in the initial sparse network model based on the target accuracy, the target sparse ratio and the test sample set to obtain a structured sparse deep learning network model includes: Determining the sensitivity of each network layer in the initial sparse network model based on the target accuracy and the test sample set; Determining an optimized sparse ratio of each network layer based on the sensitivity of each network layer, the number of the plurality of network layers, the total number of elements of the target dimension, and the target sparse ratio; Based on the optimized sparse ratio of each network layer, the sparse ratio of each network layer in the initial sparse network model is optimized to obtain an optimized network model; Based on the test sample set, determining the accuracy of the optimized network model; When the accuracy of the optimized network model reaches the target accuracy, the optimized network model is determined as a deep learning network model after structured sparseness.
7. The method according to claim 6, characterized in that The step of determining the sensitivity of each network layer in the initial sparse network model based on the target accuracy and the test sample set includes: Based on the test sample set, determining the accuracy of each network layer in the initial sparse network model; Based on the target accuracy and the accuracy of each network layer, a sensitivity of each network layer is determined.
8. The method according to claim 6, characterized in that After determining the accuracy of the optimized network model based on the test sample set, the method further includes: When the accuracy of the optimized network model does not reach the target accuracy, an element scaling operator is added to each network layer in the optimized network model, and the element scaling operator is used to scale the elements in the output matrix of the target operator of the network layer; Based on the initial magnification range and multiple granularities, the scaling magnification of the element scaling operator of each network layer in the optimized network model is optimized to obtain a structured sparse deep learning network model.
9. The method according to any one of claims 1 to 4, characterized in that: The method further comprises: When the accuracy of the initial sparse network model does not reach the target accuracy, an element scaling operator is added to each network layer in the initial sparse network model, and the element scaling operator is used to scale the elements in the output matrix of the target operator of the network layer; Based on the initial magnification range and multiple granularities, the scaling magnification of the element scaling operator of each network layer in the initial sparse network model is optimized to obtain a deep learning network model after structured sparseness.
10. The method according to claim 9, characterized in that The step of optimizing the scaling factor of the element scaling operator of each network layer in the initial sparse network model based on the initial scaling factor range and the multiple granularities to obtain a structured sparse deep learning network model includes: Let i=1, take the initial magnification range as the i-th magnification range, select the i-th granularity from the multiple granularities in descending order, and search for the reference scaling magnification corresponding to each network layer from the i-th magnification range according to the i-th granularity, where the reference scaling magnification is the scaling magnification when the accuracy of the corresponding network layer is the maximum; In the case where i is equal to the total number of the plurality of granularities, a network model whose scaling factor of the element scaling operator of each network layer is equal to the reference scaling factor is determined as a deep learning network model after structured sparsification; When i is less than the total number of the multiple granularities, let i=i+1, determine the i-1th magnification range corresponding to each network layer based on the i-1th granularity and the i-th granularity in order from large to small among the multiple granularities, and the reference scaling factor corresponding to each network layer, and return to the step of searching for the reference scaling factor corresponding to each network layer from the i-th scaling factor range according to the i-th granularity.
11. The method according to claim 10, characterized in that The determining, based on the i-1th granularity and the i-th granularity in descending order among the multiple granularities and the reference scaling factor corresponding to each network layer, the i-th scaling factor range corresponding to each network layer includes: Determine the difference between the i-1th particle size and the i-th particle size; The lower limit of the i-th magnification range corresponding to the target network layer is obtained by subtracting the difference from the reference scaling factor corresponding to the target network layer, and the upper limit of the i-th magnification range corresponding to the target network layer is obtained by adding the difference to the reference scaling factor corresponding to the target network layer. The target network layer is any one of the multiple network layers.
12. The method according to any one of claims 1 to 11, characterized in that: The multiple network layers all include modules that adopt a self-attention mechanism, and the target operator is a normalized exponential softmax operator.
13. The method according to any one of claims 1 to 12, characterized in that: The deep learning network model is suitable for any type of task scenario in semantic segmentation, image classification, object detection, image generation, language modeling, question answering, machine translation, and sentiment analysis.
14. The method according to claim 13, characterized in that In the case where the deep learning network model is applicable to an image classification task scenario, the test sample set includes a test image and a classification annotation result; In the case where the deep learning network model is applicable to a question answering task scenario, the test sample set includes test questions in text format and answer annotation results.
15. A structured sparse method for a network model, characterized in that: Applied to a server, the method comprises: The target sparsity mode, target sparsity ratio, deep learning network model, test sample set and target accuracy sent by the receiving terminal; According to the target sparse mode and the target sparse ratio, initial structured sparse is performed on each network layer of the multiple network layers to be sparse included in the deep learning network model to obtain an initial sparse network model; The number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model; Based on the test sample set, determining the accuracy of the initial sparse network model; When the accuracy of the initial sparse network model reaches the target accuracy, the initial sparse network model is determined as a structured sparse deep learning network model, and the structured sparse deep learning network model is sent to the terminal.
16. An image classification method, characterized in that: The method comprises: Inputting a target image to be classified into an image classification model, wherein the image classification model includes a plurality of sparse network layers; Determining a feature matrix based on a mapping matrix of a target network layer, wherein the target network layer is any one of the multiple network layers, and the mapping matrix is determined based on the target image; Pruning elements in the feature matrix to obtain a pruned feature matrix and a position matrix, wherein the position matrix indicates the position of each element in the pruned feature matrix in the feature matrix before the pruned feature matrix; Inputting the pruned feature matrix into the target operator of the target network layer to obtain a normalized matrix; Determining an output matrix of the target network layer based on the position matrix and the normalized matrix; Based on the output matrix of the target network layer, a classification result output by the image classification model is determined.
17. The method according to claim 16, characterized in that The target network layer includes a target sparse operator, and the target sparse operator has a target sparse mode and a target sparse ratio; The step of pruning the elements in the feature matrix to obtain a pruned feature matrix and a position matrix includes: According to the target sparse mode and the target sparse ratio, the elements in the feature matrix are pruned by the target sparse operator to obtain a pruned feature matrix and a position matrix.
18. The method according to claim 17, characterized in that The target sparse mode is an overall sparse mode, the target sparse ratio is M%, M is greater than 0 and less than 100, and the target sparse operator is a first sparse operator; The step of pruning the elements in the feature matrix according to the target sparse mode and the target sparse ratio by using the target sparse operator to obtain a pruned feature matrix and a position matrix includes: The first 1-M% of the elements with the largest values in the target dimension in the feature matrix are retained by the first sparse operator, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
19. The method according to claim 17, characterized in that The target sparse mode is a partition sparse mode, the target sparse ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R, and the target sparse operator is a second sparse operator; The target sparse mode and the target sparse ratio are used to perform the sparse operation on the elements in the feature matrix. Prune to obtain the pruned feature matrix and position matrix, including: The second sparse operator is used to retain the first N elements with the largest values in each R elements in the target dimension in the feature matrix, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
20. The method according to any one of claims 16 to 19, characterized in that: The target network layer further includes an element scaling operator. After inputting the pruned feature matrix into the target operator of the target network layer to obtain a normalized matrix, the method further includes: Scaling the elements in the normalized matrix by the element scaling operator; The step of determining the output matrix of the target network layer based on the position matrix and the normalized matrix includes: Based on the position matrix and the scaled normalized matrix, an output matrix of the target network layer is determined.
21. A structured sparse device for a network model, characterized in that: The device comprises: A structured sparse module, used for performing initial structured sparseness on each of the multiple network layers to be sparse included in the deep learning network model according to a target sparse mode and a target sparse ratio, so as to obtain an initial sparse network model; The number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model; An accuracy testing module, used to determine the accuracy of the initial sparse network model based on a test sample set; A determination module is used to determine the initial sparse network model as a deep learning network model after structured sparseness when the accuracy of the initial sparse network model reaches the target accuracy.
22. The device according to claim 21, characterized in that The structured sparse module includes: A first adding unit is used to add a target sparse operator in a target network layer according to the target sparse mode and the target sparse ratio, wherein the target sparse operator is used to prune elements of an input matrix of a target operator of the target network layer, and the target network layer is any one of the multiple network layers; The second adding unit is used to add a matrix processing operator to the target network layer, and the matrix processing operator is used to process the mapping matrix of the target network layer according to the pruning result of the target sparse operator, so that the dimension of the output matrix of the target network layer is the same before and after the initial structured sparseness is performed on the target network layer.
23. The device according to claim 22, characterized in that The target sparse mode is an overall sparse mode, and the target sparse ratio is M%, where M is greater than 0 and less than 100; The first adding unit is specifically used for: A first sparse operator is added to the target network layer according to the target sparse mode and the target sparse ratio, and the first sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the top 1-M% elements with the largest values in the target dimension.
24. The device according to claim 22, characterized in that The target sparse mode is a partition sparse mode, the target sparse ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R; The first adding unit is specifically used for: A second sparse operator is added to the target network layer according to the target sparse mode and the target sparse ratio, and the second sparse operator is used to prune the elements of the target dimension in the input matrix of the target operator of the target network layer to retain the first N elements with the largest values in every R elements of the target dimension.
25. The device according to any one of claims 21 to 23, characterized in that: The device also includes: The first sparse parameter optimization module is used to optimize the sparse ratio of each network layer in the initial sparse network model based on the target accuracy, the target sparse ratio and the test sample set when the accuracy of the initial sparse network model does not reach the target accuracy, so as to obtain a deep learning network model after structured sparseness.
26. The device according to claim 25, characterized in that The first sparse parameter optimization module includes: A first determining unit, configured to determine the sensitivity of each network layer in the initial sparse network model based on the target accuracy and the test sample set; A second determining unit, configured to determine an optimized sparse ratio of each network layer based on the sensitivity of each network layer, the number of the plurality of network layers, the total number of elements of the target dimension, and the target sparse ratio; A first optimization unit, configured to optimize the sparse ratio of each network layer in the initial sparse network model based on the optimized sparse ratio of each network layer to obtain an optimized network model; A third determining unit, configured to determine the accuracy of the optimized network model based on the test sample set; The fourth determination unit is used to determine the optimized network model as a deep learning network model after structured sparseness when the accuracy of the optimized network model reaches the target accuracy.
27. The device according to claim 26, characterized in that The first determining unit is specifically configured to: Based on the test sample set, determining the accuracy of each network layer in the initial sparse network model; Based on the target accuracy and the accuracy of each network layer, a sensitivity of each network layer is determined.
28. The device according to claim 26, characterized in that The first sparse parameter optimization module also includes: An adding unit, used for adding an element scaling operator to each network layer in the optimized network model when the accuracy of the optimized network model does not reach the target accuracy, wherein the element scaling operator is used to scale the elements in the output matrix of the target operator of the network layer; The second optimization unit is used to optimize the scaling factor of the element scaling operator of each network layer in the optimized network model based on the initial scaling factor range and multiple granularities to obtain a deep learning network model after structured sparseness.
29. The device according to any one of claims 21 to 24, characterized in that: The device also includes: An adding module, used for adding an element scaling operator to each network layer in the initial sparse network model when the accuracy of the initial sparse network model does not reach the target accuracy, and the element scaling operator is used to scale the elements in the output matrix of the target operator of the network layer; The second sparse parameter optimization module is used to optimize the scaling factor of the element scaling operator of each network layer in the initial sparse network model based on the initial factor range and multiple granularities to obtain a deep learning network model after structured sparseness.
30. The device according to claim 29, characterized in that The second sparse parameter optimization module includes: A search unit, configured to set i=1, take the initial magnification range as the i-th magnification range, select the i-th granularity from the multiple granularities in descending order, and search for a reference scaling magnification corresponding to each network layer from the i-th magnification range according to the i-th granularity, wherein the reference scaling magnification is a scaling magnification when the accuracy of the corresponding network layer is the maximum; A first determining unit is used to determine, when i is equal to the total number of the multiple granularities, a network model whose scaling factor of the element scaling operator of each network layer is a reference scaling factor as a deep learning network model after structured sparseness; The second determination unit is used to, when i is less than the total number of the multiple granularities, set i=i+1, determine the i-th magnification range corresponding to each network layer based on the i-1th granularity and the i-th granularity in order from large to small among the multiple granularities, and the reference scaling factor corresponding to each network layer, and return to the step of searching for the reference scaling factor corresponding to each network layer from the i-th scaling factor range according to the i-th granularity.
31. The device according to claim 30, characterized in that The second determining unit is specifically configured to: Determine the difference between the i-1th particle size and the i-th particle size; The lower limit of the i-th magnification range corresponding to the target network layer is obtained by subtracting the difference from the reference scaling factor corresponding to the target network layer, and the upper limit of the i-th magnification range corresponding to the target network layer is obtained by adding the difference to the reference scaling factor corresponding to the target network layer. The target network layer is any one of the multiple network layers.
32. The device according to any one of claims 21 to 31, characterized in that: The multiple network layers all include modules that adopt a self-attention mechanism, and the target operator is a normalized exponential softmax operator.
33. The device according to any one of claims 21 to 32, characterized in that: The deep learning network model is suitable for any type of task scenario in semantic segmentation, image classification, object detection, image generation, language modeling, question answering, machine translation, and sentiment analysis.
34. The device according to claim 33, characterized in that In the case where the deep learning network model is applicable to an image classification task scenario, the test sample set includes a test image and a classification annotation result; In the case where the deep learning network model is applicable to a question answering task scenario, the test sample set includes test questions in text format and answer annotation results.
35. A structured sparse device for a network model, characterized in that: The device comprises: A receiving module, used for receiving a target sparsity mode, a target sparsity ratio, a deep learning network model, a test sample set and a target accuracy sent by a terminal; A structured sparse module, configured to perform initial structured sparseness on each of the multiple network layers to be sparse included in the deep learning network model according to the target sparse mode and the target sparse ratio, so as to obtain an initial sparse network model; The number of elements of the input matrix of the target operator of each network layer in the initial sparse network model is less than the number of elements of the input matrix of the target operator of the corresponding network layer in the deep learning network model, and the dimension of the output matrix of the initial sparse network model is the same as the dimension of the output matrix of the deep learning network model; An accuracy testing module, used to determine the accuracy of the initial sparse network model based on the test sample set; A determination module, configured to determine the initial sparse network model as a deep learning network model after structured sparseness when the accuracy of the initial sparse network model reaches the target accuracy; A sending module is used to send the structured sparse deep learning network model to the terminal.
36. An image classification device, characterized in that: The device comprises: A first input module, used for inputting a target image to be classified into an image classification model, wherein the image classification model includes a plurality of sparse network layers; A first determination module is used to determine a feature matrix based on a mapping matrix of a target network layer, wherein the target network layer is any one of the multiple network layers, and the mapping matrix is determined based on the target image; A pruning module, used for pruning elements in the feature matrix to obtain a pruned feature matrix and a position matrix, wherein the position matrix indicates the position of each element in the pruned feature matrix in the feature matrix before pruning; A second input module is used to input the pruned feature matrix into the target operator of the target network layer to obtain a normalized matrix; A second determination module, used to determine the output matrix of the target network layer based on the position matrix and the normalization matrix; The third determination module is used to determine the classification result output by the image classification model based on the output matrix of the target network layer.
37. The device according to claim 36, characterized in that The target network layer includes a target sparse operator, and the target sparse operator has a target sparse mode and a target sparse ratio; The pruning module is specifically used for: According to the target sparse mode and the target sparse ratio, the elements in the feature matrix are pruned by the target sparse operator to obtain a pruned feature matrix and a position matrix.
38. The device according to claim 37, characterized in that The target sparse mode is an overall sparse mode, the target sparse ratio is M%, M is greater than 0 and less than 100, and the target sparse operator is a first sparse operator; The pruning module is specifically used for: The first 1-M% of the elements with the largest values in the target dimension in the feature matrix are retained by the first sparse operator, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
39. The device according to claim 37, characterized in that The target sparse mode is a partition sparse mode, the target sparse ratio is N:R, N and R are both integers greater than or equal to 1, and N is less than R, and the target sparse operator is a second sparse operator; The pruning module is specifically used for: The second sparse operator is used to retain the first N elements with the largest values in each R elements in the target dimension in the feature matrix, and the position of each retained element is determined to obtain the pruned feature matrix and position matrix.
40. The device according to any one of claims 36 to 39, characterized in that: The device also includes: A scaling module, used for scaling the elements in the normalized matrix by using the element scaling operator; The second determining module is specifically used for: Based on the position matrix and the scaled normalized matrix, an output matrix of the target network layer is determined.
41. A computer-readable storage medium, characterized in that The storage medium stores instructions, and when the instructions are executed on the computer, the computer executes the steps of any one of the methods of claims 1-20.
42. A computer program product comprising instructions, which, when executed on a computer, cause the computer to perform the steps of the method according to any one of claims 1 to 20.
Citation Information
Patent Citations
Structured sparse image classification method and related device of network model
CN118734921A
Image classification method based on structure optimization sparse convolutional neural network and medium
CN110929798A
Neural network accelerated reasoning method and system based on dynamic sparse convolution
CN114118425A
Cited By
Deep neural network reasoning acceleration method and system
CN120893499A
A deep neural network inference acceleration method and system
CN120893499B
