An instant defect localization method based on code change representation and query return

By using a joint training and query return mechanism, a vector representation of code changes is generated, which solves the problem of lack of contextual understanding and interactive information in existing methods and achieves a more efficient defect localization effect.

CN119105764BActive Publication Date: 2025-10-31HANGZHOU DIANZI UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411023524.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-29
Publication Date
2025-10-31
Estimated Expiration
2044-07-29

AI Technical Summary

Technical Problem

Existing real-time defect localization methods lack contextual understanding and code change interaction information, resulting in insufficient capture of semantic information of code fragments and poor localization performance.

Method used

We employ code change representation learning and joint training with a classifier on the target task, combined with a query return mechanism, to extract features from the code before and after the change through a multi-layer attention mechanism, generate a vector representation of the code change, and optimize the model through a cross-entropy loss function to achieve efficient localization.

Benefits of technology

It improved the accuracy and reliability of defect localization, enhanced Top-K Accuracy and MAP evaluation metrics, and significantly improved the effectiveness of code defect localization.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119105764B_ABST
    Figure CN119105764B_ABST
Patent Text Reader

Abstract

This invention discloses an instant defect localization method based on code change representation and query return. The method first aligns a given code change to obtain change information. Then, it extracts sequence features before and after the change to generate a feature representation of the code change, which is combined with the feature sequence of the code before the change and input into the localization model for training. The code change representation learning is jointly trained with task-specific components such as classifiers on the target task, eliminating reliance on label supervision. A "query return" mechanism is designed to highlight the changed code, allowing the changed code fragment to interact with the entire code change, adaptively selecting important information from the code change to improve the accuracy and reliability of defect localization.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer technology and relates to software testing and machine learning methods, specifically to an instant defect localization method based on code change representation and query return. Background Technology

[0002] In today's era of rapid development in information and intelligent technologies, software applications are changing every aspect of people's lives. However, during the software development and maintenance process, users may constantly raise new business requirements, and these requirements often have unique characteristics across different fields. For large-scale and complex software, defects and faults are inevitable during development. Software defect localization technology aims to help developers locate potential erroneous code elements (such as statements or methods) by analyzing various dynamic execution information (such as failed / passed tests and error reports), thereby improving the efficiency of defect localization and reducing the cost of software maintenance and debugging.

[0003] Traditional defect localization methods only work after a defect is exposed and ignore the importance of change information generated by developers during the commit process. Just-in-Time (JIT) defect prediction can help developers check whether a given commit is a bug submission by using change information. Although various techniques have been proposed by those skilled in the art to predict bug submissions in real time, they do not have the ability to pinpoint suspicious locations. To address the aforementioned challenges in defect localization, the task of Just-in-Time (JIT) defect localization has emerged, aiming to locate defective code elements before defect symptoms have any negative impact.

[0004] Existing real-time defect localization methods are based on the fundamental idea of ​​"software naturalness." They use traditional language models or neural language models to learn the naturalness of lines of code in committed changes, thereby helping developers effectively and accurately locate erroneous lines of code during check-in (the inspection phase after developers change the source code but before running the program). However, these methods lack understanding of the context and explicit interaction information between the changed code and the overall code change, resulting in the inefficient capture of semantic information from code snippets and poor localization performance. Summary of the Invention

[0005] To address the shortcomings of existing technologies, this invention proposes an instant defect localization method based on code change representation and query return. This method jointly trains code change representation learning with task-specific components such as classifiers on the target task, eliminating reliance on label supervision. Furthermore, a "query return" mechanism is designed to highlight modified code, allowing the modified code snippets to interact with the entire code change process. This adaptively selects important information from the code changes, improving the accuracy and reliability of defect localization.

[0006] An instant defect localization method based on code change representation and query return includes the following steps:

[0007] Step 1: For the given code changes Preprocessing yields the before and after results. The sequence is processed, and the alignment information between the two sequences is stored. The specific operations are as follows:

[0008] Step 1-1: Use the text difference tool Change the given code Convert to code Code It contains one or more blocks, each block including lines removed from the code before the change, lines added to the code after the change, and the lines that were changed around them, i.e., context information.

[0009] Steps 1-2: Use a pre-trained code model The provided tokenizer will code Each block is processed as The sequence will contain the code before and after the change, respectively. The sequences are concatenated in order to obtain the following results. and , , These represent the code before and after the change, respectively. The length of the sequence.

[0010] Steps 1-3, through one by one Ground alignment and To identify changes , obtain Alignment information. , Each of them You will receive the token change flag. , ,if If a change occurs, the token change flag is set to 1; otherwise, it is set to 0.

[0011] use Align the original and modified code line by line within the code block, resulting in multiple aligned line pairs. Newly added / deleted lines are treated as aligned with blank lines. Each line in the original and modified code will have a line index marker, denoted as follows: and .

[0012] Step 2: Using the data processed in Step 1, compare the code before and after the change. sequence and As input, and using a pre-trained code model They are encoded independently as two context embedding sequences. and .

[0013] Step 3: Combine the alignment information of the modified code snippet obtained in Step 1 with the context embedding obtained in Step 2. and As input, through A query return mechanism at the level to generate code changes vector representation The specific steps are as follows:

[0014] Step 3-1, for the changed Choice

[0015] In step 1, and Each of them Assigned token change flag and Based on these markers, firstly for , Constructing a flag sequence , Then, based on the flag sequence and from and Select the context embeddings of the changed code tokens and concatenate them into a new embedding sequence. .

[0016] Step 3-2, Query Construction

[0017] Use multiple layers From embedded sequence Extract change-aware features Then, apply average pooling to Compressed to query vector middle, They represent and Code changes in quantity.

[0018] Step 3-3, Query Return

[0019] A multi-head attention mechanism is used to implement the retrieval process, using the query vector obtained in step 3-2. Relevant information is retrieved from the code before and after the change. The context embedding sequence is used as the key vector and value vector to obtain the attention feature vectors retrieved from the code before and after the change, respectively. and .

[0020] Steps 3-4: Generate code changes vector representation

[0021] The attention feature vector obtained in step 3-3 and By adding elements one by one, we can obtain the final code change representation. .

[0022] Step 4: Jointly train the code change representation and the localization model.

[0023] Before the change splicing sequence The code change sequence features obtained from the input localization model encoder are compared with the code change representation obtained in step 3. Combined, as Decoder input , The decoder combines all the previously generated data... target state , will vector Convert to the target sequence, k represents The index is used. The generated target sequence is compared with the actual target sequence for training, and the cross-entropy loss function is used to calculate the loss of the localization model, with minimizing the cross-entropy loss as the training objective.

[0024] Step 5: Use the target sequence to locate the defect.

[0025] Input the submitted code changes into the trained localization model to generate a target sequence. Identify the changed lines from the code changes based on their line index markers, calculate the entropy between the changed lines and their corresponding lines in the generated target sequence, sort all the entropy values ​​of the changed lines, and use the entropy value as the basis for the suspicion score. The code line with the highest entropy value is considered the most likely location of the defect in the code.

[0026] Compared with the prior art, the present invention has the following beneficial effects:

[0027] 1. A code change representation learning method is added to the traditional Seq2Seq model. The model learns the structural and semantic information of the code and represents it as a vector to represent the features of the code. This vector is used as input in subsequent tasks to improve the model's performance on code-related tasks.

[0028] 2. Employ pre-trained code models to learn representations of the code before and after changes. Pre-trained code models can build robust representations of code changes, improving the model's ability to select contextual information and its applicability to various downstream tasks.

[0029] 3. A new "query return" mechanism is used to highlight changed code, allowing users to adaptively select important information by interacting with the changed code snippets and the overall code changes. Attached Figure Description

[0030] Figure 1 This is an overall framework diagram of an instant defect localization method based on code change representation and query return;

[0031] Figure 2 A framework diagram for representing code changes;

[0032] Figure 3 This is a framework diagram of the query return method. Detailed Implementation

[0033] The present invention will be further explained below with reference to the accompanying drawings;

[0034] like Figure 1 As shown, an instant defect localization method based on code change representation and query return is presented. It takes a sequence of code line blocks as input and outputs a sequence of code lines, thus refining the localization problem as a sequence-to-sequence learning problem. Specifically, it includes the following steps:

[0035] Step 1, as follows Figure 2 As shown, for a given code change The pre-processing stage yields the before and after results of the change. sequence and Identify and align the modified code snippets in the two sequences using... Change sign , storage Change information, if If a change occurs, then The change flag is set to 1 if the value is 1, otherwise 0. Use row index flags. and Store row pair information; newly added / deleted rows are treated as aligned with empty rows.

[0036] Step 2: Using the data processed in Step 1, compare the code before and after the change. sequence and As input, use the pre-trained code model Through multiple layers Make the code Aggregate contextual information from each other, and compare the code before and after the change. sequence and Independently encoded as two context embedding sequences and It captures the syntax and semantic information of the code before and after the changes.

[0037] Step 3, as follows Figure 3 As shown, the alignment information of the modified code snippet obtained in step 1 and the context embedding obtained in step 2 are combined. and As input, through A query return mechanism at the level of [level] is used to generate vector representations of code changes. The specific steps are as follows:

[0038] Step 3-1, for the changed Choice

[0039] Based on token change flag and ,for , Constructing a flag sequence , Using the flag sequence and Embedded sequences from context and The context embeddings of the modified code tokens are selected and then concatenated into a new embedding sequence. :

[0040]

[0041]

[0042]

[0043] in, and They represent the sequences respectively. and The number of changed tags, This represents a mask selection operation, which selects elements from the left operand based on a flag provided by the right operand. This step locates fine-grained label variations, helping the model focus on annotation operations during feature extraction.

[0044] Step 3-2, Query Construction

[0045] Use multiple layers from Extract change-aware features Then, average pooling is applied. To compress it into a query middle:

[0046]

[0047]

[0048] in, Expected to The changing information is encoded.

[0049] Step 3-3, Query Return

[0050] A multi-head attention mechanism is used to implement the retrieval process, using the query vector obtained in step 3-2. Relevant information is retrieved from the code before and after the change. The context embedding sequence is used as the key vector and value vector to obtain the attention feature vectors retrieved from the code before and after the change, respectively. and :

[0051]

[0052]

[0053] Steps 3-4: Generate code changes vector representation

[0054] Merge the elements using step 3-3, adding them one by one. and To obtain the final code change representation :

[0055]

[0056] Step 4: Jointly train the code change representation and the localization model.

[0057] Before the change splicing sequence The code change sequence features obtained from the input localization model encoder are compared with the code change representation obtained in step 3. Combined, as Decoder input , The decoder will convert the vector Transform it into the target sequence. Mathematically speaking, given the input... , Decoder calculates target sequence The conditional probability distribution, i.e. :

[0058]

[0059] in, It is the target sequence of length. The decoder first encodes the hidden state. and all previous outputs Mapped to vector Then through Operation processing vector To estimate the conditional distribution Using the aforementioned conditional distribution, an autoregressive output sequence can be generated, thereby achieving the input... To output sequence The mapping.

[0060] Step 5: Use the target sequence to locate the defect.

[0061] Input the submitted code changes into the trained localization model to generate the target sequence. Calculate the entropy between the generated target sequence and the changed lines, and the entropy of the changed code line can be calculated as the entropy of each word within that code line. The average entropy:

[0062]

[0063] in, Is a given code change of total. It is each of the lines of code The entropy value is then calculated. Finally, the entropy values ​​of all the changed lines are sorted, and the entropy value is used as a suspicion score. The line of code with the highest entropy value is considered the most likely location of the defect in the code.

[0064] To demonstrate the effectiveness of this method, a comparative experiment was conducted with commonly used methods in existing related technologies. The evaluation metrics used were Top-K Accuracy and MAP, where Top-K measures whether the k most likely error lines returned correspond to the actual error code location. If at least one of the k most likely error lines returned is the actual error location, the location is considered successful, and the Top-K value is set to 1; otherwise, the location is considered unsuccessful, and the Top-K value is set to 0. The experimental results are shown in Table 1.

[0065] Table 1

[0066]

[0067] Compared with existing technologies, this method shows a significant improvement in Top-K Accuracy, indicating a substantial increase in localization accuracy and demonstrating the effectiveness of code changes in localization tasks. MAP is a metric used to evaluate the performance of ranking tasks. MAP measures the average rank of correct answers in a ranking list; in this invention, MAP considers the rank of all incorrect rows in the ranking list. Therefore, a higher MAP score, indicating a higher rank of the incorrect row, signifies better localization performance. As shown in Table 1, this method also demonstrates a significant improvement in MAP score, indicating higher accuracy in defect localization tasks.

Claims

1. A real-time defect localization method based on code change representation and query return, characterized in that: Specifically, the following steps are included: Step 1: For a given code change Z, obtain the token sequences before and after the change through preprocessing, and store the alignment information of the two sequences; Step 2: Combine the token sequences Z of the code before and after the change. b and Z a As input, they are independently encoded into two context embedding sequences using a pre-trained code model. and Step 3: Based on the alignment information of the modified code snippet obtained in Step 1, extract the context embedding H from Step 2. b and H a The contextual embeddings that have changed are selected, concatenated into an embedding sequence H′, and input into a multi-layer Transformer structure to extract change-aware features. Then, apply average pooling to H t Compressed to query vector q t In, n b n a Z b and Z a The number of code tokens that have changed; a multi-head attention mechanism is used, with the context embedding sequence as the key vector and value vector, to obtain the attention feature vectors retrieved from the code before and after the change, respectively. and Will and By adding each element, we obtain the final code change representation v. t ; Step 4: Jointly train the code change representation and the localization model. Concatenate the token sequence Z before the change. b The code change sequence features obtained from the input localization model encoder are compared with the code change representation v obtained in step 3. t Combined, it serves as the input x to the Transformer decoder. h Generate target sequences; compare the generated target sequences with the actual target sequences for training, and use the cross-entropy loss function to calculate the loss of the localization model, with minimizing the cross-entropy loss as the training objective; Step 5: Use the target sequence to locate the defect. Input the submitted code changes into the trained localization model to generate the target sequence; Identify the modified lines from the code changes, calculate their entropy relative to the corresponding lines in the generated target sequence, sort all the entropy values ​​of the modified lines, and use the entropy value as the basis for the suspicion score. The code line with the highest entropy value is considered to be the most likely location of the defect in the code.

2. The instant defect localization method based on code change representation and query return as described in claim 1, characterized in that: The specific method for generating sequence alignment information is as follows: Step 1-1: Use the text diff tool Python difflib to convert the given code change Z into a code diff; the code diff contains one or more blocks, each block including lines removed from the code before the change, lines added to the code after the change, and the lines that changed around them, i.e., context information; Steps 1-2: Process each block of the code diff into a token sequence, and set the tokens before and after the change as follows: and |Z b |、|Z a | These represent the lengths of the code token sequences before and after the change, respectively; Steps 1-3: Align Z token by token b and Z a To identify the changed token and obtain the token alignment information; Z b Z a Each token in the token will receive a token change flag. If the token changes, the token change flag is set to 1; otherwise, it is set to 0. Use Python difflib to align the code before and after the change line by line in a code block and obtain multiple aligned line pairs; Newly added / deleted lines are treated as aligned with blank lines; each line in the code before and after the change will have a line index marker, denoted as follows: and 3. The instant defect localization method based on code change representation and query return as described in claim 1 or 2, characterized in that: The pre-trained code model Code BERT is used to generate token sequences and context embedding sequences.

4. The instant defect localization method based on code change representation and query return as described in claim 1 or 2, characterized in that: For the token sequence Z b and Z a Each token in the token is assigned a token change flag. and Based on these markers, construct a marker sequence. Then, according to the flag sequence M b and M a From H b and H a Select the context embeddings of the changed code tokens and concatenate them into a new embedding sequence H′: in, This indicates a mask selection operation, which selects elements from the left operand based on the flags provided by the right operand.

5. The instant defect localization method based on code change representation and query return as described in claim 1, characterized in that: The attention feature vector They are respectively:

6. The instant defect localization method based on code change representation and query return as described in claim 1, characterized in that: The entropy value H of the changed line of code p (n) is: Where n is the given code change t z Total number of tokens; H P (t z ) is the entropy value of each token in this line of code.

Citation Information

Patent Citations

  • Multi-target code submission defect tendency prediction method fusing multi-dimensional features

    CN117608998A

  • Predicting defects in code

    US20110041120A1