Text Generation
PEFT
Safetensors
code
English
security
cybersecurity
secure-coding
ai-security
owasp
code-generation
lora
fine-tuned
securecode
conversational
Instructions to use scthornton/granite-20b-code-securecode with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use scthornton/granite-20b-code-securecode with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("ibm-granite/granite-20b-code-instruct-8k") model = PeftModel.from_pretrained(base_model, "scthornton/granite-20b-code-securecode") - Notebooks
- Google Colab
- Kaggle
Update model card: audited SecureCode 2,372 dataset, bf16 LoRA on DGX Spark GB10
Browse files
README.md
CHANGED
|
@@ -1,143 +1,213 @@
|
|
| 1 |
---
|
| 2 |
-
library_name: peft
|
| 3 |
-
pipeline_tag: text-generation
|
| 4 |
license: apache-2.0
|
| 5 |
-
|
| 6 |
-
- code
|
| 7 |
-
base_model:
|
| 8 |
-
- ibm-granite/granite-20b-code-instruct-8k
|
| 9 |
tags:
|
| 10 |
-
-
|
| 11 |
-
-
|
| 12 |
-
-
|
| 13 |
-
-
|
| 14 |
-
-
|
| 15 |
-
-
|
| 16 |
-
-
|
| 17 |
-
-
|
| 18 |
-
-
|
| 19 |
datasets:
|
| 20 |
-
- scthornton/securecode
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
---
|
| 25 |
|
| 26 |
# Granite 20B Code SecureCode
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
---
|
| 35 |
|
| 36 |
## What This Model Does
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
-
|
| 41 |
-
- **
|
| 42 |
-
-
|
| 43 |
-
-
|
| 44 |
|
| 45 |
-
-
|
| 46 |
|
| 47 |
## Model Details
|
| 48 |
|
| 49 |
-
|
|
| 50 |
-
|---
|
| 51 |
-
| **Base Model** | [
|
| 52 |
| **Parameters** | 20B |
|
| 53 |
-
| **Architecture** | GPT
|
| 54 |
-
| **
|
| 55 |
-
| **
|
| 56 |
-
| **LoRA
|
| 57 |
-
| **
|
| 58 |
-
| **Training
|
| 59 |
-
| **Hardware** |
|
| 60 |
-
| **Framework** | PEFT 0.18.1, Transformers 5.1.0, PyTorch 2.7.1 |
|
| 61 |
|
| 62 |
-
-
|
| 63 |
|
| 64 |
## Quick Start
|
| 65 |
|
| 66 |
```python
|
| 67 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 68 |
from peft import PeftModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 71 |
"ibm-granite/granite-20b-code-instruct-8k",
|
|
|
|
| 72 |
device_map="auto",
|
| 73 |
-
load_in_4bit=True
|
| 74 |
)
|
| 75 |
-
model = PeftModel.from_pretrained(base_model, "scthornton/granite-20b-code-securecode")
|
| 76 |
tokenizer = AutoTokenizer.from_pretrained("scthornton/granite-20b-code-securecode")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.7)
|
| 81 |
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 82 |
```
|
| 83 |
|
| 84 |
-
---
|
| 85 |
-
|
| 86 |
## Training Details
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
| Epochs | 3 |
|
| 94 |
-
|
|
| 95 |
-
|
|
| 96 |
-
|
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
## SecureCode Model Collection
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|-------|------------|------|---------------|------|
|
| 105 |
-
| Llama 3.2 3B | 3B | Meta Llama 3.2 | 1h 5min | [scthornton/llama-3.2-3b-securecode](https://huggingface.co/scthornton/llama-3.2-3b-securecode) |
|
| 106 |
-
| Qwen Coder 7B | 7B | Qwen 2.5 Coder | 1h 24min | [scthornton/qwen-coder-7b-securecode](https://huggingface.co/scthornton/qwen-coder-7b-securecode) |
|
| 107 |
-
| CodeGemma 7B | 7B | Google CodeGemma | 1h 27min | [scthornton/codegemma-7b-securecode](https://huggingface.co/scthornton/codegemma-7b-securecode) |
|
| 108 |
-
| DeepSeek Coder 6.7B | 6.7B | DeepSeek Coder | 1h 15min | [scthornton/deepseek-coder-6.7b-securecode](https://huggingface.co/scthornton/deepseek-coder-6.7b-securecode) |
|
| 109 |
-
| CodeLlama 13B | 13B | Meta CodeLlama | 1h 32min | [scthornton/codellama-13b-securecode](https://huggingface.co/scthornton/codellama-13b-securecode) |
|
| 110 |
-
| Qwen Coder 14B | 14B | Qwen 2.5 Coder | 1h 19min | [scthornton/qwen2.5-coder-14b-securecode](https://huggingface.co/scthornton/qwen2.5-coder-14b-securecode) |
|
| 111 |
-
| StarCoder2 15B | 15B | BigCode StarCoder2 | 1h 40min | [scthornton/starcoder2-15b-securecode](https://huggingface.co/scthornton/starcoder2-15b-securecode) |
|
| 112 |
-
| **Granite 20B** | **20B** | **IBM Granite Code** | **1h 19min** | **This model** |
|
| 113 |
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
## Citation
|
| 117 |
|
| 118 |
```bibtex
|
| 119 |
-
@misc{
|
| 120 |
-
title={SecureCode
|
| 121 |
author={Thornton, Scott},
|
| 122 |
-
year={
|
| 123 |
publisher={perfecXion.ai},
|
| 124 |
-
url={https://
|
| 125 |
-
note={
|
| 126 |
}
|
| 127 |
```
|
| 128 |
|
| 129 |
-
---
|
| 130 |
-
|
| 131 |
## Links
|
| 132 |
|
| 133 |
-
- **Dataset**: [scthornton/securecode](https://huggingface.co/datasets/scthornton/securecode)
|
| 134 |
-
- **Paper**: [
|
| 135 |
-
- **Model Collection**: [
|
| 136 |
-
- **
|
| 137 |
-
- **Publisher**: [perfecXion.ai](https://perfecxion.ai)
|
| 138 |
-
|
| 139 |
-
---
|
| 140 |
|
| 141 |
## License
|
| 142 |
|
| 143 |
-
|
|
|
|
| 1 |
---
|
|
|
|
|
|
|
| 2 |
license: apache-2.0
|
| 3 |
+
base_model: ibm-granite/granite-20b-code-instruct-8k
|
|
|
|
|
|
|
|
|
|
| 4 |
tags:
|
| 5 |
+
- security
|
| 6 |
+
- cybersecurity
|
| 7 |
+
- secure-coding
|
| 8 |
+
- ai-security
|
| 9 |
+
- owasp
|
| 10 |
+
- code-generation
|
| 11 |
+
- lora
|
| 12 |
+
- fine-tuned
|
| 13 |
+
- securecode
|
| 14 |
datasets:
|
| 15 |
+
- scthornton/securecode
|
| 16 |
+
library_name: peft
|
| 17 |
+
pipeline_tag: text-generation
|
| 18 |
+
language:
|
| 19 |
+
- code
|
| 20 |
+
- en
|
| 21 |
---
|
| 22 |
|
| 23 |
# Granite 20B Code SecureCode
|
| 24 |
|
| 25 |
+
<div align="center">
|
| 26 |
|
| 27 |
+

|
| 28 |
+

|
| 29 |
+

|
| 30 |
+

|
| 31 |
|
| 32 |
+
**Security-specialized code model fine-tuned on the [SecureCode](https://huggingface.co/datasets/scthornton/securecode) dataset**
|
| 33 |
+
|
| 34 |
+
[Dataset](https://huggingface.co/datasets/scthornton/securecode) | [Paper (arXiv:2512.18542)](https://arxiv.org/abs/2512.18542) | [Model Collection](https://huggingface.co/collections/scthornton/securecode) | [perfecXion.ai](https://perfecxion.ai)
|
| 35 |
+
|
| 36 |
+
</div>
|
| 37 |
|
| 38 |
---
|
| 39 |
|
| 40 |
## What This Model Does
|
| 41 |
|
| 42 |
+
This model generates **secure code** when developers ask about building features. Instead of producing vulnerable implementations (like 45% of AI-generated code does), it:
|
| 43 |
|
| 44 |
+
- Identifies the security risks in common coding patterns
|
| 45 |
+
- Provides vulnerable *and* secure implementations side by side
|
| 46 |
+
- Explains how attackers would exploit the vulnerability
|
| 47 |
+
- Includes defense-in-depth guidance: logging, monitoring, SIEM integration, infrastructure hardening
|
| 48 |
|
| 49 |
+
The model was fine-tuned on **2,372 security training examples** covering both traditional web security (OWASP Top 10 2021) and AI/ML security (OWASP LLM Top 10 2025).
|
| 50 |
|
| 51 |
## Model Details
|
| 52 |
|
| 53 |
+
| | |
|
| 54 |
+
|---|---|
|
| 55 |
+
| **Base Model** | [Granite 20B Code Instruct 8K](https://huggingface.co/ibm-granite/granite-20b-code-instruct-8k) |
|
| 56 |
| **Parameters** | 20B |
|
| 57 |
+
| **Architecture** | GPT-BigCode |
|
| 58 |
+
| **Tier** | Tier 4: XL Model |
|
| 59 |
+
| **Method** | bf16 LoRA (no quantization) |
|
| 60 |
+
| **LoRA Rank** | 8 (alpha=16) |
|
| 61 |
+
| **Target Modules** | `attn.c_attn, attn.c_proj, mlp.c_fc, mlp.c_proj` (4 modules) |
|
| 62 |
+
| **Training Data** | [scthornton/securecode](https://huggingface.co/datasets/scthornton/securecode) (2,372 examples) |
|
| 63 |
+
| **Hardware** | NVIDIA DGX Spark GB10 (Blackwell, unified memory) |
|
|
|
|
| 64 |
|
| 65 |
+
Largest model in the collection. IBM's enterprise-grade code model with 8K context. Deepest security reasoning capabilities.
|
| 66 |
|
| 67 |
## Quick Start
|
| 68 |
|
| 69 |
```python
|
|
|
|
| 70 |
from peft import PeftModel
|
| 71 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 72 |
+
import torch
|
| 73 |
+
|
| 74 |
+
# Optional: 4-bit quantization for low-VRAM inference (training was bf16 LoRA)
|
| 75 |
+
bnb_config = BitsAndBytesConfig(
|
| 76 |
+
load_in_4bit=True,
|
| 77 |
+
bnb_4bit_quant_type="nf4",
|
| 78 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 79 |
+
)
|
| 80 |
|
| 81 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 82 |
"ibm-granite/granite-20b-code-instruct-8k",
|
| 83 |
+
quantization_config=bnb_config,
|
| 84 |
device_map="auto",
|
|
|
|
| 85 |
)
|
|
|
|
| 86 |
tokenizer = AutoTokenizer.from_pretrained("scthornton/granite-20b-code-securecode")
|
| 87 |
+
model = PeftModel.from_pretrained(base_model, "scthornton/granite-20b-code-securecode")
|
| 88 |
+
|
| 89 |
+
# Ask a security-relevant coding question
|
| 90 |
+
messages = [
|
| 91 |
+
{"role": "user", "content": "How do I implement JWT authentication with refresh tokens in Python?"}
|
| 92 |
+
]
|
| 93 |
|
| 94 |
+
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
|
| 95 |
+
outputs = model.generate(inputs, max_new_tokens=2048, temperature=0.7)
|
|
|
|
| 96 |
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
|
| 97 |
```
|
| 98 |
|
|
|
|
|
|
|
| 99 |
## Training Details
|
| 100 |
|
| 101 |
+
### Dataset
|
| 102 |
+
|
| 103 |
+
Trained on the full **[SecureCode](https://huggingface.co/datasets/scthornton/securecode)** unified dataset:
|
| 104 |
+
|
| 105 |
+
- **2,372 total examples** (1,625 web security + 747 AI/ML security)
|
| 106 |
+
- **20 vulnerability categories** across OWASP Top 10 2021 and OWASP LLM Top 10 2025
|
| 107 |
+
- **12+ programming languages** and **49+ frameworks**
|
| 108 |
+
- **4-turn conversational structure**: feature request, vulnerable/secure implementations, advanced probing, operational guidance
|
| 109 |
+
- **100% incident grounding**: every example tied to real CVEs, vendor advisories, or published attack research
|
| 110 |
+
|
| 111 |
+
### Hyperparameters
|
| 112 |
+
|
| 113 |
+
| Parameter | Value |
|
| 114 |
+
|-----------|-------|
|
| 115 |
+
| LoRA rank | 8 |
|
| 116 |
+
| LoRA alpha | 16 |
|
| 117 |
+
| LoRA dropout | 0.05 |
|
| 118 |
+
| Target modules | 4 linear layers |
|
| 119 |
+
| Quantization | None (bf16 base weights) |
|
| 120 |
+
| Learning rate | 2e-4 |
|
| 121 |
+
| LR scheduler | Cosine with 100-step warmup |
|
| 122 |
| Epochs | 3 |
|
| 123 |
+
| Per-device batch size | 1 |
|
| 124 |
+
| Gradient accumulation | 16x |
|
| 125 |
+
| Effective batch size | 16 |
|
| 126 |
+
| Max sequence length | 2048 tokens |
|
| 127 |
+
| Optimizer | adamw_torch_fused |
|
| 128 |
+
| Attention | PyTorch SDPA (fused) |
|
| 129 |
+
| Precision | bf16 |
|
| 130 |
|
| 131 |
+
**Notes:** Reduced LoRA rank (8) and max sequence length (2048) for A100 40GB memory. Gradient checkpointing with `use_reentrant=False`. Max gradient norm 1.0.
|
| 132 |
+
|
| 133 |
+
## Security Coverage
|
| 134 |
+
|
| 135 |
+
### Web Security (1,625 examples)
|
| 136 |
+
|
| 137 |
+
OWASP Top 10 2021: Broken Access Control, Cryptographic Failures, Injection, Insecure Design, Security Misconfiguration, Vulnerable Components, Authentication Failures, Software Integrity Failures, Logging/Monitoring Failures, SSRF.
|
| 138 |
+
|
| 139 |
+
Languages: Python, JavaScript, Java, Go, PHP, C#, TypeScript, Ruby, Rust, Kotlin, YAML.
|
| 140 |
+
|
| 141 |
+
### AI/ML Security (747 examples)
|
| 142 |
+
|
| 143 |
+
OWASP LLM Top 10 2025: Prompt Injection, Sensitive Information Disclosure, Supply Chain Vulnerabilities, Data/Model Poisoning, Improper Output Handling, Excessive Agency, System Prompt Leakage, Vector/Embedding Weaknesses, Misinformation, Unbounded Consumption.
|
| 144 |
+
|
| 145 |
+
Frameworks: LangChain, OpenAI, Anthropic, HuggingFace, LlamaIndex, ChromaDB, Pinecone, FastAPI, Flask, vLLM, CrewAI, and 30+ more.
|
| 146 |
|
| 147 |
## SecureCode Model Collection
|
| 148 |
|
| 149 |
+
This model is part of the **SecureCode** collection of 9 security-specialized models:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
| Model | Base | Size | Tier | HuggingFace |
|
| 152 |
+
|-------|------|------|------|-------------|
|
| 153 |
+
| Llama 3.2 SecureCode | meta-llama/Llama-3.2-3B-Instruct | 3B | Accessible | [`llama-3.2-3b-securecode`](https://huggingface.co/scthornton/llama-3.2-3b-securecode) |
|
| 154 |
+
| Gemma 4 E4B SecureCode | google/gemma-4-E4B-it | E4B (8B raw) | Accessible | [`gemma-4-e4b-securecode`](https://huggingface.co/scthornton/gemma-4-e4b-securecode) |
|
| 155 |
+
| Qwen2.5 Coder SecureCode | Qwen/Qwen2.5-Coder-7B-Instruct | 7B | Mid-size | [`qwen2.5-coder-7b-securecode`](https://huggingface.co/scthornton/qwen2.5-coder-7b-securecode) |
|
| 156 |
+
| DeepSeek Coder SecureCode | deepseek-ai/deepseek-coder-6.7b-instruct | 6.7B | Mid-size | [`deepseek-coder-6.7b-securecode`](https://huggingface.co/scthornton/deepseek-coder-6.7b-securecode) |
|
| 157 |
+
| CodeGemma SecureCode | google/codegemma-7b-it | 7B | Mid-size | [`codegemma-7b-securecode`](https://huggingface.co/scthornton/codegemma-7b-securecode) |
|
| 158 |
+
| CodeLlama SecureCode | codellama/CodeLlama-13b-Instruct-hf | 13B | Large | [`codellama-13b-securecode`](https://huggingface.co/scthornton/codellama-13b-securecode) |
|
| 159 |
+
| Qwen2.5 Coder 14B SecureCode | Qwen/Qwen2.5-Coder-14B-Instruct | 14B | Large | [`qwen2.5-coder-14b-securecode`](https://huggingface.co/scthornton/qwen2.5-coder-14b-securecode) |
|
| 160 |
+
| StarCoder2 SecureCode | bigcode/starcoder2-15b-instruct-v0.1 | 15B | Large | [`starcoder2-15b-securecode`](https://huggingface.co/scthornton/starcoder2-15b-securecode) |
|
| 161 |
+
| Granite 20B Code SecureCode | ibm-granite/granite-20b-code-instruct-8k | 20B | XL | [`granite-20b-code-securecode`](https://huggingface.co/scthornton/granite-20b-code-securecode) |
|
| 162 |
+
|
| 163 |
+
Choose based on your deployment constraints: **3B** for edge/mobile, **7B** for general use, **13B-15B** for deeper reasoning, **20B** for maximum capability.
|
| 164 |
+
|
| 165 |
+
## SecureCode Dataset Family
|
| 166 |
+
|
| 167 |
+
| Dataset | Examples | Focus | Link |
|
| 168 |
+
|---------|----------|-------|------|
|
| 169 |
+
| **SecureCode** | 2,372 | Unified (web + AI/ML) | [scthornton/securecode](https://huggingface.co/datasets/scthornton/securecode) |
|
| 170 |
+
| SecureCode Web | 1,625 | Web security (OWASP Top 10 2021) | [scthornton/securecode-web](https://huggingface.co/datasets/scthornton/securecode-web) |
|
| 171 |
+
| SecureCode AI/ML | 747 | AI/ML security (OWASP LLM Top 10 2025) | [scthornton/securecode-aiml](https://huggingface.co/datasets/scthornton/securecode-aiml) |
|
| 172 |
+
|
| 173 |
+
## Intended Use
|
| 174 |
+
|
| 175 |
+
**Use this model for:**
|
| 176 |
+
- Training AI coding assistants to write secure code
|
| 177 |
+
- Security education and training
|
| 178 |
+
- Vulnerability research and secure code review
|
| 179 |
+
- Building security-aware development tools
|
| 180 |
+
|
| 181 |
+
**Do not use this model for:**
|
| 182 |
+
- Offensive exploitation or automated attack generation
|
| 183 |
+
- Circumventing security controls
|
| 184 |
+
- Any activity that violates the base model's license
|
| 185 |
+
|
| 186 |
+
## Changelog
|
| 187 |
+
|
| 188 |
+
- **2026-07 (v2, current)**: Retrained on the audited SecureCode release (**2,372 examples**: 1,625 web + 747 AI/ML) using **bf16 LoRA on an NVIDIA DGX Spark GB10 (Blackwell)**. Same LoRA shape and hyperparameters as v1, except: no quantization during training, optimizer adamw_torch_fused instead of paged_adamw_8bit, PyTorch SDPA attention, dynamic per-batch padding. A new family member, [gemma-4-e4b-securecode](https://huggingface.co/scthornton/gemma-4-e4b-securecode), was added in this refresh.
|
| 189 |
+
- **v1 (2026-02)**: QLoRA 4-bit NF4 on NVIDIA A100 40GB, trained on the pre-audit 2,185-example dataset.
|
| 190 |
|
| 191 |
## Citation
|
| 192 |
|
| 193 |
```bibtex
|
| 194 |
+
@misc{thornton2026securecode,
|
| 195 |
+
title={SecureCode: A Production-Grade Multi-Turn Dataset for Training Security-Aware Code Generation Models},
|
| 196 |
author={Thornton, Scott},
|
| 197 |
+
year={2026},
|
| 198 |
publisher={perfecXion.ai},
|
| 199 |
+
url={https://huggingface.co/datasets/scthornton/securecode},
|
| 200 |
+
note={arXiv:2512.18542}
|
| 201 |
}
|
| 202 |
```
|
| 203 |
|
|
|
|
|
|
|
| 204 |
## Links
|
| 205 |
|
| 206 |
+
- **Dataset**: [scthornton/securecode](https://huggingface.co/datasets/scthornton/securecode)
|
| 207 |
+
- **Research Paper**: [arXiv:2512.18542](https://arxiv.org/abs/2512.18542)
|
| 208 |
+
- **Model Collection**: [huggingface.co/collections/scthornton/securecode](https://huggingface.co/collections/scthornton/securecode)
|
| 209 |
+
- **Author**: [perfecXion.ai](https://perfecxion.ai)
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
## License
|
| 212 |
|
| 213 |
+
This model is released under the **apache-2.0** license (inherited from the base model). The training dataset ([SecureCode](https://huggingface.co/datasets/scthornton/securecode)) is licensed under **CC BY-NC-SA 4.0**.
|