VulFixMiner: end-to-end commit classification

A standalone repository combining the fine-tuned VulFixMiner CodeBERT encoder, the trained commit classifier, a tokenizer, and a custom Transformers pipeline. Input is a raw multi-file git show / git diff string, not a commit message or plain source code. Output is the probability of a security-fixing commit. The custom pipeline handles per-file encoding and commit-level pooling.

Usage

Install the inference requirements, then load the model with trust_remote_code=True:

from transformers import pipeline

classifier = pipeline(
    "text-classification",
    model="andstor/VulFixMiner",  # Use "." to load the local repository instead.
    trust_remote_code=True,
    device="cpu",  # Also supports "cuda" or "mps".
    file_batch_size=None,  # None = all files; use e.g. 32 to encode in batches before mean-pooling.
)

diff = """diff --git a/example.c b/example.c
--- a/example.c
+++ b/example.c
@@ -1 +1,2 @@
-old_call();
+validate_input();
+fixed_call();
"""

security_probability = classifier(diff)  # One float between 0 and 1.
print("Security-patch probability:", security_probability)

# Lists contain independent commits, not files from the same commit.
batch_scores = classifier([diff, diff])  # One float per commit, in input order.

Each commit returns a SECURITY_PATCH probability between 0 and 1. A list of commits returns a list of scores in input order. The non-security probability is 1 - security_probability.

Pipeline batch_size must remain 1. Empty, comment-only, and binary-only commits raise EmptyCommitError. Review the custom code before enabling remote code, and pin a reviewed Hub revision in production.

How it works

  1. Extract added and removed lines for each changed file, filtering comment prefixes.
  2. Join added and removed text with a separator and tokenize each file to 256 tokens, truncating or padding as needed.
  3. Encode each file with CodeBERT and average its first-token embeddings across the commit.
  4. Classify the commit embedding and return the security-patch probability.

Commit metadata and unchanged lines are ignored. Use standard, unquoted Git diffs.

Direct model interface

import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(".", trust_remote_code=True)
model = AutoModelForSequenceClassification.from_pretrained(".", trust_remote_code=True).eval()

# Already-preprocessed per-file inputs: added + separator + deleted.
inputs = tokenizer(
    ["fixed();\n" + tokenizer.sep_token + "old();\n"],
    return_tensors="pt",
    padding="max_length",
    truncation=True,
    max_length=256,
)
with torch.inference_mode():
      logits = model(**inputs).logits  # [number_of_commits, 2]
    print(logits.softmax(dim=-1)[:, 1])

All files belong to one commit unless commit_ids is provided. For example, commit_ids=torch.tensor([0, 0, 1]) groups three file inputs into two commits. Indices must be contiguous and zero-based. Direct model inputs do not run raw diff preprocessing; use the custom pipeline for the full end-to-end path.

Downloads last month
25
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Dataset used to train andstor/VulFixMiner

Collection including andstor/VulFixMiner