冬
gcw_IDzXRVNw/layoutlm-funsd-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

LayoutLM-FUNSD 昇腾 NPU 适配部署文档

1. 模型简介

LayoutLM-FUNSD 是在 FUNSD 数据集上微调的 LayoutLM 模型,用于文档理解中的令牌分类任务。该模型能够识别文档中的标题、问题、答案等结构化信息。

模型架构

属性值
模型类型LayoutLMForTokenClassification
基础模型microsoft/layoutlm-base-uncased
参数量112.63 M
Hidden Size768
Num Layers12
Num Attention Heads12
Vocab Size30,522

标签类别

标签 ID类别
0O (无)
1B-HEADER (标题开始)
2I-HEADER (标题继续)
3B-QUESTION (问题开始)
4I-QUESTION (问题继续)
5B-ANSWER (答案开始)
6I-ANSWER (答案继续)

性能指标

指标值
Overall Precision0.7599
Overall Recall0.8083
Overall F10.7866
Overall Accuracy0.8106

2. 环境依赖清单

2.1 硬件要求

  • 华为昇腾系列 AI 处理器(Ascend 910B/310P 等)
  • 建议 ≥ 2 GB NPU 显存(LayoutLM-FUNSD 推理峰值约 1-2 GB)
  • 本次实测使用 910B2(单卡 64 GB)

2.2 软件依赖

依赖项版本要求说明
操作系统CentOS 7.6+ / Ubuntu 18.04+支持主流 Linux 发行版
CANN5.0.RC2+华为昇腾计算架构
Python3.10+推荐 3.10+
PyTorch2.0.0+推荐 2.12
torch_npu与 PyTorch 版本匹配昇腾 PyTorch 扩展

2.3 安装方式

conda activate pt2100
pip install transformers torch -i https://repo.huaweicloud.com/repository/pypi/simple

3. 分步推理操作流程

步骤 1:准备工作目录

cd /workspace/agent4/layoutlm-funsd-ascend
ls -la /workspace/agent3/philschmid/layoutlm-funsd/

步骤 2:检查 NPU 环境

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

步骤 3:运行推理脚本

python inference.py --device npu:0

4. 完整测试用例

测试用例 1:文档令牌分类推理(NPU:0)

conda activate pt2100
cd /workspace/agent4/layoutlm-funsd-ascend
python inference.py --device npu:0

实际输出:

============================================================
LayoutLM-FUNSD 昇腾 NPU 推理
============================================================

[Device] npu:0

[Model Path] /workspace/agent3/philschmid/layoutlm-funsd

[Config]
  model_type: layoutlm
  hidden_size: 768
  num_hidden_layers: 12
  num_attention_heads: 12
  vocab_size: 30522
  max_position_embeddings: 512
  max_2d_position_embeddings: 1024
  num_labels: 7

[Memory Before Model Load] Allocated: 0.00 GB, Reserved: 0.00 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 95.5        41                0    / 0             |
+===========================+===============+====================================================+


[Loading] LayoutLM-FUNSD model weights...
[Loading] LayoutLM-FUNSD model from ...philschmid/layoutlm-funsd
  Total keys: 206
  Total parameters: 112.63 M

[Model Created] Allocated: 0.84 GB, Reserved: 0.92 GB

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 95.5        41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4428 / 65536         |
+===========================+===============+====================================================+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1736655       | python                   | 1058                    |
+===========================+===============+====================================================+


[Running] LayoutLM-FUNSD inference...

[npu-smi info]
+------------------------------------------------------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 104.8       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4468 / 65536         |
+===========================+===============+====================================================+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1736655       | python                   | 1098                    |
+===========================+===============+====================================================+


[Results]
  Output shape: torch.Size([1, 7])
  Output mean: -0.072711
  Output std: 0.262044

[Memory After Inference] Allocated: 0.84 GB, Reserved: 0.95 GB

[Done]

性能指标

指标值
模型参数量112.63 M
推理显存峰值~1-2 GB
输入 Shape(1, 512)
输出 Shape(1, 7)

5. 参考链接

  • LayoutLM GitHub
  • HuggingFace 模型页
  • FUNSD Dataset