VulBERTa-MLP fine-tuned on DiverseVul

A binary classifier that predicts whether a single C/C++ function contains a security vulnerability (1) or not (0).

It is VulBERTa, a RoBERTa model pretrained on C/C++ code (125M parameters), fine-tuned on the DiverseVul dataset. The goal was to test whether training on a larger, more diverse and more accurately labelled dataset improves on a VulBERTa model fine-tuned on the smaller ReVeal dataset.

The full notebook (dataset choice, preprocessing, training and evaluation) is on GitHub.

Results

Evaluated on a held-out test set of 37,299 functions from DiverseVul (about 8% vulnerable). The baseline is VulBERTa-MLP fine-tuned on ReVeal, evaluated on the same test set.

Model Accuracy Precision Recall F1
VulBERTa-MLP (ReVeal) — baseline 0.874 0.211 0.217 0.214
This model (DiverseVul) 0.901 0.362 0.318 0.338

F1 improves by about 58% over the baseline. Because the classes are imbalanced, accuracy is not a meaningful metric here: a model that always predicts "not vulnerable" would reach about 92%. Precision, recall and F1 on the vulnerable class are the numbers to look at.

Confusion matrix of this model on the test set:

Predicted not vulnerable Predicted vulnerable
Not vulnerable 32,681 1,660
Vulnerable 2,018 940

Training data

DiverseVul contains 330,492 real-world C/C++ functions (18,945 vulnerable), collected from security-fix commits. It was chosen over other datasets (BigVul, CVEFixes, CrossVul, ReVeal, Devign, D2A, Draper) because it has the highest reported label accuracy among real-world datasets.

Preprocessing:

  • Functions longer than 1,024 tokens (VulBERTa's input limit) were discarded.
  • The remaining functions were sampled to a fixed ratio of 92% non-vulnerable and 8% vulnerable.
  • The data was split 80% training / 20% test, keeping the same ratio in both sets.

Training procedure

  • Base model: VulBERTa (RoBERTa architecture) with its sequence-classification (MLP) head, 2 labels.
  • Frozen layers: the embeddings and the first 5 of 12 encoder layers were frozen; the upper 7 encoder layers and the classifier head were trained.
  • Class imbalance: cross-entropy loss with balanced class weights (0.543 for non-vulnerable, 6.312 for vulnerable).
  • Hyperparameters: learning rate 2e-5, batch size 16, 10 epochs, Adam (β1 = 0.9, β2 = 0.999, ε = 1e-8), linear LR schedule, seed 42, mixed precision (fp16).
  • Hardware / time: a single GPU on Google Colab, about 2.9 hours.
  • Frameworks: Transformers 4.45.2, PyTorch 2.5.1, Tokenizers 0.20.3.

How to use

The model expects inputs produced by the VulBERTa tokenizer, which pre-splits code with Clang before applying BPE. This tokenizer is not included in this repository: get it from the VulBERTa repository and see the notebook for the exact setup. Inputs are padded to 1,024 tokens.

import torch
from transformers import RobertaForSequenceClassification

model = RobertaForSequenceClassification.from_pretrained("simonevigasio02/vulberta-mlp-diversevul")
model.eval()

# input_ids and attention_mask come from the VulBERTa tokenizer (padded to 1024 tokens)
with torch.no_grad():
    logits = model(input_ids, attention_mask=attention_mask).logits
prediction = logits.argmax(dim=-1)  # 1 = vulnerable, 0 = not vulnerable

Limitations

  • Research prototype. With F1 around 0.34, the model misses most vulnerabilities and raises many false alarms. It is not suitable for real security auditing.
  • Function-level only. Each function is analysed in isolation, so vulnerabilities that depend on interactions between functions are out of scope.
  • Label noise. The DiverseVul authors estimate that about 60% of its vulnerable labels are correct, which limits what any model trained on it can learn.
  • Input length. Functions longer than 1,024 tokens cannot be classified.
  • Language. Trained only on C/C++.

References

  • Hanif & Maffeis, VulBERTa: Simplified Source Code Pre-Training for Vulnerability Detection, 2022. arXiv:2205.12424
  • Chen et al., DiverseVul: A New Vulnerable Source Code Dataset for Deep Learning Based Vulnerability Detection, 2023. arXiv:2304.00409

Author

Simone Vigasio — work started during a Machine Learning internship at Fondazione Bruno Kessler (FBK).

Downloads last month
9
Safetensors
Model size
0.1B params
Tensor type
F32
·
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Papers for simonevigasio02/vulberta-mlp-diversevul