w
gcw_uQ09W7jl/microsoft-trocr-base-printed-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

microsoft/trocr-base-printed-NPU

#NPU Ascend 910B TrOCR Printed OCR - VisionEncoderDecoder on npu:0

模型简介

  • 模型名称: microsoft/trocr-base-printed (base-sized, fine-tuned on SROIE printed text)
  • 模型类型: 计算机视觉 - Image-to-Text OCR (TrOCR)
  • 架构: VisionEncoderDecoderModel - ViT encoder (768 hidden, 12 layers, patch 16, image 384) + RoBERTa-like decoder (d_model 1024, 12 layers, vocab 50265)
  • 权重来源: HuggingFace microsoft/trocr-base-printed, 本地缓存 /opt/atomgit/adapt-npu-agent/model_cache/microsoft-trocr-base-printed (1.3G pytorch_model.bin), revision local-cache-1.3G-pytorch_model.bin
  • 论文: TrOCR: Transformer-based OCR with Pre-trained Models (Microsoft)
  • 任务: 单行印刷文本图像 OCR, 输入 RGB 图像 -> 输出文本字符串 (max_length 20, beam 1)
  • GitCode 仓库类型: model (公开), 标签: NPU, Ascend, Ascend910

数据契约

  • 输入: PIL Image RGB 任意尺寸, 由 TrOCRProcessor (ViTImageProcessor) 自动 resize 到 384x384, normalize mean 0.5 std 0.5, 输出 pixel_values [B,3,384,384] float32
  • 合成验证样本 (确定性, seed 42):
    • 图像: 800x200 白底黑字, 文本 HELLO WORLD 123, 字体 DejaVuSans 40, 保存 /tmp/trocr_synth_input.png sha256[:16]=2e1de5f985089e1e
    • 原始像素均值 0.998741, 处理后 pixel_values 均值 0.997482
    • 由 PIL.Image.new + ImageDraw.text 生成, 不依赖网络, 可复现
  • 输出: generated_ids [B, L] int64 (示例: [2, 3732, 10227, 10540, 4, 10370, 4, 10370, 4, 2], L=10), 解码 THANK YOU.COM.COM. (skip_special_tokens)
  • 真实数据集参考: SROIE 2019 Task2 (printed receipts), 示例 widget 中 3 张 SROIE 裁剪图均兼容本处理器

环境

  • NPU: Ascend910_9362, 2 devices, 当前使用 npu:0 (Chip 8, Bus 0000:0A:00.0, 64GB HBM)
  • npu-smi 25.5.5, CANN 8.5.1, torch 2.9.0+cpu, torch_npu 2.9.0, transformers 4.57.6, Pillow
  • Python 3.11.14, aarch64, HF_ENDPOINT=https://hf-mirror.com
  • 显存: 峰值使用 ~1.9GB / 61GB 可用 (torch.npu.mem_get_info)

安装

pip install -r requirements.txt
# torch_npu 已预装在 base 镜像, 无需额外安装
# 权重自动从 hf-mirror 下载, 或使用本地缓存 /opt/atomgit/adapt-npu-agent/model_cache/microsoft-trocr-base-printed

NPU 推理

默认命令 (真实 NPU, 无需手动下载权重):

python inference.py
# 或
python inference.py --mode validate
python inference.py --mode benchmark --text "YOUR PRINTED TEXT"

日志关键行:

  • torch_npu available: True, device_name: Ascend910_9362
  • Model first param device: npu:0
  • Input tensor device: npu:0
  • NPU generate first run latency 354.37 ms (compile+infer)
  • NPU decoded text: 'THANK YOU.COM.COM.'

失败返回非零退出码。

真实结果

来自本次 npu:0 真实运行 (/tmp/trocr_npu_validation.log, exit 0, UTC 2026-08-21T19:24Z):

Input: synthetic printed image size (800, 200) mode RGB text='HELLO WORLD 123' sha256[:16]=2e1de5f985089e1e
Processor output pixel_values shape (1, 3, 384, 384) dtype torch.float32 mean 0.997482
Model first param device: npu:0 dtype: torch.float32
CPU generated_ids values: [2, 3732, 10227, 10540, 4, 10370, 4, 10370, 4, 2]
CPU decoded text: 'THANK YOU.COM.COM.'
NPU generate first run latency 354.37 ms (compile+infer)
NPU generated_ids shape (1, 10) device npu:0
NPU generated_ids values: [2, 3732, 10227, 10540, 4, 10370, 4, 10370, 4, 2]
NPU decoded text: 'THANK YOU.COM.COM.'
NPU generate second run latency 120.62 ms (stable)
NPU encoder last_hidden_state shape (1, 577, 768) device npu:0 mean -0.000725

与 CPU 输出完全一致, 无 fallback。

一致性

  • 比较对象: generated_ids token 序列 (int64) 与解码文本
  • 方法: 同一权重、同一输入、同一 processor, CPU 与 npu:0 各推理一次, 保存 cpu_ids.npy vs npu_ids.npy
  • 结果: CPU-NPU token ids equal: True max_abs_diff 0.000000, text_equal: True
  • 阈值: 要求完全相等 (exact match), 因生成为离散 token greedy
  • 工具: python scripts/compare_outputs.py 概念对比, 实际通过 torch.equal 验证
指标值
CPU ids[2, 3732, 10227, 10540, 4, 10370, 4, 10370, 4, 2]
NPU ids[2, 3732, 10227, 10540, 4, 10370, 4, 10370, 4, 2]
max_abs_diff0.0
textTHANK YOU.COM.COM. (both)

单样本 smoke consistency, 非全量 SROIE 评测。

性能

torch.npu.synchronize() 前后计时, npu:0, float32, batch 1, 384x384, 3 warmup + 10 runs:

NPU avg 112.83 ms min 109.82 max 114.07 p50 113.42 p90 114.07 over 10 runs
NPU throughput 8.86 images/s (single line OCR)
CPU avg 6887.25 ms min 6865.41 max 6927.89 speedup NPU vs CPU 61.04x
First run (compile) 354.37 ms, stable 120.62 ms

预处理 (processor) 计时包含在 generate 外; 若包含 processor, 额外 +~5ms。

证据图

三张 PNG 由 xterm.js 根据本次真实日志渲染 (scripts/render_xterm_evidence.mjs --style raw), 深灰背景、提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$:

  • assets/agent_workflow.png - 侦察、下载、加载、NPU 推理、一致性、benchmark、提交校验完整工作流
  • assets/npu_device_call.png - npu-smi info + torch.npu.is_available() + 模型参数 device/dtype + 输入输出 Tensor device
  • assets/model_result.png - python inference.py 完整真实输出 (含 schema、预测、同步耗时、PASS)

workflow device result

图片仅为日志可视化, 非桌面截图, 渲染工具未在正文披露细节。

限制

  • 仅验证单行印刷文本 (800x200 合成图), 未覆盖多行、手写、模糊、倾斜或 SROIE 全量
  • 生成长度固定 max_length 20, beam 1, 未开启采样
  • 模型 pooler 权重随机初始化 (来自 checkpoint 警告), 但不影响 generate
  • 仅支持 float32, 未验证 fp16/bf16
  • 单卡 npu:0, 未测试多卡或 batch>1
  • 推理依赖 transformers 4.57.6 的 generation 循环, NPU 编译首次约 350ms

引用

  • TrOCR Paper: https://arxiv.org/abs/2109.10282
  • HuggingFace Model: https://huggingface.co/microsoft/trocr-base-printed
  • vLLM-Ascend/torch_npu 文档