Pruning method, system and electronic device for natural language model
By dynamically adjusting the head weight of the natural language model, the problem of large model computing resource usage and inconsistent adaptability is solved, and a more efficient and robust inference process is achieved.
Patent Information
- Application Number
- CN202410842278.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-27
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2044-06-27
AI Technical Summary
Existing large natural language models take up a lot of computing resources and are slow in reasoning during the inference process, and the static pruning method leads to inconsistent adaptability to different input data and poor robustness.
By obtaining training data, a small pruning model is constructed and its parameters are merged into the natural language model, so that the model can dynamically adjust the head weight according to the input data and realize dynamic pruning processing of different data.
It improves the reasoning efficiency and robustness of the natural language model, avoids the problem of poor performance on some data under static pruning, and maintains high accuracy.
Smart Images

Figure CN118445400B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of natural language processing, and in particular to a pruning processing method, system and electronic device for a natural language model. Background Art
[0002] With the development of natural language big models, more and more natural language big models are released and open source, among which the more typical ones include llama, chatglm, baichuan, qianwen, opt, etc. However, these natural language big models with tens of billions of parameters require huge computing resources to support, resulting in extremely high inference costs and slow inference speed when using natural language big models to reason about the data to be processed. On the one hand, it takes up more device resources to perform reasoning, affecting device performance. On the other hand, it affects the response speed in scenarios with high real-time requirements.
[0003] In the prior art, some solutions have been proposed for the above-mentioned problems, including model quantization, model pruning and operator optimization. Among them, the model quantization method is to speed up the operation by changing the data type. After direct quantization, the effect is usually lost greatly, and a large amount of data is required for further fine-tuning. For large models with huge parameters, the training cost is high. The operator optimization method is to study the way to build high-performance operators. This method has high research cost and time. The model pruning method is to reduce the model parameters and calculation amount by eliminating some "unimportant" structures in the model. Since the parameters of large natural language models are huge and the degree of sparsity is high, the accuracy of the pruned model will not be greatly affected.
[0004] However, in the prior art, when pruning a model, a static pruning method is generally used, that is, the model is pruned using the same pruning strategy, which means that the same model is used to process all input data. However, different input data activate different neurons in the model, resulting in inconsistent adaptability of the pruned model to different data. Therefore, this pruning method has a greater impact on the effect of some data and has poor robustness. Summary of the invention
[0005] The purpose of the present invention includes, for example, providing a pruning processing method, system and electronic device for a natural language model, which can adaptively find the optimal head according to different inputs each time, with better reasoning effect and stronger robustness.
[0006] The embodiments of the present invention can be implemented as follows:
[0007] In a first aspect, the present invention provides a pruning method for a natural language model, wherein the natural language model comprises an embedding layer and a module layer group connected in sequence, wherein the module layer group comprises a multi-head attention operation module, wherein the multi-head attention operation module comprises a plurality of heads, and the method comprises:
[0008] Acquire multiple training data, and import each of the training data into the natural language model;
[0009] Obtaining a data vector corresponding to each of the training data through the embedding layer, obtaining corresponding input structure data and label data corresponding to the input structure data through the module layer group and based on the data vector, wherein the label data represents the weight corresponding to each head in the multi-head attention operation module;
[0010] The constructed pruned small model is trained using the input construction data and the corresponding label data to obtain model parameters of the trained pruned small model;
[0011] The model parameters of the trained pruned small model are merged with the model parameters of the natural language model to obtain a natural language model with a pruning function, wherein the model parameters of the pruned small model contained in the natural language model with a pruning function are used to prune multiple heads in the natural language model when processing the data to be processed.
[0012] In an optional embodiment, the module layer group includes a plurality of module layers connected in sequence, each of the module layers includes a multi-head attention operation module and a multi-layer perceptron connected in sequence, and the multi-head attention operation module includes a normalization layer, a first fully connected layer and a multi-head attention calculation layer connected in sequence;
[0013] The model parameters of the pruned small model are merged into the model parameters of the first fully connected layer of each module layer to obtain a natural language model with pruning function.
[0014] In an optional embodiment, the step of obtaining corresponding input construction data and label data corresponding to the input construction data based on the data vector through the module layer group includes:
[0015] For each of the multiple module layers, the input data vector or the input construction data output by the previous module layer is processed based on the normalization layer in the module layer, and the processed data is passed through the first fully connected layer in the module layer to obtain corresponding input construction data including multiple construction data blocks;
[0016] Each construction data block in the input construction data is divided into multiple data sub-blocks through the multi-head attention calculation layer in the module layer, and the multiple divided construction data blocks are fused to obtain the label data corresponding to the input construction data.
[0017] In an optional implementation, the number of data sub-blocks obtained by dividing each of the constructed data blocks is the same as the number of heads in the multi-head attention calculation layer;
[0018] The step of fusing the divided multiple construction data blocks to obtain label data corresponding to the input construction data includes:
[0019] The divided multiple constructed data blocks are merged to obtain a merged data block containing multiple data sub-blocks;
[0020] A binomial calculation is performed on each data sub-block in the fused data block to obtain a binomial result, and label data corresponding to the input constructed data is obtained based on the binomial result of each data sub-block, wherein the binomial result of each data sub-block represents the weight of the header corresponding to each data sub-block.
[0021] In an optional implementation, the number of trained pruned small models is the same as the number of module layers, and the step of merging the model parameters of the trained pruned small models with the model parameters of the natural language model includes:
[0022] Performing a completion operation on the model parameters of each of the pruned small models that have completed training;
[0023] The model parameters of each of the pruned small models after the padding operation are merged with the model parameters of the first fully connected layer in the corresponding module layer.
[0024] In an optional implementation, the step of training the constructed pruned small model using the input structured data and the corresponding label data includes:
[0025] Importing the input structured data into the constructed pruning mini-model for processing to obtain output data;
[0026] Based on the label data and output data corresponding to the input construction data, and under the guidance of the constructed loss function, the model parameters of the pruned small model are adjusted until the preset stopping condition is met, and the training is stopped.
[0027] In an optional embodiment, the natural language model further includes a classification layer connected after the module layer group, and the method further includes:
[0028] Acquire data to be processed, and import the data to be processed into the natural language model with pruning function;
[0029] For every two adjacent module layers in the natural language model with pruning function, the previous module layer processes the data input to the module layer to obtain output data and head weights, and inputs the output data and head weights to the next module layer, and the head weights are used for the next module layer to perform pruning processing on the heads in its multi-head attention operation module;
[0030] The output data of the last module layer is obtained, and the response information of the data to be processed is obtained based on the output data using the classification layer.
[0031] In an optional implementation, the step of obtaining response information of the data to be processed based on the output data by using the classification layer includes:
[0032] Obtaining a matching degree between the data to be processed and each of the preset multiple terms based on the processing result of the classification layer on the output data;
[0033] Extract the terms with the highest matching degree in a preset position to form the response information of the data to be processed, and output the response information.
[0034] In a second aspect, the present invention provides a pruning processing system for a natural language model, wherein the natural language model comprises an embedding layer and a module layer group connected in sequence, wherein the module layer group comprises a multi-head attention operation module, wherein the multi-head attention operation module comprises a plurality of heads, and the system comprises:
[0035] An acquisition module, used for acquiring a plurality of training data and importing each of the training data into the natural language model;
[0036] A processing module, configured to obtain a data vector corresponding to each of the training data through the embedding layer, obtain corresponding input structure data and label data corresponding to the input structure data through the module layer group and based on the data vector, wherein the label data represents a weight corresponding to each head in the multi-head attention operation module;
[0037] A training module, used to train the constructed pruned small model using the input construction data and the corresponding label data to obtain model parameters of the trained pruned small model;
[0038] A merging module is used to merge the model parameters of the trained pruned small model with the model parameters of the natural language model to obtain a natural language model with a pruning function, wherein the model parameters of the pruned small model contained in the natural language model with a pruning function are used to prune multiple heads in the natural language model when processing the data to be processed.
[0039] In a third aspect, the present invention provides an electronic device, characterized in that it comprises one or more storage media and one or more processors communicating with the storage media, wherein the one or more storage media store machine executable instructions executable by the processors, and when the electronic device is running, the processor executes the machine executable instructions to execute the method described in any one of the aforementioned embodiments.
[0040] The beneficial effects of the embodiments of the present invention include, for example:
[0041] The present invention provides a pruning processing method, system and electronic device for a natural language model, wherein each obtained training data is imported into the natural language model, a data vector of each training data is obtained through an embedding layer, and corresponding input construction data and corresponding label data are obtained through a module layer group and based on the data vector, wherein the label data represents the weight of each head in a multi-head attention operation module. The constructed pruned small model is trained using the input construction data and the label data to obtain the model parameters of the trained pruned small model. The model parameters of the pruned small model are merged with the model parameters of the natural language model to obtain a natural language model with a pruning function. The model parameters of the pruned small model contained in the model can be used to prune multiple heads in the natural language model when processing the data to be processed.
[0042] This solution processes the pruned small model based on the input structure data and label data obtained through natural language model processing, so that the pruned small model can obtain different weights for pruning each head for different data. This achieves the effect of dynamically pruning the head according to the input data, avoiding the defect of static pruning that is not suitable for some data and leads to poor reasoning effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] 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.
[0044] Figure 1 A flowchart of a pruning method for a natural language model provided by an embodiment of the present invention;
[0045] Figure 2 A logical schematic diagram of the overall pruning method of a natural language model provided by an embodiment of the present invention;
[0046] Figure 3A schematic diagram of the architecture of a natural language model provided by an embodiment of the present invention;
[0047] Figure 4 A schematic diagram of processing training data provided by an embodiment of the present invention;
[0048] Figure 5 A schematic diagram showing a comparison of the recall rates of the pruned small model constructed in an embodiment of the present invention and the existing two-layer network model;
[0049] Figure 6 A schematic diagram of the architecture of a pruning mini-model constructed for an embodiment of the present invention;
[0050] Figure 7 A schematic diagram of merging the model parameters of the pruned small model and the model parameters of the natural language model in an embodiment of the present invention;
[0051] Figure 8 Another flow chart of the pruning method of the natural language model provided by the embodiment of the present invention;
[0052] Fig. 9 A schematic diagram of the reasoning process of the natural language model before optimization;
[0053] Fig.10 A schematic diagram of the reasoning process of the optimized natural language model in an embodiment of the present invention;
[0054] Fig.11 Schematic diagram of PPL perplexity evaluation of the optimized natural language model in an embodiment of the present invention;
[0055] Fig.12 A functional module block diagram of a natural language model pruning processing system provided by an embodiment of the present invention;
[0056] Fig.13 This is a structural block diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0057] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are 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.
[0058] 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 ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0059] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.
[0060] In the description of the present invention, it should be noted that the terms “first”, “second”, etc., if used, are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0061] It should be noted that, in the absence of conflict, the features in the embodiments of the present invention may be combined with each other.
[0062] Figure 1 A flow chart of a pruning method for a natural language model provided by an embodiment of the present invention is shown. The pruning method for a natural language model can be implemented by a pruning system for a natural language model. The pruning system for a natural language model can be implemented by software and / or hardware and can be configured in an electronic device, which can be a computer device. The pruning method for a natural language model can be used to process a natural language model to obtain a natural language model with a pruning function.
[0063] In this embodiment, the natural language model includes an embedding layer and a module layer group connected in sequence, the module layer group includes a multi-head attention operation module, and the multi-head attention operation module includes multiple heads. The detailed steps of the pruning processing method of the natural language model are introduced as follows.
[0064] S11, obtaining a plurality of training data, and importing each of the training data into the natural language model.
[0065] S12, obtaining a data vector corresponding to each of the training data through the embedding layer, obtaining corresponding input construction data and label data corresponding to the input construction data through the module layer group and based on the data vector, wherein the label data represents the weight corresponding to each head in the multi-head attention operation module.
[0066] S13, using the input construction data and the corresponding label data to train the constructed pruned small model to obtain model parameters of the trained pruned small model.
[0067] S14, merging the model parameters of the trained pruned small model with the model parameters of the natural language model to obtain a natural language model with a pruning function. The model parameters of the pruned small model included in the natural language model with a pruning function are used to prune multiple heads in the natural language model when processing the data to be processed.
[0068] The pruning method of the natural language model provided in this embodiment mainly includes two stages, an early stage of obtaining a natural language model with a pruning function, and a later stage of reasoning based on the natural language model with a pruning function. The following first introduces the stage of obtaining a natural language model with a pruning function.
[0069] Please refer to Figure 2 The stage of obtaining a natural language model with pruning function mainly includes the processes of training data construction, pruning small model construction and model parameter merging. Among them, training data construction refers to the construction of data for training the pruning small model, which is called input construction data in this embodiment. The data input into the natural language model is called training data.
[0070] In this embodiment, the training data may be paragraph text or dialogue question and answer data, etc., and may also be voice data, which may be converted into text data by voice recognition. The natural language model used may be a currently open source large model, such as llama, chatglm, baichuan, qianwen, opt, etc.
[0071] Import each training data into the natural language model. Please refer to Figure 3 , each training data passes through the embedding layer at the front end and then outputs the data vector corresponding to each training data. The core module of the module layer group of the natural language model is the multi-head attention operation module, which includes multiple heads, each of which can learn different representations from the training data.
[0072] The data vectors corresponding to each training data are processed by the module layer group and the corresponding input structured data and the label data corresponding to the input structured data are output. Among them, the input structured data is the data constructed for training the pruned small model, and the label data is the weight of each head in the multi-head attention operation module under each input structured data. That is, in the input structured data corresponding to different training data, the weights of each head represented by the label data are different. Among them, the weight can be a value from 0 to 1. When the weight is 0, the corresponding head and its related model parameters are not involved in the reasoning process.
[0073] The obtained input structured data is used to train the pruned small model, and the model parameters of the pruned small model are adjusted by comparing the label data corresponding to the input structured data and the output results of the pruned small model. When certain requirements are met, a trained pruned small model is obtained.
[0074] In this embodiment, the model parameters of the trained pruned small model are obtained, and the model parameters of the pruned small model are merged with the model parameters of the natural language model, that is, the model parameters of the pruned small model are merged into the model parameters of the natural language model, and the merged natural language model can have a pruning function.
[0075] In the pruning processing method of the natural language model provided in this embodiment, the pruned small model is processed based on the input structure data and label data obtained through the natural language model processing, so that the pruned small model can obtain different weights when pruning each head for different data. The effect of dynamically pruning the head according to the input data is achieved, avoiding the defect of the static pruning method that it is not suitable for some data and leads to poor reasoning effect.
[0076] In addition, in this embodiment, the model parameters of the pruned small model are merged into the model parameters of the natural language model to construct a natural language model of a new architecture, thereby ensuring the integrity of the natural language model. The model parameters of the pruned small model are merged with the natural language model, and can be used as a matrix for inference calculations later, which can make full use of the parallel processing capabilities of the GPU and avoid the defect in the prior art that the small model and the large model need to be calculated separately, which leads to a long inference time.
[0077] Please refer to Figure 3 In this embodiment, the module layer group includes a plurality of module layers connected in sequence (such as Figure 3 Block-1 to Block-N in ), each of the module layers includes a multi-head attention operation module (such as Figure 3 Multi-head-attention in Figure 3 The multi-head attention operation module includes a normalization layer (RMSNorm), a first fully connected layer (Linear), and a multi-head attention calculation layer connected in sequence. In addition, the multi-head attention operation module also includes a second fully connected layer (Linear) connected after the multi-head attention calculation layer.
[0078] In this embodiment, when the data vectors corresponding to each training data are obtained through the embedding layer, assuming that the training data is "How are you?", the training data can first be encoded into tokens, and each token is converted into a vector after passing through the embedding layer.
[0079] Please refer to Figure 4 , for the training data "How are you?", after conversion to tokens, the conversion is as follows Figure 4 The data vector shown in . The data vector includes three vectors, and the length of each vector may be 4096. Therefore, the size of the data subsequently input to the first module layer of the module layer group is (3, 4096).
[0080] On this basis, the step of obtaining the corresponding input structure data and the label data corresponding to the input structure data through the module layer group and based on the data vector in the above step S12 can be implemented in the following way:
[0081] For each of the multiple module layers, the input data vector or the input construction data transmitted by the previous module layer is processed based on the normalization layer in the module layer, and the processed data passes through the first fully connected layer in the module layer to obtain the corresponding input construction data containing multiple construction data blocks.
[0082] Each constructed data block in the input constructed data is divided into multiple data sub-blocks through the multi-head attention calculation layer in the module layer, and the multiple divided constructed data blocks are fused to obtain the label data corresponding to the input constructed data.
[0083] The number of data sub-blocks obtained by dividing each construction data block is the same as the number of heads in the multi-head attention calculation layer. When the multiple construction data blocks after division are fused to obtain the label data corresponding to the input construction data, the multiple construction data blocks after division can be fused to obtain a fused data block containing multiple data sub-blocks. The bi-norm calculation is performed on each data sub-block in the fused data block to obtain the bi-norm result, and the label data corresponding to the input construction data is obtained based on the bi-norm result of each data sub-block. The bi-norm result of each data sub-block represents the weight of the head corresponding to each data sub-block.
[0084] Please refer to Figure 3 , taking the above as an example, each module layer in the module layer group mainly includes a multi-head attention operation module (Multi-head-attention) and a multi-layer perceptron (MLP). The multi-head attention operation module and the multi-layer perceptron both use residual operations after calculation.
[0085] The data vector obtained after the embedding layer (Embedding) enters the first module layer (Block-1), and the size of the data vector entering the first module layer is (3, 4096). In the first module layer, the data vector is normalized by the normalization layer (RMSNorm), and the size of the processed data vector is (3, 4096). The processed data vector enters the first fully connected layer (Linear) for processing, and the input structure data obtained can be expressed as follows Figure 3 In the first fully connected layer, based on the function Y=X W+B processes the data vector X, where W and B are network parameters of the natural language model, the size of W is (4096, 12288), and the size of B is (12288). The size of Y obtained after the data vector X is processed is (3, 12288). The input constructed data obtained includes three constructed data blocks obtained by equally dividing Y. The three constructed data blocks are represented as Q, K, and V respectively, and the size of each constructed data block is (3, 4096).
[0086] After completing the construction of the input structured data, it is also necessary to obtain the label data corresponding to each input structured data. The label data is used to characterize the weight of each head in the multi-head attention calculation layer under each input structured data.
[0087] The input constructed data enters the multi-head attention calculation layer, and each constructed data block in the input constructed data is divided into a corresponding number of data sub-blocks based on the number of heads in the multi-head attention calculation layer. For example, when the natural language model adopts the qianwen-7B model, the number of heads in the multi-head attention calculation layer is 32, then the above Q, K, and V can be divided into 32 data sub-blocks respectively, and the size of each constructed data block can be represented as (32, 3, 128).
[0088] The multiple constructed data blocks after the above division are fused, and the obtained fused data block is represented as U, and the size of U is (32, 3, 128).
[0089] On this basis, the Figure 3 The Norm in performs a two-norm calculation on the last dimension of the fused data block U, which is equivalent to converting the last dimension of the fused data block U into a two-norm result representing the weight. After the two-norm calculation, the size of the fused data block is (32, 3).
[0090] After the dimension is swapped by Transpose, the label data is obtained, and the size of the label data is (3, 32).
[0091] Through the above process, an input structured data with a size of (3, 4096) is processed by the multi-head attention calculation layer to obtain label data with a size of (3, 32), where 3 represents the number of input tokens and 32 represents the number of heads.
[0092] After the input structured data and label data are obtained through the first module layer of the natural language model, the input structured data of the first module layer enters the second module layer for processing. The processing method of the second module layer is the same as that of the first module layer. The difference is that the input of the first module layer is the data vector output by the embedding layer, while the input of the second module layer is the input structured data output by the first module layer.
[0093] Finally, each module layer can output the input structured data and the corresponding label data. The constructed pruned small model is trained based on the input structured data and label data.
[0094] In this embodiment, the constructed pruned small model is a single-layer network model, specifically, the pruned small model includes a fully connected layer. Compared with the double-layer network model, the single-layer network model has fewer parameters and less calculation. In order to verify the classification effect of the single-layer network model constructed in this embodiment and the existing commonly used double-layer network model (such as the Predictor solution provided by Deja Vu) on the head, in this embodiment, the constructed pruned small model is pre-merged into the natural language model, such as the qianwen-7B large model for recall testing, and the test results after the existing double-layer network model is merged into the natural language model are compared.
[0095] like Figure 5 As shown in , the recall rate of the test data after the pruned small model constructed in this embodiment is merged into the natural language model ( Figure 5 ), and the recall rate of test data after the existing two-layer network model is merged into the natural language model ( Figure 5 ). Among them, Figure 5 Block-N in the figure represents the 32 heads of the natural language model. Figure 5 It can be seen that the recall rate of the pruned small model constructed in this embodiment is higher, indicating that its recall effect is better.
[0096] It can be seen that the pruned small model constructed in this embodiment can ensure the effect of head classification with fewer parameters.
[0097] On the basis of the above, the constructed pruned small model is trained based on the obtained input structured data and label data.
[0098] See also Figure 2,The stage of training the pruned small model mainly includes data ,processing, small model structure design, model training and small model parameter storage.
[0099] The data processing part is mainly responsible for collecting the input structure data and label data obtained above and making them into training data for the pruned small model. For example, the original training data is "How are you?". After being processed by each module layer of the natural language model, the input structure data input (size is (3, 4096)) and label data label (size is (3, 32)) are obtained. In the data processing part, it means that 3 data are received, and the input length of each data is 4096. After the subsequent pruned small model, the output length is 32.
[0100] In implementation, more training data can be used as input in the training data construction process to construct a large amount of input construction data as training data for the pruned small model. These data are divided into training sets and test sets according to a certain ratio for training the pruned small model.
[0101] In the small model structure design part, the network structure of the pruned small model is mainly designed. The pruned small model includes a fully connected layer, where Figure 6 As shown in , the number of input neurons Xs is 4096, the number of output neurons Ys is 32, and the size of the network parameter (weight parameter) Ws is (4096, 32).
[0102] In the model training part, the constructed pruned small model is mainly trained using the input structure data and the corresponding label data. The pruned small model can be trained using the input structure data and label data output by each module layer. For example, when the module layer data is 32, 32 trained pruned small models can be obtained. The training method for each pruned small model is the same, and specifically, it can be achieved in the following ways:
[0103] The input structured data is imported into the constructed pruned small model for processing to obtain the output data. Based on the label data and output data corresponding to the input structured data, and under the guidance of the constructed loss function, the model parameters of the pruned small model are adjusted until the preset stop condition is met, and the training is stopped.
[0104] In this embodiment, the loss function used when training the pruned small model can be, but not limited to, cross entropy, and the optimizer can be, but not limited to, Adaw. The training of the pruned small model includes multiple iterations. After each iteration, the loss function is used to combine the label data and the output data to obtain the loss value, and the model parameters of the pruned small model are adjusted to enter the next round of iteration. The preset stop condition can be that the iteration time reaches the preset maximum time, or the number of iterations reaches the preset maximum number, or the loss function reaches convergence and no longer changes, etc.
[0105] After completing the training of the pruned small model, the model parameters of the trained pruned small model are stored in the small model parameter storage part, and the stored model parameters are used to merge with the model parameters of the natural language model.
[0106] In this embodiment, when the model parameters of the pruned small model are merged into the model parameters of the natural language model, the model parameters of the pruned small model are merged into the model parameters of the first fully connected layer of each module layer of the natural language model to obtain a natural language model with pruning function.
[0107] For example, when there are 32 trained pruned small models and the number of module layers is 32, the pruned small models can be matched one-to-one with the module layers, and the model parameters of the corresponding pruned small models can be merged with the model parameters of the first fully connected layer of the module layer.
[0108] Specifically, the merging process of model parameters can be achieved in the following ways:
[0109] The model parameters of each pruned small model that has been trained are padded; the model parameters of each pruned small model after the padded operation are merged with the model parameters of the first fully connected layer in the corresponding module layer.
[0110] Please refer to Figure 7 When merging the model parameters of the pruned small model into the model parameters of the natural language model, specifically, the model parameters Ws of the pruned small model are merged with the model parameters of the first fully connected layer in the corresponding module layer of the natural language model. The model parameters of the first fully connected layer include two parts, W and B, where the size of W is (4096, 12288) and the size of B is (12288). W and Ws are merged to obtain a new model parameter Wx, whose size is (4096, 12320), represented by Wx=concat(W,Ws).
[0111] Since the model parameters of the pruned small model do not use bias, the model parameters of the pruned small model can be padded. Specifically, the bias Bs can be set, the size of Bs is (32), and Bs is a vector of all 0s. Bs is merged with B to obtain a new model parameter Bx, whose size is (12320), represented by Bx=concat(B,Bs).
[0112] After the model parameters are merged, the model parameters of the first fully connected layer of each module layer of the natural language model are changed from the original Y=X W+B is updated to Y=X Wx+Bx.
[0113] In this embodiment, based on the above method, the training data is processed by the natural language model to obtain input structure data and label data, and the label data can represent the weights of each head of the multi-head attention operation module in the natural language model. On this basis, the pruned small model is trained based on the input structure data and label data, so that the trained pruned small model can determine the weights of each head of the multi-head attention operation module for different inputs, and then after the model parameters of the pruned small model are merged into the natural language model, the natural language model can have a pruning processing function. The natural language model can be used to process the data to be processed in the application stage.
[0114] The following is an introduction to the inference phase of a natural language model with pruning capabilities. Figure 3 In this embodiment, the natural language model also includes a classification layer (such as Figure 3 Linear on the upper right of the center). See Figure 8 The pruning method of the natural language model provided in this embodiment may also include the following steps:
[0115] S15, obtaining the data to be processed, and importing the data to be processed into the natural language model with the pruning function.
[0116] S16, for every two adjacent module layers in the natural language model with pruning function, the previous module layer processes the data input to the module layer to obtain output data and head weights, and inputs the output data and head weights to the next module layer, and the head weights are used by the next module layer to prune the heads in its multi-head attention operation module.
[0117] S17, obtaining output data of the last module layer, and obtaining response information of the data to be processed based on the output data using the classification layer.
[0118] Please refer to Figure 2 and Fig. 9 , Fig.10 , Figure 2 The inference text data in is the data to be processed, such as Fig. 9 As shown in , when reasoning is performed based on a traditional unoptimized natural language model, the natural language model will be processed by each module layer that has not been pruned, including Block1, Block2, ..., Block-32, a total of 32 module layers. Fig.10 As shown in , when processing based on the optimized natural language model with pruning function in this embodiment, each module layer can obtain the head weight of each head in addition to the output data, such as Fig.10 The head-prop in each module layer is used by the next module layer to prune the head in the multi-head attention operation module. The head weight is mainly calculated based on the part of the pruned small model merged in the natural language model.
[0119] It should be noted that the cropping of the head can start from the second module layer and crop the head in each subsequent module layer, or it can start from the second module layer and select a part of the subsequent module layers for head cropping. Specifically, this embodiment does not limit this.
[0120] It can be seen that the head weight output by the last module layer is useless because there is no module layer after it. Based on this, when merging the model parameters of each pruned small model into the model parameters of each module layer, the last module layer may or may not be merged, and this embodiment does not limit this.
[0121] by Fig.10 As an example, the head weights obtained in the Block1 stage will be input into Block2, where they are used to prune multiple heads. The heads corresponding to the head weights of 0 and their corresponding model parameters will be pruned and will not participate in reasoning. The head weights obtained in the Block2 stage will be input into Block3, where they are used to prune heads, and so on.
[0122] Until reaching the last module layer, such as Block 32, in the last module layer, the output data and header weight transmitted by the previous module layer are obtained. Based on the header weight, multiple headers are clipped, and after the clipping, the output data transmitted by the previous module layer is processed to obtain the output data of the module layer. Based on the output data of the last module layer, the response information of the data to be processed can be obtained through the classification layer.
[0123] Specifically, in this embodiment, based on the processing result of the classification layer on the output data (the last module layer), the matching degree between the data to be processed and each of the preset multiple entries is obtained; the entries with the matching degree ranked in the front preset position are extracted to form the response information of the data to be processed, and the response information is output.
[0124] In question-and-answer application scenarios, the data to be processed is generally a question input by the user, and the answer corresponding to the question needs to be output based on the natural language model. Based on the previous training, the natural language model can have the function of semantic analysis of the data to be processed, and can process the data to be processed to obtain the matching degree between the data to be processed and each of the preset multiple terms. In this way, the response information of the data to be processed can be formed based on the matching degree of each term.
[0125] In this embodiment, the optimized natural language model is used for reasoning, which can greatly reduce the amount of calculation of the model. For example, each Block in the Qianwen-7B model contains 32 heads. When the head halving scheme is adopted, the most effective 16 heads will be found, and the remaining heads will be deleted and will not participate in the calculation. At the same time, the model parameters used to calculate the deleted heads will also be deleted at the same time to reduce the amount of calculation of the Linear layer in the Block.
[0126] Take the input data "How are you?" as an example. The number of input tokens is 3. Before the natural language model is pruned and optimized, the computational cost of solving the Linear layer of the QKV of the Block for the three tokens is 3x4096x12288 multiplications and 3x4096x12288 additions. After the natural language model is pruned and optimized, the computational cost of Block1 is 3x4096x(12288+32) multiplications and 3x4096x(12288+32) additions. If the head is pruned in half, starting from Block2, since half of the neurons and weight parameters are pruned, the computational cost will become 3x4096x(12288 / 2+32) multiplications and 3x4096x(12288 / 2+32) additions. The total amount of computation required to solve the Linear problem of QKV for all blocks has been reduced from approximately 9.7 billion to approximately 5 billion, with a significant reduction in the amount of computation, which is reduced to approximately 52% of the previous amount.
[0127] In addition, Multi-attention calculation is performed for each head. In the Block1 stage, the number of heads remains unchanged before and after optimization. Assuming that the Multi-attention calculation amount corresponding to Block1 is Kb, the calculation amount of 32 Blocks before inference optimization is 32xKb. After inference optimization, starting from Block2, the Multi-attention calculation amount of each subsequent Block is Kb / 2, and the Multi-attention calculation amount of 32 Blocks is Kb+31x(Kb / 2)=16.5xKb+16.5. Since Kb is usually much larger than 16.5, its calculation amount is about 16.5xKb, which is reduced to about 52% of the previous amount.
[0128] In order to verify the effect change after model pruning, the PPL perplexity evaluation of the model after pruned Head is performed using the Wiki dataset. The evaluation results are as follows: Fig.11 As shown in . Among them, Fig.11 In the figure, ACC is PPL before pruning / PPL after pruning. PPL before pruning does not change, ACC is 1, and when the number of pruned heads N is small, the accuracy basically does not change. When the number of pruned heads reaches 16, the accuracy still reaches 92% of the original model. It can be seen that after pruning the natural language model, the amount of calculation can be greatly reduced while still maintaining a high accuracy.
[0129] The pruning method of the natural language model provided in this embodiment merges the model parameters of the trained pruned small model into the model parameters of the natural language model, and changes the architecture of the natural language model to ensure the integrity of the model. This avoids the large amount of inference costs caused by the need for separate inference by using a method of loading multiple small models separately. The parallel processing capabilities of the GPU can be fully and efficiently utilized.
[0130] In addition, a small pruning model of a single-layer network is designed for dynamic pruning, which can adaptively find the optimal head according to different inputs each time, avoiding the problem that the same pruning strategy is used for all data in the static pruning method, causing some data to be unsuitable, thereby resulting in poor results. In this embodiment, the pruned natural language model is more robust.
[0131] In this embodiment, by pruning the head of the natural language model and pruning the weight parameters of the Linear layer in the natural language model calculation QKV, the computational complexity of the model can be greatly reduced, and the inference cost of the large model can be reduced. When the head is halved, the computational complexity of the Linear layer and the multi-head attention operation part can be reduced to 52% before pruning, and the accuracy can reach 92% of the original model.
[0132] Based on the same inventive concept, please refer to Fig.12 , shows a functional module schematic diagram of a pruning processing system for a natural language model provided by an embodiment of the present invention. This embodiment can divide the functional modules of the pruning processing system for a natural language model according to the above method embodiment. For example, each functional module can be divided according to each function, or two or more functions can be integrated into one processing module. The above integrated module can be implemented in the form of hardware or in the form of software functional modules.
[0133] It should be noted that the division of modules in the embodiments of the present invention is schematic and is only a logical function division. There may be other division methods in actual implementation. For example, in the case of dividing each functional module according to each function, Fig.12 The pruning processing system of the natural language model shown is only a system schematic diagram. The pruning processing system of the natural language model may include an acquisition module, a processing module, a training module and a merging module. The functions of each functional module of the pruning processing system of the natural language model are described in detail below.
[0134] An acquisition module, used for acquiring a plurality of training data and importing each of the training data into the natural language model;
[0135] A processing module, configured to obtain a data vector corresponding to each of the training data through the embedding layer, obtain corresponding input structure data and label data corresponding to the input structure data through the module layer group and based on the data vector, wherein the label data represents a weight corresponding to each head in the multi-head attention operation module;
[0136] A training module, used to train the constructed pruned small model using the input construction data and the corresponding label data to obtain model parameters of the trained pruned small model;
[0137] A merging module is used to merge the model parameters of the trained pruned small model with the model parameters of the natural language model to obtain a natural language model with a pruning function, wherein the model parameters of the pruned small model contained in the natural language model with a pruning function are used to prune multiple heads in the natural language model when processing the data to be processed.
[0138] It can be understood that the above-mentioned acquisition module, processing module, training module and merging module can be used to execute the above-mentioned S11 to S14. The detailed implementation method of the acquisition module, processing module, training module and merging module can refer to the above-mentioned contents related to the above-mentioned S11 to S14.
[0139] The pruning processing system of the natural language model provided in this embodiment can execute the method described in any of the implementation modes in the above embodiments, which will not be described one by one here. In addition, the technical effect of the pruning processing system in this embodiment can refer to the technical effect of the method described in any of the implementation modes in the above embodiments, which will not be described here.
[0140] See also Fig.13 , is a structural block diagram of an electronic device provided in an embodiment of the present invention, the electronic device may be a computer device, a server, etc., and the electronic device includes a storage medium, a processor, and a communication module. The storage medium, the processor, and the communication module are electrically connected to each other directly or indirectly to achieve data transmission or interaction. For example, these components can be electrically connected to each other via one or more communication buses or signal lines.
[0141] The storage medium is used to store programs or data. The storage medium may be, but is not limited to, random access memory (RAM), read only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), and electrically erasable programmable read-only memory (EEPROM).
[0142] The processor is used to read / write data or programs stored in the storage medium and execute the pruning method of the natural language model provided by any embodiment of the present invention.
[0143] The communication module is used to establish a communication connection between the electronic device and other communication terminals through the network, and is used to send and receive data through the network.
[0144] It should be understood that Fig.13 The structure shown is only a schematic diagram of the structure of the electronic device. The electronic device may also include Fig.13 More or fewer components as shown, or with Fig.13 Different configurations are shown.
[0145] Furthermore, an embodiment of the present invention also provides a computer-readable storage medium, which stores machine-executable instructions. When the machine-executable instructions are executed, the pruning method of the natural language model provided in the above embodiment is implemented.
[0146] Specifically, the computer-readable storage medium can be a general storage medium, such as a mobile disk, a hard disk, etc. When the computer program on the computer-readable storage medium is executed, the above-mentioned natural language model pruning processing method can be executed. Regarding the process involved when the machine executable instructions in the computer-readable storage medium are executed, reference can be made to the relevant description in the above-mentioned method embodiment, which will not be described in detail here.
[0147] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A pruning method for a natural language model, characterized in that: The natural language model includes an embedding layer and a module layer group connected in sequence, the module layer group includes a multi-head attention operation module, the multi-head attention operation module includes multiple heads, and the method includes: Acquire multiple training data, and import each of the training data into the natural language model, wherein the training data is paragraph text or dialogue question and answer data; Obtaining a data vector corresponding to each of the training data through the embedding layer, obtaining corresponding input structure data and label data corresponding to the input structure data through the module layer group and based on the data vector, wherein the label data represents the weight corresponding to each head in the multi-head attention operation module; The constructed pruned small model is trained using the input construction data and the corresponding label data to obtain model parameters of the trained pruned small model; The model parameters of the trained pruned small model are merged with the model parameters of the natural language model to obtain a natural language model with a pruning function, wherein the model parameters of the pruned small model contained in the natural language model with a pruning function are used to prune multiple heads in the natural language model when processing the data to be processed.
2. The pruning method of a natural language model according to claim 1, characterized in that: The module layer group includes a plurality of module layers connected in sequence, each of the module layers includes a multi-head attention operation module and a multi-layer perceptron connected in sequence, and the multi-head attention operation module includes a normalization layer, a first fully connected layer and a multi-head attention calculation layer connected in sequence; The model parameters of the pruned small model are merged into the model parameters of the first fully connected layer of each module layer to obtain a natural language model with pruning function.
3. The pruning method of a natural language model according to claim 2, characterized in that: The step of obtaining corresponding input structure data and label data corresponding to the input structure data through the module layer group and based on the data vector comprises: For each of the multiple module layers, based on the normalization layer in the module layer, the input data vector or the input construction data transmitted by the previous module layer is processed, and the processed data passes through the first fully connected layer in the module layer to obtain the corresponding input construction data containing multiple construction data blocks; Each construction data block in the input construction data is divided into multiple data sub-blocks through the multi-head attention calculation layer in the module layer, and the multiple divided construction data blocks are fused to obtain the label data corresponding to the input construction data.
4. The pruning method of a natural language model according to claim 3, characterized in that: The number of data sub-blocks obtained by dividing each of the constructed data blocks is the same as the number of heads in the multi-head attention calculation layer; The step of fusing the divided multiple construction data blocks to obtain label data corresponding to the input construction data includes: The divided multiple constructed data blocks are merged to obtain a merged data block containing multiple data sub-blocks; A binomial calculation is performed on each data sub-block in the fused data block to obtain a binomial result, and label data corresponding to the input constructed data is obtained based on the binomial result of each data sub-block, wherein the binomial result of each data sub-block represents the weight of the header corresponding to each data sub-block.
5. The pruning method of a natural language model according to claim 2, characterized in that: The number of trained pruned small models is the same as the number of module layers, and the step of merging the model parameters of the trained pruned small models with the model parameters of the natural language model includes: Performing a completion operation on the model parameters of each of the pruned small models that have completed training; The model parameters of each of the pruned small models after the padding operation are merged with the model parameters of the first fully connected layer in the corresponding module layer.
6. The pruning method of a natural language model according to claim 1, characterized in that: The step of training the constructed pruning model using the input structured data and the corresponding label data includes: Importing the input structured data into the constructed pruning mini-model for processing to obtain output data; Based on the label data and output data corresponding to the input construction data, and under the guidance of the constructed loss function, the model parameters of the pruned small model are adjusted until the preset stopping condition is met, and the training is stopped.
7. The pruning method of a natural language model according to claim 2, characterized in that: The natural language model further includes a classification layer connected after the module layer group, and the method further includes: Acquire data to be processed, and import the data to be processed into the natural language model with pruning function; For every two adjacent module layers in the natural language model with pruning function, the previous module layer processes the data input to the module layer to obtain output data and head weights, and inputs the output data and head weights to the next module layer, and the head weights are used for the next module layer to perform pruning processing on the heads in its multi-head attention operation module; The output data of the last module layer is obtained, and the response information of the data to be processed is obtained based on the output data using the classification layer.
8. The pruning method of a natural language model according to claim 7, characterized in that: The step of obtaining response information of the data to be processed based on the output data by using the classification layer includes: Obtaining a matching degree between the data to be processed and each of the preset multiple entries based on the processing result of the output data by the classification layer; Extract the terms with the highest matching degree in a preset position to form the response information of the data to be processed, and output the response information.
9. A natural language model pruning processing system, characterized in that: The natural language model includes an embedding layer and a module layer group connected in sequence, the module layer group includes a multi-head attention operation module, the multi-head attention operation module includes multiple heads, and the system includes: An acquisition module, used to acquire multiple training data and import each of the training data into the natural language model, wherein the training data is paragraph text or dialogue question and answer data; A processing module, configured to obtain a data vector corresponding to each of the training data through the embedding layer, obtain corresponding input structure data and label data corresponding to the input structure data through the module layer group and based on the data vector, wherein the label data represents a weight corresponding to each head in the multi-head attention operation module; A training module, used to train the constructed pruned small model using the input construction data and the corresponding label data to obtain model parameters of the trained pruned small model; A merging module is used to merge the model parameters of the trained pruned small model with the model parameters of the natural language model to obtain a natural language model with a pruning function, wherein the model parameters of the pruned small model contained in the natural language model with a pruning function are used to prune multiple heads in the natural language model when processing the data to be processed.
10. An electronic device, characterized in that: The device is characterized in that it includes one or more storage media and one or more processors communicating with the storage media, wherein the one or more storage media store machine executable instructions executable by the processors, and when the electronic device is running, the processor executes the machine executable instructions to execute the method described in any one of claims 1 to 8.
Citation Information
Patent Citations
Adaptive marking and attention pruning method for visual language model
CN117787342A
Multi-modal melanoma immunotherapy prediction method based on deep learning network
CN117936105A
