Large language model rarefaction method and device, electronic equipment and storage medium
By configuring different predictors for different network layers of the large language model and flexibly selecting the sparsity according to the feature distribution, the problem of imbalanced efficiency and accuracy caused by fixed sparsity is solved, and the efficient processing and accuracy balance of the large language model are achieved. .
Patent Information
- Application Number
- CN202411155461.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-22
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2044-08-22
AI Technical Summary
Existing large language model sparsification methods usually use fixed sparsity, resulting in insufficient or excessive sparsity, affecting processing efficiency and effect, and making it difficult to achieve a balance between efficiency and accuracy.
By configuring different predictors for different network layers, the appropriate sparsity can be flexibly selected according to the difference in distribution of input features and network layer features, using higher sparsity to improve processing efficiency, and using lower sparsity to ensure accuracy, and process layer by layer. to achieve a balance between efficiency and accuracy.
It achieves a balanced effect between large language model processing efficiency and accuracy, improves processing efficiency and reduces information loss, and adapts to differences in different input features and network layer features.
Smart Images

Figure CN118673394A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a method, device, electronic device and storage medium for sparseizing a large language model. Background Art
[0002] In recent years, with the great success of ChatGPT, more and more large language models have been widely used and paid attention to by people from all walks of life. Due to the huge scale and number of parameters of large language models, optimizing the reasoning process of large language models to reduce energy consumption, operating costs and user waiting time has become the most important research and application topic of large language models.
[0003] Sparsification of large language models is one of the most important research directions for large language model optimization. Existing large language model sparsification usually uses fixed sparsity for sparsification, which is prone to insufficient or excessive sparsification, seriously affecting the processing efficiency and effect of large language models. Summary of the invention
[0004] In view of this, an object of the present invention is to provide a large language model sparsification method, device, electronic device and storage medium, which can effectively balance the processing efficiency and accuracy of the large language model.
[0005] In order to achieve the above purpose, the technical solution adopted by the embodiment of the present invention is as follows: In a first aspect, the present invention provides a method for sparseening a large language model, wherein the large language model includes a plurality of network layers, each of the network layers includes an activation function; the method comprises: Split the input text into multiple tokens and generate a feature vector for each token; Using the feature vector as input feature and the first network layer of the large language model as the target network layer; When the target network layer does not have a predictor, using the weight matrix of the target network layer as a sparse weight; When the target network layer has a predictor, the input features are input into the predictor of the target network layer to obtain the importance corresponding to multiple preset sparsities of each input feature, the target sparsity is determined according to the importance corresponding to the multiple preset sparsities of each input feature, and the weight matrix of the target network layer is sparsely calculated according to the mask corresponding to the target sparsity to obtain sparse weights; the mask is used to sparse the weight matrix; The product of each input feature and the sparse weight is input into the activation function of the target network layer, and the output result of the activation function is used as the input feature of the next network layer, and the next network layer is used as the target network layer, and the process is performed layer by layer until the last network layer of the large language model, and the output result of the activation function of the last network layer is used as the output result of the large language model.
[0006] In an optional implementation, determining the target sparsity according to the importance corresponding to a plurality of preset sparsities of each of the input features includes: For each of the input features, the preset sparsity corresponding to the importance exceeding the threshold is used as the preliminary sparsity, and the preliminary sparsity with the largest sparsity is used as the candidate sparsity; The number of each candidate sparsity is counted, and the candidate sparsity with the largest number is determined as the target sparsity.
[0007] In an optional implementation, performing sparse calculation on the weight matrix of the target network layer according to the mask corresponding to the target sparsity to obtain the sparse weights includes: The mask corresponding to the target sparsity is multiplied by the weight matrix of the target network layer to obtain the sparse weight.
[0008] In an optional embodiment, each network layer is provided with a weight matrix, and the predictor is obtained in the following manner: For each network layer that needs to adjust the sparsity, a weight importance matrix is obtained according to a plurality of training features and the weight matrix; the weight importance matrix is used to characterize the importance of each weight in the weight matrix; the training features of the first network layer are obtained by extracting features based on the received training text; the training features of the network layers other than the first network layer are the outputs of the previous network layer; According to the weight importance matrix and a plurality of preset sparsities, obtaining a mask corresponding to each of the preset sparsities; Obtaining the theoretical importance of each preset sparsity corresponding to each token vector according to the token vector corresponding to the training feature, the weight matrix and the mask corresponding to each preset sparsity; The fully connected layer to be trained is iteratively trained according to the theoretical importance of each of the token vectors and the corresponding preset sparsity to obtain the predicted importance of each preset sparsity, and the fully connected layer whose predicted importance meets the acceptance condition is used as the predictor.
[0009] In an optional implementation, the step of obtaining a weight importance matrix based on the plurality of training features and the weight matrix includes: Generate a corresponding training matrix according to each of the training features, and generate a three-dimensional training matrix according to all of the training matrices; Generate a three-dimensional weight matrix according to the weight matrix; Taking the absolute values of the three-dimensional training matrix and the three-dimensional weight matrix element by element and performing dot multiplication to obtain an activation matrix; The activation matrix is summed over the number dimension of the training matrix to obtain the weight importance matrix.
[0010] In an optional implementation, obtaining a mask corresponding to each preset sparsity according to the weight importance matrix and a plurality of preset sparsities includes: For each of the preset sparsity, when the sparse format is a ratio, the sparse quantity is calculated according to the preset sparsity, and the coordinates of the elements with small weights in the weight importance matrix are obtained according to the sparse quantity; When the sparse format is a ratio, the coordinates of the elements with small weights in the weight importance matrix are obtained in sequence according to the ratio; An all-one matrix is generated according to the weight importance matrix, and the elements corresponding to the element coordinates are set to 0, so as to obtain a mask corresponding to the preset sparsity.
[0011] In an optional implementation, obtaining the theoretical importance of each preset sparsity corresponding to each token vector according to the token vector corresponding to the training feature, the weight matrix and the mask corresponding to each preset sparsity includes: For each of the token vectors, an original importance matrix is obtained according to the token vector and the weight matrix; the token vector of the first network layer is obtained by extracting features from the tokens obtained by splitting the training text; the training features of the network layers other than the first network layer are output after the previous network layer processes the token vector; Obtaining a theoretical importance matrix corresponding to each of the preset sparsities according to the original importance matrix and the masks corresponding to each of the preset sparsities; The theoretical importance of each preset sparsity corresponding to each token vector is obtained according to each of the theoretical importance matrices and the original importance matrix.
[0012] In a second aspect, the present invention provides a large language model sparseness device, the large language model includes multiple network layers, each of the network layers includes an activation function; the device includes: A preprocessing module, used to split the input text into multiple tokens and generate a feature vector for each token; A sparse module is used to take the feature vector as an input feature and the first network layer of the large language model as a target network layer; when there is no predictor for the target network layer, the weight matrix of the target network layer is used as a sparse weight; when there is a predictor for the target network layer, the input feature is input into the predictor of the target network layer to obtain the importance corresponding to multiple preset sparsities of each input feature, the target sparsity is determined according to the importance corresponding to multiple preset sparsities of each input feature, and the weight matrix of the target network layer is sparsely calculated according to the mask corresponding to the target sparsity to obtain the sparse weight; the mask is used to sparse the weight matrix; the product of each input feature and the sparse weight is input into the activation function of the target network layer, and the output result of the activation function is used as the input feature of the next network layer, the next network layer is used as the target network layer, and the layer is processed layer by layer until the last network layer of the large language model, and the output result of the activation function of the last network layer is used as the output result of the large language model.
[0013] In a third aspect, the present invention provides an electronic device, comprising a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor can execute the machine executable instructions to implement the large language model sparsification method described in any of the aforementioned implementations.
[0014] In a fourth aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the method for sparsifying a large language model as described in any one of the aforementioned embodiments is implemented.
[0015] Compared with the prior art, the large language model sparsification method, device, electronic device and storage medium provided by the embodiments of the present invention configure different predictors for different network layers. The predictors make predictions based on different input features, fully considering the differences in the distribution of different input features and features of different network layers, and can flexibly select appropriate sparsity according to the input features and network features. For inputs with a high degree of sparsification, a higher sparsity is used for faster calculation to improve the processing efficiency of the large language model. For inputs with a low degree of sparsification, a lower sparsity is used to ensure the accuracy of the large language model, thereby achieving a balance between the processing efficiency and accuracy of the large language model.
[0016] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.
[0018] Figure 1 A schematic diagram showing the prior art of using fixed sparsity for thinning is shown.
[0019] Figure 2 A schematic flow chart of a large language model sparsification method provided by an embodiment of the present invention is shown.
[0020] Figure 3 A schematic diagram of generating sparse weights provided by an embodiment of the present invention is shown.
[0021] Figure 4 Another schematic flow chart of a large language model sparsification method provided by an embodiment of the present invention is shown.
[0022] Figure 5 A schematic diagram of generating a mask provided by an embodiment of the present invention is shown.
[0023] Figure 6 Another schematic diagram of generating a mask provided by an embodiment of the present invention is shown.
[0024] Figure 7 A schematic diagram of generating a theoretical importance matrix provided by an embodiment of the present invention is shown.
[0025] Figure 8 A block diagram of a large language model sparsification device provided by an embodiment of the present invention is shown.
[0026] Fig. 9 A block diagram of an electronic device provided by an embodiment of the present invention is shown.
[0027] Icon: 100 - electronic device; 110 - memory; 120 - processor; 130 - communication module; 300 - large language model sparseness device; 301 - preprocessing module; 302 - sparse module. DETAILED DESCRIPTION
[0028] The following will be combined with the accompanying drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.
[0029] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of the present invention.
[0030] It should be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0031] Sparsification of large language models includes static sparsification and dynamic sparsification. Static sparsification includes amplitude pruning, SparseGPT, Wanda, etc., and dynamic sparsification includes DejaVu, PowerInfer, etc. Among them, static sparsification refers to pre-pruning the model weights before forward reasoning, that is, making some weight values 0, thereby reducing the amount of calculation and accelerating reasoning during forward reasoning of the model. Dynamic sparsification refers to selecting only some features or some weight values for calculation according to the input features during the forward reasoning process, thereby reducing the amount of calculation and accelerating reasoning.
[0032] In static sparsification or dynamic sparsification, sparsity is the proportion of elements that are sparse (values become 0 or do not participate in the calculation). Existing large language model sparsification usually sets a uniform fixed sparsity. For example, based on experience, the sparsity is set to 50%. For static sparsification, 50% of the weights are sparsely reduced to 0. For dynamic sparsification, 50% of the elements are selected to participate in the calculation, such as Figure 1 shown. Figure 1 In the figure, the left matrix is the dense weight matrix before sparsification, and the right matrix is the sparse weight matrix after sparsification with a sparsity of 50%. The white box in the right matrix indicates that the weight becomes 0.
[0033] The inventors have found that if different network layers can be adapted to different sparsities for different inputs to perform operations, then as much sparsity as possible can be achieved with little impact on the accuracy of the large language model. However, due to the huge size and large number of network layers of the large language model, it is difficult to find a suitable sparsity for each layer. Some global search methods are difficult to implement.
[0034] Even without considering the inter-layer correlation, there are hundreds or even thousands of network layers when each layer is adjusted independently. If we consider that different inputs match different sparsities, the problem becomes even more complicated. Therefore, mainstream large language models all use fixed sparsity and use this fixed sparsity as a hyperparameter. For example, the fixed sparsity is set to 0.5. At most, the fixed sparsity can be manually adjusted and tested, and the best one can be found after multiple adjustments.
[0035] When a large language model uses fixed sparsity sparsification, the network layers that need to be sparsified in the large language model all use the preset fixed sparsity for sparsification calculation. Since the large language model includes multiple network layers, the weight feature distribution of each network layer is different. Some network layers have a large space available for sparsification, and after sparsification according to the fixed sparsity, there is still unused sparsification space. However, some network layers have a small space available for sparsification, and after sparsification according to the fixed sparsity, important information will inevitably be lost, affecting the accuracy of the large language model. Even for the same network layer, the degree of sparsification available for different input sequences is different.
[0036] Based on this, the core idea of the large language model sparsification method and device provided by the embodiment of the present invention is that different network layers are configured with different predictors, and the predictors make predictions based on different input features, fully considering the differences in the distribution of different input features and different network layer features, and can flexibly select suitable sparsity according to the input features and network features. For inputs with a high degree of sparsification, a higher sparsity is used for faster calculation to improve the processing efficiency of the large language model. For inputs with a low degree of sparsification, a lower sparsity is used to ensure the accuracy of the large language model, thereby achieving a balance between the processing efficiency and accuracy of the large language model.
[0037] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0038] Please refer to Figure 2 , Figure 2 A flow chart of a large language model sparseness method provided by an embodiment of the present invention is shown. The method is applied to a large language model, the large language model includes multiple network layers, each network layer includes an activation function, and the method includes the following steps: Step S101, split the input text into multiple tokens, and generate a feature vector for each token.
[0039] In the embodiment of the present invention, the large language model divides the input text into multiple tokens, and extracts features from each token to obtain a feature vector corresponding to each token. The input text may be in the form of e-books, articles, web pages, prompts, etc., and the token may be a word, punctuation mark, or other language unit.
[0040] Step S102, taking the feature vector as input feature and taking the first network layer of the large language model as the target network layer.
[0041] In the embodiment of the present invention, the reasoning calculation is started from the first network layer according to the hierarchy of the network layers in the large language model. First, the first network layer is used as the target network layer, and the feature vector is used as the input feature of the target network layer.
[0042] Step S103, determining whether there is a predictor in the target network layer.
[0043] In the embodiment of the present invention, it is queried whether the target network layer is provided with a predictor. If the target network layer does not have a predictor, step S104 is executed; if the target network layer has a predictor, step S105 is executed.
[0044] It should be noted that large language models are usually composed of stacked Transformer architectures, and the main parameters and calculations are concentrated in the attention module and the Multilayer Perceptron (MLP) module in the Transformer. The linear layer in the attention module calculates the Query, Key, and Value matrices, as well as the linear transformation layer after the multi-head attention features are merged. The MLP module usually consists of 2 linear layers, which can be sparse. In actual use, the above network layers in all transformer structures can be selected for sparseness, or the first few transformer modules can be skipped without sparseness. Generally, large language models with larger scale (for example, 70B and above) can sparse all Transformer modules, and small large language models can skip the first few transformer modules to maintain higher accuracy.
[0045] Step S104: when there is no predictor in the target network layer, the weight matrix of the target network layer is used as the sparse weight.
[0046] In the embodiment of the present invention, when there is no predictor in the target network layer, it means that the target network layer does not need to be sparse, and the weight matrix of the target network layer is directly used as the sparse weight, that is, the sparsity is 0.
[0047] Step S105, when a predictor exists for the target network layer, the input features are input into the predictor of the target network layer to obtain the importance corresponding to multiple preset sparsities of each input feature, the target sparsity is determined according to the importance corresponding to the multiple preset sparsities of each input feature, and the weight matrix of the target network layer is sparsely calculated according to the mask corresponding to the target sparsity to obtain the sparse weight.
[0048] Among them, the mask is used to sparse the weight matrix.
[0049] In an embodiment of the present invention, when a predictor exists in the target network layer, it indicates that the target network layer needs to adjust the sparsity, and each input feature is sequentially input into the predictor of the target network layer, or multiple input features are input into the predictor of the target network layer together, and the predictor predicts each input feature according to multiple preset sparsities, and obtains the importance corresponding to multiple preset sparsities of each input feature. The preset sparsity corresponds to the mask one by one, and each target network layer is provided with a mask corresponding to each preset sparsity.
[0050] It should be noted that the input features can be input one by one into the predictor of the target network layer for prediction, and the importance corresponding to multiple preset sparsities corresponding to the input features can be obtained. Multiple input features can also be input together into the predictor of the target network layer for prediction, and the importance corresponding to multiple preset sparsities corresponding to the multiple input features can be obtained. The present invention is not limited to this.
[0051] The preset sparsity can be configured in combination with the sparse computing framework and hardware support actually used. For example, if the GPU chip supports 2x, 4x, 8x and 16x sparsity, that is, the sparse ratios are 1:2, 3:4, 7:8 and 15:16, the preset sparsity and the corresponding mask can be set to 4 levels, corresponding to the sparse ratios supported. If the sparse computing framework used supports unstructured sparsity of arbitrary ratios, the preset sparsity can be set to 0.25, 0.5 and 0.75, or 0.1, 0.2, ..., 0.9. Even if the sparse computing framework or hardware only supports one sparse format, the large language model sparse method provided in the embodiment of the present invention can be used to generate different masks based on different input features, or to determine the network layer that needs to adjust the sparsity based on different input features, thereby improving the processing efficiency of the large language model.
[0052] Step S106, inputting the product of each input feature and the sparse weight into the activation function of the target network layer.
[0053] In an embodiment of the present invention, the product of each input feature and the sparse weight is passed through the activation function of the target network layer, so that the large language model can capture more complex features.
[0054] Step S107, determining whether the target network layer is the last network layer of the large language model.
[0055] In an embodiment of the present invention, it is determined whether the current target network layer is the last network layer of the large language model. If the current target network layer is not the last network layer of the large language model, step S108 is executed; if the current target network layer is the last network layer of the large language model, step S109 is executed.
[0056] Step S108, using the output result of the activation function as the input feature of the next network layer, and using the next network layer as the target network layer.
[0057] In the embodiment of the present invention, after executing step S108, a new target network layer and input features corresponding to the new target network layer are obtained. Steps S103 to S108 are continued to be executed according to the new target network layer and the corresponding input features until the last network layer of the large language model is processed, and then step S109 is continued.
[0058] Step S109, taking the output result of the activation function of the last network layer as the output result of the large language model.
[0059] In the embodiment of the present invention, the output result of the activation function of the last network layer is used as the output result of the large language model, and the output result of the large language model is sent to the user.
[0060] In summary, the large language model sparsification method provided by the embodiment of the present invention configures different predictors for different network layers, and the predictors make predictions based on different input features, fully considering the differences in the distribution of different input features and different network layer features, and can flexibly select suitable sparsity according to the input features and network features. For inputs with a high degree of sparsification, a higher sparsity is used for faster calculation to improve the processing efficiency of the large language model. For inputs with a low degree of sparsification, a lower sparsity is used to ensure the accuracy of the large language model, thereby achieving a balance between the processing efficiency and accuracy of the large language model.
[0061] Optionally, in practical applications, a threshold of information loss is set in combination with an application scenario, and target sparsities that meet the threshold conditions are screened according to different input features for sparseness. Figure 2 The sub-step of determining the target sparsity according to the importance corresponding to the multiple preset sparsities of each input feature in step S105 may include: For each input feature, the preset sparsity corresponding to the importance exceeding the threshold is used as the preliminary sparsity, and the preliminary sparsity with the largest sparsity is used as the candidate sparsity; the number of candidate sparsities is counted, and the candidate sparsity with the largest number is determined as the target sparsity.
[0062] As a possible implementation, assume that the preset sparsity is 25%, 50% and 75% respectively, and the importance threshold is set to 0.6. Input feature A is input to the predictor, and the importance of the predictor output is 0.88, 0.65 and 0.25 respectively. Among them, 0.88 is the importance of input feature A sparse at the preset sparsity of 0.25, 0.65 is the importance of input feature A sparse at the preset sparsity of 0.5, and 0.25 is the importance of input feature A sparse at the preset sparsity of 0.75.
[0063] Since 0.65 and 0.88 are greater than the threshold value 0.6, the preset sparsities 0.25 and 0.5 are used as the preliminary sparsities, and the preset sparsity 0.5 is used as the candidate sparsity. Similarly, the candidate sparsity corresponding to each input feature is determined, and the number of each candidate sparsity is counted. For example, if the candidate sparsities are 0.25 and 0.5, the number of candidate sparsity 0.25 is 500, and the number of candidate sparsity 0.5 is 600, then the candidate sparsity 0.5 is determined as the target sparsity. Each target network layer performs sparse operations according to its own target sparsity, and the target network layer corresponds to the target sparsity one by one.
[0064] It can be seen that the embodiment of the present invention utilizes the difference in input features in each network layer to dynamically adjust the sparsity of the network layer, and can better adaptively adjust the sparsity according to the input features, thereby reducing information loss, and improving the processing efficiency of the large language model through sparsification as much as possible while ensuring the accuracy of the large language model.
[0065] Optionally, in practical applications, the weight matrix is sparsely processed using a sparsity suitable for different input features and different network features, effectively reducing the weights involved in the calculation. Figure 2 The sub-step of performing sparse calculation on the weight matrix of the target network layer according to the mask corresponding to the target sparsity to obtain sparse weights in step S105 may include: The mask corresponding to the target sparsity is multiplied by the weight matrix of the target network layer to obtain the sparse weight.
[0066] In an embodiment of the present invention, a target mask is obtained according to the target sparsity, and a weight matrix of a target network layer is thinned according to the target mask to obtain sparse weights.
[0067] As a possible implementation, Figure 3For example, assuming the target sparsity is 0.5, a dot product operation is performed based on the mask and weight matrix corresponding to the sparsity of 0.5, that is, the weight matrix elements corresponding to the positions where the elements in the mask are 1 are copied to the positions corresponding to the sparse weights, and the elements in the sparse weights corresponding to the positions where the elements in the mask are 0 are set to 0. Compared with the weight matrix, the number of elements involved in the operation of the sparse weights is reduced from 15 to 8, which effectively reduces the amount of calculation in the inference process of the large language model, and maximizes the processing efficiency of the large language model while ensuring the accuracy of the large language model.
[0068] Optionally, before using the predictor to perform sparsification on a large language model, it is necessary to set a fully connected layer to be trained for each network layer that needs to adjust the sparsity, and train each fully connected layer to be trained to obtain a trained predictor. The following is an explanation of the training process of the predictor. Please refer to Figure 4 , Figure 4 Another flow chart of the method for sparseening a large language model provided by an embodiment of the present invention is shown. The predictor is obtained in the following manner: Step S201, for each network layer that needs to adjust the sparsity, a weight importance matrix is obtained according to multiple training features and a weight matrix.
[0069] Among them, the weight importance matrix is used to characterize the importance of each weight in the weight matrix; the training features of the first network layer are obtained by extracting features based on the received training text; the training features of the network layers other than the first network layer are the outputs of the previous network layer.
[0070] Step S202, obtaining a mask corresponding to each preset sparsity according to the weight importance matrix and multiple preset sparsities.
[0071] Step S203, obtaining the theoretical importance of each preset sparsity corresponding to each token vector according to the token vector corresponding to the training feature, the weight matrix and the mask corresponding to each preset sparsity.
[0072] In the embodiment of the present invention, taking the first network layer as an example, the training text is divided into multiple tokens, and features are extracted from the multiple tokens to obtain multiple token vectors corresponding to the training text, that is, multiple token vectors corresponding to the training features. The token vector of the second network layer is the token vector output by the first network layer.
[0073] For each network layer that needs to adjust the sparsity, a corresponding weight importance matrix is generated according to each training feature received by the network layer, and multiple masks corresponding to each training feature are generated according to multiple preset sparsities. The theoretical importance of each token vector based on each preset sparsity is obtained according to the mask of each training feature and each token vector corresponding to the training feature.
[0074] Step S204, iteratively train the fully connected layer to be trained according to the theoretical importance of each token vector and the corresponding preset sparsity, obtain the predicted importance of each preset sparsity, and use the fully connected layer whose predicted importance meets the acceptance condition as the predictor.
[0075] In an embodiment of the present invention, the token vector and the corresponding theoretical importance of each preset sparsity are input into the fully connected layer to be trained in turn to obtain the predicted importance of each preset sparsity corresponding to the token vector. When the difference between the predicted importance and the theoretical importance of each preset sparsity is less than the acceptance threshold, the fully connected layer is used as a predictor.
[0076] It is worth mentioning that the prediction principle of the predictor is essentially a regression task. In addition to training the fully connected layer as a predictor, regression models such as support vector regression model, decision tree regression model, gradient boosting tree model, etc. can also be trained as predictors. The present invention is not limited to this.
[0077] It should be noted that the embodiment of the present invention determines the mask corresponding to each preset sparsity according to the weight importance matrix of each network layer, and uses the mask of each preset sparsity to perform sparsification. When the information loss rate of the network layer after sparsification exceeds the preset loss threshold, it indicates that the network layer does not need to be sparsified. In other words, the network layer whose information loss rate after sparsification exceeds the preset loss threshold is not set with a predictor, and the network layer whose information loss rate after sparsification does not exceed the preset loss threshold is set with a predictor. Among them, the preset loss threshold can be set based on experience.
[0078] It can be seen that the embodiment of the present invention uses the training features received by each network layer in the large language model to generate masks of different sparsities corresponding to each training feature, and uses masks of different sparsities to generate token vectors based on the theoretical importance of different sparsities, and finally uses different token vectors and corresponding theoretical importance to iteratively train to obtain a predictor. The predictor can be used to predict the importance or information retention of different input features corresponding to different sparsities. In other words, the predictor can be used to measure the size of the loss after sparseness using different sparsities, thereby dynamically adjusting the sparsity of the large language model and improving the processing efficiency of the large language model as much as possible while ensuring the accuracy of the large language model.
[0079] Optionally, in practical applications, the importance of weights is determined in combination with the training features and the weight matrix, and the weights are as important as possible when performing weight sparsification, thereby reducing information loss. Figure 4 The sub-step of obtaining a weight importance matrix according to a plurality of training features and a weight matrix in step S201 may include: Generate a corresponding training matrix according to each training feature, and generate a three-dimensional training matrix according to all training matrices; generate a three-dimensional weight matrix according to the weight matrix; take the absolute value of each element of the three-dimensional training matrix and the three-dimensional weight matrix and perform dot multiplication to obtain an activation matrix; sum the activation matrix over the quantity dimension of the training matrix to obtain a weight importance matrix.
[0080] In the embodiment of the present invention, it is assumed that the preset feature length is D, the number of training features is N, and the D-dimensional training feature , The weight matrix . Each training feature is copied and expanded to the preset feature length The training matrix , stack all training matrices to get The three-dimensional training matrix The weight matrix is copied according to the number of training features, and the copied weight matrices are stacked to obtain The three-dimensional weight matrix .Will and Perform element-wise multiplication to get The activation matrix , sum the activation matrix A over the number dimension of the training matrix, and get The weight importance matrix The dimension of the weight importance matrix is the same as the weight matrix, that is, the number of rows and columns of the weight importance matrix is the same as the weight matrix.
[0081] It is worth mentioning that when generating the weight importance matrix, the activation matrix A can also be averaged or regularized in the number dimension of the training matrix. The present invention is not limited to this. As a possible implementation, assume that the preset feature length is 4, the number of training features is 2, and the two training features are and , the weight matrix w is:
[0082] According to the training characteristics and Copy and expand to get the corresponding training matrix and , and according to the training matrix and Generate a three-dimensional training matrix X.
[0083]
[0084]
[0085] X=
[0086] According to the number of training features, a three-dimensional weight matrix W is generated according to the weight matrix, and the absolute value of each element of the three-dimensional training matrix and the three-dimensional weight matrix is taken and dot multiplication is performed to obtain the activation matrix A.
[0087] W=
[0088] A=
[0089] in, It is a training matrix and the weight matrix The absolute value of each element is taken and then the dot product is performed. It is a training matrix and the weight matrix It is obtained by taking the absolute value of each element and performing dot multiplication.
[0090]
[0091]
[0092] The activation matrix is summed over the number dimension of the training matrix, that is, according to and Sum each element to get the weight importance matrix S.
[0093]
[0094] It should be noted that the three-dimensional training matrix, the three-dimensional weight matrix and the activation matrix are calculated in parallel using matrices to obtain the weight importance matrix. When calculating the weight importance matrix, the number N of training matrices can be set based on experience, for example, N is any integer between 128 and 512. The training features of the first network layer correspond to the received training texts one by one, each training text is divided into multiple tokens, and features are extracted for each token to obtain a token vector. The feature values of the token vectors corresponding to a training text are averaged to obtain the corresponding training features.
[0095] It can be seen that the embodiment of the present invention fully considers the differences in the distribution of different input features and different network layer features to construct a weight importance matrix suitable for each network layer, and obtains a mask suitable for each network layer based on the weight importance matrix. When generating the weight importance matrix, the training features and weight matrix input by each network layer are used for parallel calculation, so as to use the GPU chip for acceleration, effectively improving the reasoning speed and processing efficiency of the large language model.
[0096] Optionally, in practical applications, dynamically generating masks for each network layer according to the weight importance matrix can effectively reduce information loss. Figure 4 The sub-steps of step S202 may include: For each preset sparsity, when the sparse format is a ratio, the sparse quantity is calculated according to the preset sparsity, and the coordinates of the elements with small weights in the weight importance matrix are obtained according to the sparse quantity; when the sparse format is a proportion, the coordinates of the elements with small weights in the weight importance matrix are obtained in sequence according to the proportion; an all-1 matrix is generated according to the weight importance matrix, and the elements corresponding to the element coordinates are set to 0, so as to obtain the mask corresponding to the preset sparsity.
[0097] In an embodiment of the present invention, if the sparse format is a ratio, that is, the preset sparsity is unstructured data, the sparse quantity is calculated according to the number of elements in the weight importance matrix and the preset sparsity, and the element coordinates corresponding to the first sparse number of weights are obtained in order from small to large in the weight importance matrix. An all-1 matrix is generated according to the rows and columns of the weight importance matrix, and the elements corresponding to the element coordinates are updated to 0 to obtain a mask corresponding to the preset sparsity.
[0098] In one possible implementation, Figure 5 For example, assuming that the weight importance matrix S is Matrix, with a preset sparsity of 50%. First, determine the number of sparsity (i.e. 8) based on the number of elements in the weight importance matrix S (i.e. 16) and the preset sparsity (i.e. 50%). Second, start searching from the smallest weight in the weight importance matrix S and find the coordinates of the 8 elements with small weights (i.e. Figure 5 The white box position in the middle matrix). Finally, generate a The matrix of all 1s, updates the elements corresponding to the element coordinates to 0, and obtains Figure 5 The mask on the right.
[0099] It should be noted that the element in the mask is 1 to represent the element of the coordinate to be retained, and 0 to represent the element of the coordinate to be thinned. Similarly, 50% can be replaced with the corresponding ratio, and the masks corresponding to the required sparsity can be saved, for example, the masks corresponding to the preset sparsity of 25%, 50% and 75% can be saved.
[0100] In an embodiment of the present invention, if the sparse format is a ratio, that is, the preset sparsity is semi-structured data, for example, the preset sparsity is K:L, then when generating the mask, K elements out of every L elements are set to 0. Among them, the element coordinates of the K elements are the coordinates of the elements with the smallest weights selected from the corresponding L weights in the weight importance matrix in the order of the weights from the smallest to the largest.
[0101] In another possible implementation, Figure 6 For example, assuming that the weight importance matrix S is Matrix, with preset sparsities of 50%, 2:4, and 3:4. Figure 6 In , masks corresponding to each preset sparsity are generated according to the weight importance matrix S and three preset sparsities (50%, 2:4, and 3:4, respectively). It can be seen that although the preset sparsities of 50% and 2:4 both retain general elements, the preset sparsity of 50% is to select the coordinates of elements with small weights in the weight importance matrix S for sparseness, while the preset sparsity of 2:4 is to select the coordinates of elements with small weights for sparseness according to every 4 weights.
[0102] It can be seen that the embodiment of the present invention constructs a weight importance matrix suitable for each network layer based on the differences in different input features and feature distributions of different network layers, and generates masks of preset sparsity in each network layer based on the weight importance matrix. By setting masks that meet different input features and feature distributions of different network layers, more zero values are introduced, effectively reducing the number of weights actually involved in the calculation, thereby improving the speed of matrix operations in large language models. Since only non-zero weights need to be stored in large language models, the memory space occupancy is effectively reduced through sparse processing, further optimizing the processing efficiency of large language models.
[0103] Optionally, in practical applications, different token vectors are used to generate theoretical importance of each preset sparsity, which effectively enriches the training data of the predictor, thereby improving the prediction accuracy of the predictor. Figure 4 The sub-steps of step S203 may include: For each token vector, the original importance matrix is obtained according to the token vector and the weight matrix; the token vector of the first network layer is obtained by feature extraction of the tokens obtained by splitting the training text; the training features of the network layers other than the first network layer are the output of the previous network layer after processing the token vector; according to the original importance matrix and the masks corresponding to each preset sparsity, the theoretical importance matrix corresponding to each preset sparsity is obtained; according to each theoretical importance matrix and the original importance matrix, the theoretical importance of each preset sparsity corresponding to each token vector is obtained.
[0104] In a possible implementation, for each token vector, the token vector is copied and expanded to obtain a token matrix, and the original importance matrix is obtained according to the token matrix and the weight matrix. Figure 7 For example, assuming the token vector is , the weight matrix is the weight matrix in the previous example , the preset sparsity is 25%, 50% and 75%. The original importance matrix T is calculated as:
[0105] According to the original importance matrix T, the total weight of the token vector before sparsification is 128. According to the preset sparsity and the original importance matrix T, the masks corresponding to the sparsity of 25%, the masks corresponding to the sparsity of 50%, and the masks corresponding to the sparsity of 75% are obtained respectively. Then, according to the masks corresponding to each preset coefficient and the original importance matrix T, the theoretical importance matrix corresponding to the sparsity of 25%, the theoretical importance matrix corresponding to the sparsity of 50%, and the theoretical importance matrix corresponding to the sparsity of 50% are obtained respectively. Finally, according to each theoretical importance matrix, the weight sum corresponding to the sparsity of 25%, the weight sum corresponding to the sparsity of 50%, and the weight sum corresponding to the sparsity of 75% are obtained respectively. The three weight sums are 115, 90, and 58 respectively.
[0106] According to the sum of the weights after sparseness and the sum of the weights before sparseness, the theoretical importance of 25% sparsity is 0.9, the theoretical importance of 50% sparsity is 0.7, and the theoretical importance of 75% sparsity is 0.45. Thus, a set of training data, i.e., token vector and theoretical importance Assuming that about 1,000 training texts are used when training the predictor, splitting by token will result in about 100,000 sets of training data. For the network layers that need to adjust the sparsity, the training data corresponding to each network layer needs to be obtained in the same way as above to calculate the theoretical importance.
[0107] Based on the same inventive concept, the embodiment of the present invention also provides a large language model sparse device. Its basic principle and technical effects are the same as those of the above embodiment. For the sake of brief description, for parts not mentioned in this embodiment, reference can be made to the corresponding contents in the above embodiment.
[0108] Please refer to Figure 8 , Figure 8 A block diagram of a large language model sparse device 300 provided by an embodiment of the present invention is shown. The large language model includes multiple network layers, each network layer includes an activation function. The large language model sparse device 300 includes a preprocessing module 301 and a sparse module 302.
[0109] A preprocessing module 301 is used to split the input text into multiple tokens and generate a feature vector for each token; The sparse module 302 is used to use the feature vector as the input feature and the first network layer of the large language model as the target network layer; when the target network layer does not have a predictor, the weight matrix of the target network layer is used as the sparse weight; when the target network layer has a predictor, the input feature is input into the predictor of the target network layer to obtain the importance corresponding to multiple preset sparsities of each input feature, the target sparsity is determined according to the importance corresponding to the multiple preset sparsities of each input feature, and the weight matrix of the target network layer is sparsely calculated according to the mask corresponding to the target sparsity to obtain the sparse weight; the mask is used to sparse the weight matrix; the product of each input feature and the sparse weight is input into the activation function of the target network layer, and the output result of the activation function is used as the input feature of the next network layer, and the next network layer is used as the target network layer, and the layer is processed layer by layer until the last network layer of the large language model, and the output result of the activation function of the last network layer is used as the output result of the large language model.
[0110] In summary, the large language model sparsification device provided by the embodiment of the present invention has different predictors configured for different network layers, and the predictors make predictions based on different input features, which fully consider the differences in the distribution of different input features and features of different network layers, and can flexibly select suitable sparsity according to the input features and network features. For inputs with a high degree of sparsification, a higher sparsity is used for faster calculation to improve the processing efficiency of the large language model. For inputs with a low degree of sparsification, a lower sparsity is used to ensure the accuracy of the large language model, thereby achieving a balance between the processing efficiency and accuracy of the large language model.
[0111] Optionally, the sparse module 302 is specifically used to, for each input feature, use the preset sparsity corresponding to the importance exceeding the threshold as the preliminary sparsity, and use the preliminary sparsity with the largest sparsity as the candidate sparsity; count the number of each candidate sparsity, and determine the candidate sparsity with the largest number as the target sparsity.
[0112] Optionally, the sparse module 302 is specifically used to perform a dot multiplication of a mask corresponding to a target sparsity and a weight matrix of a target network layer to obtain a sparse weight.
[0113] Optionally, each network layer is provided with a weight matrix. The preprocessing module 301 is also used to obtain a weight importance matrix for each network layer that needs to adjust the sparsity according to multiple training features and a weight matrix; the weight importance matrix is used to characterize the importance of each weight in the weight matrix; the training features of the first network layer are obtained by extracting features based on the received training text; the training features of the network layers other than the first network layer are the outputs of the previous network layer; according to the weight importance matrix and multiple preset sparsities, a mask corresponding to each preset sparsity is obtained; according to the token vector corresponding to the training feature, the weight matrix and the mask corresponding to each preset sparsity, the theoretical importance of each preset sparsity corresponding to each token vector is obtained; according to each token vector and the theoretical importance of each corresponding preset sparsity, the fully connected layer to be trained is iteratively trained to obtain the predicted importance of each preset sparsity, and the fully connected layer whose predicted importance meets the acceptance condition is used as a predictor.
[0114] Optionally, the preprocessing module 301 is specifically used to generate a corresponding training matrix according to each training feature, and generate a three-dimensional training matrix according to all the training matrices; generate a three-dimensional weight matrix according to the weight matrix; take the absolute value of each element of the three-dimensional training matrix and the three-dimensional weight matrix and perform dot multiplication to obtain an activation matrix; sum the activation matrix over the quantity dimension of the training matrix to obtain a weight importance matrix.
[0115] Optionally, the preprocessing module 301 is specifically used to calculate the sparse quantity according to the preset sparsity for each preset sparsity, when the sparse format is a ratio, and obtain the coordinates of the elements with small weights in the weight importance matrix according to the sparse quantity; when the sparse format is a proportion, obtain the coordinates of the elements with small weights in the weight importance matrix in sequence according to the proportion; generate an all-1 matrix according to the weight importance matrix, and set the elements corresponding to the element coordinates to 0, so as to obtain a mask corresponding to the preset sparsity.
[0116] Optionally, the preprocessing module 301 is specifically used to obtain, for each token vector, an original importance matrix according to the token vector and the weight matrix; the token vector of the first network layer is obtained by feature extraction of the tokens obtained by splitting the training text; the training features of the network layers other than the first network layer are output after the previous network layer processes the token vector; according to the original importance matrix and the masks corresponding to the preset sparsities, a theoretical importance matrix corresponding to each preset sparsity is obtained; according to the theoretical importance matrices and the original importance matrix, the theoretical importance of each preset sparsity corresponding to each token vector is obtained.
[0117] Please refer to Fig. 9 , Fig. 9A block diagram of an electronic device 100 provided in an embodiment of the present invention. The electronic device 100 may be any device having a data processing function, such as a personal computer, a laptop computer, a server, etc. The electronic device 100 includes a memory 110, a processor 120, and a communication module 130. The memory 110, the processor 120, and the communication module 130 are electrically connected to each other directly or indirectly to achieve data transmission or interaction. For example, these components may be electrically connected to each other via one or more communication buses or signal lines.
[0118] The memory 110 is used to store programs or data. The memory 110 may be, but is not limited to, a random access memory (RAM), a read only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), etc.
[0119] The processor 120 is used to read / write data or programs stored in the memory 110 and execute corresponding functions. For example, when the computer program stored in the memory 110 is executed by the processor 120, the large language model sparseness method disclosed in the above embodiments can be implemented.
[0120] The communication module 130 is used to establish a communication connection between the electronic device 100 and other communication terminals through a network, and to send and receive data through the network.
[0121] It should be understood that Fig. 9 The structure shown is only a schematic diagram of the structure of the electronic device 100. The electronic device 100 may also include Fig. 9 More or fewer components as shown, or with Fig. 9 Different configurations are shown. Fig. 9 Each component shown in the figure can be implemented by hardware, software or a combination thereof.
[0122] An embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by the processor 120, the large language model sparsification method disclosed in the above embodiments is implemented.
[0123] In several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for implementing the specified logical functions. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.
[0124] In addition, the functional modules in the various embodiments of the present invention may be integrated together to form an independent part, or each module may exist independently, or two or more modules may be integrated to form an independent part.
[0125] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for enabling a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage media include: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, and other media that can store program codes.
[0126] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for sparseening a large language model, characterized in that: The large language model includes a plurality of network layers, each of which includes an activation function; and the method includes: Split the input text into multiple tokens and generate a feature vector for each token; Using the feature vector as input feature and the first network layer of the large language model as the target network layer; When the target network layer does not have a predictor, using the weight matrix of the target network layer as a sparse weight; When the target network layer has a predictor, the input features are input into the predictor of the target network layer to obtain the importance corresponding to multiple preset sparsities of each input feature, the target sparsity is determined according to the importance corresponding to the multiple preset sparsities of each input feature, and the weight matrix of the target network layer is sparsely calculated according to the mask corresponding to the target sparsity to obtain sparse weights; the mask is used to sparse the weight matrix; The product of each input feature and the sparse weight is input into the activation function of the target network layer, and the output result of the activation function is used as the input feature of the next network layer, and the next network layer is used as the target network layer, and the process is performed layer by layer until the last network layer of the large language model, and the output result of the activation function of the last network layer is used as the output result of the large language model.
2. The method for sparseening a large language model according to claim 1, characterized in that: The determining the target sparsity according to the importance corresponding to the multiple preset sparsities of each of the input features includes: For each of the input features, the preset sparsity corresponding to the importance exceeding the threshold is used as the preliminary sparsity, and the preliminary sparsity with the largest sparsity is used as the candidate sparsity; The number of each candidate sparsity is counted, and the candidate sparsity with the largest number is determined as the target sparsity.
3. The method for sparseening a large language model according to claim 1, characterized in that: The step of performing sparse calculation on the weight matrix of the target network layer according to the mask corresponding to the target sparsity to obtain sparse weights includes: The mask corresponding to the target sparsity is multiplied by the weight matrix of the target network layer to obtain the sparse weight.
4. The method for sparseening a large language model according to claim 1, characterized in that: Each network layer is provided with a weight matrix, and the predictor is obtained in the following way: For each network layer that needs to adjust sparsity, a weight importance matrix is obtained according to a plurality of training features and the weight matrix; the weight importance matrix is used to characterize the importance of each weight in the weight matrix; the training features of the first network layer are obtained by extracting features based on the received training text; The training features of the network layers except the first network layer are the output of the previous network layer; According to the weight importance matrix and a plurality of preset sparsities, obtaining a mask corresponding to each of the preset sparsities; Obtaining the theoretical importance of each preset sparsity corresponding to each token vector according to the token vector corresponding to the training feature, the weight matrix and the mask corresponding to each preset sparsity; The fully connected layer to be trained is iteratively trained according to the theoretical importance of each of the token vectors and the corresponding preset sparsity to obtain the predicted importance of each preset sparsity, and the fully connected layer whose predicted importance meets the acceptance condition is used as the predictor.
5. The method for sparseening a large language model according to claim 4, characterized in that: The step of obtaining a weight importance matrix according to the plurality of training features and the weight matrix comprises: Generate a corresponding training matrix according to each of the training features, and generate a three-dimensional training matrix according to all of the training matrices; Generate a three-dimensional weight matrix according to the weight matrix; Taking the absolute values of the three-dimensional training matrix and the three-dimensional weight matrix element by element and performing dot multiplication to obtain an activation matrix; The activation matrix is summed over the number dimension of the training matrix to obtain the weight importance matrix.
6. The method for sparseening a large language model according to claim 4, characterized in that: The step of obtaining a mask corresponding to each preset sparsity according to the weight importance matrix and a plurality of preset sparsities includes: For each of the preset sparsity, when the sparse format is a ratio, the sparse quantity is calculated according to the preset sparsity, and the coordinates of the elements with small weights in the weight importance matrix are obtained according to the sparse quantity; When the sparse format is a ratio, the coordinates of the elements with small weights in the weight importance matrix are obtained in sequence according to the ratio; An all-one matrix is generated according to the weight importance matrix, and the elements corresponding to the element coordinates are set to 0, so as to obtain a mask corresponding to the preset sparsity.
7. The method for sparseening a large language model according to claim 4, characterized in that: The obtaining, according to the token vector corresponding to the training feature, the weight matrix and the mask corresponding to each preset sparsity, the theoretical importance of each preset sparsity corresponding to each token vector comprises: For each of the token vectors, an original importance matrix is obtained according to the token vector and the weight matrix; the token vector of the first network layer is obtained by extracting features from the tokens obtained by splitting the training text; the training features of the network layers other than the first network layer are output after the previous network layer processes the token vector; Obtaining a theoretical importance matrix corresponding to each of the preset sparsities according to the original importance matrix and the masks corresponding to each of the preset sparsities; The theoretical importance of each preset sparsity corresponding to each token vector is obtained according to each of the theoretical importance matrices and the original importance matrix.
8. A large language model sparse device, characterized in that: The large language model includes a plurality of network layers, each of which includes an activation function; the device includes: A preprocessing module, used to split the input text into multiple tokens and generate a feature vector for each token; A sparse module is used to take the feature vector as an input feature and the first network layer of the large language model as a target network layer; when there is no predictor for the target network layer, the weight matrix of the target network layer is used as a sparse weight; when there is a predictor for the target network layer, the input feature is input into the predictor of the target network layer to obtain the importance corresponding to multiple preset sparsities of each input feature, the target sparsity is determined according to the importance corresponding to multiple preset sparsities of each input feature, and the weight matrix of the target network layer is sparsely calculated according to the mask corresponding to the target sparsity to obtain the sparse weight; the mask is used to sparse the weight matrix; the product of each input feature and the sparse weight is input into the activation function of the target network layer, and the output result of the activation function is used as the input feature of the next network layer, the next network layer is used as the target network layer, and the layer is processed layer by layer until the last network layer of the large language model, and the output result of the activation function of the last network layer is used as the output result of the large language model.
9. An electronic device, characterized in that: It includes a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor can execute the machine executable instructions to implement the large language model sparsification method described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the large language model sparsification method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Network model calculation method, electronic equipment and storage medium
CN114861867A
Vehicle traffic light detection method and device, vehicle and storage medium
CN115861964A
Detection network lightweight method combining progressive guide distillation and structure reconstruction
CN117557857A
Cited By
Passenger head injury prediction method and device based on sparse weight neural network, and storage medium
CN119150929A
Two-stage sparse method based on small sample fast sparse large language model
CN119623534A
Multi-modal data processing method and device and model training method and device
CN119862424A
Context sparsity large language model efficient optimization system based on input driving
CN120524322A
Input-driven context sparsity-based efficient optimization system for large language models
CN120524322B
