Skip to content

Latest commit

Β 

History

33 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Quantify Credibility: LLM Uncertainty Quantification

PyTorch HuggingFace License

A comprehensive research toolkit for quantifying uncertainty and credibility in Large Language Model outputs using Mutual Information (MI) estimation, iterative prompting, and NLI-based semantic clustering. Implements methods from "To Believe or Not to Believe Your LLM" (DeepMind, 2024).

🎯 Key Features

  • MI-Based Uncertainty Quantification: Measure epistemic uncertainty through mutual information between iterative prompts
  • Expected Calibration Error (ECE): Evaluate how well model confidence aligns with actual accuracy
  • Multiple Baseline Methods: Greedy, self-consistency, semantic-entropy, self-verification
  • NLI Semantic Clustering: Group semantically equivalent answers using DeBERTa-MNLI
  • Multi-Benchmark Support: ARC, OpenBookQA, TruthfulQA, SQuAD v2, TriviaQA

πŸ“ Repository Structure

quantify_credibility/
β”œβ”€β”€ llm-belief-mi-test/            # πŸ§ͺ Main MI evaluation framework
β”‚   β”œβ”€β”€ llm_belief_mi_test/        # Core Python package
β”‚   β”‚   β”œβ”€β”€ cli.py                 # Command-line interface
β”‚   β”‚   β”œβ”€β”€ calibration.py         # ECE & evaluation (with baselines)
β”‚   β”‚   β”œβ”€β”€ mi_estimator.py        # MI computation (listing/plugin)
β”‚   β”‚   β”œβ”€β”€ iterative_prompting.py # Iterative prompting chains
β”‚   β”‚   β”œβ”€β”€ llm_client_local.py    # Local Llama client
β”‚   β”‚   β”œβ”€β”€ datasets.py            # Dataset loaders
β”‚   β”‚   └── cache.py               # SQLite caching
β”‚   β”œβ”€β”€ scripts/                   # Utility scripts
β”‚   β”‚   β”œβ”€β”€ test_gpu_setup.py      # GPU verification
β”‚   β”‚   β”œβ”€β”€ compare_results.py     # Results comparison
β”‚   β”‚   └── plot_results.py        # Visualization
β”‚   β”œβ”€β”€ outputs/                   # Results & logs
β”‚   └── docs/                      # Detailed documentation
β”‚
β”œβ”€β”€ llm-belief-mi-repro/           # πŸ“Š Paper reproduction experiments
β”‚   β”œβ”€β”€ llm_belief_mi_repro/       # Reproduction code
β”‚   └── outputs/                   # Reproduction results
β”‚
β”œβ”€β”€ nli-semantic-clustering/       # πŸ” NLI clustering module
β”‚   β”œβ”€β”€ nli_clustering/            # Core NLI package
β”‚   β”‚   β”œβ”€β”€ core.py               # DeBERTa-MNLI model & clustering
β”‚   β”‚   └── utils.py              # Evaluation metrics
β”‚   β”œβ”€β”€ scripts/
β”‚   β”‚   └── threshold_sweep.py    # Threshold optimization
β”‚   └── data/                     # Sample data
β”‚
└── theory/                        # πŸ“š Theoretical foundations
    β”œβ”€β”€ MI_ALGORITHMS.md          # MI estimator algorithms
    β”œβ”€β”€ MI_ECE_FORMULAS.md        # Mathematical formulas
    └── MI_ESTIMATOR_EXAMPLE.md   # Worked examples

πŸš€ Quick Start

Installation

cd quantify_credibility/llm-belief-mi-test
pip install -r requirements.txt

# Set HuggingFace token for Llama models
export HF_TOKEN="hf_YOUR_TOKEN_HERE"

Verify Setup

python scripts/test_gpu_setup.py

Run Quick Test

python -m llm_belief_mi_test.cli \
  --method mi \
  --dataset arc-easy --limit 5 \
  --k 10 --n 2 --temperature 0.9 \
  --load-in-4bit --max-tokens 10 \
  --answer-format strict \
  --output outputs/results/test_quick.csv

πŸ“Š Evaluation Methods

1. MI Method (Paper's Main Contribution)

Uses iterative prompting to estimate mutual information between response chains.

python -m llm_belief_mi_test.cli \
  --method mi \
  --dataset arc-challenge --limit 500 \
  --k 10 --n 2 --temperature 0.9 \
  --load-in-4bit --max-tokens 10 \
  --answer-format strict \
  --output outputs/results/mi_500.csv

2. Baseline Methods

# Greedy (single decode)
python -m llm_belief_mi_test.cli --method greedy --dataset arc-easy ...

# Self-Consistency (k samples + majority vote)
python -m llm_belief_mi_test.cli --method self-consistency --k 10 ...

# Semantic Entropy (F1 clustering + entropy)
python -m llm_belief_mi_test.cli --method semantic-entropy --k 10 ...

# Self-Verification (samples + verification query)
python -m llm_belief_mi_test.cli --method self-verification --k 10 ...

πŸ”¬ Supported Datasets

Dataset Type Size Key Metric
ARC-Challenge MCQ 1,172 Accuracy
ARC-Easy MCQ 2,376 Accuracy
OpenBookQA MCQ 500 Accuracy
TruthfulQA MC1 MCQ 817 Accuracy
TruthfulQA MC2 MCQ (multi-true) 817 Accuracy
SQuAD v2 Extractive QA 11,873 EM/F1
TriviaQA Open-domain QA 87,622 EM/F1

πŸ“ˆ Key Metrics

Expected Calibration Error (ECE)

Measures how well model confidence correlates with actual accuracy:

ECE = Ξ£ (n_b / N) |accuracy_b - confidence_b|

Lower ECE = Better Calibration (main contribution of MI method)

Mutual Information (MI)

Captures epistemic uncertainty through iterative prompting:

MI(Y₁; Yβ‚‚; ...; Yβ‚™) = Ξ£α΅’ H(Yα΅’) - H(Y₁,...,Yβ‚™)

Higher MI = More Uncertainty

πŸŽ›οΈ Key Parameters

Parameter Description Default
--k Number of independent chains 10
--n Chain length (pseudo-joint dimension) 2
--temperature Sampling temperature 0.9
--mi-method MI estimator (listing/plugin) listing
--confidence-method MI→confidence mapping inverse
--answer-format Output format (strict/codeblock) strict

πŸ“Š Expected Results

Key Insight: MI method is designed for better calibration (lower ECE), not necessarily higher accuracy.

Method ARC-C Acc ARC-C ECE ARC-E Acc ARC-E ECE
Greedy ~65% ~0.12 ~80% ~0.08
Self-Consistency ~67% ~0.10 ~82% ~0.06
MI Method ~66% ~0.05 ~81% ~0.04

πŸ” NLI Semantic Clustering

The NLI module groups semantically equivalent answers to reduce spurious uncertainty:

cd nli-semantic-clustering
python scripts/threshold_sweep.py \
  --log-dir ../llm-belief-mi-test/outputs/logs/triviaqa_mi_200 \
  --thresholds 0.3 0.4 0.5 0.6 0.7 \
  --correctness-based \
  --output results/threshold_sweep.json

Dual-Mode System

  • Clustering: Strict bidirectional entailment (A ↔ B)
  • Grading: Loose unidirectional (A β†’ B) + substring matching

πŸ“š Theoretical Background

MI Estimation Algorithms

Plugin Estimator (simple):

MI = Ξ£α΅’ H(Yα΅’) - H(Y₁,...,Yβ‚™)
H(X) = -Ξ£ p(x) log p(x)

Listing Estimator (paper's Algorithm 1):

MI = Ξ£ ΞΌΜ‚ Β· log((ΞΌΜ‚ + γ₁) / (ΞΌΜ‚_prod + Ξ³β‚‚))
γ₁, Ξ³β‚‚ = 1/k  (regularization)

See theory/MI_ALGORITHMS.md for detailed explanations.

βš™οΈ Hardware Requirements

Minimum (4-bit quantization)

  • GPU: 12GB VRAM (RTX 3060, RTX 4060 Ti)
  • RAM: 16GB

Recommended

  • GPU: 16GB+ VRAM (RTX 4080, A4000+)
  • RAM: 32GB

Time Estimates (k=10, n=2)

Dataset Examples Time (A100)
ARC-Challenge 1,172 ~3-4 hours
ARC-Easy 2,376 ~6-7 hours
OpenBookQA 500 ~1.5 hours

πŸ“– Documentation

πŸ“š References

Datasets

πŸ“ License

MIT License

🀝 Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.


Research toolkit for quantifying LLM uncertainty and calibration.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages