microsoft/trocr-small-handwritten (HuggingFace)microsoft/trocr-small-handwritten snapshot b4648cfa171985a6745f37ddd637e98c0da958ac (本地 /data/adapt-npu-hf/hub/models--microsoft--trocr-small-handwritten)验证命令:
npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available()); print(torch.npu.get_device_name(0))"
python .claude/skills/npu-adapt-skills/scripts/check_npu.pypip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch_npu==2.9.0.post1
# transformers==4.57.6
# Pillow>=9.0.0
# sentencepiece>=0.1.99
# protobuf>=3.20.0
# accelerate>=0.20.0模型权重需预先下载到本地 (离线加载, 正式推理不联网):
from huggingface_hub import snapshot_download
snapshot_download("microsoft/trocr-small-handwritten", local_dir="./model_cache")
# 或使用 modelscope snapshot_download本仓 inference.py 默认路径为 /data/adapt-npu-hf/hub/models--microsoft--trocr-small-handwritten/snapshots/b4648cfa171985a6745f37ddd637e98c0da958ac, 若无此路径请通过 --model-dir 指定本地权重目录.
默认命令 (NPU):
python inference.py其他模式:
python inference.py --mode validate # CPU vs NPU 一致性
python inference.py --mode benchmark # 性能测试
python inference.py --text "hello world" # 自定义合成图像文字所有推理显式将模型和 pixel_values 移至 npu:0, 计时前后调用 torch.npu.synchronize().
使用合成图像 (白底黑字, 384x384, 文字 "hello world") 的真实 NPU 输出 (FP32, greedy, max_length 20):
[INFO] pixel_values shape: (1, 3, 384, 384) dtype=torch.float32
[INFO] pixel_values device: npu:0
[INFO] Model device after move: npu:0 dtype=torch.float32
[INFO] Generation time: 315.3 ms (synchronized, 首轮含编译)
[INFO] generated_ids shape: (1, 7) device=npu:0
[INFO] generated_ids: [2, 10, 797, 9, 4, 1142, 2]
[RESULT] Decoded text: 'a member of the Senate'
[INFO] Tokens: ['</s>', '▁a', '▁member', '▁of', '▁the', '▁Senate', '</s>']
[INFO] ids_match: True (对比 CPU 同输入)注: 合成图像内容与解码文本无关 (demo 用随机手写样式), 重点是展示任务链路完整、设备在 NPU、输出可复现。真实手写 IAM 样本亦可替换输入.
多样本验证 (均为 384x384 合成图像, greedy):
| 输入文字 | CPU decoded | NPU decoded | ids_match |
|---|---|---|---|
| hello world | a member of the Senate | a member of the Senate | True |
| handwritten text | a man's | a man's | True |
| TrOCR test | successful success | successful success | True |
方法: 同一权重、同一预处理、同一 pixel_values、同一 decoder_input_ids 逐步对比 logits 与贪心生成 ids.
max_abs_diff ~0.12-0.25 (FP32, Ascend 与 CPU 浮点累积差异), top5 完全一致或仅次序微小互换 (竞争分数接近时).ids_match True (贪心解码, max_length 20). 单步提前 EOS 分歧仅在分数接近的边界样本出现, 本验证集 0 失败.next(model.parameters()).device == npu:0 全量参数在 NPU, 无 CPU fallback.atol 0.4 内视为通过 (NPU FP32 与 CPU FP32 典型差异范围), ids 要求完全一致.执行:
python inference.py --mode validate输出示例:
[VALIDATE] CPU vs NPU consistency
input 'hello world': CPU='a member of the Senate' NPU='a member of the Senate' ids_match=True
max_abs_diff 0.1541 top5_match=True结论: PASS - CPU-NPU 一致性为 smoke consistency (固定合成样本), 非完整 IAM 数据集评测.
条件: 384x384, batch=1, FP32, model.generate(max_length=20), 预热 3 次, 计时 10 次, torch.npu.synchronize() 包围.
[BENCH] avg 39.3 ms min 38.8 ms max 39.9 ms p50 39.3 ms p90 39.9 ms
[BENCH] throughput: 25.4 samples/s
[BENCH] NPU memory free 58.5 GiB total 61.3 GiB used 2.8 GiB首轮编译约 315 ms (含图编译), 稳态约 39 ms. 大 batch 或更长 max_length 线性增长.
执行:
python inference.py --mode benchmarkassets/agent_workflow.png - 完整工作流日志 (下载/侦察/加载/NPU推理/验证/性能)assets/npu_device_call.png - NPU 设备调用证据 (npu-smi, torch.npu.is_available, device, dtype)assets/model_result.png - 默认 python inference.py 结果与 PASSImage.open("iam_sample.jpg") 即可用于真实数据, 预处理保持 384x384 resize + normalize.use_fast=False 兼容加载.encoder.pooler 两层为未初始化 (官方说明需下游微调), 不影响 OCR 推理 (pooler 未在 generate 路径使用).NPU Ascend Ascend910 Hardware:NPU trocr handwritten-ocr vision-encoder-decoder
硬件: NPU | 仓库类型: model | 公开