g
gcw_C8PI9e90/trocr-small-handwritten-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

trocr-small-handwritten 昇腾 NPU 适配

1. 模型介绍

  • Hugging Face 原始模型:https://huggingface.co/microsoft/trocr-small-handwritten
  • GitCode HF 镜像:https://ai.gitcode.com/hf_mirrors/microsoft/trocr-small-handwritten
  • GitCode 权重文件:https://ai.gitcode.com/hf_mirrors/microsoft/trocr-small-handwritten/blob/main/pytorch_model.bin
  • 任务类型:手写文字识别(OCR)
  • 模型框架:PyTorch + Transformers + torch-npu
  • 模型架构:VisionEncoderDecoderModel (DeiT-small encoder + TrOCR decoder)
  • 输入格式:灰度/彩色手写文字图片
  • 输出格式:识别文本、token ids、解码器 logits
  • 验证环境:Ascend 910, CANN 8.5.1, torch-npu 2.9.0.post1

Synthetic deterministic handwritten-style samples are used for CPU/NPU parity testing; greedy decoding, fp32.

2. NPU 适配说明

本仓库提供独立 CPU/NPU 推理脚本与精度对比脚本。测试严格先运行 CPU、释放资源,再运行昇腾 NPU;NPU 计时前后执行同步,结束后释放缓存。

3. 环境准备

python -m pip install -r requirements.txt --index-url "${PIP_INDEX_URL:-https://pypi.tuna.tsinghua.edu.cn/simple}"

4. 推理命令

python inference.py --device cpu --model-dir /path/to/model --output cpu_inference.json
python inference.py --device npu --model-dir /path/to/model --output npu_inference.json

5. CPU/NPU 精度测试

python compare_cpu_npu.py --model-dir /path/to/model --output comparison.json
指标CPUNPU
推理耗时4867.2091 ms200.8611 ms
精度指标实测值
主指标mean_relative_error_pct
CPU/NPU 主误差0.3196435885%
最大相对误差15016.25981%
最大绝对误差0.3981361389
余弦相似度0.9999893027
语义输出一致False

结论:NPU 与 CPU 推理结果误差小于 1%。

6. 性能说明

性能数据与输入大小、批量、预热、模型规模和首次算子编译有关。上表为本次真实运行结果,不作为跨模型性能排名。

7. 执行记录

7.1 Agent 完整适配工作流

Agent 完整适配工作流

7.2 NPU 硬件设备调用日志

NPU 硬件设备调用日志

7.3 模型最终适配验收结果

模型最终适配验收结果