[2026春季][T2-1-1] goog00#499
Open
goog00 wants to merge 1 commit into
Open
Conversation
Add end-to-end support for the ERNIE-4.5-VL-28B-A3B multimodal MoE model across text, image and video inputs, validated token-for-token against HuggingFace transformers (reference only; inference does not depend on it). - Processor: text/image/video preprocessing byte-identical to HF (decord frame sampling, timestamp burn-in, smart_resize, patchify, 3D mRoPE with temporal merge). - Model: 28-layer backbone with text/vision modality MoE, DFNRope vision tower + resampler/merger adapter, GPT-J interleaved RoPE, softmax gating. - Multimodal wiring for image/video message inputs. - MetaX C500 (TP=2) runtime workarounds: stage pixel_values to the real compute device, chunk the large H2D copy, per-layer syncStream to keep the command queue from deadlocking on large prefills. - Correctness test harness for all three modalities. Verified end-to-end on NVIDIA (4x RTX 4090D, TP=4) and MetaX C500 (TP=2). Signed-off-by: sunteng <steng2009@163.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
T2-1-1 赛题报告:ERNIE-4.5-VL-28B-A3B 模型适配
一、实现的功能概述
将百度 ERNIE-4.5-VL-28B-A3B-Thinking(多模态 MoE 大模型)适配到 InfiniCore / InfiniLM 推理框架,在框架内完整实现视觉编码器、MoE、多模态适配器与单机多卡分布式推理,支持三种输入模态:
正确性验证:以 HuggingFace transformers 官方模型作为测试参照,进行 processor 级逐位对齐与端到端 greedy 逐 token 对照;适配代码本身的推理不依赖任何第三方框架。
模型规模:28 层文本骨干 + 文本/视觉双模态 MoE(各 64 专家)+ DFNRope 视觉塔(32 层)+ 重采样/merger 适配器 + 权重共享 lm_head;权重约 59GB(bf16)。
平台:NVIDIA(4×RTX 4090D,TP=4)三模态全部正确;MetaX C500(2×C500,TP=2)文本、图像正确。
二、使用的技术方案和参考资料
2.1 预处理(processor,平台无关,与 HF 逐位一致)
复现于
python/infinilm/processors/ernie4_5_moe_vl_processor.py,与 HFprocessing_ernie4_5_vl.py逐位对齐:图像:CLIP 归一化、
grid_thw计算、patch 切分顺序与 HF 一致。视频:decord 解码;
fps=2leading 抽帧,帧数夹到[16,180],奇数补帧凑偶(配 resampler 2:1 时序合并);时间戳烧帧(每帧左上角time: HH:MM:SS.ss,黑字白描边,Roboto 字体,在原分辨率、resize 之前);smart_resize(视频像素预算 234416/937664)、patchify、归一化。3D 位置编码:视频时序合并
t_eff = t // temporal_conv_size,占位符数= t_eff·gh·gw。验证:自建脚本对同一输入分别过 HF 与本实现,
grid_thw、占位符数、逐帧像素、pixel_values均max|Δ|=0。2.2 模型结构(C++,
csrc/models/ernie4_5_moe_vl/)RoPE:ERNIE 使用 GPT-J / interleaved 旋转(非框架默认 GPT-NeoX half-split);文本走 1D,视频/图像走 3D mRoPE(
rope_scaling.mrope_section,如 [22,22,20])。模态 MoE:softmax 门控(非 sigmoid);按 token 模态(文本/视觉)分派到对应专家组;combine 权重取所选专家的原始 softmax 概率、不归一化,correction_bias 仅影响选择。
视觉塔(DFNRope ViT):32 层、2D RoPE、块对角注意力(按帧分段 cu_seqlens)、quick_gelu;
patch_embed→ blocks → 最终 LayerNorm 与 HF 逐位一致。重采样/merger 适配器:图像(t=1)也须经
temporal_linear;after_norm为 RMSNorm(eps=1e-5)。权重映射(
python/infinilm/modeling_utils.py::_remap_ernie4_5_vl,注册到框架_WEIGHT_REMAPPER扩展点):gate.weight需转置且.contiguous();vision_model.*→visual.*、ln_1/ln_2→norm1/norm2;model.resampler_model.*→visual.merger.*。2.3 定位并修复的关键问题
4 处数值 bug:RoPE 算法(GPT-J)、MoE 门控(softmax)、gate 权重转置 +
.contiguous()、token_type_ids 在设备端构造不可靠(改 CPU 显式构造)。nn::Module::device_** 未初始化**(框架通用问题):父模块device_为默认构造的非法设备,张量搬运会崩;在模型构造时显式保存计算设备compute_device_。MetaX 运行时适配:大块 pixel_values H2D 分块拷贝;MoE 大 prefill 命令队列在解码层内
syncStream排空(MetaX 特有的功能性同步,无硬件平台特判——所有平台无条件执行)。补齐 MetaX 算子:为 InfiniCore 增加 MetaX(MACA) 的
softmax、quickgelu实现并注册。2.4 验证方法
两级对照:processor 级逐位 diff(预处理)+ HF greedy 逐 token 对照(端到端),以 forward hook 逐层 dump 精确定位每处偏差。HF 仅作参照,推理不依赖之。
2.5 参考资料
HuggingFace 官方模型与源码:
baidu/ERNIE-4.5-VL-28B-A3B-Thinking(modeling_ernie4_5_vl.py/processing_ernie4_5_vl.py)。InfiniCore / InfiniLM 框架现有模块(RoPE 工厂、MoE/MLP、注意力、线性层、分布式 CCL 等)。
三、测试脚本在各平台的运行方式及运行效果
测试脚本:
test/models/ernie4_5_moe_vl/test_correctness.py(覆盖三模态,与 HF greedy 逐 token 对照,--with-reference开启参照)。3.1 NVIDIA(4×RTX 4090D 24GB,TP=4)
运行效果(合成测试素材:橙色竖条 + 蓝色横条的图/视频):

终端运行效果截图
文本
图片
视频
3.2 MetaX C500(2×C500 64GB,TP=2;MACA 3.3.0.15,内核驱动 3.8.30)
与 NVIDIA 相同的测试脚本,
--device cuda --tp 2(59GB 权重单卡放不下,须双卡 TP=2):运行效果:
终端运行效果截图
文本
图片
视频
4.1 MetaX C500 视频模态
视频
pixel_values(约 21.5MB)在 host→device 传输时,触发 MetaX 驱动的registered user pointer显存异常([MXKW][E] analysis_memory_exception)并最终挂起。已做隔离实验确认根因不在代码/依赖:将 InfiniLM 与 InfiniCore 均回退至此前视频可正常运行的版本——InfiniLM
c645e8d+ InfiniCorefdc3c24f(2026-07-03 该组合在早期 MetaX 环境下视频可正常运行)——在当前 MetaX 环境(MACA 3.3.0.15 / 内核驱动 3.8.30 / 共享主机分配的 2×C500)上重建运行,仍复现同一异常并挂起。作为对照,当前提交版本为 InfiniLMe9d2118+ InfiniCore29f5afa2,在当前环境同样复现。即"同一份早期可运行的代码,换到当前环境即失败",变量仅剩环境层(容器/驱动/所分配的物理卡)。代码层已尝试分块拷贝、pinned host 中转、逐块小 bounce 缓冲等多种传输方式,故障区大小恒为完整缓冲(21.5MB),与传输方式无关,无法在代码层规避。据此定位为 MetaX 环境对大块 host 内存注册的限制,而非模型适配代码或依赖升级导致(早期 MetaX 环境下同一份代码视频可正常运行)。
五、赛题要求之外的额外功能说明
多平台适配:在必选的 NVIDIA 之外,完成国产 MetaX C500 平台的文本/图像模态适配(含为 InfiniCore 补齐 MetaX
softmax、quickgelu算子),并打通 TP=2 多卡。两级 HF 对照验证工具链:processor 级逐位 diff + 端到端 greedy 逐 token 对照 + 逐层 hook dump,可精确定位任一数值偏差。
附:代码提交信息
InfiniLM:分支
2026-spring-goog00-T2-1-1,单提交feat(ernie-vl): adapt ERNIE-4.5-VL-28B-A3B to InfiniLM。InfiniCore:分支
2026-spring-goog00-T2-1-1,单提交feat(ernie-vl): MetaX softmax/quickgelu ops + nvidia build fixes。均已清理调试代码、还原框架公共文件、复用框架模块化设计、不使用 legacy llama 代码。
Honor Code.pdf