Code generation

By integrating a reward model for segment-level feedback, the method addresses the sparse reward issue in reinforcement learning for code generation, enhancing learning efficiency and error correction, resulting in improved code generation performance.

WO2026085665A1PCT designated stage Publication Date: 2026-04-30BEIJING YOUZHUJU NETWORK TECH CO LTD +1
View PDF 5 Cites 0 Cited by

Patent Information

Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
BEIJING YOUZHUJU NETWORK TECH CO LTD
Filing Date
2024-10-21
Publication Date
2026-04-30

AI Technical Summary

Technical Problem

Reinforcement learning from unit test feedback for code generation suffers from sparse rewards, limiting learning efficiency and incremental improvements due to the lack of intermediate feedback, hindering the model's ability to identify and correct errors during training.

Method used

Integrate a reward model that provides segment-level feedback by determining a correctness score for each code segment generated, using a trained reward model to fine-tune the target machine learning model through reinforcement learning, mimicking human-like iterative refinement.

Benefits of technology

Enhances learning efficiency by offering immediate guidance during code generation, improving the model's ability to identify and correct errors, leading to more efficient and effective code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024126171_30042026_PF_FP_ABST
    Figure CN2024126171_30042026_PF_FP_ABST
Patent Text Reader

Abstract

Embodiments of the present disclosure provide a solution for code generation. A method comprises: receiving a query indicating a code generation problem; generating, using a target machine learning model, a first code segment based on the query and prefix code, wherein the target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem; determining, using a trained reward model, a first reward score corresponding to the first code segment based on updated prefix code, the first reward score indicating a correctness of the first code segment with respect to the code generation problem, wherein the first code segment is attached to the prefix code, to obtain the updated prefix code; and fine-tuning the target machine learning model through reinforcement learning based at least on the first reward score.
Need to check novelty before this filing date? Find Prior Art

Description

CODE GENERATIONField

[0001] The disclosed example embodiments relate generally to machine learning and, more particularly, to a method, apparatus, device and computer readable storage medium for code generation.Background

[0002] The rapid advancement of language models (LMs) has revolutionized code generation, enabling models to achieve near-human performance on programming tasks. These models have demonstrated abilities to generate syntactically correct and functionally viable code snippets, significantly aiding software development processes. Building upon these successes, recent research has explored the use of reinforcement learning (RL) from unit test feedback to further enhance the code generation capabilities of LMs. By incorporating unit tests as a reward mechanism, these methods aim to guide LMs toward generating code that not only compiles but also passes specified test cases, thereby improving overall code reliability and quality.Summary

[0003] In a first aspect of the present disclosure, there is provided a method for code generation. The method comprises: receiving a query indicating a code generation problem; generating, using a target machine learning model, a first code segment based on the query and prefix code, wherein the target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem, and wherein the prefix code comprise none or at least one code segment previously generated by the target machine learning model for the query; determining, using a trained reward model, a first reward score corresponding to the first code segment based on updated prefix code, the first reward score indicating a correctness of the first code segment with respect to the code generation problem, wherein the first code segment is attached to the prefix code, to obtain the updated prefix code; and fine-tuning the target machine learning model through reinforcement learning based at least on the first reward score.

[0004] In a second aspect of the present disclosure, there is provided an apparatus for code generation. The apparatus comprises: a query receiving module configured to receive a query indicating a code generation problem; a first code segment generating module configured to generate, using a target machine learning model, a first code segment based on the query and prefix code, wherein the target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem, and wherein the prefix code comprise none or at least one code segment previously generated by the target machine learning model for the query; a first reward score determining module configured to determine, using a trained reward model, a first reward score corresponding to the first code segment based on updated prefix code, the first reward score indicating a correctness of the first code segment with respect to the code generation problem, wherein the first code segment is attached to the prefix code, to obtain the updated prefix code; and a model fine-tuning module configured to fine-tune the target machine learning model through reinforcement learning based at least on the first reward score.

[0005] In a third aspect of the present disclosure, there is provided an electronic device. The device comprises at least one processing unit; and at least one memory coupled to the at least one processing unit and storing instructions executable by the at least one processing unit. The instructions, upon execution by the at least one processing unit, cause the device to perform the method of the first aspect.

[0006] In a fourth aspect of the present disclosure, a computer-readable storage medium is provided. The medium stores a computer program which, when executed by a processor, causes the method of the first aspect to be implemented.

[0007] In a fifth aspect of the present disclosure, a computer program product is provided. The computer program product comprises a computer program which, when executed by a processor, causes the method of the first aspect to be implemented.

[0008] It would be appreciated that the content described in the Summary section of the present invention is neither intended to identify key or essential features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will be readily envisaged through the following description.Brief Description of the Drawings

[0009] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent in combination with the accompanying drawings and with reference to the following detailed description. In the drawings, the same or similar reference symbols refer to the same or similar elements, where:

[0010] FIG. 1 illustrates a schematic diagram of an example environment in which embodiments of the present disclosure may be implemented;

[0011] FIG. 2 illustrates a schematic diagram of fine-tuning a target machine learning by a reward model in accordance with some embodiments of the present disclosure;

[0012] FIG. 3 illustrates a schematic diagram of training a reward model in accordance with some embodiments of the present disclosure;

[0013] FIG. 4 illustrates a schematic diagram of binary search over code segments in accordance with some embodiments of the present disclosure;

[0014] FIG. 5 illustrates an algorithm of binary search for labeling partial code prefix in accordance with some embodiments of the present disclosure;

[0015] FIG. 6 illustrates a flowchart of a process for code generation in accordance with some embodiments of the present disclosure;

[0016] FIG. 7 shows a block diagram of an apparatus for code generation in accordance with some embodiments of the present disclosure; and

[0017] FIG. 8 illustrates a block diagram of an electronic device in which one or more embodiments of the present disclosure can be implemented.Detailed Description

[0018] The embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although some embodiments of the present disclosure are shown in the drawings, it would be appreciated that the present disclosure may be implemented in various forms and should not be interpreted as limited to the embodiments described herein. On the contrary, these embodiments are provided for a more thorough and complete understanding of the present disclosure. It would be appreciated that the drawings and embodiments of the present disclosure are only for the purpose of illustration and are not intended to limit the scope of protection of the present disclosure.

[0019] In the description of the embodiments of the present disclosure, the term "including" and similar terms would be appreciated as open inclusion, that is, "including but not limited to" . The term "based on" would be appreciated as "at least partially based on" . The term "one embodiment" or "the embodiment" would be appreciated as "at least one embodiment" . The term "some embodiments" would be appreciated as "at least some embodiments" . Other explicit and implicit definitions may also be included below. As used herein, the term "model" can represent the matching degree between various data. For example, the above matching degree can be obtained based on various technical solutions currently available and / or to be developed in the future.

[0020] It will be appreciated that the data involved in this technical proposal (including but not limited to the data itself, data acquisition or use) shall comply with the requirements of corresponding laws, regulations and relevant provisions.

[0021] It will be appreciated that before using the technical solution disclosed in each embodiment of the present disclosure, users should be informed of the type, the scope of use, the use scenario, etc. of the personal information involved in the present disclosure in an appropriate manner in accordance with relevant laws and regulations, and the user’s authorization should be obtained.

[0022] For example, in response to receiving an active request from a user, a prompt message is sent to the user to explicitly prompt the user that the operation requested operation by the user will need to obtain and use the user's personal information. Thus, users may select whether to provide personal information to the software or the hardware such as an electronic device, an application, a server or a storage medium that perform the operation of the technical solution of the present disclosure according to the prompt information.

[0023] As an optional but non-restrictive implementation, in response to receiving the user's active request, the method of sending prompt information to the user may be, for example, a pop-up window in which prompt information may be presented in text. In addition, pop-up windows may also contain selection controls for users to choose “agree” or “disagree” to provide personal information to electronic devices.

[0024] It will be appreciated that the above notification and acquisition of user authorization process are only schematic and do not limit the implementations of the present disclosure. Other methods that meet relevant laws and regulations may also be applied to the implementation of the present disclosure.

[0025] As used herein, the term "model" can learn a correlation between respective inputs and outputs from training data, so that a corresponding output can be generated for a given input after training is completed. The generation of the model can be based on machine learning techniques. Deep learning is a machine learning algorithm that processes inputs and provides corresponding outputs by using multiple layers of processing units. A neural networks model is an example of a deep learning-based model. As used herein, "model" may also be referred to as "machine learning model" , "learning model" , "machine learning network" , or "learning network" , and these terms are used interchangeably herein.

[0026] “Neural networks” are a type of machine learning network based on deep learning. Neural networks are capable of processing inputs and providing corresponding outputs, typically comprising input and output layers and one or more hidden layers between the input and output layers. Neural networks used in deep learning applications typically comprise many hidden layers, thereby increasing the depth of the network. The layers of neural networks are sequentially connected so that the output of the previous layer is provided as input to the latter layer, where the input layer receives the input of the neural network and the output of the output layer serves as the final output of the neural network. Each layer of a neural network comprises one or more nodes (also known as processing nodes or neurons) , each of which processes input from the previous layer.

[0027] Usually, machine learning can roughly comprise three stages, namely training stage, test stage, and application stage (also known as inference stage) . During the training stage, a given model can be trained using a large scale of training data, iteratively updating parameter values until the model can obtain consistent inference from the training data that meets the expected objective. Through the training, the model can be considered to learn the correlation between input and output (also known as input-to-output mapping) from the training data. The parameter values of the trained model are determined. In the test stage, test inputs are applied to the trained model to test whether the model can provide correct outputs, thereby determining the performance of the model. In the application stage, the model can be used to process actual inputs and determine corresponding outputs based on the parameter values obtained from training.

[0028] FIG. 1 illustrates a block diagram of an example environment 100 in which various embodiments of the present disclosure may be implemented. In the environment 100 of FIG. 1, a code generation system 110 applies a machine learning model 105 to perform code generation. The machine learning model 105 is configured to process a query 112 input by a user to generate a code segment 114.

[0029] The environment 100 further includes a reward model 116 which is configured to determine a reward score 118 corresponding to the code segment 114 generated by the machine learning model 105. The machine learning model 105 is fine-tuned based at least on the reward score 118.

[0030] In FIG. 1, the code generation system 110 may be implemented at any computing system with computing capability, such as various computing devices / systems, terminal devices, servers, etc. Terminal devices may include any type of mobile terminals, fixed terminals, or portable terminals, including mobile phones, desktop computers, laptops, netbooks, tablets, media computers, multimedia tablets, or any combination of the aforementioned, including accessories and peripherals of these devices or any combination thereof. Servers include but are not limited to mainframe, edge computing nodes, computing devices in cloud environment, etc.

[0031] It should be understood that the components and arrangements in the environment 100 shown in FIG. 1 are merely examples, and a computing system suitable for implementing the example embodiments described in the present disclosure may include one or more different components, other components, and / or different arrangements. For example, although shown as separate, the model training system 110 and the model application system 130 may be integrated into the same system or device. The implementation of the present disclosure is not restricted in this regard.

[0032] It should be understood that the structure and function of each element in the environment 100 is described for illustrative purposes only and does not imply any limitations on the scope of the present disclosure.

[0033] As mentioned above, unit tests may be leveraged to generate code. However, a challenge arises from the nature of the reward signals derived from unit tests. These signals are inherently sparse, as they are only received at the end of an episode after the entire code snippet has been generated and evaluated. This delay in feedback impedes learning efficiency and limits the model’s ability to make incremental improvements during code generation. When an LM fails to generate code that passes any unit tests, it receives no meaningful learning signal, making it difficult to learn to solve more complex coding problems. In contrast, programmers typically do not rewrite code from scratch when their programs fail unit tests. Instead, they analyze the code to pinpoint and fix errors, leveraging their understanding of programming logic and structure to iteratively improve upon the current version. This process of step-by-step refinement, which involves receiving and acting upon fine-grained feedback, is missing in the current RL training loop for code generation from unit test feedback.

[0034] Code generation tasks may be formulated within a reinforcement learning (RL) framework, where code generation is treated as a sequence of decision-making steps. Once a language model has undergone supervised fine-tuning (SFT) , the RL phase is employed to refine the model’s ability to generate functionally correct code using feedback from unit tests. Unit test feedback is derived by executing the generated program on predefined test cases. The feedback serves as a signal for learning and may be transformed into a reward. A simple reward function based on the outcome of the unit tests may be defined as follows:

[0035] This binary reward formulation encourages the language model to generate programs that can successfully pass all unit test cases. Given a collection of unlabeled code generation prompts the model pθ is optimized to maximize the expected reward over all possible code generation trajectories.

[0036] While the reinforcement learning from unit test feedback (RLTF) offers a framework for improving code generation models, it suffers from significant limitations due to the sparsity of its reward signal. The binary nature of unit test feedback, indicating only whether the entire program passes or fails, provides no guidance on which specific parts of the code contributed to the outcome. This lack of intermediate feedback makes it challenging for the model to identify and correct errors during training, leading to slow convergence and suboptimal performance. In contrast, human programmers iteratively develop and refine their code. When a program fails to pass unit tests, they do not typically rewrite it from scratch. Instead, they analyze the code to pinpoint and fix errors, leveraging their understanding of programming logic and structure. This process of step-by-step refinement is crucial for efficient problem-solving.

[0037] RL with unit test feedback has enhanced language models’ code generation, but relies on sparse rewards provided only after complete code evaluation, limiting learning efficiency and incremental improvements. When generated code fails all unit tests, no learning signal is received, hindering progress on complex tasks.

[0038] To address at least some of the above issues, embodiments of the present disclosure propose an improved solution for code generation. In this solution, a query indicating a code generation problem is received. A target machine learning model is used to generate a first code segment based on the query and prefix code. The target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem. The prefix code comprises none or at least one code segment previously generated by the target machine learning model for the query. A trained reward model is used to determine a first reward score corresponding to the first code segment based on updated prefix code. The first reward score indicates a correctness of the first code segment with respect to the code generation problem. The first code segment is attached to the prefix code, to obtain the updated prefix code. The target machine learning model is fine-tuned through reinforcement learning based at least on the first reward score.

[0039] With these embodiments of the present disclosure, a reward model is integrated into the RL training framework for code generation and the reward model provides signals by offering segment-level feedback that indicates the correctness of each code segment generated by the target machine learning model. In this way, the learning efficiency of target machine learning model may be enhanced by providing immediate guidance during code generation.

[0040] Example embodiments of the present disclosure will be described with the reference to FIGS. 2 to 8.

[0041] Reference is now made to FIG. 2, which illustrates a schematic diagram 200 of fine-tuning a target machine learning by a reward model in accordance with some embodiments of the present disclosure. As shown in FIG. 2, a query 205 indicating a code generation problem is received. In code generation tasks, the code generation problem may be defined as a sequence of tokens x= (x1, x2, …, xm) , where each xi denotes the i-th element or token of the input prompt, which may include problem descriptions. In an example, the query 205 may be “please sort a list of numbers in Python” .

[0042] After the query 205 is received, a target machine learning model (not shown) is used to generate a first code segment 210 based on the query 205 and prefix code. The target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem. The prefix code comprises none or at least one code segment previously generated by the target machine learning model for the query. In some embodiments, a code segment may refer to each code line, two or more code lines. In some embodiments, a code segment may refer to one or more code tokens. Herein, a code segment is a unit for reward calculation, or a unit for feedback signal collection.

[0043] In some embodiments, the primary objective for the target machine learning model is to process the given input query 205 (denoted as x) and generate a coherent and syntactically correct sequence of code tokens (also referred to as first code segment 210) . This sequence may be denoted as y= (y (1) , y (2) , …, y (T) ) , where  T represents the total number of code generation steps and each code generation step corresponds to each code segment. Each individual code generation step, y (t) , t=1, 2, …, T, is composed of a series of tokens  where corresponds to the i-th token within the t-th step, and nt denotes the number of tokens in this step.

[0044] In some embodiments, the target machine learning model denoted as pθ, is employed to model the conditional probability distribution of the code generation steps (also referred to as the first code segment 210) y, given the code generation problem (also referred to as the query 205) x, which is mathematically represented as pθ (y|x) , parameterized by θ. The target machine learning model may be optimized through training on a dataset containing pairs of prompts (e.g., queries indicating a code generation problem) and their corresponding code solutions. This training process, often referred to as supervised fine-tuning (SFT) , involves maximizing the log-likelihood of the dataset.

[0045] After the first code segment 210 is generated, a trained reward model 215 is used to determine a first reward score 220 corresponding to the first code segment 210 based on updated prefix code. The first reward score 220 indicates a correctness of the first code segment 210 with respect to the code generation problem. The first code segment 210 is attached to the prefix code, to obtain the updated prefix code. In some embodiments, a process reward model (PRM, as an example of the reward model 215) may be introduced to assess the correctness of each line (i.e., each code segment) of the code during the generation process. The PRM may serve as an oracle that provides intermediate rewards based on the potential of the current code prefix to be extended into a correct program.

[0046] In the following, how the reward model 215 is trained will be described with reference to FIG. 3, which illustrates a schematic diagram 300 of training a reward model 215 in accordance with some embodiments of the present disclosure.

[0047] In some embodiments, a training data sample for the reward model 215 to be trained may be obtained. The training data sample may comprise a sample query 305 and a sample computer code 310 for the sample query 305. The sample computer code 310 may comprise a plurality of sample code segments with respective labeled reward scores 315. For example, the training data sample may be denoted as { (x, y≤m, lm) } ) , where x represents the sample query, y≤m represents the plurality of sample code segments up to the m-th sample code segment and lm represents respective labeled reward scores corresponding to the plurality of sample code segments.

[0048] To effectively train the reward model 215, a dataset that provides fine-grained annotations indicating the correctness of partial code sequences (also referred to as code segments) may be required. The correctness may be represented by labeled reward scores. Manually annotating the correctness of each line of code generated by the target machine learning model is costly and not scalable. Instead, the capability of the target machine learning model is leveraged to generate completions for partial code prefixes and automated testing is used to assess their correctness. The key idea is to determine whether a partial code prefix (e.g., sample code segment) can be extended into a complete program that passes all unit tests. If so, the prefix may be considered as potentially correct; otherwise, it may be labeled as incorrect. As a result, respective labeled reward scores 315  may be obtained.

[0049] In the following, obtaining the training data sample may be described with reference to FIG. 4, which illustrates a schematic diagram 400 of binary search over code segments at line level in accordance with some embodiments of the present disclosure. In some embodiments, for a first reference code segment in the sample computer code, sample prefix code may be extracted from the sample computer code. The sample prefix code may comprise the first reference code segment and at least one sample code segment preceding the first reference code segment in the sample computer code. In an example, as shown in FIG. 4, there are 5 code segments in the sample computer code, the midpoint, i.e., code segment 430 (denoted as y (3) ) may be selected as the first reference code segment. Sample prefix code comprising the first reference code segment (i.e., code segment 430) and at least one sample code segment (i.e., code segment 410, denoted as y (1) and code segment 420, denoted as y (2) ) preceding the first reference code segment in the sample computer code may be extracted.

[0050] After the sample prefix code is extracted, code completion on the sample prefix code may be performed to obtain at least one predicted computer code. Then, a first labeled reward score corresponding to the first reference code segment may be determined based on respective test results of a second plurality of unit test cases applied on the at least one predicted computer code. In some embodiments, given a prompt x (e.g., the sample prefix code) , a complete code response (as an example of the at least one predicted computer code) y= (y (1) , y (2) , …, y (T) ) is generated using the current policy pθ of the target machine learning model. In some embodiments, the correctness of each partial code prefixes (also referred to as the first reference code segment) y≤t for t=1, 2, …, T may be determined. To achieve this, a best-of-K sampling strategy may be employed to approximate an oracle capable of completing the sample prefix code comprising the first reference code segment. For each partial code prefix y≤t, K potential completions may be generated using the current policy of the target machine learning model. Full programs may be formed and executed against the unit tests which are applied on the at least one predicted computer code.

[0051] For example, in some embodiments, in accordance with a determination that the respective test results of a second plurality of unit test cases applied on one of the at least one predicted computer code indicates successful test results, the first labeled reward score may be determined to indicate that prefix code comprising the first reference code segment in the sample computer code is correct. If any of full programs (i.e., programs including the at least one predicted computer code) pass all unit tests, the first reference code segment may be labeled as correct (e.g., labeled as +1) to indicate prefix code comprising the first reference code segment in the sample computer code is correct.

[0052] In some embodiments, in accordance with a determination that the respective test results of a second plurality of unit test cases applied on the at least one predicted computer code indicates failure test results, the first labeled reward score may be determined to indicate that prefix code comprising the first reference code segment in the sample computer code is incorrect. If all of these programs fail to pass all unit tests, the first reference code segment may be labeled as incorrect (e.g., labeled as -1) to indicate prefix code comprising the first reference code segment in the sample computer code is incorrect.

[0053] In some embodiments, in accordance with a determination that the first labeled reward score  indicates that prefix code comprising the first reference code segment in the sample computer code is correct, at least one labeled reward score corresponding to the at least one sample code segment preceding the first reference code segment may be determined to indicate that corresponding prefix code is correct. If any of these programs pass all unit tests and the first reference code is labeled as correct (e.g., labeled as +1) , at least one sample code segment preceding the first reference code segment may also be labeled as correct (e.g., labeled as +1) to indicate that corresponding prefix code is correct. In an example, as shown in FIG. 4, in a case where first reference code segment is code segment 430, prefix code up to first reference code segment can be completed to pass all unit tests, code segment 410, 420 and 430 (i.e., sample code segments preceding the first reference code segment and the first reference code segment) are labeled as correct. In a case where the first reference code segment is code segment 440, the prefix code up to first reference code segment cannot be completed to pass all unit tests, code segment 440 is labeled as incorrect. For each partial code prefix y≤m, the label lm is assigned based on the outcome of the completion attempts:

[0054] where represents full programs formed and executed against the unit tests

[0055] In some embodiments, in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, a second reference code segment may be selected from suffix code of the sample computer code following the first reference code segment.

[0056] In some embodiments, the first reference code segment may be selected as a midpoint code segment in the sample computer code. The second reference code segment may be selected as a midpoint code segment in the suffix code. In some examples, in a case where there are 10 code segments in the sample computer code, a midpoint (i.e., the fifth code segment) is selected as the first reference code segment. If the first reference code segment is labeled as correct, a second reference code segment may be selected from the suffix code (i.e., the sixth to the tenth code segment) of the sample computer code following the first reference code segment may be selected. In an example, a midpoint of the suffix code (i.e., the eighth code segment) may be selected as the second reference code segment.

[0057] In some embodiments, in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is incorrect, a second reference code segment may be selected from partial prefix code of the sample computer code which precedes the first reference code segment and has no labeled reward scores. In some examples, if the first reference code segment is labeled as incorrect, the error may come from partial prefix code of the sample computer code which precedes the first reference code segment. The second reference code segment which has no labeled reward scores may be selected from the partial prefix code.

[0058] In some embodiments, the second reference code segment may be selected as a midpoint code segment in the partial prefix code. In some examples, in a case where there are 10 code segments in the sample computer code, a midpoint (i.e., the fifth code segment) is selected as the first reference code segment. If the first reference code segment is labeled as incorrect, a second reference code segment may be selected from the  partial code (i.e., the first to the fourth code segment) of the sample computer code preceding the first reference code segment may be selected. In an example, a midpoint of the partial code (i.e., the third code segment) which has no labeled reward scores may be selected as the second reference code segment. To efficiently identify the transition point where errors occur, a binary search over the code generation steps (e.g., the sample computer code) may be employed, which is formalized in FIG. 5, which illustrates an algorithm of binary search for labeling partial code prefix in accordance with some embodiments of the present disclosure. As shown in FIG. 5, firstly a midpoint code segment in generated code segments may be selected. Then, code completion may be performed on prefix code comprising the midpoint code segment and code segments preceding the midpoint code segment to obtain a full program comprising at least one predicted computer code. If the full program passes all unit tests, the midpoint code segment is labeled as correct and the algorithm will operate on the range of the midpoint code segment and the upper bound of generated code segments. Otherwise, the midpoint code segment is labeled as incorrect and the algorithm will operate on the range of the midpoint code segment and the lower bound of generated code segments.

[0059] In this way, by using the binary search method, the efficiency of labelling each code segment generated by the target machine learning model may be improved.

[0060] After the second reference code segment is selected, a second labeled reward score corresponding to the second reference code segment may be determined. In some embodiments, the second labeled reward score may be determined based on unit test of code comprising the second reference code segment.

[0061] During the process of training the reward model 215, after the training data sample is obtained, a plurality of predicted reward scores corresponding to the plurality of sample code segments may be generated respectively by using the reward model 215 to be trained. Using the obtained training data sample, the reward model 215 to be trained may predict the correctness (also referred to as predicted reward scores) of the plurality of sample code segments in the training data sample. The reward model 215 learns to assign higher rewards to sample code segment labeled as correct and lower rewards to those labeled as incorrect.

[0062] After the predicted reward scores are generated, the reward model may be trained based on respective differences between the plurality of predicted reward scores and the respective labeled reward scores corresponding to the plurality of sample code segments, to obtain the trained reward model.

[0063] In some embodiments, a loss value of a predefined loss function may be determine based on the respective differences between the plurality of predicted reward scores and the respective labeled reward scores corresponding to the plurality of sample code segments, and the reward model may be trained to minimize or reduce the loss value of the predefined loss function. The training objective to minimize or reduce the loss value may be expressed as follows:

[0064] where Rφ (x, y≤m) represents the plurality of predicted reward scores and lm represents respective labeled reward scores, Rφ (x, y≤m) -lm represents respective differences between the plurality of predicted reward  scores and the respective labeled reward scores. In some examples, mean squared error (MSE) loss or cross-entropy loss may be employed as the loss function.

[0065] After the first reward score is determined, the target machine learning model is fine-tuned through reinforcement learning based at least on the first reward score. In some embodiments, by offering intermediate feedback (e.g., first reward score) , the PRM helps the target machine learning model identify and reinforce beneficial code generation patterns while discouraging those that introduce errors. This fine-grained feedback mirrors the human approach to coding, where programmers continuously evaluate and adjust their code. In this way, the reward model may be well trained and may provide more accurate reward scores corresponding to the generated code segments.

[0066] In some embodiments, a model parameter set of a value model may be initialized with a model parameter set of the trained reward model 215. The value model may be utilized in the reinforcement learning. Reference is made back to FIG. 2, a set of values 225 is output by the value model. The reward model’s method of annotating code, by fixing a prefix y≤t and rolling out the policy to sample correct responses, may be viewed as a value estimation of y≤t. The capability of the trained reward model 215 to provide line-level feedback may serve as a useful inductive bias for initializing the value function (also referred to as the value model) in RL algorithms, which can ease the credit assignment problem by offering a more informed starting point.

[0067] In some embodiments, the trained reward model 215 may be used to provide step-level reward signals that guide more efficient policy exploration during RL training. By rating the correctness of each line in the code response (as an example of a code segment generated by the target machine learning model) , the trained reward model 215 supplies dense rewards that encourage the policy of the target machine learning model to explore more promising code paths, leading to improved performance of the target machine learning model.

[0068] In some embodiments, the computer code for the code generation problem may be obtained. The computer code comprises a plurality of code segments iteratively generated by the target machine learning model for the query. An overall reward score for the computer code may be generated based on respective test results of a first plurality of unit test cases applied on the computer program. In an example, as shown in Eq. (1) , if a computer code passes all unit test cases applied on the computer program, the overall reward score of the computer code is 1, otherwise the overall reward score is 0.

[0069] After the overall reward score is generated, the target machine learning model may be fine-tuned through reinforcement learning based on respective reward scores generated by the reward model 215 for the plurality of code segments and the overall reward score.

[0070] In some embodiments, a base model of the target machine learning model is initially fine-tuned on a supervised fine-tuning (SFT) dataset, which then is served as the initialization for the subsequent RLHF training phase. The fine-tuned base model (denoted as πref) may be fine-tuned on the RLHF dataset using proximal policy optimization (PPO) to obtain a model (denoted as πθ) . In the present disclosure, two types of outcome reward models (ORMs) may be employed as the objective functions for RL training. For non-coding prompts, a general reward model, Rgeneral (x, y) , derived from preference learning on a human-annotated dataset may be used. For coding prompts, the ORM (e.g., the reward model 215) may be defined as a  binary indicator of whether the response passes all unit tests, RUT (x, y) . The RLHF optimization objective may be defined as:

[0071] where R (x, y) =Rgeneral (x, y) for non-coding prompts and R (x, y) = RUT (x, y) for coding prompts.

[0072] To ensure that the training data for PRM (e.g., the reward model 215) effectively covers the state space the language model (e.g., the target machine learning model) may encounter during the next RL training phase, policy models may be sampled from various stages of the RL. Specifically, 4 checkpoints are selected evenly spaced throughout the RL baseline model’s training process. For each checkpoint, n responses are sampled for each coding prompt in the training dataset For each sampled response, the binary search labeling procedure described in the algorithm in FIG. 5 may be applied, using K=20 completions for each partial code prefix. The data collected from all checkpoints is then aggregated into a PRM training set, denoted as The PRM (e.g., the reward model 215) may be initialized with the value model from the RL baseline and fine-tuned on the aggregated dataset,  using the objective function defined Eq. (3) .

[0073] As described above, regarding integrating the reward model 215 into RL training, the reward model 215 may be used as a source of dense reward signals and used to initialize the value model. In the approach of providing dense reward signals, the reward model 215 assigns additional reward signals at each end-of-line token (\n) in the code response for coding prompts. Therefore, the RL optimization objective for coding prompts is modified to the weighted sum of RUT and RPRM, defined as follows:

[0074] where λ controls the relative importance of the reward model 215 in shaping the reward. In an example, λ=0.25 may be set when the code response does not pass all unit tests, i.e., RUT (x, y) =0, and λ=0.025 may be set when the response passes all unit tests, i.e., RUT (x, y) =1. The intuition behind this reward shaping is to leverage the reward model 215 to provide informative signals when the RL policy fails to generate a valid solution, while minimizing the risk of reward model over-optimization once a correct solution is found.

[0075] Some embodiments propose a PRM (e.g., the reward model 215) that delivers dense, line-level feedback on code correctness during generation, mimicking human code refinement and providing immediate guidance.

[0076] Some embodiments conduct a comprehensive analysis of how PRMs can be integrated into RL training for code generation. Various strategies for training a robust code PRM are explored and different methods of utilizing PRMs to improve code generation performance are investigated. Based on some experiments, a practical solution for using PRMs and integrating them into RL training in the context of code generation problems is provided. In addition, using PRMs concurrently as both dense rewards and value function initialization in RL training leads to a significant performance improvement of code generation.

[0077] Some embodiments propose an effective approach that automatically generates process-level  supervision data by identifying the first error line in generated code using binary search. Then, a PRM may be trained on this data to generate dense signals during RL training. The PRM may benefit RL from unit test feedback in code generation.

[0078] Some embodiments conduct systematic experiments to determine how to properly and effectively integrate PRMs into RL. Various strategies for training a high-quality code PRM and utilizing PRMs to improve code generation are explored. As a result, a practical solution for successfully using PRMs in the context of code generation is provided. By following the solution, the pass rate of the target machine learning model is enhanced. In addition, it is found that integrating PRMs into RL training benefits code generation in long-horizon scenarios.

[0079] FIG. 6 illustrates a flowchart of a process 600 for code generation in accordance with some embodiments of the present disclosure. The process 600 may be implemented at the code generation system 110 of FIG. 1.

[0080] At block 610, the code generation system 110 receives a query indicating a code generation problem.

[0081] At block 620, the code generation system 110 generates, using a target machine learning model, a first code segment based on the query and prefix code. The target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem, and the prefix code comprises none or at least one code segment previously generated by the target machine learning model for the query.

[0082] At block 630, the code generation system 110 determines, using a trained reward model, a first reward score corresponding to the first code segment based on updated prefix code. The first reward score indicates a correctness of the first code segment with respect to the code generation problem, and the first code segment is attached to the prefix code, to obtain the updated prefix code.

[0083] At block 640, the code generation system 110 fine-tunes the target machine learning model through reinforcement learning based at least on the first reward score.

[0084] In some embodiments, fine-tuning the target machine learning model through reinforcement learning based at least on the first reward score comprise: initializing a model parameter set of a value model with a model parameter set of the trained reward model, wherein the value model is utilized in the reinforcement learning; and fine-tuning the target machine learning together with the value model through the reinforcement learning based at least on the first reward score.

[0085] In some embodiments, the process 600 further comprises: obtaining the computer code for the code generation problem, the computer code comprising a plurality of code segments iteratively generated by the target machine learning model for the query; and generating an overall reward score for the computer code based on respective test results of a first plurality of unit test cases applied on the computer program. Fine-tuning the target machine learning model through reinforcement learning based at least on the first reward score comprise: fine-tuning the target machine learning model through reinforcement learning based on respective reward scores generated by the reward model for the plurality of code segments and the overall reward score.

[0086] In some embodiments, the process 600 further comprises: obtaining a training data sample for the  reward model to be trained, the training data sample comprising a sample query and a sample computer code for the sample query, the sample computer code comprising a plurality of sample code segments with respective labeled reward scores; generating, using the reward model to be trained, a plurality of predicted reward scores corresponding to the plurality of sample code segments, respectively; and training the reward model based on respective differences between the plurality of predicted reward scores and the respective labeled reward scores corresponding to the plurality of sample code segments, to obtain the trained reward model.

[0087] In some embodiments, obtaining the training data sample comprises: for a first reference code segment in the sample computer code, extracting, from the sample computer code, sample prefix code comprising the first reference code segment and at least one sample code segment preceding the first reference code segment in the sample computer code; performing code completion on the sample prefix code to obtain at least one predicted computer code; and determining a first labeled reward score corresponding to the first reference code segment based on respective test results of a second plurality of unit test cases applied on the at least one predicted computer code.

[0088] In some embodiments, determining a first labeled reward score comprises: in accordance with a determination that the respective test results of a second plurality of unit test cases applied on one of the at least one predicted computer code indicates successful test results, determining the first labeled reward score to indicate that prefix code comprising the first reference code segment in the sample computer code is correct; and in accordance with a determination that the respective test results of a second plurality of unit test cases applied on the at least one predicted computer code indicates failure test results, determining the first labeled reward score to indicate that prefix code comprising the first reference code segment in the sample computer code is incorrect.

[0089] In some embodiments, obtaining the training data sample further comprises: in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, determining at least one labeled reward score corresponding to the at least one sample code segment preceding the first reference code segment to indicate that corresponding prefix code is correct.

[0090] In some embodiments, obtaining the training data sample further comprises: in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, select a second reference code segment from suffix code of the sample computer code following the first reference code segment; in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is incorrect, select a second reference code segment from partial prefix code of the sample computer code which precedes the first reference code segment and has no labeled reward scores; and determining a second labeled reward score corresponding to the second reference code segment.

[0091] In some embodiments, the first reference code segment is selected as a midpoint code segment in the sample computer code, and / or wherein the second reference code segment is selected as a midpoint code segment in the suffix code or the partial prefix code.

[0092] FIG. 7 shows a block diagram of an apparatus 700 for code generation in accordance with some embodiments of the present disclosure. The apparatus 700 may be implemented, for example, or included at the code generation system 110 of FIG. 1. Various modules / components in the apparatus 700 may be implemented by hardware, software, firmware, or any combination thereof.

[0093] As shown, the apparatus 700 includes a query receiving module 710 configured to receive a query indicating a code generation problem; a first code segment generating module 720 configured to generate, using a target machine learning model, a first code segment based on the query and prefix code, wherein the target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem, and wherein the prefix code comprise none or at least one code segment previously generated by the target machine learning model for the query; a first reward score determining module 730 configured to determine, using a trained reward model, a first reward score corresponding to the first code segment based on updated prefix code, the first reward score indicating a correctness of the first code segment with respect to the code generation problem, wherein the first code segment is attached to the prefix code, to obtain the updated prefix code; and a model fine-tuning module 740 configured to fine-tune the target machine learning model through reinforcement learning based at least on the first reward score.

[0094] In some embodiments, the model fine-tuning module 740 is further configured to initialize a model parameter set of a value model with a model parameter set of the trained reward model, wherein the value model is utilized in the reinforcement learning; and fine-tune the target machine learning together with the value model through the reinforcement learning based at least on the first reward score.

[0095] In some embodiments, the apparatus 700 further comprises an overall reward score configured to obtain the computer code for the code generation problem, the computer code comprising a plurality of code segments iteratively generated by the target machine learning model for the query; and generate an overall reward score for the computer code based on respective test results of a first plurality of unit test cases applied on the computer program. The model fine-tuning module 740 is further configured to fine-tune the target machine learning model through reinforcement learning based on respective reward scores generated by the reward model for the plurality of code segments and the overall reward score.

[0096] In some embodiments, the apparatus 700 further comprises a reward model training module configured to obtain a training data sample for the reward model to be trained, the training data sample comprising a sample query and a sample computer code for the sample query, the sample computer code comprising a plurality of sample code segments with respective labeled reward scores; generate, using the reward model to be trained, a plurality of predicted reward scores corresponding to the plurality of sample code segments, respectively; and train the reward model based on respective differences between the plurality of predicted reward scores and the respective labeled reward scores corresponding to the plurality of sample code segments, to obtain the trained reward model.

[0097] In some embodiments, the reward model training module is further configured to, for a first reference code segment in the sample computer code, extract, from the sample computer code, sample prefix code comprising the first reference code segment and at least one sample code segment preceding the first reference  code segment in the sample computer code; perform code completion on the sample prefix code to obtain at least one predicted computer code; and determine a first labeled reward score corresponding to the first reference code segment based on respective test results of a second plurality of unit test cases applied on the at least one predicted computer code.

[0098] In some embodiments, the reward model training module is further configured to, in accordance with a determination that the respective test results of a second plurality of unit test cases applied on one of the at least one predicted computer code indicates successful test results, determine the first labeled reward score to indicate that prefix code comprising the first reference code segment in the sample computer code is correct; and in accordance with a determination that the respective test results of a second plurality of unit test cases applied on the at least one predicted computer code indicates failure test results, determine the first labeled reward score to indicate that prefix code comprising the first reference code segment in the sample computer code is incorrect.

[0099] In some embodiments, the reward model training module is further configured to, in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, determine at least one labeled reward score corresponding to the at least one sample code segment preceding the first reference code segment to indicate that corresponding prefix code is correct.

[0100] In some embodiments, the reward model training module is further configured to, in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, select a second reference code segment from suffix code of the sample computer code following the first reference code segment; in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is incorrect, select a second reference code segment from partial prefix code of the sample computer code which precedes the first reference code segment and has no labeled reward scores; and determine a second labeled reward score corresponding to the second reference code segment.

[0101] In some embodiments, the first reference code segment is selected as a midpoint code segment in the sample computer code, and / or the second reference code segment is selected as a midpoint code segment in the suffix code or the partial prefix code.

[0102] FIG. 8 illustrates a block diagram of an electronic device 800 in which one or more embodiments of the present disclosure can be implemented. It would be appreciated that the electronic device 800 shown in FIG. 8 is only an example and should not constitute any restriction on the function and scope of the embodiments described herein. The electronic device 800 may be used, for example, to implement the code generation system 110 of FIG. 1. The electronic device 800 may also be used to implement the apparatus 700 of FIG. 7.

[0103] As shown in FIG. 8, the electronic device 800 is in the form of a general computing device. The components of the electronic device 800 may include, but are not limited to, one or more processors or processing units 810, a memory 820, a storage device 830, one or more communication units 840, one or more input devices 850, and one or more output devices 860. The processing unit 810 may be an actual or virtual processor and can execute various processes according to the programs stored in the memory 820. In a multiprocessor system,  multiple processing units execute computer executable instructions in parallel to improve the parallel processing capability of the electronic device 800.

[0104] The electronic device 800 typically includes a variety of computer storage medium. Such medium may be any available medium that is accessible to the electronic device 800, including but not limited to volatile and non-volatile medium, removable and non-removable medium. The memory 820 may be volatile memory (for example, a register, cache, a random access memory (RAM) ) , a non-volatile memory (for example, a read-only memory (ROM) , an electrically erasable programmable read-only memory (EEPROM) , a flash memory) or any combination thereof. The storage device 830 may be any removable or non-removable medium, and may include a machine-readable medium, such as a flash drive, a disk, or any other medium, which can be used to store information and / or data (such as training data for training) and can be accessed within the electronic device 800.

[0105] The electronic device 800 may further include additional removable / non-removable, volatile / non-volatile storage medium. Although not shown in FIG. 8, a disk driver for reading from or writing to a removable, non-volatile disk (such as a "floppy disk" ) , and an optical disk driver for reading from or writing to a removable, non-volatile optical disk can be provided. In these cases, each driver may be connected to the bus (not shown) by one or more data medium interfaces. The memory 820 may include a computer program product 825, which has one or more program modules configured to perform various methods or acts of various embodiments of the present disclosure.

[0106] The communication unit 840 communicates with a further computing device through the communication medium. In addition, functions of components in the electronic device 800 may be implemented by a single computing cluster or multiple computing machines, which can communicate through a communication connection. Therefore, the electronic device 800 may be operated in a networking environment using a logical connection with one or more other servers, a network personal computer (PC) , or another network node.

[0107] The input device 850 may be one or more input devices, such as a mouse, a keyboard, a trackball, etc. The output device 860 may be one or more output devices, such as a display, a speaker, a printer, etc. The electronic device 800 may also communicate with one or more external devices (not shown) through the communication unit 840 as required. The external device, such as a storage device, a display device, etc., communicate with one or more devices that enable users to interact with the electronic device 800, or communicate with any device (for example, a network card, a modem, etc. ) that makes the electronic device 800 communicate with one or more other computing devices. Such communication may be executed via an input / output (I / O) interface (not shown) .

[0108] According to example implementation of the present disclosure, a computer-readable storage medium is provided, on which a computer-executable instruction or computer program is stored, where the computer-executable instructions or the computer program is executed by the processor to implement the method described above. According to example implementation of the present disclosure, a computer program product is also provided. The computer program product is physically stored on a non-transient computer-readable medium and includes computer-executable instructions, which are executed by the processor to implement the  method described above.

[0109] Various aspects of the present disclosure are described herein with reference to the flow chart and / or the block diagram of the method, the device, the equipment and the computer program product implemented in accordance with the present disclosure. It would be appreciated that each block of the flowchart and / or the block diagram and the combination of each block in the flowchart and / or the block diagram may be implemented by computer-readable program instructions.

[0110] These computer-readable program instructions may be provided to the processing units of general-purpose computers, special computers or other programmable data processing devices to produce a machine that generates a device to implement the functions / acts specified in one or more blocks in the flow chart and / or the block diagram when these instructions are executed through the processing units of the computer or other programmable data processing devices. These computer-readable program instructions may also be stored in a computer-readable storage medium. These instructions enable a computer, a programmable data processing device and / or other devices to work in a specific way. Therefore, the computer-readable medium containing the instructions includes a product, which includes instructions to implement various aspects of the functions / acts specified in one or more blocks in the flowchart and / or the block diagram.

[0111] The computer-readable program instructions may be loaded onto a computer, other programmable data processing apparatus, or other devices, so that a series of operational steps can be performed on a computer, other programmable data processing apparatus, or other devices, to generate a computer-implemented process, such that the instructions which execute on a computer, other programmable data processing apparatus, or other devices implement the functions / acts specified in one or more blocks in the flowchart and / or the block diagram.

[0112] The flowchart and the block diagram in the drawings show the possible architecture, functions and operations of the system, the method and the computer program product implemented in accordance with the present disclosure. In this regard, each block in the flowchart or the block diagram may represent a part of a module, a program segment or instructions, which contains one or more executable instructions for implementing the specified logic function. In some alternative implementations, the functions marked in the block may also occur in a different order from those marked in the drawings. For example, two consecutive blocks may actually be executed in parallel, and sometimes can also be executed in a reverse order, depending on the function involved. It should also be noted that each block in the block diagram and / or the flowchart, and combinations of blocks in the block diagram and / or the flowchart, may be implemented by a dedicated hardware-based system that performs the specified functions or acts, or by the combination of dedicated hardware and computer instructions.

[0113] Each implementation of the present disclosure has been described above. The above description is an example, not exhaustive, and is not limited to the disclosed implementations. Without departing from the scope and spirit of the described implementations, many modifications and changes are obvious to ordinary skill in the art. The selection of terms used in this article aims to best explain the principles, practical application or improvement of technology in the market of each implementation, or to enable other ordinary skill in the art to understand the various embodiments disclosed herein.

Claims

1.A method for code generation, comprising:receiving a query indicating a code generation problem;generating, using a target machine learning model, a first code segment based on the query and prefix code, wherein the target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem, and wherein the prefix code comprises none or at least one code segment previously generated by the target machine learning model for the query;determining, using a trained reward model, a first reward score corresponding to the first code segment based on updated prefix code, the first reward score indicating a correctness of the first code segment with respect to the code generation problem, wherein the first code segment is attached to the prefix code, to obtain the updated prefix code; andfine-tuning the target machine learning model through reinforcement learning based at least on the first reward score.2.The method of claim 1, wherein fine-tuning the target machine learning model through reinforcement learning based at least on the first reward score comprise:initializing a model parameter set of a value model with a model parameter set of the trained reward model, wherein the value model is utilized in the reinforcement learning; andfine-tuning the target machine learning together with the value model through the reinforcement learning based at least on the first reward score.3.The method of claim 1 or 2, further comprising:obtaining the computer code for the code generation problem, the computer code comprising a plurality of code segments iteratively generated by the target machine learning model for the query;generating an overall reward score for the computer code based on respective test results of a first plurality of unit test cases applied on the computer program; andwherein fine-tuning the target machine learning model through reinforcement learning based at least on the first reward score comprise:fine-tuning the target machine learning model through reinforcement learning based on respective reward scores generated by the reward model for the plurality of code segments and the overall reward score.4.The method of any of claims 1 to 3, further comprises:obtaining a training data sample for the reward model to be trained, the training data sample comprising a sample query and a sample computer code for the sample query, the sample computer code comprising a plurality of sample code segments with respective labeled reward scores;generating, using the reward model to be trained, a plurality of predicted reward scores corresponding to the plurality of sample code segments, respectively; andtraining the reward model based on respective differences between the plurality of predicted reward scores and the respective labeled reward scores corresponding to the plurality of sample code segments, to obtain the trained reward model.5.The method of claim 4, wherein obtaining the training data sample comprises:for a first reference code segment in the sample computer code,extracting, from the sample computer code, sample prefix code comprising the first reference code segment and at least one sample code segment preceding the first reference code segment in the sample computer code;performing code completion on the sample prefix code to obtain at least one predicted computer code; anddetermining a first labeled reward score corresponding to the first reference code segment based on respective test results of a second plurality of unit test cases applied on the at least one predicted computer code.6.The method of claim 5, wherein determining a first labeled reward score comprises:in accordance with a determination that the respective test results of a second plurality of unit test cases applied on one of the at least one predicted computer code indicates successful test results, determining the first labeled reward score to indicate that prefix code comprising the first reference code segment in the sample computer code is correct; andin accordance with a determination that the respective test results of a second plurality of unit test cases applied on the at least one predicted computer code indicates failure test results, determining the first labeled reward score to indicate that prefix code comprising the first reference code segment in the sample computer code is incorrect.7.The method of claim 6, wherein obtaining the training data sample further comprises:in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, determining at least one labeled reward score corresponding to the at least one sample code segment preceding the first reference code segment to indicate that corresponding prefix code is correct.8.The method of claim 6, wherein obtaining the training data sample further comprises:in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, selecting a second reference code segment from suffix code of the sample computer code following the first reference code segment;in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is incorrect, selecting a second reference code segment from partial prefix code of the sample computer code which precedes the first reference code segment and has no labeled reward scores; anddetermining a second labeled reward score corresponding to the second reference code segment.9.The method of claim 5 or 8, wherein the first reference code segment is selected as a midpoint code segment in the sample computer code, and / orwherein the second reference code segment is selected as a midpoint code segment in the suffix code or the partial prefix code.10.An electronic device, comprising:at least one processing unit; andat least one memory coupled to the at least one processing unit and storing instructions executable by the at least one processing unit, the instructions, upon execution by the at least one processing unit, causing the device to perform acts comprising:receiving a query indicating a code generation problem;generating, using a target machine learning model, a first code segment based on the query and prefix code, wherein the target machine learning model is constructed based on a language model and is configured to iteratively generate code segments to form computer code for the code generation problem, and wherein the prefix code comprise none or at least one code segment previously generated by the target machine learning model for the query;determining, using a trained reward model, a first reward score corresponding to the first code segment based on updated prefix code, the first reward score indicating a correctness of the first code segment with respect to the code generation problem, wherein the first code segment is attached to the prefix code, to obtain the updated prefix code; andfine-tuning the target machine learning model through reinforcement learning based at least on the first reward score.11.The electronic device of claim 10, wherein fine-tuning the target machine learning model through reinforcement learning based at least on the first reward score comprise:initializing a model parameter set of a value model with a model parameter set of the trained reward model, wherein the value model is utilized in the reinforcement learning; andfine-tuning the target machine learning together with the value model through the reinforcement learning based at least on the first reward score.12.The electronic device of claim 10 or 11, the acts further comprising:obtaining the computer code for the code generation problem, the computer code comprising a plurality of code segments iteratively generated by the target machine learning model for the query;generating an overall reward score for the computer code based on respective test results of a first plurality of unit test cases applied on the computer program ; andwherein fine-tuning the target machine learning model through reinforcement learning based at least on the first reward score comprise:fine-tuning the target machine learning model through reinforcement learning based on respective reward scores generated by the reward model for the plurality of code segments and the overall reward score.13.The electronic device of any of claims 10-12, the acts further comprising:obtaining a training data sample for the reward model to be trained, the training data sample comprising a sample query and a sample computer code for the sample query, the sample computer code comprising a plurality of sample code segments with respective labeled reward scores; andgenerating, using the reward model to be trained, a plurality of predicted reward scores corresponding to the plurality of sample code segments, respectively;training the reward model based on respective differences between the plurality of predicted reward scores and the respective labeled reward scores corresponding to the plurality of sample code segments, to obtain the trained reward model.14.The electronic device of claim 13, wherein obtaining the training data sample comprises:for a first reference code segment in the sample computer code,extracting, from the sample computer code, sample prefix code comprising the first reference code segment and at least one sample code segment preceding the first reference code segment in the sample computer code;performing code completion on the sample prefix code to obtain at least one predicted computer code; anddetermining a first labeled reward score corresponding to the first reference code segment based on respective test results of a second plurality of unit test cases applied on the at least one predicted computer code.15.The electronic device of claim 14, wherein determining a first labeled reward score comprises:in accordance with a determination that the respective test results of a second plurality of unit test cases applied on one of the at least one predicted computer code indicates successful test results, determining the first labeled reward score to indicate that prefix code comprising the first reference code segment in the sample computer code is correct; andin accordance with a determination that the respective test results of a second plurality of unit test cases applied on the at least one predicted computer code indicates failure test results, determining the first labeled reward score to indicate that prefix code comprising the first reference code segment in the sample computer code is incorrect.16.The electronic device of claim 15, wherein obtaining the training data sample further comprises:in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, determining at least one labeled reward score corresponding to the at least one sample code segment preceding the first reference code segment to indicate that corresponding prefix code is correct.17.The electronic device of claim 15, wherein obtaining the training data sample further comprises:in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is correct, selecting a second reference code segment from suffix code of the sample computer code following the first reference code segment;in accordance with a determination that the first labeled reward score indicates that prefix code comprising the first reference code segment in the sample computer code is incorrect, selecting a second reference code segment from partial prefix code of the sample computer code which precedes the first reference code segment and has no labeled reward scores; anddetermining a second labeled reward score corresponding to the second reference code segment.18.The electronic device of claim 14 or 17, wherein the first reference code segment is selected as a midpoint code segment in the sample computer code, and / orwherein the second reference code segment is selected as a midpoint code segment in the suffix code or the partial prefix code.19.A computer-readable storage medium, having a computer program stored thereon which, upon execution by an electronic device, causes the device to perform the method according to any of claims 1 to 9.20.A computer program product being embodied on a computer-readable medium and comprising computer-executable instructions which are executed by a processor to perform the method according to any of claims 1 to 9.

Citation Information

Patent Citations

  • Code generation and defect repair method and device

    CN116909532A

  • Code generation model fine tuning method and device based on clustering and natural language strategy optimization algorithm

    CN118468982A

  • Code automatic completion method, system and device based on deep reinforcement learning

    CN118519681A

  • Method for performing efficient parameter fine tuning on code model in combination with abstract syntax tree

    CN118733052A

  • Code generation through reinforcement learning using code-quality rewards

    US20230195428A1