LoRA fine tuning method based on parameter-free attention module
By introducing a parameterless attention module and a low-rank separable matrix into the pre-training model, the deficiencies in flexibility and efficiency of fine-tuning methods in the existing technology in a multi-task learning environment are solved, and better semantic information acquisition and Performance improvements.
Patent Information
- Application Number
- CN202410598355.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-15
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2044-05-15
AI Technical Summary
Existing fine-tuning pre-training model methods lack flexibility and efficiency in multi-task learning environments, and are difficult to adapt to complex downstream tasks.
The LoRA fine-tuning method based on the parameter-free attention module is used to set the parameter-free attention module in each layer of the pre-trained model, and calculate the linear separability between the target neuron and surrounding neurons to adjust the weight and realize the attention mechanism. , and add a low-rank separable matrix to incrementally update the weights during the fine-tuning process.
It improves the model's ability to acquire semantic information in natural language processing, improves task adaptability and performance, and does not increase inference time.
Smart Images

Figure CN118569343A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of pre-training model parameter fine-tuning, and more specifically, to a LoRA fine-tuning method based on a parameter-free attention module. Background Art
[0002] Generative pre-trained models have revolutionized the field of natural language processing, providing a powerful methodology for handling language understanding and generation tasks. These models learn rich language representations by pre-training on large-scale corpora, capturing complex structures at the lexical, syntactic, and semantic levels. In particular, the GPT series of models have attracted widespread attention for their outstanding performance on a variety of NLP tasks. Its design based on autoregressive mechanisms and Transformer architecture achieves high efficiency and high accuracy in prediction tasks.
[0003] There are several important techniques for fine-tuning pre-trained models to adapt to specific tasks. Among them, Adapter, LoRA, and Prefix / Prompt are three commonly used ideas. They all play a key role in improving model adaptability and performance. LoRA is a popular method that adds two trainable bypass matrices, also known as low-rank separable matrices, to the specified layer. This method allows the model to adapt to specific tasks by learning additional parameters. These matrices are usually designed to be low-rank, so that significant adjustments to the model can be achieved while adding a very small number of parameters without retraining the entire model.
[0004] Although these fine-tuning methods have achieved significant progress in model adaptability and performance, they are generally only applicable to simple downstream tasks. In actual application scenarios, it is often a multi-task learning environment, which requires more flexible and efficient fine-tuning methods. Therefore, further research and innovation are still necessary. Summary of the invention
[0005] The purpose of the present invention is to propose a LoRA fine-tuning method based on a parameter-free attention module in view of the shortcomings of the prior art.
[0006] In the first aspect, a LoRA fine-tuning method based on a parameter-free attention module is provided, including:
[0007] S1. Obtain a pre-trained model and model parameters, and set a parameter fine-tuning module in each layer of the pre-trained model to obtain a model to be fine-tuned; the parameter fine-tuning module is a LoRA model with a parameter-free attention module;
[0008] S2. Obtain a fine-tuning dataset, and preprocess the fine-tuning dataset;
[0009] S3, loading and freezing the parameters of the pre-trained model, and initializing the parameters of the parameter fine-tuning module;
[0010] S4. Fine-tune the parameters of the parameter fine-tuning module based on the fine-tuning dataset to obtain a fine-tuned large language model.
[0011] Preferably, S2 comprises:
[0012] S201, setting a specific prompt structure and content for the data set according to different data set tasks, and editing the label result of the data set to modify it into text form;
[0013] S202: shuffle different data set tasks and merge them into a large data set.
[0014] Preferably, S4 includes:
[0015] S401: Perform forward propagation calculations on the pre-trained model and parameter fine-tuning module based on the fine-tuning data set to obtain the result Result base_model and Result lora ;
[0016] S402, Result base_model Add Result lora As the output of the pre-trained model, generate results;
[0017] S403. Calculate the loss based on the cross entropy, and perform back propagation based on the loss.
[0018] Preferably, in S1, the parameter-free attention module calculates the linear separability between the target neuron and its surrounding neurons to obtain the importance of each neuron, and then uses the importance to adjust the weight of the neuron to implement the attention mechanism; the following energy function is defined for each neuron:
[0019]
[0020] where s′=w s s+b s And x′ i =w s x i +b s , where s refers to the target neuron, x i refers to other neurons, w s and b s It is s and x i The weight and bias of the transformation, L is the sequence length, λw s 2 is the regularization matrix;
[0021] The parameter-free attention module is expressed as:
[0022]
[0023] Among them, the sigmoid function is used to limit E s ; X is the input sequence, ⊙ represents the dot product.
[0024] Preferably, in the expression of the energy function, when s′ is equal to y, and all other x′ i When y0 is equal to y0, the equation reaches the minimum value, where y and y0 are two different values; by minimizing this equation, it is equivalent to finding the linear separability between the target neuron s and all other neurons in the same channel; for simplicity, binary labels are used to represent y and y0, and the final expression of the energy function is:
[0025]
[0026] In addition, there is a fast closed-form solution for the above formula:
[0027]
[0028]
[0029] in and Since the mean and variance calculated by all neurons in the formula are obtained on a single sequence, it is reasonable to assume that all pixels in a single channel follow the same distribution. Given this assumption, the mean and variance can be calculated on all neurons and reused in all neurons on that channel; after substituting the above formula into it, we get:
[0030]
[0031] in represents the variance of the input, represents the mean of the input, t represents the target neuron, and λ is a hyperparameter; the greater the gap between the target object and other objects, the more important the information contained in the object
[0032] Preferably, in S401, the hidden layer information h is expressed as follows after the parameter fine-tuning module is loaded:
[0033] h=W0X+BA*TkLoRA(X)
[0034] Among them, W0 is the initial weight of pre-training, X is the input sequence, and BA is the two low-rank matrices obtained by the LoRA model by disassembling the change △W0 of the initialization weight W0.
[0035] Preferably, in S1, the pre-trained model is a pre-trained large language model structure.
[0036] In a second aspect, a LoRA fine-tuning system based on a parameter-free attention module is provided, which is used to execute any method described in the first aspect, including:
[0037] The first acquisition module is used to obtain a pre-trained model and model parameters, and set a parameter fine-tuning module in each layer of the pre-trained model to obtain a model to be fine-tuned; the parameter fine-tuning module is a LoRA model with a parameter-free attention module;
[0038] A second acquisition module is used to acquire a fine-tuning data set and pre-process the fine-tuning data set;
[0039] A loading module, used to load and freeze the parameters of the pre-trained model and initialize the parameters of the parameter fine-tuning module;
[0040] The fine-tuning module is used to fine-tune the parameters of the parameter fine-tuning module based on the fine-tuning dataset to obtain a fine-tuned large language model.
[0041] In a third aspect, a computer storage medium is provided, in which a computer program is stored; when the computer program is run on a computer, the computer executes any LoRA fine-tuning method based on a parameter-free attention module as described in the first aspect.
[0042] In a fourth aspect, an electronic device is provided, comprising a storage medium, a processor, and instructions or codes stored on the storage medium and executable on the processor, wherein when the processor executes the instructions or codes, the LoRA fine-tuning method based on the parameter-free attention module as described in any one of the first aspects is implemented.
[0043] The beneficial effects of the present invention are as follows: the parameters trained by the present invention are only the modules of LoRA, and there is no need to fine-tune all parameters of the diffusion model. Moreover, the present invention is modified on the basis of LoRA, and a parameter-free attention module is added before the LoRA parameters, so that LoRA can better acquire the semantic information of natural language in learning, and has a better effect; at the same time, the module is simple and will not increase the reasoning time of the module. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] Figure 1 This is a flowchart of a LoRA fine-tuning method based on a parameter-free attention module;
[0045] Figure 2 This is a schematic diagram of LoRA with a parameter-free attention module added;
[0046] Figure 3Schematic diagram of the structure of a LoRA fine-tuning system based on a parameter-free attention module. DETAILED DESCRIPTION
[0047] The present invention is further described below in conjunction with embodiments. The description of the following embodiments is only used to help understand the present invention. It should be noted that for ordinary persons in the art, without departing from the principle of the present invention, the present invention can also be modified in some ways, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.
[0048] Embodiment 1:
[0049] The present application provides a LoRA fine-tuning method based on a parameter-free attention module, which can be applied to electronic devices with the ability to run instructions or programs. Among them, the electronic devices can be, but are not limited to, various servers, workstations, personal computers, laptops, etc. Running on different computing devices is only a difference in the execution subject of the scheme. People in this field can foresee that running in different computing devices can produce the same technical effects. The present invention is described in detail below through specific embodiments.
[0050] Figure 1 A flow chart of a LoRA fine-tuning method based on a parameter-free attention module is provided for an embodiment of the present application, comprising the following steps:
[0051] S1. Obtain a pre-trained model and model parameters, and set a parameter fine-tuning module in each layer of the pre-trained model to obtain a model to be fine-tuned; the parameter fine-tuning module is a LoRA model with a parameter-free attention module.
[0052] In S1, the parameter-free attention module calculates the linear separability between the target neuron and its surrounding neurons to obtain the importance of each neuron, and then uses the importance to adjust the weight of the neuron to implement the attention mechanism; the following energy function is defined for each neuron:
[0053]
[0054] where s′=w s s+b s And x′ i =w s x i +b s , where s refers to the target neuron, x i refers to other neurons, w s and b s It is s and x i The weight and bias of the transformation, L is the sequence length, is the regularization matrix. When s′ is equal to y, and all other x′i When y0 is equal to y0, the equation reaches a minimum, where y and y0 are two different values. By minimizing this equation, it is equivalent to finding the linear separability between the target neuron s and all other neurons in the same channel. For simplicity, binary labels (i.e., 1 and -1) are used to represent y and y0, so the final expression of the energy function is:
[0055]
[0056] In addition, there is a fast closed-form solution for the above formula:
[0057]
[0058]
[0059]
[0060]
[0061] Since the mean and variance calculated by all neurons in the formula are obtained on a single sequence, it is reasonable to assume that all pixels in a single channel follow the same distribution. Given this assumption, the mean and variance can be calculated on all neurons and reused in all neurons on that channel. Therefore, after substituting the above formula into the formula, we get:
[0062]
[0063] From the above formula, we can find that the greater the gap between the target object and other objects, the more important the information contained in the object.
[0064] Finally, the parameter-free attention module is expressed as:
[0065]
[0066] Among them, the sigmoid function is used to limit E s This will not affect the relative importance of each neuron because sigmoid is a monotonically increasing function. Using the input sequence relationship for transformation, in order to calculate the sequence attention module, this application performs an average pooling operation on the first dimension of the input sequence X to generate Then subtract X' from each column of the original input sequence and take the absolute value, that is, Copy the extension Then calculate
[0067] X A =(XX′) 2=(X-AvgPool(X)) 2
[0068] Then X A Perform the following transformation:
[0069]
[0070] This application adds this module before each LoRA. Since the module of this application only involves the mean and variance, it is relatively easy to implement. It only needs to add calculation code in the forward function of LoRA. In summary, the proposal of this module is based on the basic theory of neuroscience. In view of the research on computer vision, this application combines this module into the field of natural language processing. Moreover, this module is easy to implement and can be combined with most networks.
[0071] S2. Obtain a fine-tuning dataset, and preprocess the fine-tuning dataset.
[0072] S2 includes:
[0073] S201, setting a specific prompt structure and content for the data set according to different data set tasks, and editing the label result of the data set to modify it into text form;
[0074] S202: shuffle different data set tasks and merge them into a large data set.
[0075] S3. Load and freeze the parameters of the pre-trained model, and initialize the parameters of the parameter fine-tuning module.
[0076] S4. Fine-tune the parameters of the parameter fine-tuning module based on the fine-tuning dataset to obtain a fine-tuned large language model.
[0077] Embodiment 2:
[0078] Based on Example 1, Example 2 of the present application provides a more specific LoRA fine-tuning method based on a parameter-free attention module, including:
[0079] S1. Obtain a pre-trained model and model parameters, and set a parameter fine-tuning module in each layer of the pre-trained model to obtain a model to be fine-tuned; the parameter fine-tuning module is a LoRA model with a parameter-free attention module.
[0080] S2. Obtain a fine-tuning dataset, and preprocess the fine-tuning dataset.
[0081] S3. Load and freeze the parameters of the pre-trained model, and initialize the parameters of the parameter fine-tuning module.
[0082] S4. Fine-tune the parameters of the parameter fine-tuning module based on the fine-tuning dataset to obtain a fine-tuned large language model.
[0083] S4 includes:
[0084] S401: Perform forward propagation calculations on the pre-trained model and parameter fine-tuning module based on the fine-tuning data set to obtain the result Result base_model and Result lora .
[0085] In S401, the specific calculation method of the LoRA model with the parameter-free attention module is:
[0086] For a linear layer calculation, its forward propagation has:
[0087] h=W0X
[0088] LoRA is based on the assumption that updates made during fine-tuning exhibit a lower "inherent rank". It adds two trainable bypass matrices, also called low-rank separable matrices, to the specified layer, and incrementally updates the pre-trained weights by multiplying the two low-rank matrices. LoRA Usage To simulate the change of the initial weight W0 during the fine-tuning of the downstream task, △W0 is decomposed into two low-rank matrices of BA, and And r<<min(d,k). So the model weight W′ after LoRA loading is expressed as:
[0089] W′=W0+△W0=W0+BA
[0090] The hidden layer information h is expressed as follows after LoRA loading:
[0091] h=W′X=W0X+△W0X=W0X+BAX
[0092] like Figure 2 As shown, this application adds a parameter-free attention module on the basis of LoRA, and then the input X will pass through the sequence attention module without training parameters, and then introduce the LoRA structure. The final expression is:
[0093] h=W0X+BA*TkLora(X)
[0094] S402, Result base_model Add Result lora As the output of the pre-trained model, the result is generated.
[0095] S403. Calculate the loss based on the cross entropy, and perform back propagation based on the loss.
[0096] To verify the effectiveness, this paper selected ChatGLM3-6B as the pre-trained model in the actual scenario, and compared the following centralized and efficient fine-tuning methods, including: LoRA and TkLoRA. The dataset includes GLUE and Super-GLUE. GLUE contains six datasets: CoLA, MNLI, QNLI, QQP, RTE and SST-2; Super-GLUE contains four datasets: BoolQ, CB, WiC and WSC.
[0097] The experimental results are shown in Tables 1 and 2 below. The experimental results show that LoRA based on the parameter-free attention module is better than the original LoRA.
[0098] Table 1 Evaluation results on the GLUE dataset, the indicators are accuracy
[0099]
[0100] Table 2 Evaluation results on the Super-GLUE dataset, the indicators are accuracy
[0101]
[0102] It can be seen that the present invention is modified on the basis of LoRA, and a parameter-free attention module is added before the LoRA parameters, so that LoRA can better acquire the semantic information of natural language in learning. The training parameters of TkLoRA have no additional training parameters under the same hyperparameters as LoRA. At the same time, the module is simple and will not increase the reasoning time of the module.
[0103] Embodiment 3:
[0104] Based on Examples 1 and 2, Example 3 of the present application provides a LoRA fine-tuning system based on a parameter-free attention module, including:
[0105] The first acquisition module is used to obtain a pre-trained model and model parameters, and set a parameter fine-tuning module in each layer of the pre-trained model to obtain a model to be fine-tuned; the parameter fine-tuning module is a LoRA model with a parameter-free attention module;
[0106] A second acquisition module is used to acquire a fine-tuning data set and pre-process the fine-tuning data set;
[0107] A loading module, used to load and freeze the parameters of the pre-trained model and initialize the parameters of the parameter fine-tuning module;
[0108] The fine-tuning module is used to fine-tune the parameters of the parameter fine-tuning module based on the fine-tuning dataset to obtain a fine-tuned large language model.
[0109] Specifically, the system provided in this embodiment is a system corresponding to the method provided in Embodiments 1 and 2. Therefore, the parts in this embodiment that are the same or similar to Embodiments 1 and 2 can be referenced to each other and will not be repeated in this application.
Claims
1. A LoRA fine-tuning method based on a parameter-free attention module, characterized in that: include: S1. Obtain a pre-trained model and model parameters, and set a parameter fine-tuning module in each layer of the pre-trained model to obtain a model to be fine-tuned; The parameter fine-tuning module is a LoRA model with a parameter-free attention module; S2. Obtain a fine-tuning dataset, and preprocess the fine-tuning dataset; S3, loading and freezing the parameters of the pre-trained model, and initializing the parameters of the parameter fine-tuning module; S4. Fine-tune the parameters of the parameter fine-tuning module based on the fine-tuning dataset to obtain a fine-tuned large language model.
2. The LoRA fine-tuning method based on the non-parameter attention module according to claim 1, characterized in that S2 include: S201, setting a specific prompt structure and content for the data set according to different data set tasks, and editing the label result of the data set to modify it into text form; S202: shuffle different data set tasks and merge them into a large data set.
3. The LoRA fine-tuning method based on the non-parameter attention module according to claim 2, characterized in that S4 include: S401: Perform forward propagation calculations on the pre-trained model and parameter fine-tuning module based on the fine-tuning data set to obtain the result Result base_model and Result lora ; S402, Result base_model Add Result lora As the output of the pre-trained model, generate results; S403. Calculate the loss based on the cross entropy, and perform back propagation based on the loss.
4. The LoRA fine-tuning method based on the non-parameter attention module according to claim 3 is characterized in that: In S1, the parameter-free attention module calculates the linear separability between the target neuron and its surrounding neurons to obtain the importance of each neuron, and then uses the importance to adjust the weight of the neuron to implement the attention mechanism; the following energy function is defined for each neuron: where s′=w s s+b s And x′ i =w s x i +b s , where s refers to the target neuron, x i refers to other neurons, w s and b s It is s and x i The weight and bias of the transformation, L is the sequence length, λw s 2 is the regularization matrix; The parameter-free attention module is expressed as: Among them, the sigmoid function is used to limit E s ; X is the input sequence, ⊙ represents the dot product.
5. The LoRA fine-tuning method based on the non-parameter attention module according to claim 4 is characterized in that: In the expression of the energy function, when s′ is equal to y, and all other x′ i When y0 is equal to y0, the equation reaches the minimum value, where y and y0 are two different values; by minimizing this equation, it is equivalent to finding the linear separability between the target neuron s and all other neurons in the same channel; for simplicity, binary labels are used to represent y and y0, and the final expression of the energy function is: In addition, there is a fast closed-form solution for the above formula: in and Since the mean and variance calculated by all neurons in the formula are obtained on a single sequence, it is assumed that all pixels in a single channel follow the same distribution; the mean and variance are calculated on all neurons and reused in all neurons on this channel; after substituting the closed-form solution into it, we get: in represents the variance of the input, represents the mean of the input, t represents the target neuron, and λ is a hyperparameter; the greater the gap between the target object and other objects, the more important the information contained in the object.
6. The LoRA fine-tuning method based on the non-parameter attention module according to claim 5 is characterized in that: In S401, the hidden layer information h is expressed as follows after the parameter fine-tuning module is loaded: h=W0X+BA*TkLoRA(X) Among them, W0 is the initial weight of pre-training, X is the input sequence, and BA is the two low-rank matrices obtained by the LoRA model by disassembling the change △W0 of the initialization weight W0.
7. The LoRA fine-tuning method based on the non-parameter attention module according to claim 6 is characterized in that: In S1, the pre-trained model is a pre-trained large language model structure.
8. A LoRA fine-tuning system based on a parameter-free attention module, characterized in that: Used to perform the method according to any one of claims 1 to 7, comprising: The first acquisition module is used to obtain a pre-trained model and model parameters, and set a parameter fine-tuning module in each layer of the pre-trained model to obtain a model to be fine-tuned; the parameter fine-tuning module is a LoRA model with a parameter-free attention module; A second acquisition module is used to acquire a fine-tuning data set and pre-process the fine-tuning data set; A loading module, used to load and freeze the parameters of the pre-trained model and initialize the parameters of the parameter fine-tuning module; The fine-tuning module is used to fine-tune the parameters of the parameter fine-tuning module based on the fine-tuning dataset to obtain a fine-tuned large language model.
9. A computer storage medium, characterized in that The computer storage medium stores a computer program; when the computer program runs on a computer, the computer executes the LoRA fine-tuning method based on a parameter-free attention module as described in any one of claims 1 to 7.
10. An electronic device comprising a storage medium, a processor, and instructions or codes stored in the storage medium and executable on the processor, characterized in that: When the processor executes the instructions or codes, the LoRA fine-tuning method based on the parameter-free attention module described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Pre-training language model parameter fine tuning method and device based on memory mechanism
CN115146651A
Diffusion model rapid fine tuning method based on contribution degree
CN117634595A
Low-rank adaptation-based desensitive picture generation method and device, equipment and medium
CN117853865A
Model temperature self-adaptive fine tuning method for intelligent breeding questions and answers
CN117951276A
Custom models for source code generation
US20230162040A1
Cited By
Product classification recommendation method and system and storage medium
CN119027220A
Small sample fine tuning method for preventing disastrous forgetting based on regularization constraint
CN120764613A
