# Planning with Large Language Models for Code Generation

**Authors:** Shun Zhang, Zhenfang Chen, Yikang Shen, Mingyu Ding, Joshua B. Tenenbaum, Chuang Gan  
**arXiv:** [2303.05510](https://arxiv.org/abs/2303.05510)  
**Date:** 2023-03-09  
**Summary page:** https://www.summarizepaper.com/en/paper/2303.05510v1/

## Key points

- Existing large language model-based pipelines often use beam search or sampling algorithms for code generation
- These methods can result in issues like failed compilation or incorrect outputs
- Conventional Transformer decoding algorithms may not be suitable for code generation tasks
- The authors propose a novel Transformer decoding algorithm called Planning-Guided Transformer Decoding (PG-TD)
- PG-TD incorporates a planning algorithm that performs lookahead search and guides the Transformer in generating improved programs
- The Transformer utilizes a planner to generate candidate programs and evaluates them against public test cases
- This allows the Transformer to make more informed decisions and generate higher-quality programs
- A mechanism is designed to facilitate information sharing between the Transformer and the planner for computational efficiency
- Empirical evaluation shows that PG-TD consistently generates programs with higher performance compared to baseline methods
- PG-TD enables controllable code generation by optimizing modified objectives such as concise codes or highly-commented codes
- Overall, PG-TD improves code generation in large language model-based pipelines by incorporating planning algorithms and leveraging public test cases.

## Summary

In the field of code generation, existing large language model-based pipelines often rely on beam search or sampling algorithms during the decoding process. While these methods may produce programs with high token-matching scores, they frequently encounter issues such as failed compilation or incorrect outputs. This can be attributed to the fact that conventional Transformer decoding algorithms may not be the most suitable choice for code generation tasks. To address this limitation, the authors propose a novel Transformer decoding algorithm called Planning-Guided Transformer Decoding (PG-TD). This approach incorporates a planning algorithm that performs lookahead search and guides the Transformer in generating improved programs. Instead of solely optimizing the likelihood of generated sequences, the Transformer utilizes a planner to generate candidate programs and evaluates them against public test cases. By doing so, the Transformer can make more informed decisions and generate tokens that ultimately lead to higher-quality programs. To ensure computational efficiency, the authors design a mechanism that facilitates information sharing between the Transformer and the planner. This optimization enhances the overall performance of their algorithm. The framework is empirically evaluated using several large language models as backbones on public coding challenge benchmarks. The results demonstrate two key findings: firstly, it consistently generates programs with higher performance compared to competing baseline methods; secondly, it enables controllable code generation by optimizing modified objectives such as concise codes or highly-commented codes. Overall, this work introduces PG-TD as an innovative approach to improve code generation in large language model-based pipelines. By incorporating planning algorithms and leveraging public test cases, this method enhances program quality and offers controllability in generating different types of code structures.
