本文档记录 layoutlm-funsd 在昇腾 NPU 环境的快速部署与验证结果。
LayoutLM 是微软提出的多模态文档理解模型,将文本语义与布局空间信息联合建模,在表单理解、票据识别等场景表现优异。layoutlm-funsd 基于 microsoft/layoutlm-base-uncased(112M 参数,12 层 BERT 架构)在 FUNSD(Fundamental Structures of Scientific Documents)数据集上微调,用于文档词元级别的语义分类,输出 7 类标签:O / B-HEADER / I-HEADER / B-QUESTION / I-QUESTION / B-ANSWER / I-ANSWER。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.9.0+cpu |
torch_npu | 2.9.0.post1+gitee7ba04 |
transformers | 4.57.6 |
numpy | 1.26.4 |
pillow | 12.2.0 |
| CANN | 8.5.1 |
2 逻辑卡(Ascend910_9362)/opt/atomgit/0821/layoutlm-funsd检查 NPU 设备可用性:
npu-smi info
python -c "import torch; import torch_npu; print('npu available:', torch.npu.is_available())"layoutlm-funsd 接受 FUNSD 风格的 JSON 格式输入(词元 + 布局框):
{
"id": "sample_001",
"words": ["Total", "Amount", "$", "1,250.00"],
"boxes": [[300, 100, 400, 130], [300, 130, 450, 160], [300, 160, 350, 190], [360, 160, 480, 190]]
}words:文档中的词元序列boxes:每个词元对应的轴对齐包围框(像素坐标或 0~1000 归一化坐标均可,脚本自动归一化)[x1, y1, x2, y2],左上角 (x1,y1),右下角 (x2,y2)单条推理:
python inference.py --input test_document.json --device npu:0批量推理:
python inference.py --input_file test_samples.json --device npu:0完整命令示例(指定模型路径):
python inference.py \
--input test_document.json \
--model_path /opt/atomgit/0821/layoutlm-funsd \
--device npu:0 \
--max_length 512推理参数说明:
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
--input | str | - | 单条输入 JSON 文件路径 |
--input_file | str | - | 批量输入 JSON 文件路径(文档列表) |
--model_path | str | 当前目录 | 模型权重目录 |
--device | str | auto | 推理设备,如 npu:0、cuda:0、cpu |
--max_length | int | 512 | 最大序列长度 |
基础检查:
# 模型加载验证
python -c "
import torch
from transformers import LayoutLMForTokenClassification
model = LayoutLMForTokenClassification.from_pretrained('/opt/atomgit/0821/layoutlm-funsd')
n_params = sum(p.numel() for p in model.parameters())
print(f'模型加载成功: 参数量={n_params:,} | 标签数={model.config.num_labels}')
"
# NPU 前向推理验证
python inference.py --input test_document.json --device npu:0验证结果:
112,633,351,标签数 7~0.16s(NPU)vs ~1.8s(CPU),约 11x 加速输入:
{
"id": "sample_invoice_001",
"words": ["INVOICE", "No.", "20260820", "Date", "Aug", "20,", "2026", "Customer", "Name:", "John", "Doe", "Item", "Consulting", "Service", "Quantity", "5", "Unit", "Price", "$", "250.00", "Total", "Amount", "$", "1,250.00", "Payment", "Paid", "in", "full", "Signature", "Jane", "Smith"],
"boxes": [[520, 80, 980, 140], [120, 200, 170, 230], [180, 200, 360, 230], [560, 200, 620, 230], [630, 200, 680, 230], [690, 200, 740, 230], [750, 200, 830, 230], [120, 300, 280, 330], [290, 300, 400, 330], [410, 300, 470, 330], [480, 300, 540, 330], [120, 400, 180, 430], [190, 400, 380, 430], [390, 400, 470, 430], [560, 400, 660, 430], [670, 400, 700, 430], [800, 400, 860, 430], [870, 400, 940, 430], [950, 400, 970, 430], [980, 400, 1100, 430], [120, 520, 220, 550], [230, 520, 340, 550], [350, 520, 370, 550], [380, 520, 560, 550], [120, 620, 240, 650], [250, 620, 310, 650], [320, 620, 350, 650], [360, 620, 420, 650], [120, 720, 240, 750], [250, 720, 310, 750], [320, 720, 380, 750]]
}输出:
============================================================
词级预测结果(word-level predictions)
============================================================
word label score
--------------------------------------
INVOICE B-HEADER 0.9842 *
No. B-QUESTION 0.9689 *
20260820 B-ANSWER 0.9115 *
Date B-QUESTION 0.9977 *
Aug B-ANSWER 0.9969 *
20, I-ANSWER 0.9592 *
2026 I-ANSWER 0.9485 *
Customer B-QUESTION 0.9887 *
Name: I-QUESTION 0.9954 *
John B-ANSWER 0.9969 *
Doe I-ANSWER 0.9964 *
Item B-QUESTION 0.9979 *
Consulting B-QUESTION 0.9858 *
Service I-QUESTION 0.7316 *
Quantity B-QUESTION 0.9981 *
5 B-ANSWER 0.9763 *
Unit B-QUESTION 0.9984 *
Price I-QUESTION 0.9980 *
$ B-ANSWER 0.9567 *
250.00 B-ANSWER 0.8889 *
Total B-QUESTION 0.9982 *
Amount I-QUESTION 0.9985 *
$ B-ANSWER 0.9584 *
1,250.00 I-ANSWER 0.4914 *
Payment B-HEADER 0.5336 *
Paid I-HEADER 0.6775 *
in I-HEADER 0.8370 *
full I-QUESTION 0.6499 *
Signature B-QUESTION 0.9794 *
Jane B-ANSWER 0.9576 *
Smith I-ANSWER 0.9902 *
--------------------------------------
* = 有效语义标签(非 O)
推理设备: npu | 前向耗时: 0.1579 s测试条件:单次前向推理,batch=1,float32 精度,NPU 单卡。
| 指标 | 数值 |
|---|---|
| 模型加载耗时 | ~4s |
| 前向推理耗时 | ~0.16s |
| 参数量 | 112,633,351(112.6M) |
| 输入词元数 | 31 |
| 输出标签数 | 7 |
CPU vs NPU 前向推理耗时对比:
| 设备 | 前向耗时 |
|---|---|
| CPU(Intel/AMD) | ~1.8s |
| NPU(Ascend 910) | ~0.16s |
| 加速比 | ~11x |
LayoutLMTokenizer 在 transformers >= 4.45 后移除了 boxes 参数,推理脚本自行构造 LayoutLM 编码(BERT 分词 + 逐词 box 展开),兼容所有版本config.json、pytorch_model.bin、vocab.txt、tokenizer_config.json、tokenizer.json(tokenizer 文件可从 HuggingFace 或 ModelScope 下载)