This toolkit provides comprehensive model validation capabilities using KL divergence metrics to compare two models. It's designed to evaluate the similarity between model outputs across different optimization techniques, frameworks, and hardware backends.
The toolkit measures output similarity between models using KL (Kullback-Leibler) divergence, which quantifies how one probability distribution differs from another. Lower KL divergence values indicate more similar model outputs.
Primary Use Cases:
- Model Optimization Validation - Verify that optimized models (quantization, pruning) maintain output quality
- Framework Comparison - Compare Hugging Face models vs ONNX Runtime GenAI models
- Precision Analysis - Evaluate FP16 vs INT4 vs INT8 model outputs
- Execution Provider Testing - Test different EP implementations (CUDA, DirectML, CPU, TensorRT)
| Script | Purpose | Comparison Modes |
|---|---|---|
compute_kl_divergence.py |
Two-model sequential comparison | • HF vs GenAI • GenAI vs GenAI (same EP) • GenAI vs HF • HF vs HF |
- Wikitext-2 test split for consistent evaluation across all models
- Automatic dataset loading and preprocessing via HuggingFace datasets
pip install -r requirements.txtNote: Install torch with CUDA for faster inference: "pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu129"
Install one of the following based on your hardware:
# For CUDA
pip install onnxruntime-genai-cuda
# For DirectML support
pip install onnxruntime-genai-directml
# For CPU
pip install onnxruntime-genaipython compute_kl_divergence.py \
--model1 "meta-llama/Llama-3.1-8B-Instruct" --model1_type hf \
--model2 "G:\models\genai_model" --model2_type genai \
--device cuda \
--output results.jsonpython compute_kl_divergence.py \
--model1 "G:\models\genai_fp16" --model1_type genai \
--model2 "G:\models\genai_int4" --model2_type genai \
--output fp16_vs_int4.jsonpython compute_kl_divergence.py \
--model1 "meta-llama/Llama-3.1-8B-Instruct" --model1_type hf \
--model2 "G:\models\genai_model" --model2_type genai \
--device cuda \
--output results.json \
--debug # Enables verbose loggingRequired Parameters:
| Parameter | Description | Values |
|---|---|---|
--model1 |
Path to first model | Local path or HF Hub identifier |
--model1_type |
Type of first model | hf, genai |
--model2 |
Path to second model | Local path or HF Hub identifier |
--model2_type |
Type of second model | hf, genai |
Optional Parameters:
| Parameter | Description | Default |
|---|---|---|
--device |
Device for HF model inference | cuda |
--output |
Output JSON file path | None (prints to console) |
--debug |
Enable verbose debug output | False |
Model Path Formats:
- HF models:
- Hub identifier:
meta-llama/Llama-3.1-8B-Instruct - Local path:
F:\shared\Llama-3.1-8B-Instruct
- Hub identifier:
- GenAI models:
- Local path only:
G:\models\genai_model
- Local path only:
- Lower is better: Smaller KL divergence = more similar outputs
- Relative comparison: Compare against baseline (e.g., HF FP32)
Error:
RuntimeError: CUDA out of memory
Solutions:
- Use CPU for HF model:
--device cpu - Close other applications using GPU
- Try smaller batch size (modify code if needed)
- Ensure only one model loads at a time (script should handle this)
Error:
[INFO] Comparing two GenAI models (same execution provider)
Note: This is informational. GenAI vs GenAI comparisons require same EP.
Solution: Ensure both models were created for the same execution provider.