Abstract
While large language models (LLMs) are increasingly being used for program synthesis, they lack the global view needed to develop useful abstractions; they generally predict programs one at a time, often repeating the same functionality. Generating redundant code from scratch is both inefficient and error-prone. To address this, we propose Refactoring for Generalizable Abstraction Learning (ReGAL), a gradient-free method for learning a library of reusable functions via code refactorization, i.e. restructuring code without changing its execution output. ReGAL learns from a small set of existing programs, iteratively verifying and refining its abstractions via execution. We find that the shared function libraries discovered by ReGAL make programs easier to predict across diverse domains. On three datasets (LOGO graphics generation, Date reasoning, and TextCraft, a Minecraft-based text game), both open-source and proprietary LLMs improve in accuracy when predicting programs with ReGAL functions. For CodeLlama-13B, ReGAL results in absolute accuracy increases of 11.5% on graphics, 26.1% on date understanding, and 8.1% on TextCraft, outperforming GPT-3.5 in two of three domains. Our analysis reveals ReGAL's abstractions encapsulate frequently-used subroutines as well as environment dynamics.
Introduction
Current Large Language Models (LLMs) typically approach program synthesis as a series of "one-off" tasks, generating code from scratch for every query. This "primitive-only" approach often leads to redundant logic and increases the likelihood of errors, such as math slips or logic inconsistencies, especially when generating complex subroutines. Furthermore, without high-level abstractions, models must map complex natural language directly to long sequences of low-level primitives, requiring intense reasoning for every line of code.
To address these limitations, we introduce Refactoring for Generalizable Abstraction Learning (ReGAL). ReGAL provides LLMs with a "global view," allowing them to identify common patterns across multiple tasks and refactor them into a library of shared, verified abstractions stored in a "Code Bank."
Methodology: The ReGAL Framework
ReGAL is a gradient-free learning framework that updates the context available to the model rather than its weights. The process involves two primary phases:
1. The Induction Phase (Training)
The model processes training examples in batches, following a curriculum from simple to complex tasks.
- Refactoring: The LLM identifies common subroutines and rewrites them into helper functions.
- Verification & Retrial: Each new function is executed. If the refactored program's output matches the original, the function is added to the Code Bank. If it fails, the model uses execution feedback to attempt a fix.
- Library Management: The system periodically prunes the Code Bank of unreliable functions and edits others to improve generalizability.
2. The Synthesis Phase (Testing)
For new queries, ReGAL retrieves relevant helper functions from the Code Bank and successful usage examples from a Demo Bank. It then uses In-Context Learning (ICL) to prompt the LLM to solve the task using these high-level abstractions.
Experimental Evaluation
ReGAL was evaluated across five diverse domains: LOGO (Turtle graphics), Date Reasoning, TextCraft (Minecraft-based crafting), MATH (Algebra), and TabMWP (tabular mathematical reasoning).
Key Results
ReGAL yielded significant improvements in absolute accuracy, particularly for the CodeLlama-13B model:
- LOGO: +11.5% accuracy boost.
- Date Reasoning: +26.1% accuracy boost.
- TextCraft: +8.1% accuracy boost.
Notably, CodeLlama-13B augmented with ReGAL outperformed the baseline GPT-3.5 in the LOGO and TextCraft domains. The system also demonstrated robustness to distribution shifts, learning new abstractions through iterative pruning and editing on novel data.
Discussion and Insights
- Reasoning Shortcuts: Verified functions like
draw_small_9gon()offload complex geometry to the library, allowing the model to focus on high-level task recognition at test time. - Execution Feedback: Unlike standard training, ReGAL uses ground-truth execution to filter hallucinations, ensuring only functional abstractions are stored.
- Bridging the Model Gap: ReGAL enables smaller open-source models (e.g., 13B parameters) to rival or exceed the performance of much larger proprietary models like GPT-3.5.
- Curriculum Learning: Starting with simpler problems allows the model to build foundational blocks before tackling complex structures.
Limitations and Future Work
While effective, ReGAL's code-based abstractions are less flexible than neural representations and may lack human-like "elegance" in naming. The system currently relies on a predefined set of domain-specific primitives; future research could explore discovering these base primitives from raw data. Additionally, adapting code-based abstractions to highly dynamic environments, such as robotics, remains a challenge.
Summary updated