z
zhangfeng1133/bert-large-cased-wwm
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

bert-large-cased-wwm — 昇腾 NPU 掩码语言推理

一、模型简介

BERT(Bidirectional Encoder Representations from Transformers)是 Google 开源的 预训练语言模型,本变体为 large-cased-wwm。本仓库交付其在昇腾 NPU 上的掩码语言 (fill-mask / MLM)推理方案。

属性说明
模型google-bert/bert-large-cased-whole-word-masking
变体large-cased-wwm
任务掩码语言建模(预测 [MASK] 位置的 token)
输入含 [MASK] 的文本
输出top-k 候选 token 及概率
下载源modelscope.cn
推理精度fp32

该模型已在 ModelScope 发布,推理脚本会自动从 modelscope.cn 下载权重并缓存到本地。

二、环境依赖

  • 昇腾 NPU (Ascend 910B 或同类) / CUDA / CPU
  • CANN 8.5.1
  • Python 3.11
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/

三、分步推理操作流程

Step 1: 推理(自动下载模型)

python inference.py "The capital of France is [MASK]." --device npu

首次运行会通过 ModelScope 自动下载权重;也可显式指定来源:

python inference.py "I like to [MASK] books." --model /data/.cache/models/google-bert/bert-large-cased-whole-word-masking --device npu

Step 2: 调整候选数与批量推理

python inference.py "I like to [MASK] books." --top-k 10 --device npu
python inference.py --file sentences.txt --device npu     # 每行一句

Step 3: 输出到文件

python inference.py "Paris is the capital of [MASK]." --out result.txt --device npu

Step 4: 启动 FastAPI 服务(可选)

python -m uvicorn main:app --host 0.0.0.0 --port 8000
curl http://localhost:8000/health
curl -X POST http://localhost:8000/predict \
     -H "Content-Type: application/json" \
     -d '{"text": "The capital of France is [MASK].", "top_k": 5}'

四、测试用例与输出结果

$ python inference.py "The capital of France is [MASK]." --device npu
$ python3 inference.py "The capital of France is [MASK]." --device npu
[info] variant=large-cased-wwm model=google-bert/bert-large-cased-whole-word-masking device=npu
input:	The capital of France is [MASK].
filled:	the capital of france is paris.
mask#1:	paris (0.41), lille (0.07), lyon (0.06), marseille (0.04), tours (0.03)

五、截图说明

见 assets/README.md,交付前需放入三张截图: agent_workflow.png、npu_device_call.png、model_result.png。

六、标签

NPU 昇腾 bert 掩码语言 fill-mask NLP large-cased-wwm