Skip to content

[2026春季][T2-1-1]xuzheng567#497

Open
xuzheng567 wants to merge 4 commits into
InfiniTensor:mainfrom
xuzheng567:2026-spring-xuzheng567-T2-1-1
Open

[2026春季][T2-1-1]xuzheng567#497
xuzheng567 wants to merge 4 commits into
InfiniTensor:mainfrom
xuzheng567:2026-spring-xuzheng567-T2-1-1

Conversation

@xuzheng567

@xuzheng567 xuzheng567 commented Jul 12, 2026

Copy link
Copy Markdown

基本信息

赛题: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 权重:

    • weight

    • weight_1

  • 使用:

    • infinicore::op::moe_topk_softmax

    • infinicore::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_proj

    • up_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

输出

===Query===
<|begin_of_sentence|>You are a multimodal AI assistant called ERNIE developed by Baidu based on the PaddlePaddle framework.
User: How are you
Assistant: 
<think>

===Response===
Let me start by recalling the user's query: "How are you". As an AI assistant, I need to respond in a friendly and professional manner.

First, I should introduce myself clearly. Mentioning that I'm ERNIE, developed by Baidu, adds context. Then, I need to express my readiness to help. Keeping the tone warm and inviting encourages the user to ask further questions.

I should avoid any markdown or complex formatting, just plain text. Let me structure the response: start with a greeting, introduce myself, state my purpose, and end with an open question to engage the user.

Wait, the user might be looking for a quick and straightforward answer. I should ensure the response is concise yet informative. Also, using emojis could make it more friendly, but since the user didn't specify, maybe stick to a standard greeting.

Double-checking: ERNIE is correct, Baidu is accurate, and the response should be in English as per the user's query. No need to overcomplicate. Just a simple, polite answer.
</think>

Hello! I'm ERNIE, a large language model developed by Baidu. I'm here to help you with any questions or tasks you have. How can I assist you today?</s>

total_time: 3709.89 ms

图片+文字

pikachu

指令

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

输出

===Query===
<|begin_of_sentence|>You are a multimodal AI assistant called ERNIE developed by Baidu based on the PaddlePaddle framework.
User:  Picture 1:<|IMAGE_START|><|image@placeholder|><|IMAGE_END|>图片里有什么
Assistant: 
<think>

===Response===
用户现在需要识别图片内容。图片是《宝可梦》系列中的皮卡丘,从表情看是生气或严肃的样子。要确认元素:黄色电鼠型宝可梦,特征如红色脸颊、闪电尾巴、尖耳朵等。所以描述图片里的角色。
</think>

图片里是《宝可梦》系列中的角色皮卡丘,它呈现出愤怒或严肃的表情。</s>

total_time: 6255.92 ms

视频+文字

bear.mp4

指令

python test_infer.py --device=nvidia --model=baidu_ERNIE-4.5-VL-28B-A3B-Thinking/ --enable-paged-attn --attn=flash-attn --num-blocks 64 --prompt '视频里有什么' --video bear.mp4

输出

===Query===
<|begin_of_sentence|>You are a multimodal AI assistant called ERNIE developed by Baidu based on the PaddlePaddle framework.
User:  Video 1:<|VIDEO_START|><|video@placeholder|><|VIDEO_END|>视频里有什么
Assistant: 
<think>

===Response===
用户现在需要回答视频里有什么。首先看视频内容:一条河,河里有只棕熊,周围有很多白鸟(比如海鸥),还有石头,背景是绿色的草地。所以要列出这些元素。首先,主体是棕熊,然后是河流、白鸟、石头、绿色植被。需要准确描述。比如:视频里有一条河流,河中央站着一只棕熊,周围有许多白色的海鸥,还有一些石头,背景是绿色的草地。这样应该覆盖了主要元素。
</think>

视频里有一条河流,河中央站着一只棕熊,周围有许多白色的海鸥,还有一些石头,背景是绿色的草地。</s>

total_time: 2584.3 ms

HONOR_CODE.md
REFERENCE.md

@xuzheng567 xuzheng567 requested a review from a team July 12, 2026 11:43
@xuzheng567 xuzheng567 changed the title [2026春季][T2-1-1]xz [2026春季][T2-1-1]xuzheng567 Jul 12, 2026
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