Ranking LLM-Generated Loop Invariants for Program Verification

Saikat Chakraborty, Shuvendu K. Lahiri, Sarah Fakhoury, Akash Lal, Madanlal Musuvathi, Aseem Rastogi, Aditya Senthilnathan, Rahul Sharma, Nikhil Swamy

Abstract

Large Language Models (LLMs) have demonstrated a remarkable ability to generate loop invariants, a critical component in formal program verification. However, due to their stochastic nature, LLMs often produce many incorrect candidates before finding a valid one, leading to a high "verification tax" in the form of numerous expensive calls to SMT solvers like Z3. This paper introduces iRank, a contrastive re-ranking framework that employs neural embeddings to prioritize correct invariants. By training a transformation network to align problem definitions with correct invariants in a vector space, iRank reduces the median rank of a correct solution from 31 to 4 for GPT-3.5, significantly streamlining the verification process.

Introduction

Program verification ensures that software behaves according to its specifications. A central challenge in verifying programs with loops is the discovery of loop invariants—logical properties that hold true throughout the loop's execution. Finding these invariants is an undecidable problem traditionally addressed through complex symbolic reasoning.

Recent advancements have leveraged LLMs to synthesize these invariants. While effective, LLMs typically generate a large set of candidates, only a few of which are correct. The current "guess-and-check" paradigm is bottlenecked by the computational cost of verifying each candidate using automated provers. iRank addresses this by re-ordering candidates so that the most likely correct invariants are checked first, minimizing the number of calls to the verifier.

The iRank Methodology

The iRank framework utilizes a contrastive learning approach to re-rank candidate invariants. The process involves three main stages: embedding, transformation, and ranking.

  1. Embedding: Program problem definitions (including pre-conditions, loop bodies, and post-conditions) and candidate invariants are converted into high-dimensional vectors using pre-trained LLM embedding models, such as OpenAI’s text-embedding-ada-002.
  2. Transformation: These raw embeddings are processed through a trainable 3-layer fully connected neural network. This network is specifically optimized to "warp" the embedding space.
  3. Contrastive Training: During training, the model learns to minimize the distance (maximize cosine similarity) between a problem and its verified invariants while maximizing the distance from incorrect ones. This ensures that logical relevance is reflected in spatial proximity.
The iRank framework during training (top) and ranking (bottom) phases.

Experimental Evaluation

The authors evaluated iRank using a dataset of 940 challenge problems from the Loop Invariant Synthesis benchmarks, represented in the SyGuS (Syntax-Guided Synthesis) format. Training and evaluation data were generated by prompting GPT-3.5 and GPT-4 to solve these problems, capturing both successful and failed invariant attempts to create a robust training set for the contrastive ranker.

Key Results and Insights

The experimental results demonstrate that iRank significantly outperforms raw LLM ordering and standard embedding baselines.

  • Rank Reduction: For GPT-3.5, the median rank of a verified invariant improved from 31 to 4. For the more capable GPT-4, the median rank improved from approximately 5 to 2.
  • Success@1: iRank placed a correct invariant in the top spot nearly 45% of the time for GPT-4, compared to only 17% for the default LLM ranking.
  • The Necessity of Training: A key insight was that "out-of-the-box" LLM embeddings are insufficient for this task, sometimes performing worse than random baselines. This highlights the necessity of the contrastive training step to align general semantic embeddings with the strict requirements of formal logic.
Distribution of the rank of verified invariants across various ranking experiments. iRank-Ada and iRank-Davinci show significant improvements over baselines.

Conclusion

iRank provides a practical neural solution to the "verification tax" associated with LLM-based invariant synthesis. By reducing the number of required Z3 calls, iRank substantially lowers the total time required for program verification. While the current implementation focuses on the SyGuS format, future work could extend this approach to general-purpose programming languages like C or Java. iRank demonstrates that neural re-ranking is a powerful tool for making automated formal verification more efficient and accessible for real-world software development.

Summary updated

BibTeX
@inproceedings{chakraborty2023ranking,
  author = {Saikat Chakraborty and Shuvendu K. Lahiri and Sarah Fakhoury and Akash Lal and Madanlal Musuvathi and Aseem Rastogi and Aditya Senthilnathan and Rahul Sharma and Nikhil Swamy},
  title = {{Ranking LLM-Generated Loop Invariants for Program Verification}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2023},
  doi = {10.18653/v1/2023.findings-emnlp.614},
}
Figure