Instant defect positioning method based on code change representation and query return

Through the method based on code change representation and query return, combined with the multi-head attention mechanism, the problem of lack of context understanding and code change interaction information in the existing technology is solved, and more efficient defect positioning is achieved, which significantly improves positioning accuracy and reliability. sex.

CN119105764AActive Publication Date: 2024-12-10HANGZHOU DIANZI UNIV
View PDF 8 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

In the absence of context understanding and code changes interactive information, existing instant defect positioning methods lead to ineffective capture of code snippet semantic information, poor positioning effect, and inability to effectively predict and locate potential error code elements.

Method used

Using the method based on code change representation and query return, the code change information is preprocessed, the code change vector representation is generated using the pre-trained code model, and interact with the multi-head attention mechanism to highlight the changed code, thereby improving defect positioning accuracy and reliability.

Benefits of technology

The accuracy and reliability of defect positioning are significantly improved. Through Top-K Accuracy and MAP evaluation indicators, the effectiveness of code change representation in positioning tasks is proved, and the performance of code-related tasks is improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119105764A_ABST
    Figure CN119105764A_ABST
Patent Text Reader

Abstract

The invention discloses an instant defect positioning method based on code change representation and query return. The method comprises the following steps: firstly, aligning given code changes to obtain change information; and then extracting sequence features before and after the change to generate a feature representation of code change, combining the feature representation with the code feature sequence before the change, and inputting the feature representation into a positioning model for training. Code change representation learning and task-specific components such as a classifier on a target task are subjected to joint training, and dependence on label supervision is avoided. And a query return mechanism is designed to highlight the changed code, so that the changed code snippets interact with the whole code change, important information is adaptively selected from the code change, and the accuracy and reliability of defect positioning are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] With the rapid development of information and intelligent technology, software applications are changing every aspect of people's lives. However, in the process of software development and maintenance, users may constantly put forward new business requirements, and the business requirements in various fields often have their own characteristics. For large-scale and complex software, fault defects are inevitable during the development process. Software defect location technology aims to help developers locate potential error code elements (such as statements or methods) by analyzing various dynamic execution information (such as failed / passed tests, error reports), improve the efficiency of locating fault defects, and thus reduce the cost of software maintenance and debugging.

[0003] Traditional defect localization methods can only work after the defects are exposed, and ignore the importance of change information generated by developers during the submission process. Just-in-time (JIT) defect prediction can help developers check whether a given submission is a bug submission through change information. Although technicians in this field have proposed different techniques to predict bug submissions in real time, they do not have the ability to locate suspicious locations. In order to solve the above challenges related to fault localization, the task of just-in-time (JIT) defect localization has emerged, which aims to locate defective code elements before the defect symptoms have any negative effects.

[0004] Existing methods for real-time defect localization are based on the basic idea of ​​"software naturalness". They use traditional language models or neural language models to learn the naturalness of code lines in submitted changes, thereby helping developers to effectively and accurately locate erroneous code lines at check-in time (the inspection phase after developers change the source code and before running the program). However, due to the lack of understanding of the context and the explicit interaction information between the changed code and the entire code change, these methods cannot efficiently capture the semantic information of the code snippets, resulting in poor localization effects. Summary of the invention

[0005] In view of the shortcomings of the existing technology, this paper proposes an instant defect location method based on code change representation and query return, which jointly trains code change representation learning with task-specific components such as classifiers on the target task to get rid of the dependence on label supervision. A "query return" mechanism is designed to highlight the changed code, so that the changed code fragments interact with the entire code change, and adaptively select important information from the code change to improve the accuracy and reliability of defect location.

[0006] A real-time defect location method based on code change representation and query return includes the following steps: Step 1: For a given code change , through preprocessing to obtain the before and after changes Sequences and store the alignment information of the two sequences. The specific operations are as follows:

[0007] Step 1-1. Use a text difference tool Change the given code Convert to code . Code Contains one or more blocks, each of which includes the lines deleted from the code before the change, the lines added in the code after the change, and the lines changed around them, that is, the context information.

[0008] Step 1-2: Use the pre-trained code model The provided tokenizer converts the code Each block is processed as Sequence, respectively change the code before and after Sequences are spliced ​​in order to obtain and , , Respectively represent the code before and after the change The length of the sequence.

[0009] Steps 1-3, by one by one Ground Alignment and , to identify the changed , get alignment information. , Each of Will get the token change flag , ,if If a change occurs, the value of the token change flag is 1, otherwise it is 0.

[0010] use Align the code before and after the change line by line in the code block and get multiple aligned line pairs. Newly added / deleted lines are considered to be aligned with empty lines. Each line in the code before and after the change will have a line index marker, represented as and .

[0011] Step 2: Use the data processed in step 1 to compare the codes before and after the change. sequence and As input, and use the pre-trained code model Encode them independently into two context embedding sequences and .

[0012] Step 3: embed the alignment information of the changed code snippet obtained in step 1 and the context obtained in step 2 and As input, through A query-return mechanism to generate code changes Vector representation of The specific operations are as follows:

[0013] Step 3-1: Change Choice In step 1, and Each of Assigned token change flag and Based on these signs, first , Constructing a Flag Sequence , Then, according to the flag sequence and from and The context embeddings of the changed code tokens are picked out and further concatenated into a new embedding sequence .

[0014] Step 3-2: Query construction Use multiple layers From the embedded sequence Extracting change-aware features , then, applying average pooling will Compress to query vector middle, Respectively and Code that has changed in quantity.

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

[0016] Step 3-4: Generate code changes Vector representation of For the attention feature vector obtained in step 3-3 and Perform element-by-element addition to obtain the final code change representation .

[0017] Step 4: Jointly train the code change representation and localization model Change the Splicing sequence Input the positioning model encoder, and the obtained code change sequence features are compared with the code change representation obtained in step 3 Combined as Decoder Input , The decoder combines all previous Target state , the vector Converted to target sequence, k represents 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 positioning model, with minimizing the cross entropy loss as the training goal.

[0018] Step 5: Use target sequence to locate defects The submitted code changes are input to the trained positioning model to generate the target sequence. The changed lines are found from the code changes according to the line index tags, and the entropy between them and the corresponding lines in the generated target sequence is calculated. The entropy values ​​of all the changed lines are sorted, and the entropy value is used as the basis for the suspicious score. The code line with the highest entropy value is considered to be the location where defects are most likely to occur in the code.

[0019] Compared with the prior art, the present invention has the following beneficial effects: 1. The code change representation learning method is added to the traditional Seq2Seq model. The model learns the structure and semantic information of the code and represents it as a vector to characterize the features of the code. It is used as input in subsequent tasks to improve the performance of the model for code-related tasks.

[0020] 2. Use pre-trained code models to learn the representation of code before and after changes. Pre-trained code models can build powerful representations of code changes, improve the model's selection of contextual information and its applicability to various downstream tasks.

[0021] 3. A new "query return" mechanism is used to highlight the changed code, adaptively selecting important information by interacting with the changed code snippet and the entire code change. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] Figure 1 The overall framework diagram of an instant defect location method based on code change representation and query return; Figure 2 A framework diagram for representing code changes; Figure 3 Returns a method skeleton diagram for the query. DETAILED DESCRIPTION

[0023] The present invention will be further explained below with reference to the accompanying drawings; like Figure 1 As shown, an instant defect localization method based on code change representation and query return takes a code line block sequence as input and outputs a code line sequence, so that the localization problem is expressed as a sequence-to-sequence learning problem. Specifically, it includes the following steps:

[0024] Step 1: Figure 2 As shown, for a given code change , through the preprocessing stage to obtain the before and after changes sequence and , identify the modified code fragments in the two sequences and align them, using Change logo , storage If the change information If a change occurs, The value of the change flag is 1, otherwise it is 0. Use the row index marker and Row pair information is stored, and newly added / deleted rows are considered aligned with empty rows.

[0025] Step 2: Use the data processed in step 1 to compare the codes before and after the change. sequence and As input, use the pre-trained code model , through multiple layers Make Code Aggregate contextual information from each other to combine the code before and after the change sequence and Independently encoded into two context embedding sequences and , capturing syntactic and semantic information of the code before and after the change.

[0026] Step 3: Figure 3 As shown, the alignment information of the changed code snippet obtained in step 1 and the context obtained in step 2 are embedded and As input, through A query-return mechanism at the level to generate vector representations of code changes The specific operations are as follows:

[0027] Step 3-1: Change Choice Changing flags based on token and ,for , Constructing a Flag Sequence , , using the flag sequence and Embedding Sequences from Context and We pick out the contextual embeddings of the changed code tokens and further concatenate them into a new embedding sequence : in, and Respectively in the sequence and The number of tags changed in Represents a mask selection operation that selects elements from the left operand based on the flags provided by the right operand. This step locates fine-grained label changes and can help the model focus on operations during feature extraction.

[0028] Step 3-2: Query construction Use multiple layers from Extracting change-aware features , then, average pooling is applied to to compress it into the query middle: in, Expected to Encodes the changing information.

[0029] Step 3-3, query return A multi-head attention mechanism is used to implement the retrieval process, using the query vector obtained in step 3-2 Retrieve relevant information from the code before and after the change, use the context embedding sequence as the key vector and value vector, and obtain the attention feature vectors retrieved from the code before and after the change, respectively. and : Step 3-4: Generate code changes Vector representation of Merge by adding elements one by one in step 3-3 and , to obtain the final code change representation : Step 4: Jointly train the code change representation and localization model Change the Splicing sequence Input the positioning model encoder, and the obtained code change sequence features are compared with the code change representation obtained in step 3 Combined as Decoder Input , The decoder will convert the vector Convert to the target sequence. Mathematically speaking, given the input , The decoder computes the target sequence The conditional probability distribution of :

[0030] in, is the target sequence of length. The decoder first converts the hidden state of the encoder and all previous output Map to vector Then by Operational processing vector To estimate the conditional distribution Through the above conditional distribution, the output sequence can be generated by autoregression, thus achieving the input To the output sequence 's mapping.

[0031] Step 5: Use target sequence to locate defects The submitted code changes are fed into the trained localization model to generate the target sequence. The entropy between the generated target sequence and the changed line is calculated, and the entropy of the changed code line can be calculated as the entropy of each word in the code line. The average entropy of is:

[0032] in, Is a given code change of total. Each Finally, the entropy values ​​of all the changed lines are sorted and used as the suspicious score. The code line with the highest entropy value is considered to be the location where defects are most likely to occur in the code.

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

[0034] Table 1 Compared with the prior art, the Top-K Accuracy of this method has been greatly improved, indicating that the positioning accuracy of this method has been greatly improved, reflecting the effectiveness of code change representation for positioning tasks. MAP is an indicator used to evaluate the performance of ranking tasks. MAP measures the average ranking of correct answers in a ranking list. In the present invention, MAP considers the ranking of all error rows in the sorted list. Therefore, the higher the MAP score, that is, the higher the ranking of the error row, the better the positioning performance of the method. As can be seen from the data in Table 1, the MAP score of this method has also been greatly improved, indicating that this method has achieved higher accuracy in defect location tasks.

Claims

1. An instant defect location method based on code change representation and query return, characterized in that: The specific steps include: Step 1: For a given code change , through preprocessing to obtain the before and after changes Sequences and store the alignment information of the two sequences; Step 2: Change the code before and after the change sequence and as input and encode them independently into two context embedding sequences using the pre-trained code model and Step 3: Based on the alignment information of the changed code snippet obtained in step 1, embed the context obtained in step 2 and Select the changed context embedding information and connect it into an embedding sequence , input multiple layers Extracting change-aware features from the structure , then, applying average pooling will Compress to query vector middle, Respectively and Code that has changed in The multi-head attention mechanism is used to embed the context 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 Add each element to get the final code change representation Step 4: Jointly train the code change representation and localization model Change the Splicing sequence Input the positioning model encoder, and obtain the code change sequence features, which are compared with the code change representation obtained in step 3 Combined as Decoder Input , generate a target sequence; compare the generated target sequence with the actual target sequence for training, and use the cross entropy loss function to calculate the loss of the positioning model, with minimizing the cross entropy loss as the training goal; Step 5: Use target sequence to locate defects Input submitted code changes to the trained localization model to generate target sequences; Find the changed lines from the code changes, calculate the entropy between them and the corresponding lines in the generated target sequence, sort the entropy values ​​of all the changed lines, and use the entropy value as the basis for the suspicious score. The code line with the highest entropy value is considered to be the location where defects are most likely to occur in the code.

2. The instant defect location method based on code change representation and query return as claimed in claim 1, characterized in that: The specific method for generating sequence alignment information is: Step 1-1. Use a text difference tool Change the given code Convert to code Code Contains one or more blocks, each of which includes lines deleted from the code before the change, lines added to the code after the change, and lines changed around them, i.e., context information; Step 1-2: Write the code Each block is processed as Sequence, respectively change the code before and after Sequences are spliced ​​in order to obtain and , , Respectively represent the code before and after the change The length of the sequence; Steps 1-3, by one by one Ground Alignment and , to identify the changed , get Alignment information , Each of Will get the token change flag , ,if If a change occurs, the value of the token change flag is 1, otherwise it is 0; use Align the codes before and after the change line by line in the code block and obtain multiple aligned line pairs; Newly added / deleted lines are considered to be aligned with empty lines; each line in the code before and after the change will have a line index marker, represented as and .

3. The instant defect location method based on code change representation and query return as claimed in claim 1 or 2, characterized in that: Using pre-trained code models conduct Generation of sequence-to-sequence and contextual embeddings.

4. The instant defect location method based on code change representation and query return as claimed in claim 1 or 2, characterized in that: for sequence and Each of Assigned token change flag and Based on these flags, construct a flag sequence , Then, according to the flag sequence and from and The context embeddings of the changed code tokens are picked out and further concatenated into a new embedding sequence : in, Represents a mask selection operation which selects elements from the left operand based on the flags provided by the right operand.

5. The instant defect location method based on code change representation and query return as claimed in claim 1, characterized in that: The attention feature vector , They are: 。 6. The instant defect location method based on code change representation and query return as claimed in claim 1, characterized in that: Entropy of changed lines of code for: in, Is a given code change of total Each The entropy value of .

Citation Information

Patent Citations

  • Code method-level software defect positioning method

    CN111309607A

  • Software source code defect detection method and system, electronic equipment and storage medium

    CN113641586A

  • Code defect predicting and positioning method and device, storage medium and computer equipment

    CN116756041A

  • Instant defect prediction method and system based on data selection and submission type perception

    CN116880854A

  • Method for automatically positioning root cause of defect

    CN117234908A