Apparatus and method for the training of a code synthesis model

The method trains a code synthesis model using functional tests as feedback and a secondary model to predict test results, addressing the inefficiencies and inaccuracies in existing models, resulting in improved code generation accuracy and efficiency across various programming languages.

WO2024213252A1PCT designated stage expired Publication Date: 2024-10-17HUAWEI TECH CO LTD +1
View PDF 0 Cites 4 Cited by

Patent Information

Application Number
PCT/EP2023/059676
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Filing Date
2023-04-13
Publication Date
2024-10-17

AI Technical Summary

Technical Problem

Existing code synthesis models face challenges in training due to the lack of execution feedback and sparse data for programming language code, leading to inefficiencies and inaccuracies in generating error-free code.

Method used

The proposed solution involves an apparatus and method for training a code synthesis model using functional tests as feedback, where a secondary machine learning model is trained simultaneously to predict the results of these tests, allowing for reinforcement learning and generation of functional tests across different programming language representations, thereby improving data and computation efficiency.

Benefits of technology

This approach enables the training of an accurate code synthesis model that produces properly functioning code by leveraging execution feedback and overcoming the sparsity of functional tests, enhancing coding capabilities for both novice and experienced programmers.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure EP2023059676_17102024_PF_FP_ABST
    Figure EP2023059676_17102024_PF_FP_ABST
Patent Text Reader

Abstract

An apparatus (201, 801) for training a code synthesis model, the code synthesis model configured to output code based on natural language input, the apparatus comprising one or more processors (802) configured to: receive (701) a code synthesis model (203, 301); input (702) natural language data (302) to the code synthesis model (301) to form output code (303) corresponding to the data (302); determine (703) the result of one or more functional tests (306) for the output code (303); and update (704) the code synthesis model (301) based on the result of the tests (306) and feedback from a secondary machine learning model (304) trained simultaneously with the code synthesis model (301). Using the result of functional tests as feedback during training may allow accurate code synthesis models to be trained. Training the secondary model simultaneously with the code synthesis model may further improve the efficiency of the training process.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] APPARATUS AND METHOD FOR THE TRAINING OF A CODE SYNTHESIS MODEL

[0002] FIELD OF THE INVENTION

[0003] This invention relates to code synthesis models for the generation of code from natural language input, in particular to the training of such models.

[0004] BACKGROUND

[0005] Code synthesis has recently gained much traction, aiming to provide computational models that are able to generate programming language code based on a natural language description of a problem. An example is shown in Figure 1. At 101 , a natural language input “Write a function that computes the n’th Fibonacci number” is input to a code synthesis model 102 and the resulting function is output at 103.

[0006] Providing this technology may greatly improve the coding capabilities of human users, for example by enabling complete novices to start writing code. It may also save experienced programmers time when writing specific parts of a larger code base. As the potential user base for code synthesis ranges from complete beginners to expert programmers, the potential commercial value of such code synthesis models is equally large.

[0007] Machine learning (ML) for code synthesis faces some problems also encountered in ML for language models, such as those used for natural language generation, but also comes with a unique set of reliefs and challenges. For example, ML models for natural language trained via imitation learning on a large data set might be desired to reflect the real use of human language, including idiosyncrasies such as wilful wrong use of some grammar or vocabulary, or use of vernacular. Models trained to synthesize code, on the other hand, may be expected to produce programming language code without errors, as programming languages are typically strict about syntax and grammar, and any deviation will cause bugs in the code, or lead the code to not compile or run in the first place.

[0008] Generally, existing models for code synthesis are not able to exploit execution feedback in a data-driven or imitation learning setting and are trained with execution feedback and critics use large external data- and compute-intensive critic models. Furthermore, there is only limited data available that provides programming language code with associated inputs and expected outputs from which to learn from execution feedback. It is desirable to develop an approach for training code synthesis models that can overcome at least some of the above issues.

[0009] SUMMARY OF THE INVENTION

[0010] According to a first aspect, there is provided an apparatus for training a code synthesis model, the code synthesis model configured to output code based on natural language input, the apparatus comprising one or more processors configured to perform the following steps: receive a code synthesis model; input natural language data to the code synthesis model to form output code corresponding to the natural language data; determine the result of one or more functional tests for the output code or a part thereof; and update the code synthesis model based on the result of the one or more functional tests and feedback from a secondary machine learning model trained simultaneously with the code synthesis model.

[0011] Using the result of functional tests as feedback during the training of the code synthesis model may allow an accurate code synthesis model to be trained. Furthermore, training the secondary machine learning model simultaneously with the code synthesis model may further improve the data and computation efficiency of the training process.

[0012] The one or more processors may be configured to train the secondary machine learning model by updating the secondary machine learning model based on the result of the one or more functional tests. This may allow the secondary machine learning model to be trained to predict the result of the one or more functional tests.

[0013] The code synthesis model may be a policy for reinforcement learning. Using reinforcement learning, small changes to the code synthesis model which may impact the output code may be explored, using the result of the functional tests as the reward, or as part of the overall reward function. The reward may be partially or fully based on the result of the one or more functional tests.

[0014] The one or more processors may be configured to generate at least some of the one or more functional tests. This may help to overcome a traditional limitation of sparsely available data comprising code with corresponding functional tests. Functional tests may be sparse for some representations, such as weakly typed programming languages. Therefore, generating functional tests, such as unit tests, during the training process that can then be used to evaluate the functionality of the output code or a part thereof may result in a more accurate trained code synthesis model.

[0015] The output code may be formed in a target representation. The target representation may be a representation that can be functionally evaluated. The one or more processors may be configured to generate at least some of the one or more functional tests (optionally, all of the one or more functional tests) based on a data set comprising natural language data and corresponding code in a source representation. The source representation may be a representation that can be functionally evaluated. The one or more processors may be configured to generate at least some of the one or more functional tests for the source representation. The one or more processors may be configured to convert the at least some of the one or more functional tests from the source representation to the target representation. This may allow for the generation of functional tests that can provide programming language code with associated inputs and expected outputs from which to learn from execution feedback and alleviate a problem of sparse functional tests for some target representations.

[0016] The one or more processors may be configured to extract one or more code signatures from code in the source representation. The one or more processors may be configured to generate the one or more functional tests for the target representation based on the extracted code signatures and / or the generated functional tests for the source representation. The one or more processors may be configured to convert the one or more code signatures in the source representation to one or more code signatures in the target representation. This may allow unit tests for output code corresponding to a problem description in a source representation, such as a strongly typed programming language, to be converted to unit tests for output code in a target representation, such as a weakly typed programming language, where there may be a sparsity of unit tests available. For example, the generated functional tests may be Jllnit tests in Java, which are converted to Python assert statements. The signature may comprise a definition of function name and / or input type or types and / or output type or types.

[0017] The source representation may be a strongly typed programming language. For example, the source programming language may be Java or C. The target representation may be a weakly typed programming language. For example, the target programming language may be Python. This may allow more widely available unit tests for strongly typed programming languages to be used to generate unit tests for weakly typed programming languages where unit tests may not be readily available. The result of the one or more functional tests may provide an indication of the functionality of the output code or part thereof. This can provide programming language code with associated inputs and expected outputs from which to learn from execution feedback. This may provide a trained model that can produce properly functioning output code.

[0018] The one or more functional tests may be one or more unit tests for the output code (or one or more parts thereof). A unit may be the smallest piece of code that can be logically isolated in a system. This may allow the operation of units of the code to be evaluated. This may allow each part of the code to be isolated and may show that individual parts of the code are correct. At least some of the one or more functional tests may alternatively be used to test the functionality of larger code fragments or whole pieces of code. The result of the tests can be used as feedback to the code synthesis model. In response to this feedback, parameters of the code synthesis model may be updated.

[0019] The secondary machine learning model may model a current actor’s performance in the training of the code synthesis model. In reinforcement learning, an actor is a model that decides the next action to take, based on a policy output. The secondary machine learning model may be a critic model in an actor-critic reinforcement learning framework. The use of a critic model may allow for a more fine-grained or nuanced estimate of the reward than using a reward function / environment alone.

[0020] The secondary machine learning model may be trained simultaneously with the code synthesis model so as to learn to predict the result of the one or more functional tests. The secondary machine model may be trained to predict the result of the one or more unit tests based on the output code from the code synthesis model, or one or more parts of the output code. Training the secondary machine learning model simultaneously with the code synthesis model using the same training data may advantageously mean that model inputs and outputs do not need to be generated twice. Furthermore, as the secondary model can be tied directly to the policy, it may therefore produce more reliable results.

[0021] The output of the one or more functional tests may be used as a reward signal (for example as the reward or part of the reward function) for reinforcement learning for training of the code synthesis model. This may allow the code synthesis model to be iteratively updated based on the reward signal, which may help to improve the accuracy of the resulting code synthesis model once trained. The natural language data may indicate an operation to be performed by the corresponding output code when the corresponding output code is executed. This may improve the coding capabilities of human users, for example by enabling complete novices to start writing code. It may also save experienced programmers time when writing specific parts of a larger code base.

[0022] The received code synthesis model may be a pre-trained code synthesis model. The apparatus may be configured to update the code synthesis model to form an output code synthesis model for use during inference. Pre-training the model may improve the efficiency of the subsequent training process. In other embodiments, the code synthesis model may not be trained and the parameters of the initially received model may be initial parameters that are modified during training.

[0023] According to a second aspect, there is provided a method for training a code synthesis model, the code synthesis model configured to output code based on natural language input, the method comprising: receiving a code synthesis model; inputting natural language data to the code synthesis model to form output code corresponding to the natural language data; determining the result of one or more functional tests for the output code or a part thereof; and updating the code synthesis model based on the result of the one or more functional tests and feedback from a secondary machine learning model trained simultaneously with the code synthesis model.

[0024] In this method, using the result of functional tests as feedback during the training of the code synthesis model may allow an accurate code synthesis model to be trained. Furthermore, training the secondary machine learning model simultaneously with the code synthesis model may further improve the data and computation efficiency of the training process.

[0025] According to a further aspect, there is provided an apparatus for generating one or more functional tests in a target representation, the apparatus comprising one or more processors configured to generate the one or more functional tests based on a data set comprising natural language data and corresponding code in a source representation. The one or more processors may be configured to generate the one or more functional tests for the source representation. The one or more processors may be configured to convert at least some of the one or more functional tests from the source representation to a target representation. The source representation may be different to the target representation. The source representation may be a strongly typed programming language and the target representation may be a weakly typed programming language. In some implementations, the target representation may be a strongly typed programming language. This may allow for the generation of functional tests that can provide programming language code with associated inputs and expected outputs from which to learn from execution feedback and alleviate a problem of sparse functional tests for some target representations.

[0026] According to a further aspect, there is provided a method for generating one or more functional tests in a target representation, the method comprising generating the one or more functional tests based on a data set comprising natural language data and corresponding code in a source representation. The method may comprise generating the one or more functional tests for the source representation. The method may comprise converting at least some of the one or more functional tests from the source representation to the target representation. This method may allow for the generation of functional tests that can provide programming language code with associated inputs and expected outputs from which to learn from execution feedback and alleviate a problem of sparse functional tests for some target representations.

[0027] The above methods may be computer-implemented methods that are implemented on-device (for example, on a smartphone) or externally, such as on cloud-based services.

[0028] According to a further aspect, there is provided a computer program comprising instructions that when executed by a computer cause the computer to perform the methods above.

[0029] According to a further aspect, there is provided a computer-readable storage medium having stored thereon computer readable instructions that when executed at a computer (for example, comprising one or more processors) cause the computer to perform the methods above. The computer-readable storage medium may be a non-transitory computer-readable storage medium. The computer may be implemented as a system of interconnected devices.

[0030] BRIEF DESCRIPTION OF THE FIGURES

[0031] The present invention will now be described by way of example with reference to the accompanying drawings.

[0032] In the drawings:

[0033] Figure 1 schematically illustrates an example of the operation of a code synthesis model. Figure 2 schematically illustrates an exemplary system to which embodiments of the present invention can be applied.

[0034] Figure 3 schematically illustrates an overview of the operation of a framework for training a code synthesis model according to an embodiment of the present invention.

[0035] Figure 4 shows a flow chart illustrating a process for generating functional tests that can be used to evaluate code output by the code synthesis model during training.

[0036] Figure 5 schematically illustrates the conversion of unit tests for code in a strongly typed programming language to unit tests for code in a weakly typed programming language.

[0037] Figure 6 schematically illustrates the conversion of code signatures to generate unit tests that can be used to evaluate the functionality of code output by a code synthesis model during training.

[0038] Figure 7 shows an example of a method of training a code synthesis model.

[0039] Figure 8 shows an example of an apparatus for training a code synthesis model and some of its associated components.

[0040] DETAILED DESCRIPTION

[0041] Embodiments of the present invention relate to the training of code synthesis models. In the exemplary embodiments described herein, the code synthesis model receives a natural language input (for example, words typed or spoken by a user) and outputs a representation that can be functionally evaluated, such as computer code. The output code can be functionally evaluated to provide the answer to a corresponding problem description defined by the natural language input to the code synthesis model.

[0042] As used herein, language modelling is the process of representing language as a probability distribution over words; especially in the context of other words, for example, sentences or phrases, modelling the probability distribution of the next word in causal language modelling, or any hidden word in masked language modelling. A language model (LM) is an amalgamation of computer-based mathematical functions that perform the task of language modelling. A pretrained language model (PLM) is an amalgamation of computer-based mathematical functions that perform the task of language modelling, which have been engineered in such a way to reflect the probability distribution of specific underlying data. Natural language generation (NLG) is the process of generating natural language phrases, sentences, paragraphs etc. by means of a PLM. More specifically, NLG in the setting of embodiments of the present invention is modelled as a structure prediction problem, where the output is formed by a sequence of actions, each corresponding to production of a word.

[0043] Code synthesis is similar to NLG, with the explicit target language being a computer language rather than natural language, i.e., code in a specific programming language. In the context of embodiments of the present invention, code synthesis encompasses the task of presenting to a PLM a problem formulated in natural language, and the objective is to generate code in a representation that can be functionally evaluated (such as computer code) that can solve that problem.

[0044] The embodiments described herein use reinforcement learning (RL) to train the code synthesis model. However, other machine learning techniques can be used that use a learning signal based on expected and actual results.

[0045] RL is ML based on data for which perfect inputs and outputs are not available, and the machine is left to learn from its own outputs, based on an external reward signal. The external reward signal may use negative or smaller numbers to signal a bad output, and positive or larger numbers to signal a good output. In RL, the actor is a model that decides the next action to take, based on a policy output. The policy evaluates a state and provides a distribution over possible action. The state represents the input to the policy. Actions are the outcome of the actor’s decision. Rewards are external signals provided by an environment that determine how good a chosen action fits the state. In RL, the environment is an external model that can evaluate an actor’s chosen action for a given state, and provide a reward signal as well as the follow-up state. In RL, a critic is an external model that tries to approximate the quality (e.g., the environment reward) of an actor’s chosen action for a given state, without having to explicitly query the environment. Proximal Policy Optimization (PPO) and actor-critic RL are specific types of RL.

[0046] Figure 2 shows an example of a system architecture to which embodiments of the present invention can be applied. Computer system 201 may comprise, for example, one or more of a personal computer (PC), a cloud compute server, a mobile device such as a smartphone or any other computing device. Computer system 201 may comprise one or more processors and one or more memories. Computer system 201 can be provided with data D 202 and a ML model M 203 that is to be trained on D via some ML method, leading to an updated model M’ 204. The model M 203 may be iteratively updated and a final model to be used for inference may be output by the computer system 201 when the updated model has reached a predefined level of convergence. At any time, D and / or M and / or M’ might be fully or partially stored on the computer system 201. At any time, D and / or M and / or M’ might be fully or partially accessible by the computer system 201. At any time, D and / or M and / or M’ might be fully or partially inaccessible by the computer system 201. In particular, the final model M’ might be removed from the computer system after training on D, to be deployed in a different production system and / or on a different computer system. The final model M’ may be used for inference.

[0047] Preferably, computer system 201 comprises a computing device or devices suitable for training large neural models. This can be the achieved, for example, by using a desktop machine with sufficient data memory and a graphics card or cards, or dedicated machine learning servers or cloud solutions.

[0048] The trained model M’ 204 resulting from the training process described herein can be deployed as part of services that provide code synthesis solutions (among other tasks) to users.

[0049] In embodiments where the code is output in a programming language, such as Python, the rigidity of programming languages allows for precise assessment of the generated code’s semantics, a task notoriously hard for natural language, by means of functional testing.

[0050] One example of a functional test is a unit test for programming language code, defining inputs and the associated correct outputs. The result of the unit test can provide an indication that units of the code are properly functional. The result of the test for a given piece of the code (or part thereof) output by the code synthesis model may be a pass or fail. The unit test may be a manual or automatic test.

[0051] The ability to evaluate the functional correctness of generated code may allow for deviation from the original code seen during imitation learning, and instead the result of functional tests such as unit tests (i.e. passing or failing) can be used as an indicator of the quality of synthesized code. Small changes to the code can impact these results. In particular, RL can be used to explore such changes, using the outcome of functional tests as the reward, or as part of the overall reward function. Figure 3 schematically illustrates the operation of an exemplary RL framework 300 where the outcome of unit tests is used as part of the reward function for RL.

[0052] The code synthesis model is shown at 301 . The model 301 is a machine learning model. In this example the code synthesis model is a language model that receives a natural language input and outputs code. In Figure 3, the problem description 302 is the natural language input to the model 301 (for example, words in the form of text input by a user, or words spoken by a user that are input to a speech-to-text module). Generally, the natural language data input indicates an operation to be performed by the code output from model 301 when the code is executed. For example, the problem description may be “Write a function that computes the n’th Fibonacci number”, as in the example shown in Figure 1. The model 301 may also receive as input a code signature, which for the exemplary problem description above is def fib(n): . In general, a signature is the definition of function name and / or input type or types and / or output type or types, and can be provided as part of the input to model 301 to allow the model 301 to generate a function with specific name and input arguments. The code solution 303 is the output of the model 301. An exemplary corresponding code solution 303 may be similar to that shown at 103 in Figure 1. In this example, the code synthesis model 301 is a policy for reinforcement learning.

[0053] In one example, a pretrained language model, such as Huawei’s PanGu-Coder code synthesis model, may be used as the initial received code synthesis model. However, the approach is model-agnostic and can be applied to any pre-trained or untrained language model.

[0054] As mentioned above, when using reinforcement learning to train the code synthesis model 301 , the model 301 is a policy for reinforcement learning, which evaluates a state and provides a distribution over possible actions, where an action is the outcome of an actor’s decision. Critic model 304 is a secondary machine learning model that models the current actor’s performance as part of the training process. The critic 304 is an external model (external to model 301) that aims to approximate the quality (e.g. the environment reward) of an actor’s chosen action for a given state, without having to explicitly query the environment. For each iteration of the training process, the critic model can determine a score for the output code 303. The code synthesis model 301 can then be updated based on the score predicted by the critic model 304. The code synthesis model 301 and the secondary critic model 304 are both machine learning models and are trained simultaneously (i.e. at the same time). After each iteration of the training process, respective parameters of the code synthesis model 301 and the secondary machine learning model 304 may be updated accordingly. The training of the code synthesis model 301 may be performed by the same processor that is used to train secondary machine learning model 304 or a different processor within the apparatus. One or more of the processor(s) may be on-device or alternatively (or additionally) in the cloud.

[0055] As shown at 306, functional tests, for example in the form of unit tests, can be used to evaluate the output code solution 303 in the test environment 305. The environment 305 is an external model (external to model 301) that can evaluate an actor’s chosen action for a given state and provide a reward signal, as well as the follow-up state. The reward signal is an external signal (external to model 301) provided by the environment 305 that determines how good a chosen action fits the state. The results of the unit tests 306 are thus used as feedback for the model 301 and the parameters of the model 301 can be updated accordingly based on the results of the unit tests (via the reward signal in a RL framework). The critic model 304 is also updated based on the results of the unit tests 306.

[0056] Therefore, a problem description 302 in natural language form is presented to model 301 as input, for which model 301 produces a solution or a number of solutions 303, in the form of programming language code of a target programming language L.

[0057] The generated code solution or solutions together with corresponding functional tests, such as unit tests 306, are provided to test environment, 305, which may for example be a unit test evaluation server, which assesses whether the generated solution or solutions correctly solve the problem defined by a problem description.

[0058] The evaluation result may be used in one or more of the following two ways.

[0059] Firstly, together with the internal representation of model M corresponding to the generated code i.e., M’s hidden state or states, the evaluation result can be used to train a small critic model 304 on top of (i.e. simultaneously and / or concurrently with) the code synthesis model 301 , which learns to predict the functional test server’s evaluation result of the produced code or codes 303. The critic model 304 can be updated based on the results of the functional tests 306. Secondly, together with the predicted evaluation score of the critic model 304, the evaluation result for the functional tests 306 can be used as part of the reward signal of a reinforcement learning process. The code synthesis model 301 can therefore be updated based on (i) the result of the functional tests 306 and (ii) based on feedback from the critic model 304.

[0060] Examples of reinforcement learning frameworks that may be used for the present approach are REINFORCE-based policy-gradients, as well as proximal policy optimization (PPO). However, the present method is agnostic of the specific RL approach, and furthermore, other types of ML may be employed.

[0061] The code synthesis language model 301 and critic model 304 are therefore updated during training, through the test environment reward. The code synthesis model 301 thus learns to produce better code during the training process.

[0062] The secondary model, which in this example is a critic model 304, can be used to (i) give reward for partial code of output code 303, where the environment 305 may only provide reward for full code; and (ii) give more fine-grained reward prediction where the environment returns the same reward, for example where multiple solutions are correct and pass all functional tests, but one of them might be “better” than the other(s). For example, it could be that two pieces of code pass all unit tests, but one implicitly also handles some edge case that happens to not be covered by the tests, which may instead be picked up by the critic model.

[0063] In embodiments of the present invention, the critic model may be a relatively small machine learning model that is conveniently trained simultaneously with the code synthesis model. Training the secondary machine learning model simultaneously and / or concurrently with the code synthesis model using the same training data may advantageously mean that model inputs and outputs do not need to be generated twice. In one exemplary implementation, the critic model may be a small 2-layer linear network with approximately 500k parameters that is trained on top of a frozen PanGu-Coder model.

[0064] One exemplary problem arising for the application of RL to code synthesis using functional tests is the relative sparseness of functional tests available for ready use. Most code that can be easily used for imitation learning does not specify functional tests such as unit tests, and available datasets with specific unit tests included are very small, in the context of modern-day ML. This may limit what a large language model can learn from them. In some embodiments of the present invention, functional tests may be automatically generated by one or more processors of the computer system. This may be the same processor as that used to train the code synthesis model or a different processor of the apparatus. The processor may be on-device or in the cloud.

[0065] Functional tests such as unit tests can be generated for large amounts of available, functionally correct code, defining a large data set of valid inputs and associated outputs that can then be made available to the RL process. This also allows for the use of code for a given problem originally written for a given programming language to be used to generate functional tests that can evaluate the functionality of code written for the same problem in a different programming language. This task is vastly easier than translating full code between programming languages.

[0066] Figure 4 shows a flowchart for a particular implementation of a method 400 where a software routine S with functional code C in a programming language A is used to generate functional tests for use in the training of the code synthesis model M.

[0067] In particular, in this implementation a software routine S with functional code C in a source programming language A is provided, as shown at 401. The code C may in some implementations be taken from some large dataset, such as dataset D 202 in Figure 2.

[0068] At 402, a separate software routine is applied to code C to generate functional tests T for code C. At 403, it is checked if source programming language A is different from the actual target programming language L that the model M is targeting. If it is different, at 403, the original function signature in the source programming language, for example the definition of function name and / or input type or types and / or output type or types, is converted to a signature compatible with target programming language L. The unit tests can also be adjusted to match the target language input types and / or output types. If language A is the same as language L, conversions need not be applied and the generated unit tests can be used in the training process directly, as generated.

[0069] In addition to or instead of unit tests generated at step 402, the functional test generation procedure can similarly be applied to codes C coming from some dataset that already provides functional tests (such as unit tests) for C in a programming language B, as shown at 404. If the target programming language L is different to programming language B, the functional tests can be converted to tests compatible with programming language L at step 403. In some embodiments, programming language B may be the same as the target programming language L, and so the unit tests obtained at step 404 do not need to be converted to the target programming language at 403.

[0070] After data preparation as described, problem descriptions P in natural language can be provided to a software routine M (i.e. implemented by the code synthesis model) at step 405.

[0071] At step 406, output code C’ is synthesized from natural language input P using model M and the correctness of the output code C’ is evaluated by inputting the output code or one or more parts thereof to the functional tests T. M is therefore used to generate code corresponding to a potential solution or solutions for P, and the solution or solutions can be evaluated with the corresponding functional tests.

[0072] At step 407, based on the results of the functional test(s), the original software routine M can be updated, with the goal of improving its solutions for the problems P.

[0073] In the embodiment described below, an exemplary software routine to generate unit tests for target programming language L as in steps 401 , 402 and 403 of Figure 4 is described.

[0074] In one example, the target programming language L is Python. This is a weakly typed programming language for which only limited data in form of problem descriptions and corresponding source code including unit tests may be available.

[0075] As automatic unit test generation for weakly typed languages is a difficult problem, a strongly typed programming language, such as Java or C, can be used as the source programming language A. For example, for Java, a large dataset containing natural language descriptions and corresponding code is available, and can be used with an existing unit test generation framework, such as EvoSuite (see, for example, www.evosuite.org) to automatically generate Jllnit unit tests for said code.

[0076] After the unit tests are generated for the Java code, a heuristic approach can be used to convert the signature of the targeted Java function into an equivalent signature compatible with the target programming language, Python. A separate heuristic approach can be used to convert the generated Jllnit unit tests into assert statements compatible with Python. The result is a dataset of natural language problem descriptions, Python code signature, and Python-compatible unit tests, that can be used to include execution signal to the training of a pretrained language model for code synthesis using reinforcement learning.

[0077] In one example, using the approach for unit test generation described above on ~815k Java code snippets could infer unit tests for ~200k instances. This is approximately a 500x increase in data available for RL, making the approach more language agnostic, when the function signature and functional tests are converted to the target programming language, with no gold- standard code necessary.

[0078] A diagram of the described process, and an example of a specific output code fragment on which the functional test evaluation procedure is applied, are described below with reference to Figures 5 and 6 respectively.

[0079] In Figure 5 the unit test generation for the strongly typed code is shown generally at 501 and the conversion of the unit tests for the strongly typed code to the weakly typed code is shown generally at 502.

[0080] To generate unit tests for the strongly typed programming language, a data set comprising problems descriptions and corresponding strongly typed code, shown at 503, is input to a unit test generation framework 504, which may be a model such as a neural network for generating unit tests. The output of the unit test generation framework 504 is one or more unit tests for the strongly typed programming language, shown at 505.

[0081] Code signatures are also extracted from the strongly typed code for the strongly typed programming language and converted to the weakly typed programming language at 507.

[0082] At 506, the unit tests for the strongly typed programming language are converted to unit tests for the weakly typed programming language, shown at 508.

[0083] A further illustration of the unit test generation and conversion process is illustrated in Figure 6.

[0084] The original code in the source programming language is shown at 601. This code is used to evaluate the results of the unit tests, once generated. At 602, a code signature has been extracted from the code in the source programming language and translated to the target programming language at 603. The translated signature 603 is used to generate the unit tests 604 in the target programming language, which can then be used to evaluate the output code or parts thereof, with the results of the unit tests illustrated at 605.

[0085] Therefore, generally, one or more functional tests may be generated based on a data set comprising natural language data and corresponding code in a source representation (for example, a source programming language, which may be strongly typed). The functional tests can then be converted from the source representation to the target representation (for example, a target programming language, which may be weakly typed). One or more code signatures can be extracted from code in the source representation and converted to the target representation. The one or more functional tests for the target representation can be generated based on the extracted code signatures and / or the generated functional tests for the source representation.

[0086] The approach described herein uses automatic unit test generation in the overall training pipeline, which may assist in overcoming the limitation of sparsely available data comprising code with corresponding functional tests. The approach described herein uses signature and unit test conversion between source programming language(s) and actual target programming language(s). This can assist in overcoming difficultly of automatic test generation for certain programming languages and overcoming potential sparsity of available code with unit tests of the target programming language. It also uses a small critic model for RL reward modelling, which is trained directly on top and simultaneously with the code synthesis model. This can result in a reduced number of parameters, and thus reduced training complexity of the critic model, compared to using large external critics.

[0087] The described approach can also be applied to other problems where the output code may represent step-by-step solutions to problems or tasks, with observable and verifiable outcomes, often called “scripts” in natural language processing.

[0088] Code is executable and has very strict syntactic and semantic rules, and very minor changes can lead to entirely wrong results. In embodiments of the present invention, the apparatus makes use of the fact that code can easily evaluated with respect to whether it behaves corrected (i.e. it can be tested for functional correctness). Therefore, the model is trained with the help of reinforcement learning based on functional tests. However, as code is available from many sources, such as public code repositories, the available data may not provide suitable functional tests. In particular, for weakly typed or untyped languages such as Python, inferring functional tests such as is very hard. As discussed above, to alleviate this constraint, embodiments of the present invention may automatically generate functional tests for, for example, a statically typed programming language, and convert a function signature and tests into a target programming language. These tests can then be used in the training process to evaluate the functionality of the output code.

[0089] For example, “scripts” apply to how-to scenarios, for example, “how to bake a cake” or “how to build a garden shed”, where achieving the goal includes steps, such as a recipe or building instructions, and the steps if described correctly should lead to the desired outcome.

[0090] In embodiments of the present innovation, the “script” can be interpreted to be the line-by-line instructions given by the programming language code, and the desired outcome can be assessed by functional tests, for example using a test server. In the more general case, any method could be used to assess the outcome of generic generated scripts, such as human feedback, or virtual environments that simulate execution of the generated steps.

[0091] In the implementations described above, the code synthesis model received by the system to be trained is a pretrained language model for code synthesis. However, the received code synthesis model need not be a pretrained language model and may instead have parameters with initial values that have not been optimized by pre-training of the model. However, using a pretrained language model as the received model may result in improved efficiency of the further training process.

[0092] The training of the code synthesis model may be step-wise, with evaluations and / or updates after each generated code token / step, or applied only after generating and evaluating full solutions.

[0093] Figure 7 shows a flowchart illustrating the steps of an exemplary method 700 for training a code synthesis model configured to output code based on natural language input. At step 701 , the method comprises receiving a code synthesis model. At step 702, the method comprises inputting natural language data to the code synthesis model to form output code corresponding to the natural language data. At step 703, the method comprises determining the result of one or more functional tests for the output code or a part thereof. At step 704, the method comprises updating the code synthesis model based on the result of the one or more functional tests and feedback from a secondary machine learning model trained simultaneously with the code synthesis model. Figure 8 shows an example of an apparatus 800 for performing the methods described herein. The apparatus may be implemented as computer system 201 in Figure 2. The apparatus 801 may comprise at least one processor, such as processor 802, and at least one memory, such as memory 803. The memory stores in a non-transient way code that is executable by the processor(s) to implement the apparatus in the manner described herein. The apparatus may be associated with multiple processors and each processor may be used to perform a different function. For example, one processor may be used to train the code synthesis model and another processor may be used to generate functional tests, as described above. One of the processors associated with the apparatus may, for example, be based in the cloud.

[0094] Using functional tests to evaluate the accuracy of the generated code and using this result to update the parameters of the code synthesis model during training is advantageous compared to prior approaches using compiler feedback to assess whether a program does compile, as simply compiling a program does not take into account whether the program actually does what it is supposed to do, i.e., solve a specific problem. The present approach also does not rely on an additional discriminator network, which is costly to train, to distinguish multiple generated solutions.

[0095] Embodiments of the present invention can use techniques such as reinforcement learning to train a language model for code synthesis, with rewards partially or fully based on functional test outcomes (such as the outcome of unit tests). A small, data- and compute-efficient critic model (which may be a neural network) can be trained directly on top of and at the same time as the code synthesis model during the RL process. Compared to approaches relying on large, external critics, which are completely independent of the code synthesis model that is being trained and require relatively expensive training of the external critic, the process can be made much more efficient.

[0096] Furthermore, functional tests, such as unit tests, for code taken from very large code datasets can be generated, and the generated tests can be used for training code synthesis models for arbitrary programming languages, which may be different from the original programming language(s) of said large code dataset.

[0097] Embodiments of the present invention may therefore provide a trainable method that exploits a signal from actively executing code, in addition to demonstrations. The present approach may allow the use of RL to train a model based on whether a generated piece of code successfully (and optionally to what extent) passes functional execution tests. Embodiments of the present invention may also provide a methodology to automatically infer training data (such as problem descriptions, function definitions, and execution tests) from a strongly typed language to use with a RL approach. The present approach also provides a methodology to infer functional tests of a strongly typed language to a non-typed language, to be used for training code synthesis models.

[0098] The applicant hereby discloses in isolation each individual feature described herein and any combination of two or more such features, to the extent that such features or combinations are capable of being carried out based on the present specification as a whole in the light of the common general knowledge of a person skilled in the art, irrespective of whether such features or combinations of features solve any problems disclosed herein, and without limitation to the scope of the claims. The applicant indicates that aspects of the present invention may consist of any such individual feature or combination of features. In view of the foregoing description it will be evident to a person skilled in the art that various modifications may be made within the scope of the invention.

Claims

CLAIMS1. An apparatus (201 , 801) for training a code synthesis model, the code synthesis model configured to output code based on natural language input, the apparatus comprising one or more processors (802) configured to perform the following steps: receive (701) a code synthesis model (203, 301); input (702) natural language data (302) to the code synthesis model (301) to form output code (303) corresponding to the natural language data (302); determine (703) the result of one or more functional tests (306) for the output code (303) or a part thereof; and update (704) the code synthesis model (301) based on the result of the one or more functional tests (306) and feedback from a secondary machine learning model (304) trained simultaneously with the code synthesis model (301).

2. The apparatus (201 , 801) as claimed in claim 1 , wherein the one or more processors (802) are configured to train the secondary machine learning model (304) by updating the secondary machine learning model based on the result of the one or more functional tests (306).

3. The apparatus (201 , 801) as claimed in any preceding claim, wherein the code synthesis model (301) is a policy for reinforcement learning.

4. The apparatus (201 , 801) as claimed in any preceding claim, wherein the one or more processors (802) are configured to generate at least some of the one or more functional tests (306).

5. The apparatus (201 , 801) as claimed in claim 4, wherein the output code (303) is formed in a target representation and wherein the one or more processors (802) are configured to generate at least some of the one or more functional tests based on a data set comprising natural language data and corresponding code in a source representation and convert the at least some of the one or more functional tests from the source representation to the target representation.

6. The apparatus (201 , 801) as claimed in claim 5, wherein the one or more processors (802) are configured to extract one or more code signatures from code in the source representation and generate the one or more functional tests for the target representation based on the extracted code signatures and / or the generated functional tests in the source representation.

7. The apparatus (201 , 801) as claimed in claim 5 or claim 6, wherein the source representation is a strongly typed programming language and wherein the target representation is a weakly typed programming language.

8. The apparatus (201 , 801) as claimed in any preceding claim, wherein the result of the one or more functional tests provides an indication of the functionality of the output code (303) or part thereof.

9. The apparatus (201 , 801) as claimed in any preceding claim, wherein the one or more functional tests (306) are one or more unit tests for the output code (303).

10. The apparatus (201 , 801) as claimed in any preceding claim, wherein the secondary machine learning model (304) models a current actor’s performance in the training of the code synthesis model (301).

11. The apparatus (201 , 801) as claimed in any preceding claim, wherein the secondary machine learning model (304) is trained simultaneously with the code synthesis model (301) so as to learn to predict the result of the one or more functional tests (306).

12. The apparatus (201 , 801) as claimed in any preceding claim, wherein the output of the one or more functional tests (306) are used as a reward signal for reinforcement learning for training of the code synthesis model (301).

13. The apparatus (201 , 801) as claimed in any preceding claim, wherein the natural language data (302) indicates an operation to be performed by the corresponding output code (303) when the corresponding output code is executed.

14. The apparatus (201 , 801) as claimed in any preceding claim, wherein the received code synthesis model (301) is a pre-trained code synthesis model and wherein the apparatus is configured to update the code synthesis model to form an output code synthesis model for use during inference.

15. A method (700) for training a code synthesis model (301), the code synthesis model configured to output code based on natural language input (302), the method comprising: receiving (701) a code synthesis model (301);inputting (702) natural language data (302) to the code synthesis model (301) to form output code (303) corresponding to the natural language data (302); determining (703) the result of one or more functional tests (306) for the output code (303) or a part thereof; and updating (704) the code synthesis model (301) based on the result of the one or more functional tests (306) and feedback from a secondary machine learning model (304) trained simultaneously with the code synthesis model (301).

16. A computer-readable storage medium (803) having stored thereon computer readable instructions that when executed by a computer (201 , 801) cause the computer to perform the method (700) of claim 15.

Citation Information

Cited By

  • Code generation method based on process supervision reinforcement learning

    CN120386514A

  • Large language model rapid reasoning method and system based on Android system deployment

    CN120821515A

  • Systems and methods for generating and executing function calls using machine learning

    US12625732B2

  • Automated generationof models of computation, such as pushdown automata, state machines, or petri nets

    US20250110712A1