Code generation method and system based on data enhancement and hybrid similarity negative sampling

CN122132012BActive Publication Date: 2026-07-03SHANDONG NORMAL UNIV +1

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SHANDONG NORMAL UNIV
Filing Date
2026-05-07
Publication Date
2026-07-03

Smart Images

  • Figure CN122132012B_ABST
    Figure CN122132012B_ABST
Patent Text Reader

Abstract

This invention relates to the field of automated code generation technology, providing a code generation method and system based on data augmentation and hybrid similarity negative sampling. The method includes: generating multiple sets of candidate codes and test cases based on a variant question set using multiple different large language models; iteratively calculating the credibility score of the candidate codes and the quality score of the test cases; selecting the candidate code with the highest credibility score as the standard answer code; and determining the test cases that pass the standard answer code as valid test cases; generating multiple extended codes and verifying their correctness using valid test cases; selecting the code with the shortest execution time as a positive sample from the correct codes; and selecting erroneous codes whose hybrid similarity meets a preset threshold as negative samples; and fine-tuning and training the target code generation model before using the target code generation model to generate code. This method combines high accuracy with high execution efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of automated code generation technology, and in particular relates to a code generation method and system based on data augmentation and hybrid similarity negative sampling. Background Technology

[0002] The statements in this section are merely background information related to the present invention and do not necessarily constitute prior art.

[0003] With the rapid development of deep learning and Large Language Model (LLM) technologies, automatic code generation has become an important tool for assisting software development. Existing code generation models (such as StarCoder and DeepSeek-Coder) have acquired the ability to understand natural language requirements and generate corresponding code snippets through pre-training on large-scale code corpora and instruction fine-tuning (SFT). These technologies have significantly lowered the programming threshold and improved the efficiency of software development.

[0004] However, current training methods (including SFT and conventional Reinforcement Learning from Human Feedback (RLHF)) primarily focus on the "functional correctness" of the generated code (i.e., whether it passes test cases), often neglecting the code's "execution efficiency" and "quality." In practical engineering applications, code not only needs to produce correct results but also requires low time complexity. Code generated by existing technologies often suffers from logical redundancy, low algorithmic efficiency, or excessive resource consumption. Most current training datasets lack explicit optimization signals for "code execution efficiency," making it difficult for models to distinguish between "runnable but slow" code and "runnable and fast" code, thus failing to meet the demands of high-performance computing scenarios.

[0005] In the training phase of large-scale code model preference alignment (such as Direct Preference Optimization (DPO) and Conditional Preference Optimization (CPO), constructing high-quality "cue word-positive sample-negative sample" triplet data is crucial for improving model performance. However, existing negative sample construction methods have significant drawbacks, primarily manifested in overly simplistic negative sampling strategies and a lack of awareness of the deep structure of the code. Specifically, existing techniques typically randomly select erroneous generated code as negative samples. This approach makes it difficult to control the quality of negative samples, often resulting in "simple negative samples" due to obvious errors or completely irrelevant logic, failing to enable the model to learn subtle logical differences. Furthermore, in measuring the similarity between positive and negative samples, existing methods often rely solely on surface-level text edit distance or simple semantic similarity, failing to deeply consider the unique structural features of the code, such as Abstract Syntax Trees (ASTs), Control Flow Graphs (CFGs), and variable naming styles. This lack of dimension makes it difficult to discover "hard negative samples" that are structurally similar to positive samples but logically flawed, thus failing to provide effective gradient signals for the model. Therefore, existing technologies cannot accurately control the appropriate "similarity distance" between positive and negative samples, making it difficult for models to balance the correctness of code logic and the optimization of code structure during training, thus failing to meet the need to generate efficient and standardized code.

[0006] Furthermore, building a high-quality code instruction dataset that covers multiple dimensions (such as depth of thought, engineering quality, and breadth of functionality) is extremely expensive. Although large models can be used for synthetic data augmentation, the generated code and test cases often contain a lot of noise (such as code that cannot run or test case errors). Summary of the Invention

[0007] To address the technical problems mentioned above, this invention provides a code generation method and system based on data augmentation and hybrid similarity negative sampling. By generating multi-dimensional variants and using a consensus mechanism based on a bipartite graph collaborative sorting algorithm, high-quality "standard answers" and test cases are constructed. Furthermore, a positive and negative sample selection strategy that balances code execution efficiency and structural similarity is introduced, thereby training a code generation model with both high accuracy and high execution efficiency.

[0008] To achieve the above objectives, the present invention adopts the following technical solution:

[0009] The first aspect of the present invention provides a code generation method based on data augmentation and hybrid similarity negative sampling, comprising:

[0010] Obtain a set of code programming problems, and use a large language model to rewrite and mutate the set of code programming problems from multiple dimensions to generate a set of variant problems;

[0011] Based on the variant problem set, multiple sets of candidate codes and test cases are generated through multiple different large language models as nodes. If a candidate code passes a test case, a connection edge is established between the two to construct a code-test case bipartite graph. The credibility score of the candidate code and the quality score of the test case are calculated iteratively. The candidate code with the highest credibility score is selected as the standard answer code, and the test cases that pass the standard answer code are determined as valid test cases.

[0012] For the original problem set and variant problem set, multi-dimensional efficiency optimization suggestions are provided to generate multiple extended codes. The correctness of the multiple extended codes is verified by using effective test cases to obtain correct codes and error codes. Among the correct codes, the code with the shortest execution time is selected as a positive sample. The mixture similarity between the error code and the positive sample is calculated. The error code whose mixture similarity meets the preset threshold is selected as a negative sample.

[0013] Preference-aligned training data is constructed using a set of code programming problems, positive samples, and negative samples. After fine-tuning the target code generation model, code generation is performed using the target code generation model.

[0014] Furthermore, the multiple dimensions include application background, engineering quality, functional breadth, depth of thinking, and technical implementation.

[0015] Furthermore, in the t-th iteration, the candidate code c i Credibility score t (c i ) and test cases t j Quality Score t (t j They are respectively:

[0016] ;

[0017] ;

[0018] In the formula, t represents the current iteration round; and represents the confidence score of the candidate code and the quality score of the test case in the (t-1)th iteration, respectively; d represents the damping update factor; Represents test case t j With candidate code c i The relationship between them is identified by the candidate code c. i The test case t was successfully run and passed. j ,but =1, otherwise, =0; and They have the same meaning.

[0019] Furthermore, the mixed similarity between the error code and the positive sample is represented as:

[0020] ;

[0021] In the formula, This represents the control flow similarity calculated based on the basic blocks and transition relationships of the control flow graph. This represents the similarity of node structures calculated based on an abstract syntax tree; Indicates the sequence similarity between variable names and function names; The expression represents semantic similarity extracted based on the code vectorization model; w1, w2, w3, and w4 represent weight coefficients.

[0022] Furthermore, the preset threshold ensures that the selected negative samples are structurally similar to the positive samples but logically incorrect.

[0023] A second aspect of the present invention provides a code generation system based on data augmentation and hybrid similarity negative sampling, comprising:

[0024] The data augmentation module obtains a set of code programming problems, and uses a large language model to rewrite and mutate the set of code programming problems from multiple dimensions to generate a set of variant problems.

[0025] The truth mining module, based on a variant question set, generates multiple sets of candidate codes and test cases through multiple different large language models. These serve as nodes. If a candidate code passes a test case, a connection edge is established between the two, constructing a code-test case bipartite graph. The module iteratively calculates the credibility score of the candidate code and the quality score of the test cases, selects the candidate code with the highest credibility score as the standard answer code, and determines the test cases that the standard answer code passes as valid test cases.

[0026] The efficiency evaluation module provides multi-dimensional efficiency optimization suggestions for the original problem set and variant problem set to generate multiple extended codes. It then uses effective test cases to verify the correctness of the multiple extended codes, obtaining correct codes and incorrect codes. Among the correct codes, the code with the shortest execution time is selected as a positive sample. The mixed similarity between the incorrect code and the positive sample is calculated, and the incorrect code whose mixed similarity meets the preset threshold is selected as a negative sample.

[0027] The model training module uses a set of code programming problems, positive samples, and negative samples to construct preference-aligned training data. After fine-tuning the target code generation model, it generates code using the target code generation model.

[0028] Furthermore, the multiple dimensions include application background, engineering quality, functional breadth, depth of thinking, and technical implementation.

[0029] Furthermore, in the t-th iteration, the candidate code c i Credibility score t (c i ) and test cases t j Quality Score t (t j They are respectively:

[0030] ;

[0031] ;

[0032] In the formula, t represents the current iteration round; and represents the confidence score of the candidate code and the quality score of the test case in the (t-1)th iteration, respectively; d represents the damping update factor; Represents test case t j With candidate code c i The relationship between them is identified by the candidate code c. i The test case t was successfully run and passed. j ,but =1, otherwise, =0; and They have the same meaning.

[0033] Furthermore, the mixed similarity between the error code and the positive sample is represented as:

[0034] ;

[0035] In the formula, This represents the control flow similarity calculated based on the basic blocks and transition relationships of the control flow graph. This represents the similarity of node structures calculated based on an abstract syntax tree; Indicates the sequence similarity between variable names and function names; The expression represents semantic similarity extracted based on the code vectorization model; w1, w2, w3, and w4 represent weight coefficients.

[0036] Furthermore, the preset threshold ensures that the selected negative samples are structurally similar to the positive samples but logically incorrect.

[0037] Compared with the prior art, the beneficial effects of the present invention are:

[0038] This invention constructs high-quality "standard answers" and test cases through multi-dimensional variant generation and a consensus mechanism based on a bipartite graph-based collaborative sorting algorithm. It also introduces a positive and negative sample selection strategy that balances code execution efficiency and structural similarity, thereby training a code generation model with both high accuracy and high execution efficiency. Attached Figure Description

[0039] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.

[0040] Figure 1 This is a flowchart of the code generation method based on data augmentation and hybrid similarity negative sampling according to Embodiment 1 of the present invention;

[0041] Figure 2 This is a schematic diagram of the truth mining framework based on collaborative sorting in Embodiment 1 of the present invention. Detailed Implementation

[0042] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings.

[0043] It should be noted that the following detailed description is illustrative and intended to provide further explanation of the invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains.

[0044] Example 1

[0045] This embodiment provides a code generation method based on data augmentation and hybrid similarity negative sampling.

[0046] Existing technologies lack an effective, automated mechanism to sift out highly reliable "standard answers" and "effective test cases" from massive amounts of synthetic data, which limits further improvements in model performance.

[0047] The code generation method based on data augmentation and hybrid similarity negative sampling provided in this embodiment constructs high-quality "standard answers" and test cases through multi-dimensional variant generation and consensus mechanism based on bipartite graph collaborative sorting algorithm. It also introduces a positive and negative sample screening strategy that takes into account both code execution efficiency and structural similarity, thereby training a code generation model with both high accuracy and high execution efficiency.

[0048] To address the issues of existing code generation models neglecting code execution efficiency, high training data noise, and a single negative sampling strategy, this embodiment provides a code generation method based on data augmentation and hybrid similarity negative sampling. First, it utilizes a large language model to expand the variations of basic programming problems from five dimensions, including application background and depth of thought, thereby increasing data diversity. Second, it innovatively introduces a bipartite graph-based collaborative ranking algorithm to accurately filter highly credible standard answers and effective test cases from massive synthetic data, automatically filtering noise. In the preference data construction stage, it implements efficiency-oriented positive sample screening, selecting the correct code with the shortest execution time through sandbox testing. Simultaneously, it proposes a hybrid similarity calculation method based on control flow, AST, naming, and semantics to discover "difficult negative samples" (preferably with a similarity threshold of 0.3) that are structurally similar to positive samples but logically flawed. Finally, it uses the constructed high-quality triplet data for preference alignment training, enabling the model to achieve both high accuracy and high execution efficiency.

[0049] The code generation method based on data augmentation and hybrid similarity negative sampling provided in this embodiment, such as Figure 1 and Figure 2 As shown, the specific steps include:

[0050] Step S1: Generation of multidimensional variant data.

[0051] Specifically, a set of code programming problems is obtained, and a large language model is used to rewrite and mutate the set of code programming problems from multiple dimensions to generate a set of variant problems. These multiple dimensions include application background, engineering quality, functional breadth, depth of thinking, and technical implementation.

[0052] To address the shortcomings of existing public datasets (Basic Programming Problems Set MBPP and Advanced Programming Problems Set LBPP) in terms of insufficient sample size and limited problem formats, this embodiment employs the DeepSeekv3 language model to rewrite and expand the basic and advanced programming problems in multiple dimensions.

[0053] Specifically, such as Figure 2 The process for generating multidimensional variant problems in the first stage, as shown, includes: Terminating data preparation: selecting 374 problems from the MBPP training set and 161 Python problems from LBPP as seed data; setting the generation temperature to 1.5; prompting the large language model to add constraints or background descriptions from the following five dimensions to perform variant transformations while keeping the core logic of the original problem unchanged:

[0054] Application context variant (i.e., application context dimension injection): embedding pure algorithmic problems into specific real-world application scenarios (such as financial computing, game logic, network communication).

[0055] Engineering quality variant (i.e., engineering quality dimension injection): adds requirements for engineering elements such as exception handling, input validation, and logging;

[0056] Functional breadth variant (i.e., functional breadth dimension injection): Add related functional requirements to the original problem;

[0057] The thinking depth variant (i.e., thinking depth dimension injection): increases the logical complexity of the problem and requires a deeper level of algorithm derivation;

[0058] Technical implementation variants (i.e., technical implementation dimension injection): require the use of specific libraries, frameworks, or programming paradigms (such as functional programming) to solve the same problem.

[0059] Through the above steps, this embodiment ultimately constructed a high-quality variant question set containing 2675 questions.

[0060] Step S2: Truth filtering based on code-test case bipartite graph collaborative sorting.

[0061] Specifically, the variant problem set is input into multiple different large language models to generate multiple sets of candidate codes and test cases; a reference network between candidate codes and test cases is constructed, and the weight of each candidate code is calculated using a bipartite graph-based collaborative sorting algorithm. The code with the highest weight is selected as the standard answer code, and the test cases that pass the standard answer code are determined as valid test cases, thereby constructing a (problem, standard answer code, valid test case) triplet.

[0062] To address the issue that the quality of the large number of candidate codes generated by the variant problem varies greatly, and that the synthesized test cases may contain errors (such as logical errors or assertion errors), this embodiment proposes a co-ranking algorithm based on a bipartite graph. This algorithm utilizes the mutual verification logic that "high-quality code tends to pass high-quality test cases, and high-quality test cases tend to be passed by high-quality code," while simultaneously filtering the standard answer and valid test cases.

[0063] like Figure 2 The second-stage candidate set construction and the third-stage bipartite graph collaborative screening are shown in the diagram. The specific execution process is as follows:

[0064] Step 201: Input the high-quality variant question set (2675 questions) into multiple different large language models (i.e., multi-model generation engines, including: DeepSeek, Spark, Qwen or QwQ, DeepSeekv3), generate multiple sets of candidate code and test cases, and obtain the candidate code set VC (with noise) and the test case set VT (with noise).

[0065] Step 202: Construct a code-test case bipartite graph (i.e., a bipartite graph structure). After initializing the scores, candidate code is designated as the first type of node, and test cases as the second type of node. If a candidate code passes a test case, a connection edge is established between the two. That is, a bipartite graph G = (VC∪VT,E) containing both types of nodes is constructed, where VC = {c1,c2,...,c...}. m} represents the set of candidate codes generated for the current problem, where m is the total number of candidate codes; VT={t1,t2,...,t n} represents the set of test cases generated for the current problem, where n is the total number of test cases; E is the edge set, if candidate code c i Successfully passed test case t j Then in candidate code node c i With test case node t j Establish connecting edges between them.

[0066] Step 203: Collaborative Iterative Calculation. Based on the bipartite graph, the quality score of each node is iteratively calculated using the collaborative update formula.

[0067] In the t-th iteration, the candidate code c i Credibility score t (c i ) and test cases t j Quality Score t (t j Update according to the following formulas respectively:

[0068] ;

[0069] ;

[0070] In the formula, the meanings of each variable and parameter are as follows: t represents the current iteration round (t=1,2,...,T), and T represents the total number of iterations; This indicates that after the t-th iteration, the i-th candidate code c i The confidence score indicates that the code is more likely to be the correct standard answer; This represents the j-th test case t after the t-th iteration. j The quality score indicates that the test case is more accurate and discriminative. The higher the score, the more accurate the test case is. and represents the scores of the code and test cases in the previous iteration (t-1); d represents the damping update factor, which takes a value of (0,1). This parameter is used to balance the historical scores of the previous iteration with the neighbor contribution values ​​calculated in the current iteration, so as to smooth the update and prevent oscillations. Represents test case t j With candidate code c i The relationship between them is identified by the code c. i If the test cases run and pass successfully, then =1, otherwise (including runtime errors or assertion failures). =0; and The two symbols have the same meaning, representing the symmetry of the edges in a bipartite graph; ∑ represents the summation operation.

[0071] The first line of the formula indicates that the score of the code depends on the weighted sum of the scores of all the test cases that pass it; the second line of the formula indicates that the score of the test case depends on the weighted sum of the scores of all the code that passes it.

[0072] As one implementation method, d=0.85 is set.

[0073] As another implementation method, the damping update factor is updated using the following formula:

[0074] ;

[0075] Where d0 is the basic damping coefficient, ranging from 0.7 to 0.9; t is the current iteration round; T is the total number of iterations; ρ is the iteration progress gain coefficient, ranging from 1.5 to 2.5, controlling the rate of increase of d in the later stages; α is the midpoint offset of the Sigmoid curve, ranging from 0.4 to 0.6, causing d to increase significantly in the middle of the iteration; k is the Sigmoid steepness coefficient, ranging from 3 to 6, controlling the smoothness of the transition; σ noise σ represents the data noise rate. noise = (Number of code that fails any test cases + Number of test cases that fail any test cases) / (m + n); β is the noise suppression coefficient, ranging from 0.3 to 0.6. The higher the noise, the lower the d.

[0076] The damping update factor can increase the mutual verification weight with iteration and automatically suppress the update intensity with noise, so that the iteration process can converge stably and improve the screening accuracy of standard answers and effective test cases.

[0077] Step 204: Determine the truth value.

[0078] (1) Initialization: Before the iteration begins (t=0), the scores of all code nodes and test case nodes are initialized to a uniform distribution, i.e. , .

[0079] (2) Iteratively calculate the credibility score of the candidate code and the quality score of the test cases; wherein, the credibility score of the candidate code is updated based on the weighted sum of the quality scores of all test cases that pass it, and the quality score of the test cases is updated based on the weighted sum of the credibility scores of all candidate codes that can pass it;

[0080] (3) Iteration termination: Repeat step 203 (i.e. step (2)) until the number of iterations reaches the preset threshold T (in this embodiment, preferably T=10) or the fraction change is less than the convergence threshold.

[0081] (4) Result screening: After the iteration is completed, the confidence score after the iteration is selected. The code with the largest value is taken as the unique standard answer code (Code Truth) for the problem; at the same time, all test cases that can pass the unique standard answer code are directly selected as valid test cases, resulting in the valid test case set (Test Truth).

[0082] Step S3: Efficiency-oriented candidate set expansion and evaluation.

[0083] Specifically, multiple efficiency optimization strategies are added to the original prompts, and extended code is generated for the question using a large language model. The correctness of the extended code is verified using effective test cases, and the execution time of the verified code is recorded.

[0084] After obtaining "valid test cases" with high confidence through step S2, this step aims to discover code implementations that are more efficient in execution, provided that the logic is correct.

[0085] Step 301, as follows Figure 1 The first stage shown is: multi-model collaboration and heterogeneous prompt-driven data generation.

[0086] Input the question set, and call the large language model again (DeepSeekv3, Spark-x1 (70 billion parameters) (70b), Qwen3 (235b) (235 billion parameters), QwQ (32b) (32 billion parameters) to generate multi-dimensional efficiency optimization for the original question set and variant question set.

[0087] Specifically, in addition to the basic default instructions, the original problem's hints also include five different efficiency optimization and heterogeneous generation instructions to drive the generation of large-scale candidate code. These instructions specifically include:

[0088] Forced time complexity optimization: requires the model to forcibly reduce time complexity at the algorithm level;

[0089] Pre-computation and lookup table optimization: The model is required to adopt a space-for-time strategy, which accelerates the execution logic by pre-computing or building lookup tables;

[0090] Encourage innovation and heterogeneous algorithms: Guide models to break out of conventional problem-solving approaches and explore and use novel or non-traditional heterogeneous algorithms for solving problems;

[0091] Minimalist style constraint: Constrain the model to streamline redundant structures and achieve the expected functionality with the fewest and most concise lines of code;

[0092] Code implementation-level optimization: This requires the model to be tuned for execution efficiency in specific code writing details (such as data structure selection, built-in function calls, and underlying syntax features).

[0093] Through this multi-strategy guidance, large-scale code generation is carried out, generating a total of 140 extended code for each problem, thereby building a rich code pool containing diverse implementation paths and multiple efficiency levels, namely the candidate code library.

[0094] Next, we will proceed as follows: Figure 1 The second stage shown: mining of difficult negative samples based on multidimensional weighted similarity.

[0095] Step 302, Sandbox Execution Evaluation and Cleaning: Using the "effective test cases" selected in Step S2, the above 140 extended code snippets are rigorously verified for correctness and performance, resulting in a set of correct code and a set of incorrect code. In a standardized isolated sandbox environment, a performance monitoring program is started for the verified correct code to accurately record the CPU execution time of each code snippet during operation. This quantified performance data will serve as the key basis for selecting the "optimal positive samples" in subsequent steps, thereby ensuring that the model training signal points to a more efficient code implementation.

[0096] Step S4: Constructing hybrid similarity negative sampling and training data.

[0097] Specifically, the code with the shortest execution time (i.e., the fastest correct code) is selected from the verified code as a positive sample; the mixed similarity between the erroneous code and the positive sample is calculated based on the code structure features, and the erroneous code whose mixed similarity meets the preset threshold is selected as a negative sample.

[0098] This step aims to construct high-quality preference-aligned training data from the expanded candidate set in step S3, based on the principles of "efficiency first, structure awareness".

[0099] Step 401: Perform the screening of positive samples (yw).

[0100] From all the correct code that passed the verification in step S3, the code with the shortest CPU execution time is directly selected as the positive sample based on the recorded CPU execution time data; if there are multiple codes with the same CPU execution time and all of them are the shortest, the code with the shorter text length is preferred as the positive sample implementation to ensure that the positive sample represents the optimal time efficiency solution under the current problem.

[0101] Step 402: Mining candidate negative samples (i.e., error codes that failed the test cases in step S3).

[0102] To enable the model to learn subtle differences between code structure and logic, this embodiment proposes a multi-dimensional similarity evaluation algorithm: calculating the mixed structural similarity between each erroneous code and a selected positive sample yw. The hybrid similarity score is a weighted score calculated from control flow similarity, abstract syntax tree node similarity (i.e., AST node similarity), naming similarity, and semantic similarity. The calculation formula is as follows:

[0103] ;

[0104] The specific calculation method for the similarity of each component is as follows:

[0105] (1) This represents the node structural similarity calculated based on the Abstract Syntax Tree (AST). It constructs feature vectors by traversing the code and counting the frequency of occurrence of nodes in various ASTs, and then calculates the cosine similarity between two feature vectors.

[0106] ;

[0107] in, and These represent the frequency vectors of AST node types for positive samples and error codes, respectively. This embodiment assigns them the highest weight of 0.4 to emphasize the consistency of the syntactic skeleton.

[0108] (2) This indicates naming similarity. It extracts all identifiers from the code (including class names, function names, parameter names, and variable names) to form a set, and calculates the Jaccard Similarity of this identifier set combined with a fuzzy matching score based on the Normalized Levenshtein Distance.

[0109] ;

[0110] in, and A set of identifiers representing positive and negative samples; This represents the average of the optimal normalized Levenstein similarity between two sets.

[0111] (3) This represents control flow similarity. It extracts control flow keywords (such as If, For, While, Return, etc.) in the order of code syntax parsing to form a keyword sequence, and calculates it based on the Longest Common Subsequence (LCS) algorithm.

[0112] ;

[0113] in, and These represent the control flow keyword sequences for positive samples and error codes, respectively. This represents the length of the longest common subsequence of the two sequences. and This represents the length of the corresponding sequence.

[0114] (4) This represents semantic similarity. It extracts and merges the function call sets, module import sets, and constant literal sets (including strings, numbers, booleans, etc.) involved in the code to form a global semantic feature set, which is then calculated using the Jaccard similarity coefficient.

[0115] ;

[0116] in, and These represent the combined semantic feature sets of positive samples and error codes, respectively (i.e., ).

[0117] , , and This represents the weighting coefficient. In this embodiment, to balance structural consistency and semantic deceptiveness, the weighting coefficients are set as follows: , , , .

[0118] Based on the calculation results, a mixed similarity is automatically selected. Error codes with values ​​closest to the threshold of 0.3 are selected as negative samples yl. Under this configuration, the selected negative samples are "difficult negative samples" that are structurally similar to the positive samples but logically incorrect.

[0119] Experimental verification confirms a key "cognitive trap" in the selection of similarity for negative samples. If the similarity threshold is set too high (e.g., 0.6), negative samples are often merely "corrected versions" of positive samples (e.g., simply miswriting < as <=). The model can easily distinguish them by focusing on local symbols, failing to learn robust global logic. Conversely, when the similarity threshold is set to 0.3, the selected negative samples often represent code with "completely different problem-solving approaches but extremely similar appearances" (e.g., using bubble sort to disguise quicksort). These samples force the model to understand the algorithmic skeleton (AST) and execution flow (CFG) of the code to identify errors. Therefore, a threshold of 0.3 most effectively balances the difficulty of distinguishing samples with the depth of logic, preventing the model from taking shortcuts during training and ensuring that the model truly learns efficient and correct programming patterns.

[0120] Finally, by combining the question, positive sample yw, and negative sample yl, 2147 pairs of training data were constructed, resulting in DPO training data pairs.

[0121] Step S5: Model training and experimental validation, i.e. Figure 1 The third stage (DPO training) is shown.

[0122] Specifically, preference-aligned training data is constructed using questions, positive samples, and negative samples to fine-tune the target code generation model.

[0123] Using the high-quality triplet data (DPO training data pairs) constructed in step S4, the pre-trained code generation pedestal model is fine-tuned using the Direct Preference Optimization (DPO) algorithm.

[0124] The model parameters are updated by gradient based on training triples. The objective function of the training aims to maximize the generation probability of positive samples (i.e., the fastest code) (in short, positive sample probability) while minimizing the generation probability of negative samples (i.e., code with similar structure but logical errors) (in short, negative sample probability). This guides the code generation base model to internalize the programming preference of "fast and correct", forces the model to pay attention to subtle logical pitfalls (such as loop boundaries and misuse of data structures), and improves model performance.

[0125] To verify the effectiveness of the method in this embodiment, extensive comparative experiments were conducted on multiple base models such as StarCoder-1b (1 billion parameters), DeepSeek-1.3b (1.3 billion parameters), Qwen (3 billion parameters), DeepSeek-6.7b (6.7 billion parameters), and Qwen (7 billion parameters), as well as mainstream benchmark sets such as MBPP (Basic Programming Problem Set), HumanEval (Human Evaluation Benchmark Set), Classeval (Class-level Code Evaluation Dataset), and RepoExec (Repository-level Code Execution Evaluation Dataset).

[0126] Experimental results show that, compared with the existing CodeDPO method, which is based on direct code preference optimization based on self-generated and verified source code alignment, and the Code-Optimise method, which is based on self-generated preference data optimization for correctness and execution efficiency, the model trained using the method in this embodiment has achieved significant advantages in two key indicators: Pass@1 (first-pass rate) and average execution time (Avg Runtime). This proves that the method in this embodiment can effectively improve the time efficiency of code generation without sacrificing accuracy.

[0127] To verify the impact of the selection of the hybrid similarity threshold on model performance, this embodiment uses DeepSeek-1.3b (1.3 billion parameters) as the base model and conducts ablation experiments on four mainstream datasets: HumanEval (human evaluation benchmark), HumanEval-Plus (human evaluation benchmark with enhanced test cases), MBPP (basic programming problem set), and MBPP-Plus (basic programming problem set with enhanced test cases).

[0128] Experimental results show that the similarity selection of negative samples has a decisive impact on model performance:

[0129] (1) When the similarity was set to 0.6, the model showed a significant performance decline, especially on the HumanEval and HumanEval-Plus datasets. The Pass@1 (pass rate) of the model dropped by 9.76 and 10.37 percentage points respectively compared with the base model. At the same time, on the HumanEval-Plus dataset, the code execution efficiency even showed a negative growth of 13.31%. This strongly proves that excessively similar negative samples will cause the model to fall into structural overfitting, making it confused with the subtle differences in the code and logical errors, thus destroying the existing programming ability.

[0130] (2) When the similarity is 0, although the model performance has improved compared to the base, the negative samples are too simple and the model is unable to learn deep code optimization strategies. Its performance improvement on complex datasets such as MBPP-Plus (3.01%) is much lower than that of the method in this embodiment.

[0131] (3) When the similarity is set to 0.3, the model achieves a significant improvement in both accuracy and efficiency on all test sets. On the HumanEval dataset, its Pass@1 is 6.71 percentage points higher than the base model. More importantly, on the MBPP dataset, the code execution efficiency is significantly improved by 23.15%.

[0132] In summary, the experimental data confirms that a similarity threshold of 0.3 can construct the most valuable 'hard negative samples' for training, effectively guiding the model to significantly improve code generation accuracy while significantly reducing code execution time.

[0133] The code generation method based on data augmentation and hybrid similarity negative sampling provided in this embodiment breaks through the limitation of traditional training that only focuses on functional correctness. By introducing the "fastest execution time" criterion in the selection of positive samples and adding efficiency optimization hints during the training data generation stage, the model can automatically generate code with higher execution efficiency and faster execution speed while ensuring the correctness of the code logic. Experiments show that this method outperforms existing CodeDPO and Code-Optimise methods in both accuracy and efficiency metrics in multiple benchmark tests such as MBPP and HumanEval.

[0134] The code generation method based on data augmentation and hybrid similarity negative sampling provided in this embodiment greatly enriches the diversity of training data and enhances the model's ability to cope with complex and ever-changing programming needs by varying the questions from five dimensions, including application background, engineering quality, and depth of thinking.

[0135] The code generation method based on data augmentation and hybrid similarity negative sampling provided in this embodiment addresses the noise problem commonly found in synthetic data by innovatively introducing a bipartite graph collaborative ranking algorithm to iteratively score candidate codes (e.g., T=10, d=0.85). This consensus-based method can accurately identify the "relatively correct" standard answer from massive model-generated results, thereby automatically filtering out incorrect test cases and significantly improving the purity and credibility of training data.

[0136] This embodiment presents a code generation method based on data augmentation and hybrid similarity negative sampling, proposing a negative sample mining strategy based on hybrid similarity (control flow, AST, naming, and semantics). This embodiment finds that traditional negative sampling strategies often pursue high similarity (e.g., >0.6). These negative samples typically only exhibit minor variations of positive samples (e.g., simple spelling or symbol errors), causing the model to easily distinguish between positive and negative samples by memorizing surface-level textual differences (token-level nuances), thus falling into "structural overfitting." In contrast, this embodiment sets the similarity threshold to 0.3, constructing a "structurally difficult negative sample." These samples are highly deceptive in naming and semantics, but differ fundamentally from positive samples in control flow and AST structure (e.g., using an incorrect algorithmic framework). This strategy forces the model to go beyond surface-level text comparison and delve into the deep logical structure of the code (e.g., loop boundaries, recursion depth) to make correct judgments, effectively avoiding the problem of sacrificing accuracy for high efficiency.

[0137] Example 2

[0138] The code generation system based on data augmentation and hybrid similarity negative sampling provided in this embodiment, such as Figure 1 As shown, it includes a data augmentation module, a truth mining module, an efficiency evaluation module, and a model training module.

[0139] The data augmentation module is used to expand basic programming problems into multiple dimensions: it loads a set of basic programming problems and calls the large language model interface to expand the set into multiple dimensions, generating a set of variant problems. Specifically, the data augmentation module is configured to rewrite programming problems from five dimensions: application background, engineering quality, functional breadth, depth of thinking, and technical implementation.

[0140] The truth mining module is used to filter standard answers and valid test cases from candidate codes in a set of variant questions generated by multiple models using a bipartite graph-based co-ranking algorithm. Specifically, the truth mining module is configured as follows: it constructs a code-test case bipartite graph containing candidate code nodes and test case nodes, and iteratively calculates the confidence score of candidate codes and the quality score of test cases using the bipartite graph co-ranking algorithm; after iteration, it selects the candidate code with the highest confidence score as the standard answer, and directly selects all test cases that the standard answer code can pass as valid test cases.

[0141] The efficiency evaluation module is used to verify the correctness and quantify the performance of the extended candidate code using effective test cases, and to identify positive samples. Specifically, it runs the verified correct code in an isolated sandbox environment, using a time performance monitoring unit to capture the CPU execution time during code execution; all correct code for the same problem is sorted according to CPU execution time, and the code with the shortest execution time is marked as a positive sample. In other words, it obtains the execution time of the code and selects the correct and fastest code as the positive sample.

[0142] This module is also used for weighted similarity calculation based on control flow, AST nodes, naming, and semantics, to mine erroneous codes with moderate similarity to positive samples as negative samples, and to construct triplet data for preference alignment training. The specific configuration is as follows:

[0143] The code's structural features are extracted using the built-in Abstract Syntax Tree (AST) parser and control flow analyzer. Then, the mixed similarity between erroneous code and positive samples is calculated based on preset weights. The mixed similarity is calculated by weighting control flow, AST, naming, and semantic features. The formula for calculating the mixed similarity is as follows:

[0144] ;

[0145] in, To control flow similarity, For AST node similarity, For naming similarity, For semantic similarity;

[0146] It is also equipped with adaptive negative sampling logic, which selects the error code with the closest mixed similarity to 0.3 as the negative sample.

[0147] The model training module is used to optimize and train the model based on the constructed positive and negative sample pairs: it receives triple data consisting of a question, a positive sample, and a negative sample, and loads a pre-trained code generation base model; it fine-tunes the base model using the preference alignment loss function to maximize the probability of generating positive samples and minimize the probability of generating negative samples, and outputs the target code generation model.

[0148] It should be noted that each module in this embodiment corresponds one-to-one with each step in Embodiment 1, and their specific implementation processes are the same, so they will not be repeated here.

[0149] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A code generation method based on data augmentation and hybrid similarity negative sampling, characterized in that, include: Obtain a set of code programming problems, and use a large language model to rewrite and mutate the set of code programming problems from multiple dimensions to generate a set of variant problems; Based on the variant problem set, multiple sets of candidate codes and test cases are generated through multiple different large language models as nodes. If a candidate code passes a test case, a connection edge is established between the two to construct a code-test case bipartite graph. The credibility score of the candidate code and the quality score of the test case are calculated iteratively. The candidate code with the highest credibility score is selected as the standard answer code, and the test cases that pass the standard answer code are determined as valid test cases. For the original problem set and variant problem set, multi-dimensional efficiency optimization suggestions are provided to generate multiple extended codes. The correctness of the multiple extended codes is verified by using effective test cases to obtain correct codes and error codes. Among the correct codes, the code with the shortest execution time is selected as a positive sample. The mixture similarity between the error code and the positive sample is calculated. The error code whose mixture similarity meets the preset threshold is selected as a negative sample. Preference-aligned training data is constructed using a set of code programming problems, positive samples, and negative samples. After fine-tuning the target code generation model, code generation is performed using the target code generation model.

2. The code generation method based on data augmentation and hybrid similarity negative sampling as described in claim 1, characterized in that, The multiple dimensions include application background, engineering quality, functional breadth, depth of thinking, and technical implementation.

3. The code generation method based on data augmentation and hybrid similarity negative sampling as described in claim 1, characterized in that, In the t-th iteration, the candidate code c i Credibility score t (c i ) and test cases t j Quality Score t (t j They are respectively: ; ; In the formula, t represents the current iteration round; and represents the confidence score of the candidate code and the quality score of the test case in the (t-1)th iteration, respectively; d represents the damping update factor; Represents test case t j With candidate code c i The relationship between them is identified by the candidate code c. i The test case t was successfully run and passed. j ,but =1, otherwise, =0; and They have the same meaning.

4. The code generation method based on data augmentation and hybrid similarity negative sampling as described in claim 1, characterized in that, The mixed similarity between the error code and the positive sample is represented as follows: ; In the formula, This represents the control flow similarity calculated based on the basic blocks and transition relationships of the control flow graph. This represents the similarity of node structures calculated based on an abstract syntax tree; Indicates the sequence similarity between variable names and function names; The expression represents semantic similarity extracted based on the code vectorization model; w1, w2, w3, and w4 represent weight coefficients.

5. The code generation method based on data augmentation and hybrid similarity negative sampling as described in claim 1, characterized in that, The preset threshold ensures that the selected negative samples are structurally similar to the positive samples but logically incorrect.

6. A code generation system based on data augmentation and hybrid similarity negative sampling, characterized in that, include: The data augmentation module obtains a set of code programming problems, and uses a large language model to rewrite and mutate the set of code programming problems from multiple dimensions to generate a set of variant problems. The truth mining module, based on a variant question set, generates multiple sets of candidate codes and test cases through multiple different large language models. These serve as nodes. If a candidate code passes a test case, a connection edge is established between the two, constructing a code-test case bipartite graph. The module iteratively calculates the credibility score of the candidate code and the quality score of the test cases, selects the candidate code with the highest credibility score as the standard answer code, and determines the test cases that the standard answer code passes as valid test cases. The efficiency evaluation module provides multi-dimensional efficiency optimization suggestions for the original problem set and variant problem set to generate multiple extended codes. It then uses effective test cases to verify the correctness of the multiple extended codes, obtaining correct codes and incorrect codes. Among the correct codes, the code with the shortest execution time is selected as a positive sample. The mixed similarity between the incorrect code and the positive sample is calculated, and the incorrect code whose mixed similarity meets the preset threshold is selected as a negative sample. The model training module uses a set of code programming problems, positive samples, and negative samples to construct preference-aligned training data. After fine-tuning the target code generation model, it generates code using the target code generation model.

7. The code generation system based on data augmentation and hybrid similarity negative sampling as described in claim 6, characterized in that, The multiple dimensions include application background, engineering quality, functional breadth, depth of thinking, and technical implementation.

8. The code generation system based on data augmentation and hybrid similarity negative sampling as described in claim 6, characterized in that, In the t-th iteration, the candidate code c i Credibility score t (c i ) and test cases t j Quality Score t (t j They are respectively: ; ; In the formula, t represents the current iteration round; and represents the confidence score of the candidate code and the quality score of the test case in the (t-1)th iteration, respectively; d represents the damping update factor; Represents test case t j With candidate code c i The relationship between them is identified by the candidate code c. i The test case t was successfully run and passed. j ,but =1, otherwise, =0; and They have the same meaning.

9. The code generation system based on data augmentation and hybrid similarity negative sampling as described in claim 6, characterized in that, The mixed similarity between the error code and the positive sample is represented as follows: ; In the formula, This represents the control flow similarity calculated based on the basic blocks and transition relationships of the control flow graph. This represents the similarity of node structures calculated based on an abstract syntax tree; Indicates the sequence similarity between variable names and function names; The expression represents semantic similarity extracted based on the code vectorization model; w1, w2, w3, and w4 represent weight coefficients.

10. The code generation system based on data augmentation and hybrid similarity negative sampling as described in claim 6, characterized in that, The preset threshold ensures that the selected negative samples are structurally similar to the positive samples but logically incorrect.