[犀牛鸟] Tencent-Hunyuan/HunyuanOCR ncnn 移植与多平台部署 #6800
infinity955
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Tencent-Hunyuan/HunyuanOCR ncnn 移植与多平台部署
背景
项目最初目标是参考 ncnn_llm 的 GLM-OCR 架构,将腾讯 HunYuanOCR(1B 多模态 OCR 模型)移植到 ncnn,用纯 C++ 实现跨平台推理。
过程
开始按照 GLM-OCR 的模式拆模块、写 C++ 推理代码。工作中发现 futz12 已经在 ncnn discussion #6793 发布了完整的 HunYuanOCR 适配教程。按教程重新导出后模型顺利跑通。
补充发现
在复现过程中发现两个教程未覆盖的问题:
1.
int(gh)/int(gw)导致 pnnx 双尺寸崩溃 —VisionPatchMerger.forward中int()将 traced tensor 转为常量,双尺寸时冲突。修复:去掉int()。2. ncnn Mat COW 导致 use-after-free —
memcpy(row(), ...)绕过 COW 写入共享数据,Extractor 析构后 crash。修复:修改前.clone()。完整流程
以下是从零复现的完整步骤。详细说明见仓库 README_CN.md。
阶段零:获取仓库
git clone https://github.com/infinity955/ncnn_ocr.git cd ncnn_ocr阶段一:环境搭建
Python 环境:
pip install torch==2.12.1 numpy pillow pip install "git+https://github.com/huggingface/transformers@82a06db03535c49aa987719ed0746a76093b1ec4" pip install pnnx==2026052682a06db)编译 ncnn:
下载 HunYuanOCR 原始模型:
阶段二:导出 ncnn 模型
阶段三:编译 C++
阶段四:运行
架构
结果
C++ ncnn 端到端跑通,文字输出与 PyTorch HF 一致,任意图片可用。
坐标差 2-6 px(
size=vsscale_factor+0.1已知取舍)。致谢
Beta Was this translation helpful? Give feedback.
All reactions