A code generation model training method based on adaptive knowledge distillation

By employing an adaptive knowledge distillation training method and combining forward and backward KL divergence optimization of the student model, the high computational cost and inference latency of deep learning models in code generation tasks are addressed, enabling efficient deployment and performance improvement on resource-constrained devices.

CN118863009BActive Publication Date: 2026-11-24CHONGQING UNIV
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202410966801.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-18
Publication Date
2026-11-24
Estimated Expiration
2044-07-18

AI Technical Summary

Technical Problem

Existing deep learning models suffer from high computational costs and significant inference latency in code generation tasks, and are difficult to deploy efficiently on resource-constrained devices.

Method used

An adaptive knowledge distillation method is adopted, which combines forward KL divergence and backward KL divergence to train student models to improve their performance in code generation tasks and achieve efficient model compression and deployment.

Benefits of technology

It significantly improves the performance of student models in code generation tasks, reduces computational costs and inference latency, and makes them more suitable for running on resource-constrained devices. Experimental results show significant performance improvements on the HumanEval and MBPP test sets.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118863009B_ABST
    Figure CN118863009B_ABST
Patent Text Reader

Abstract

This invention discloses a code generation model training method based on adaptive knowledge distillation. The method includes acquiring and formatting a training dataset, inputting a template into a generator model to produce prediction results, then softening the outputs of the student and teacher models, and using the Softmax function in conjunction with a temperature parameter T to obtain the probability distribution. Normalized logistic values ​​are obtained through normalization, and an adaptive loss function is calculated by combining backward and forward KL divergences. The Adam optimization algorithm is used to update the student model parameters, and a forward pass is performed after each parameter update to evaluate the model's predicted output and loss value, ensuring the convergence and stability of the training. This invention also provides detailed calculation formulas and template content to ensure the feasibility and effectiveness of the method. This method can significantly improve the performance of the model on resource-constrained devices while maintaining efficient local deployment capabilities.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of code generation technology, and in particular to a method for training a code generation model based on adaptive knowledge distillation. Background Technology

[0002] Code generation is 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 has also brought high computational costs and significant inference latency, limiting its widespread deployment in practical applications.

[0003] Traditionally, deep learning models typically run on cloud computing platforms, delivering results to smart devices over the network. While this approach is feasible in some cases, it's not optimal for applications requiring low latency or high data privacy. Furthermore, the unpredictable network connectivity between the cloud and devices can pose significant challenges. Therefore, running deep learning systems locally has become a crucial requirement. Knowledge distillation, as an effective model compression method, allows smaller student models to mimic the behavior of larger teacher models, significantly reducing model size while maintaining performance as much as possible. This enables efficient local deployment on resource-constrained devices with limited memory and power. Summary of the Invention

[0004] This invention addresses the challenges faced by existing deep learning models in code generation tasks, such as high computational costs, significant inference latency, and difficulties in deployment on resource-constrained devices. The technical problem solved by this invention is how to achieve a more efficient, accurate, and resource-adaptable code generation model.

[0005] To address the aforementioned issues, this invention proposes a code generation model training method based on adaptive knowledge distillation. This method aims to improve the performance of student models in code generation tasks by combining forward and backward KL divergence, while simultaneously achieving efficient model compression and deployment on resource-constrained devices.

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

[0007] S101: Obtain the training dataset, which contains instructions and input text, and format it into a template format.

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

[0009] S103: Soften the student model output by performing Softmax processing using the temperature parameter T to obtain the softened probability distribution. .

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

[0011] S105: Normalize the probability distributions of the student model and the teacher model to obtain the normalized logistic value of the teacher model. Logical values ​​of student models .

[0012] S106: Based on the softening probability distributions of the teacher and student models, and combining the reverse and forward KL divergences, calculate the adaptive loss function.

[0013] S107: The calculated adaptive loss function The data is passed back to the student model, and the gradient of the adaptive loss function with respect to the student model parameters is calculated using the backpropagation algorithm.

[0014] S108: Update the parameters of the student model using the Adam optimization algorithm.

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

[0016] S110: Repeat steps S105 to S108 until the student model's performance on the validation set reaches the expected result or the training reaches the preset maximum number of iterations. After training, the final output data consists of the final parameters of the student model trained using adaptive knowledge distillation, along with the training data. The final parameters include the model's weights and biases, while the data includes training log information, training loss, validation loss, and accuracy, used to evaluate model performance and the stability of the training process.

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

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

[0019]

[0020]

[0021]

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

[0023]

[0024] in, Let logits be the logits of the student model at position t for the i-th word. By softening the probability distribution of the student model, we can obtain the logits of the student model, defined as follows: .in, Indicates context, This represents the probability of the word predicted by the student model at position t. , It is the size of the model vocabulary. This indicates that the student model predicts the th th at position t. The probability of each word Temperature parameters Control the degree of softening, It represents all possible words. This is the exponential sum of the logit values ​​of the student model at position t for all possible words. Higher values ​​indicate a higher probability of success. A higher value will make the probability distribution smoother, which will help with the transfer of knowledge.

[0025] Furthermore, the formula for calculating the probability distribution after softening the S104 teacher model is as follows:

[0026]

[0027] in, Let logits be the logits of the teacher model at position t for the i-th word. By softening the probability distribution of the teacher model, we can obtain the logits value of the student model. ,in, Indicates context, This represents the probability of the word predicted by the teacher model at position t. , It is the size of the model vocabulary. This indicates that the teacher model predicts the first time at position t. The probability of each word Temperature parameters Control the degree of softening, It represents all possible words. The teacher model is the exponential sum of the logit values ​​for all possible words at position t, with higher values ​​being... A higher value will make the probability distribution smoother, which will help with the transfer of knowledge.

[0028] Furthermore, the S105 logic value Logical values ​​of student models The calculation formula is as follows:

[0029]

[0030]

[0031] in, for Its position in the vocabulary list In the teacher model The logit value, used Indicates to The normalized logit value, In the student model The logit value, Indicates to The normalized logit value, Representing context The word in the middle position t.

[0032] Furthermore, the S106 reverse KL divergence ( ) and positive KL divergence ( The calculation formula is as follows:

[0033]

[0034]

[0035] in, express The reverse KL divergence, express . This represents summing the probability distributions of each time step 𝑡 in the sequence. By calculating in a given context and previous time step conditions, and The difference between them is obtained by summing up all time steps. Then, by calculating in the given context and the previous time step, Under the condition, student model distribution Teacher model distribution The difference between them is obtained by summing up all time steps.

[0036] Furthermore, the S106 adaptive loss function The calculation method is as follows:

[0037]

[0038]

[0039]

[0040]

[0041] Among them, the mask It's the difference in the normalized logit values, a hyperparameter. It is a threshold. The proportion of sample data whose labeled, normalized logit values ​​differ more than a threshold. Indicates whether the i-th data item is used. Adaptive loss function. When LM[i] is 1, it means that an adaptive loss function is applied to sample i. An adaptive loss function is used; otherwise, it is not used. It is a weighted sum of the errors of all data points at all time steps, and then the sum is divided by the sum of the sequence lengths of all data points to obtain a sequence length-weighted average error.

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

[0043] 1. This invention improves the performance of student models in code generation tasks by adaptively combining forward KL divergence and backward KL divergence, especially when dealing with complex distributions, and is able to learn the behavior of teacher models more effectively.

[0044] 2. By employing an adaptive knowledge distillation strategy, this invention achieves efficient model compression and deployment, reduces computational costs and inference latency, and makes the model more suitable for running on resource-constrained devices.

[0045] 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. Attached Figure Description

[0046] Figure 1 This is a simplified flowchart of the method of the present invention. Detailed Implementation

[0047] The present invention will now be described in further detail.

[0048] See Figure 1 A code generation model training method based on adaptive knowledge distillation includes the following steps:

[0049] S101: Obtain the training dataset, which contains instructions and input text, and format it into a template format.

[0050] S102: Input the above template into both the student model and the teacher model to generate prediction results.

[0051] S103: Soften the student model output by performing Softmax processing using the temperature parameter T to obtain the softened probability distribution. .

[0052] S104: Soften the output of the teacher model by performing Softmax processing using the same temperature parameter T as in S103 to obtain the softened probability distribution. .

[0053] S105: Normalize the probability distributions of the student model and the teacher model to obtain the normalized logistic value of the teacher model. Logical values ​​of student models .

[0054] S106: Based on the softening probability distributions of the teacher and student models, and combining the reverse and forward KL divergences, calculate the adaptive loss function.

[0055] S107: The calculated adaptive loss function The data is passed back to the student model, and the gradient of the adaptive loss function with respect to the student model parameters is calculated using the backpropagation algorithm.

[0056] S108: Update the parameters of the student model using the Adam optimization algorithm.

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

[0058] S110: Repeat steps S105 to S108 until the student model's performance on the validation set reaches the expected result or the training reaches the preset maximum number of iterations. After training, the final output data consists of the final parameters of the student model trained using adaptive knowledge distillation, along with the training data. The final parameters include the model's weights and biases, while the data includes training log information, training loss, validation loss, and accuracy, used to evaluate model performance and the stability of the training process.

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

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

[0061]

[0062]

[0063]

[0064] Furthermore, the formula for calculating the softened probability distribution of the S103 student model is as follows:

[0065]

[0066] in, Let logits be the logits of the student model at position t for the i-th word. By softening the probability distribution of the student model, we can obtain the logits of the student model, defined as follows: .in, Indicates context, This represents the probability of the word predicted by the student model at position t. , It is the size of the model vocabulary. This indicates that the student model predicts the th th at position t. The probability of each word Temperature parameters Control the degree of softening, It represents all possible words. This is the exponential sum of the logit values ​​of the student model at position t for all possible words. Higher values ​​indicate a higher probability of success. A higher value will make the probability distribution smoother, which will help with the transfer of knowledge.

[0067] Furthermore, the formula for calculating the probability distribution after softening the S104 teacher model is as follows:

[0068]

[0069] in, Let logits be the logits of the teacher model at position t for the i-th word. By softening the probability distribution of the teacher model, we can obtain the logits value of the student model. ,in, Indicates context, This represents the probability of the word predicted by the teacher model at position t. , It is the size of the model vocabulary. This indicates that the teacher model predicts the first time at position t. The probability of each word Temperature parameters Control the degree of softening, It represents all possible words. The teacher model is the exponential sum of the logit values ​​for all possible words at position t, with higher values ​​being... A higher value will make the probability distribution smoother, which will help with the transfer of knowledge.

[0070] Furthermore, the S105 logic value Logical values ​​of student models The calculation formula is as follows:

[0071]

[0072]

[0073] in, for Its position in the vocabulary list In the teacher model The logit value, used Indicates to The normalized logit value, In the student model The logit value, Indicates to The normalized logit value, Representing context The word in the middle position t.

[0074] Furthermore, the S106 reverse KL divergence ( ) and positive KL divergence ( The calculation formula is as follows:

[0075]

[0076]

[0077] in, express The reverse KL divergence, express . This represents summing the probability distributions of each time step 𝑡 in the sequence. By calculating in a given context and previous time step conditions, and The difference between them is obtained by summing up all time steps. Then, by calculating in the given context and the previous time step, Under the condition, student model distribution Teacher model distribution The difference between them is obtained by summing up all time steps.

[0078] Furthermore, the S106 adaptive loss function The calculation method is as follows:

[0079]

[0080]

[0081]

[0082]

[0083] Among them, the mask It's the difference in the normalized logit values, a hyperparameter. It is a threshold. The proportion of sample data whose labeled, normalized logit values ​​differ more than a threshold. Indicates whether the i-th data item is used. Adaptive loss function. When LM[i] is 1, it means that an adaptive loss function is applied to sample i. An adaptive loss function is used; otherwise, it is not used. It is a weighted sum of the errors of all data points at all time steps, and then the sum is divided by the sum of the sequence lengths of all data points to obtain a sequence length-weighted average error.

[0084] Experimental Design and Analysis

[0085] 1. Dataset: The experiments used Codealpaca, a mainstream publicly available dataset in the code generation field, for training. This dataset is used to train an LLaMA model that follows instructions to generate code. A series of novel tasks were generated using a pre-trained large language model (LLM), namely text-davinci-003, including task descriptions, inputs, and expected outputs, with 40% of the data containing inputs. We selected 1000 data points from the Codealpaca dataset as the test set, and the remaining data as the training set.

[0086] 2. Comparative Experiment

[0087] The pass@k metric is used to evaluate the student model’s ability to achieve the expected correctness in the code generation task, which is the probability that at least one of the first k outputs correctly implements the specified function.

[0088]

[0089] Where c is the number of n generated code snippets that pass the tests, and k is the number of code snippets generated by the model.

[0090] The experimental environment was: Python 3.10.13, PyTorch 2.0.1, and 3. NVIDIA A800 Tensor Core GPU, with evaluation metrics of human-eval@pass1 and mbpp@pass1.

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

[0092] To demonstrate the validity of the experiment, the teacher models were selected from leading open-source code generation model families: codellama-python-7B, codegen-6B-mono, and StarCoder-7B. We chose the smallest model in each family as the student models: TinyLlama-1.1B, codegen-350MB-moni, and StarCoder-1B. The test results are shown in Table 1.

[0093] Compared with the best method in the experimental setup, the method of this invention improved the efficiency of the three student models by 4.9%, 8.4%, 0.7%, 3.2%, 3.1%, and 1.4% on Human_eval@pass1 and mbpp@pass1, respectively. The mean efficiency of Human_eval@pass1 was improved by 2.9%, and the mean efficiency of mbpp@pass1 was improved by 4.3%.

[0094] Table 1 Experimental Results

[0095]

[0096] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention, and all such modifications or substitutions should be covered within the scope of the claims of the present invention.

Claims

1. A code generation model training method based on adaptive knowledge distillation, characterized in that: Includes the following steps: S101: Obtain the training dataset, which contains instructions and input text, and format it into a template format; S102: Input the above template into both the student model and the teacher model to generate prediction results; S103: Soften the student model output result, use temperature parameter T to perform Softmax processing, and obtain a softened probability distribution ; The probability distribution The calculation formula is as follows: in, Let logits be the logits of the i-th word at position t in the student model. Indicates context, This represents the probability of the word predicted by the student model at position t. , It is the size of the model vocabulary. This indicates that the student model predicts the th th at position t. The probability of each word Temperature parameters Control the degree of softening, Indicates all possible words; Let be the exponential sum of the logit values ​​of the student model at position t for all possible words; S104: Soften the output of the teacher model by performing Softmax processing using the same temperature parameter T as in S103 to obtain the softened probability distribution. ; The probability distribution The calculation formula is as follows: in, Let logits be the logits of the i-th word at position t in the teacher model. Indicates context, This represents the probability of the word predicted by the teacher model at position t. , It is the size of the model vocabulary. This indicates that the teacher model predicts the first time at position t. The probability of each word Temperature parameters Control the degree of softening, Indicates all possible words; Let be the exponential sum of the logit values ​​of the teacher model at position t for all possible words; S105: Normalize the probability distributions of the student model and the teacher model to obtain the normalized logistic value of the teacher model. Logical values ​​of student models ; S106: Based on the softening probability distributions of the teacher and student models, and combining the reverse and forward KL divergences, calculate the adaptive loss function. ; The reverse KL divergence and positive KL divergence The calculation formula is as follows: in, express The reverse KL divergence, express Positive KL divergence; The adaptive loss function For the first data in a batch of size I The calculation method for each data point is as follows: Among them, the mask It's the difference in the normalized logit values, a hyperparameter. It is a threshold. The proportion of sample data whose labeled, normalized logit values ​​differ more than a threshold. Indicates whether the i-th data item is used. Adaptive loss function; when LM[i] is 1, it means that the loss function applied to sample i is... An adaptive loss function is used, otherwise it is not used; S107: The calculated adaptive loss function The data is passed back to the student model, and the gradient of the adaptive loss function with respect to the student model parameters is calculated using the backpropagation algorithm. S108: Update the parameters of the student model using the Adam optimization algorithm; S109: After updating the student model parameters, perform a forward pass to calculate the student model's predicted output on the current batch of data, and then recalculate the adaptive loss function. To evaluate the convergence and stability of the model during the training process; S110: Repeat steps S105 to S108 above until the student model achieves the expected performance on the validation set or the training reaches the preset maximum number of iterations. S111: Format the input data into a template format according to the method in S101, and then input the formatted input data into the trained student model to generate prediction results.

2. The code generation model training method based on adaptive knowledge distillation as described in claim 1, characterized in that: In S105, the teacher model logical value Logical values ​​of student models The calculation formula is as follows: in, for Its position in the vocabulary list In the teacher model The logit value, used Indicates to The normalized logit value, In the student model The logit value, Indicates to The normalized logit value, Representing context The word in the middle position t.

Citation Information

Patent Citations

  • Self-adaptive KL divergence distillation training method and device based on federal large language model

    CN120851123A