k8si commited on
Commit
950c10a
·
verified ·
1 Parent(s): 4966f2d

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
1_Pooling/config.json ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "word_embedding_dimension": 768,
3
+ "pooling_mode_cls_token": false,
4
+ "pooling_mode_mean_tokens": true,
5
+ "pooling_mode_max_tokens": false,
6
+ "pooling_mode_mean_sqrt_len_tokens": false,
7
+ "pooling_mode_weightedmean_tokens": false,
8
+ "pooling_mode_lasttoken": false,
9
+ "include_prompt": true
10
+ }
README.md ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - sentence-transformers
4
+ - sentence-similarity
5
+ - feature-extraction
6
+ - dense
7
+ - onnx
8
+ - onnxruntime
9
+ - ai-security
10
+ - duplicate-detection
11
+ - jailbreak-detection
12
+ language: multilingual
13
+ pipeline_tag: sentence-similarity
14
+ library_name: onnx
15
+ ---
16
+
17
+ # jailbreak-embeddings-base-onnx
18
+
19
+ ONNX export of the `multilingual-e5-base-wjb-threatfeed_v1` model — a fine-tuned [sentence-transformers](https://www.SBERT.net) model for detecting duplicate vulnerability submissions (jailbreak and prompt injection attacks) in the 0din threat feed.
20
+
21
+ It maps prompts to a 768-dimensional dense vector space optimized for semantic similarity comparison of attack prompts.
22
+
23
+ This model achieves a **+50.6% F1 improvement** over the OpenAI `text-embedding-3-large` baseline on duplicate detection.
24
+
25
+ ## Model Details
26
+
27
+ ### Model Description
28
+
29
+ - **Model Type:** Sentence Transformer (two-stage fine-tuned), exported to ONNX
30
+ - **Base Model:** [intfloat/multilingual-e5-base](https://huggingface.co/intfloat/multilingual-e5-base) (~278M parameters)
31
+ - **Maximum Sequence Length:** 512 tokens
32
+ - **Output Dimensionality:** 768 dimensions
33
+ - **Similarity Function:** Cosine Similarity
34
+ - **Language:** Multilingual (XLM-RoBERTa backbone)
35
+ - **Format:** ONNX (compatible with onnxruntime, tract-onnx, and other ONNX runtimes)
36
+
37
+ ### Embedding Pipeline
38
+
39
+ ```
40
+ Input Text → Tokenizer → ONNX Model → Mean Pooling → L2 Normalization → Embedding
41
+ ```
42
+
43
+ The ONNX model contains only the transformer backbone. Mean pooling and L2 normalization must be implemented in application code (see usage examples below).
44
+
45
+ ### Model Inputs
46
+
47
+ The ONNX model requires 3 inputs:
48
+ - `input_ids`: Token IDs from tokenizer
49
+ - `attention_mask`: 1 for real tokens, 0 for padding
50
+ - `token_type_ids`: All zeros for single-sentence embeddings
51
+
52
+ ### ONNX Verification
53
+
54
+ The ONNX export produces **bit-for-bit identical** embeddings to the native sentence-transformers model (0.000000 max difference across all test sentences).
55
+
56
+ ## Intended Use
57
+
58
+ This model is designed for:
59
+
60
+ - **Duplicate detection** in AI security vulnerability reports (jailbreak/prompt injection attacks)
61
+ - **Semantic similarity** comparison of attack prompts that may use different surface-level techniques but target the same underlying vulnerability
62
+ - **Embedding generation** for LSH-based similarity search in vulnerability management systems
63
+ - **Edge/server deployment** via ONNX runtime without requiring PyTorch
64
+
65
+ The model is trained to recognize semantic equivalence between attack prompts even when they use different jailbreak tactics (e.g., role-playing, encoding, academic framing) to elicit the same harmful behavior.
66
+
67
+ ## Usage
68
+
69
+ ### sentence-transformers (with ONNX backend)
70
+
71
+ ```python
72
+ from sentence_transformers import SentenceTransformer
73
+
74
+ # Load directly with ONNX backend
75
+ model = SentenceTransformer("0dinai/jailbreak-embeddings-base-onnx", backend="onnx")
76
+
77
+ sentences = ["First attack prompt", "Second attack prompt"]
78
+ embeddings = model.encode(sentences)
79
+ similarity = model.similarity(embeddings, embeddings)
80
+ print(similarity)
81
+ ```
82
+
83
+ ### Python (onnxruntime)
84
+
85
+ ```python
86
+ import numpy as np
87
+ import onnxruntime as ort
88
+ from tokenizers import Tokenizer
89
+
90
+ # Load model and tokenizer
91
+ session = ort.InferenceSession("onnx/model.onnx")
92
+ tokenizer = Tokenizer.from_file("tokenizer.json")
93
+ tokenizer.enable_padding(pad_id=1, pad_token="<pad>")
94
+ tokenizer.enable_truncation(max_length=512)
95
+
96
+ # Tokenize
97
+ texts = ["First attack prompt", "Second attack prompt"]
98
+ encodings = tokenizer.encode_batch(texts)
99
+ input_ids = np.array([e.ids for e in encodings], dtype=np.int64)
100
+ attention_mask = np.array([e.attention_mask for e in encodings], dtype=np.int64)
101
+ token_type_ids = np.zeros_like(input_ids)
102
+
103
+ # Run ONNX inference
104
+ outputs = session.run(None, {
105
+ "input_ids": input_ids,
106
+ "attention_mask": attention_mask,
107
+ "token_type_ids": token_type_ids,
108
+ })
109
+ token_embeddings = outputs[0] # [batch, seq_len, 768]
110
+
111
+ # Mean pooling
112
+ mask = attention_mask[:, :, np.newaxis].astype(np.float32)
113
+ embeddings = (token_embeddings * mask).sum(axis=1) / mask.sum(axis=1)
114
+
115
+ # L2 normalization
116
+ norms = np.linalg.norm(embeddings, axis=1, keepdims=True)
117
+ embeddings = embeddings / norms
118
+
119
+ # Cosine similarity
120
+ similarity = np.dot(embeddings[0], embeddings[1])
121
+ print(f"Similarity: {similarity:.4f}")
122
+ ```
123
+
124
+ ### Rust (tract-onnx)
125
+
126
+ ```rust
127
+ use tract_onnx::prelude::*;
128
+ use tokenizers::Tokenizer;
129
+
130
+ // Load model and tokenizer
131
+ let model = tract_onnx::onnx()
132
+ .model_for_path("onnx/model.onnx")?
133
+ .into_optimized()?
134
+ .into_runnable()?;
135
+ let tokenizer = Tokenizer::from_file("tokenizer.json")?;
136
+
137
+ // Tokenize
138
+ let encoding = tokenizer.encode("Attack prompt text", true)?;
139
+ let input_ids: Vec<i64> = encoding.get_ids().iter().map(|&x| x as i64).collect();
140
+ let attention_mask: Vec<i64> = encoding.get_attention_mask().iter().map(|&x| x as i64).collect();
141
+ let token_type_ids: Vec<i64> = vec![0i64; input_ids.len()];
142
+
143
+ // Run inference, then apply mean pooling + L2 normalization
144
+ // (see full Rust implementation at github.com/0din-ai)
145
+ ```
146
+
147
+ ## Training Details
148
+
149
+ This model was trained using a **two-stage fine-tuning approach**:
150
+
151
+ ### Stage 1: WildJailbreak Pre-training
152
+
153
+ Pre-trained on public synthetic data to learn jailbreak semantics.
154
+
155
+ - **Dataset:** [Allen AI WildJailbreak](https://huggingface.co/datasets/allenai/wildjailbreak) — vanilla-adversarial prompt pairs
156
+ - **Pairs:** 161,396 positive pairs (same intent, different formulation)
157
+ - **Split:** 153,326 train / 4,034 val / 4,036 test (95% / 2.5% / 2.5%)
158
+ - **Loss:** MultipleNegativesRankingLoss (in-batch negatives)
159
+ - **Batch size:** 16 (per device) x 2 gradient accumulation steps = 32 effective
160
+ - **Learning rate:** 1e-5
161
+ - **FP16:** True
162
+ - **Purpose:** Teach the model to see through jailbreak wrappers and match prompts by underlying intent
163
+
164
+ ### Stage 2: Threat Feed Fine-tuning
165
+
166
+ Fine-tuned on annotated pairs from the internal 0din threat feed.
167
+
168
+ - **Pairs:** 9,598 annotated pairs (7,678 train / 958 val / 962 test)
169
+ - **Label Distribution:** ~34% duplicates / ~66% non-duplicates
170
+ - **Annotation:** Google Gemini 2.5 Pro (single-model annotation)
171
+ - **Source Similarity Threshold:** Candidate pairs generated with Thor similarity >= 0.5
172
+ - **Loss:** ContrastiveLoss (cosine distance, margin=0.5)
173
+ - **Purpose:** Calibrate the model for real-world duplicate detection on production vulnerability data
174
+
175
+ #### Stage 2 Hyperparameters
176
+
177
+ | Parameter | Value |
178
+ |-----------|-------|
179
+ | Epochs | 50 (early stopped) |
180
+ | Batch size | 8 (per device) x 4 gradient accumulation = 32 effective |
181
+ | Learning rate | 1e-5 |
182
+ | LR scheduler | Linear |
183
+ | Warmup ratio | 0.1 |
184
+ | Weight decay | 0.01 |
185
+ | FP16 | True |
186
+ | Early stopping patience | 10 |
187
+ | Eval steps | 50 |
188
+ | Seed | 1 |
189
+ | Best checkpoint | Step 1200 (epoch 5.0) |
190
+ | Best validation loss | 0.0149 |
191
+
192
+ ## Evaluation Results
193
+
194
+ ### Duplicate Detection Performance
195
+
196
+ Evaluated on 55 human-labeled vulnerability pairs (10 duplicates, 45 non-duplicates) from a corpus of 3,749 vulnerabilities. Best F1 score at each model's optimal threshold:
197
+
198
+ | Model | Best F1 | Threshold | Precision | Recall |
199
+ |-------|---------|-----------|-----------|--------|
200
+ | OpenAI text-embedding-3-large (baseline) | 0.462 | 0.80 | 1.000 | 0.300 |
201
+ | Finetuned V1 (WildJailbreak only, e5-small) | 0.500 | 0.50 | 0.333 | 1.000 |
202
+ | Finetuned V2 (WJB + threat feed v1, e5-small) | 0.526 | 0.70 | 0.556 | 0.500 |
203
+ | Finetuned V3 (WJB + threat feed v2, e5-small) | 0.556 | 0.75 | 0.625 | 0.500 |
204
+ | Finetuned V4 (WJB + threat feed 10k, e5-small) | 0.600 | 0.70 | 0.600 | 0.600 |
205
+ | **This model (Base V1)** | **0.696** | **0.70** | **0.615** | **0.800** |
206
+
207
+ ### Threshold Analysis (This Model)
208
+
209
+ | Threshold | Precision | Recall | F1 | TP | FP | FN | TN |
210
+ |-----------|-----------|--------|------|----|----|----|----|
211
+ | 0.50 | 0.243 | 0.900 | 0.383 | 9 | 28 | 1 | 17 |
212
+ | 0.55 | 0.308 | 0.800 | 0.444 | 8 | 18 | 2 | 27 |
213
+ | 0.60 | 0.381 | 0.800 | 0.516 | 8 | 13 | 2 | 32 |
214
+ | 0.65 | 0.500 | 0.800 | 0.615 | 8 | 8 | 2 | 37 |
215
+ | **0.70** | **0.615** | **0.800** | **0.696** | **8** | **5** | **2** | **40** |
216
+ | 0.75 | 0.625 | 0.500 | 0.556 | 5 | 3 | 5 | 42 |
217
+ | 0.80 | 0.800 | 0.400 | 0.533 | 4 | 1 | 6 | 44 |
218
+ | 0.85 | 1.000 | 0.300 | 0.462 | 3 | 0 | 7 | 45 |
219
+ | 0.90 | 1.000 | 0.100 | 0.182 | 1 | 0 | 9 | 45 |
220
+
221
+ ### Key Findings
222
+
223
+ - **+50.6% F1 improvement** over the OpenAI text-embedding-3-large baseline (0.696 vs 0.462)
224
+ - **Largest single jump in the series:** +16% F1 over the e5-small V4 model (0.696 vs 0.600), showing that model capacity matters for this task.
225
+ - **Substantially higher recall:** At threshold 0.70, this model achieves 0.800 recall vs 0.600 for e5-small V4, while maintaining comparable precision (0.615 vs 0.600).
226
+ - **Wide effective threshold band:** Recall stays at 0.800 across thresholds 0.50–0.70, suggesting the larger model produces more confident and well-separated similarity scores for true duplicate pairs.
227
+
228
+ > **Note:** The evaluation dataset is small (55 pairs, 10 positive). With only 10 true duplicates, each TP/FP change causes large metric swings. Results should be interpreted with caution.
229
+
230
+ ## Limitations
231
+
232
+ - **Small evaluation set:** Only 55 human-labeled pairs (10 duplicates). Results should be taken as directional rather than definitive.
233
+ - **LLM annotation bias in training data:** Stage 2 training data was annotated by a single LLM (Gemini 2.5 Pro), which may affect calibration.
234
+ - **Model size:** ~278M parameters with 768-dim embeddings. The ONNX model is ~1GB.
235
+ - **Domain-specific:** Optimized for jailbreak/prompt injection duplicate detection. Performance on general semantic similarity tasks is not evaluated.
236
+
237
+ ## Citation
238
+
239
+ ### BibTeX
240
+
241
+ #### Sentence Transformers
242
+ ```bibtex
243
+ @inproceedings{reimers-2019-sentence-bert,
244
+ title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
245
+ author = "Reimers, Nils and Gurevych, Iryna",
246
+ booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
247
+ month = "11",
248
+ year = "2019",
249
+ publisher = "Association for Computational Linguistics",
250
+ url = "https://arxiv.org/abs/1908.10084",
251
+ }
252
+ ```
253
+
254
+ #### ContrastiveLoss
255
+ ```bibtex
256
+ @inproceedings{hadsell2006dimensionality,
257
+ author={Hadsell, R. and Chopra, S. and LeCun, Y.},
258
+ booktitle={2006 IEEE Computer Society Conference on Computer Vision and Pattern Recognition (CVPR'06)},
259
+ title={Dimensionality Reduction by Learning an Invariant Mapping},
260
+ year={2006},
261
+ volume={2},
262
+ number={},
263
+ pages={1735-1742},
264
+ doi={10.1109/CVPR.2006.100}
265
+ }
266
+ ```
267
+
268
+ #### WildJailbreak
269
+ ```bibtex
270
+ @article{jiang2024wildteaming,
271
+ title={WildTeaming at Scale: From In-the-Wild Jailbreaks to (Adversarially) Safer Language Models},
272
+ author={Jiang, Liwei and Bhatt, Kavel and Phute, Seungju and Hwang, Jaehun and Liang, Dongwei and Sap, Maarten and Hajishirzi, Hannaneh and Choi, Yejin},
273
+ journal={arXiv preprint arXiv:2406.18510},
274
+ year={2024}
275
+ }
276
+ ```
config.json ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "XLMRobertaModel"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "bos_token_id": 0,
7
+ "classifier_dropout": null,
8
+ "dtype": "float32",
9
+ "eos_token_id": 2,
10
+ "hidden_act": "gelu",
11
+ "hidden_dropout_prob": 0.1,
12
+ "hidden_size": 768,
13
+ "initializer_range": 0.02,
14
+ "intermediate_size": 3072,
15
+ "layer_norm_eps": 1e-05,
16
+ "max_position_embeddings": 514,
17
+ "model_type": "xlm-roberta",
18
+ "num_attention_heads": 12,
19
+ "num_hidden_layers": 12,
20
+ "output_past": true,
21
+ "pad_token_id": 1,
22
+ "position_embedding_type": "absolute",
23
+ "transformers_version": "4.57.6",
24
+ "type_vocab_size": 1,
25
+ "use_cache": true,
26
+ "vocab_size": 250002
27
+ }
config_sentence_transformers.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "SentenceTransformer",
3
+ "__version__": {
4
+ "sentence_transformers": "5.2.2",
5
+ "transformers": "4.57.6",
6
+ "pytorch": "2.10.0"
7
+ },
8
+ "prompts": {
9
+ "query": "",
10
+ "document": ""
11
+ },
12
+ "default_prompt_name": null,
13
+ "similarity_fn_name": "cosine"
14
+ }
modules.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "idx": 0,
4
+ "name": "0",
5
+ "path": "",
6
+ "type": "sentence_transformers.models.Transformer"
7
+ },
8
+ {
9
+ "idx": 1,
10
+ "name": "1",
11
+ "path": "1_Pooling",
12
+ "type": "sentence_transformers.models.Pooling"
13
+ },
14
+ {
15
+ "idx": 2,
16
+ "name": "2",
17
+ "path": "2_Normalize",
18
+ "type": "sentence_transformers.models.Normalize"
19
+ }
20
+ ]
onnx/model.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:88ce21bc3aa9f5157d49909faf029883b6c3647b5b178a5ebaa5792eb2dd304b
3
+ size 1110007849
sentence_bert_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "max_seq_length": 512,
3
+ "do_lower_case": false
4
+ }
special_tokens_map.json ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "cls_token": {
10
+ "content": "<s>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "eos_token": {
17
+ "content": "</s>",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "mask_token": {
24
+ "content": "<mask>",
25
+ "lstrip": true,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "pad_token": {
31
+ "content": "<pad>",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ },
37
+ "sep_token": {
38
+ "content": "</s>",
39
+ "lstrip": false,
40
+ "normalized": false,
41
+ "rstrip": false,
42
+ "single_word": false
43
+ },
44
+ "unk_token": {
45
+ "content": "<unk>",
46
+ "lstrip": false,
47
+ "normalized": false,
48
+ "rstrip": false,
49
+ "single_word": false
50
+ }
51
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:883b037111086fd4dfebbbc9b7cee11e1517b5e0c0514879478661440f137085
3
+ size 17082987
tokenizer_config.json ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "<s>",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "<pad>",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "</s>",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "<unk>",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "250001": {
36
+ "content": "<mask>",
37
+ "lstrip": true,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "bos_token": "<s>",
45
+ "clean_up_tokenization_spaces": true,
46
+ "cls_token": "<s>",
47
+ "eos_token": "</s>",
48
+ "extra_special_tokens": {},
49
+ "mask_token": "<mask>",
50
+ "max_length": 512,
51
+ "model_max_length": 512,
52
+ "pad_to_multiple_of": null,
53
+ "pad_token": "<pad>",
54
+ "pad_token_type_id": 0,
55
+ "padding_side": "right",
56
+ "sep_token": "</s>",
57
+ "stride": 0,
58
+ "tokenizer_class": "XLMRobertaTokenizerFast",
59
+ "truncation_side": "right",
60
+ "truncation_strategy": "longest_first",
61
+ "unk_token": "<unk>"
62
+ }