[犀牛鸟 2026 #6787] HunyuanOCR ncnn 移植:C++ 端到端,Linux/Windows 双平台部署 #6847
LiamFan16-mikasa
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.
【腾讯犀牛鸟2026】HunyuanOCR-ncnn:HunyuanOCR v1.0 的 ncnn C++ 移植与双平台验证
项目仓库:LiamFan16-mikasa/HunyuanOCR-ncnn
一、任务要求与完成情况
二、核心亮点
optlevel=0稳定崩溃、inplace 层直连输入运行时崩溃\p{}正则)三、结果展示
四、实现流程
KV cache、逐 token logits)。复跑两次逐位相同(确定性),故此后任何 ncnn 侧不一致
责任 100% 在 ncnn 侧。参考必须 fp32——bf16 参考会在长程 greedy 出现精度域边界翻转的假分叉。
(x, cos, sin, mask),额外输出 K/V 供 runtime 填 cache。rope 的 cos/sin 作为图输入喂入,不在 ncnn 里算 XDRoPE。
dqkv(出 q/k/v,k 已 rope+QK-Norm)+dattn(吃完整定长窗口cache + mask 出 hidden),cache 写入放在两图之间的 runtime。
RMSNorm、preprocess;零依赖 byte-level BPE tokenizer。
五、实现难点
base = rope_theta · alpha^(d/(d-2)),非 config 表面值)。mrope_section=[16,16,16,16]但inv_freq在 j≥32已近零,width/image_index 两轴近似恒等;C++ 仍完整实现四轴 gather 保证逐位一致。
1 + h×(w+1) + 1:274 行 = 首尾各 1 行文本式位置 + 16×17 网格(17 = 16 列 + 1 换行列)。
optlevel=0稳定崩溃(跳过 pass_level3/4/5 却仍进 pass_ncnn,两平台稳定复现,最小复现仅需 Conv2d+reshape);
optlevel=1另有独立崩溃;只有默认optlevel=2可用。RotaryEmbed→ 改用 ±1 置换阵 matmul;repeat_interleave被静默丢弃(无报错、结果错、FLOPS=0 信号)→ 改expand+reshape;-infmask 加载不了 → 改有限值外部输入。splitncnn_0);inplace 层(RMSNorm 等)直连网络输入会运行时崩溃,与形状无关 → 为其插 Split 隔离层。
ncnn::Mat(data)只借引用不拷贝 → 输入缓冲区生命周期须覆盖整个 extract。六、构建与复现
仓库不含模型权重,需自行从官方渠道下载 HunyuanOCR v1.0;ncnn 以 submodule 形式拉取源码构建(CPU-only,关 vulkan/tools)。转换(PyTorch → ncnn)、C++ 构建(Linux gcc / Windows MinGW-w64)、运行、逐级对齐验证的完整命令见仓库 README。
七、当前边界
八、参考链接
HunyuanOCR ·
HunyuanOCR Hugging Face model ·
ncnn ·
pnnx ·
HunyuanOCR-ncnn(本项目仓库)
All reactions