A reimplementation of the BERTScore metric optimized for modern inference workflows.
Project description
Modern BERTScore for Fast Inference
Modern-BERT-Score is a reimplementation of the BERTScore metric introduced by Zhang et al., 2019, optimized for modern inference workflows using SentenceTransformers and vLLM.
This library provides fast, GPU-accelerated scoring for text generation evaluation, making BERTScore practical for large-scale inference tasks.
⚡ Features
- Fast, efficient computation with optional vLLM support
- Compatible with all Hugging Face transformer models
- Supports truncated and optimized model versions for faster inference
- Works seamlessly with both CPU and GPU setups
📦 Installation
Modern-BERT-Score comes in two variants: a base version and a vLLM-enhanced version. For vLLM, an NVIDIA GPU is strongly recommended.
Base Version
pip install modern-bert-score
vLLM Version
pip install modern-bert-score[vllm]
This implementation is significantly faster than the original BERTScore, especially with GPU acceleration.
📝 BERTScore
BERTScore (Zhang et al., 2019) evaluates the similarity between candidate and reference texts by comparing their contextual embeddings from a pre-trained transformer model. For each token in the candidate, it finds the most similar token in the reference (using cosine similarity) and aggregates these scores to compute precision, recall, and F1. Optionally, IDF-weighting can be applied to give more importance to rare and informative words, improving the metric’s sensitivity to meaningful content over common words. Additionally, optional Baseline Rescaling shifts the scores such that the score is in the range of [0,1]. This approach captures semantic similarity beyond exact word matches, making it robust for tasks such as machine translation and text generation evaluation.
The following figure, taken from the original paper, illustrates how BERTScore works:
🛠 Usage
Example
from modern_bert_score import BertScore
candidates = ["Hello World!", "A robin is a bird."]
references = ["Hi World!", "A robin is not a bird."]
metric = BertScore(model_id="roberta-base")
scores = metric(candidates, references)
# scores is a list of (Precision, Recall, F1) tuples
# To get separate lists of P, R, F1:
P, R, F1 = zip(*scores)
print("Precision scores:", P)
print("Recall scores:", R)
print("F1 scores:", F1)
⚠️ NOTICE
- For best performance, an optimal layer should be used for each model.
- To find the optimal layer, please use this script from the original BERTScore implementation.
Some pre-truncated models optimized for vLLM are available on Hugging Face and directly available in this library:
LazerLambda/ModernBERT-base-ModBERTScore-12->ModernBERTBaseScoreLazerLambda/ModernBERT-large-ModBERTScore-19->ModernBERTLargeScoreLazerLambda/roberta-base-ModBERTScore-10->RobertaBaseScoreLazerLambda/roberta-large-ModBERTScore-17->RobertaLargeScoreLazerLambda/roberta-large-mnli-ModBERTScore-19->RobertaLargeMNLIScore
🗺 Roadmap
- Implement base version and vLLM addon
- Add IDF-weighted scoring
- Add baseline-rescaling and scripts for identifying optimal baselines
- Add model (vLLM-)adaptation script for slicing the model
- Add multilingual support
- Add CLI tool
Project details
Release history Release notifications | RSS feed
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 modern_bert_score-0.0.2.tar.gz.
File metadata
- Download URL: modern_bert_score-0.0.2.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5108515b3e7dc1840af1563139cbc200bc8b38159877eb55e876c9b8e55cfa40
|
|
| MD5 |
f18e4156596e07f9e101dc631c7cc214
|
|
| BLAKE2b-256 |
2bce7918919cf59b9b766a1a5a6ccb46637b62a3f035c7cc4fcf3cdcc035ad06
|
File details
Details for the file modern_bert_score-0.0.2-py3-none-any.whl.
File metadata
- Download URL: modern_bert_score-0.0.2-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11d51899339ee03c01ba82301cc0b98eca8d9359d6515ef5f93d3e8d7dc19530
|
|
| MD5 |
13aa3fa325711d7913c3154620dabcb8
|
|
| BLAKE2b-256 |
26bf5807f9cad5aac9fc5fa614b7c2b2e439e8e10880a4c03bcf80eb6b40193a
|