[2026春季][T2-1-1]xuzheng567#497
Open
xuzheng567 wants to merge 4 commits into
Open
Conversation
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模型适配)
队名:你说得都
成员:xz
github:
https://github.com/xuzheng567/InfiniCore
https://github.com/xuzheng567/InfiniLM
开发说明
依赖
本赛题的代码依赖于 InfiniTensor/InfiniCore#1388 中开发的fused moe算子
更改
https://github.com/xuzheng567/InfiniLM/tree/2026-spring-xuzheng567-T2-1-1
模型注册
新增 C++ 模型目录:
csrc/models/ernie4_5_vl在
ernie4_5_for_causal_lm.cpp中通过INFINILM_REGISTER_CAUSAL_LM_MODEL注册模型类型ernie4_5_moe_vl。将 ERNIE 配置中的字段统一转换为 InfiniLM 使用的配置格式,并补充默认值,包括:
MoE 配置
数据类型(dtype)
Attention Head Dimension
Dropout 默认值
RoPE 算法配置
语言模型
ernie4_5_model.cpp实现了:Token Embedding
Decoder Stack
最终 RMSNorm
可选的视觉特征插入(Visual Embedding Insertion)
ernie4_5_decoder_layer.cpp根据 ERNIE 的 MoE Layer Interval / Start / End 配置,在每一层自动选择:
Dense MLP
或 ERNIE MoE
ernie4_5_attention.cpp实现了 Attention 的 Q/K/V/O 计算
同时支持:
Static Attention
Paged Attention
ERNIE MRoPE
在现有 GPT-J RoPE 的基础上新增 ERNIE 专用的 Grouped MRoPE。
在
Ernie45Model中一次性构建 H/W/T(三个维度)的 sin/cos 查找表,并通过Ernie45MropeCache在所有 Attention Layer 之间共享。支持形状为
[batch, seq, 3]的三维position_ids,并能够将:Height
Width
Text/Time
三个维度的旋转分别应用到对应的 RoPE 维度组。
MoE
ernie4_5_moe.cpp实现了 ERNIE 的 Router 和 Expert。Router 支持分别使用文本和视觉两套 Gate 权重:
weightweight_1使用:
infinicore::op::moe_topk_softmaxinfinicore::op::fused_moe实现 MoE 推理。
支持独立的文本和视觉 Expert:
文本 Expert:
w1/w2视觉 Expert:
w1_1/w2_1根据
visual_token_ranges判断 Token 类型:文本 Token 路由到文本 Expert
视觉 Token 路由到视觉 Expert
Shared Expert 的输出会在对应模态 Expert 输出之后再进行累加。
视觉模型
ernie4_5_vision.cpp实现了:Patch Embedding
Vision Transformer Block
Vision LayerNorm
带二维 RoPE 的 QKV Attention
Quick GELU MLP
Resampler 模型(支持 Spatial 和 Temporal Packing)
语言模型流程中:
先运行 Vision Tower 提取视觉特征;
再通过 Resampler 将视觉特征映射到语言模型 Hidden Size;
将视觉特征插入到
image_bound指定的位置;同时记录视觉 Token 区间(
visual spans),供 MoE 路由使用。Processor
新增 Processor:
python/infinilm/processors/ernie4_5_vl_processor.py并注册到模型类型
ernie4_5_moe_vl。主要功能包括:
使用 Hugging Face
AutoProcessor和 Tokenizer(启用trust_remote_code=True)支持图片和视频输入
处理 Chat Template 的标准化
计算 Image Bounds
Pixel Normalization
构建
grid_thw管理多模态 Request ID
在 Prefill 阶段构建 ERNIE 三维 MRoPE 的
position_ids在 Decode 阶段利用
mrope_position_delta构建调整后的一维 Position IDs权重加载
python/infinilm/modeling_utils.py新增_remap_ernie4_5_moe_vl。主要完成以下工作:
将 Router Gate 权重转换为目标数据类型,避免 BF16/F32 混合导致的 Router MatMul 类型不一致问题。
将 Hugging Face Expert 中的:
gate_projup_proj融合为目标格式中的
w1。将
down_proj映射为w2。将 Hugging Face 的 Expert 按文本和视觉两类拆分,并分别映射到:
文本 Expert:
w1/w2视觉 Expert:
w1_1/w2_1将该权重重映射器注册到模型类型
"ernie4_5_moe_vl"。结果
推理测试
使用项目自带的test_infer.py进行测试
纯文字
使用默认query:How are you
指令
输出
图片+文字
指令
python test_infer.py --device=nvidia --model=baidu_ERNIE-4.5-VL-28B-A3B-Thinking --enable-paged-attn --attn=flash-attn --num-blocks 8 --prompt="图片里有什么" --image=pikachu.jpg输出
视频+文字
bear.mp4
指令
输出
HONOR_CODE.md
REFERENCE.md