liucu/philschmid-layoutlm-funsd-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

philschmid/layoutlm-funsd on Ascend NPU

1. 模型简介

  • 模型名称: philschmid/layoutlm-funsd
  • 基础模型: microsoft/layoutlm-base-uncased (LayoutLM)
  • 架构: LayoutLMForTokenClassification - 文档理解 / 表单语义标注
  • 任务: Token Classification on FUNSD (Form Understanding in Noisy Scanned Documents)
  • 标签集 (7类): O, B-HEADER, I-HEADER, B-QUESTION, I-QUESTION, B-ANSWER, I-ANSWER
  • 参数量: 112.6M, 权重大小 450.6 MB (pytorch_model.bin, FP32)
  • 输入契约: input_ids [1,128], bbox [1,128,4] (0-1000 归一化坐标), attention_mask [1,128], token_type_ids [1,128], 预处理使用 LayoutLMv2Processor (apply_ocr=False, size 224)
  • 输出契约: logits [1,128,7] -> softmax -> 每 token 标签与得分
  • 官方数据集指标 (README): Overall F1 0.7866, Precision 0.7599, Recall 0.8083

适用于发票、表单等扫描文档的版式理解与键值抽取,为计算机视觉 + NLP 跨模态场景。

2. 验证环境

  • 硬件: Ascend910 (Ascend910_9362), 2 devices, HBM 64GB per chip
  • NPU 信息: npu-smi 显示 2x Ascend910, Health OK, CANN 8.5.1, Version 25.5.5
  • 软件: torch==2.9.0+cpu, torch_npu==2.9.0.post1+gitee7ba04, transformers==4.57.6, Pillow
  • HF 缓存: /data/hf-cache/models--philschmid--layoutlm-funsd/snapshots/5dc97b05b27e5efed5f331a2216726dd7b338e43
  • 验证时间: 2026-08-23 UTC
  • npu-smi 与 torch.npu 均验证可用: torch.npu.is_available()==True, device_count==2, device_name==Ascend910_9362, 显存 ~61GB free

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.57.6
# pillow
# huggingface_hub

权重通过 huggingface_hub snapshot_download 或 from_pretrained 自动走 HF_ENDPOINT=https://hf-mirror.com 镜像,无需手动设置代理。

4. NPU 推理

默认单条推理 (NPU 同步计时):

python inference.py

指定模式:

python inference.py --mode validate   # CPU-NPU 一致性验证
python inference.py --mode benchmark  # 性能测试 (warmup 3 + 10 runs, synchronized)
python inference.py --device cpu      # CPU 对比

inference.py 已在 npu:0 上验证,自动处理本地缓存权重加载、固定合成输入、NPU/CPU 一致性对比与同步计时。

5. 真实推理结果

命令: python inference.py (device=npu:0, dtype=float32, batch=1, seq_len=128) 输入: 合成发票样本

  • words: ["Invoice","Number","12345","Date","2023-01-01","Total","Amount",":","$","100.00","Thank","you"]
  • boxes (归一化 0-1000): [[100,100,250,150],[260,100,380,150],[390,100,500,150],...]
  • image: invoice_example.png (2457x3179) 或白底 1000x1400 合成图,经 LayoutLMv2Processor 处理为 input_ids/bbox/attention_mask
  • Latency (首帧含编译, 同步): 174.99 ms

输出 logits shape: [1, 128, 7] Token 级预测 (前20):

token=in           label=B-QUESTION score=0.9920 bbox=[100, 100, 250, 150]
token=##vo         label=I-QUESTION score=0.5127 bbox=[100, 100, 250, 150]
token=##ice        label=I-QUESTION score=0.8085 bbox=[100, 100, 250, 150]
token=number       label=I-QUESTION score=0.9721 bbox=[260, 100, 380, 150]
token=123          label=B-ANSWER   score=0.9696 bbox=[390, 100, 500, 150]
token=##45         label=I-ANSWER   score=0.7241 bbox=[390, 100, 500, 150]
token=date         label=B-QUESTION score=0.9968 bbox=[100, 200, 200, 250]
token=202          label=B-ANSWER   score=0.9694 bbox=[210, 200, 380, 250]
token=##3          label=I-ANSWER   score=0.5602 bbox=[210, 200, 380, 250]
token=-            label=I-ANSWER   score=0.9585 bbox=[210, 200, 380, 250]
token=01           label=I-ANSWER   score=0.9393 bbox=[210, 200, 380, 250]
token=-            label=I-ANSWER   score=0.9666 bbox=[210, 200, 380, 250]
token=01           label=I-ANSWER   score=0.9015 bbox=[210, 200, 380, 250]
token=total        label=B-QUESTION score=0.9971 bbox=[100, 300, 200, 350]
token=amount       label=I-QUESTION score=0.9975 bbox=[210, 300, 340, 350]
token=:            label=I-QUESTION score=0.9709 bbox=[350, 300, 370, 350]
token=$            label=B-ANSWER   score=0.9144 bbox=[380, 300, 420, 350]
token=100          label=I-ANSWER   score=0.5332 bbox=[430, 300, 540, 350]
token=.            label=I-HEADER   score=0.2431 bbox=[430, 300, 540, 350]
token=00           label=I-ANSWER   score=0.5856 bbox=[430, 300, 540, 350]

逻辑符合预期: Invoice Number 识别为 QUESTION, 12345/2023-01-01/100.00 识别为 ANSWER, Total Amount 为 QUESTION, 与 FUNSD 语义一致。 最终状态: PASS: NPU inference successful, output shape correct

6. CPU-NPU 一致性验证

命令: python inference.py --mode validate

  • 对比项: 同一输入在 CPU (FP32) 与 NPU:0 (FP32) 上 logits 对比
  • 结果:
    • max_abs_diff = 0.014254
    • mean_abs_diff = 0.005327
    • Label agreement = 100.00% (argmax 标签完全一致)
    • 阈值: 0.05 (FP32 NPU 数值误差容忍)
    • 判定: PASS
  • 说明: NPU 与 CPU 误差在 1.5e-2 以内,属于正常 FP32 计算差异,无功能回退,已在 npu:0 上完成真实推理,模型参数 device=npu:0, dtype=torch.float32。

7. 性能测试

命令: python inference.py --mode benchmark

  • 配置: warmup 3 次, 正式 10 次, torch.npu.synchronize() 同步计时, batch=1, seq_len=128, dtype=float32
  • 首帧编译延迟: ~175 ms (含图编译与缓存)
  • 稳态延迟 (10 次):
    • avg=7.18 ms, min=7.12 ms, max=7.30 ms, p50=7.18 ms, p90=7.30 ms, p95=7.30 ms
    • Throughput = 139.26 inferences/sec (seq_len 128)
  • 显存: free 60.43 GiB / total 61.27 GiB (峰值占用 ~1 GiB)
  • 设备: Ascend910_9362, torch.npu 已验证
  • 说明: 首次推理含编译,后续稳定在 7ms 级,满足表单实时处理需求。

8. 自验证截图

三张 PNG 由 scripts/render_xterm_evidence.mjs --style raw 从真实日志渲染,含 SHA-256 与退出码:

  • assets/agent_workflow.png - 完整工作流: 环境检查、获取模型、分析模型、NPU适配、NPU验证、性能测试、整理仓库
  • assets/npu_device_call.png - NPU 设备调用: npu-smi info, torch.npu.is_available()==True, device_name Ascend910_9362, 模型参数 device npu:0
  • assets/model_result.png - 模型推理结果: 默认 python inference.py 的任务输出、一致性与 PASS

9. 已知限制

  • 需要 torch_npu 与 CANN 8.5.1 环境,当前仅验证 FP32 (官方权重为 float32, 未测试 FP16/BF16 量化)
  • 输入使用 LayoutLMv2Processor apply_ocr=False 的固定合成 words/boxes + invoice_example.png;真实业务需接入 OCR (如 tesseract/pytesseract) 获取 words/boxes,处理器会额外生成 image 张量但 LayoutLMv1 模型不使用图像特征 (仅文本+bbox)
  • 模型为 12 层 LayoutLM-base, 序列长度固定 128, 超长文档需滑动窗口或截断
  • FUNSD 官方完整数据集评测需额外脚本,本次为单样本 smoke + CPU-NPU 一致性验证,非全量精度评测
  • 首次 NPU 推理有 ~175ms 编译开销,需预热

10. 标签

Hardware: NPU, NPU, Ascend, Ascend910, LayoutLM, TokenClassification, DocumentUnderstanding, ComputerVision, FUNSD