昇腾 Model-Agent 模型适配大赛(第二季)· 适配赛道交付件 基于 Ascend 910 NPU + torch_npu 的 image-text-to-text 推理适配(FUNSD 表单字段识别),CPU/NPU 精度对齐 cos=0.999993 / rel_err=0.004。
/v1/chat/completions(文档理解,识别表单字段)昇腾 NPU 平台环境已提供以下基础组件:
| 组件 | 版本 |
|---|---|
| Python | 3.11.14 |
| PyTorch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| CANN | 8.5.1 |
| NPU 驱动 | npu-smi 25.5.5 |
| 硬件 | Ascend 910 × 2(HBM 64GB/卡) |
应用层依赖见 requirements.txt:
| 包 | pin 版本 | 用途 |
|---|---|---|
| transformers | 4.57.6 | LayoutLMForTokenClassification 加载 |
| fastapi | 0.115.6 | server.py 推理服务 |
| uvicorn | 0.34.0 | ASGI 服务器 |
| pydantic | 2.10.4 | 请求/响应模型 |
一键复现:也可直接运行
bash reproduce.sh ./model自动完成上述全部步骤(环境检查 → 模型加载 → NPU 推理 → CPU/NPU 精度对比),并给出截图提示。
git clone https://gitcode.com/gcw_Pp8UzTBn/layoutlm-funsd-NPU.git
cd layoutlm-funsd-NPUpip install -r requirements.txtexport HF_ENDPOINT=https://hf-mirror.com
pip install -U huggingface_hub
huggingface-cli download philschmid/layoutlm-funsd \
--local-dir ./model权重文件清单(共约 432MB):
| 文件 | 大小 | 用途 |
|---|---|---|
| pytorch_model.bin | 430MB | 主权重(LayoutLMForTokenClassification) |
| config.json | 1KB | 模型配置 |
| tokenizer.json | 694KB | tokenizer(LayoutLMv2 Fast) |
| vocab.txt | 226KB | 词表 |
| preprocessor_config.json | 199B | 预处理配置 |
| training_args.bin | 3.3KB | 训练参数 |
| handler.py | 2.3KB | 官方推理 handler |
python inference.py --compare --model ./model --seq_len 64 --output comparison.jsonexport MODEL_PATH=./model
python server.py
# 服务监听 0.0.0.0:8000/v1/chat/completionscurl -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
}'在赛事平台「模型评测」tab,选择本模型权重仓库,点击「可用性测试」,等待 10-30 分钟查看日志。
命令:
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
达标判定: ✅ 通过命令:
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(防假) | 10 | 10 | ✅ > 0 |
| 推理时延 | 3.295s | 1.537s | NPU 加速 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 |
三张官方指定截图(由参赛用户在真机/云 NPU 终端补位):
| 截图 | 文件名 | 内容要求 |
|---|---|---|
| Agent 完整适配工作流 | assets/agent_workflow.png | 适配全过程,体现完整流程 |
| NPU 硬件设备调用日志 | assets/npu_device_call.png | npu-smi info 输出 + 推理 NPU 调用日志 |
| 模型最终适配验收结果 | assets/model_result.png | 推理输出 + 精度对比结果 |
本适配仓库遵循原模型许可证(Apache-2.0)。模型权重版权归原模型作者所有。