本文档记录 d4data/biomedical-ner-all 在昇腾 Ascend 910B3 NPU 上的迁移适配、推理部署与精度评测结果。
该模型是一个基于 DistilBERT 的生物医学命名实体识别(NER)模型,支持 84 种 BIO 标注标签(42 种实体类型 × B-/I- 前缀 + O),参数量约 66.43M。可用于识别临床文本中的疾病、药物、症状、解剖结构等医学实体。
本次适配工作包括:
inference.pyeval.py相关获取地址:
参考文档:
| 组件 | 版本 |
|---|---|
Python | 3.9.13 |
torch | 2.8.0+cpu |
torch_npu | 2.8.0.post4 |
transformers | 4.57.6 |
numpy | 1.24.4 |
Ascend 910B3 × 8 逻辑卡25.5.2该模型使用 DistilBERT 架构,transformers 库原生支持。NPU 适配无需修改模型结构或权重。
已验证通过的适配流程:
import torch
import torch_npu
from transformers import AutoModelForTokenClassification, AutoTokenizer
model = AutoModelForTokenClassification.from_pretrained("d4data/biomedical-ner-all")
model = model.npu()
model.eval()
tokenizer = AutoTokenizer.from_pretrained("d4data/biomedical-ner-all")
inputs = tokenizer(["The patient was diagnosed with leukemia."], return_tensors="pt")
inputs = {k: v.npu() for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
preds = outputs.logits.cpu().argmax(dim=-1)pip install torch torch_npu transformers -i https://repo.huaweicloud.com/repository/pypi/simple/
export HF_ENDPOINT=https://hf-mirror.com# NPU 推理
python inference.py --text "The patient was diagnosed with acute lymphoblastic leukemia."
# CPU 推理
python inference.py --text "CT scan revealed a tumor in the left lung." --device cpu
# 从文件批量推理
python inference.py --input texts.txt
# 列出所有 NER 标签
python inference.py --list-labelspython inference.py --text "The patient was diagnosed with acute lymphoblastic leukemia and prescribed methotrexate."预期输出:
NER Results (NPU)
--- Result 1 ---
Text: The patient was diagnosed with acute lymphoblastic leukemia and prescribed methotrexate.
[Disease_disorder] acute lymphoblastic leukemia
[Medication] methotrexate测试条件:batch_size=12,seq_len=128,float32 精度,连续 20 次取平均。
| 指标 | CPU | NPU (Ascend 910B3) |
|---|---|---|
| 平均推理时间 (12 samples) | 182.70 ms | 11.49 ms |
| 单样本平均耗时 | 15.23 ms | 0.96 ms |
| 加速比 | 1x | 15.90x |
| 参数量 | 66.43M | 66.43M |
| 模型大小 | 253.4 MB | 253.4 MB |
使用 12 条生物医学文本进行评测:
| 指标 | 数值 | 要求 | 结果 |
|---|---|---|---|
| MSE | 1.49e-5 | - | - |
| Max Absolute Error | 9.37e-2 | - | - |
| Cosine Similarity | 0.99999824 | > 0.99 | ✓ |
| Token Prediction Agreement | 100.00% | > 99% | ✓ |
| Prob Mean Diff | 0.0019% | < 1% | ✓ PASS |
结论:NPU 精度误差 0.0019%,满足精度要求(< 1%),模型在 NPU 上的推理结果与 CPU 一致。
详细评测日志见 eval_log.txt。
torch_npu 版本:确保与 torch 版本匹配