Skip to content

[2026春季][T2-1-1] goog00#499

Open
goog00 wants to merge 1 commit into
InfiniTensor:mainfrom
goog00:2026-spring-goog00-T2-1-1
Open

[2026春季][T2-1-1] goog00#499
goog00 wants to merge 1 commit into
InfiniTensor:mainfrom
goog00:2026-spring-goog00-T2-1-1

Conversation

@goog00

@goog00 goog00 commented Jul 12, 2026

Copy link
Copy Markdown

T2-1-1 赛题报告:ERNIE-4.5-VL-28B-A3B 模型适配

 小组:justdoit 赛题:T2-1-1 模型适配


一、实现的功能概述

将百度 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,与 HF processing_ernie4_5_vl.py 逐位对齐:

  • 图像:CLIP 归一化、grid_thw 计算、patch 切分顺序与 HF 一致。

  • 视频:decord 解码;fps=2 leading 抽帧,帧数夹到 [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_valuesmax|Δ|=0

2.2 模型结构(C++,csrc/models/ernie4_5_moe_vl/

  • RoPE:ERNIE 使用 GPT-J / interleaved 旋转(非框架默认 GPT-NeoX half-split);文本走 1D,视频/图像走 3D mRoPErope_scaling.mrope_section,如 [22,22,20])。

  • 模态 MoEsoftmax 门控(非 sigmoid);按 token 模态(文本/视觉)分派到对应专家组;combine 权重取所选专家的原始 softmax 概率、不归一化,correction_bias 仅影响选择。

  • 视觉塔(DFNRope ViT):32 层、2D RoPE、块对角注意力(按帧分段 cu_seqlens)、quick_gelu;patch_embed → blocks → 最终 LayerNorm 与 HF 逐位一致。

  • 重采样/merger 适配器:图像(t=1)也须经 temporal_linearafter_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_2norm1/norm2model.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) 的 softmaxquickgelu 实现并注册。

2.4 验证方法

两级对照:processor 级逐位 diff(预处理)+ HF greedy 逐 token 对照(端到端),以 forward hook 逐层 dump 精确定位每处偏差。HF 仅作参照,推理不依赖之。

2.5 参考资料

  • HuggingFace 官方模型与源码:baidu/ERNIE-4.5-VL-28B-A3B-Thinkingmodeling_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)

export MODEL=/path/to/ERNIE-4.5-VL-28B-A3B-Thinking
# 文本
python test/models/ernie4_5_moe_vl/test_correctness.py \--model $MODEL --device cuda --tp 4 --cases text --max-new-tokens 32# 图像
python test/models/ernie4_5_moe_vl/test_correctness.py \--model $MODEL --device cuda --tp 4 --cases image \--image /tmp/test.jpg --max-cache-len 2048 --max-new-tokens 32# 视频
python test/models/ernie4_5_moe_vl/test_correctness.py \--model $MODEL --device cuda --tp 4 --cases video \--video /tmp/test.mp4 --max-cache-len 3072 --max-new-tokens 8

运行效果(合成测试素材:橙色竖条 + 蓝色横条的图/视频):
image

终端运行效果截图

文本
image
图片
image
视频
image

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):

export MODEL=/path/to/ERNIE-4.5-VL-28B-A3B-Thinking
# 文本
python test/models/ernie4_5_moe_vl/test_correctness.py \--model $MODEL --device cuda --tp 2 --cases text --max-new-tokens 32# 图像
python test/models/ernie4_5_moe_vl/test_correctness.py \--model $MODEL --device cuda --tp 2 --cases image \--image /tmp/test.jpg --max-cache-len 2048 --max-new-tokens 32# 视频
python test/models/ernie4_5_moe_vl/test_correctness.py \--model $MODEL --device cuda --tp 2 --cases video \--video /tmp/test.mp4 --max-cache-len 3072 --max-new-tokens 8

运行效果:

说明:文本、图像与 NVIDIA 结果一致;视频命令与 NVIDIA 完全相同,仅在当前 MetaX 环境的驱动层受限(非代码问题,见 4.1)。

终端运行效果截图

文本
image
图片
image
视频
image ## 四、未实现或未适配部分的说明

4.1 MetaX C500 视频模态

视频 pixel_values(约 21.5MB)在 host→device 传输时,触发 MetaX 驱动的 registered user pointer 显存异常([MXKW][E] analysis_memory_exception)并最终挂起。

已做隔离实验确认根因不在代码/依赖:将 InfiniLM 与 InfiniCore 均回退至此前视频可正常运行的版本——InfiniLM c645e8d + InfiniCore fdc3c24f(2026-07-03 该组合在早期 MetaX 环境下视频可正常运行)——在当前 MetaX 环境(MACA 3.3.0.15 / 内核驱动 3.8.30 / 共享主机分配的 2×C500)上重建运行,仍复现同一异常并挂起。作为对照,当前提交版本为 InfiniLM e9d2118 + InfiniCore 29f5afa2,在当前环境同样复现。即"同一份早期可运行的代码,换到当前环境即失败",变量仅剩环境层(容器/驱动/所分配的物理卡)。

代码层已尝试分块拷贝、pinned host 中转、逐块小 bounce 缓冲等多种传输方式,故障区大小恒为完整缓冲(21.5MB),与传输方式无关,无法在代码层规避。据此定位为 MetaX 环境对大块 host 内存注册的限制,而非模型适配代码或依赖升级导致(早期 MetaX 环境下同一份代码视频可正常运行)。

说明:NVIDIA 平台视频模态正确运行,不受此问题影响。


五、赛题要求之外的额外功能说明

  • 多平台适配:在必选的 NVIDIA 之外,完成国产 MetaX C500 平台的文本/图像模态适配(含为 InfiniCore 补齐 MetaX softmaxquickgelu 算子),并打通 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

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>
@goog00 goog00 requested a review from a team July 12, 2026 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant