本文档记录 sentence-transformers/all-MiniLM-L6-v2 句嵌入模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
all-MiniLM-L6-v2 是一个基于 BERT 的轻量级句嵌入模型(6 层 Transformer,384 维隐藏层),通过 Mean Pooling + L2 归一化将文本映射到 384 维语义向量空间,广泛用于语义搜索、文本聚类和相似度计算等任务。
相关获取地址:
sentence-transformers/all-MiniLM-L6-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 910B3# 创建 conda 环境
conda create -n all-MiniLM-L6-v2 python=3.11 -y
conda activate all-MiniLM-L6-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/simple# ModelScope 下载
modelscope download --model sentence-transformers/all-MiniLM-L6-v2 \
--local_dir ./all-MiniLM-L6-v2
# 或 HuggingFace 镜像下载
HF_ENDPOINT=https://hf-mirror.com \
huggingface-cli download sentence-transformers/all-MiniLM-L6-v2 \
--local-dir ./all-MiniLM-L6-v2提供 inference.py 作为独立的句嵌入推理脚本:
# 单条文本推理
python inference.py \
--model_path ./all-MiniLM-L6-v2 \
--text "Hello world" \
--device npu
# 批量文本推理
python inference.py \
--model_path ./all-MiniLM-L6-v2 \
--batch_file texts.txt \
--device npu \
--batch_size 32编程接口调用:
from inference import MiniLMEncoder
encoder = MiniLMEncoder(model_path="./all-MiniLM-L6-v2", device="npu")
embeddings = encoder.encode(["Hello world", "Another sentence"])
# embeddings.shape → (2, 384)基础功能检查:
python inference.py \
--model_path ./all-MiniLM-L6-v2 \
--text "This is a test sentence." \
--device npu预期输出:
384验证 NPU 设备可用性:
import torch
import torch_npu
assert torch.npu.is_available(), "NPU 不可用"
print(f"NPU 数量: {torch.npu.device_count()}")
print(f"NPU 型号: {torch.npu.get_device_name(0)}")测试条件:24 条多样化测试句子(含短句、长句、标点、多语言),batch_size=32,NPU 预热 3 轮后取 10 轮平均。
| 指标 | 数值 |
|---|---|
| CPU 吞吐量 | 42.32 sentences/s |
| NPU 吞吐量 (bs=32) | 1,123.75 sentences/s |
| NPU 延迟 (bs=1) | 9.83 ms |
| NPU 延迟 (bs=32) | 21.36 ms |
| CPU/NPU 加速比 | 28.34 × |
不同 batch size 的 NPU 吞吐量:
| Batch Size | 延迟 (ms) | 吞吐量 (sentences/s) |
|---|---|---|
| 1 | 9.83 | 101.70 |
| 4 | 11.15 | 358.75 |
| 16 | 15.91 | 1,005.36 |
| 32 | 21.36 | 1,123.75 |
| 64 | 20.09 | 1,194.44 |
采用 24 条多样化测试句子(英文、中文、法文、标点、数字、短句、长句),分别在 CPU 和 NPU 上执行推理,计算以下指标:
| 指标 | 数值 |
|---|---|
| 平均余弦相似度 | 0.999999 |
| 最小余弦相似度 | 0.999998 |
| 精度误差率 | 0.0001% |
| 最大元素绝对误差 | 0.000413 |
| MSE | 0.00000000 |
| 相似度矩阵 Pearson r | 1.000000 |
结论:精度误差率 0.0001%,远低于 1% 要求,评测通过。
all-MiniLM-L6-v2 是一个 sentence-transformers 格式的模型,由三个子模块组成:
由于该模型为标准 BERT 架构,NPU 适配过程无需修改模型结构:
AutoModel.from_pretrained() 加载权重,model.to("npu:0") 迁移到 NPUsentence-transformers 库的 Pooling 模块依赖 torch.nn.Module,可正常在 NPU 上运行)AutoTokenizer 分词后在 CPU 执行,input_ids/attention_mask 通过 .to(device) 转移到 NPU.cpu().numpy() 转回 CPU# NPU 推理核心流程
import torch
import torch_npu
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("all-MiniLM-L6-v2").to("npu:0")
model.eval()
tokenizer = AutoTokenizer.from_pretrained("all-MiniLM-L6-v2")
inputs = tokenizer("Hello world", return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
embeddings = mean_pooling(outputs, inputs["attention_mask"])AutoModel + Mean Pooling 替代 SentenceTransformer.encode(),精度完全一致。npu:0),如需多卡并行可指定不同 device ID。max_seq_length=256,与原始 sentence_bert_config.json 配置一致,超出部分将被截断。