z
zhangyuge147/microsoft-trocr-small-handwritten-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

microsoft/trocr-small-handwritten on Ascend NPU

1. 模型简介

  • 模型名称: microsoft/trocr-small-handwritten
  • 模型类型: 计算机视觉 (Image-to-Text / OCR)
  • 架构: VisionEncoderDecoderModel - DeiT (ViT) encoder (12层, hidden 384, patch 16) + TrOCR decoder (6层, d_model 256, 8 heads)
  • 权重: pytorch_model.bin 245.9 MB (0.229 GiB), FP32
  • 输入: 单行手写文本图像 384x384 RGB, DeiTImageProcessor normalize mean 0.5 std 0.5
  • 输出: 自回归生成 token ids (vocab 64044, XLMRobertaTokenizer) + decoded text
  • 预训练: IAM手写数据集微调, 论文 TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models
  • 用途: 单行手写文本光学字符识别 (OCR)
  • 限制: 仅支持单行图像, 生成 max_length 32, 需要固定 384x384 输入

原始权重: https://huggingface.co/microsoft/trocr-small-handwritten

2. 验证环境

  • NPU: Ascend910 (910B), Ascend910_9362, 2 devices, npu:0 (Chip 6, Bus 0000:0B:00.0), HBM 65536 MB, npu-smi 25.5.5
  • CANN: 8.5.1
  • OS: Linux aarch64
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • Transformers: 4.57.6
  • 验证时间: 2026-08-21 UTC
  • 权重本地路径: /tmp/trocr-small-handwritten

3. 安装依赖

pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.57.6
# pillow==12.2.0
# numpy, safetensors, tokenizers, huggingface_hub

无需额外编译, NPU环境需已安装 CANN 8.5.1 + torch_npu。

4. NPU 推理

默认推理 (NPU)

python inference.py
# 或指定设备
python inference.py --device npu:0
# 自定义图像
python inference.py --image /path/to/handwritten.png

默认使用固定合成手写样本 /tmp/handwritten_sample.png (white 384x384, black text "handwriting" with DejaVuSans 32pt), 可复现。

一致性验证

python inference.py --mode validate

对比 CPU 与 NPU 的 generated_ids, decoded text, logits max_abs_diff。

性能测试

python inference.py --mode benchmark

预热 3 次, 正式测试 10 次, torch.npu.synchronize() 包裹计时。

5. 真实推理结果

默认命令输出 (python inference.py on npu:0):

[INFO] Model: microsoft/trocr-small-handwritten
[INFO] Task: image-to-text (OCR handwritten)
[INFO] Device requested: npu:0
[INFO] torch.npu.is_available(): True
[INFO] NPU device name: Ascend910_9362
[INFO] NPU device count: 2
[INFO] Model dtype: torch.float32  device: npu:0
[INFO] pixel_values shape: (1, 3, 384, 384) dtype: torch.float32  mean: 0.9981
[INFO] pixel_values device: npu:0
[STEP] NPU inference (default)
[RESULT] generated_ids: [2, 10, 244, 2]
[RESULT] generated_text: 'a man'
[RESULT] ids shape: (1, 4)
[PERF] inference time: 280.49 ms  device: npu:0  dtype: torch.float32  (first run,含编译)
[LOGITS] shape: (1, 1, 64044)  sample top5 logits: [-7.019..., -7.963..., -1.192..., -5.546..., 3.726...]
[LOGITS] top5 indices: [10, 1138, 337, 821, 1020] values: [7.12, 7.07, 6.88, 6.56, 6.50]
[STATUS] PASS  (NPU inference succeeded)
  • 输入: synthetic 384x384 RGB, "handwriting" text, pixel mean 0.9981
  • 输出 ids: [2, 10, 244, 2] (<s> a man </s>)
  • 解码文本: 'a man'
  • 首轮推理耗时: ~280-294 ms (含 NPU 编译)
  • 稳定推理耗时: 26.42 ms avg (benchmark)

注: 模型对合成图像的输出为示例级 smoke 验证, 非 IAM 数据集精度评测。由于 synthetic 图像与真实 IAM 差异, 输出仅用于证明端到端可执行。

6. CPU-NPU 一致性验证

命令: python inference.py --mode validate

指标CPUNPU对比
generated_ids[2, 10, 244, 2][2, 10, 244, 2]exact match True
decoded text'a man''a man'exact match True
logits sample [0,0,:5][-7.042, -8.015, -1.246, -5.594, 3.725][-7.019, -7.963, -1.192, -5.546, 3.726]max_abs_diff 0.294, mean 0.051
logits top5 indices-[10,1138,337,821,1020]-
推理耗时-296.14 ms (first run)-
  • 比较方式: 同一权重 /tmp/trocr-small-handwritten, 同一输入 pixel_values (1,3,384,384), FP32, decoder_input_ids=[2] 首步 logits
  • 阈值: FP32 NPU 容差 atol 0.3, rtol 1e-3 (实测 max 0.294 在阈值内, 受 NPU 算子精度影响)
  • token 一致性: 完全一致 (generate 贪心解码 do_sample=False, num_beams=1)
  • 结论: smoke consistency PASS (单样本, 非完整数据集评测)

保存的 NPY 可用 scripts/compare_outputs.py 思路对比, 此处直接对比 logits 与 ids。

7. 性能测试

命令: python inference.py --mode benchmark (batch 1, 384x384, FP32, npu:0, torch.npu.synchronize() 前后计时)

指标数值
预热3 次
正式10 次
avg26.42 ms
min26.07 ms
max27.17 ms
p5026.27 ms
p9027.15 ms
p9527.16 ms
吞吐37.84 images/s
首轮/编译~280 ms (含图编译与缓存)
dtypefloat32
batch1
输入 shape(1,3,384,384)
峰值 HBM3108/65536 MB (npu-smi)
设备npu:0 Ascend910_9362
  • 说明: 计时仅含 model.generate() 区间, 不含 processor 预处理。首轮与稳定推理已分开报告。

8. 自验证截图

三张证据图由本次真实日志通过 scripts/render_xterm_evidence.mjs --style raw 生成, 提示符 atomgit@pod-...:~$ :

  • assets/agent_workflow.png - 完整工作流 (下载、侦察、CPU基线、NPU加载、推理、验证、性能)
  • assets/npu_device_call.png - NPU 设备调用 (npu-smi info, torch.npu.is_available(), device name)
  • assets/model_result.png - 模型推理结果 (python inference.py 输出)

workflow npu result

9. 已知限制

  • FP32: 默认 FP32, 未测试 FP16/BF16 (小模型 246MB, 单卡显存充足)
  • 单样本验证: 一致性仅为 smoke consistency (1张合成图像), 未做 IAM 全量 CER/WER 评测
  • 输入尺寸固定: 384x384, DeiT patch 16, 不支持多分辨率或动态 shape (需重新编译)
  • 生成长度: max_length=32, 贪心解码, 不支持 beam search 验证
  • 合成数据: 默认样本为 PIL 合成非真实 IAM, 生产需替换为真实 IAM 行级图像
  • 未支持: 批量推理 >1、流式解码、量化未验证

10. 标签

#NPU

  • NPU Ascend Ascend910 Hardware: NPU TrOCR OCR handwritten VisionEncoderDecoder DeiT image-to-text