w
willwell888/layoutlm-funsd
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

layoutlm-funsd on Ascend NPU

1. 简介

本文档记录 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。

相关获取地址:

  • 权重下载地址(HuggingFace):https://huggingface.co/philschmid/layoutlm-funsd
  • 权重下载地址(ModelScope):https://modelscope.cn/models/philschmid/layoutlm-funsd
  • 基础模型(LayoutLM Base Uncased):https://huggingface.co/microsoft/layoutlm-base-uncased

2. 验证环境

组件版本
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
transformers4.57.6
numpy1.26.4
pillow12.2.0
CANN8.5.1
  • NPU:2 逻辑卡(Ascend910_9362)
  • 模型路径:/opt/atomgit/0821/layoutlm-funsd
  • 运行环境:Python 3.11.14

3. 推理步骤

3.1 环境准备

检查 NPU 设备可用性:

npu-smi info
python -c "import torch; import torch_npu; print('npu available:', torch.npu.is_available())"

3.2 推理输入格式

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)

3.3 运行推理

单条推理:

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

推理参数说明:

参数类型默认值说明
--inputstr-单条输入 JSON 文件路径
--input_filestr-批量输入 JSON 文件路径(文档列表)
--model_pathstr当前目录模型权重目录
--devicestrauto推理设备,如 npu:0、cuda:0、cpu
--max_lengthint512最大序列长度

4. Smoke 验证

基础检查:

# 模型加载验证
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
  • NPU 前向推理正常完成,输出词元级语义标签
  • 前向耗时 ~0.16s(NPU)vs ~1.8s(CPU),约 11x 加速

5. 测试样例及输出结果

样例 1:发票表单词元分类

输入:

{
  "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

6. 性能参考

测试条件:单次前向推理,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

7. 注意事项

  • LayoutLMTokenizer 在 transformers >= 4.45 后移除了 boxes 参数,推理脚本自行构造 LayoutLM 编码(BERT 分词 + 逐词 box 展开),兼容所有版本
  • 输入框坐标支持像素坐标(>1000)和归一化坐标(0~1000),脚本自动识别并归一化
  • 模型路径下需包含 config.json、pytorch_model.bin、vocab.txt、tokenizer_config.json、tokenizer.json(tokenizer 文件可从 HuggingFace 或 ModelScope 下载)
  • 推理脚本优先使用 NPU,回退到 CUDA,最后使用 CPU