gravitee-io-labs/Llama-Prompt-Guard-2-onnx

ipeline to convert Llama-Prompt-Guard-2 models into ONNX format

Python

1

2 commits

updated Sep 22, 2025

See the code

README

Llama-Prompt-Guard-2-onnx

This repository provides a pipeline to convert Llama-Prompt-Guard-2 models into ONNX format, perform optional optimizations and quantization, and evaluate performance on classification datasets.

πŸ“¦ Features

  • βœ… Convert AutoModelForSequenceClassification models to ONNX.
  • πŸ”„ Optimize the exported ONNX models.
  • πŸ“‰ Quantize ONNX models to reduce size and improve inference speed.
  • πŸ“Š Evaluate accuracy, precision, recall, F1-score, and AUC-ROC.
  • πŸ§ͺ Compare performance between full and quantized models.

🧠 Built With

πŸš€ Usage

1. Export a Hugging Face Model

pip install -r requirements.txt
python convert_to_onnx.py \
  --model-name meta-llama/Llama-Prompt-Guard-2-22M \
  --export-onnx \
  --quantize-onnx \
  --update-config

This will:

  • Download the model and tokenizer
  • Save them to gravitee-io/Llama-Prompt-Guard-2-22M-onnx
  • Export to ONNX (model.onnx)
  • Optimize and quantize (model.optim.onnx and model.quant.onnx)
  • Update config.json with id2label and label2id

2. Evaluate the Model

python evaluate.py \
  --dataset jackhhao/jailbreak-classification \
  --model-path gravitee-io/Llama-Prompt-Guard-2-22M-onnx \
  --quantized \
  --plot-roc

You can toggle --quantized to evaluate either the full or quantized model.

πŸ§ͺ Evaluation Results

ModelAccuracyPrecisionRecallF1 ScoreAUC-ROC
Llama-Prompt-Guard-2-22M0.95640.98880.92490.95580.9234
Llama-Prompt-Guard-2-22M-q0.95790.99670.92040.94490.9180
Llama-Prompt-Guard-2-86M0.98010.99840.96250.98010.9519
Llama-Prompt-Guard-2-86M-q0.89891.00000.80180.890.7452

πŸ“₯ Dataset

We use jackhhao/jailbreak-classification for the evaluation

πŸ€— Usage

from transformers import AutoTokenizer
from optimum.onnxruntime import ORTModelForSequenceClassification
import numpy as np

# Load model and tokenizer using optimum
model = ORTModelForSequenceClassification.from_pretrained("gravitee-io/Llama-Prompt-Guard-2-22M-onnx", file_name="model.quant.onnx")
tokenizer = AutoTokenizer.from_pretrained("gravitee-io/Llama-Prompt-Guard-2-22M-onnx")

# Tokenize input
text = "Your comment here"
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)

# Run inference
outputs = model(**inputs)
logits = outputs.logits

# Optional: convert to probabilities
probs = 1 / (1 + np.exp(-logits))
print(probs)

You can find and use the quantized ONNX model from the Hugging:xFace repository:

Contributors

remisultan

2 commits

gravitee-io-labs/Llama-Prompt-Guard-2-onnx

ipeline to convert Llama-Prompt-Guard-2 models into ONNX format

Python

1

2 commits

updated Sep 22, 2025

See the code

README

Llama-Prompt-Guard-2-onnx

This repository provides a pipeline to convert Llama-Prompt-Guard-2 models into ONNX format, perform optional optimizations and quantization, and evaluate performance on classification datasets.

πŸ“¦ Features

  • βœ… Convert AutoModelForSequenceClassification models to ONNX.
  • πŸ”„ Optimize the exported ONNX models.
  • πŸ“‰ Quantize ONNX models to reduce size and improve inference speed.
  • πŸ“Š Evaluate accuracy, precision, recall, F1-score, and AUC-ROC.
  • πŸ§ͺ Compare performance between full and quantized models.

🧠 Built With

πŸš€ Usage

1. Export a Hugging Face Model

pip install -r requirements.txt
python convert_to_onnx.py \
  --model-name meta-llama/Llama-Prompt-Guard-2-22M \
  --export-onnx \
  --quantize-onnx \
  --update-config

This will:

  • Download the model and tokenizer
  • Save them to gravitee-io/Llama-Prompt-Guard-2-22M-onnx
  • Export to ONNX (model.onnx)
  • Optimize and quantize (model.optim.onnx and model.quant.onnx)
  • Update config.json with id2label and label2id

2. Evaluate the Model

python evaluate.py \
  --dataset jackhhao/jailbreak-classification \
  --model-path gravitee-io/Llama-Prompt-Guard-2-22M-onnx \
  --quantized \
  --plot-roc

You can toggle --quantized to evaluate either the full or quantized model.

πŸ§ͺ Evaluation Results

ModelAccuracyPrecisionRecallF1 ScoreAUC-ROC
Llama-Prompt-Guard-2-22M0.95640.98880.92490.95580.9234
Llama-Prompt-Guard-2-22M-q0.95790.99670.92040.94490.9180
Llama-Prompt-Guard-2-86M0.98010.99840.96250.98010.9519
Llama-Prompt-Guard-2-86M-q0.89891.00000.80180.890.7452

πŸ“₯ Dataset

We use jackhhao/jailbreak-classification for the evaluation

πŸ€— Usage

from transformers import AutoTokenizer
from optimum.onnxruntime import ORTModelForSequenceClassification
import numpy as np

# Load model and tokenizer using optimum
model = ORTModelForSequenceClassification.from_pretrained("gravitee-io/Llama-Prompt-Guard-2-22M-onnx", file_name="model.quant.onnx")
tokenizer = AutoTokenizer.from_pretrained("gravitee-io/Llama-Prompt-Guard-2-22M-onnx")

# Tokenize input
text = "Your comment here"
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)

# Run inference
outputs = model(**inputs)
logits = outputs.logits

# Optional: convert to probabilities
probs = 1 / (1 + np.exp(-logits))
print(probs)

You can find and use the quantized ONNX model from the Hugging:xFace repository:

Contributors

remisultan

2 commits

Languages

Python

100.0%