模型适配 · 孟加拉语细粒度命名实体识别 (Named Entity Recognition) · 昇腾 NPU
Beta 版本头信息
{hardware NPU}google/muril-large-cased(多语言 BERT,MuRIL-large)骨干微调得到的 token 级序列标注模型,在 SAMPUR 基准上对孟加拉语文本做 132 类细粒度命名实体识别(如 B-person-politician / I-location-GPE / B-organization-company 等)。Linear(1024 → 133),输出每个 token 的 133 维分类 logits(132 个细粒度实体标签 + O)。{entity, type}attn_implementation="eager"(NPU 上 sdpa/flash 注意力走 aclnn 算子易报错),以 eager 注意力 稳定推理,并用标准库 HTTP 服务提供服务化推理。权重为单文件 model.safetensors(约 2.0GB,fp32)。本项目 inference.py 支持三路获取:
| 通道 | 说明 |
|---|---|
| 本地目录 | --model-id <本地目录> 直接加载 |
| HuggingFace hf-mirror | 默认经 HF_ENDPOINT=https://hf-mirror.com snapshot_download |
| AtomGit/gitcode 镜像 | 回退通道:git clone + LFS batch API 下载权重 |
也可以手动下载后放到本地目录:
# 已通过 gitcode LFS 拉取的完整权重目录(含 config.json / tokenizer.json / model.safetensors)
mkdir -p /tmp/models/sampurner_weights
# model.safetensors 约 2.0GB, sha256 见官方 LFS pointerPython 3.10+ / 3.11,CUDA 无关,依赖于昇腾 NPU 驱动与 CANN。核心依赖见 requirements.txt:
torch>=2.1.0
torch_npu>=2.1.0.post1
transformers>=4.40.0
safetensors>=0.4.0
huggingface_hub>=0.23.0
numpy>=1.24.0注意:若
transformers>=4.40报huggingface-hub版本冲突,请安装与之兼容的huggingface-hub<1.0:pip install "huggingface-hub<1.0" -i https://mirrors.aliyun.com/pypi/simple/
# 1) 检查 NPU 环境
npu-smi info
# 2) 直接以 NPU 推理(默认演示孟加拉语文本)
python3 inference.py --device npu:0
# 3) 自定义输入文本
python3 inference.py --device npu:0 --text "রবীন্দ্রনাথ ঠাকুর ১৮৬১ সালে কলকাতায় জন্মগ্রহণ করেন।"
# 4) CPU vs NPU 精度对比(约束同一输入)
python3 inference.py --model-id /tmp/models/sampurner_weights --device npu:0 --compare
# 5) HTTP 服务化推理(POST /predict)
python3 inference.py --device npu:0 --serve --port 8000服务化请求示例:
curl -X POST http://localhost:8000/predict \
-H "Content-Type: application/json" \
-d '{"text": "রবীন্দ্রনাথ ঠাকুর কলকাতায় জন্মগ্রহণ করেন।"}'返回:
{"entities": [{"entity": "...", "type": "person-artist/author"}, ...]}| 项目 | 结果 |
|---|---|
| 设备 | Ascend NPU (torch_npu 2.9.0.post1 + CANN 8.5.1) |
| 注意力 | eager(显式 attn_implementation="eager",避开 aclnn sdpa/flash 报错) |
| 输入/精度相对 CPU | logits 余弦相似度 0.999997,argmax 一致率 100.00% |
| 时延 | 单条文本(max_length=512)ms 级(NPU warm-up 后第 2 次起稳定) |
model.to(device).eval() 推理即可,无需 CUDA graph / vLLM 编排。## 子词),实体解码时对 ## 前缀子词去重后再拼接,str/int 两种 id2label 键均可兼容。要本文(assets/)中放置三张截图,见下表;当前由用户自行截图上传。
assets/agent_workflow.png — 模型适配 / 推理流程(Agent 工作流)assets/npu_device_call.png — NPU 设备调用(npu-smi / NPU 推理回显)assets/model_result.png — 模型推理结果(--compare 输出 / entities 实体)== 实体解码结果 ==
[person-artist/author] ঠাকুর১৮৬১
[location-GPE] জন্মগ্রহণ
[location-GPE] কবি[对比] CPU vs npu:0:余弦相似度=0.999997 argmax一致率=100.00%