Remove unnecessary files (batch 111)
Browse files- research/securebert2/opensource_data/data_sentence_pairs.parquet +0 -3
- research/securebert2/opensource_data/data_sentence_pairs_test.parquet +0 -3
- research/securebert2/opensource_data/data_vuln_dataset.parquet +0 -3
- research/securebert2/opensource_data/data_vuln_dataset_test.parquet +0 -3
- research/securebert2/requirements.txt +0 -211
- research/securebert2/vuln_classification/CodeVuln_eval.py +0 -119
- research/securebert2/vuln_classification/CodeVuln_train.py +0 -124
- research/securebert2/vuln_classification/init.txt +0 -1
research/securebert2/opensource_data/data_sentence_pairs.parquet
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:d00b5141b51b7a0c546374f29ffe7fa57c5f448b516642399985a1e82ce225b3
|
| 3 |
-
size 879057
|
|
|
|
|
|
|
|
|
|
|
|
research/securebert2/opensource_data/data_sentence_pairs_test.parquet
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:3190943fc8528a62d8bdbbdc122e3d706b4eb97c5fa6235f03c6caaa916706a3
|
| 3 |
-
size 197063
|
|
|
|
|
|
|
|
|
|
|
|
research/securebert2/opensource_data/data_vuln_dataset.parquet
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:294ddc5c3732ca5ccf087bfe9bc49e08fff84102c9fee79845ad8bbd2b81e193
|
| 3 |
-
size 338245
|
|
|
|
|
|
|
|
|
|
|
|
research/securebert2/opensource_data/data_vuln_dataset_test.parquet
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:5297d540407236def70adac3866f60ed1f4c980bad854cafb7473e07d13a82bc
|
| 3 |
-
size 88363
|
|
|
|
|
|
|
|
|
|
|
|
research/securebert2/requirements.txt
DELETED
|
@@ -1,211 +0,0 @@
|
|
| 1 |
-
absl-py==2.3.0
|
| 2 |
-
accelerate==1.9.0
|
| 3 |
-
aiohappyeyeballs==2.6.1
|
| 4 |
-
aiohttp==3.12.9
|
| 5 |
-
aiosignal==1.3.2
|
| 6 |
-
annotated-types==0.7.0
|
| 7 |
-
anyio==4.9.0
|
| 8 |
-
argon2-cffi==25.1.0
|
| 9 |
-
argon2-cffi-bindings==21.2.0
|
| 10 |
-
arrow==1.3.0
|
| 11 |
-
asttokens==3.0.0
|
| 12 |
-
async-lru==2.0.5
|
| 13 |
-
async-timeout==5.0.1
|
| 14 |
-
attrs==25.3.0
|
| 15 |
-
babel==2.17.0
|
| 16 |
-
backcall==0.2.0
|
| 17 |
-
backoff==2.2.1
|
| 18 |
-
beautifulsoup4==4.13.4
|
| 19 |
-
bleach==6.2.0
|
| 20 |
-
boto3==1.38.29
|
| 21 |
-
botocore==1.38.29
|
| 22 |
-
cachetools==5.5.2
|
| 23 |
-
certifi==2025.4.26
|
| 24 |
-
cffi==1.17.1
|
| 25 |
-
charset-normalizer==3.4.2
|
| 26 |
-
click==8.2.1
|
| 27 |
-
comm==0.2.2
|
| 28 |
-
contourpy==1.3.2
|
| 29 |
-
cycler==0.12.1
|
| 30 |
-
datasets==3.6.0
|
| 31 |
-
debugpy==1.8.14
|
| 32 |
-
decorator==5.2.1
|
| 33 |
-
defusedxml==0.7.1
|
| 34 |
-
dill==0.3.8
|
| 35 |
-
docker==7.1.0
|
| 36 |
-
docopt==0.6.2
|
| 37 |
-
exceptiongroup==1.3.0
|
| 38 |
-
executing==2.2.0
|
| 39 |
-
fastapi==0.115.12
|
| 40 |
-
fastjsonschema==2.21.1
|
| 41 |
-
filelock==3.18.0
|
| 42 |
-
fonttools==4.58.1
|
| 43 |
-
fqdn==1.5.1
|
| 44 |
-
frozenlist==1.6.2
|
| 45 |
-
fsspec==2025.3.0
|
| 46 |
-
google-auth==2.40.3
|
| 47 |
-
google-auth-oauthlib==1.2.2
|
| 48 |
-
grpcio==1.72.1
|
| 49 |
-
h11==0.16.0
|
| 50 |
-
hf-xet==1.1.3
|
| 51 |
-
httpcore==1.0.9
|
| 52 |
-
httptools==0.6.4
|
| 53 |
-
httpx==0.28.1
|
| 54 |
-
huggingface-hub==0.32.4
|
| 55 |
-
idna==3.10
|
| 56 |
-
inquirerpy==0.3.4
|
| 57 |
-
ipykernel==6.26.0
|
| 58 |
-
ipython==8.12.3
|
| 59 |
-
ipywidgets==8.1.1
|
| 60 |
-
isoduration==20.11.0
|
| 61 |
-
jedi==0.19.2
|
| 62 |
-
Jinja2==3.1.6
|
| 63 |
-
jmespath==1.0.1
|
| 64 |
-
joblib==1.5.1
|
| 65 |
-
json5==0.12.0
|
| 66 |
-
jsonpointer==3.0.0
|
| 67 |
-
jsonschema==4.24.0
|
| 68 |
-
jsonschema-specifications==2025.4.1
|
| 69 |
-
jupyter-events==0.12.0
|
| 70 |
-
jupyter-lsp==2.2.5
|
| 71 |
-
jupyter_client==8.6.3
|
| 72 |
-
jupyter_core==5.8.1
|
| 73 |
-
jupyter_server==2.16.0
|
| 74 |
-
jupyter_server_terminals==0.5.3
|
| 75 |
-
jupyterlab==4.2.0
|
| 76 |
-
jupyterlab_pygments==0.3.0
|
| 77 |
-
jupyterlab_server==2.27.3
|
| 78 |
-
jupyterlab_widgets==3.0.15
|
| 79 |
-
kiwisolver==1.4.8
|
| 80 |
-
lightning==2.5.1.post0
|
| 81 |
-
lightning-cloud==0.5.70
|
| 82 |
-
lightning-utilities==0.14.3
|
| 83 |
-
lightning_sdk==0.2.18
|
| 84 |
-
litdata==0.2.45
|
| 85 |
-
litserve==0.2.11
|
| 86 |
-
Markdown==3.8
|
| 87 |
-
markdown-it-py==3.0.0
|
| 88 |
-
MarkupSafe==3.0.2
|
| 89 |
-
matplotlib==3.8.2
|
| 90 |
-
matplotlib-inline==0.1.7
|
| 91 |
-
mdurl==0.1.2
|
| 92 |
-
mistune==3.1.3
|
| 93 |
-
mpmath==1.3.0
|
| 94 |
-
multidict==6.4.4
|
| 95 |
-
multiprocess==0.70.16
|
| 96 |
-
nbclient==0.10.2
|
| 97 |
-
nbconvert==7.16.6
|
| 98 |
-
nbformat==5.10.4
|
| 99 |
-
nest-asyncio==1.6.0
|
| 100 |
-
networkx==3.4.2
|
| 101 |
-
notebook_shim==0.2.4
|
| 102 |
-
numpy==1.26.4
|
| 103 |
-
nvidia-cublas-cu12==12.8.3.14
|
| 104 |
-
nvidia-cuda-cupti-cu12==12.8.57
|
| 105 |
-
nvidia-cuda-nvrtc-cu12==12.8.61
|
| 106 |
-
nvidia-cuda-runtime-cu12==12.8.57
|
| 107 |
-
nvidia-cudnn-cu12==9.7.1.26
|
| 108 |
-
nvidia-cufft-cu12==11.3.3.41
|
| 109 |
-
nvidia-cufile-cu12==1.13.0.11
|
| 110 |
-
nvidia-curand-cu12==10.3.9.55
|
| 111 |
-
nvidia-cusolver-cu12==11.7.2.55
|
| 112 |
-
nvidia-cusparse-cu12==12.5.7.53
|
| 113 |
-
nvidia-cusparselt-cu12==0.6.3
|
| 114 |
-
nvidia-nccl-cu12==2.26.2
|
| 115 |
-
nvidia-nvjitlink-cu12==12.8.61
|
| 116 |
-
nvidia-nvtx-cu12==12.8.55
|
| 117 |
-
oauthlib==3.2.2
|
| 118 |
-
overrides==7.7.0
|
| 119 |
-
packaging==24.2
|
| 120 |
-
pandas==2.1.4
|
| 121 |
-
pandocfilters==1.5.1
|
| 122 |
-
parso==0.8.4
|
| 123 |
-
pexpect==4.9.0
|
| 124 |
-
pfzy==0.3.4
|
| 125 |
-
pickleshare==0.7.5
|
| 126 |
-
pillow==11.2.1
|
| 127 |
-
pipreqs==0.5.0
|
| 128 |
-
platformdirs==4.3.8
|
| 129 |
-
prometheus_client==0.22.1
|
| 130 |
-
prompt_toolkit==3.0.51
|
| 131 |
-
propcache==0.3.1
|
| 132 |
-
protobuf==4.23.4
|
| 133 |
-
psutil==7.0.0
|
| 134 |
-
ptyprocess==0.7.0
|
| 135 |
-
pure_eval==0.2.3
|
| 136 |
-
pyarrow==20.0.0
|
| 137 |
-
pyasn1==0.6.1
|
| 138 |
-
pyasn1_modules==0.4.2
|
| 139 |
-
pycparser==2.22
|
| 140 |
-
pydantic==2.11.5
|
| 141 |
-
pydantic_core==2.33.2
|
| 142 |
-
Pygments==2.19.1
|
| 143 |
-
PyJWT==2.10.1
|
| 144 |
-
pyparsing==3.2.3
|
| 145 |
-
python-dateutil==2.9.0.post0
|
| 146 |
-
python-dotenv==1.1.0
|
| 147 |
-
python-json-logger==3.3.0
|
| 148 |
-
python-multipart==0.0.20
|
| 149 |
-
pytorch-lightning==2.5.1.post0
|
| 150 |
-
pytz==2025.2
|
| 151 |
-
PyYAML==6.0.2
|
| 152 |
-
pyzmq==26.4.0
|
| 153 |
-
referencing==0.36.2
|
| 154 |
-
regex==2024.11.6
|
| 155 |
-
requests==2.32.3
|
| 156 |
-
requests-oauthlib==2.0.0
|
| 157 |
-
rfc3339-validator==0.1.4
|
| 158 |
-
rfc3986-validator==0.1.1
|
| 159 |
-
rich==14.0.0
|
| 160 |
-
rpds-py==0.25.1
|
| 161 |
-
rsa==4.9.1
|
| 162 |
-
s3transfer==0.13.0
|
| 163 |
-
safetensors==0.5.3
|
| 164 |
-
scikit-learn==1.3.2
|
| 165 |
-
scipy==1.11.4
|
| 166 |
-
Send2Trash==1.8.3
|
| 167 |
-
sentence-transformers==5.0.0
|
| 168 |
-
seqeval==1.2.2
|
| 169 |
-
simple-term-menu==1.6.6
|
| 170 |
-
six==1.17.0
|
| 171 |
-
sniffio==1.3.1
|
| 172 |
-
soupsieve==2.7
|
| 173 |
-
stack-data==0.6.3
|
| 174 |
-
starlette==0.46.2
|
| 175 |
-
sympy==1.14.0
|
| 176 |
-
tensorboard==2.15.1
|
| 177 |
-
tensorboard-data-server==0.7.2
|
| 178 |
-
terminado==0.18.1
|
| 179 |
-
threadpoolctl==3.6.0
|
| 180 |
-
tifffile==2025.5.10
|
| 181 |
-
tinycss2==1.4.0
|
| 182 |
-
tokenizers==0.21.1
|
| 183 |
-
tomli==2.2.1
|
| 184 |
-
torch==2.7.0+cu128
|
| 185 |
-
torchmetrics==1.3.1
|
| 186 |
-
torchvision==0.22.0+cu128
|
| 187 |
-
tornado==6.5.1
|
| 188 |
-
tqdm==4.67.1
|
| 189 |
-
traitlets==5.14.3
|
| 190 |
-
transformers==4.52.4
|
| 191 |
-
triton==3.3.0
|
| 192 |
-
types-python-dateutil==2.9.0.20250516
|
| 193 |
-
typing-inspection==0.4.1
|
| 194 |
-
typing_extensions==4.14.0
|
| 195 |
-
tzdata==2025.2
|
| 196 |
-
uri-template==1.3.0
|
| 197 |
-
urllib3==2.4.0
|
| 198 |
-
uvicorn==0.34.3
|
| 199 |
-
uvloop==0.21.0
|
| 200 |
-
watchfiles==1.0.5
|
| 201 |
-
wcwidth==0.2.13
|
| 202 |
-
webcolors==24.11.1
|
| 203 |
-
webencodings==0.5.1
|
| 204 |
-
websocket-client==1.8.0
|
| 205 |
-
websockets==15.0.1
|
| 206 |
-
Werkzeug==3.1.3
|
| 207 |
-
wget==3.2
|
| 208 |
-
widgetsnbextension==4.0.14
|
| 209 |
-
xxhash==3.5.0
|
| 210 |
-
yarg==0.1.9
|
| 211 |
-
yarl==1.20.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
research/securebert2/vuln_classification/CodeVuln_eval.py
DELETED
|
@@ -1,119 +0,0 @@
|
|
| 1 |
-
# Copyright 2025 Cisco Systems, Inc. and its affiliates
|
| 2 |
-
#
|
| 3 |
-
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
-
|
| 5 |
-
import argparse
|
| 6 |
-
import torch
|
| 7 |
-
from torch.utils.data import DataLoader
|
| 8 |
-
from transformers import (
|
| 9 |
-
AutoTokenizer,
|
| 10 |
-
AutoModelForSequenceClassification,
|
| 11 |
-
)
|
| 12 |
-
from sklearn.metrics import (
|
| 13 |
-
accuracy_score,
|
| 14 |
-
precision_recall_fscore_support,
|
| 15 |
-
confusion_matrix,
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
from dataset import Eval_SentimentVulnerabilityDataset # your dataset class
|
| 19 |
-
from tqdm import tqdm
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
# ---------- Collate ----------
|
| 23 |
-
def cls_collate_fn(batch):
|
| 24 |
-
texts, labels = zip(*batch)
|
| 25 |
-
return list(texts), torch.tensor(labels, dtype=torch.long)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# ---------- Evaluation ----------
|
| 29 |
-
def evaluate(
|
| 30 |
-
ckpt_path: str,
|
| 31 |
-
batch_size: int = 32,
|
| 32 |
-
max_len: int = 1024,
|
| 33 |
-
device: torch.device = torch.device("cuda:0"),
|
| 34 |
-
):
|
| 35 |
-
# 1. Dataset and dataloader
|
| 36 |
-
test_ds = Eval_SentimentVulnerabilityDataset() # adjust if needed
|
| 37 |
-
test_dl = DataLoader(
|
| 38 |
-
test_ds,
|
| 39 |
-
batch_size=batch_size,
|
| 40 |
-
shuffle=False,
|
| 41 |
-
collate_fn=cls_collate_fn,
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
# 2. Tokenizer & model
|
| 45 |
-
tokenizer = AutoTokenizer.from_pretrained("answerdotai/ModernBERT-base")
|
| 46 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
| 47 |
-
"answerdotai/ModernBERT-base",
|
| 48 |
-
num_labels=2,
|
| 49 |
-
attn_implementation="sdpa",
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
# Load model directly
|
| 53 |
-
|
| 54 |
-
tokenizer = AutoTokenizer.from_pretrained("SynamicTechnologies/CYBERT")
|
| 55 |
-
model = AutoModelForSequenceClassification.from_pretrained("SynamicTechnologies/CYBERT")
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# 3. Load checkpoint
|
| 59 |
-
# ckpt = torch.load(ckpt_path, map_location="cpu")
|
| 60 |
-
# model.load_state_dict(ckpt["model_state_dict"], strict=True)
|
| 61 |
-
model.to(device)
|
| 62 |
-
model.eval()
|
| 63 |
-
|
| 64 |
-
# tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")
|
| 65 |
-
# model = AutoModelForSequenceClassification.from_pretrained("mahdin70/codebert-devign-code-vulnerability-detector")
|
| 66 |
-
|
| 67 |
-
# Load model directly
|
| 68 |
-
|
| 69 |
-
model.eval()
|
| 70 |
-
model.to(device)
|
| 71 |
-
|
| 72 |
-
# 4. Inference loop
|
| 73 |
-
all_preds, all_labels = [], []
|
| 74 |
-
with torch.no_grad():
|
| 75 |
-
for texts, labels in tqdm(test_dl):
|
| 76 |
-
enc = tokenizer(
|
| 77 |
-
texts,
|
| 78 |
-
padding="max_length",
|
| 79 |
-
truncation=True,
|
| 80 |
-
max_length=max_len,
|
| 81 |
-
return_tensors="pt",
|
| 82 |
-
).to(device)
|
| 83 |
-
|
| 84 |
-
labels = labels.to(device)
|
| 85 |
-
logits = model(**enc).logits
|
| 86 |
-
preds = logits.argmax(dim=-1)
|
| 87 |
-
|
| 88 |
-
all_preds.append(preds.cpu())
|
| 89 |
-
all_labels.append(labels.cpu())
|
| 90 |
-
|
| 91 |
-
# 5. Metrics
|
| 92 |
-
y_pred = torch.cat(all_preds).numpy()
|
| 93 |
-
y_true = torch.cat(all_labels).numpy()
|
| 94 |
-
|
| 95 |
-
acc = accuracy_score(y_true, y_pred)
|
| 96 |
-
prec, rec, f1, _ = precision_recall_fscore_support(
|
| 97 |
-
y_true, y_pred, average="binary"
|
| 98 |
-
)
|
| 99 |
-
cm = confusion_matrix(y_true, y_pred)
|
| 100 |
-
|
| 101 |
-
print("======= Evaluation (single GPU) =======")
|
| 102 |
-
print(f"Accuracy : {acc:.4f}")
|
| 103 |
-
print(f"Precision: {prec:.4f}")
|
| 104 |
-
print(f"Recall : {rec:.4f}")
|
| 105 |
-
print(f"F1-score : {f1:.4f}")
|
| 106 |
-
print("Confusion matrix (rows: true, cols: pred):")
|
| 107 |
-
print(cm)
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# ---------- CLI ----------
|
| 111 |
-
if __name__ == "__main__":
|
| 112 |
-
# evaluate(
|
| 113 |
-
# ckpt_path="sentiment_classif/checkpoint_epoch_6.pth", max_len = 1024
|
| 114 |
-
# )
|
| 115 |
-
# If running codebert-devign. If you get error RuntimeError: The expanded size of the tensor (1024) must match the existing size (514) at non-singleton dimension 1. Target sizes: [32, 1024]. Tensor sizes: [1, 514]
|
| 116 |
-
# Then chnage max_len from 1024 to 512
|
| 117 |
-
evaluate(
|
| 118 |
-
ckpt_path="sentiment_classif/checkpoint_epoch_6.pth", max_len = 512
|
| 119 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
research/securebert2/vuln_classification/CodeVuln_train.py
DELETED
|
@@ -1,124 +0,0 @@
|
|
| 1 |
-
# Copyright 2025 Cisco Systems, Inc. and its affiliates
|
| 2 |
-
#
|
| 3 |
-
# SPDX-License-Identifier: Apache-2.0
|
| 4 |
-
|
| 5 |
-
from dataset import ModernBertDataset, ContrastiveLearningDataset, SentimentVulnerabilityDataset
|
| 6 |
-
import torch
|
| 7 |
-
import tqdm
|
| 8 |
-
from transformers import AutoModelForMaskedLM, AutoTokenizer, get_scheduler, DataCollatorForLanguageModeling
|
| 9 |
-
from torch.nn.parallel import DistributedDataParallel as DDP
|
| 10 |
-
from torch.utils.data.distributed import DistributedSampler
|
| 11 |
-
import random
|
| 12 |
-
import os
|
| 13 |
-
from lightning.fabric import Fabric
|
| 14 |
-
from lightning.fabric.strategies import DDPStrategy
|
| 15 |
-
from transformers import AutoModelForSequenceClassification
|
| 16 |
-
torch.set_float32_matmul_precision('high')
|
| 17 |
-
|
| 18 |
-
fabric = Fabric(accelerator="gpu", devices=4, strategy=DDPStrategy(find_unused_parameters=True))
|
| 19 |
-
|
| 20 |
-
def cls_collate_fn(batch):
|
| 21 |
-
texts, labels = zip(*batch)
|
| 22 |
-
labels = torch.tensor(labels, dtype=torch.long)
|
| 23 |
-
return list(texts), labels
|
| 24 |
-
|
| 25 |
-
def train_model(checkpoint_path):
|
| 26 |
-
fabric.launch()
|
| 27 |
-
# torch.distributed.init_process_group(backend="nccl", rank=rank, world_size=world_size)
|
| 28 |
-
# torch.cuda.set_device(rank)
|
| 29 |
-
# if fabric.is_global_zero: # Only the main process writes to the log file
|
| 30 |
-
log_file = open("cont_training_log.txt", "w")
|
| 31 |
-
# Paths to your datasets
|
| 32 |
-
df = SentimentVulnerabilityDataset()
|
| 33 |
-
rng = random.Random()
|
| 34 |
-
# Hyperparameters
|
| 35 |
-
max_seq_length = 1024
|
| 36 |
-
batch_size_per_gpu = 8 # Adjust based on memory usage
|
| 37 |
-
print(f"Using per-GPU batch size: {batch_size_per_gpu}")
|
| 38 |
-
# print(f"Total effective batch size: {total_batch_size}")
|
| 39 |
-
num_epochs = 10
|
| 40 |
-
learning_rate = 1e-5
|
| 41 |
-
weight_decay = 0.01
|
| 42 |
-
mlm_prob = 0.15
|
| 43 |
-
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 44 |
-
# device = torch.device(f"cuda:{rank}")
|
| 45 |
-
# Model init
|
| 46 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 47 |
-
"answerdotai/ModernBERT-base",
|
| 48 |
-
)
|
| 49 |
-
model = AutoModelForSequenceClassification.from_pretrained(
|
| 50 |
-
"answerdotai/ModernBERT-base", # or SecureBERT checkpoint if you have one
|
| 51 |
-
num_labels=2, # binary (vulnerable / non-vulnerable)
|
| 52 |
-
attn_implementation="sdpa"
|
| 53 |
-
)
|
| 54 |
-
# model = DDP(model, device_ids=[rank], output_device=rank)
|
| 55 |
-
# Use DistributedSampler for the DataLoader
|
| 56 |
-
# sampler = DistributedSampler(df, num_replicas=world_size, rank=rank, shuffle=True)
|
| 57 |
-
sampler = DistributedSampler(df, num_replicas=fabric.world_size, rank=fabric.global_rank, shuffle=True)
|
| 58 |
-
dataloader = torch.utils.data.DataLoader(
|
| 59 |
-
df, batch_size=batch_size_per_gpu, sampler=sampler, collate_fn = cls_collate_fn
|
| 60 |
-
)
|
| 61 |
-
num_training_steps = len(dataloader) * num_epochs
|
| 62 |
-
dataloader = fabric.setup_dataloaders(dataloader)
|
| 63 |
-
# dataloader = torch.utils.data.DataLoader(df, batch_size=batch_size, shuffle=True)
|
| 64 |
-
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)
|
| 65 |
-
model, optimizer = fabric.setup(model, optimizer)
|
| 66 |
-
# 2 to 5 thousands warmup steeps if checkpoint resume
|
| 67 |
-
print("Has no warmup steps")
|
| 68 |
-
lr_scheduler = get_scheduler("linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps)
|
| 69 |
-
mask_id = tokenizer.mask_token_id
|
| 70 |
-
vocab_size = tokenizer.vocab_size
|
| 71 |
-
model.train()
|
| 72 |
-
# Load base model
|
| 73 |
-
checkpoint = torch.load(checkpoint_path, map_location=fabric.device)
|
| 74 |
-
model.load_state_dict(checkpoint["model_state_dict"], strict = False)
|
| 75 |
-
for epoch in range(num_epochs):
|
| 76 |
-
epoch_loss = 0
|
| 77 |
-
sampler.set_epoch(epoch)
|
| 78 |
-
skipped = 0
|
| 79 |
-
for batch in tqdm.tqdm(dataloader, disable=not fabric.is_global_zero):
|
| 80 |
-
optimizer.zero_grad()
|
| 81 |
-
text, labels = batch
|
| 82 |
-
try:
|
| 83 |
-
enc = tokenizer(
|
| 84 |
-
text,
|
| 85 |
-
padding="max_length",
|
| 86 |
-
truncation=True,
|
| 87 |
-
max_length=max_seq_length,
|
| 88 |
-
return_tensors="pt",
|
| 89 |
-
add_special_tokens=True
|
| 90 |
-
)
|
| 91 |
-
except:
|
| 92 |
-
skipped += 1
|
| 93 |
-
continue
|
| 94 |
-
labels = labels.to(fabric.device)
|
| 95 |
-
outputs = model(**enc, labels=labels)
|
| 96 |
-
loss = outputs.loss
|
| 97 |
-
fabric.backward(loss)
|
| 98 |
-
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) # Gradient clipping
|
| 99 |
-
optimizer.step()
|
| 100 |
-
lr_scheduler.step()
|
| 101 |
-
|
| 102 |
-
epoch_loss += loss.item()
|
| 103 |
-
avg_epoch_loss = epoch_loss / len(dataloader)
|
| 104 |
-
if fabric.is_global_zero:
|
| 105 |
-
log_message = f"Epoch Number: {epoch + 1} | Average Epoch Loss: {avg_epoch_loss} | Skipped: {skipped}\n"
|
| 106 |
-
print(log_message.strip()) # Print to console
|
| 107 |
-
log_file.write(log_message) # Write to log file
|
| 108 |
-
|
| 109 |
-
# save_path = f"model_epoch_{epoch + 1}.pth"
|
| 110 |
-
checkpoint_path = f"checkpoint_epoch_{epoch + 1}.pth"
|
| 111 |
-
torch.save({
|
| 112 |
-
"epoch": epoch + 1,
|
| 113 |
-
"model_state_dict": model.state_dict(),
|
| 114 |
-
"optimizer_state_dict": optimizer.state_dict(),
|
| 115 |
-
"lr_scheduler_state_dict": lr_scheduler.state_dict(),
|
| 116 |
-
"loss": avg_epoch_loss,
|
| 117 |
-
}, checkpoint_path)
|
| 118 |
-
# torch.save(model.state_dict(), save_path) # Save the model weights
|
| 119 |
-
print(f"Model saved to {checkpoint_path}")
|
| 120 |
-
log_file.close()
|
| 121 |
-
# torch.distributed.destroy_process_group()
|
| 122 |
-
|
| 123 |
-
if __name__ == "__main__":
|
| 124 |
-
train_model(checkpoint_path = "final_base_modernsecurebert_pths/checkpoint_epoch_20.pth")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
research/securebert2/vuln_classification/init.txt
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
|
|
|
|
|
|