Knowledge distillation-based large language model fine tuning and software testing method and system

By using a large language model fine-tuning method based on knowledge distillation, the problem of balancing generation and execution costs in software testing is solved, semantic robustness and assertion accuracy are improved, and low-cost, high-efficiency test generation is achieved.

CN121809576APending Publication Date: 2026-04-07NO 15 INST OF CHINA ELECTRONICS TECH GRP
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-22
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

Existing technologies struggle to control generation and execution costs while ensuring a high kill rate in software testing. Furthermore, large language models suffer from issues such as weak assertions, insufficient generation stability, high resource consumption, and unstable cross-project generalization capabilities in engineering implementation.

Method used

We employ a large language model fine-tuning method based on knowledge distillation. We learn a structured test paradigm through a supervised fine-tuning stage, and introduce a teacher model in the knowledge distillation stage to train the student model with soft target distillation, hard target supervision, Jensen-Shannon distillation and semantic consistency regularization. We also combine LoRA low-rank adapter to reduce resource consumption.

Benefits of technology

It improves semantic robustness and stability, enhances assertion accuracy and coverage quality, reduces memory usage and latency, meets CI/CD constraints and localized deployment resource limitations, and achieves low-cost, high-efficiency test generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121809576A_ABST
    Figure CN121809576A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of software testing, and particularly discloses a knowledge distillation-based large language model fine tuning and software testing method and system, and the method comprises a supervision fine tuning stage, a knowledge distillation stage and a reasoning stage. In the supervised fine tuning stage, taking the pre-trained large model as a backbone, inserting an LoRA low-rank adapter, and learning a structured unit test generation normal form under to-be-tested codes and demand conditions to obtain a student model; in the knowledge distillation stage, a teacher model is introduced, and soft target distillation, hard target supervision, Jensen-Shannon distillation and semantic keeping consistency regularization are jointly used as constraint conditions to train a student model; in the reasoning stage, the software code to be tested is tested based on the trained student model. According to the method, variation-sensitive and semantic-robust test generation can be realized under the constraint of actual engineering only by depending on supervised fine tuning and distillation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of software testing technology, and more specifically to a method and system for fine-tuning large language models based on knowledge distillation and software testing. Background Technology

[0002] Software testing is a crucial step in ensuring software quality and reliability. With the increasing prevalence of continuous integration / continuous delivery, the automated generation of test cases and test code is becoming increasingly important in shortening release cycles and reducing labor costs. Test adequacy is typically measured by metrics such as coverage and defect detection rate. Among these, mutation testing, which introduces small semantic changes into the program under test and counts the proportion of mutants killed by test cases to evaluate the effectiveness of the test suite, is widely considered by industry and academia to be closer to defect detection capability than simple structural coverage. However, mutation testing is costly in engineering practice: large-scale mutant generation, instrumentation execution, and regression analysis all incur significant time and resource consumption. How to control generation and execution costs while ensuring the kill rate has become a major challenge for automated testing technology.

[0003] The existing technical approaches for automatically generating test cases mainly include: 1) Random and template-based methods, which are simple to implement but have limited ability to explore complex paths and abnormal scenarios, and the assertion quality is unstable; 2) Search / evolutionary testing, which optimizes the input through a coverage-driven fitness function, can improve coverage, but is prone to getting stuck in local optima for code with complex constraints and strong environmental dependencies, and the call and execution costs are high; 3) Symbolic execution / constraint solving, which can systematically explore paths, but suffers from path explosion, constraint solving bottlenecks, and difficulty in modeling external environments and third-party dependencies; 4) Fuzz testing / grey-box testing, which is effective in program-level robustness verification and vulnerability discovery, but it is still challenging to transform its results into maintainable, replayable unit tests with high-quality assertions.

[0004] In recent years, large language models have been used to automatically generate test code and assertions. Through instruction fine-tuning (SFT) or retrieval enhancement, models can generate test cases, construct boundary inputs, and write assertions based on the code or document under test, thereby significantly reducing writing costs and improving test development efficiency. However, pure SFT solutions generally face the following challenges in engineering implementation: 1) Weak assertions, including assertion tautology and over-reliance on print statements / exceptions, leading to insufficient mutant elimination capabilities; 2) Insufficient generation stability, resulting in significant output differences due to minor contextual changes; 3) High inference latency and memory usage, making it difficult to match the minute-level CI constraints at the PR level and the resource limitations of local deployment; 4) Unstable generalization ability across projects and code styles, easily affected by training data distribution and code semantic perturbations.

[0005] In summary, existing technologies suffer from at least the following common problems: 1) It is difficult to balance test generation with high kill rate and low latency and low resource consumption, limiting engineering usability; 2) There is a mismatch between SFT and general distillation targets and core indicators in the testing domain, resulting in insufficient professional capabilities of student models; 3) Insufficient generation stability and reproducibility, sensitivity to random seeds, prompt templates or semantically equivalent perturbations, affecting reliable deployment in CI / CD; 4) Lack of invariant constraints on semantically equivalent perturbations, leading to test case degradation during refactoring and code style evolution.

[0006] Therefore, how to provide a test generation technology that relies solely on supervised fine-tuning and distillation to achieve variation-sensitive and semantically robust results under practical engineering constraints is a problem that urgently needs to be solved by those skilled in the art. Summary of the Invention

[0007] In view of the above problems, the present invention proposes a method and system for fine-tuning a large language model and testing software based on knowledge distillation, so as to overcome the above problems or at least partially solve them.

[0008] To achieve the above objectives, the present invention adopts the following technical solution: In a first aspect, the present invention provides a method for fine-tuning a large language model and testing software based on knowledge distillation, including: a supervised fine-tuning stage, a knowledge distillation stage, and a reasoning stage; During the supervised fine-tuning phase, a pre-trained large model is used as the backbone, and a LoRA low-rank adapter is inserted. The structured unit test generation paradigm is learned under the conditions of the code to be tested and the requirements, and a student model is obtained. In the knowledge distillation stage, a teacher model is introduced, and the student model is trained by using soft target distillation, hard target supervision, Jensen-Shannon distillation and semantic consistency regularization as constraints. During the inference phase, the software code under test is tested based on the trained student model.

[0009] Furthermore, during the supervised fine-tuning phase, each training sample is reduced to a tuple (Prompt, Chosen). The Prompt is the input template, which is assembled from the code to be tested (CODE), the interface / exception contract (SIG), the requirement specification (INTENT), and the historical examples (HIST). The Chosen is the content that the large model corresponding to the input template should return, i.e., the reference answer, which includes four parts: SETUP, ACT, ASSERT, and EXPLAIN.

[0010] Furthermore, during the supervised fine-tuning phase, a LoRA low-rank adapter is inserted only in the attention layer or feedforward layer, making the layer weights as follows: ,in, This indicates the updated layer weights. Representation layer original weights, This represents a low-rank increment.

[0011] Furthermore, the loss calculation method during the monitoring and fine-tuning phase is as follows: Apply intra-segment masks to the truly supervised tokens within the target sequence corresponding to Chosen in the training samples, minimizing the masked conditional cross-entropy loss:

[0012] in, Let x be the set of trainable parameters and x be the input template. The conditional context, The token for the target answer at position i. for Previous prefix token sequence; For the model in parameters The conditional probability is given below; N is the length of the target sequence; The segment supervision mask is set to 1 if and only if the token belongs to one of the four supervised regions: CODE, SIG, INTENT, and HIST. Display the alignment requirements and test intent, add a multi-label auxiliary header on the encoding side, predict the test intent label from the encoding side output, and calculate the binary cross-entropy loss:

[0013] in, This represents the model's predicted probability vector for each dimension of test intent; For the corresponding one-hot truth value label or multiple-hot truth value label, the one-hot truth value label is used for single-label tasks, and the multiple-hot truth value label is used for multi-label tasks; Conditional cross-entropy loss With binary cross-entropy loss The total loss during the monitoring and fine-tuning phase is expressed as:

[0014] in, , which is the weight of the binary cross-entropy loss.

[0015] Furthermore, the total loss in the knowledge distillation stage is expressed as:

[0016] in, This indicates the distillation loss of soft targets. This represents the hard target cross-entropy loss. This indicates Jensen–Shannon distillation. This represents the consistency regularization loss. These represent the weight hyperparameters of each loss component.

[0017] Furthermore, the formula for calculating the distillation loss of soft targets is as follows:

[0018] in, This indicates the distillation loss of soft targets; Temperature coefficient; KL divergence measures the difference between two distributions; This indicates that the teacher model is under the following input conditions. Below, after temperature Scaled probability distribution of the next token; This indicates that the student model is under the input conditions. Below, after temperature Scaled probability distribution of the next token; The conditional context for the input template Prompt.

[0019] Furthermore, the formula for calculating the hard target supervision loss is as follows:

[0020] in, For hard target supervision loss with masking; The total number of tokens in the target sequence; For sequence position index; Indicates the intra-segment supervision mask, when the first segment... When a token belongs to a supervised area, Otherwise, it is 0; Indicates the student model in parameters Given the following input conditions With prefix At that time, generate The conditional probability; Indicates the location of the reference answer. The truth token at the location; For reference answer The prefix token sequence.

[0021] Furthermore, the formula for calculating Jensen–Shannon distillation loss is as follows:

[0022] in, This indicates the Jensen–Shannon distillation loss; This represents the conditional distribution of the next token in the teacher model. Indicates the conditional distribution of the student's next token; Let KL divergence be a metric. express and The mean distribution.

[0023] Furthermore, the formula for calculating the consistency regularization loss is as follows:

[0024] in, This represents the consistency regularization loss; Indicates the first A semantically preserving transformation; , These represent the next token distributions of the student model on the original input and the transformed input, respectively; Let KL divergence be denoted as KL divergence.

[0025] Secondly, the present invention provides a large language model fine-tuning and software testing system based on knowledge distillation, which employs the method described above, including: The supervised fine-tuning module is used to learn a structured unit test generation paradigm under the conditions of the code to be tested and the requirements, using a pre-trained large model as the backbone and inserting a LoRA low-rank adapter to obtain a student model. The knowledge distillation module is used to introduce the teacher model and train the student model by using soft target distillation, hard target supervision, Jensen-Shannon distillation and semantic consistency regularization as constraints. The inference module tests the software code under test based on a trained student model.

[0026] As can be seen from the above technical solution, compared with the prior art, the present invention has the following beneficial effects: 1. In the supervised fine-tuning stage, the present invention learns a structured testing paradigm, which aims to enable the model to master the basic skills of unit test generation; in the knowledge distillation stage, teacher knowledge is injected and semantic robustness and consistency are strengthened, which aims to make the student model closer to the teacher distribution in terms of correctness, readability and clarity of intent under the teacher-student paradigm; at the same time, the semantic transformation is maintained to keep the output consistent, thereby improving semantic robustness and resistance to anti-cue vulnerability.

[0027] 2. In the supervision and fine-tuning stage, this invention uses structured templates such as SETUP / ACT / ASSERT / EXPLAIN and intent multi-label supervision. In the distillation stage, it inherits the teacher's assertion selection and writing style, reducing false positives / false negatives. The explanatory fields facilitate code review and regression localization, thereby enhancing the accuracy and interpretability of assertions.

[0028] 3. This invention applies a distribution consistency regularization to the semantically equivalent perturbation family to ensure that the output remains unchanged before and after the perturbation; it improves semantic robustness and stability under multiple verifications, different random seeds and minor context changes.

[0029] 4. This invention preserves the generation preference of "boundary / abnormal path priority" by combining soft target distillation and hard target constraints in the teacher model. Compared with methods driven solely by structure coverage, it can obtain more meaningful coverage gain on branches / abnormal paths, resulting in higher and more balanced coverage quality.

[0030] 5. This invention uses LoRA parameter optimization and knowledge distillation to compress the teacher's model capabilities to the student, significantly reducing memory usage and latency while maintaining key testing behaviors. It meets the CI / CD constraints of PR-level minute-scale and the resource limitations of localized deployment, achieving low-cost and high-efficiency deployment. Attached Figure Description

[0031] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0032] Figure 1 This is a flowchart of a method for fine-tuning a large language model based on knowledge distillation and software testing provided in an embodiment of the present invention. Figure 2 This is a detailed architecture diagram of the knowledge distillation-based large language model fine-tuning and software testing method provided in this embodiment of the invention. Detailed Implementation

[0033] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0034] like Figures 1-2 As shown, this embodiment of the invention discloses a method for fine-tuning a large language model and testing software based on knowledge distillation, including: a supervised fine-tuning stage, a knowledge distillation stage, and an inference stage; During the supervised fine-tuning phase, a pre-trained large model is used as the backbone, and a LoRA low-rank adapter is inserted. The structured unit test generation paradigm is learned under the conditions of the code to be tested and the requirements. The large model trained in this phase is regarded as the student model. In the knowledge distillation stage, a teacher model is introduced, and the student model is trained by using soft target distillation, hard target supervision, Jensen-Shannon distillation and semantic consistency regularization as constraints. During the inference phase, the software code under test is tested based on the trained student model.

[0035] The following section provides further explanation of the monitoring and fine-tuning phase and the knowledge distillation phase.

[0036] 1) In the supervised fine-tuning phase (SFT), learn structured test paradigms. The goal is to enable the model to master the basics of unit test generation, including the arrange-act-assert paradigm, exception / boundary assertion templates, and conditional understanding of "code to be tested and functional / boundary / exception requirements".

[0037] ① Acquisition of training samples and model construction: Each training sample is reduced to a tuple (Prompt, Chosen). The Prompt is the input template, also known as the prompt word, which is composed of the code to be tested (i.e., the specific implementation of the function under test), the interface / exception contract (SIG) (including the function signature and exception throwing conditions and other behavioral specifications), the requirement specification (INTENT) (the functional or business rules described in natural language), and the historical examples (HIST) (containing past test cases or failure records to provide contextual evolution information). The Chosen is the content that the large model corresponding to the input template should return, i.e., the reference answer, which includes four parts: SETUP, ACT, ASSERT, and EXPLAIN.

[0038] The large model uses the pre-trained large model Qwen2.5-3B-Instruct as its backbone, freezing most of the base weights, and inserting LoRA low-rank adapters only in the attention layer or feedforward layer, making the layer weights as follows: ,in, This indicates the updated layer weights. Representation layer original weights, Indicates a low-rank increment; , , , , Let W be the set of real numbers; A and B are trainable small matrices, representing two factors of the low-rank increment ΔW; d represents the dimension of the original weight matrix W (i.e., the hidden layer dimension), typically 0. r is a low-rank parameter set by the user, much smaller than d, for example, r = 8 or 16, which reduces the number of trainable parameters; therefore, if the original weights Then there is , .

[0039] Because LoRA is used for training, there is no need to directly update the complete weight matrix W. Instead, only the low-rank increment ΔW needs to be trained, which significantly reduces the number of training parameters and reduces memory usage and computational overhead.

[0040] ② Loss calculation during the monitoring and fine-tuning phase: Apply intra-segment masks to the truly supervised tokens within the target sequence corresponding to Chosen in the training samples, minimizing the masked conditional cross-entropy loss:

[0041] in, The set of trainable parameters (including LoRA weights for each layer and necessary layer normalization / output layer parameters), where x is the input template. The conditional context, The token for the target answer at position i. for Previous prefix token sequence; For the model in parameters The conditional probability is given below; N is the length of the target sequence; It is the intra-segment supervision mask, which is 1 if and only if the token belongs to one of the four supervised regions: CODE, SIG, INTENT, and HIST.

[0042] Display the alignment requirements and test intent, add a multi-label auxiliary header on the encoding side, predict the test intent label from the encoding side output, and calculate the binary cross-entropy loss:

[0043] in, This represents the model's predicted probability vector for each test intent dimension (including functionality / boundaries / anomalies / optional performance, etc.); This refers to either a one-hot or multiple-hot truth label. A one-hot truth label is a vector where exactly one of the K categories is 1 and the rest are 0. It is used for single-label tasks, where each sample belongs to only one class. A multiple-hot truth label allows multiple 0 / 1 vectors with 1 at each position. For example, [1,0,1,0] indicates that the sample has both the first and third attributes. It is used for multi-label tasks, where each sample can have multiple intents / attributes at the same time.

[0044] Conditional cross-entropy loss With binary cross-entropy loss The total loss during the monitoring and fine-tuning phase is expressed as:

[0045] in, , which is the weight of the binary cross-entropy loss.

[0046] ③ The specific training and verification process for the supervision and fine-tuning phase: For training samples Perform word segmentation and length trimming to ensure Information completeness and Constructing segment markers and masks to prevent overflow of maximum length. The batch size B is fed into the model, and the AdamW optimizer is used to update the data. Learning rate Combined with linear preheating and cosine annealing, weight decay Suppress overfitting.

[0047] Gradients are aggregated by the number of accumulation steps and then processed using a threshold c. Pruning is used to stabilize training; mixed precision and Dropout are used for further cost reduction and regularization.

[0048] Training and validation are divided into time-series segments, and validation metrics include... Intra-segment token accuracy and intent prediction F1 score: ,

[0049] In this model, TP (True Positive) represents the number of positive samples predicted as positive. FP (False Positive) represents the number of negative samples incorrectly predicted as positive. FN (False Negative) represents the number of positive samples incorrectly predicted as negative. Precision indicates the percentage of truly positive samples among those correctly identified by the model. Recall indicates the percentage of truly positive samples identified by the model. F1 represents the harmonic mean of precision and recall, used to achieve a balance between the two.

[0050] If the conditional cross-entropy loss of the validation set If there is no improvement within 5 epochs, the process is stopped early. Finally, the checkpoint with the best validation metric is used as the reference strategy for SFT output to ensure that the model has learned the paradigms, anomalies and boundary assertion templates in the training set, and that the conditional alignment of the code to be tested and the requirements specification achieves knowledge distillation that can be used for subsequent steps.

[0051] 2) Knowledge Distillation (JKD) stage: This stage injects teacher knowledge and strengthens semantic robustness and consistency. Its aim is to make the student model, within the teacher-student paradigm, closely approximate the teacher's distribution in terms of correctness, readability, and clarity of intent. Simultaneously, it maintains output consistency across semantic transformations (equivalent rewriting, renaming, harmless insertion, formatting, etc.), thereby enhancing semantic robustness and resistance to anti-cueing vulnerabilities. The student model (with parameters denoted as...) obtained in step 1) is used in this stage. Based on this, a larger and more powerful teacher model (denoted as ) is introduced. (Parameters are fixed and not updated). The model is trained using soft target distillation, hard target supervision, Jensen-Shannon distillation, and semantic consistency regularization as a joint loss. This significantly improves the student model's alignment with the requirements-code-test paradigm and its semantic robustness without increasing the number of trainable parameters. In implementation, LoRA is continued to update a small number of parameters on the student side with low overhead, ensuring consistent / aligned word segmentation and vocabulary with the teacher's side. The specific process is as follows: For each training sample As a condition, with As a reference answer, a teacher model is used to force the minimization of joint loss on the student side. .

[0052] First, temperature distillation is performed to fit the soft distribution of the teacher. The formula for calculating the soft target distillation loss is:

[0053] in, This represents the soft-target distillation loss, intended to allow the student model to fit the temperature-scaled distribution of the teacher model. For temperature coefficient, , The larger the value, the smoother the distribution, and the better it reflects the teacher model's preference for suboptimal words; the preceding coefficients... It is a common temperature correction item; KL divergence measures the difference between two distributions; This indicates that the teacher model is under the following input conditions. Below, after temperature Scaled probability distribution of the next token; Represents the student model (parameters are) ) In input conditions Below, after temperature Scaled probability distribution of the next token; The conditional context for the input template Prompt includes the code to be tested, the contract, requirements, etc.

[0054] Subsequently, the hard target cross-entropy of the intra-segment mask is used to solidify the accurate imitation of the response-paradigm. The formula for calculating the hard target supervision loss is as follows:

[0055] in, For hard target supervision loss with masking; The total number of tokens in the target sequence (and the reference answer sequence); From 1 to Sequence position index; Indicates the intra-segment supervision mask, when the first segment... When a token belongs to a supervised area, Otherwise, it is 0; Indicates the student model in parameters Given the following input conditions With prefix At that time, generate The conditional probability; Indicates the location of the reference answer. The truth token at the location; For reference answer The prefix token sequence, from 1 to .

[0056] Next, Jensen-Shannon distillation, which offers better stability, is introduced to mitigate numerical instability under extreme distributions. The formula for calculating the Jensen-Shannon distillation loss is as follows:

[0057] in, This indicates the Jensen–Shannon distillation loss; This represents the conditional distribution of the next token in the teacher model. This represents the conditional distribution of the student's next token, omitting the condition. The symbol is understood as being related to Same context; Let KL divergence be a metric. express and The mean distribution.

[0058] Finally, construct an equivalence family of the same input for semantically preserving transformations (identifier renaming, harmless insertion, equivalent rewriting, formatting, etc.). Consistency regularization is used to solidify the invariance of equivalent rewrites. The formula for calculating the consistency regularization loss is:

[0059] in, This represents the consistency regularization loss; Indicates the first These semantically preserving transformations include renaming, harmless insertion, equivalent expression replacement, and formatting. , These represent the next token distributions of the student model on the original input and the transformed input, respectively; The KL divergence encourages the two distributions to be as consistent as possible, thereby improving robustness and invariance.

[0060] The final total loss after the merger is expressed as:

[0061] in, This indicates the distillation loss of soft targets. This represents the hard target cross-entropy loss. This indicates Jensen–Shannon distillation. This represents the consistency regularization loss. All are greater than or equal to 0, representing the weight hyperparameters of each loss component. The overall goal is to approximate the teacher distribution (soft / hard / symmetric) while ensuring invariance of equivalent rewriting and representation alignment, ultimately obtaining a semantically robust student model with output style / paradigm alignment.

[0062] Overall, this invention consists of a two-stage method consisting of supervised fine-tuning and knowledge distillation. In the supervised fine-tuning stage, a mapping is established between requirement semantics, test intent, and assertion strategy by using four fields: structured Prompt code (CODE), function signature (SIG), test intent (INTENT), and historical information (HIST), and four parts: test generation template preconditions (SETUP), execution operation (ACT), assertion (ASSERT), and explanation (EXPLAIN). Furthermore, intent multi-labels and intra-segment masks are used to enhance the learning of assertions and exception paths.

[0063] During the knowledge distillation stage, a strong teacher model (such as Qwen2.5-14B-Instruct) is used to teach students soft and hard objectives. Pairwise preference distillation and semantic equivalence perturbation consistency regularization are combined to target and solidify mutant-sensitive assertion reasoning, anomaly / boundary coverage, and maintainable style. To reduce costs and adapt resources, LoRA low-overhead training is introduced. Ultimately, without introducing reinforcement learning, higher mutant elimination rate and assertion accuracy, better stability and semantic robustness, as well as lower training and inference overhead and stronger cross-project generalization ability are achieved.

[0064] In another embodiment, the present invention also provides a large language model fine-tuning and software testing system based on knowledge distillation, which employs the above-described method, including: The supervised fine-tuning module is used to learn a structured unit test generation paradigm under the conditions of the code to be tested and the requirements, using a pre-trained large model as the backbone and inserting a LoRA low-rank adapter to obtain a student model. The knowledge distillation module is used to introduce the teacher model and train the student model by using soft target distillation, hard target supervision, Jensen-Shannon distillation and semantic consistency regularization as constraints. The inference module tests the software code under test based on a trained student model.

[0065] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to the method section.

[0066] The above description of the disclosed embodiments enables those skilled in the art to make or use the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A method for fine-tuning a large language model and testing software based on knowledge distillation, characterized in that, include: The process includes the monitoring and fine-tuning phase, the knowledge distillation phase, and the reasoning phase. During the supervised fine-tuning phase, a pre-trained large model is used as the backbone, and a LoRA low-rank adapter is inserted. The structured unit test generation paradigm is learned under the conditions of the code to be tested and the requirements, and a student model is obtained. In the knowledge distillation stage, a teacher model is introduced, and the student model is trained by using soft target distillation, hard target supervision, Jensen-Shannon distillation and semantic consistency regularization as constraints. During the inference phase, the software code under test is tested based on the trained student model.

2. The method for fine-tuning a large language model based on knowledge distillation and software testing as described in claim 1, characterized in that, During the supervised fine-tuning phase, each training sample is reduced to a tuple (Prompt, Chosen). Prompt is the input template, which is assembled from the code to be tested (CODE), interface / exception contract (SIG), requirement specification (INTENT), and historical examples (HIST). Chosen is the content that the large model corresponding to the input template should return, i.e., the reference answer, which includes four parts: preconditions (SETUP), execution operations (ACT), assertions (ASSERT), and explanations (EXPLAIN).

3. The method for fine-tuning a large language model based on knowledge distillation and software testing as described in claim 1, characterized in that, During the supervised fine-tuning phase, a LoRA low-rank adapter is inserted only in the attention layer or feedforward layer, making the layer weights as follows: ,in, This indicates the updated layer weights. Representation layer original weights, This represents a low-rank increment.

4. The method for fine-tuning a large language model based on knowledge distillation and software testing as described in claim 2, characterized in that, The loss calculation method during the monitoring and fine-tuning phase is as follows: Apply intra-segment masks to the truly supervised tokens within the target sequence corresponding to Chosen in the training samples, minimizing the masked conditional cross-entropy loss: in, Let x be the set of trainable parameters and x be the input template. The conditional context, The token for the target answer at position i. for Previous prefix token sequence; For the model in parameters The conditional probability is given below; N is the length of the target sequence; The segment supervision mask is set to 1 if and only if the token belongs to one of the four supervised regions: CODE, SIG, INTENT, and HIST. Display the alignment requirements and test intent, add a multi-label auxiliary header on the encoding side, predict the test intent label from the encoding side output, and calculate the binary cross-entropy loss: in, This represents the model's predicted probability vector for each dimension of test intent; For the corresponding one-hot truth value label or multiple-hot truth value label, the one-hot truth value label is used for single-label tasks, and the multiple-hot truth value label is used for multi-label tasks; Conditional cross-entropy loss With binary cross-entropy loss The total loss during the monitoring and fine-tuning phase is expressed as: in, , which is the weight of the binary cross-entropy loss.

5. The method for fine-tuning a large language model based on knowledge distillation and software testing as described in claim 1, characterized in that, The total loss during the knowledge distillation stage is expressed as: in, This indicates the distillation loss of soft targets. This represents the hard target cross-entropy loss. This indicates Jensen–Shannon distillation. This represents the consistency regularization loss. These represent the weight hyperparameters of each loss component.

6. The method for fine-tuning a large language model based on knowledge distillation and software testing as described in claim 5, characterized in that, The formula for calculating the distillation loss of soft targets is: in, This indicates the distillation loss of soft targets; Temperature coefficient; KL divergence measures the difference between two distributions; This indicates that the teacher model is under the following input conditions. Below, after temperature Scaled probability distribution of the next token; This indicates that the student model is under the input conditions. Below, after temperature Scaled probability distribution of the next token; The conditional context for the input template Prompt.

7. The method for fine-tuning a large language model based on knowledge distillation and software testing as described in claim 5, characterized in that, The formula for calculating the hard target supervision loss is: in, For hard target supervision loss with masking; The total number of tokens in the target sequence; For sequence position index; Indicates the intra-segment supervision mask, when the first segment... When a token belongs to a supervised area, Otherwise, it is 0; Indicates the student model in parameters Given the following input conditions With prefix At that time, generate The conditional probability; Indicates the location of the reference answer. The truth token at the location; For reference answer The prefix token sequence.

8. The method for fine-tuning a large language model based on knowledge distillation and software testing as described in claim 5, characterized in that, The formula for calculating Jensen–Shannon distillation loss is: in, This indicates the Jensen–Shannon distillation loss; This represents the conditional distribution of the next token in the teacher model. Indicates the conditional distribution of the student's next token; Let KL divergence be a metric. express and The mean distribution.

9. The method for fine-tuning a large language model based on knowledge distillation and software testing as described in claim 5, characterized in that, The formula for calculating the consistency regularization loss is: in, This represents the consistency regularization loss; Indicates the first A semantically preserving transformation; , These represent the next token distributions of the student model on the original input and the transformed input, respectively; Let KL divergence be denoted as KL divergence.

10. A large language model fine-tuning and software testing system based on knowledge distillation, characterized in that, It employs the method described in any one of claims 1-9, comprising: The supervised fine-tuning module is used to learn a structured unit test generation paradigm under the conditions of the code to be tested and the requirements, using a pre-trained large model as the backbone and inserting a LoRA low-rank adapter to obtain a student model. The knowledge distillation module is used to introduce the teacher model and train the student model by using soft target distillation, hard target supervision, Jensen-Shannon distillation and semantic consistency regularization as constraints. The inference module tests the software code under test based on a trained student model.