S im SCOOD : Systematic Analysis of Out-of-Distribution Generalization in Fine-tuned Source Code Models (2024)

Abstract: Large code datasets have become increasingly accessible for pre-training source code models. However, for the fine-tuning phase, obtaining representative training data that fully covers the code distribution for specific downstream tasks remains challenging due to the task-specific nature and limited labeling resources. These lead to out-of-distribution (OOD) generalization issues with unexpected model inference behaviors that have not been systematically studied yet.In this paper, we contribute the first systematic approach that simulates various OOD scenarios along different dimensions of source code data properties and study the fine-tuned model behaviors in such scenarios. We investigate the behaviors of models under different fine-tuning methodologies, including full fine-tuning and Low-Rank Adaptation (LoRA) fine-tuning methods. Our comprehensive analysis, conducted on four state-of-the-art pretrained models and applied to two code generation tasks, exposes multiple failure modes attributed to OOD generalization issues.

## introduction there has been increasing success in applying large language models (llms) to various source code understanding and generation tasks. llms for codes such as @xcite , codet5+ @xcite , codegen @xcite , and code llama @xcite are pretrained using large-scale code datasets, and serve as universal initialization for a variety of downstream tasks. these tasks include code summarization @xcite ), text-to-code @xcite , and program repair @xcite . the emerging abilities of llms, such as incontext learning, demonstrate their potential to handle a wide range of tasks @xcite . however, it has been shown that not all tasks can be effectively addressed by relying only on the pretrained llms @xcite . to adapt pretrained models for specific tasks, they can be fine-tuned with specific datasets. this fine-tuning process can involve optimizing all parameters or adopting a parameter-efficient approach @xcite , such as low-rank adaptation (lora) @xcite . despite having access to the large code datasets to pre-train these models, it remains challenging in practice to fully cover the code distribution, specifically in fine-tuning datasets, where the availability of labeled data is limited. furthermore, @xcite show that, in the image classification tasks, fine-tuning the parameters of the pretrained models can distort the pretrained features. therefore, it is unclear how the fine-tuned code generation models generalize to scenarios not seen or are rare in the fine-tuning distribution @xcite . for example, there is a lack of existing studies to uncover how these models generalize to programs with specific language elements or semantics not seen in fine-tuning datasets. a common way to study model behaviors in ood scenarios is to collect testing datasets in the complementary domains of the fine-tuning dataset domain @xcite . however, because the underlying distribution of programs is intractable, it is barely feasible to justify whether two raw datasets share a domain or not. not to mention the substantial costs of constituting a variety of ood datasets. simulating various ood scenarios by masking out sub-regions of training data distribution is an alternative way to systematically study the model behaviors @xcite . there are several distribution dimensions based on data properties. in the source code domain, we can have access to the structural information to model the source code distribution based on the length, syntax, and semantics of programs. for example, in terms of the syntax dimension, we can mask out all the data with uniray expressions or specific api to create a syntax-based ood scenario. in this work, we propose a systematic approach to analyzing the behaviors of fine-tuned source code models in various ood and few-data regime scenarios. we achieve this by harnessing the token size, syntax information, and contextual embeddings of programs to simulate the ood scenarios in terms of length, syntax, and semantics dimensions, as illustrated in figure 1 . by utilizing these data dimensions and control over the data, we can systematically examine the performance of fine-tuned models in ood scenarios and investigate their generalization capabilities. to summarize, the main contributions of this paper are as follows: 1. our work pioneers in investigating the behaviors of the fine-tuned source code models in ood scenarios. 2. we propose a systematic approach to simulate various ood scenarios by masking out sub-regions of source code distribution along the length, syntax, and semantics dimensions. the code and data are available at https://github.com/hajipour/simscood . 3. we find that the performance of the fine-tuned models can significantly deteriorate in various ood scenarios despite the model encountering similar examples during the pre-training phase. in particular, in syntax and length-based ood scenarios, the drop can be as substantial as 90%. 4. our systematic analysis shows that, while full fine-tuning and lora fine-tuning perform comparably on in-distribution code data, lora fine-tuning demonstrates significantly better performance on ood data. 5. our analysis of data/model properties provides insights into model fine-tuning and shapes future datasets/research to focus on ood of code models, which has the potential to enhance generalization accuracy across various code generation tasks. ## simscood: simulation of source code out-of-distribution scenarios in this work, we propose a systematic approach to investigate the fine-tuned code model behaviors on ood data by simulating the ood scenarios in multiple dimensions. our simulation strategy allows us to construct measurable ood scenarios without the additional costs of accessing another dataset. more importantly, by simulating the ood scenarios, we have control over different properties of ood scenarios. we achieve this by masking out specific sub-regions of data distribution. these ood scenarios span over three data dimensions, including length, syntax, and semantics. these dimensions cover different aspects of the programs. in length-based ood scenarios, we can study the length-generalization ability of the fine-tuned models. for example, can the models produce longer codes with high quality, and how well can the models interpolate over distribution gaps? syntax-based scenarios enable us to study the models by masking out specific language elements. more interestingly, using syntax-based scenarios, we can analyze to what extent each model can generate unseen language elements. using semantic-based scenarios, we can investigate how the models behave if we mask out the data with specific functionalities. benefiting from these scenarios, we can also implicitly quantify how well the models compose different code language elements to achieve unseen or rare functionality. ## experiments in this section, we first articulate the experiment setups, including the pretrained models, downstream tasks, and the ood data construction. then, we demonstrate the model performance in ood scenarios. we also analyze how well the model can perform by revealing 50% of the masked data (≈ 1.5% of the entire data). in the following, we call the 50% masked-out cases few-data regime. ## future work the central message of our work is that ood issues for the code data need to be explicitly taken care of. our study reveals that using lora fine-tuning the models have higher ood generalization than fully fine-tuned models. in future research, methods from various domains, such as catastrophic forgetting @xcite , ood generalization @xcite , ood detection @xcite , and continual learning @xcite , could play a role in mitigating or monitoring these ood issues. for example, employing the recall and relearn method suggested by @xcite , adapting meta-learning approaches @xcite , or applying regularization techniques like dropout @xcite could be intriguing approaches to mitigate ood issues of the fine-tuned source code model. furthermore, ood detection techniques proposed by @xcite ; @xcite can be adapted to detect the ood source code data. ## conclusion in this work, we propose a systematic approach to investigate the behaviors of fine-tuned llms in ood scenarios for the program domain. given the data, we simulate ood scenarios based on the program's length, syntax, and semantics. using these scenarios, we shed light on the models' fragility in the ood scenarios, potential performance drop, and the necessity to improve dataset construction. we also reveal the model's impotence in handling considered ood dimensions and to what extent we can improve the generalization of the models by exposing the relevant data. furthermore, our results reveal that, although models fine-tuned with full fine-tuning and lora exhibit similar indistribution accuracy, lora shows higher ood generalization accuracy.