Skip to main content
QUICK REVIEW

[Paper Review] Planning with Large Language Models for Code Generation

Shun Zhang, Zhenfang Chen|arXiv (Cornell University)|Mar 9, 2023
Software Testing and Debugging TechniquesComputer Science13 citations
TL;DR

The paper introduces Planning-Guided Transformer Decoding (PG-TD), a planning-based decoding method that collaborates with a pre-trained code-generation Transformer to generate higher-quality programs by performing lookahead using a planner and test-case rewards, achieving better pass rates and enabling controllable code generation without finetuning.

ABSTRACT

Existing large language model-based code generation pipelines typically use beam search or sampling algorithms during the decoding process. Although the programs they generate achieve high token-matching-based scores, they often fail to compile or generate incorrect outputs. The main reason is that conventional Transformer decoding algorithms may not be the best choice for code generation. In this work, we propose a novel Transformer decoding algorithm, Planning-Guided Transformer Decoding (PG-TD), that uses a planning algorithm to do lookahead search and guide the Transformer to generate better programs. Specifically, instead of simply optimizing the likelihood of the generated sequences, the Transformer makes use of a planner to generate candidate programs and test them on public test cases. The Transformer can therefore make more informed decisions and generate tokens that will eventually lead to higher-quality programs. We also design a mechanism that shares information between the Transformer and the planner to make our algorithm computationally efficient. We empirically evaluate our framework with several large language models as backbones on public coding challenge benchmarks, showing that 1) it can generate programs that consistently achieve higher performance compared with competing baseline methods; 2) it enables controllable code generation, such as concise codes and highly-commented codes by optimizing modified objective.

Motivation & Objective

  • Motivate the limitations of traditional Transformer decoding (beam search/sampling) for code generation where even high token-matching often yields non-compiling or incorrect programs.
  • Propose PG-TD, a planning-guided decoding framework that uses a planner to search candidate programs guided by test-case rewards.
  • Enable model-agnostic integration with any standard code-generation Transformer without requiring finetuning or extra sample collection.
  • Show that PG-TD yields higher pass rates and supports controllable objectives (e.g., conciseness and commentary) via reward design.

Proposed method

  • Formulate code generation as an MDP where states are problem descriptions concatenated with partial programs and actions are next tokens.
  • Apply a Monte-Carlo tree search-inspired planner (with P-UCB selection) to perform lookahead using Transformer-provided next-token probabilities as guidance.
  • Expand nodes with top-k high-probability next tokens to form candidate continuations.
  • Evaluate partial states by completing them with Transformer beam search and testing on public test cases to obtain rewards, which backpropagate in the tree.
  • Introduce caching mechanisms (tree structure and generated sequences) to share information between the Transformer and the planner and improve efficiency.
  • Demonstrate model-agnostic applicability by testing with GPT-2 and GPT-Neo backbones on APPS and CodeContests datasets.
  • Explore controllable objectives by modifying the planner reward (e.g., shorter code, more comments) and show potential for finetuning using PG-TD-generated samples.

Experimental results

Research questions

  • RQ1Can integrating a planning algorithm with a pre-trained code-generation Transformer yield better programs than standard decoding or sampling-based baselines?
  • RQ2Is PG-TD effective across different Transformer backbones (model-agnostic)?
  • RQ3Does planning-based decoding improve sample efficiency and computation time via information sharing and caching?
  • RQ4Can PG-TD support controllable code generation objectives without fine-tuning the Transformer?
  • RQ5Can PG-TD-generated samples be used to finetune the Transformer to further improve performance?

Key findings

  • PG-TD consistently outperforms beam search and sampling+filtering baselines on APPS and CodeContests in terms of pass rate.
  • PG-TD demonstrates model-agnostic improvements when applied to GPT-2 and GPT-Neo backbones.
  • Planning-based decoding provides effective lookahead, outperforming Sequential Monte-Carlo-Guided TD (SMCG-TD) and matching or exceeding pass rates under equivalent compute budgets.
  • Caching mechanisms (tree-structure and sequence caching) significantly reduce computation time and redundant Transformer calls.
  • PG-TD enables controllable code generation (e.g., shorter code, more comments) by changing the planner reward function, and generated samples can finetune the Transformer to further improve performance.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.