E XECUTION -G UIDED N EURAL P ROGRAM S YNTHESIS
Dawn Song
A BSTRACT
Neural program synthesis from input output examples has attracted an increasing interest from both the machine learning and the programming language community
Most existing neural program synthesis approaches employ an encoder decoder architecture which uses an encoder to compute the embedding of the given input output examples as well as a decoder to generate the program from the embedding following a given syntax.
Although such approaches achieve a reasonable performance on simple tasks such as FlashFill on more complex tasks such as Karel the state -of -the-art approach can only achieve an accuracy of around 77%.
We observe that the main drawback of existing approaches is that the semantic information is greatly under-utilized.
In this work , we propose two simple yet principled tech - niques to better leverage the semantic information which are execution -guided synthesis and synthesizer ensemble .
These techniques are general enough to be combined with any existing encoder-decoder-style neural program synthesizer.
Applying our techniques to the Karel dataset, we can boost the accuracy from around % to more than 90%
I NTRODUCTION
Program synthesis is a traditional challenging problem.
Such a problem typically takes a speciﬁcation as the input, and the goal is to generate a program within a target domain-speciﬁc language (DSL).
One of the most interesting forms of the speciﬁcations is input-output examples, and there have been several applications, such as FlashFill (Gulwani, 2011; Gulwani et al., 2012).
Recently, there is an increasing interest of applying neural network approaches to tackle the program synthesis problem.
For example, Devlin et al.
have demonstrated that using an encoder-decoderstyle neural network, their neural program synthesis algorithm called RobustFill can outperform the performance of the traditional non-neural program synthesis approach by a large margin on the
FlashFill task (Devlin et al., 2017b).
Despite their promising performance, we identify several inefﬁciencies of such encoder-decoder-style neural program synthesis approaches.
In particular, such a neural network considers program synthesis as a sequence generation problem; although some recent work take the syntactical information into consideration during program generation (Bunel et al., 2018;
Rabinovich et al., 2017; Yin & Neubig, 2017; Parisotto et al., 2017; Xu et al., 2017), the semantic information, which is typically well-deﬁned in the target DSL, is not effectively leveraged by existing work.
In light of this observation, in this work, we develop simple yet principled techniques that can be combined with any existing encoder-decoder-style neural program synthesizers.
The main novel technique is called execution-guided synthesis .
The basic idea is to view the program execution as a sequence of manipulations to transform each input state into the corresponding output state.
In such a view, executing a partial program can result in intermediate states; thus, synthesizing the rest of the program can be conditioned on these intermediate states, so that the synthesizer can take the state changes into account in the followup program generation process.
Therefore, we can leverage this idea to combine with any existing encoder-decoder-style neural synthesizer, and we observe that it can signiﬁcantly improve the performance of the underlying synthesizers.
In addition, we also propose a simple yet effective technique called synthesizer ensemble , which leverages the semantic information to ensemble multiple neural program synthesizers.
In particular, for the input-output program synthesis problem, we can easily verify if a synthesized program satisﬁes the input-output speciﬁcation, which allows us to remove invalid predictions from the ensemble during inference time.
Albeit its simplicity, to the best of our knowledge, we are not aware of any previous neural program synthesis work applying this approach.
We observe that this technique further boosts the performance substantially.
We evaluate our techniques on the Karel task (Bunel et al., 2018; Devlin et al., 2017a), the largest publicly available benchmark for input-output program synthesis, on which the most performant model in the past can achieve only an accuracy of around 77% (Bunel et al., 2018).
We observe that our proposed techniques can gain better performance than the previous state-of-the-art results.
In particular, by combining both of our techniques, we can achieve an accuracy of 92% , which is around 15 percentage points better than the state-of-the-art results.
This shows that our approach is effective in boosting the performance of algorithms for neural program synthesis from input-output examples.
N EURAL P ROGRAM S YNTHESIS F ROM I NPUT -O UPUT E XAMPLES
In this section, we ﬁrst introduce the input-output program synthesis problem and existing encoderdecoder-style neural program synthesis approaches, then present an overview of our approaches.
2. P ROBLEM D EFINITION
We follow the literature (Devlin et al., 2017b; Bunel et al., 2018; Chen et al., 2018) to formally deﬁne the input-output program synthesis problem below.
Problem Deﬁnition 1 (Program emulation) Let L be the space of all valid programs in the domainspeciﬁc language (DSL).
Given a set of input-output pairs { ( I k , O k ) } Kk =1 (or { IO K } in short), where there exists a program P ∈ L , such that P ( I k ) =
O k , ∀ k ∈ { 1 , ..., K } .
Our goal is to compute the output O test for a new test input I test , so that O test =
P ( I test ) .
Although the problem deﬁnition only requires to compute the output for a test input, a typical method is to synthesize a program P (cid:48)
∈
L such that P (cid:48) is consistent with all input-output pairs { IO K } , and then use P (cid:48) to compute the output.
In this case, we say program P (cid:48) emulates the program P corresponding to { IO K } .
In particular, in this work, we are mainly interested in the following formulation of the problem.
Problem Deﬁnition 2 (Program synthesis) Let L be the space of all valid programs in the domainspeciﬁc language (DSL).
Given a training dataset of { IO K } i for i = 1 , ..., N , where N is the size of the training data, compute a synthesizer Γ , so that given a test input-output example set { IO K } test , the synthesizer Γ( { IO K } test ) =
P produces a program P , which emulates the program corresponding to { IO K } test .
2. E NCODER - DECODER - STYLE N EURAL P ROGRAM S YNTHESIS A PPROACHES
There have been many approaches proposed for different neural program synthesis tasks, and most of them follow an encoder-decoder-style neural network architecture (Bunel et al., 2018;
Devlin et al., 2017b; Parisotto et al., 2017).
Figure 1 shows a general neural network architecture for input-output program synthesis.
First, an encoder converts input-output examples { IO K } into an embedding.
For example, RobustFill (Devlin et al., 2017b) deals with the string transformation tasks, thus it uses LSTMs as the encoder.
For the Karel task, both inputs and outputs are 2D grids (see Figure 2); therefore, Bunel et al.
(2018) employ a CNN as the encoder.
Once the IO embeddings are computed, these approaches employ an LSTM decoder to generate the programs conditioned on the embeddings.
For program synthesis, one unique property is that the generated program should satisfy the synthax of L .
Therefore, a commonly used reﬁnement is to ﬁlter syntactically invalid program preﬁxes during generation (Parisotto et al., 2017; Devlin et al., 2017b; Bunel et al., 2018).
In the above approaches, only the syntax information is leveraged; the semantics of L is not utilized.
In particular, standard supervised training procedure could suffer from program aliasing : for the same input-output examples, there are multiple semantically equivalent programs, but all except the one provided in the training data will be penalized as wrong programs.
To mitigate this issue, Bunel et al.
propose to train with reinforcement learning, so that it rewards all semantically correct programs once they are fully generated (Bunel et al., 2018).
In our work, we demonstrate that we can leverage the semantic information in an effective way that provides a better performance.
2. A N O VERVIEW OF OUR A PPROACHES
In this work, we propose two general and principled techniques that can improve the performance over existing work, which are execution-guided synthesis (Section 3) and synthesizer ensemble (Section 4).
The main idea of our techniques is to better leverage the semantics of the language L during synthesis.
Meanwhile, our techniques are compatible with any existing encoder-decoder-style neural program synthesis architecture.
We will describe these techniques in detail in the following sections.
E XECUTION -G UIDED S YNTHESIS
Existing approaches generate the entire program only based on the input-output examples before execution.
However, this is an inefﬁcient use of the semantics of L .
For example, when a program consists of a sequence of statements, we can view the output to be a result by continuously executing each statement in the sequence to convert the input state into a sequence of intermediate states.
Figure 2 illustrates such an example.
From this perspective, instead of generating the whole program at once, we can generate one statement at a time based on the intermediate/output state pairs.
However, most interesting programs are not just sequential.
In this work, we explore this idea using a general control-ﬂow framework.
In particular, given any language L , we extend it with three classical types of control-ﬂow: sequential, branching, and looping.
The extended language is call L ext .
Then, we develop our above idea based on L ext , called execution-guided synthesis .
In the following, to make our discussion concise, we ﬁrst formalize L ext (Section 3.1), and then present the idea of execution-guided synthesis (Section 3.2).
[Table]
[Table]
3. T HE F ORMAL S PECIFICATION OF THE E XTENDED L ANGUAGE L ext
P := B ; ⊥
B := ⊥ | S | B ; B | if [Table] then [Table]
else B ﬁ | while [Table] do [Table]
S , C ∈
L
In this work
[Table]
we assume some additional control-ﬂow syntax on top of L .
We deﬁne the extended language L ext in Table 1.
In particular, we assume that a code block
B can be composed by a sequence of statements S ∈ L or sub code blocks, and each code block can also be an if-statement or a while-statement.
We use C to indicate a condition expression in L , and ⊥ to indicate the termination of a program execution.
The semantics of L ext is speciﬁed in Table 2.
These rules are largely standard following the convention in programming language literature.
In particular, each rule’s name starts with S- indicating that they are semantics rules; the sufﬁxes indicate the constructors each rule speciﬁes, (e.g., Stmt for statements, Seq for sequences, etc.).
The judgment (cid:104) B, s (cid:105) → (cid:104) B (cid:48) , s (cid:48) (cid:105) indicates a small-step execution of program B over state s to result in a new program B (cid:48) and a new state s (cid:48) .
The judgments (cid:104)S , s (cid:105) ⇓ s (cid:48) and (cid:104) C, s (cid:105) ⇓ b capture the big-step execution in L that statement
S evaluates to a new state s (cid:48) from s , and condition C evaluates to a boolean value b from s .
Following the semantics of L ext , we can formally deﬁne a program execution.
Deﬁnition 1 (Program execution)
Given a program P ∈ L ext and an input I , the execution is a sequence s 0 ...
s T , such that (1) s
= I ; (2) B 0 = P ; (3) (cid:104) B i , s i (cid:105) →
(cid:104) B i +1 ,
s i +1 (cid:105) for i = 0 , ..., T − 1 ; and (4) B T = ⊥ .
The output of the program is O = s T .
3. E XECUTION -G UIDED S YNTHESIS A LGORITHM
In Deﬁnition 1, we can observe that the initial and ﬁnal states are simply two special states provided as the input-output examples of the synthesis problem.
Thus, a synthesizer Γ for input-output pairs should also be able to take any state-pairs as inputs.
Our execution-guided synthesis algorithm takes advantage of this fact to improve the performance of the synthesizer.
In the following, we discuss three cases from the easiest to the hardest to present our approach.
Sequential programs.
We now consider the simplest case, where the program is in the form of
S 1 ; ... ; S T , to illustrate the basic idea of execution-guided synthesis algorithm.
We present the algorithm in Algorithm 1.
Assuming the input-output examples are { IO K } , we can treat them as K state-pairs { ( s k i , s k o ) } Kk =1 , where s k i
=
I
k , s k
o = O k .
The Exec algorithm takes the synthesizer Γ and the input-output pairs { IO K } as its input.
It also takes an additional input ∆ , which is the ending token to be synthesized.
For the top-level program, ∆ will be the ⊥ token.
Later we will see that when synthesizing the sub-program for If- and While-blocks, different ending tokens will be used.
The synthesized program is initially empty (line 3).
Then the algorithm iteratively generates one statement S at a time (line 4-10 and 17), and appends it to the end of P (line 16).
Importantly, if S is not an if-statement or a while-statement, for which we handle separately, the algorithm executes the newly generated statement S to transit s k
i into s k new
(line 11-14).
Therefore, in the subsequent iteration, the synthesizer can start from the new states s k new after executing the partial program P
1: function E XEC ( Γ , { ( s k i , s k o ) }
Kk =1 , ∆ ) 2: //
The main algorithm is called using Exec ( Γ , { IO K } , ⊥ ) 3:
P ← ⊥ 4:
S ← Γ( { ( s k i , s k o ) }
Kk =1 ) 5: while S (cid:54) =
∆ do 6: if S = if -token then // If-statement synthesis 7:
S , { ( s k i , s k o ) } Kk =1 ← ExecIf (Γ , { ( s k i , s k o ) }
Kk =1 ) 8: else 9: if S = while -token
then // While-statement synthesis 10:
S , { ( s k i , s k o ) }
Kk =1 ← ExecWhile (Γ , { ( s k i , s k o ) }
Kk =1 ) 11: else // Execution of S 12: (cid:104)S , s k
i (cid:105) → (cid:104)⊥ , s k new (cid:105) for k = 1 , ..., K 13: s k i ← s k new for k = 1 , ..., K 14: end if 15: end if 16:
P ← P ; S 17:
S ← Γ( { ( s k i , s k o ) } K k =1 ) 18: end while 19: return P 20: end function
1: function E XEC
I F ( Γ , I ) 2:
C ← Γ( I ) 3:
I t ← { ( s i , s o ) ∈ I|(cid:104)C , s i (cid:105) ⇓ true } 4:
I f ← { ( s i , s o ) ∈ I|(cid:104)C , s i (cid:105) ⇓ false } 5:
B t ← Exec (Γ , I t , else -token ) 6:
B f ← Exec (Γ , I f , ﬁ -token ) 7:
I (cid:48)
t ← { ( s new , s o ) | ( s i , s o ) ∈
I t ∧ (cid:104) B t , s i (cid:105) ⇓ s new } 8:
I (cid:48)
f ← { ( s new , s o ) | ( s i , s o ) ∈
I f ∧ (cid:104) B f , s i (cid:105) ⇓ s new } 9:
I ← I (cid:48)
t ∪ I (cid:48)
f 10:
S ← if C then B t else
B f ﬁ 11: return S , I 12: end function generated so far.
In doing so, the synthesizer can see all intermediate states to better adjust the followup synthesis strategies to improve the overall synthesis performance.
Branching programs.
Dealing with if-statements is slightly more complicated than sequential programs, since in an if-statement, not all statements will be executed on all inputs.
Following the execution in Algorithm 1 naively, we have to use Γ to synthesize the entire if-statement before being able to execute the partially generated program to derive intermediate states.
Therefore, in Algorithm 2, we extend the above idea to handle if-statements.
When the next predicted token is an if -token, our execution-guided synthesizer ﬁrst predicts the condition of the if-statement
C (line 2).
Then, we evaluate C over all state-pairs.
Based on the evaluation results, we can divide the IO pairs into two sets I t and I f (line 3-4), so that all states in the former meet the branching condition to go to the true branch, and all states in the latter go to the false branch.
Therefore, in the followup synthesis, we do not need to consider I f (or I t ) when synthesizing the true branch (or the false branch) (line 5-6).
Note that in line 5-6, synthesizing both true-branch and false-branch employ execution-guided synthesis algorithm to leverage intermediate states, and different ending tokens (i.e., else and ﬁ ) are supplied respectively.
Once we have done the synthesis of both branches, we can execute the generated branches
In Algorithm 2, we use (cid:104) B, s (cid:105) ⇓ s (cid:48) to indicate a big-step execution of code block B over state s to get s (cid:48) .
In particular, this means that (cid:104) B, s (cid:105) → (cid:104) B 1 , s 1 (cid:105) → ... → (cid:104)⊥ , s (cid:48) (cid:105) .
Looping programs.
The remaining problem is to handle while-statements.
Due to the rule S-While (see Table 2), a while-statement while C do B end (1) is equivalent to if C then ( B ; while C do B end ) else ⊥ ﬁ (2)
Therefore, we can employ a procedure similar to Algorithm 2 once a while -token is predicted.
However, there are two differences.
First, in (2), the false-branch is empty, thus we do not need to deal with the false-branch.
Second, although the true-branch is B ; while C do B end , once we have generated B , we do not need to generate the rest of the true-branch, since both C and B have been generated.
The detailed algorithm can be found in Appendix B.
Remarks.
The ﬁnal algorithm is called by Exec ( Γ , { IO K } , ⊥ ).
Note that our execution-guided synthesis algorithm can be applied to any neural synthesizer Γ , and we can train the synthesizer Γ using any supervised or reinforcement learning algorithms that have been proposed before (Devlin et al., 2017b; Bunel et al., 2018).
In our evaluation, we demonstrate that our execution-guided synthesis technique helps boost the performance of both supervised and reinforcement learning algorithms proposed in existing work (Bunel et al., 2018).
S YNTHESIZER E NSEMBLE
In our experiments, we observe that when we use different random initializations of the synthesizer during training, even if the synthesizer architectures are the same, they will be effective on different subsets of the dataset, although the overall prediction accuracy is similar to each other.
Thus, a natural idea is to train multiple synthesizers, and ensemble them to build a more performant synthesizer.
Different from other deep learning tasks, for program synthesis task, without knowing the ground truth, we can already ﬁlter out those wrong predictions that cannot satisfy the input-output speciﬁcation.
Thus, we ensemble multiple synthesizers as follows: we run all synthesizers to obtain multiple programs, and select from programs that are consistent with all input-output examples.
This provides us with a better chance to select the correct ﬁnal prediction that generalizes to held-out IO pairs.
The main subtlety of such an approach is to deal with the case when multiple generated programs satisfy the input-output examples.
In this work, we consider several alternatives as follows:
• Majority vote.
We can choose the most frequently predicted program as the ﬁnal prediction.
• Shortest.
Following the Occam’s razor principle, we can choose the shortest program as the ﬁnal prediction.
E VALUATION
In this section, we demonstrate the effectiveness of our approaches on the Karel dataset (Pattis, 1981;
Bunel et al., 2018).
We ﬁrst introduce the task, discuss the experimental details, and present the results.
5. T HE K AREL T ASK
Karel is an educational programming language proposed in the 1980s (Pattis, 1981).
Using this language, we can control a robot to move inside a 2D grid world and modify the world state, and our goal is to synthesize a program given a small number of input and output grids as the speciﬁcation.
Such tasks have been used in Stanford CS introductory courses (CS106A, 2018) and the Hour of
Code (HoC, 2018), and have been studied recently in several neural program synthesis works (Devlin et al., 2017a; Bunel et al., 2018; Shin et al., 2018).
Figure 2 shows an example in the Karel domain.
We provide the grammar speciﬁcation and the state representation in Appendix A.
In particular, the
[Table]
Karel DSL includes control ﬂow constructs such as conditionals and loops, which is more challenging than problems well-studied before, such as FlashFill (Gulwani, 2011; Devlin et al., 2017b).
Our evaluation follows the setup in (Bunel et al., 2018).
We train and evaluate our approaches on their dataset, which is built by randomly sampling programs from the DSL.
For each program,
IO pairs serve as the speciﬁcation, and the sixth one is the held-out test sample.
In total, there are 1,116,854 programs for training, 2,500 in the validation set, and 2,500 in the test set.
We evaluate the following two metrics, which are the same as in (Bunel et al., 2018):
• Exact Match.
The predicted program is an exact match if it is the same as the ground truth.
• Generalization.
The predicted program is a generalization if it satisﬁes the input-output examples in both the speciﬁcation and the held-out examples.
Training dataset construction for the Exec algorithm.
Note that the original Karel dataset only provides the input-output examples and the ground truth programs.
To train the synthesizer Γ with our Exec algorithm in the supervised learning setting, we need the supervision on intermediate states as well, which can be obtained easily by executing the ground truth programs following the semantics (Table 2).
In particular, for each sample (cid:104){ IO K } , P (cid:105) in the original training set, we construct a sequence of training samples (cid:104){
( s ki − 1 , O k ) } K i k
=1 , S i (cid:105) ( i = 1 , 2 , ..., T ), with each sample containing
K i ≤ K state pairs and a program S i ∈ P .
The algorithm to construct this set is largely analogous to the semantics speciﬁcation, and we defer the details to Appendix B.
Training algorithms.
Once the training set is constructed, the neural synthesizer Γ can be trained on this new dataset using the same algorithm as the one for training Γ on the original dataset.
Therefore, our Exec algorithm can be applied to both supervised learning and reinforcement learning algorithms proposed in (Bunel et al., 2018) for evaluation.
Model details.
We employ the same neural network architecture as in (Bunel et al., 2018) to synthesize the programs, which is brieﬂy discussed in Section 2.2.
During the inference time, we set the beam size B = 64 , and select the one with the highest prediction probability from the remaining programs.
More details can be found in Appendix C.
5. R ESULTS
We present our main results in Table 3.
We report the results of ensembling 15 models for our ensemble techniques.
For reference, we include MLE and RL results in (Bunel et al., 2018), which were the state-of-the-art on the Karel task for the exact match and generalization
We ﬁrst apply our ensemble techniques to these approaches, and observe that the performance could be signiﬁcantly boosted by around 7% .
We next observe that our execution-guided synthesis alone can signiﬁcantly improve the generalization accuracy over all approaches from (Bunel et al., 2018), even after we accompany their approaches with our ensemble techniques.
In particular, without the ensemble, “Exec+SL” already improves “MLE+RL” by 8 points on generalization accuracy; and when ensemble approaches are applied to “MLE+RL”, this gap is shrunk, but still positive.
Similar to (Bunel et al., 2018), we can also train our
Exec model using the RL technique, which improves the generalization accuracy by another 1 point, while slightly decreases the exact match accuracy.
These results show that utilizing intermediate execution states alone is already an effective approach to boost the performance.
Note that the improvement of Exec on the exact match accuracy is relatively minor, and sometimes negative when applying the ensemble to baseline training algorithms.
This is because our Exec algorithm is not designed to optimize for exact match accuracy.
In particular, we decouple the full programs in the original training dataset into small pieces, thus our synthesizer Γ is trained with segments of the original training programs instead of the full programs.
In doing so, our synthesizer is more capable of generating programs piece-by-piece and thus tends to generate semantically correct programs (i.e., with a better generalization accuracy) rather than the same programs in the training and testing sets (i.e., with a better exact match accuracy).
In fact, for real-world applications, the generalization accuracy is more important than the exact match accuracy, because exact match accuracy is more about evaluating how well the synthesizer recovers the language model of the pCFG sampler used to generate the dataset.
More discussion can be found in Appendix D.1.
Finally, we apply our ensemble approaches on top of Exec+SL and Exec+RL.
We observe that this can further improve the generalization accuracy and exact match accuracy by around 6% on top of the best single model.
These results show that our ensemble approaches consistently boost the performance, regardless of the underlying models used for ensembling.
In addition, we investigate the performance of ensembling different number of models.
We present the results of ensembling Exec + RL models in Figure 3, and defer the results of ensembling Exec models to Appendix D.2.
We observe that using the shortest principle is generally more effective than using the majority vote principle, especially when fewer number of models are included in the ensemble.
However, when there are more models, majority vote may achieve a better generalization accuracy than the shortest principle.
This is reasonable, since when there are too few models, there might not be enough effective models to form the majority.
Interestingly, we observe that Exec+RL+Ensemble does not signiﬁcantly improve the performance over Exec+SL+Ensemble.
This may be due to that the improvement from ensemble hides the improvement from RL.
More discussion of our evaluation results can be found in Appendix D.
To summarize, we make the following key observations:
Our execution-guided synthesis technique can effectively improve previous approaches, which only use the syntactic information, or the ﬁnal program execution outputs.
Our ensemble approaches can effectively improve the performance regardless of the underlying models being used.
The different modules of our proposed approaches, i
e., execution-guided synthesis and ensemble techniques, can work independently to improve the performance, and thus they can be applied independently to other tasks as well.
By combining all our novel techniques, we improve the state-of-the-art on the Karel task by 14 . 88 points (generalization) and 7 .
14 points (exact match).
R ELATED W ORK
Synthesizing a program from input-output examples is an important challenging problem with many applications (Devlin et al., 2017b; Gulwani et al., 2012; Gulwani, 2011; Bunel et al., 2018;
Chen et al., 2018;
Cai et al., 2017; Li et al., 2017; Reed & De Freitas, 2016; Zaremba et al., 2016; Zaremba &
Sutskever, 2015; Fox et al., 2018; Xiao et al., 2018; Ganin et al., 2018).
There has been an emerging interest in studying neural networks for program synthesis.
A line of work studies training a neural network to directly generate the outputs given the inputs (Devlin et al., 2017b;a; Graves et al., 2014;
Joulin & Mikolov, 2015; Kaiser & Sutskever, 2015).
In particular, Devlin et al.
study the Karel domain (Devlin et al., 2017a).
However, as shown in (Devlin et al., 2017a), this approach is incapable of handling the case when the number of input-output examples is small, and is hard to generalize to unseen inputs.
Recent work study using neural networks to generate programs in a domain-speciﬁc language (DSL) from a few input-output examples (Devlin et al., 2017b; Bunel et al., 2018; Parisotto et al., 2017;
Polosukhin & Skidanov, 2018).
Several work synthesize programs for FlashFill tasks, which are in the string transformation domain (Devlin et al., 2017b; Parisotto et al., 2017).
Other work synthesize programs in a LISP-style DSL for array manipulation (Polosukhin & Skidanov, 2018).
However, these DSLs only include sequential programs, and do not support more complex control ﬂows such as loops and conditionals in our studied Karel problem.
Prior works also consider incorporating syntax constraints and information from program execution to facilitate program synthesis (Devlin et al., 2017b;
Wang et al., 2018a; Bunel et al., 2018).
However, all these works generate the whole program, and use its execution results to guide the synthesis process; in contrast, our work leverages more ﬁne-grained yet generic semantic information that can be gathered during executing programs in most imperative languages.
As a result, our approach’s performance is signiﬁcantly better than previous work (Bunel et al., 2018).
Previous work also study program synthesis given intermediate states.
For example, Sun et al.
(2018) propose to synthesize the program from demonstration videos, which can be viewed as sequences of states.
In such a problem, all intermediate states can be extracted from the videos.
On the contrary, in the input-output program synthesis problem studied in our work, the input to the program synthesizer provides only the initial state and the ﬁnal state.
Thus, our synthesizer is required to address the challenge of inferring intermediate states, which is mainly tackled by our execution-guided synthesis algorithm.
In contrast to training a neural network to generate the entire program, a recent line of research studies using a neural network to guide the symbolic program search based on the input-output speciﬁcation, so that the search process prioritizes the operators that have higher domain-speciﬁc scores predicted by the neural networks (Balog et al., 2017; Vijayakumar et al., 2018).
Instead of predicting such domain-speciﬁc scores to guide the program search, we directly incorporate the domain knowledge by executing partial programs, and utilize the execution results for program generation of the neural network synthesizer.
Meanwhile, recent work propose to leverage the full execution traces in the context of program repair (Wang et al., 2018b).
Our work is the ﬁrst to leverage partial execution traces for the program synthesis task, which is a much harder task than program repair.
C ONCLUSION
In this work, we propose two general and principled techniques to better leverage the semantic information for neural program synthesis: (1) execution-guided synthesis; and (2) synthesizer ensemble.
On a rich DSL with complex control ﬂows, we achieve a signiﬁcant performance gain over the existing work, which demonstrates that utilizing the semantic information is crucial in boosting the performance of neural program synthesis approaches.
We believe that our techniques are also beneﬁcial to other program generation applications, and we consider extending our techniques to handle programming languages with richer semantics as important future work.
At the same time, we have observed that utilizing existing reinforcement learning techniques does not provide much performance gain when combined with our approaches.
We believe that there is plenty of room left for further improvement and we are also interested in exploring this problem in the future
A CKNOWLEDGEMENT
We thank the anonymous reviewers for their valuable comments.
This material is in part based upon work supported by the National Science Foundation under Grant No. TWC-1409915, Berkeley
DeepDrive, and DARPA D3M under Grant No. FA8750-17-2-0091.
Any opinions, ﬁndings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reﬂect the views of the National Science Foundation
A M ORE D ESCRIPTIONS OF THE K AREL D OMAIN
Figure 4 presents the grammar speciﬁcation of the Karel DSL.
Each Karel grid world has a maximum size of 18 × 18 , and is represented as a 16 × 18 × 18 tensor, where each cell of the grid is represented as a 16 -dimensional vector corresponding to the features described in Table 4.
Prog p ::=
def run() : s
Stmt s ::=
while(b) : s | repeat(r) :
s | s 1 ; s 2 | a | if(b) : s | ifelse(b) : s 1 else : s
Cond b ::= frontIsClear() | leftIsClear() | rightIsClear | markersPresent() | noMarkersPresent() | not b
Action a ::= move()
| turnRight() | turnLeft() | pickMarker() | putMarker()
Cste r ::= 0 | 1 | ... |
[Table]
B M ORE D ETAILS ABOUT THE E XECUTION - GUIDED A LGORITHM While
-statement synthesis algorithm.
Algorithm 3 demonstrates the execution-guided algorithm for while-statement synthesis.
Training dataset construction for supervised learning.
Consider a program P = S 1 ; ... ; S T ; ⊥ , where each S i is in one of the following forms: (1) S i ∈ L ; (2) if C then B t else B f ﬁ ; and (3) while C do B end .
For each S i ∈ L , we construct a sample of (cid:104){
( s ki − 1 , O k ) }
Kk =1 , S i (cid:105)
directly.
For S i
= if C then B t else B f ﬁ , we ﬁrst construct a training sample (cid:104){ ( s ki − 1 , O k ) } Kk =1 , if C then (cid:105) .
Afterwards, we split the input-output examples into two subsets I t (cid:93) I f , where for all ( s, O ) ∈
I t , we have (cid:104)C , s (cid:105) ⇓ true ; and C evaluates to false for all ( s, O
) ∈
I f on the other hand.
Then we obtain two derived samples (cid:104)I t , B t else ; S i +1 ; ... ; S T ; ⊥(cid:105) and (cid:104)I f , B f ﬁ ; S i +1 ; ... ; S T ; ⊥(cid:105) , from which we construct training samples respectively using the same approach as discussed above.
In a similar way, we can deal with S i = while C do B end .
Finally, we include (cid:104){
( O k , O k ) }
Kk =1 , ⊥(cid:105) in our constructed training set.
1: function E XEC W HILE ( Γ , I ) 2:
C ← Γ( I ) 3:
I t ← { ( s i , s o ) ∈ I|(cid:104)C , s i (cid:105) ⇓ true } 4:
I f ← { ( s i , s o ) ∈ I|(cid:104)C , s i (cid:105) ⇓ false } 5:
B t ← Exec (Γ , I t , end -token ) 6:
I (cid:48)
t ← { ( s new , s o ) | ( s i , s o ) ∈
I t ∧ (cid:104) B t , s i (cid:105) ⇓ s new } 7:
I ← I (cid:48)
t ∪ I f 8:
S ← while C do B t end 9: return S , I 10: end function
C M ODEL D ETAILS C. N EURAL N ETWORK A RCHITECTURE
Our neural network architecture can be found in Figure 1, which follows the design in Bunel et al.
In particular, the IO Encoder is a convolutional neural network to encode the input and output grids, which outputs a 512-dimensional vector for each input-output pair.
The decoder is a 2-layer
LSTM with a hidden size of 256.
The embedding size of the program tokens is 256.
Each program is represented as a sequence of tokens G =
[ g 1 , g 2 , ..., g L ] , where each program token g i belongs to a vocabulary Σ .
At each timestep t , the decoder LSTM generates a program token g t conditioned on both the input-output pair and the previous program token g t − 1 , thus the input dimension is 768.
Each IO pair is fed into the LSTM individually, and we a max-pooling operation is performed over the hidden states { h t } Kk =1 of the last layer of LSTM for all IO pairs.
The resulted 256-dimensional vector is fed into a softmax layer to obtain a prediction probability distribution over all the 52 possible program tokens in the vocabulary.
Notice that this neural network architecture can also be applied to other program synthesis problems, with modiﬁcations of the IO encoder architectures for different formats of input-output pairs.
For example, in the domain where input-output examples are text strings, such as FlashFill (Gulwani, 2011), the IO encoders can be recurrent neural networks (RNNs) (Devlin et al., 2017b).
C. T RAINING O BJECTIVE F UNCTIONS
To estimate the parameters θ of the neural network, we ﬁrst perform supervised learning to maximize the conditional log-likelihood of the referenced programs (Parisotto et al., 2017; Devlin et al., 2017b;
Bunel et al., 2018).
In particular, we estimate θ ∗ such that
[Equation]
Where π i are the ground truth programs provided in the training set.
When training with reinforcement learning, we leverage the policy gradient algorithm REINFORCE
(Williams, 1992) to solve the following objective:
[Equation]
Where R i ( G ) is the reward function to represent the quality of the sampled program G .
In our evaluation, we set R i ( G )
= 1 if G gives the correct outputs for given inputs, and R i ( G )
=otherwise.
C. T RAINING H YPER - PARAMETERS
We use the Adam optimizer (Kingma & Ba, 2015) for both the supervised training and the RL training.
The learning rate of supervised training is 10 − 4 , and the learning rate of reinforcement learning is 10 − 5 .
We set the batch size to be 128 for supervised training, and 16 for RL training.
D E VALUATION D ETAILS D. M ORE A NALYSIS OF E VALUATION R ESULTS
In our evaluation, we observe that while our Exec approach signiﬁcantly boosts the generalization accuracy, the performance gain of the exact match accuracy is much smaller, and sometimes even negative.
We attribute this to the fact that the ground truth program in the Karel benchmark is not always the simplest one satisfying the input-output examples; on the other hand, our approach tends to provide short predictions among all programs consistent with the input-output speciﬁcation.
For example, Figure 5 shows a predicted program that is simpler than the ground truth, while also satisﬁes the input-output pairs.
Notice that different from the MLE approach in (Bunel et al., 2018), our model is not directly optimized for the exact match accuracy, since the training set not only includes the input-output examples in the original training set, but also the intermediate state pairs, which constitute a larger part of our augmented training set.
Meanwhile, in our training set, for state pairs resembling { ( s k 1 , s k 2 ) }
Kk =1 in Figure 5, it is more common for the ground truth program to be a single “move()” statement than other more complicated ones.
Therefore, when training with our approach, the model is less prone to overﬁtting to the sample distribution of the original dataset, and focuses more on the program semantics captured by the intermediate execution.
D. M ORE D ETAILS OF THE E NSEMBLE
For different training approaches of a single model, we train 15 models with different random initializations.
To do the ensemble, we ﬁrst sort the 15 models according to the descending order of their generalization accuracies on the validation set, then select the ﬁrst k models to compute the results of the k -model ensemble.
When multiple programs satisfy the ensemble criterion, e.g., with the shortest length for the Shortest method, we choose the one from the models with better generalization accuracies on the validation set.
Figure 6 shows the results of the ensemble trained with our Exec approach.
Tables 5 and 6 show the numerical results of applying ensemble to our Exec approach and Exec + RL approach.
[Table] [Table]