[2026腾讯犀牛鸟]Youtu-VL到ncnn的适配与验证 #6848
AiChiTuDouPian
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.
Uh oh!
There was an error while loading. Please reload this page.
Youtu-VL → ncnn 端到端推理技术报告
1. 项目背景与目标
Youtu-VL-4B的推理链路整体搬到 ncnn + C++2. 系统架构
2.1 模型结构(Youtu-VL-4B,MLA 架构)
语言模型(LLM)部分
vocab_size=283386,num_hidden_layers=40,hidden_size=2560q_lora_rank=1536,kv_lora_rank=512NHEAD=32,QK_NOPE=128,QK_ROPE=64,VHEAD=128,QK_HEAD=192rope_theta=500000,rope_interleave布局BOS=128000,EOS=128001,VISION_START=128262,VISION_END=128263,IMAGE_PAD=128264lm_head与embed_tokens共享权重视觉塔(Vision Tower)部分
hidden_size=1152,num_hidden_layers=27num_attention_heads=16,head_dim=72(=1152/16)intermediate_size=4304,layer_norm_eps=1e-6hidden_act=gelu_pytorch_tanh(tanh 近似 GELU,ncnn 侧用gelu_tanh复现)window_size=256(patch 数),vit_merger_window_size=8(merge 空间窗口)2.2 Decoder 拆分方案(已验证,本阶段未改动)
每层拆成两个 ncnn 子图,注意力在 C++ 侧手算:
x[1,1,2560],单输入、无 RoPE):输出 5 个张量 ——qn(q_nope) /qr_raw(q_rot 未旋转) /kn(k_nope) /kr_raw(k_rot 未旋转) /v。RoPE 旋转移出图,C++ 侧build_R_at逐位置旋转。attn_flat[1,1,4096],无 add):只做注意力输出投影o_proj(attn)[1,1,2560]。h[1,1,2560],无 add):图内只做RMSNorm → MLP(SiLU) → down,输出 MLP 贡献。两处残差相加(o_proj(attn)+x、MLP贡献+h)均放在 C++ 完成。2.3 C++ 引擎
embed.bin(283386×2560, ~2.9GB, 与 lm_head 共享)、norm.bin(2560, final RMSNorm)、merger 系列权重。run_layer,图固定S=1,KV cache 累积保证因果)→ final_norm →logits = normed_hidden @ embed^T(权重共享)→ 贪心解码。run_vision(动态)→run_merger(动态)→ 用 visual token 替换 prompt 中对应数量的IMAGE_PAD槽位 → 走与文本相同的 40 层解码。[A,B,C]→ ncnn(w=C, h=B, c=A);fp16 bin 用use_fp16_storage=false加载为 fp32。3. 数值验证结果
3.1 固定尺寸(16×16)
→ 与纯文本路径(logits cosine 0.999035、12/12)
3.2 动态尺寸(figure1.png,12×18 grid)
输入
figure1.png(1636×1124)经 processor 真实缩放为 12×18 = 216 patch → 54 visual token,gw=18 → llm_grid_w=9 > 8触发 2 个 window(固定尺寸版本根本测不出的场景)。C++(ncnn)与 PyTorch 逐项对比:4. 结论
4.1 结论
C++ ncnn 引擎已实现 Youtu-VL-4B 的动态尺寸端到端推理,在任意输入图片下与 PyTorch 原版最终文本一致:
动态尺寸链路(figure1,12×18 grid,216 patch → 54 visual token,2 窗口):vision cosine 0.99976、visual token cosine 0.99954、末位 logits cosine 0.99784、贪心 24/24 完全一致。
项目地址:https://github.com/AiChiTuDouPian/Youtu-VL-ncnn
模型地址:https://huggingface.co/xxzigou/youtu-vl-ncnn
All reactions