IP Library Granted Patent US 12,197,896
Granted Patent B2
US 12,197,896 · App. 18/386,934 · Granted Jan 14, 2025

Code generation with reinforcement learning

Inventors: Miltiadis Allamanis (Cambridge, GB); Daya Guo (Zhuhai, CN); Neelakantan Sundaresan (Bellevue, WA); Alexey Svyatkovskiy (Bellevue, WA)
Assignee: MICROSOFT TECHNOLOGY LICENSING, LLC.
G06F8/33G06F8/36G06N3/045G06N3/08
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 12,197,896
App. No.
18/386,934
Granted
Jan 14, 2025
Kind
B2
Abstract

A code generation system uses a non-terminal expansion model and a non-terminal selector model to generate a code sketch to complete a partially-formed source code snippet. The non-terminal expansion model is a neural transformer model trained on a supervised dataset through reinforcement learning to learn to predict the production rule to expand for a given non-terminal symbol. The non-terminal selector model is trained through reinforcement learning to predict the non-terminal symbol to expand given a partial-code state. The models are used in a two-step beam search to generate the top candidate code sketches, where a candidate code sketch may contain a hole that represents an unexpanded non-terminal symbol.

Claims (44)

1. A system for grammar-based code generation comprising:

a processor and a memory;

wherein the memory includes executable instructions that when executed by the processor perform actions that:

obtain a partially-formed source code snippet of a source code program associated with a programming language, wherein the programming language is associated with a grammar comprising a plurality of production rules, a set of non-terminal symbols, and a set of terminal symbols, wherein a production rule replaces a non-terminal symbol with one or more terminal symbols or one or more non-terminal symbols;

transform the partially-formed source code snippet into a partial code state comprising at least one non-terminal symbol; and

generate a code sketch to complete the partially-formed source code snippet, wherein the code sketch comprises an output sequence of terminal symbols and at least one hole,

wherein the generation of the code sketch uses a non-terminal selector model to predict a select non-terminal symbol in the partial code state to expand and a non-terminal expansion model to predict a select production rule to expand the select non-terminal symbol, and

wherein the hole represents a position in the output sequence where the non-terminal selector model is uncertain about which non-terminal symbol to expand.

2. The system of claim 1 , wherein the memory includes executable instructions that when executed by the processor perform actions that:

output the code sketch in the source code program.

3. The system of claim 1 , wherein the memory includes executable instructions that when executed by the processor perform actions that:

execute a first beam search that uses the non-terminal selector model to predict the select non-terminal symbol to expand; and

execute a second beam search that uses the non-terminal expansion model to predict the select production rule to expand the select non-terminal symbol.

4. The system of claim 1 , wherein the code sketch generates source code to complete a line of source code, a method body, a class, an expression, or a consecutive set of lines of source code.

5. The system of claim 1 , wherein the non-terminal selector model is a multilayer perceptron model trained through reinforcement learning.

6. The system of claim 1 , wherein the non-terminal expansion model is an encoder-decoder neural transformer model with attention trained through reinforcement learning.

7. A computer-implemented method for grammar-based code generation, comprising:

accessing a partially-formed source code snippet of a source code program associated with a programming language, wherein the programming language is associated with a grammar comprising a plurality of production rules, a plurality of non-terminal symbols, and a plurality of terminal symbols, wherein a production rule is a rule that replaces a first non-terminal symbol with one or more terminal symbols or one or more non-terminal symbols;

transforming the partially-formed source code snippet into a partial code state comprising at least one non-terminal symbol;

generating a code sketch to complete the partially-formed source code snippet, wherein the code sketch comprises a sequence of terminal symbols and a hole, wherein generating the code sketch comprises invoking a non-terminal selector model to predict a non-terminal symbol to expand in the partial code state and a non-terminal expansion model to predict the production rule that expands the predicted non-terminal symbol,

wherein the hole represents an unexpanded non-terminal symbol.

8. The computer-implemented method of claim 7 , further comprising:

outputting the code sketch in the source code program.

9. The computer-implemented method of claim 7 , further comprising:

executing a first beam search that invokes the non-terminal selector model to predict top-m non-terminal symbols to expand; and

for each of the top-m predicted non-terminal symbols, executing a second beam search that invokes the non-terminal expansion model to predict top-n expansions of each of the top-m non-terminal symbols.

10. The computer-implemented method of claim 9 , wherein the non-terminal selector model generates an output probability for each non-terminal symbol in the plurality of non-terminal symbols.

11. The computer-implemented method of claim 10 , wherein the top-m predicted non-terminal symbols have highest output probabilities.

12. The computer-implemented method of claim 9 , wherein the non-terminal expansion model generates an output probability for each production rule.

13. The computer-implemented method of claim 12 , wherein the top-n expansions have highest output probabilities.

14. The computer-implemented method of claim 9 , further comprising:

expanding each of the predicted top-m non-terminal symbols until a special stop symbol is predicted.

15. The computer-implemented method of claim 9 , further comprising:

terminating expansion of a select one of the predicted top-m non-terminal symbols when the non-terminal expansion model predicts a hole to replace a non-terminal symbol.

16. A hardware storage device having stored thereon computer executable instructions that are structured to be executable by a processor of a computing device to thereby cause the computing device to perform actions for grammar-based code completion that:

obtain a partially-formed source code snippet of a source code program in a programming language associated with a grammar comprising a plurality of production rules, a set of non-terminal symbols, and a set of terminal symbols, wherein a production rule is a rule that replaces a non-terminal symbol with one or more terminal symbols or one or more non-terminal symbols;

transform the partially-formed source code snippet into a partial code state comprising at least one non-terminal symbol and a squash non-terminal symbol, wherein the squash non-terminal symbol represents a position where a code sketch is to be inserted into the partially-formed source code snippet; and

generate a code sketch to insert into the position of the squash non-terminal symbol, wherein the code sketch comprises a sequence of terminal symbols and a hole, wherein the generation of the code sketch comprises invoking a non-terminal selector model to predict a non-terminal symbol in the partial code state to expand and a non-terminal expansion model to predict the production rule that expands the predicted non-terminal symbol,

wherein the hole represents an unexpanded non-terminal symbol.

17. The hardware device of claim 16 , wherein the partial code state comprises a first sequence of tokens, the squash non-terminal symbol, and a second sequence of tokens, wherein the first sequence of tokens represents source code preceding the code sketch, wherein the second sequence of tokens represents source code following the code sketch.

18. The hardware device of claim 16 , having stored thereon computer executable instructions that are structured to be executable by the processor of the computing device to thereby cause the computing device to perform actions for grammar-based code completion that:

output the code sketch in the source code program.

19. The hardware device of claim 16 , wherein the non-terminal selector model is a multi-layer perceptron model.

20. The hardware device of claim 16 , wherein the non-terminal expansion model is a neural transformer model with attention.

Assignments (1)
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Nov 3, 2023
From: ALLAMANIS, MILTIADIS; GUO, DAYA; SUNDARESAN, NEELAKANTAN; SVYATKOVSKIY, ALEXEY
To: MICROSOFT TECHNOLOGY LICENSING, LLC.
Reel/Frame 065457/0274 →
Continuity (3)
Continuation 17403583 · Aug 16, 2021
Provisional Application 63210971 · Jun 15, 2021
Related Publication 20240061655A1 · Feb 22, 2024
References Cited (19)
US 11340898B1 · Sabharwal · 2022 [cited by examiner]
US 11442702B2 · Smith · 2022 [cited by examiner]
US 11861333B2 · Tabachnyk · 2024 [cited by applicant]
US 20180349106A1 · Makkar · 2018 [cited by applicant]
US 20190205763A1 · Hirano · 2019 [cited by examiner]
US 20190228319A1 · Gupta · 2019 [cited by examiner]
US 20190303107A1 · Kelly · 2019 [cited by applicant]
US 20190391792A1 · Sabharwal · 2019 [cited by examiner]
US 20200097261A1 · Smith · 2020 [cited by examiner]
US 20210026605A1 · Evangelopoulos · 2021 [cited by examiner]
US 20210034335A1 · Svyatkovskiy · 2021 [cited by examiner]
US 20210349696A1 · Nupponen · 2021 [cited by applicant]
US 20220398071A1 · Allamanis et al. · 2022 [cited by applicant]
US 20230393817A1 · Johnson · 2023 [cited by applicant]
Chakraborty et al., “CODIT: Code Editing with Tree-Based Neural Models,” IEEE, 2019, 14pg. (Year: 2019). [cited by examiner]
Le et al., “Deep Learning for Source Code Modeling and Generation: Models, Applications and Challenges,” ACM, 2020, 37pg. ( Year: 2020). [cited by examiner]
Hussain, et al., Improving Source Code Suggestion with Code Embedding and Enhanced Convolutional Long Short-Term Memory, IET, Software, Mar. 2021, pp. 1-15. [cited by applicant]
Non-Final Office Action mailed on Apr. 11, 2024, in U.S. Appl. No. 17/842,725, 20 pages. [cited by applicant]
Notice of Allowance mailed on Aug. 13, 2024, in U.S. Appl. No. 17/842,725, 8 pages. [cited by applicant]