Skip to main content
QUICK REVIEW

[Paper Review] Finetuning Large Language Models for Vulnerability Detection

Alexey Shestov, Rodion Levichev|arXiv (Cornell University)|Jan 30, 2024
Web Application Security VulnerabilitiesComputer Science5 citations
TL;DR

The paper finetunes WizardCoder, a StarCoder-based LLM, for binary classification of Java functions as vulnerable, explores training regimes, and shows improved vulnerability detection over CodeBERT-like models, including handling data imbalance and faster training via batch packing and LoRA:**

ABSTRACT

This paper presents the results of finetuning large language models (LLMs) for the task of detecting vulnerabilities in source code. We leverage WizardCoder, a recent improvement of the state-of-the-art LLM StarCoder, and adapt it for vulnerability detection through further finetuning. To accelerate training, we modify WizardCoder's training procedure, also we investigate optimal training regimes. For the imbalanced dataset with many more negative examples than positive, we also explore different techniques to improve classification performance. The finetuned WizardCoder model achieves improvement in ROC AUC and F1 measures on balanced and imbalanced vulnerability datasets over CodeBERT-like model, demonstrating the effectiveness of adapting pretrained LLMs for vulnerability detection in source code. The key contributions are finetuning the state-of-the-art code LLM, WizardCoder, increasing its training speed without the performance harm, optimizing the training procedure and regimes, handling class imbalance, and improving performance on difficult vulnerability detection datasets. This demonstrates the potential for transfer learning by finetuning large pretrained language models for specialized source code analysis tasks.

Motivation & Objective

  • Investigate whether large language models pretrained on code can be effectively fine-tuned for vulnerability detection in source code.
  • Determine if StarCoder-family models outperform CodeBERT-family models on balanced vulnerability datasets.
  • Explore training regime optimizations, including classification-focused loss, context size, and batch packing to improve efficiency and performance.
  • Assess methods to handle imbalanced vulnerability datasets, including focal loss and sample weighting, and evaluate calibration effects.
  • Provide practical guidance on adapting large code LLMs under limited computational resources for classification tasks.

Proposed method

  • Select WizardCoder (13B parameters) as the base model and compare with ContraBERT on vulnerability detection tasks.
  • Apply low-rank adaptation (LoRA) with r=8, alpha=32, dropout=0.05 to enable memory-efficient fine-tuning.
  • Replace next-token prediction loss with a dedicated classification loss that uses the probability of the final token to predict vulnerability.
  • Pack multiple short code functions into a single input sequence (batch packing) to improve training throughput.
  • Evaluate two training regimes: next-token prediction vs. binary classification objective, and study their impact on ROC AUC and F1.
  • Experiment with context sizes (512 vs 2048 tokens) to isolate the source of performance gains.

Experimental results

Research questions

  • RQ1RQ1: Is a StarCoder-based model more effective than a CodeBERT-based model for balanced vulnerability detection?
  • RQ2RQ2: Is a StarCoder-based model more effective than a CodeBERT-based model for imbalanced vulnerability detection?
  • RQ3RQ3: Is the standard LLM training approach (next-token plus QA) effective for vulnerability detection?
  • RQ4RQ4: Does dynamic batch packing affect model quality?
  • RQ5RQ5: Is mean loss reduction or sum loss reduction better for packed batches?
  • RQ6RQ6: Does larger context size improve vulnerability detection performance?
  • RQ7RQ7: Is focal loss with sample weighting effective for class-imbalanced vulnerability detection?

Key findings

  • WizardCoder outperforms ContraBERT on balanced vulnerability detection (ROC AUC 0.69 vs 0.68; F1 0.71 vs 0.68).
  • On imbalanced data with P3 negatives, WizardCoder achieves ROC AUC of 0.86 and F1 of 0.27, slightly better than ContraBERT.
  • Treating vulnerability detection as a binary classification task with a dedicated classification loss yields better ROC AUC than the next-token prediction approach (0.86 vs 0.75).
  • Batch packing speeds training by up to ~13x, with no significant loss in performance when using sum loss reduction rather than mean loss.
  • Context size (2048 vs 512) does not drive performance gains; improvements are attributed to better code representations learned by the model.
  • Focal loss with gamma=1 offers a small ROC AUC gain over standard cross-entropy, and sample weighting further improves calibration and test metrics.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.