Abstract
Neural program synthesis models often struggle with long, complex programs because they generate code token-by-token, making them susceptible to small errors that break logic. This paper introduces Hierarchical Neural Program Synthesis (HNPS), a framework that composes programs by predicting a sequence of "behavioral embeddings" representing sub-programs. By focusing on semantics rather than syntax, HNPS significantly improves performance on multi-step tasks, achieving nearly 90x higher accuracy than naive baselines on the longest programs tested.
The Challenge of Neural Program Synthesis
Program synthesis—generating code from input/output (I/O) examples—is a fundamental goal in automated software engineering. Traditional neural models treat this as a translation task, predicting the next token based on context. However, code differs from natural language in two critical ways:
- The Critical Token Problem: A single character change (e.g., an index shift) can invalidate an entire program.
- Program Aliasing: Multiple distinct code sequences can produce identical behavior, creating noisy training signals for token-level models.
As programs grow in length, these issues compound, leading to a "performance cliff" where standard models fail once a program exceeds a few dozen tokens.
Methodology: Hierarchical Composition
To address these hurdles, the authors propose shifting the focus from syntax (how code is written) to semantics (what code does). The HNPS framework operates in two distinct stages:
- Stage 1: Learning a Behavioral Latent Space. A Task Encoder and Program Decoder are trained on short programs. The encoder maps I/O pairs to a task embedding ($z$), creating a latent space where similar behaviors are clustered.
- Stage 2: Hierarchical Composition. For complex tasks, a Program Composer analyzes I/O pairs to predict a sequence of task embeddings. These embeddings are decoded into sub-programs and concatenated.
A key innovation is the Program Embedding Loss ($\\mathcal{L}^{PE}$), which supervises the composer in the behavioral space rather than the token space, providing a more stable and meaningful training signal.
Experimental Evaluation
The model was evaluated on string transformation tasks using datasets of varying complexity: $D_{short}$ (single expressions), $D_{composed}$ (2–4 joined programs), and $D_{long}$ (unseen programs up to 63 tokens).
The results demonstrate that HNPS is uniquely capable of scaling to long programs. While naive token-by-token models drop to near-zero accuracy as complexity increases, HNPS maintains functional correctness.
Specifically, for programs with 55-70 tokens, HNPS achieved 0.87% accuracy, compared to 0.01% for the baseline. In the 40-55 token range, HNPS reached 9.71% accuracy, while the baseline managed only 0.61%. Furthermore, the use of behavioral embeddings improved data efficiency; when training data was cut by 75%, the model with embedding loss remained 8 times more accurate than its counterpart without it.
Conclusion
HNPS demonstrates that hierarchical, semantic-level composition is a powerful alternative to sequential token generation. By modularizing the synthesis process, the model becomes resilient to the "critical token" problem. Future research aims to extend this approach to non-linear control flows (loops and conditionals) and integrate these hierarchical priors into Large Language Models to enhance their code generation reliability.
Summary updated