#NPU Ascend 910B TrOCR Printed OCR - VisionEncoderDecoder on npu:0
microsoft/trocr-base-printed (base-sized, fine-tuned on SROIE printed text)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.binmodel (公开), 标签: NPU, Ascend, Ascend910RGB 任意尺寸, 由 TrOCRProcessor (ViTImageProcessor) 自动 resize 到 384x384, normalize mean 0.5 std 0.5, 输出 pixel_values [B,3,384,384] float32HELLO WORLD 123, 字体 DejaVuSans 40, 保存 /tmp/trocr_synth_input.png sha256[:16]=2e1de5f985089e1ePIL.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)npu:0 (Chip 8, Bus 0000:0A:00.0, 64GB HBM)pip install -r requirements.txt
# torch_npu 已预装在 base 镜像, 无需额外安装
# 权重自动从 hf-mirror 下载, 或使用本地缓存 /opt/atomgit/adapt-npu-agent/model_cache/microsoft-trocr-base-printed默认命令 (真实 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_9362Model first param device: npu:0Input tensor device: npu:0NPU 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) 与解码文本cpu_ids.npy vs npu_ids.npyCPU-NPU token ids equal: True max_abs_diff 0.000000, text_equal: Truepython 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_diff | 0.0 |
| text | THANK 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 deviceassets/model_result.png - python inference.py 完整真实输出 (含 schema、预测、同步耗时、PASS)

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