A method and apparatus for fine-tuning a code model
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-20
- Publication Date
- 2026-08-11
AI Technical Summary
自动程序修复方法主要包括:基于启发式的方法、基于模板的方法、基于语义的方法以及基于深度学习的方法,这几种方法或者受限于繁琐的、基于专家经验的规则设计过程,或者受限于所能够修复的代码问题类型,或者受限于模型参数和训练数据质量,在程序修复任务上的性能表现和泛化能力仍有不足
[0024]根据第三方面,提供了一种计算机程序产品,包括计算机程序/指令,该计算机程序/指令被处理器执行时实现第一方面所述方法的步骤。
Smart Images

Figure CN119988198B_ABST
Abstract
Description
Technical Field
[0001] This specification relates to one or more embodiments in the field of large language modeling and code generation technology, and in particular to a method and apparatus for fine-tuning a code model. Background Technology
[0002] With the rapid development of the software industry, program repair (such as vulnerability fixing and code correction) is taking up more and more time in software development and maintenance, squeezing the effective development time in software engineering projects and consuming a large amount of human resources, becoming an increasingly serious problem. To cope with the increasingly heavy program repair tasks, Automatic Program Repair (APR) has emerged, aiming to reduce the time and human resources consumed by program repair tasks in software engineering. APR methods mainly include heuristic-based methods, template-based methods, semantic-based methods, and deep learning-based methods. However, these methods are either limited by cumbersome rule design processes based on expert experience, limited by the types of code problems they can repair, or limited by model parameters and training data quality, resulting in insufficient performance and generalization ability in program repair tasks.
[0003] Therefore, we hope to find a solution that can improve the efficiency and accuracy of code models in program repair tasks through technical means. Summary of the Invention
[0004] One or more embodiments of this specification describe a method and apparatus for fine-tuning a code model to improve the code model's ability to understand the target source code, thereby better completing the program repair task.
[0005] According to the first aspect, a method for fine-tuning a code model is provided, based on a code repository containing multiple source code entries and corresponding patch code. The method includes:
[0006] Obtain a first representation vector corresponding to any first source code among the multiple source codes. The first representation vector is determined by performing a first encoding on the text of the first source code and a second encoding on the first abstract syntax tree corresponding to the first source code.
[0007] Based on the first representation vector, a similarity-based retrieval is performed in the code library to obtain the target context; the target context includes several other source codes and their corresponding patch codes that have a similarity to the first source code that meets a first threshold.
[0008] Based on the first source code and the target context, generate prompt text, input the code model, and obtain the inference patch.
[0009] Based on the inferred patch and the first patch code corresponding to the first source code, the parameters of the code model are fine-tuned.
[0010] According to one implementation, the fine-tuned code model is used to infer the corresponding patch code for the source code to be checked based on the target prompt text; the target prompt text includes the source code to be checked, several reference source codes whose similarity to the source code to be checked meets a first threshold, and their corresponding patch codes.
[0011] According to one implementation, the first encoding includes: text encoding the first source code using a first text model.
[0012] According to one implementation, the second encoding includes: performing a serialization operation on the first abstract syntax tree to obtain a converted text sequence, the converted text sequence including node identifiers representing each node of the first abstract syntax tree and structure labels representing the structural relationships between nodes; and using a second text model to perform text encoding on the converted text sequence.
[0013] In one scenario of the above implementation, the structural label includes a pair of labels marked with node identifiers, which are used to indicate the child nodes of the node corresponding to the marked node identifier; the serialization operation includes:
[0014] During the target traversal of the first abstract syntax tree, for any target node currently visited, its node identifier is extracted and added to the range of the first label pair in the current sequence. The first label pair is labeled with the node identifier of the parent node of the target node. The current sequence obtained at the end of the target traversal is used as the converted text sequence.
[0015] According to one implementation, the first representation vector is a weighted average of the first vector obtained by the first encoding and the second vector obtained by the second encoding.
[0016] According to one implementation, in the prompt text, the other source codes are arranged from high to low in terms of similarity to the first source code, and the number of the other source codes is such that the prompt text does not exceed the maximum number of the upper bound of the number of input tokens.
[0017] According to one implementation, in the prompt text, a first tag is used to mark each source code, and a second tag is used to mark each patch code.
[0018] According to one implementation, the code model is a large language model, and the fine-tuning of the parameters of the code model includes: performing full parameter fine-tuning of the large language model.
[0019] According to the second aspect, an apparatus for fine-tuning a code model is provided, which operates based on a code repository containing multiple source code lines and corresponding patch code; the apparatus includes:
[0020] The encoding module is configured to obtain a first representation vector corresponding to any first source code among the plurality of source codes. The first representation vector is determined by performing a first encoding on the text of the first source code and a second encoding on the first abstract syntax tree corresponding to the first source code.
[0021] The retrieval module is configured to perform a similarity-based retrieval in the code library based on the first representation vector to obtain a target context; the target context includes several other source codes and their corresponding patch codes that have a similarity to the first source code that meets a first threshold.
[0022] The inference module is configured to generate prompt text based on the first source code and the target context, input the code model, and obtain the inference patch.
[0023] The fine-tuning module is configured to fine-tune the parameters of the code model based on the inferred patch and the first patch code corresponding to the first source code.
[0024] According to a third aspect, a computer program product is provided, including a computer program / instructions that, when executed by a processor, implement the steps of the method described in the first aspect.
[0025] According to a fourth aspect, a computing device is provided, including a memory and a processor, characterized in that the memory stores executable code, and when the processor executes the executable code, it implements the method described in the first aspect.
[0026] In summary, the methods and apparatus provided in the embodiments of this specification include a method for fine-tuning a code model. This method can retrieve relevant code fragments based on the semantic and structural representations of the source code in the code library, thereby improving the accuracy of context-based code fragment retrieval. By utilizing the retrieved context, prompt text is constructed to guide the code model inferring the patch code of the training source code. By progressively fine-tuning the parameters of the code model, it is possible for the code model to output inference results that approximate the sample labels.
[0027] This fine-tuning process can enhance the code model's understanding of code semantics and structure, improve the inference accuracy of the code model in performing program repair tasks, and optimize the code model's generalization ability so that the code model can maintain stable code repair performance when faced with diverse programming languages and code styles. Attached Figure Description
[0028] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are merely some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without any creative effort.
[0029] Figure 1 This is a schematic diagram of a method framework for fine-tuning a code model disclosed in an embodiment of this specification;
[0030] Figure 2 This is a flowchart illustrating a method for fine-tuning a code model according to an embodiment of this specification.
[0031] Figure 3A This is a schematic diagram of a serialization operation provided in an embodiment of this specification;
[0032] Figure 3B This is a schematic diagram of a serialization operation provided in an embodiment of this specification;
[0033] Figure 4 This is a schematic diagram of an apparatus for fine-tuning a code model according to an embodiment of this specification. Detailed Implementation
[0034] The solutions provided in the embodiments of this specification will now be described with reference to the accompanying drawings.
[0035] In one or more embodiments of this specification, the process of fine-tuning the code model will be illustrated using the Python programming language as an example, but this does not imply a limitation on the application scenarios or technical tools of the embodiments of the present invention. The technical concepts embodied in the various embodiments of this specification can be applied to various code models capable of performing program repair tasks and their supported programming language environments.
[0036] As mentioned earlier, Automated Program Repair (APR) is widely used to improve the development efficiency of software engineering projects. Considering the limitations of traditional APR, Large Language Models (LLMs) are used to perform APR tasks. Compared to traditional deep learning models, LLMs, thanks to training with massive amounts of code samples, have shown potential in understanding code and generating patches. Numerous related experimental results also demonstrate that LLMs can not only deeply understand source code but also automatically generate corresponding patch code.
[0037] Building upon this foundation, Retrieval-augmented Generation (RAG) technology can help LLMs retrieve and integrate relevant code fragments (context) needed during the inference process, thereby effectively improving the inference capabilities of LLMs when handling code patching tasks. Specifically, during the LLM's inference of patch code from source code, RAG technology can first retrieve context fragments related to the source code from a vast external knowledge base and pass them to the LLM; then, the LLM can use these context fragments to guide its inference of patch code, thereby improving the quality and accuracy of the LLM's inference of patch code.
[0038] However, in some related technologies, using RAG technology to assist LLM in inferring patch code still results in significant errors. The reason for this is that the context code snippets used for project hints and model fine-tuning often lack key code features, making it difficult for large language models to accurately capture the deeper meaning of source code files. The inventors' research revealed that during the retrieval of context code snippets, related technologies often neglect the analysis of the code structure and syntactic features of source code files. This leads to a low matching degree between context retrieval results and source code features, or the inclusion of a large amount of redundant information, or the lack of key context snippets. These factors all affect the fine-tuning effect of large language models and their performance in APR tasks.
[0039] To address the aforementioned technical issues, the inventors proposed a method for fine-tuning a code model. This method comprehensively considers the semantic and structural features of the code during context retrieval, searches for context fragments closely related to the source code to be repaired, assists the code model in inferring patch code for the source code to be repaired, and continuously fine-tunes the code model parameters by comparing them with sample labels of the patch code, thereby gradually improving the code model's ability to understand the source code.
[0040] Figure 1This paper discloses a framework for fine-tuning a code model in one embodiment. During the encoding of the representation vector, the source code text undergoes a first encoding to obtain an encoded representation containing semantic information. The corresponding Abstract Syntax Tree (AST) is then encoded a second time to obtain an encoded representation containing code structural information. Based on these encoded representations, the representation vector of the source code can be determined. It is easy to understand that the resulting representation vector contains not only the semantic features of the source code but also its structural features. Any source code in the codebase can be used as the source code to be repaired, and its corresponding patch code as sample labels. Based on the representation vector corresponding to the source code to be repaired, several other source codes related to the source code to be repaired are obtained through similarity retrieval. These related source codes and their corresponding patch codes together constitute the target context, which is combined with the source code to be repaired to form a prompt text. This text is input into the code model for inference, resulting in an inferred patch corresponding to the source code to be repaired. The code model is then fine-tuned based on the evaluation results between the inferred patch and the sample labels (e.g., cross-entropy loss). Because the extraction and retrieval of code structure features are added during the context retrieval process, the target context can be made closer to the features of the source code to be repaired, which helps the code model to accurately grasp the deep features of the source code, perform reasoning, and give full play to the program repair capabilities of the code model.
[0041] Following the above technical concept, in Figure 2 The diagram illustrates a method flowchart for fine-tuning a code model according to embodiments of this specification. It is understood that this method can be executed by any device, apparatus, platform, or cluster of devices with computing and processing capabilities. See also... Figure 2 In one embodiment, the method is based on a code library containing multiple source codes and corresponding patch codes; the method includes at least the following steps: S201: Obtain a first representation vector corresponding to any first source code among the multiple source codes, the first representation vector being determined by performing a first encoding on the text of the first source code and a second encoding on the first abstract syntax tree corresponding to the first source code. S203: Based on the first representation vector, perform a similarity-based search in the code library to obtain a target context; the target context includes several other source codes whose similarity to the first source code satisfies a first threshold and their corresponding patch codes. S205: Based on the first source code and the target context, generate prompt text, input it into the code model, and obtain an inferred patch. S207: Based on the inferred patch and the first patch code corresponding to the first source code, fine-tune the parameters of the code model.
[0042] The specific execution method of each of the above steps will be described in detail below with reference to the accompanying drawings.
[0043] In step S201, a first representation vector corresponding to any first source code among the plurality of source codes is obtained. The first representation vector is determined by performing a first encoding on the text of the first source code and a second encoding on the first abstract syntax tree corresponding to the first source code.
[0044] The source code contained in the code repository can be program code written in any programming language, such as C++, Java, Python, etc. In some scenarios, the code repository may also contain source code written in multiple programming languages. In the embodiments of this specification, the Python programming language will be used as an example. However, it should be understood that one or more embodiments in this specification are intended to provide a method for fine-tuning a code model, and are not limited to a specific programming language. The method provided by the embodiments of this invention can be applied to any scenario involving the technical concept provided in the embodiments of this invention.
[0045] First, it should be noted that source code defects are diverse, including but not limited to syntax errors, code dependency issues, and logical errors. These defects can all prevent the source code from achieving its intended function. This specification does not limit the specific types of defects described in the embodiments. For code defects present in the source code, the code repository also contains corresponding patch code. Patch code can fix these defects, ensuring that the source code can be interpreted, compiled, and executed. Patch code can take various forms, such as code text, patch packages, and configuration statements; this specification does not specifically limit its presentation.
[0046] In this step, for any source code in the code library (i.e., the first source code), its first representation vector is obtained. The first representation vector can be pre-encoded and stored in the code library with the first source code, or it can be generated in real time when the first source code is selected; there is no limitation here.
[0047] As mentioned earlier, the first representation vector can be determined by performing a first encoding on the text of the first source code and a second encoding on the first abstract syntax tree corresponding to the first source code. The result of the first encoding can represent the semantic features of the code in the first source code, and the result of the second encoding can represent the structural features of the code in the first source code.
[0048] According to one implementation, the first encoding can be text encoding of the first source code using a first text model. The first text model is typically a natural language processing model, which has powerful language understanding capabilities and can encode the code text of the first source code, extracting semantic information from the code and representing it as a dense vector. For example, the first text model could be a Transformer-based BERT model, or a CodeGPT model pre-trained on the code file, etc.
[0049] The second encoding is based on the first abstract syntax tree (AST) and can capture the structured information of the code in the first source code. Specifically, an AST is a data structure that displays the various code elements and their relationships in a source code file in a tree structure. Each node represents a code element in the source code, such as a module, class declaration, function definition, etc. The connections between nodes reflect the syntactic hierarchy and code relationships between code elements, such as function calls, variable references, etc. The second encoding, by encoding the nodes and edges in the first AST, transforms the syntactic structure and logical relationships in the first source code into vector representations, capturing the potential relationships between various code elements in the first source code and representing its deep code structure features. The AST can also be viewed as a special type of graph structure, and the second encoding can utilize Graph Neural Networks (GNNs) to encode the first AST. Furthermore, in one embodiment, the second encoding can use an attention-based graph neural network, where high weights are applied to key nodes in the first AST, thereby highlighting these key nodes.
[0050] According to one implementation, the second encoding process can be as follows: first, the first abstract syntax tree is converted into a text sequence, and then the text sequence is encoded. In this implementation, the second encoding specifically includes the following steps: performing a serialization operation on the first abstract syntax tree to obtain a converted text sequence, including node identifiers representing each node of the first abstract syntax tree, and structure labels representing the structural relationships between nodes; and then using a second text model to encode the converted text sequence.
[0051] The converted text sequence contains not only the node information of the first abstract syntax tree, but also the structural information between the nodes. The serialization operation may involve first instantiating the first abstract syntax tree as a programming object, and then using an object serialization method provided by the programming language (e.g., the pickle module in Python) to convert the entire first abstract syntax tree into a byte stream, which serves as the converted text sequence.
[0052] According to one implementation, the serialization operation can introduce preset tags into the converted text sequence to mark the parent-child relationship between nodes. As mentioned earlier, the parent-child relationship between nodes of the abstract syntax tree (represented as the connection between nodes) reflects the syntax hierarchy and code relationship between code elements. Therefore, it can be understood that marking the parent-child relationship between nodes of the abstract syntax tree in the text sequence can achieve the purpose of incorporating code structure information into the text sequence.
[0053] Specifically, the structural labels include a pair of labels marked with node identifiers, which are used to indicate the child nodes of the node corresponding to the marked node identifier; the serialization operation may include: during the target traversal of the first abstract syntax tree, for any target node currently visited, extracting its node identifier, adding the node identifier to the range of the first label pair in the current sequence, where the first label pair marks the node identifier of the parent node of the target node; the current sequence obtained at the end of the target traversal is used as the converted text sequence.
[0054] The target traversal can employ traversal methods such as breadth-first traversal or depth-first traversal to visit each node of the first abstract syntax tree. During the target traversal, for any target node visited, its node identifier is extracted. The node identifier can be the name of the code element represented by the node, such as module name, class name, function name, etc., or it can be a summary information of the code element represented by the node, such as the function definition body, the class and its member definition headers, etc.
[0055] To accurately depict the hierarchical relationships between nodes, when adding the node identifier of a target node to the current sequence, it can be added to the first tag pair corresponding to the node identifier of its parent node. In this way, the content within each tag pair range can reflect the parent-child relationship between nodes; in other words, the nesting relationship between structural tags can reflect the hierarchical relationship between nodes. It's easy to understand that, for a single syntax tree, the root node can be used as the node identifier labeled by the outermost tag pair in the current sequence. Figure 3A This diagram illustrates a serialization operation. Each code element in the source code is represented as a node in an abstract syntax tree (AST), and the relationships between these elements are represented by connecting edges. It's important to understand that the AST in the diagram is for illustrative purposes only. For simplicity, node identifiers are represented by numbers, and only a three-level tree is used as an example. In practical applications, ASTs typically have deeper levels and more detailed node identifiers. See further... Figure 3ADuring the target traversal, any visited target node will be added to the current sequence. In the example, parentheses are used as tag pairs in the structural labels. Taking any step in the target traversal process as an example, when the visited target node is #1.1.1, the corresponding tag pair of the target node's parent node in the current sequence is (#1.1). Therefore, the node identifier of the target node can be added to the scope of this tag pair, updating it to (#1.1#1.1.1). Thus, after the target traversal is completed, the structural relationships between the nodes can be embedded in the current sequence through tag pairs, and this sequence can serve as the transformed text sequence. It should be understood that the target traversal process shown in the attached diagram does not represent a specific limitation on the order of node visits. In practice, the choice of traversal algorithm will determine the order in which the target nodes are visited. Nodes in the syntax tree may be visited in different orders depending on the traversal algorithm; no specific limitation is made here.
[0056] Besides using self-closing tags like parentheses in the examples above as tag pairs in structural tags, in some practices, double tags can also be used as tag pairs. These tags consist of a start tag and a corresponding end tag, with tag content in between, for example: start tag <x> End tag< / x> . Figure 3B This diagram illustrates a serialization operation using double tags as tag pairs. Figure 3A Using the same abstract syntax tree as in the example, by performing a target traversal, a current sequence that can serve as the transformed text sequence is generated. In the current sequence, double labels are used ( <x> ……< / x> As a pair of labels, the node identifiers of the parent nodes of the target nodes are marked. After the target traversal is completed, the current sequence shown in the figure can be obtained. The node identifier within any label of a label pair is the parent node, and the content between the start and end labels of the label pair is the node identifier of each child node of that parent node. In this way, the purpose of expressing the structural relationships between nodes in the transformed text sequence can also be achieved.
[0057] It is understandable that, in certain application scenarios, filters can be applied during the execution of the target traversal to filter out nodes that are accessed but have no coding significance, thus preventing the inclusion of nodes unrelated to the code structure in the converted text sequence. The rules for the filters can be flexibly formulated according to the specific application scenario, and will not be elaborated here.
[0058] The above describes the serialization operation of the abstract syntax tree included in the second encoding process. After obtaining the transformed text sequence corresponding to the first abstract syntax tree, text encoding can be performed based on this transformed text sequence to obtain a vector that can represent the structural features of the first source code. Similar to the first encoding method described above, a second text model capable of text encoding can be used in the implementation of the second encoding process. This second text model can be the same as the first text model, or it can be another model; no limitation is made here.
[0059] By executing the first encoding and the second encoding, a first vector and a second vector are obtained, respectively. Based on this, the first representation vector can be formed by fusing these two vectors. This process can flexibly employ different fusion strategies according to specific practical needs. According to one practice, principal component analysis can be used to extract the principal components from the first and second vectors to generate the first representation vector, thereby preserving the key features of both vectors. According to another practice, vector concatenation can be used to connect the first and second vectors end-to-end to form the first representation vector. There are various methods for obtaining the fused vector, and the embodiments in this specification will not be exemplified one by one.
[0060] According to one implementation, the first representation vector can be a weighted average of the first vector and the second vector. Specifically, specific weight coefficients are applied to corresponding elements in the first vector and the second vector, and then they are added together to obtain the first representation vector. In this way, the feature information carried by the two vectors can be combined more flexibly, and the weight setting can be used to control the proportion of a certain type of feature of the first source code in the first representation vector.
[0061] According to one implementation, the first vector and the second vector may not be merged; that is, the first representation vector may simply include the first vector and the second vector.
[0062] Next, in step S203, based on the first representation vector, a similarity-based retrieval is performed in the code library to obtain the target context; the target context includes several other source codes and their corresponding patch codes that have a similarity to the first source code that meets a first threshold.
[0063] In this step, a search is performed in the code repository for the first source code to obtain other source code files related to the first source code. This search process can be based on the first representation vector. Other source codes in the code repository each have their own corresponding representation vectors for similarity retrieval. These representation vectors can also be determined based on their respective source code text and abstract syntax tree, following the calculation method described above for the first representation vector.
[0064] During the retrieval process, the similarity between any other source code in the codebase and the first source code is calculated based on the first representation vector. If the first representation vector is a single vector obtained by fusing the first and second vectors, the vector similarity between the first representation vector and the representation vector of another source code can be calculated. If the vector similarity is higher than a preset similarity threshold (i.e., the first threshold), then the other source code and its corresponding patch code are included in the target context. The first threshold defines the minimum similarity level that other source code can achieve with the first source code to be considered relevant. That is, if the similarity between the representation vector of a source code and the first representation vector exceeds the first threshold, then this source code can be considered highly relevant to the first source code in terms of code semantics and code structure, and this source code and its corresponding patch code can be included in the target context.
[0065] If the first representation vector includes a first vector and a second vector, then comparisons can be made separately based on the first and second vectors, and the comparison results can be combined. For example, the first similarity of the first vector of the first source code with the first vectors of other source codes can be calculated, and a first source code set can be selected based on the first similarity. The second similarity of the second vector of the first source code with the second vectors of other source codes can be calculated, and a second source code set can be selected based on the second similarity. The source codes and their corresponding patch codes in the intersection of the first and second source code sets are then assigned to the target context. Alternatively, the combined similarity between the other source code and the first source code can be determined based on the first and second similarities of the other source code. Several other source codes similar to the first source code can be identified based on the combined similarity, and these source codes and their corresponding patch codes are then assigned to the target context.
[0066] In other examples, other similarity retrieval strategies can be flexibly adopted based on the first representation vector, which will not be described in detail here.
[0067] After determining the target context, in step S205, a prompt text can be generated based on the first source code and the target context, and the input code model can be used to obtain the inference patch.
[0068] In this step, the established target context, combined with the first source code, can be used to generate hint text to guide the code model. The purpose of the hint text is to help the code model understand the potential characteristics of the first source code and generate corresponding patch code that can fix program defects in the first source code. The target context contained in the hint text can provide the code model with rich relevant information (including several related source codes and corresponding defect-fixing patch code) to help it accurately infer the patch code applicable to the first source code.
[0069] However, it's important to note that in practice, codebases often contain a massive number of code patch pairs (i.e., source code and its corresponding patches), and these patch pairs are typically quite large (some source files can even have tens of thousands of lines of code). This can result in extremely large prompt text containing a large number of tokens. A token is the basic unit for text processing in a code model; it can represent a word, punctuation mark, or other text element. Generally, the longer the prompt text, the more tokens it contains, which increases the amount of information the code model needs to process during inference, thus affecting analysis efficiency. Therefore, to ensure inference efficiency, code models usually preset a limit on the number of input tokens.
[0070] Accordingly, according to one implementation, in the prompt text, the other source codes are arranged in descending order of similarity to the first source code, and the number of the other source codes is such that the prompt text does not exceed the maximum number of input tokens. The purpose of sorting is to place the other source codes most relevant to the first source code and their corresponding patch codes at the top of the prompt text, so that they can be given priority in subsequent analysis or reasoning.
[0071] Meanwhile, to comply with the code model's limit on the number of input tokens (if any), the number of other source codes in the prompt text can be controlled at a critical value. At this value, the total number of tokens in the prompt text will not exceed the maximum number of input tokens that the code model can accept, and there will be no more space to accommodate another code fix pair. The prompt text constructed according to this implementation method ensures both the richness and relevance of the prompt text, while avoiding the risk of the number of tokens exceeding the code model's receiving capacity due to excessive text length.
[0072] Furthermore, according to one implementation, to enhance the code model's ability to understand the relationship between source code and corresponding patch code in the prompt text, a first tag can be used to mark each source code and a second tag can be used to mark each patch code in the prompt text. The introduction of this tagging mechanism allows the code model to more clearly identify the correspondence between source code and patch code, thereby accurately locating and utilizing relevant information during the reasoning process, and effectively extracting the repair pattern between the source code and patch code.
[0073] The specific forms of the first and second tags can be varied. For example, the first tag can be [BUG], and the corresponding second tag can be [FIX]; the first tag can be [source code], and the corresponding second tag can be [patch], and so on. This specification does not provide a complete list of these examples.
[0074] It's easy to understand that in the prompt text, several other source codes related to the first source code are each marked with a first tag, and their corresponding patch codes are marked with a second tag. The first source code is also marked with a first tag, but the content of its corresponding second tag can be empty, or no second tag can be set. This is because the patch code marked by the second tag is the result that needs to be inferred and output by the code model.
[0075] Next, the code model can make inferences based on the input prompt text and output an inference patch corresponding to the first source code. In step S207: the parameters of the code model are fine-tuned according to the first patch code corresponding to the inference patch and the first source code.
[0076] By comparing the differences between the inferred patch and the first patch code (as the sample label), the code model can capture the biases that exist in the inference process and fine-tune the model parameters to optimize the predictive ability of the code model, making its inferred patch code more accurate and closer to the sample label.
[0077] In a specific practice, the code model is a large language model, and the parameter fine-tuning can be a full parameter fine-tuning of the large language model. During this fine-tuning process, the large language model can utilize feedback obtained from comparing the inferred patch with the first patch code to adjust its internal parameters through backpropagation, including but not limited to updating weight parameters and bias parameters. Full parameter fine-tuning updates all model parameters, meaning that both general feature knowledge learned during pre-training and task-specific knowledge learned during fine-tuning are integrated into the model's parameters. This not only enhances the model's inference performance on the current automatic program repair task but also improves its generalization ability when handling similar tasks.
[0078] This iterative parameter fine-tuning helps the code model better adapt to the characteristics of programming languages and learn program defect features, thereby enabling it to efficiently and accurately identify and repair various code defects in subsequent automatic program repair tasks. Steps S203-S205 described above are also applicable to the code model's processing of automatic program repair tasks. Based on the semantic and structural features of the input source code to be checked, relevant repair pairs are retrieved from the code library and filtered based on a first threshold. These, along with the source code to be checked, constitute a prompt text, which is then input into the code model to infer the patch code. In other words, the fine-tuned code model can be used to infer the corresponding patch code for the source code to be checked based on the target prompt text; the target prompt text includes the source code to be checked, several reference source codes whose similarity to the source code to be checked meets the first threshold, and their corresponding patch codes.
[0079] The foregoing description, based on one or more embodiments, details a method for fine-tuning a code model. Using the method provided in the embodiments of this specification, the semantic and structural representations of the code can be comprehensively utilized to perform accurate contextual retrieval. The retrieved context is then used to construct prompt text, guiding the code model to infer patch code. The fine-tuning of the code model is based on the differences between the inferred patch and the patch sample labels. Through iterative parameter fine-tuning, the code model's understanding of code semantics and structure is continuously enhanced, improving the inference accuracy of code repair tasks and optimizing the generalization ability of the code model.
[0080] In this specification, the terms "first" in the first code, first model, etc., and the corresponding terms "second" and "third" (if they exist) in the text, are merely for the convenience of distinction and description, and do not have any limiting meaning.
[0081] The foregoing description describes specific embodiments of this specification; other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than those shown in the embodiments, and the desired result may still be achieved. Furthermore, the processes depicted in the drawings do not necessarily need to follow the specific or sequential order shown to achieve the desired result. In some embodiments, multitasking and parallel processing are possible or may be advantageous.
[0082] Figure 4 This is a schematic diagram of an apparatus for fine-tuning a code model according to an embodiment of this specification. The apparatus 400 is deployed in a computing device, which can be implemented using any device, equipment, platform, device cluster, etc., with computing and processing capabilities. This apparatus embodiment is similar to... Figure 2 Corresponding to the illustrated method embodiment, it operates based on a codebase containing multiple source code snippets and corresponding patch code; the device 400 includes:
[0083] The encoding module 401 is configured to obtain a first representation vector corresponding to any first source code among the plurality of source codes. The first representation vector is determined by performing a first encoding on the text of the first source code and a second encoding on the first abstract syntax tree corresponding to the first source code.
[0084] The retrieval module 402 is configured to perform a similarity-based retrieval in the code library based on the first representation vector to obtain a target context; the target context includes several other source codes and their corresponding patch codes that have a similarity to the first source code that meets a first threshold.
[0085] The inference module 403 is configured to generate a prompt text based on the first source code and the target context, input the code model, and obtain an inference patch;
[0086] The fine-tuning module 404 is configured to fine-tune the parameters of the code model based on the inferred patch and the first patch code corresponding to the first source code.
[0087] According to another embodiment, this specification also provides a computer program product, including a computer program / instructions that, when executed by a processor, implement the foregoing combinations. Figure 2 The steps of the method are described.
[0088] According to yet another embodiment, this specification also provides a computing device including a memory and a processor, characterized in that the memory stores executable code, and when the processor executes the executable code, it implements the foregoing combination. Figure 2 The steps of the method are described.
[0089] Those skilled in the art will recognize that the functions described in the embodiments of the present invention in one or more of the above examples can be implemented using hardware, software, firmware, or any combination thereof. When implemented in software, these functions can be stored in a computer-readable medium or transmitted as one or more instructions or code on a computer-readable medium.
[0090] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above descriptions are merely specific embodiments of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, or improvements made based on the technical solutions of the present invention should be included within the scope of protection of the present invention.
Claims
1. A method for fine-tuning a code model, based on a code repository containing multiple source code lines and corresponding patch code; the method includes: Obtain a first representation vector corresponding to any first source code among the multiple source codes. The first representation vector is determined by performing a first encoding on the text of the first source code and a second encoding on the first abstract syntax tree corresponding to the first source code. The second encoding includes: performing a serialization operation on the first abstract syntax tree to obtain a converted text sequence, the converted text sequence including node identifiers representing each node of the first abstract syntax tree and structure labels representing the structural relationships between nodes; and performing text encoding on the converted text sequence using a second text model. Based on the first representation vector, a similarity-based retrieval is performed in the code library to obtain the target context; the target context includes several other source codes and their corresponding patch codes that have a similarity to the first source code that meets a first threshold. Based on the first source code and the target context, generate prompt text, input it into the code model, and obtain the inference patch; Based on the inferred patch and the first patch code corresponding to the first source code, the parameters of the code model are fine-tuned.
2. The method according to claim 1, wherein, The fine-tuned code model is used to infer the corresponding patch code for the source code to be checked based on the target prompt text; the target prompt text includes the source code to be checked, several reference source codes whose similarity to the source code to be checked meets a first threshold, and their corresponding patch codes.
3. The method according to claim 1, wherein, The first encoding includes: The first source code is text-encoded using the first text model.
4. The method according to claim 1, wherein, The structural label includes a pair of labels marked with node identifiers, which are used to indicate the child nodes of the node corresponding to the marked node identifier; The serialization operation includes: During the target traversal of the first abstract syntax tree, for any target node currently visited, its node identifier is extracted and added to the range of the first label pair in the current sequence. The first label pair is labeled with the node identifier of the parent node of the target node. The current sequence obtained at the end of the target traversal is used as the converted text sequence.
5. The method according to claim 1, wherein, The first representation vector is the weighted average of the first vector obtained by the first encoding and the second vector obtained by the second encoding.
6. The method according to claim 1, wherein, In the prompt text, the other source codes are arranged from high to low similarity to the first source code, and the number of the other source codes is such that the prompt text does not exceed the maximum number of the upper bound of the number of input tokens.
7. The method according to claim 1, wherein, In the prompt text, each source code is marked with a first tag, and each patch code is marked with a second tag.
8. The method according to claim 1, wherein, The code model is a large language model, and the fine-tuning of the parameters of the code model includes: fine-tuning all parameters of the large language model.
9. An apparatus for fine-tuning a code model, operating based on a code repository containing multiple source code lines and corresponding patch code, the apparatus comprising: The encoding module is configured to: obtain a first representation vector corresponding to any first source code among the plurality of source codes; the first representation vector is determined by performing a first encoding on the text of the first source code and a second encoding on the first abstract syntax tree corresponding to the first source code; the second encoding includes: performing a serialization operation on the first abstract syntax tree to obtain a converted text sequence; the converted text sequence includes node identifiers representing each node of the first abstract syntax tree and structure labels representing the structural relationships between nodes; and performing text encoding on the converted text sequence using a second text model. The retrieval module is configured to perform a similarity-based retrieval in the code library based on the first representation vector to obtain a target context; the target context includes several other source codes and their corresponding patch codes that have a similarity to the first source code that meets a first threshold. The inference module is configured to generate prompt text based on the first source code and the target context, input the code model, and obtain the inference patch; The fine-tuning module is configured to fine-tune the parameters of the code model based on the inferred patch and the first patch code corresponding to the first source code.
10. A computer program product comprising a computer program / instructions that, when executed by a processor, implement the steps of the method according to any one of claims 1-8.
11. A computing device, comprising a memory and a processor, characterized in that, The memory stores executable code, and when the processor executes the executable code, it implements the method of any one of claims 1-8.
Citation Information
Patent Citations
Program code similarity measurement method based on abstract syntax tree access context
CN113434145A
Code processing model training method, code task processing method and code development method
CN118860900A
System and method for obtaining enhanced patch generation for automatic program repair
CN119213410A