Coding-PTMs: How to Find Optimal Code Pre-trained Models for Code Embedding in Vulnerability Detection?

Yu Zhao, Lina Gong, Zhiqiu Huang, Yongwei Wang, Mingqiang Wei, Fei Wu

Abstract

Vulnerability detection is garnering increasing attention in software engineering, since code vulnerabilities possibly pose significant security. Recently, reusing various code pre-trained models has become common for code embedding without providing reasonable justifications in vulnerability detection. The premise for casually utilizing pre-trained models (PTMs) is that the code embeddings generated by different PTMs would generate a similar impact on the performance. Is that TRUE? To answer this important question, we systematically investigate the effects of code embedding generated by ten different code PTMs on the performance of vulnerability detection, and get the answer, i.e., that is NOT true. We observe that code embedding generated by various code PTMs can indeed influence the performance and selecting an embedding technique based on parameter scales and embedding dimension is not reliable. Our findings highlight the necessity of quantifying and evaluating the characteristics of code embedding generated by various code PTMs to understand the effects. To achieve this goal, we analyze the numerical representation and data distribution of code embedding generated by different PTMs to evaluate differences and characteristics. Based on these insights, we propose Coding-PTMs, a recommendation framework to assist engineers in selecting optimal code PTMs for their specific vulnerability detection tasks. Specifically, we define thirteen code embedding metrics across three dimensions (i.e., statistics, norm, and distribution) for constructing a specialized code PTM recommendation dataset. We then employ a Random Forest classifier to train a recommendation model and identify the optimal code PTMs from the candidate model zoo.

1. Introduction

In modern AI-assisted software engineering, the use of pre-trained models (PTMs) like CodeBERT or CodeT5 for generating code embeddings has become standard practice. These embeddings serve as the mathematical foundation for downstream classifiers in tasks such as vulnerability detection. However, the selection of a specific PTM is often arbitrary, guided by popularity or the assumption that larger models inherently provide superior embeddings.

This research challenges these assumptions, demonstrating that different PTMs produce embeddings with vastly different numerical distributions, leading to significant performance variations across different datasets. Currently, engineers lack a systematic method to select the optimal PTM, often resorting to resource-intensive trial-and-error.

2. Methodology

To address this selection gap, we propose Coding-PTMs, a recommendation framework designed to predict the performance of a PTM based on the intrinsic characteristics of its generated embeddings.

2.1 Metric Definition

We defined 13 numerical metrics across three key dimensions to characterize code embeddings:

  • Statistics: Mean, Variance, Standard Deviation, Skewness, Kurtosis, and Range.
  • Norms: L0, L1, L2, and Nuclear norms, which measure vector magnitude and sparsity.
  • Distribution: Maximum Mean Discrepancy (MMD) and Dot Product distance, measuring the alignment between training and test data distributions.

2.2 The Coding-PTMs Framework

The framework utilizes a Random Forest classifier trained on a meta-dataset of embedding metrics. This model predicts whether a candidate PTM will be a "top-tier" performer for a specific task. We evaluated 10 embedding configurations from four major families: CodeBERT, CodeT5, CodeGen, and PolyCoder.

2.3 Datasets

The study utilized four major C/C++ vulnerability detection benchmarks: Devign (real-world exploits), Reveal (challenging real-world data), and CWE119/CWE399 (specific vulnerability types). A meta-dataset was created by sampling these benchmarks 200 times each, resulting in 8,000 unique instances for training the recommendation model.

3. Results and Analysis

Our investigation revealed that the choice of PTM is critical and non-trivial. Performance varies significantly across models and datasets, with no single model dominating all tasks.

Performance comparison (AUC) of 10 PTMs across four vulnerability detection datasets, demonstrating significant performance variance.

3.1 Key Insights

  • Parameter Scale vs. Performance: Larger models do not guarantee better results. For instance, CodeGen-small (350M parameters) frequently outperformed CodeGen-base (2B parameters).
  • Mathematical Personalities: Different PTM families exhibit distinct embedding distributions. Models in the CodeT5 family tend to produce near-normal distributions, whereas PolyCoder produces highly skewed distributions. These "mathematical personalities" significantly impact classifier learning.
Embedding value distributions for various PTMs, illustrating the distinct 'mathematical personalities' that influence downstream performance.
  • Feature Importance: Standard Deviation and Variance emerged as the most critical metrics for predicting PTM success. Embeddings that are too tightly packed or overly scattered generally lead to poorer detection performance.

3.2 Framework Performance

The Coding-PTMs recommendation model achieved 91% Accuracy and an 83% F1-Score in identifying top-performing PTMs. In real-world validation on unseen data, the framework correctly recommended the optimal PTM 78% of the time, offering a significant efficiency gain over brute-force training.

4. Conclusion

Coding-PTMs provides a systematic, metric-driven approach to PTM selection for vulnerability detection. While currently focused on C/C++ and specific model families, the framework demonstrates that statistical analysis of embeddings is a powerful predictor of model performance. Future work will expand this methodology to larger LLMs and additional software engineering tasks.

Summary updated

Abstract

Vulnerability detection is garnering increasing attention in software engineering, since code vulnerabilities possibly pose significant security. Recently, reusing various code pre-trained models has become common for code embedding without providing reasonable justifications in vulnerability detection. The premise for casually utilizing pre-trained models (PTMs) is that the code embeddings generated by different PTMs would generate a similar impact on the performance. Is that TRUE? To answer this important question, we systematically investigate the effects of code embedding generated by ten different code PTMs on the performance of vulnerability detection, and get the answer, i.e., that is NOT true. We observe that code embedding generated by various code PTMs can indeed influence the performance and selecting an embedding technique based on parameter scales and embedding dimension is not reliable. Our findings highlight the necessity of quantifying and evaluating the characteristics of code embedding generated by various code PTMs to understand the effects. To achieve this goal, we analyze the numerical representation and data distribution of code embedding generated by different PTMs to evaluate differences and characteristics. Based on these insights, we propose Coding-PTMs, a recommendation framework to assist engineers in selecting optimal code PTMs for their specific vulnerability detection tasks. Specifically, we define thirteen code embedding metrics across three dimensions (i.e., statistics, norm, and distribution) for constructing a specialized code PTM recommendation dataset. We then employ a Random Forest classifier to train a recommendation model and identify the optimal code PTMs from the candidate model zoo.
BibTeX
@inproceedings{zhao2024coding,
  author = {Yu Zhao and Lina Gong and Zhiqiu Huang and Yongwei Wang and Mingqiang Wei and Fei Wu},
  title = {{Coding-PTMs: How to Find Optimal Code Pre-trained Models for Code Embedding in Vulnerability Detection?}},
  booktitle = {ASE},
  year = {2024},
  doi = {10.1145/3691620.3695539},
}
Figure