Erlangshen-RoBERTa-110M-Similarity 是由 Fengshenbang 团队开发的基于 RoBERTa 架构的句子相似度计算模型。该模型基于 BertForSequenceClassification 架构,参数量 110M,支持中文和英文的句子对相似度判断。
1 -> similar, 0 -> not similar相关获取地址:
config.json| 组件 | 版本 |
|---|---|
| 昇腾 NPU | Ascend910 |
| CANN | 25.5.2 |
| PyTorch | 2.9.0 |
| torch_npu | 2.9.0.post1 |
| transformers | 4.57.6 |
| Python | 3.11.14 |
/opt/atomgit/models/Erlangshen-RoBERTa-110M-Similarity由于该模型是 BERT 序列分类模型(非文本生成模型),直接使用 PyTorch 进行推理:
import torch
import torch.nn.functional as F
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_path = "/path/to/model"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
model.eval()
# 使用 NPU
device = torch.device("npu") if torch.npu.is_available() else torch.device("cpu")
model = model.to(device)
# 句子对相似度判断
sent1 = "我喜欢看电影"
sent2 = "我爱看电影"
inputs = tokenizer(sent1, sent2, return_tensors="pt", padding=True, truncation=True, max_length=128)
inputs = {k: v.to(device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
probs = F.softmax(logits, dim=-1)
pred = torch.argmax(logits, dim=-1).item()
label_map = {1: "similar", 0: "not similar"}
print(f"Prediction: {label_map[pred]} (confidence: {probs[0][pred].item():.4f})")使用 6 组中英文句子对进行相似度测试:
python3 accuracy_test.py| 测试用例 | 句子1 | 句子2 | 预期 | 预测 | 置信度 | 结果 |
|---|---|---|---|---|---|---|
| 1 | 我喜欢看电影 | 我爱看电影 | similar | similar | 99.77% | PASS |
| 2 | 今天天气真好 | 明天可能会下雨 | not similar | not similar | 99.98% | PASS |
| 3 | 苹果是一种水果 | 苹果是一种电子产品品牌 | not similar | not similar | 83.68% | PASS |
| 4 | 我想去北京旅游 | 我打算去北京玩 | similar | similar | 99.02% | PASS |
| 5 | 这本书很有趣 | 这本书很无聊 | not similar | not similar | 99.97% | PASS |
| 6 | 人工智能是未来趋势 | AI是未来发展方向 | similar | similar | 99.25% | PASS |
总体精度:100% (6/6)
测试条件:20 轮推理,输入长度不超过 128 tokens
| 指标 | 数值 |
|---|---|
| 平均推理时间 | 6.61 ms |
| 最小推理时间 | 6.29 ms |
| 最大推理时间 | 7.36 ms |
| 吞吐量 | 142.86 queries/sec |
| 总测试时间 | 0.14 sec |
modelscope.snapshot_download('Fengshenbang/Erlangshen-RoBERTa-110M-Similarity')