Code generation model training method based on adaptive knowledge distillation

By combining the adaptive knowledge distillation method of forward and reverse KL divergence in the code generation model, the student model is optimized, the computational cost and delay problems of the deep learning model in the code generation task are solved, and the system is implemented on resource-constrained devices. Efficient deployment and performance improvement.

CN118863009APending Publication Date: 2024-10-29CHONGQING UNIV
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202410966801.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-18
Publication Date
2024-10-29

AI Technical Summary

Technical Problem

Deep learning models face high computational costs and significant inference delays in code generation tasks, and are difficult to deploy on resource-constrained devices.

Method used

A code generation model training method based on adaptive knowledge distillation is adopted to optimize the performance of the student model by combining forward KL divergence and reverse KL divergence, and reduce the model size through softening processing and optimization algorithms to adapt to resource-constrained environments.

Benefits of technology

It significantly improves the performance of the student model in code generation tasks, reduces computational costs and inference delays, and makes the model more suitable for running on resource-constrained devices. The experimental results have achieved significant performance improvements on the HumanEval and MBPP test sets.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118863009A_ABST
    Figure CN118863009A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation model training method based on adaptive knowledge distillation, and the method comprises the steps: obtaining and formatting a training data set, inputting a template to a generator model to generate a prediction result, carrying out the softening processing of output results of a student model and a teacher model, and obtaining a prediction result. And a Softmax function is combined with the temperature parameter T to obtain probability distribution. A normalized logic value is obtained through normalization processing, a self-adaptive knowledge distillation loss function is calculated in combination with reverse and forward KL divergence, student model parameters are updated by using a dam optimization algorithm, forward transmission is performed after each time of parameter updating, prediction output and a loss value of the model are evaluated, and convergence and stability of training are ensured. The invention further provides a detailed calculation formula and template content, and the feasibility and effectiveness of the method are ensured. By means of the method, the performance of the model on the resource-constrained equipment can be remarkably improved, and meanwhile the efficient local deployment capacity is kept.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code generation, and in particular to a code generation model training method based on adaptive knowledge distillation. Background Art

[0002] Code generation tasks are of great significance in the field of software engineering, aiming to improve development efficiency and code quality by automatically generating code. With the development of large language models (LLMs), such as GPT-4, OPT, and LLaMA, natural language processing (NLP) tasks have made significant progress. However, the increase in model size also brings high computational costs and significant inference delays, limiting its widespread deployment in practical applications.

[0003] Traditionally, deep learning models are usually run on cloud computing platforms, and the results are delivered to smart devices over the network. This approach is feasible in some cases, but it is not the best choice in application scenarios that require low latency or high data privacy. In addition, unpredictable network connections between the cloud and devices can also pose major challenges. Therefore, running deep learning systems on local devices has become an important requirement. Knowledge distillation, as an effective model compression method, can significantly reduce the model size while maintaining performance as much as possible by allowing smaller student models to imitate the behavior of large teacher models, adapting to resource-constrained devices with limited memory and power, and achieving efficient local deployment. Summary of the invention

[0004] In view of the challenges faced by deep learning models in code generation tasks in the prior art, such as high computing costs, significant inference delays, and difficulties in deployment on resource-constrained devices, the technical problem solved by the present invention is how to achieve a more efficient, more accurate, and more adaptable code generation model for resource-constrained environments.

[0005] To solve the above problems, the present invention proposes a code generation model training method based on adaptive knowledge distillation, which aims to improve the performance of the student model in the code generation task by combining forward KL divergence and reverse KL divergence, while achieving efficient compression of the model and deployment on resource-constrained devices.

[0006] A large model lightweight method based on adaptive knowledge distillation includes the following steps:

[0007] S101: Obtain a training data set including instructions and input text, and format it into a template format.

[0008] S102: Input the above template into the student model and the teacher model at the same time to generate prediction results.

[0009] S103: Soften the output of the student model and use the temperature parameter T to perform Softmax processing to obtain the softened probability distribution q θ .

[0010] S104: Soften the output result of the teacher model and perform Softmax processing using the same temperature parameter T to obtain the softened probability distribution p.

[0011] S105: Normalize the probability distribution of the student model and the teacher model to obtain the normalized teacher model logic value and student model logical values

[0012] S106: Calculate the adaptive knowledge distillation loss based on the softened probability distribution of the teacher model and the student model, combined with the reverse KL divergence and the forward KL divergence

[0013] S107: The calculated adaptive loss function The gradient of the loss function with respect to the parameters of the student model is calculated using the back-propagation algorithm.

[0014] S108: Use the optimization algorithm Adam to update the parameters of the student model.

[0015] S109: After updating the student model parameters, a forward pass is performed to calculate the predicted output of the student model on the current batch of data and calculate the new loss value to evaluate the convergence and stability of the model during training.

[0016] S110: Repeat the above steps S105 to S108 until the performance of the student model on the validation set reaches the expected effect or the training reaches the preset maximum number of iterations. After the training is completed, the final output data is the final parameters and training data of the student model after adaptive knowledge distillation training. The final parameters include the weight and bias value training of the model, and the data includes training log information, training loss, validation loss and accuracy, which are used to evaluate the model performance and the stability of the training process.

[0017] S111: Format the input data into a template format according to the S101 method, and then input the formatted input data into the trained student model to generate a prediction result.

[0018] Furthermore, the content of the S101 template is as follows:

[0019] Prompt 1:{instruction}(input)\nWrite your response here.

[0020] Prompt 2:{instruction}(input)\n

[0021] Prompt 3:Below is an instruction that describes a task.Write aresponse that appropriately completes the request.\n{instruction}\n{input}\n###Response:

[0022] Furthermore, the probability distribution calculation formula of the softened student model in S103 is as follows:

[0023]

[0024] in, is the logits of the i-th word in the student model at position t. By softening the probability distribution of the student model, the logits of the student model can be obtained, defined as q θ (y t |y <t ,x). Where x represents the context, y t represents the probability of the word predicted by the student model at position t, y t ∈{Υ1,Υ2,…,Υ V}, V is the size of the model vocabulary, Υ V represents the probability of the Vth word predicted by the student model at position t, The temperature parameter T controls the degree of softening, and j represents all possible words. is the exponential sum of the logit values ​​of the student model at position t for all possible words. A higher T value will make the probability distribution smoother and help transfer knowledge.

[0025] Furthermore, the probability distribution calculation formula of the softened teacher model in S104 is as follows:

[0026]

[0027] in, is the logits of the i-th word in the teacher model at position t. By softening the probability distribution of the teacher model, the logits value p(y′) of the student model can be obtained. t |y′ <t ,x), where x represents the context and y′ t represents the probability of the word predicted by the teacher model at position t, y′ t ∈{Υ′1,Υ′2,…,Υ′ V}, V is the size of the model vocabulary, Υ′ V represents the probability of the Vth word predicted by the classroom model at position t, The temperature parameter T controls the degree of softening, and j represents all possible words. It is the exponential sum of the logit values ​​of the teacher model at position t for all possible words. A higher T value will make the probability distribution smoother and help the transfer of knowledge.

[0028] Furthermore, the S105 logic value and student model logical values The calculation formula is as follows:

[0029]

[0030] Among them, v(x t ) is x t Position in the vocabulary, Represents x in the teacher model t The logit value of Express The normalized logit value is Represents x in the student model t The logit value of Express The normalized logit value, x t Represents the word at position t in context x.

[0031] Furthermore, the reverse KL divergence (L RKD ) and forward KL divergence (L FKD ) is calculated as follows:

[0032]

[0033] in, Represents p and q θ The reverse KL divergence of Represents p and q θ ∑ represents the sum of the probability distribution of each time step t in the sequence, By calculating y given the context x and the previous time step <t Under the condition, p(y|y <t ,x) and q θ (y|y <t ,x), and accumulate them for all time steps t. By calculating y given the context x and the previous time step <t Under this condition, the student model distribution q θ (y|y<t ,x) and the teacher model distribution p(y|y <t ,x), and accumulate them over all time steps y.

[0034] Furthermore, the S106 adaptive The loss function is calculated as follows:

[0035]

[0036] Among them, the mask M t is the difference of the labeled normalized logit value, the hyperparameter μ is the threshold, α is the proportion of the difference of the labeled normalized logit value in the sample data that is greater than the threshold, LM[i] represents the i-th data, whether to use Loss function. When LM[i] is 1, it means that the loss function is used for sample i. Loss function, otherwise it is not used. It is a weighted sum of the errors of all data points at all time steps, and then this sum is divided by the sum of the lengths of all data point sequences to obtain an average error weighted by the sequence length.

[0037] Compared with the prior art, the present invention has at least the following beneficial effects:

[0038] 1. The present invention improves the performance of the student model in code generation tasks by adaptively combining forward KL divergence and reverse KL divergence, especially when dealing with complex distributions, and can more effectively learn the behavior of the teacher model.

[0039] 2. Through the adaptive knowledge distillation strategy, the present invention achieves efficient compression and deployment of the model, reduces computing costs and inference latency, and makes the model more suitable for running on resource-constrained devices.

[0040] 3. Experimental results show that the method of the present invention achieves significant performance improvement on the HumanEval and MBPP test sets, proving its effectiveness and superiority in practical applications. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] Figure 1 The figure is a simplified flow chart of the method of the present invention. DETAILED DESCRIPTION

[0042] The present invention is described in further detail below.

[0043] See also Figure 1 , a code generation model training method based on adaptive knowledge distillation, comprising the following steps:

[0044] S101: Obtain a training data set including instructions and input text, and format it into a template format.

[0045] S102: Input the above template into the student model and the teacher model at the same time to generate prediction results.

[0046] S103: Soften the output result of the student model and perform Softmax processing using the temperature parameter T to obtain the softened probability distribution p.

[0047] S104: Soften the output of the teacher model and perform Softmax processing using the same temperature parameter T as S103 to obtain the softened probability distribution q θ .

[0048] S105: Normalize the probability distribution of the student model and the teacher model to obtain the normalized teacher model logic value and student model logical values

[0049] S106: Calculate the adaptive knowledge distillation loss based on the softened probability distribution of the teacher model and the student model, combined with the reverse KL divergence and the forward KL divergence

[0050] S107: The calculated adaptive loss function The gradient of the loss function with respect to the parameters of the student model is calculated using the back-propagation algorithm.

[0051] S108: Use the optimization algorithm Adam to update the parameters of the student model.

[0052] S109: After updating the student model parameters, a forward pass is performed to calculate the predicted output of the student model on the current batch of data and calculate the new loss value to evaluate the convergence and stability of the model during training.

[0053] S110: Repeat the above steps S105 to S108 until the performance of the student model on the validation set reaches the expected effect or the training reaches a preset maximum number of iterations.

[0054] S110: Repeat the above steps S105 to S108 until the student model reaches the preset maximum number of iterations in training, the training is completed, and a trained student model is obtained. After the training is completed, the final output data is the final parameters and training data of the student model after adaptive knowledge distillation training. The final parameters include the weight and bias value training of the model, and the data includes training log information, training loss, verification loss and accuracy, which are used to evaluate the model performance and the stability of the training process.

[0055] S111: Format the input data into a template format according to the S101 method, and then input the formatted input data into the trained student model to generate a prediction result.

[0056] Specifically, the content of the S101 template is as follows:

[0057] Prompt 1:{instruction}(input)\nWrite your response here.

[0058] Prompt 2:{instruction}(input)\n

[0059] Prompt 3:Below is an instruction that describes a task.Write aresponse that appropriately completes the request.\n{instruction}\n{input}\n###Response:

[0060] Furthermore, the probability distribution calculation formula of the softened student model in S103 is as follows:

[0061]

[0062] in, is the logits of the i-th word in the student model at position t. By softening the probability distribution of the student model, the logits of the student model can be obtained, defined as q θ (y t |y <t ,x). Where x represents the context, y t represents the probability of the word predicted by the student model at position t, y t ∈{Υ1,Υ2,…,Υ V}, V is the size of the model vocabulary, Υ V represents the probability of the Vth word predicted by the student model at position t, The temperature parameter T controls the degree of softening, and j represents all possible words. is the exponential sum of the logit values ​​of the student model at position t for all possible words. A higher T value will make the probability distribution smoother and help transfer knowledge.

[0063] Furthermore, the probability distribution calculation formula of the softened teacher model in S104 is as follows:

[0064]

[0065] in, is the logits of the i-th word in the teacher model at position t. By softening the probability distribution of the teacher model, the logits value p(y′) of the student model can be obtained. t |y′ <t ,x), where x represents the context and y′ t represents the probability of the word predicted by the teacher model at position t, y′ t ∈{Υ′1,Υ′2,…,Υ′ V}, V is the size of the model vocabulary, Υ′ V represents the probability of the Vth word predicted by the classroom model at position t, The temperature parameter T controls the degree of softening, and j represents all possible words. It is the exponential sum of the logit values ​​of the teacher model at position t for all possible words. A higher T value will make the probability distribution smoother and help the transfer of knowledge.

[0066] Furthermore, the S105 logic value and student model logical values The calculation formula is as follows:

[0067]

[0068] Among them, v(x t ) is x t Position in the vocabulary, Represents x in the teacher model t The logit value of Express The normalized logit value is Represents x in the student model t The logit value of Express The normalized logit value, x t Represents the word at position t in context x.

[0069] Furthermore, the reverse KL divergence (L RKD ) and forward KL divergence (L FKD ) is calculated as follows:

[0070]

[0071] in, Represents p and q θ The reverse KL divergence of Represents p and q θ∑ represents the sum of the probability distribution of each time step t in the sequence, By calculating y given the context x and the previous time step <t Under the condition, p(y|y <t ,x) and q θ (y|y <t ,x), and accumulate them for all time steps t. By calculating y given the context x and the previous time step <t Under this condition, the student model distribution q θ (y|y <t ,x) and the teacher model distribution p(y|y <t ,x), and accumulate them for all time steps t.

[0072] Furthermore, the S106 adaptive The loss function is calculated as follows:

[0073]

[0074] Among them, the mask M t is the difference of the labeled normalized logit value, the hyperparameter μ is the threshold, α is the proportion of the difference of the labeled normalized logit value in the sample data that is greater than the threshold, LM[i] represents the i-th data, whether to use Loss function. When LM[i] is 1, it means that the loss function is used for sample i. Loss function, otherwise it is not used. It is a weighted sum of the errors of all data points at all time steps, and then this sum is divided by the sum of the lengths of all data point sequences to obtain an average error weighted by the sequence length.

[0075] Experimental design and analysis

[0076] 1. Dataset: The experiment uses Codealpaca, a mainstream public dataset in the field of code generation, for training. This dataset is based on the LLaMA model that follows training instructions to generate code. By using a pre-trained large language model (LLM), text-davinci-003, a series of novel tasks are generated, including task instructions, inputs, and expected outputs, of which 40% of the data is input. We select 1,000 data from the Codealpaca dataset as the test set and the rest as the training set.

[0077] 2. Comparative Experiment

[0078] The pass@k metric is used to evaluate the ability of the student model to achieve the expected correctness in the code generation task, that is, the probability that at least one of the top k output results correctly implements the specified function.

[0079]

[0080] Where c is the number of passed tests among the n codes generated, and k is the number of codes generated by the model.

[0081] The experimental environment is: Python 3.10.13, PyTorch 2.0.1, 3*NVIDIA A800Tensor Core GPU, and the evaluation indicators are human-eval@pass1, mbpp@pass1.

[0082] The hyperparameters for all experiments were kept the same, μ was set to 0.2, the batch size was set to 16, training was performed for 10 epochs, the learning rate was 5e-4, and the maximum input was 512.

[0083] To prove the effectiveness of the experiment, the teacher model is selected from the current leading open source code generation model family: codellama-python-7B, codegen-6B-mono, StarCoder-7B. We select the smallest model in each model family as the student model, TinyLlama-1.1B, codegen-350MB-moni and StarCoder-1B. The test results are shown in Table 1.

[0084] Compared with the best method in the experimental setting, the three student models of the present invention improved by 4.9%, 8.4%, 0.7%, 3.2%, 3.1% and 1.4% on Human_eval@pass1 and mbpp@pass1 respectively. The average value of Human_eval@pass1 improved by 2.9% and the average value of mbpp@pass1 improved by 4.3%.

[0085] Table 1 Experimental results

[0086]

[0087]

[0088] Finally, it should be noted that the above embodiments are only used to illustrate the technical solution of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solution of the present invention can be modified or replaced by equivalents without departing from the purpose and scope of the technical solution of the present invention, which should be included in the scope of the claims of the present invention.

Claims

1. A code generation model training method based on adaptive knowledge distillation, Features: The steps include: S101: Obtain a training data set including instructions and input text, and format it into a template format; S102: Input the above template into the student model and the teacher model at the same time to generate a prediction result; S103: Soften the output of the student model and use the temperature parameter T to perform Softmax processing to obtain the softened probability distribution q θ ; S104: Soften the output result of the teacher model, perform Softmax processing using the same temperature parameter T as S103, and obtain the softened probability distribution p; S105: Normalize the probability distribution of the student model and the teacher model to obtain the normalized teacher model logic value and student model logical values S106: Calculate the adaptive knowledge distillation loss based on the softened probability distribution of the teacher model and the student model, combined with the reverse KL divergence and the forward KL divergence S107: The calculated adaptive loss function Pass it back to the student model and use the back-propagation algorithm to calculate the gradient of the loss function relative to the student model parameters; S108: Use the optimization algorithm Adam to update the parameters of the student model; S109: After updating the student model parameters, a forward pass is performed to calculate the predicted output of the student model on the current batch of data and adapt the knowledge distillation loss again To evaluate the convergence and stability of the model during training; S110: Repeat the above steps S105 to S108 until the student model reaches a preset maximum number of iterations in training, and the training is completed to obtain a trained student model; S111: Format the input data into a template format according to the S101 method, and then input the formatted input data into the trained student model to generate a prediction result.

2. A code generation model training method based on adaptive knowledge distillation as claimed in claim 1, Features: The probability distribution calculation formula of the S103 student model after softening is as follows: in, is the logits of the i-th word in the student model at position t, x represents the context, y t represents the probability of the word predicted by the student model at position t, y t ∈{γ 1 ,γ 2 ,…,γ V }, V is the size of the model vocabulary, γ V represents the probability of the Vth word predicted by the student model at position t, The temperature parameter T controls the degree of softening, and j represents all possible words; is the exponential sum of the logit values ​​of the student model at position t for all possible words.

3. A code generation model training method based on adaptive knowledge distillation as claimed in claim 1, Features: The probability distribution calculation formula of the S104 teacher model after softening is as follows: in, is the logits of the teacher model at position t for the i-th word, x represents the context, y′ t represents the probability of the word predicted by the teacher model at position t, y′ t ∈{γ′ 1 ,γ′ 2 ,…,γ′ V }, V is the size of the model vocabulary, γ′ V represents the probability of the Vth word predicted by the classroom model at position t, The temperature parameter T controls the degree of softening, and j represents all possible words; is the exponential sum of the logit values ​​of the teacher model at position t for all possible words.

4. A code generation model training method based on adaptive knowledge distillation as claimed in claim 1, Features: In S105, the teacher model logic value and student model logical values The calculation formula is as follows: Among them, v(x t ) is x t Position in the vocabulary, Represents x in the teacher model t The logit value of Express The normalized logit value is Represents x in the student model t The logit value of Express The normalized logit value, x t Represents the word at position t in context x.

5. A code generation model training method based on adaptive knowledge distillation as claimed in claim 1, Features: The S106 reverse KL divergence L RKD and the forward KL divergence L FKD The calculation formula is as follows: in, Represents p and q θ The reverse KL divergence of Represents p and q θ .

6. A code generation model training method based on adaptive knowledge distillation as claimed in claim 1, Features: The S106 Adaptive The loss function is calculated for the i-th data in the batch size I as follows: Among them, the mask M t is the difference of the labeled normalized logit value, the hyperparameter μ is the threshold, α is the proportion of the difference of the labeled normalized logit value in the sample data that is greater than the threshold, LM[i] represents the i-th data, whether to use Loss function; when LM[i] is 1, it means that the sample i is used Loss function, otherwise it is not used.

Citation Information

Cited By

  • Knowledge distillation method, device, equipment, medium and product

    CN119990257A

  • Spatial analysis code large model fine tuning method and system based on data distillation

    CN121478297A

  • Knowledge distillation using hybrid loss function for different sample types

    WO2026155807A1