Evaluation of LLMs on Syntax-Aware Code Fill-in-the-Middle Tasks

Linyuan Gong, Sida Wang, Mostafa Elhoushi, Alvin Cheung

Introduction

While Large Language Models (LLMs) for code have traditionally been evaluated on their ability to generate functions from scratch—a "Left-to-Right" (L2R) approach—real-world software development frequently requires "Fill-in-the-Middle" (FIM) capabilities. Developers often insert logic into existing codebases, complete conditional expressions, or fill in API calls. The Syntax-Aware Fill-in-the-Middle (SAFIM) benchmark addresses the limitations of previous evaluation frameworks by introducing a large-scale, multilingual, and contamination-resistant environment for assessing these specific skills.

The Limitations of Current Benchmarks

Existing benchmarks like HumanEval or MBPP primarily focus on standalone function generation. Previous attempts to measure FIM performance suffered from two critical flaws:

  1. Semantic Randomness: Many benchmarks used random character or line masking. This fails to reflect how developers actually work, as they typically fill in logical "chunks" like loops or specific API calls rather than arbitrary strings.
  2. Data Contamination: LLMs are trained on massive repositories like GitHub. Models often "memorize" common test cases, leading to inflated performance scores.

SAFIM addresses these by using syntax-aware masking and sourcing data exclusively from post-April 2022 submissions to ensure the models haven't seen the code during pretraining.

The SAFIM Methodology

To solve these issues, SAFIM introduces three key innovations:

1. Syntax-Aware Masking

Using Abstract Syntax Trees (AST), SAFIM identifies and masks three distinct categories of code:

  • Algorithmic Blocks: The core logic within a function.
  • Control-Flow Expressions: Logic found within if or while statements.
  • API Function Calls: Specific external library interactions.

2. Syntax-Aware Truncation

A common failure mode in LLM evaluation is "hallucination," where a model generates the correct code but continues to output irrelevant text, causing unit tests to fail. SAFIM employs an iterative algorithm that trims model output until it fits perfectly back into the original code's syntax tree, revealing the model's true underlying proficiency.

3. Diverse Prompting

To ensure fair comparison across different architectures, SAFIM supports multiple prompt designs, including Prefix-Suffix-Middle (PSM) for FIM-native models and One-Shot (1S) prompting for chat-tuned models.

Dataset and Scale

The SAFIM benchmark consists of 17,720 examples across four major programming languages: Python, Java, C++, and C#. To minimize data contamination, the researchers sourced data exclusively from Codeforces and GitHub submissions created between April 2022 and January 2023—a period following the training cutoff for many popular models. This scale is significantly larger than previous standards; for comparison, HumanEval-Infilling contains only 164 examples.

Key Findings

The study yielded several surprising insights into the nature of code LLMs:

The "Free Lunch" of FIM Pretraining

The researchers found that pretraining models with a FIM objective—teaching them to predict missing segments based on surrounding context—not only makes them better at infilling but actually improves their standard L2R performance. This suggests that bidirectional context provides a deeper semantic understanding of code.

Model Performance and Efficiency

The results challenged the assumption that larger models are always superior. DeepSeekCoder-33B was the top performer with a 69.0% average Pass@1 score. However, smaller models like StarCoder (15.5B) achieved a 55.5% average, outperforming GPT-4 (53.3%) despite the massive difference in parameter count.

The Impact of Truncation

The importance of the syntax-aware truncation algorithm cannot be overstated. For CodeLLaMa-13B, applying this truncation caused the Pass@1 rate to jump from 16.4% to 41.4%. This indicates that many models possess the correct logic but struggle with the "stopping problem" in infilling tasks.

Conclusion and Future Work

SAFIM demonstrates that FIM capability is a critical and distinct dimension of coding proficiency. While the benchmark provides a robust framework, future work is needed to isolate the effects of pretraining data versus architecture within single model families. Additionally, as models are trained on increasingly recent data, the benchmark will require periodic updates to maintain its contamination-free status.

Summary updated

BibTeX
@inproceedings{gong2024evaluation,
  author = {Linyuan Gong and Sida Wang and Mostafa Elhoushi and Alvin Cheung},
  title = {{Evaluation of LLMs on Syntax-Aware Code Fill-in-the-Middle Tasks}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=jKYyFbH8ap},
}
Figure