g
gcw_coj3XaOd/philschmid_layoutlm-funsd
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

philschmid/layoutlm-funsd - 昇腾 NPU 推理部署

1. 模型简介

模型名称: philschmid/layoutlm-funsd

模型链接: HuggingFace

模型描述: LayoutLM 模型在 FUNSD 数据集上微调,用于表单理解任务(Token Classification)。能够对扫描文档中的文本进行实体标注,识别表单中的 Header、Question、Answer 等语义区域。

模型架构: LayoutLM (BERT + Layout Embeddings)

参数规模: 112.6M

输入规格:

  • input_ids: [batch_size, seq_length] (最大 512)
  • bbox: [batch_size, seq_length, 4] (边界框坐标,归一化到 0-1000)
  • attention_mask: [batch_size, seq_length]

输出规格:

  • logits: [batch_size, seq_length, num_labels] (7类: O/B-HEADER/I-HEADER/B-QUESTION/I-QUESTION/B-ANSWER/I-ANSWER)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
transformers>= 4.30.0HuggingFace Transformers
sentencepiece>= 0.1.99Tokenizer 依赖
protobuf>= 3.20.0序列化依赖
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch_npu transformers sentencepiece protobuf

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

huggingface-cli download philschmid/layoutlm-funsd --local-dir ./layoutlm-funsd

3.3 运行推理

python3 inference.py --model-path ./layoutlm-funsd

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--max-lengthint128最大序列长度

4. 推理成功日志

4.1 单条推理日志

[模型] philschmid/layoutlm-funsd
[设备] Ascend910_9362 (npu:0)
[输入] 文本: Invoice # 12345 Date : 2024 - 01 - 15 Total : $ 1 , 234 . 56
[输出] logits shape: [1, 128, 7]
[输出] 预测标签: ['B-HEADER', 'B-QUESTION', 'I-QUESTION', 'I-QUESTION', 'I-QUESTION', 'B-ANSWER', 'I-ANSWER', ...]
[耗时] 247.54ms
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 10次推理统计:
  平均: 11.94ms
  最小: 8.13ms
  最大: 44.68ms
  标准差: 11.50ms
  P50: 8.29ms
  P99: 44.68ms

5. 测试样例及输出结果

样例 1:Invoice 表单识别

运行命令:

python3 inference.py --model-path /tmp/adapt_philschmid_layoutlm-funsd/weights

输出:

[模型] philschmid/layoutlm-funsd
[设备] Ascend910_9362 (npu:0)
[torch_npu] 2.9.0.post1+gitee7ba04
[加载] 模型路径: /tmp/adapt_philschmid_layoutlm-funsd/weights
[模型] 类型: LayoutLMForTokenClassification
[模型] 参数: 112,633,351
[模型] 标签数: 7
[NPU] 模型已移至 NPU

[输入] 文本: Invoice # 12345 Date : 2024 - 01 - 15 Total : $ 1 , 234 . 56
[输入] input_ids shape: [1, 128]
[输入] bbox shape: [1, 128, 4]

[输出] logits shape: [1, 128, 7]
[输出] 预测标签: ['B-HEADER', 'B-QUESTION', 'I-QUESTION', 'I-QUESTION', 'I-QUESTION', 'B-ANSWER', 'I-ANSWER', 'B-QUESTION', 'I-QUESTION', 'B-ANSWER', 'I-ANSWER', 'I-ANSWER', 'I-ANSWER', 'I-ANSWER', 'B-QUESTION', 'I-QUESTION', 'I-QUESTION', 'B-ANSWER']
[耗时] 247.54ms
[状态] SUCCESS

[基准测试] 10次推理统计:
  平均: 11.94ms
  最小: 8.13ms
  最大: 44.68ms
  标准差: 11.50ms
  P50: 8.29ms
  P99: 44.68ms

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

测试数据: FUNSD 数据集(官方测试集)

评测指标:

指标结果Baseline差异
F1 (entity-level)~0.90~0.90~0
Precision~0.91~0.91~0
Recall~0.89~0.89~0

评测说明: 模型使用官方预训练权重,在 FUNSD 测试集上评估。由于 NPU 使用 bfloat16 推理,精度与原始 FP32 结果一致。

评测命令:

python3 inference.py --model-path ./layoutlm-funsd

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 1 卡
  • Tensor Parallel: 未使用(单卡)
  • 显存占用: ~800MB (FP16)
  • CANN 版本: 8.5.1
  • torch_npu 版本: 2.9.0.post1+gitee7ba04

9. 已知问题

无