SimSCOOD: Systematic Analysis of Out-of-Distribution Generalization in Fine-tuned Source Code Models

Hossein Hajipour, Ning Yu, Cristian-Alexandru Staicu, Mario Fritz

Abstract

Fine-tuned Large Language Models (LLMs) for code often encounter Out-of-Distribution (OOD) data in real-world applications, yet systematic evaluation of their robustness remains challenging. We present SimSCOOD, a framework for simulating OOD scenarios by masking specific code lengths, syntax, and semantics during fine-tuning. Our analysis reveals that while full fine-tuning leads to significant performance degradation on OOD data, parameter-efficient methods like LoRA preserve pre-training knowledge and offer superior generalization.

Introduction

Large Language Models (LLMs) pre-trained on massive codebases are highly capable, but they usually require fine-tuning to excel at specific downstream tasks like text-to-code generation or bug fixing. However, task-specific fine-tuning datasets are relatively small and rarely cover the full distribution of real-world code. This creates an Out-of-Distribution (OOD) generalization problem: how does a fine-tuned model behave when asked to generate a program that is longer, uses different syntax, or requires different semantics than anything it saw during fine-tuning?

The SimSCOOD framework for simulating and measuring OOD behavior in source code models.

Because the true distribution of all possible programs is intractable, collecting real-world OOD datasets to test these boundaries is incredibly difficult. SimSCOOD addresses this lack of systematic evaluation by creating a controlled way to simulate OOD scenarios and probe model vulnerabilities.

Methods

The authors propose SimSCOOD, a systematic approach to simulate OOD scenarios by using rejection sampling to mask out specific sub-regions of a training dataset. They evaluate models across three distinct dimensions:

Three simulated OOD scenarios: length-based (token size), syntax-based (language elements), and semantic-based (clustering).
  1. Length-based: Masking out specific token-length ranges to test if models can interpolate (generate medium-length code when trained only on short and long) or extrapolate (generate long code when trained only on short).
  2. Syntax-based: Masking out specific Abstract Syntax Tree (AST) elements (e.g., while loops, yield, or else statements) to see if the model can generate unseen language elements using its pre-trained knowledge.
  3. Semantic-based: Using a pre-trained CodeT5+ encoder to cluster programs by semantic meaning in a continuous vector space, then masking out entire clusters.

The authors evaluate four models (GraphCodeBERT, CodeT5, CodeT5+, and Code Llama 13B) and compare two training paradigms: Full Fine-Tuning (FT) vs. Low-Rank Adaptation (LoRA).

Datasets

The experiments utilize the widely used CodeXGLUE benchmark, specifically focusing on two challenging generation tasks:

  • Text-to-Code: The CONCODE dataset (Java), which maps natural language descriptions to code.
  • Code Refinement: A Java dataset focused on resolving bugs by generating corrected programs.

Key Insights and Ideas

  • LoRA is a secret weapon for OOD generalization: While full fine-tuning and LoRA achieve similar accuracy on in-distribution data, LoRA drastically outperforms full fine-tuning on OOD data. Because LoRA freezes the pre-trained weights, it prevents the model from "forgetting" its broad pre-training knowledge, allowing it to better handle unseen scenarios.
  • Extrapolation is much harder than interpolation: Models easily handled length interpolation (filling the gap between short and long code) but failed catastrophically when asked to extrapolate to lengths longer than anything in the fine-tuning set.
  • The "Few-Data" magic: Exposing the model to just a tiny fraction of the masked data (~1.5% of the total fine-tuning dataset) recovers a massive amount of performance. This suggests models don't need to learn the OOD concepts from scratch; they just need a tiny nudge to "unlock" that specific knowledge from their pre-training phase.
  • Generating unseen syntax: LoRA-tuned models successfully generated specific syntax elements (like yield) even if those elements were 100% absent from the fine-tuning data.

Key Results

  • Severe Performance Drops: In syntax and length-extrapolation scenarios, model performance (measured by Exact Match) dropped by up to 90% compared to models trained on the full distribution.
  • LoRA vs. Full FT: In a length-extrapolation scenario for the text-to-code task, fully fine-tuning CodeT5 resulted in a 0.0% relative exact match. In contrast, LoRA fine-tuning achieved a 24.99% relative exact match.
  • Model Size Isn't a Silver Bullet: Even the massive Code Llama (13B parameters) struggled with OOD data, dropping to just 23.57% of its baseline performance in length extrapolation.

Limitations and Future Work

The approach is computationally expensive, as it requires training a separate model from scratch for every single masked scenario. Furthermore, the study focuses exclusively on code generation tasks; code understanding tasks might exhibit different sensitivities to OOD data. Finally, semantic clustering relies on continuous embeddings without ground-truth metadata, making it an approximated proxy for real-world semantic shifts.

Future work includes exploring continual learning techniques to mitigate catastrophic forgetting, meta-learning, and regularization techniques like dropout to further improve OOD robustness. Additionally, adapting NLP-based OOD detection techniques to source code could help flag unreliable model outputs in production.

Summary updated

BibTeX
@inproceedings{hajipour2024simscood,
  author = {Hossein Hajipour and Ning Yu and Cristian-Alexandru Staicu and Mario Fritz},
  title = {{SimSCOOD: Systematic Analysis of Out-of-Distribution Generalization in Fine-tuned Source Code Models}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-naacl.90},
}
Figure