Spectral diagnostics for trust in LLMs
Project description
Spectral Trust Framework
A Graph Signal Processing (GSP) framework for measuring the trustworthiness of LLM internal representations.
spectral_trust constructs dynamic graphs from attention patterns and applies spectral analysis (eigenvalues, Dirichlet energy) to detect hallucinations, quantify uncertainty, and map the "smoothness" of reasoning flows.
What is it?
By treating the transformer's attention mechanism as a graph and the hidden states as signals on that graph, we can calculate rigorous mathematical metrics:
- Dirichlet Energy: How much the signal varies across connected tokens (proxy for conflict/uncertainty).
- Smoothness Index: Normalized energy indicating how well the representation aligns with the attention structure.
- Fiedler Value: Algebraic connectivity of the attention graph.
- HFER (High-Frequency Energy Ratio): Energy concentration in high-frequency spectral components.
Features
- Plug-and-Play: Works out-of-the-box with
Llama-3,Mistral,Qwen,Gemma, andPhi. - Offline Ready:
--offlinemode to use cached models without internet access. - Spectral Metrics: Automatically computes Energy, Entropy, Fiedler Value, HFER, and Smoothness.
- Robustness Tools: Includes hooks for head ablation and residual patching.
Structure
src/spectral_trust/: Core package source code.notebooks/: Jupyter notebooks for demonstration.examples/: Minimal example scripts.dist/: Wheel and source distributions.
Installation
pip install spectral_trust
# OR install from source
pip install -e .
Usage
CLI Power Tool
Analyze a sentence (uses cuda if available):
gsp-cli analyze --text "The capital of France is Paris." --model llama-3.1-8b
Offline Mode (no internet required):
gsp-cli analyze --text "Refactoring is fun." --model llama-3.2-1b --offline
Python API
from spectral_trust import GSPDiagnosticsFramework, GSPConfig
config = GSPConfig(model_name="llama-3.2-1b", device="cuda", local_files_only=True)
with GSPDiagnosticsFramework(config) as framework:
framework.instrumenter.load_model("meta-llama/Llama-3.2-1B")
results = framework.analyze_text("The capital of France is Paris.")
print(f"Smoothness: {results['layer_diagnostics'][-1].smoothness_index:.4f}")
Compare Two Texts
Compare the spectral properties of two different inputs side-by-side:
python -m spectral_trust.cli compare \
--text1 "Total confidence: The capital of France is Paris." \
--text2 "Low confidence: I think the capital might be Paris." \
--model llama-3.2-1b
This will generate a comparison plot overlaying the metrics for both texts.
Multi-Run Analysis (Stochastic)
Run the analysis multiple times (useful with sampling enabled) to see metric stability:
python -m spectral_trust.cli analyze \
--text "The capital of France is Paris." \
--runs 5 \
--temperature 0.7
Advanced GSP Options
For rigorous spectral graph analysis, you may want to exclude self-attention loops (the diagonal) to match standard spectral graph theory (where $A_{ii}=0$).
- Default: Self-loops kept. Faithful to Transformer mechanics. Fiedler values $\approx 1.0$.
--remove_self_loops: Self-loops removed. Faithful to Graph Signal Processing theory. Fiedler values $\approx 2.0$ (for connected graphs). Better for measuring pure token-to-token mixing.
gsp-cli analyze --text "..." --remove_self_loops
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spectral_trust-0.1.2.tar.gz.
File metadata
- Download URL: spectral_trust-0.1.2.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0b057f203057fca28555325f448727a3135de543a6d122f39e339e5b32d7ef2
|
|
| MD5 |
6a52b3d3fff6f60e755599b616149bfa
|
|
| BLAKE2b-256 |
e2874a164f7074c7e0108a890b8209f98997df22efbcb9c36d2cf088cf733238
|
File details
Details for the file spectral_trust-0.1.2-py3-none-any.whl.
File metadata
- Download URL: spectral_trust-0.1.2-py3-none-any.whl
- Upload date:
- Size: 23.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7da08281afeb40ef81f9f94cd6d6ea338d59cd33221023df06cf7997f1cf425
|
|
| MD5 |
f9b844183029c6610182fd203f2e798a
|
|
| BLAKE2b-256 |
7d552edadb956390cd8580c4a85b92ea01bc91bed7624dae7f00bfdbf7e6473f
|