g
gcw_Pp8UzTBn/layoutlm-funsd-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

philschmid/layoutlm-funsd 昇腾 NPU 适配

昇腾 Model-Agent 模型适配大赛(第二季)· 适配赛道交付件 基于 Ascend 910 NPU + torch_npu 的 image-text-to-text 推理适配(FUNSD 表单字段识别),CPU/NPU 精度对齐 cos=0.999993 / rel_err=0.004。

一、模型简介

  • 模型名称:philschmid/layoutlm-funsd
  • 架构:LayoutLMForTokenClassification(LayoutLM 文档理解模型)
  • 参数规模:约 125M 参数
  • 配置:hidden_size=768, num_hidden_layers=12, num_attention_heads=12, max_position_embeddings=512, num_labels=7, type_vocab_size=2
  • 标签体系(FUNSD):O / B-HEADER / I-HEADER / B-QUESTION / I-QUESTION / B-ANSWER / I-ANSWER
  • 任务类型:image-text-to-text → /v1/chat/completions(文档理解,识别表单字段)
  • 原模型链接:https://huggingface.co/philschmid/layoutlm-funsd
  • 适配仓库(本项目):https://gitcode.com/gcw_Pp8UzTBn/layoutlm-funsd-NPU

二、环境依赖

昇腾 NPU 平台环境已提供以下基础组件:

组件版本
Python3.11.14
PyTorch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
CANN8.5.1
NPU 驱动npu-smi 25.5.5
硬件Ascend 910 × 2(HBM 64GB/卡)

应用层依赖见 requirements.txt:

包pin 版本用途
transformers4.57.6LayoutLMForTokenClassification 加载
fastapi0.115.6server.py 推理服务
uvicorn0.34.0ASGI 服务器
pydantic2.10.4请求/响应模型

三、分步推理操作流程

一键复现:也可直接运行 bash reproduce.sh ./model 自动完成上述全部步骤(环境检查 → 模型加载 → NPU 推理 → CPU/NPU 精度对比),并给出截图提示。

1. 克隆交付仓

git clone https://gitcode.com/gcw_Pp8UzTBn/layoutlm-funsd-NPU.git
cd layoutlm-funsd-NPU

2. 安装依赖

pip install -r requirements.txt

3. 下载模型权重(国内镜像)

export HF_ENDPOINT=https://hf-mirror.com
pip install -U huggingface_hub
huggingface-cli download philschmid/layoutlm-funsd \
  --local-dir ./model

权重文件清单(共约 432MB):

文件大小用途
pytorch_model.bin430MB主权重(LayoutLMForTokenClassification)
config.json1KB模型配置
tokenizer.json694KBtokenizer(LayoutLMv2 Fast)
vocab.txt226KB词表
preprocessor_config.json199B预处理配置
training_args.bin3.3KB训练参数
handler.py2.3KB官方推理 handler

4. CPU/NPU 精度对比

python inference.py --compare --model ./model --seq_len 64 --output comparison.json

5. 启动推理服务

export MODEL_PATH=./model
python server.py
# 服务监听 0.0.0.0:8000

6. 调用 /v1/chat/completions

curl -X POST http://127.0.0.1:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "philschmid/layoutlm-funsd",
    "messages": [{"role": "user", "content": "Name: John Smith, Address: 123 Main Street"}],
    "max_tokens": 64,
    "temperature": 0.0
  }'

7. 平台可用性测试

在赛事平台「模型评测」tab,选择本模型权重仓库,点击「可用性测试」,等待 10-30 分钟查看日志。

四、完整测试样例及输出

测试 1:CPU/NPU 精度对比

命令:

python inference.py --compare --model /data/models/philschmid/layoutlm-funsd --seq_len 64

输出(摘要):

=== CPU 基线推理 ===
CPU num_non_o: 10, entities: [{'token': 'name', 'label': 'B-QUESTION'}, ...]
CPU time: 3.295s

=== NPU 推理 ===
NPU num_non_o: 10, entities: [{'token': 'name', 'label': 'B-QUESTION'}, ...]
NPU time: 1.537s

=== 精度对比 ===
cosine_similarity: 0.999993  (>0.999)
relative_error:    0.004041  (<0.01)
labels_match:      True
entities_match:    True
num_non_o (防假):  CPU=10 NPU=10 (需>0)
CPU: 3.295s | NPU: 1.537s | speedup: 2.14x

达标判定: ✅ 通过

测试 2:server.py 端到端

命令:

curl -X POST http://127.0.0.1:8000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"philschmid/layoutlm-funsd","messages":[{"role":"user","content":"Name: John Smith, Address: 123 Main Street"}],"max_tokens":64,"temperature":0.0}'

输出(OpenAI 兼容格式):

{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "created": 1755475200,
  "model": "philschmid/layoutlm-funsd",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "Entities: name(B-QUESTION), :(I-QUESTION), ,(B-QUESTION), address(B-QUESTION), :(I-QUESTION), ,(B-QUESTION)"},
      "finish_reason": "stop"
    }
  ],
  "usage": {"prompt_tokens": 64, "completion_tokens": 1, "total_tokens": 65}
}

五、精度与性能验证

指标CPU 基线NPU达标判定
cosine_similarity—0.999993✅ > 0.999
relative_error—0.004041✅ < 0.01
labels_match—True✅
entities_match—True✅
num_non_o(防假)1010✅ > 0
推理时延3.295s1.537sNPU 加速 2.14x

权重路径:pytorch_model.bin sha256:见 CHECKSUM.md

六、平台提交信息

字段内容
原始权重地址https://huggingface.co/philschmid/layoutlm-funsd
项目地址https://gitcode.com/gcw_Pp8UzTBn/layoutlm-funsd-NPU
提交说明philschmid/layoutlm-funsd(LayoutLMForTokenClassification, 约 125M 参数)昇腾 NPU 适配:torch_npu float32 + eager 注意力,CPU/NPU 精度对比 cos=0.999993(误差<1%),标签序列完全一致(FUNSD 表单字段识别),NPU 1.537s(CPU 3.295s,加速 2.14x),模型卡片标注 #NPU

七、资产清单(assets/)

三张官方指定截图(由参赛用户在真机/云 NPU 终端补位):

截图文件名内容要求
Agent 完整适配工作流assets/agent_workflow.png适配全过程,体现完整流程
NPU 硬件设备调用日志assets/npu_device_call.pngnpu-smi info 输出 + 推理 NPU 调用日志
模型最终适配验收结果assets/model_result.png推理输出 + 精度对比结果

八、许可证

本适配仓库遵循原模型许可证(Apache-2.0)。模型权重版权归原模型作者所有。

九、引用

  • 原模型:https://huggingface.co/philschmid/layoutlm-funsd
  • transformers:https://github.com/huggingface/transformers
  • torch_npu:https://gitee.com/ascend/pytorch
  • 昇腾 Model-Agent 大赛:https://competition.gitcode.com/competition/2083120580017467393/intro