g
gcw_coj3XaOd/SampurNER_Bengali_MuRIL
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

SampurNER Bengali MuRIL - 昇腾 NPU 推理部署

1. 模型简介

模型名称: prachuryyaIITG/SampurNER_Bengali_MuRIL

模型链接: HuggingFace

模型描述: 基于 MuRIL-large 微调的孟加拉语细粒度命名实体识别(Fine-grained NER)模型。使用 SampurNER 数据集训练,支持 133 个 BIO 标注标签,涵盖 8 个粗粒度类别(Location, Person, Organization, Building, Art, Product, Event, Misc)。

模型架构: BertForTokenClassification (BERT-large based)

基础模型: google/muril-large-cased

参数规模: ~504M (BERT-large: 24层, 1024隐藏维度, 16注意力头)

输入规格:

  • 文本输入(孟加拉语/多语言)
  • 最大序列长度: 512 tokens
  • 词表大小: 197,285 (MuRIL SentencePiece)

输出规格:

  • 每个 token 的 133 分类标签(BIO 格式)
  • 细粒度实体类型(如 B-location-GPE, B-person-artist/author)

模型性能 (Bengali NER):

  • F1: 68.26%
  • Precision: 66.54%
  • Recall: 70.08%

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.48.0HuggingFace Transformers
sentencepiece>= 0.1.99SentencePiece tokenizer
protobuf>= 3.20.0Protocol Buffers
numpy< 2NumPy (版本兼容)
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch torch_npu transformers sentencepiece protobuf "numpy<2"

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download prachuryyaIITG/SampurNER_Bengali_MuRIL --local-dir ./model

方式二:AtomGit 镜像(HuggingFace 下载慢时使用)

git clone https://ai.gitcode.com/hf_mirrors/prachuryyaIITG/SampurNER_Bengali_MuRIL.git ./model

3.3 运行推理

单条文本推理:

python inference.py --model-path ./model --text "কলকাতা পশ্চিমবঙ্গের রাজধানী।"

批量推理:

python inference.py --model-path ./model --input-file input.txt

基准测试:

python inference.py --model-path ./model --text "কলকাতা পশ্চিমবঙ্গের রাজধানী।" --benchmark 10

3.4 启动服务

本模型为 Token Classification 模型,无需启动推理服务器。直接使用 inference.py 进行推理即可。

# 验证模型可正常加载(单次推理测试)
python inference.py --model-path ./model --text "কলকাতা পশ্চিমবঙ্গের রাজধানী।"

3.5 推理调用

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--textstrNone单条输入文本
--input-filestrNone批量输入文件路径(每行一条文本)
--devicestrnpu:0NPU 设备编号
--max-lengthint512最大序列长度
--benchmarkint0基准测试迭代次数
--output-jsonstrNone结果 JSON 输出路径

4. 推理成功日志

4.1 单条推理日志

[模型] prachuryyaIITG/SampurNER_Bengali_MuRIL
[设备] Ascend910_9362 (npu:0)
[输入] কলকাতা পশ্চিমবঙ্গের রাজধানী। রবীন্দ্রনাথ ঠাকুর বাংলা সাহিত্যের বিখ্যাত কবি।
[输出] 耗时: 219.14 ms
[输出] 实体数量: 4
  - কলকাতা -> location-GPE
  - পশ্চিমবঙ্গের -> location-GPE
  - রবীন্দ্রনাথঠাকুর -> person-artist/author
  - বাংলা -> location-GPE
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 5次推理统计:
  平均: 66.52 ms
  最小: 14.28 ms
  最大: 274.03 ms
  标准差: 116.00 ms
  P50: 14.59 ms

5. 测试样例及输出结果

样例 1:孟加拉语地名识别

运行命令:

python inference.py --model-path ./model --text "কলকাতা পশ্চিমবঙ্গের রাজধানী।"

输出:

[输入] কলকাতা পশ্চিমবঙ্গের রাজধানী।
[输出] 实体数量: 2
  - কলকাতা -> location-GPE
  - পশ্চিমবঙ্গের -> location-GPE
[状态] SUCCESS

样例 2:孟加拉语人物识别

运行命令:

python inference.py --model-path ./model --text "রবীন্দ্রনাথ ঠাকুর বাংলা সাহিত্যের বিখ্যাত কবি।"

输出:

[输入] রবীন্দ্রনাথ ঠাকুর বাংলা সাহিত্যের বিখ্যাত কবি।
[输出] 实体数量: 2
  - রবীন্দ্রনাথ -> person-artist/author
  - ঠাকুর -> person-artist/author
[状态] SUCCESS

样例 3:中英混合文本

运行命令:

python inference.py --model-path ./model --text "ടമ ക്രൂസ是一名美国演员,曾出演《碟中谍》系列电影。"

输出:

[输入] टम क्रूस是一名美国演员,曾出演《碟中谍》系列电影。
[输出] 实体数量: 1
  - टमक्रूस -> person-other
[状态] SUCCESS

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: SampurNER Bengali Test Set

评测指标:

指标结果说明
F168.26%Fine-grained NER F1 (论文报告)
Precision66.54%精确率
Recall70.08%召回率

推理延迟(NPU Ascend910_9362):

指标值
平均推理耗时~66 ms (含模型加载)
纯推理耗时~14-24 ms
模型参数量504,993,925

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 2 卡
  • Tensor Parallel: 不需要(单卡即可运行)
  • 显存占用: ~3GB HBM (单卡)
  • CANN 版本: 8.5.1
  • 推理框架: torch_npu + transformers (非 vLLM,Token Classification 任务)

9. 已知问题

  1. ** transformers 版本兼容性:** transformers >= 5.0 后 config.id2label 使用整数 key 而非字符串 key,需使用 id2label.get(int(pred_idx), "O") 而非 id2label.get(str(pred_idx), "O")。

  2. SentencePiece tokenizer: 模型使用 SentencePiece 词表(vocab_size=197285),需安装 sentencepiece 包。如遇 tokenizer 加载错误,使用 BertTokenizer.from_pretrained() 而非 AutoTokenizer.from_pretrained()。

  3. NPU 日志警告: 首次加载时可能出现 can not create directory /home/atomgit/ascend/log 警告,不影响推理功能。


10. 引用

@inproceedings{kaushik2026sampurner,
      title={SampurNER: Fine-Grained Named Entity Recognition Dataset for 22 Indian Languages},
      volume={40},
      url={https://ojs.aaai.org/index.php/AAAI/article/view/40405},
      DOI={10.1609/aaai.v40i37.40405},
      number={37},
      journal={Proceedings of the AAAI Conference on Artificial Intelligence},
      author={Kaushik, Prachuryya and Anand, Ashish},
      year={2026},
      month={Mar.},
      pages={31410-31418}
}