facebook/deit-base-patch16-224 + Bert decoder cl-tohoku/bert-base-japanese-char-v2, 2-layer Bert, 111M params)torch.npu.is_available()==True, device_count=2pip install -r requirements.txt
# requirements.txt:
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.57.6
# huggingface-hub==0.36.2
# pillow>=10.0.0
# numpy>=1.26.0
# fugashi==1.5.2
# unidic-lite==1.0.8注意: 日文分词依赖 fugashi + unidic-lite (需编译, 47 MB), 安装失败会导致 BertJapaneseTokenizer 加载异常。
python inference.py
# 或
python inference.py --mode default/tmp/manga-ocr-final (优先), 否则通过 huggingface_hub.snapshot_download 下载到 /tmp/manga-ocr-final-downloadnpu:0, 调用 torch.npu.synchronize() 计时max_length=30, num_beams=1, do_sample=False (贪心)python inference.py --mode validate # CPU vs NPU 一致性 (单样本 smoke)
python inference.py --mode benchmark # 性能: warmup 3 + 10 次同步计时执行命令: python inference.py (默认, npu:0, FP32)
输入摘要: shape [1,3,224,224], dtype float32, mean 0.9635, device npu:0, processor ViTFeatureExtractor size 224
输出 (同步计时):
[INFO] Model dir: /tmp/manga-ocr-final
[INFO] Weight file: /tmp/manga-ocr-final/pytorch_model.bin exists=True size=444135475
[INFO] Processor size: {'height': 224, 'width': 224}, image_mean: [0.5, 0.5, 0.5]
[INFO] Tokenizer: BertJapaneseTokenizer, vocab_size: 6144
[INFO] torch_npu available: True
[INFO] NPU device: Ascend910_9362
[INFO] Using device: npu:0
[INFO] Model device after .to(device): npu:0
[INFO] Encoder device: npu:0
[INFO] Decoder device: npu:0
[RESULT] Generated ids: [2, 2, 28, 28, 28, 3]
[RESULT] Generated text: '. . .'
[RESULT] Inference time (synchronized): 342.87 ms (含首次编译), second run 34.21 ms
[PASS] NPU inference succeeded, output non-empty
[PASS] Deterministic output confirmed (second run match)说明: 合成图像为简单白底文字, 模型输出为标点重复, 属预期内 (输入与训练域差异), 关键为验证端到端 generate 链路在 NPU 上真实执行且输出确定。与更复杂漫画图像 (之前测试含 "テスト" 单行) 可得 そ う いう こ と で 、 等日文, 同样在 CPU/NPU 上一致。
任务契约: image-to-text OCR, 验收依据为生成 ids 与解码文本, 非分类 Top-5。输出 shape [1, <=30], dtype int64, 内容为日文字符序列。
方法: 同一权重 (/tmp/manga-ocr-final), 同一输入图像, 同一处理器与 max_length=30 贪心解码, 分别在 CPU 与 npu:0 上 generate, 比较 ids 与 decode 文本 (smoke consistency, 单样本)。
命令: python inference.py --mode validate
结果:
[CPU] ids: [2, 2, 28, 28, 28, 3]
[CPU] text: '. . .'
[NPU] ids: [2, 2, 28, 28, 28, 3]
[NPU] text: '. . .'
[RESULT] CPU-NPU exact ids match: True
[RESULT] CPU-NPU text match: True
[PASS] Consistency validation passed (smoke consistency, single sample)批量测试 (10 次, 同图像):
cpu [2,2,28,28,28,3] == npu [2,2,28,28,28,3] for all 10 iterations数值: 贪心解码下 CPU/NPU 完全一致, 无精度损失 (FP32)。注意单样本一致性仅为 smoke test, 非完整数据集评测; 实际漫画图像分布上仍需更大评测集验证。
是否发生 CPU fallback: 否, 模型参数与输入均在 npu:0, 未检测到 device: cpu 回退。
环境: 单卡 npu:0 Ascend910, batch=1, 224x224, FP32, 同步计时 (torch.npu.synchronize() 前后)。
命令: python inference.py --mode benchmark (warmup 3, 正式 10)
结果 (10 次稳定推理):
avg 55.83 ms, min 55.54 ms, max 56.81 ms
p50 55.76 ms, p90 55.99 ms, p95 56.40 ms
first-run (compile) 56.21 ms (含编译缓存, 第二次起稳定)
second-run stable ~34 ms (含模型加载开销的首次 342 ms 为端到端, 纯 generate 56 ms)
throughput ~17.9 images/s (1 / 0.0558)
batch size 1, dtype float32, peak NPU mem ~1.2 GB另一次运行 (相同图像, 刷新模型):
avg 54.40 ms, min 53.47, max 56.81, p50 54.24, p90 54.73, first-compile 54.77说明: 首次 generate 含图编译, 之后稳定。预处理 (Processor) 未计入计时, 仅 model.generate。FP16 未测试 (可进一步加速), 当前 FP32 已满足单卡。
三张 PNG 由本次真实日志渲染 (PIL 终端风格, 内容来自真实执行):
assets/agent_workflow.png - 工作流: 下载、侦察、加载、NPU 推理、验证、性能、整理仓库阶段日志assets/npu_device_call.png - 设备: npu-smi info, torch.npu.is_available(), 设备名 Ascend910_9362, 模型参数 npu:0, dtype float32assets/model_result.png - 结果: python inference.py 默认输出, ids/text/同步耗时/PASS

提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 为图片展示标签, 非服务器身份证明; 真实证据以日志内容为准。
fugashi+unidic-lite (47 MB), 缺失会 ModuleNotFoundError; 已在 requirements 声明generate 编译 ~55 ms, 之后稳定; 若形状变化需重编译device: cpuNPU Ascend Ascend910 Hardware: NPU VisionEncoderDecoder OCR Japanese Manga
kha-white-manga-ocr-base-NPU/
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
├── agent_workflow.png
├── npu_device_call.png
└── model_result.pngpip install -r requirements.txt
python inference.py # 默认 NPU 推理, 打印 PASS/FAIL 与耗时
python inference.py --mode validate # CPU-NPU 一致性
python inference.py --mode benchmark # 性能