kha-white/manga-ocr-baseVisionEncoderDecoderModel
ViT facebook/deit-base-patch16-224 — hidden_size 768, 12 layers, 12 heads, patch_size 16, image_size 224, intermediate 3072BertForMaskedLM cl-tohoku/bert-base-japanese-char-v2 — hidden_size 768, 2 layers, 12 heads, vocab_size 6144 (character-level Japanese, vocab.txt), max_length 300, decoder_start_token_id 2 ([CLS])vision-encoder / OCR image-to-text — 非分类模型,使用生成文本一致性验收(CPU vs NPU token exact match)transformers VisionEncoderDecoderModel + ViTFeatureExtractor, 本地权重加载 (snapshot_download 缓存 /opt/atomgit/.cache/huggingface/hub/models--kha-white--manga-ocr-base), trust_remote_code=False, 手动 vocab.txt 字符解码避免 fugashi/unidic_lite 依赖pytorch_model.bin + processor/tokenizer 配置| 项 | 值 |
|---|---|
| NPU | Ascend910_9362 (npu-smi 25.5.5, CANN 8.5.1) |
| 设备 | npu:0 (PhyID 6, Bus 0000:0B:00.0), 64GB HBM, 2 devices |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
| torch.npu.is_available() | True |
| transformers | 4.40.0 |
| huggingface_hub | 0.36.2 |
| Pillow | 11.x |
| Python | 3.11.14 |
| 验证日期 | 2026-08-21 UTC |
| 输入 | 合成确定性图像 224x224x3 白底+黑色2px边框, 零随机, ViT 224 |
| dtype | float32 (FP32) — Base 模型 FP32 原生, 未做 FP16/BF16 切换 |
| batch | 1 |
环境检查命令:
npu-smi info
python -c "import torch; import torch_npu; print(torch.npu.is_available()); print(torch.npu.get_device_name(0))"
python scripts/check_npu.py --output env_check.jsonpip install -r requirements.txt
# requirements.txt
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.40.0
# huggingface_hub==0.34.0
# Pillow>=10.0.0
# numpy>=1.24.0系统已配置 HF_ENDPOINT=https://hf-mirror.com 与代理,无需手动设置。
模型下载(首次):
python -c "from huggingface_hub import snapshot_download; print(snapshot_download(repo_id='kha-white/manga-ocr-base'))"
# 缓存至 ~/.cache/huggingface/hub/models--kha-white--manga-ocr-base默认命令(npu:0 真实推理,3次 warmup + 10次计时,含 CPU-NPU 一致性检查):
python inference.py其他模式:
python inference.py --mode validate # 显式 CPU-NPU 一致性验证
python inference.py --mode benchmark # 10次稳定计时 + 吞吐
python inference.py --device cpu # 纯 CPU 对比
python inference.py --model-dir /path/to/local/snapshotinference.py 默认打印: 模型名/任务/后端/device/dtype/输入摘要/真实生成文本与 token ids/分位延迟/吞吐/显存/最终 PASS/FAIL。
NPU npu:0 FP32, 合成图 224x224, max_length=30, num_beams=1, decoder_start_token_id=2:
Mode: default, Device: npu:0, Model dir: /opt/atomgit/.cache/huggingface/hub/models--kha-white--manga-ocr-base/snapshots/aa6573bd10b0d446cbf622e29c3e084914df9741
Model: kha-white/manga-ocr-base
Model weight URL: https://huggingface.co/kha-white/manga-ocr-base
Task: 图像 OCR (VisionEncoderDecoder image-to-text)
Architecture: VisionEncoderDecoderModel (ViT encoder + BERT Japanese char decoder, 111M)
Loading backend: transformers VisionEncoderDecoderModel + ViTFeatureExtractor, trust_remote_code=False
Weight: .../pytorch_model.bin (444MB, FP32)
Requested device: npu:0, dtype: float32
torch.npu.is_available(): True, device_name: Ascend910_9362, device_count: 2
Vocab: 6144 tokens (vocab.txt), sample: [CLS]=2 [SEP]=3 こ=869
Processor: ViTFeatureExtractor size=224, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]
Loading model from ... (0.34s, 111.01M)
Encoder: vit hidden=768 layers=12
Decoder: bert hidden=768 layers=2 vocab=6144
Decoder start token id: 2
Model moved to npu:0, first param device: npu:0, dtype: torch.float32
Input: synthetic deterministic image 224x224x3, white+black pattern, seed deterministic
Preprocessed pixel_values shape: (1, 3, 224, 224), dtype: torch.float32, range: [-1.000, 1.000]
Input device after .to(npu:0): npu:0
Warmup 3x ...
Running NPU inference (torch.npu.synchronize around generate) ...
Iter 1: ids=[2, 2, 879, 926, 897, 828, 3] ... tokens=['[CLS]', '[CLS]', 'そ', 'れ', 'は', '、', '[SEP]'] ... text='それは、' latency=32.6ms
Latency (n=10) avg=31.9ms min=31.6ms max=32.6ms p50=31.8ms p90=32.6ms p95=32.6ms
Throughput: 31.30 images/s (batch=1)
NPU memory free=64.80GB total=65.79GB used=0.98GB
CPU-NPU consistency validation (greedy, max_length=30, same seed image) ...
CPU ids: [2, 2, 879, 926, 897, 828, 3]
NPU ids: [2, 2, 879, 926, 897, 828, 3]
Exact token id match: True
CPU text: 'それは、'
NPU text: 'それは、'
Text match: True
[PASS] CPU-NPU consistency (exact match)
Model param device: npu:0
FINAL: PASS - NPU inference successful, CPU-NPU consistent, latency reportedViTFeatureExtractor 后 pixel_values shape (1,3,224,224) float32 [-1,1]ids=[2,2,879,926,897,828,3] 对应 ['[CLS]','[CLS]','そ','れ','は','、','[SEP]'] 文本 それは、 (soreha, 常见日文开头)smoke consistency — 单张合成图的 CPU vs NPU greedy 生成 exact match(token ids 完全一致,文本一致),非完整数据集评测。已通过 stricter exact match,比近似阈值更可靠。| 检查 | 结果 |
|---|---|
| CPU ids | [2, 2, 879, 926, 897, 828, 3] |
| NPU ids | [2, 2, 879, 926, 897, 828, 3] |
| Exact match | True |
| CPU text | それは、 |
| NPU text | それは、 |
| Text match | True |
| 模型参数 device | npu:0 |
| dtype | float32 |
| 判定 | [PASS] CPU-NPU consistency |
测试方法: 同步合成图, max_length=30, num_beams=1, greedy, 分别在 cpu 与 npu:0 上 model.generate,比较 torch.equal 与字符串相等。NPU 计时均包裹 torch.npu.synchronize()。
All timings with torch.npu.synchronize() before/after model.generate, warmup 3, measured 10:
| 指标 | 值 |
|---|---|
| batch | 1, image 224x224x3, FP32, npu:0 |
| avg | 31.9 ms |
| min | 31.6 ms |
| max | 32.6 ms |
| p50 | 31.8 ms |
| p90 | 32.6 ms |
| p95 | 32.6 ms |
| throughput | 31.30 images/s |
| 单次生成 (含解码) | ~32 ms (端到端 VisionEncoderDecoder.generate) |
| 模型加载 | 0.34s |
| 权重 | 444MB |
| 峰值 NPU 显存 | used 0.98GB / 64.80GB free / 65.79GB total |
| Warmup | 3, 测量 10 |
--mode benchmark 复用同一计时策略。大模型可降低次数并在 README 说明,本模型 10 次已稳定。
三张终端证据图由本次真实日志通过 scripts/render_xterm_evidence.mjs --style raw 生成:
assets/agent_workflow.png — 下载/侦察/环境/NPU 推理/验证/性能全流程assets/npu_device_call.png — npu-smi info, torch.npu.is_available(), 设备名, npu:0 参数设备/dtypeassets/model_result.png — python inference.py 真实输出, 含生成文本, 延迟, PASS

smoke consistency (单张合成图 CPU vs NPU exact match),非 Manga109 全量精度评测vocab.txt 直接映射,未使用 BertJapaneseTokenizer + fugashi/unidic_lite (体积 250MB 且需 MeCab),推理阶段无需编码日文输入,不影响 image-to-text 生成与解码ViTFeatureExtractor 已 deprecated,未来可迁移至 ViTImageProcessor 但行为一致npu:0,分布式未验证Hardware: NPU, NPU, Ascend, Ascend910, vision, ocr, manga-ocr, vision-encoder-decoder, vit, bert-japanese