philschmid/layoutlm-funsdmicrosoft/layoutlm-base-uncased (LayoutLM)LayoutLMForTokenClassification - 文档理解 / 表单语义标注O, B-HEADER, I-HEADER, B-QUESTION, I-QUESTION, B-ANSWER, I-ANSWERinput_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 标签与得分适用于发票、表单等扫描文档的版式理解与键值抽取,为计算机视觉 + NLP 跨模态场景。
npu-smi 显示 2x Ascend910, Health OK, CANN 8.5.1, Version 25.5.5torch==2.9.0+cpu, torch_npu==2.9.0.post1+gitee7ba04, transformers==4.57.6, Pillow/data/hf-cache/models--philschmid--layoutlm-funsd/snapshots/5dc97b05b27e5efed5f331a2216726dd7b338e43torch.npu.is_available()==True, device_count==2, device_name==Ascend910_9362, 显存 ~61GB freepip 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 镜像,无需手动设置代理。
默认单条推理 (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 一致性对比与同步计时。
命令: python inference.py (device=npu:0, dtype=float32, batch=1, seq_len=128)
输入: 合成发票样本
["Invoice","Number","12345","Date","2023-01-01","Total","Amount",":","$","100.00","Thank","you"][[100,100,250,150],[260,100,380,150],[390,100,500,150],...]invoice_example.png (2457x3179) 或白底 1000x1400 合成图,经 LayoutLMv2Processor 处理为 input_ids/bbox/attention_mask输出 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
命令: python inference.py --mode validate
max_abs_diff = 0.014254mean_abs_diff = 0.005327Label agreement = 100.00% (argmax 标签完全一致)0.05 (FP32 NPU 数值误差容忍)npu:0 上完成真实推理,模型参数 device=npu:0, dtype=torch.float32。命令: python inference.py --mode benchmark
torch.npu.synchronize() 同步计时, batch=1, seq_len=128, dtype=float32torch.npu 已验证三张 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:0assets/model_result.png - 模型推理结果: 默认 python inference.py 的任务输出、一致性与 PASStorch_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)Hardware: NPU, NPU, Ascend, Ascend910, LayoutLM, TokenClassification, DocumentUnderstanding, ComputerVision, FUNSD