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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
base_model: mistralai/Ministral-8B-Instruct-2410
|
3 |
+
tags:
|
4 |
+
- unsloth
|
5 |
+
- lora
|
6 |
+
- qlora
|
7 |
+
- vulnerability-detection
|
8 |
+
- security
|
9 |
+
- code-analysis
|
10 |
+
- cybersecurity
|
11 |
+
- ultival
|
12 |
+
- peft
|
13 |
+
- adapter
|
14 |
+
language:
|
15 |
+
- en
|
16 |
+
license: apache-2.0
|
17 |
+
library_name: peft
|
18 |
+
pipeline_tag: text-generation
|
19 |
+
---
|
20 |
+
|
21 |
+
# UltiVal: Ministral-8B QLoRA Adapter for Vulnerability Detection
|
22 |
+
|
23 |
+
This is a **QLoRA adapter** fine-tuned from **Ministral-8B-Instruct-2410** for detecting security vulnerabilities in source code as part of the **UltiVal** project.
|
24 |
+
|
25 |
+
## π¨ Important Note
|
26 |
+
|
27 |
+
This is a **LoRA adapter**, not a standalone model. You must load it together with the base model `mistralai/Ministral-8B-Instruct-2410`.
|
28 |
+
|
29 |
+
## π Model Details
|
30 |
+
|
31 |
+
- **Base Model**: `mistralai/Ministral-8B-Instruct-2410`
|
32 |
+
- **Adapter Type**: QLoRA (4-bit Low-Rank Adaptation)
|
33 |
+
- **Training Framework**: Unsloth
|
34 |
+
- **Task**: Security vulnerability detection in source code
|
35 |
+
- **Model Size**: ~334MB (adapter only)
|
36 |
+
- **Context Length**: 2048 tokens
|
37 |
+
- **Languages**: Multi-language code analysis (Python, JavaScript, Java, C/C++, etc.)
|
38 |
+
|
39 |
+
## π― Training Configuration
|
40 |
+
|
41 |
+
| Parameter | Value |
|
42 |
+
|-----------|--------|
|
43 |
+
| **Training Steps** | 6,000 (best checkpoint) |
|
44 |
+
| **Total Steps** | 6,184 |
|
45 |
+
| **Validation Loss** | 0.5840 (lowest achieved at step 6000) |
|
46 |
+
| **Final Training Loss** | 0.4081 |
|
47 |
+
| **Epochs** | 2 |
|
48 |
+
| **Learning Rate** | 2e-4 β 1.76e-7 (cosine schedule) |
|
49 |
+
| **Batch Size** | 8 (2 Γ 4 gradient accumulation) |
|
50 |
+
| **Sequence Length** | 2048 tokens |
|
51 |
+
| **LoRA Rank** | 32 |
|
52 |
+
| **LoRA Alpha** | 32 |
|
53 |
+
| **LoRA Dropout** | 0.0 |
|
54 |
+
| **Weight Decay** | 0.01 |
|
55 |
+
| **Warmup Steps** | ~5% of total steps |
|
56 |
+
|
57 |
+
### Target Modules
|
58 |
+
```
|
59 |
+
q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
|
60 |
+
```
|
61 |
+
|
62 |
+
## π§ Usage
|
63 |
+
|
64 |
+
### Option 1: Using Unsloth (Recommended)
|
65 |
+
|
66 |
+
```python
|
67 |
+
from unsloth import FastLanguageModel
|
68 |
+
import torch
|
69 |
+
|
70 |
+
# Load base model
|
71 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
72 |
+
model_name="mistralai/Ministral-8B-Instruct-2410",
|
73 |
+
max_seq_length=2048,
|
74 |
+
dtype=None,
|
75 |
+
load_in_4bit=True,
|
76 |
+
)
|
77 |
+
|
78 |
+
# Add LoRA configuration
|
79 |
+
model = FastLanguageModel.get_peft_model(
|
80 |
+
model,
|
81 |
+
r=32,
|
82 |
+
target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
|
83 |
+
"gate_proj", "up_proj", "down_proj"],
|
84 |
+
lora_alpha=32,
|
85 |
+
lora_dropout=0,
|
86 |
+
bias="none",
|
87 |
+
use_gradient_checkpointing="unsloth",
|
88 |
+
random_state=3407,
|
89 |
+
)
|
90 |
+
|
91 |
+
# Load the trained adapter
|
92 |
+
model.load_adapter("starsofchance/Mistral-Unsloth-QLoRA-adapter")
|
93 |
+
|
94 |
+
# Enable inference mode
|
95 |
+
FastLanguageModel.for_inference(model)
|
96 |
+
```
|
97 |
+
|
98 |
+
### Option 2: Using Transformers + PEFT
|
99 |
+
|
100 |
+
```python
|
101 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
102 |
+
from peft import PeftModel
|
103 |
+
import torch
|
104 |
+
|
105 |
+
# Load base model
|
106 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
107 |
+
"mistralai/Ministral-8B-Instruct-2410",
|
108 |
+
torch_dtype=torch.float16,
|
109 |
+
device_map="auto",
|
110 |
+
load_in_4bit=True
|
111 |
+
)
|
112 |
+
|
113 |
+
tokenizer = AutoTokenizer.from_pretrained("mistralai/Ministral-8B-Instruct-2410")
|
114 |
+
|
115 |
+
# Load LoRA adapter
|
116 |
+
model = PeftModel.from_pretrained(base_model, "starsofchance/Mistral-Unsloth-QLoRA-adapter")
|
117 |
+
```
|
118 |
+
|
119 |
+
## π» Inference Example
|
120 |
+
|
121 |
+
```python
|
122 |
+
# Example: SQL Injection Detection
|
123 |
+
code_snippet = '''
|
124 |
+
def authenticate_user(username, password):
|
125 |
+
query = "SELECT * FROM users WHERE username='" + username + "' AND password='" + password + "'"
|
126 |
+
cursor.execute(query)
|
127 |
+
return cursor.fetchone()
|
128 |
+
'''
|
129 |
+
|
130 |
+
messages = [
|
131 |
+
{"role": "user", "content": f"Analyze this code for security vulnerabilities:\n\n{code_snippet}"}
|
132 |
+
]
|
133 |
+
|
134 |
+
# Tokenize and generate
|
135 |
+
input_ids = tokenizer.apply_chat_template(
|
136 |
+
messages,
|
137 |
+
add_generation_prompt=True,
|
138 |
+
return_tensors="pt"
|
139 |
+
).to(model.device)
|
140 |
+
|
141 |
+
outputs = model.generate(
|
142 |
+
input_ids,
|
143 |
+
max_new_tokens=512,
|
144 |
+
do_sample=False,
|
145 |
+
pad_token_id=tokenizer.eos_token_id,
|
146 |
+
temperature=0.1
|
147 |
+
)
|
148 |
+
|
149 |
+
response = tokenizer.decode(outputs[0][len(input_ids[0]):], skip_special_tokens=True)
|
150 |
+
print(response)
|
151 |
+
```
|
152 |
+
|
153 |
+
### Expected Output
|
154 |
+
```
|
155 |
+
This code contains a critical SQL injection vulnerability. The user input (username and password)
|
156 |
+
is directly concatenated into the SQL query without any sanitization or parameterization.
|
157 |
+
|
158 |
+
**Vulnerability Type**: SQL Injection (CWE-89)
|
159 |
+
**Severity**: High
|
160 |
+
**Location**: Line 2, query construction
|
161 |
+
|
162 |
+
**How to exploit**: An attacker could input malicious SQL code like:
|
163 |
+
- Username: `admin' --`
|
164 |
+
- Password: `anything`
|
165 |
+
|
166 |
+
**Secure fix**: Use parameterized queries:
|
167 |
+
```python
|
168 |
+
def authenticate_user(username, password):
|
169 |
+
query = "SELECT * FROM users WHERE username=? AND password=?"
|
170 |
+
cursor.execute(query, (username, password))
|
171 |
+
return cursor.fetchone()
|
172 |
+
```
|
173 |
+
```
|
174 |
+
|
175 |
+
## π‘οΈ Supported Vulnerability Types
|
176 |
+
|
177 |
+
The model is trained to detect various security vulnerabilities including:
|
178 |
+
|
179 |
+
| Category | Examples |
|
180 |
+
|----------|----------|
|
181 |
+
| **Injection** | SQL Injection, Command Injection, LDAP Injection |
|
182 |
+
| **XSS** | Reflected XSS, Stored XSS, DOM-based XSS |
|
183 |
+
| **Authentication** | Weak passwords, Authentication bypass, Session management |
|
184 |
+
| **Authorization** | Privilege escalation, Access control issues |
|
185 |
+
| **Cryptography** | Weak encryption, Hardcoded keys, Improper random generation |
|
186 |
+
| **File Operations** | Path traversal, File inclusion, Unsafe deserialization |
|
187 |
+
| **Memory Safety** | Buffer overflow, Use after free, Memory leaks |
|
188 |
+
| **Web Security** | CSRF, SSRF, Insecure redirects |
|
189 |
+
|
190 |
+
## π Performance Metrics
|
191 |
+
|
192 |
+
### Training Progress
|
193 |
+
- **Initial Loss**: 1.5544
|
194 |
+
- **Final Loss**: 0.4081
|
195 |
+
- **Best Validation Loss**: 0.5840 (step 6000)
|
196 |
+
- **Training Duration**: ~15 hours
|
197 |
+
- **Convergence**: Stable convergence with cosine learning rate schedule
|
198 |
+
|
199 |
+
### Hardware Requirements
|
200 |
+
- **Training**: NVIDIA GPU with 4-bit quantization
|
201 |
+
- **Inference**: Can run on CPU or GPU (GPU recommended for speed)
|
202 |
+
- **Memory**: ~6GB GPU memory for inference with 4-bit quantization
|
203 |
+
|
204 |
+
## π Repository Structure
|
205 |
+
|
206 |
+
```
|
207 |
+
starsofchance/Mistral-Unsloth-QLoRA-adapter/
|
208 |
+
βββ adapter_config.json # LoRA configuration
|
209 |
+
βββ adapter_model.safetensors # Trained adapter weights (~334MB)
|
210 |
+
βββ tokenizer.json # Tokenizer configuration
|
211 |
+
βββ tokenizer_config.json # Tokenizer settings
|
212 |
+
βββ special_tokens_map.json # Special tokens mapping
|
213 |
+
βββ README.md # This file
|
214 |
+
```
|
215 |
+
|
216 |
+
## β οΈ Limitations
|
217 |
+
|
218 |
+
1. **Adapter Dependency**: Requires the base model to function
|
219 |
+
2. **Context Window**: Limited to 2048 tokens
|
220 |
+
3. **Language Coverage**: Primarily trained on common programming languages
|
221 |
+
4. **False Positives**: May flag secure code patterns as potentially vulnerable
|
222 |
+
5. **Novel Vulnerabilities**: May not detect cutting-edge or highly obfuscated attacks
|
223 |
+
6. **Code Context**: Performance depends on having sufficient code context
|
224 |
+
|
225 |
+
## π Integration Tips
|
226 |
+
|
227 |
+
### Batch Processing
|
228 |
+
```python
|
229 |
+
def analyze_multiple_files(code_files):
|
230 |
+
results = []
|
231 |
+
for file_path, code_content in code_files:
|
232 |
+
# Analyze each file
|
233 |
+
messages = [{"role": "user", "content": f"Analyze for vulnerabilities:\n\n{code_content}"}]
|
234 |
+
# ... generate response
|
235 |
+
results.append({"file": file_path, "analysis": response})
|
236 |
+
return results
|
237 |
+
```
|
238 |
+
|
239 |
+
### Custom Prompting
|
240 |
+
```python
|
241 |
+
# For specific vulnerability types
|
242 |
+
prompt = f"""
|
243 |
+
Focus on SQL injection vulnerabilities in this code:
|
244 |
+
{code_snippet}
|
245 |
+
|
246 |
+
Provide:
|
247 |
+
1. Vulnerability assessment (Yes/No)
|
248 |
+
2. Risk level (Low/Medium/High/Critical)
|
249 |
+
3. Specific location
|
250 |
+
4. Remediation steps
|
251 |
+
"""
|
252 |
+
```
|
253 |
+
|
254 |
+
## π Training Data
|
255 |
+
|
256 |
+
The model was fine-tuned on a curated dataset featuring:
|
257 |
+
- **Real-world vulnerabilities** from CVE databases
|
258 |
+
- **Secure code patterns** for contrast learning
|
259 |
+
- **Multi-language examples** across different frameworks
|
260 |
+
- **Detailed explanations** with remediation guidance
|
261 |
+
- **Context-rich examples** showing vulnerability in realistic scenarios
|
262 |
+
|
263 |
+
## π Model Lineage
|
264 |
+
|
265 |
+
```
|
266 |
+
Ministral-8B-Instruct-2410 (Mistral AI)
|
267 |
+
β
|
268 |
+
QLoRA Fine-tuning (Unsloth)
|
269 |
+
β
|
270 |
+
UltiVal Vulnerability Detection Adapter
|
271 |
+
```
|
272 |
+
|
273 |
+
## π Citation
|
274 |
+
|
275 |
+
If you use this model in your research or applications, please cite:
|
276 |
+
|
277 |
+
```bibtex
|
278 |
+
@misc{ultival_mistral_lora_2025,
|
279 |
+
title={UltiVal: Ministral-8B QLoRA Adapter for Vulnerability Detection},
|
280 |
+
author={StarsOfChance},
|
281 |
+
year={2025},
|
282 |
+
publisher={Hugging Face},
|
283 |
+
url={https://huggingface.co/starsofchance/Mistral-Unsloth-QLoRA-adapter}
|
284 |
+
}
|
285 |
+
```
|
286 |
+
|
287 |
+
## βοΈ License
|
288 |
+
|
289 |
+
This adapter inherits the license from the base model `mistralai/Ministral-8B-Instruct-2410`. Please refer to the [base model's license](https://huggingface.co/mistralai/Ministral-8B-Instruct-2410) for specific terms and conditions.
|
290 |
+
|
291 |
+
## π Acknowledgments
|
292 |
+
|
293 |
+
- **Unsloth Team**: For the efficient LoRA fine-tuning framework
|
294 |
+
- **Mistral AI**: For the powerful Ministral-8B-Instruct-2410 base model
|
295 |
+
- **Hugging Face**: For the model hosting and PEFT library
|
296 |
+
- **UltiVal Project**: Part of ongoing research in automated vulnerability detection
|
297 |
+
|
298 |
+
## π Contact & Support
|
299 |
+
|
300 |
+
- **Issues**: Report bugs or issues in the [model repository](https://huggingface.co/starsofchance/Mistral-Unsloth-QLoRA-adapter/discussions)
|
301 |
+
- **Updates**: Follow for model updates and improvements
|
302 |
+
- **Community**: Join discussions about vulnerability detection and code security
|
303 |
+
|
304 |
+
---
|
305 |
+
|
306 |
+
**π Security Note**: This model is designed to assist in security analysis but should not be the sole method for vulnerability assessment. Always conduct comprehensive security reviews with multiple tools and expert analysis.
|