本文档记录 sentence-transformers/all-mpnet-base-v2 句嵌入模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
all-mpnet-base-v2 是一个基于 MPNet 的句嵌入模型(12 层 Transformer,768 维隐藏层),通过 Mean Pooling + L2 归一化将文本映射到 768 维语义向量空间。相比 all-MiniLM-L6-v2,该模型精度更高但参数量更大,适用于对语义质量要求较高的场景。
相关获取地址:
sentence-transformers/all-mpnet-base-v2| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 5.8.1 |
sentence-transformers | 5.5.0 |
CANN | 8.5.1 |
8 × Ascend 910B3conda create -n all-mpnet-base-v2 python=3.11 -y
conda activate all-mpnet-base-v2
pip install torch==2.8.0 torch_npu==2.8.0.post4 \
-i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers sentence-transformers numpy \
-i https://pypi.tuna.tsinghua.edu.cn/simplemodelscope download --model sentence-transformers/all-mpnet-base-v2 \
--local_dir ./all-mpnet-base-v2# 命令行推理
python inference.py --text "Hello world" --device npu
# 批量推理
python inference.py --batch_file texts.txt --device npu --batch_size 32编程接口:
from inference import MPNetEncoder
encoder = MPNetEncoder(model_path="./all-mpnet-base-v2", device="npu")
embeddings = encoder.encode(["Hello world", "Another sentence"])
# embeddings.shape -> (2, 768)python inference.py --text "This is a test sentence." --device npu预期输出:嵌入维度 768,无运行时错误。
测试条件:23 条多样化测试句子,batch_size=32。
| 指标 | 数值 |
|---|---|
| CPU 吞吐量 | 27.3 sentences/s |
| NPU 吞吐量 | 876.7 sentences/s |
| CPU/NPU 加速比 | 32.1 × |
分别在 CPU 和 NPU 上对 23 条多样化测试句子推理,计算:
| 指标 | 数值 |
|---|---|
| 平均余弦相似度 | 0.999995 |
| 最小余弦相似度 | 0.999977 |
| 精度误差率 | 0.0005% |
| 最大元素绝对误差 | 0.000878 |
| MSE | 0.00000001 |
| 相似度矩阵 Pearson r | 0.999997 |
结论:精度误差率 0.0005%,远低于 1% 要求,评测通过。
all-mpnet-base-v2 由三个子模块组成:
该模型为标准 MPNet 架构,NPU 适配无需修改模型结构:
AutoModel.from_pretrained() 加载权重,model.to("npu:0") 迁移到 NPUAutoTokenizer 在 CPU 分词后转移到 NPU.cpu().numpy() 转回 CPUnpu:0),多卡并行可指定不同 device ID