A high-performance NLP evaluation metrics library with a Rust core.
Project description
BlazeMetrics 🔥
BlazeMetrics is a Python library designed to be the fastest implementation of standard NLP evaluation metrics, powered by a highly optimized Rust core. It leverages Rust's performance, memory safety, and true parallelism to offer significant speedups over pure Python implementations, especially on large datasets.
✨ Key Features
- Blazing Fast: Core logic is written in Rust, compiled to native code, and parallelized with Rayon to use all available CPU cores.
- NumPy Integration: Efficiently handles numerical data like embeddings via NumPy, with matrix operations accelerated by Rust's
ndarray. - No GIL: CPU-bound tasks run on the Rust backend without being constrained by Python's Global Interpreter Lock (GIL).
- Simple API: A clean, intuitive API that feels familiar to Python developers.
- Extensible: Designed with a clear path for adding new, high-performance metrics.
- New: LLM Guardrails: Ultra-fast guardrails (blocklists, regex policies, PII redaction, lightweight safety scoring) implemented in Rust for streaming and batch workflows.
- New: LLM Guardrails++: JSON Schema validation & auto-repair, prompt-injection/jailbreak heuristics, Unicode spoof detection, and ANN-like unsafe similarity — all blazing fast.
🚀 Installation
End users (pip install)
Most users can just install from PyPI and do not need Rust:
pip install blazemetrics
Prebuilt wheels are published for Linux (manylinux2014), macOS, and Windows across Python 3.8–3.12 via CI. On supported platforms, pip will download a wheel and skip any Rust compilation. If you still see a build step, it likely means a wheel for your exact Python/OS/arch wasn’t available yet.
If you want to enforce wheel-only installs to avoid any local build attempts:
pip install --only-binary :all: blazemetrics
Tip: If
pipfalls back to building from source, it will be noticeably slower. Prefer wheels when possible.
Developers/contributors (from source)
If you are developing or installing from a fresh clone, you will need the Rust toolchain to build the native extension. Use the dev requirements to get build tooling:
# Install Rust (one time)
curl --proto '=https' --tlsv1.2 -sSf https://rustup.rs | sh
# From a cloned repo
pip install -r requirements-dev.txt
Alternatively, editable install directly (also requires Rust):
pip install -e .
Maintainers: building wheels
We use GitHub Actions to build wheels for Linux, macOS, and Windows and publish them on tagged releases. To build locally or for custom targets:
# Build wheels (example for Linux manylinux2014)
maturin build --release --compatibility manylinux2014 -o dist
# Upload
maturin upload dist/*
⚡ Quickstart
The API is straightforward. Provide a list of candidate strings and a list of reference lists.
import numpy as np
from blazemetrics import rouge_score, bleu, bert_score_similarity, chrf_score, token_f1, jaccard, meteor, wer
candidates = ["the cat sat on the mat", "the dog ate the homework"]
references = [
["the cat was on the mat"],
["the dog did my homework"]
]
# ROUGE
rouge_2_scores = rouge_score(candidates, references, score_type="rouge_n", n=2)
rouge_l_scores = rouge_score(candidates, references, score_type="rouge_l")
# BLEU
bleu_scores = bleu(candidates, references)
# chrF
chrf_scores = chrf_score(candidates, references, max_n=6, beta=2.0)
# Token-level metrics
print(token_f1(candidates, references))
print(jaccard(candidates, references))
# METEOR-lite and WER
print(meteor(candidates, references))
print(wer(candidates, references))
# BERTScore similarity kernel (requires embeddings)
cand_embeddings = np.random.rand(5, 768).astype(np.float32)
ref_embeddings = np.random.rand(8, 768).astype(np.float32)
print(bert_score_similarity(cand_embeddings, ref_embeddings))
🛡️ LLM Guardrails (New)
High-performance guardrails accelerated by Rust and parallelized with Rayon. Ideal for streaming moderation, batch post-processing, and preflight checks.
- Blocklist matching via Aho–Corasick (case-insensitive option)
- Regex policy checks (precompiled DFA)
- PII redaction (email, phone, card, SSN)
- Lightweight safety score heuristic (hate/sexual/violence/self-harm cues)
- JSON Schema validation with best-effort auto-repair
- Prompt-injection / jailbreak heuristics and Unicode spoofing detection
- ANN-like unsafe similarity: fast cosine max vs exemplar bank
from blazemetrics import Guardrails, guardrails_check
texts = [
"My email is alice@example.com and my SSN is 123-45-6789.",
"I will bomb the building.",
]
gr = Guardrails(
blocklist=["bomb", "terror"],
regexes=[r"\b\d{3}-\d{2}-\d{4}\b"],
case_insensitive=True,
redact_pii=True,
safety=True,
json_schema='{"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}'
)
print(gr.check(texts))
Streaming guardrails and enforcement
- Sync token monitoring:
monitor_tokens_sync(tokens_iter, rails, every_n_tokens=25) - Async token monitoring:
monitor_tokens_async(async_tokens, rails, every_n_tokens=25) - Multiprocessing batch mapper:
map_large_texts(texts, rails, processes=..., chunk_size=...) - Enforcement wrapper:
enforce_stream_sync(tokens_iter, rails, replacement="[BLOCKED]", safety_threshold=0.6)
Examples:
examples/openai_stream_guardrails.py(OpenAI Chat Completions streaming)examples/claude_stream_guardrails.py(Anthropic Claude streaming)
⚙️ Parallelism Controls (Rayon overhead vs throughput)
The Rust core uses Rayon to parallelize CPU-heavy work. Parallelism isn’t always faster on very small batches due to scheduling overhead. You can control this globally:
-
Environment variables (affects all functions):
BLAZEMETRICS_PARALLEL:1to enable (default),0to disable
-
BLAZEMETRICS_PAR_THRESHOLD: minimum batch size to parallelize (default512) -
Python API:
from blazemetrics import set_parallel, get_parallel, set_parallel_threshold, get_parallel_threshold
set_parallel(True) # enable or disable
set_parallel_threshold(512) # switch to sequential below threshold
print(get_parallel(), get_parallel_threshold())
- Benchmark CLI (for experiments):
python examples/benchmark.py --n 5000 --repeat 3 --warmup 1 --parallel 1 --par-threshold 512
python examples/benchmark.py --n 200 --repeat 3 --warmup 1 --parallel 0
Guidance:
- Set a higher threshold or disable parallelism for small inputs (e.g., streaming with tiny micro-batches).
- Leave parallelism on for large datasets to maximize throughput.
👷 Batch Workflow Example
See examples/batch_workflow.py for integrating metrics into training/evaluation loops. It demonstrates:
- Computing batch metrics efficiently in Rust
- Aggregating per-epoch metrics
- Writing results to
training_metrics.csv
Run:
python examples/batch_workflow.py
📈 Live Monitoring Example (Production-like)
See examples/live_monitoring.py for a simulated streaming setup with a rolling window and alerts.
- Maintains a 100-sample window
- Computes a fast subset of metrics (
BLEU,ROUGE-1,chrF,WER) - Emits alerts on threshold breaches (latency-friendly)
Run:
python examples/live_monitoring.py
📊 Benchmarking
See examples/benchmark.py for a fair, reproducible benchmark comparing blazemetrics with popular Python implementations (when installed):
- ROUGE (
rouge-score), BLEU (nltk), chrF (sacrebleu), METEOR (nltk), WER (jiwer), BERTScore (bert-score), and MoverScore (moverscore_v2) - Cleanly skips baselines if dependencies or resources are missing
- Saves a single combined overview chart at
examples/images/benchmark_overview.png
Parallelism flags available in the benchmark:
python examples/benchmark.py --parallel {0,1} --par-threshold 512
📓 Showcase Notebooks (Open in Colab)
Open the end-to-end showcase notebooks directly in Colab:
- 01 — Installation and Setup:
- 02 — Core Metrics Showcase:
- 03 — Guardrails Showcase:
- 04 — Streaming Monitoring:
- 05 — Production Workflows:
- 06 — Performance Benchmarking:
🔧 How to Add a New Metric
blazemetrics is designed to be easily extensible. To add your own custom metric:
- Implement the logic in Rust under
src/, using Rayon for parallel batch processing. - Expose a
#[pyfunction]insrc/lib.rs, releasing the GIL for CPU-bound work. - Rebuild:
pip install -e .ormaturin develop.
⚖️ License
This project is licensed under the MIT License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
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 blazemetrics-0.1.3-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: blazemetrics-0.1.3-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
069061225299171ad3a9aec3976b8dd6b053e8da8dba35730bdca051cb9ab6f5
|
|
| MD5 |
9bba63f91ebf2a5880c5a851b6c80e06
|
|
| BLAKE2b-256 |
f6a7145f06d26373bd4207850daa03f0c3b7ec12e9cdb5d86849a863f9d9b91c
|
File details
Details for the file blazemetrics-0.1.3-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: blazemetrics-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec56af9248eed0790edca6f21056f2c49f0a7e4dd24903046d8ce3f495ecf6fd
|
|
| MD5 |
187f42599f0e68615392ff2b10027ec1
|
|
| BLAKE2b-256 |
b7835e11644c4467f357a56fbc29c73c45274148e119a4ba6be598476c9e6395
|
File details
Details for the file blazemetrics-0.1.3-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: blazemetrics-0.1.3-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab572296ec23cce3f56f4b1865caf37534db4bb09ae9c374cd75daace586f791
|
|
| MD5 |
c24d4497f6c11157df4753083f96e4a2
|
|
| BLAKE2b-256 |
72ab987ac5c7c7893a9b2b99fd3c5e2f562414b85a2dc0e0c7a5aec715de6157
|
File details
Details for the file blazemetrics-0.1.3-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: blazemetrics-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82ce21e76512f4a73b8eeb9d794218127cedcd4c09b1fe8d64e7634e09ce1c26
|
|
| MD5 |
b14a6c54284f7ba4d5c6e048e8f77be6
|
|
| BLAKE2b-256 |
34749cc263b0328196a827ff9045466d91f54c0f1cfa7d06a8bc507e40daa752
|
File details
Details for the file blazemetrics-0.1.3-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: blazemetrics-0.1.3-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32ebcccda6acd93b78d920a4d6936c7163385c7868d9abf704c179d7e916f4c9
|
|
| MD5 |
067f8577064a970bcff30ad097cc7e18
|
|
| BLAKE2b-256 |
88ac4ed17b00b251923d5d5b7b1799c63982341bc94742185d02244ba643e97c
|
File details
Details for the file blazemetrics-0.1.3-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: blazemetrics-0.1.3-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.3 MB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bbfbed440721fa3c8c3e2c0d22406b6890b932c3f6055583f07ee3198872d6f2
|
|
| MD5 |
d4a758cd6fa50b390f1a09b6812e7027
|
|
| BLAKE2b-256 |
8712a3d035f77becf0023db1a2a8b9454e2f458f99cc21f2af5a2c4607967a74
|
File details
Details for the file blazemetrics-0.1.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: blazemetrics-0.1.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.9.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7359e89efa97e16f14f8bc0f70050e9ae118c2b5ba6d2af71c4290ab7c9d3b1
|
|
| MD5 |
93e8e737a8b4ea9dc68022733e4d1568
|
|
| BLAKE2b-256 |
a60f175039c1e811306ff7d20366b0bca05424253cef2f53d24da1aecf0ab920
|