Skip to content

增加internlm3的支持#4167

Open
learncat163 wants to merge 8 commits into
PaddlePaddle:developfrom
learncat163:feature/pr-add-rm3
Open

增加internlm3的支持#4167
learncat163 wants to merge 8 commits into
PaddlePaddle:developfrom
learncat163:feature/pr-add-rm3

Conversation

@learncat163

Copy link
Copy Markdown
Contributor

PR类型

新增模型支持,增加internlm3支持

特别注意

internlm3的版本有点特殊,其依赖的transformers特性在4.53.0-5.x之间。transformers不能太老或者太新,transformers有些特性出现了一下后续又被删除了。而且即使强行修改lm3原版代码,通过patch 的方式绕过一些依赖,原版也无法实现精确推理。所以,建议使用确定的transformers 的4.53.0版本测试

相关模型权重下载

推理和token对齐

tests/transformers/intern_lm3/test_modeling.py 中的 test_torch_paddle_model_alignment 中前20个logits值对齐 < 1e-2,top 10 token一致。

tests/transformers/intern_lm3/test_modeling.py 中的test_paddle_model_load_and_infer 方法有@slow标记,可以实现8b规模的情况下,直接加载hg上的原始权重,并推理出和transformers版本上一样的output。

训练对齐

使用ms-swift【一样需要切换到transformers 的4.53.0版本】,指令如下:

swift sft \
    --model "$MODEL_PATH" \
    --model_type internlm3 \
    --tuner_type full \
    --template internlm3 \
    --custom_register_path "$REGISTER_SCRIPT" \
    --dataset "$TRAIN_DATA" \
    --max_length 512 \
    --per_device_train_batch_size 1 \
    --learning_rate 1e-5 \
    --max_steps 500 \
    --warmup_steps 5 \
    --gradient_accumulation_steps 4 \
    --num_train_epochs 1 \
    --bf16 true \
    --seed 23 \
    --output_dir "$OUTPUT_DIR" \
    --logging_steps 1 \
    --save_strategy no \
    --gradient_checkpointing true \
    --freeze_parameters_ratio 1.0 \
    --trainable_parameters model.layers.0 model.layers.1 model.layers.2 \
    --ddp_find_unused_parameters true \
    2>&1 | tee "$OUTPUT_DIR/training.log"

ms-swift的注册模板代码如下:

from swift.model import ModelMeta, ModelGroup, Model, register_model
from swift.template import TemplateMeta, register_template

# 注册 internlm3 模板 (基于 internlm2 的 ChatML 模板)
register_template(
    TemplateMeta(
        template_type='internlm3',
        prefix=[],
        prompt=['<|im_start|>user\n{{QUERY}}<|im_end|>\n<|im_start|>assistant\n'],
        chat_sep=['<|im_end|>\n'],
        suffix=['<|im_end|>\n'],
        system_prefix=['<|im_start|>system\n{{SYSTEM}}<|im_end|>\n'],
        default_system='You are an AI assistant whose name is InternLM (书生·浦语).\n- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.\n- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.',
        auto_add_bos=True,
    )
)

# 注册 internlm3 模型
register_model(
    ModelMeta(
        model_type='internlm3-8b',
        model_groups=[
            ModelGroup([
                Model('internlm/internlm3-8b'),
                Model('Shanghai_AI_Laboratory/internlm3-8b'),
            ])
        ],
        architectures=['InternLM3ForCausalLM'],
        template='internlm3',
    )
)

paddleformers cli的配置在tests/config/ci/interlm3_sft.yaml

结果如下:

step ms-swift paddle
1 2.243019 12.481102
2 2.140157 12.910933
3 1.084858 12.613197
4 2.069410 11.815698
5 1.902594 10.363028
6 1.802376 8.557021
7 1.698214 7.979319
8 1.637394 6.697113
9 1.460361 7.710663
10 0.875674 5.979547
11 1.585849 3.219007
12 1.498954 4.750943
13 1.298560 4.020440
14 1.560261 4.577221
15 1.089507 2.964619
16 1.026917 2.722546
17 1.240063 4.095179
18 1.468783 2.982727
19 1.221604 0.839921
20 1.278656 1.234530
21 0.903565 0.927739
22 0.969437 0.713693
23 1.279214 0.368439
24 0.659534 0.307858
25 1.134080 0.212019
26 1.019280 0.074972
27 0.692112 0.040519
28 0.693346 0.059971
29 0.969113 0.018377
30 1.135387 0.002534
31 0.631088 0.001987
32 0.883193 0.002824
33 0.784517 0.105294
34 0.414218 0.001445
35 0.910909 0.056540
36 0.291419 0.000331
37 0.708792 0.047140
38 0.343475 0.000264
39 0.755488 0.113719
40 0.183462 0.041646
41 0.503327 0.000847
42 0.762694 0.035772
43 0.427115 0.000186
44 0.478457 0.000318
45 0.557624 0.000595
46 0.329178 0.000144
47 0.362048 0.111909
48 0.442757 0.035732
49 0.308152 0.002381
50 0.176344 0.026593
51 0.393076 0.000065
52 0.298820 0.000109
53 0.191250 0.034424
54 0.160566 0.000058
55 0.239285 0.000733
56 0.044221 0.000107
57 0.118099 0.000076
58 0.182260 0.044494
59 0.037016 0.012905
60 0.080779 0.000079
61 0.058605 0.000074
62 0.070486 0.000086
63 0.051596 0.057774
64 0.022288 0.107316
65 0.023129 0.000051
66 0.086075 0.000087
67 0.042252 0.000044
68 0.011872 0.003289
69 0.004668 0.005306
70 0.006726 0.000056
71 0.005324 0.000090
72 0.034473 0.000028
73 0.015915 0.000082
74 0.004006 0.017707
75 0.000476 0.000076
76 0.008106 0.002010
77 0.002856 0.000032
78 0.002138 0.017140
79 0.004742 0.000034
80 0.002378 0.000037
81 0.000836 0.018095
82 0.002084 0.000030
83 0.004035 0.001163
84 0.001164 0.000044
85 0.003352 0.000034
86 0.001269 0.000032
87 0.000969 0.030906
88 0.003181 0.000050
89 0.001312 0.000024
90 0.000545 0.000033
91 0.001009 0.046814
92 0.002320 0.000042
93 0.001087 0.000032
94 0.001516 0.000035
95 0.000909 0.000030
96 0.000874 0.010881
97 0.001644 0.005075
98 0.000462 0.000341
99 0.000832 0.000023
100 0.000867 0.000031
... ... ...
total 126

@paddle-bot

paddle-bot Bot commented Mar 27, 2026

Copy link
Copy Markdown

Thanks for your contribution!

@codecov-commenter

codecov-commenter commented Mar 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.73753% with 181 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@90f5048). Learn more about missing BASE report.

Files with missing lines Patch % Lines
paddleformers/transformers/intern_lm3/tokenizer.py 26.54% 83 Missing ⚠️
paddleformers/transformers/intern_lm3/modeling.py 81.43% 49 Missing ⚠️
...eformers/transformers/intern/bert_padding_delte.py 0.00% 46 Missing ⚠️
...leformers/transformers/intern_lm3/configuration.py 93.75% 2 Missing ⚠️
paddleformers/datasets/template/template.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #4167   +/-   ##
==========================================
  Coverage           ?   34.15%           
==========================================
  Files              ?      467           
  Lines              ?    87868           
  Branches           ?        0           
==========================================
  Hits               ?    30008           
  Misses             ?    57860           
  Partials           ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

("gemma3_text", "Gemma3TextConfig"),
("glm4v_moe", "Glm4vMoeConfig"),
("glm_ocr", "GlmOcrConfig"),
("internlm3", "InternLM3Config"),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要在auto/modeling.py也添加InternLM3,否则通过 AutoModelForCausalLM.from_pretrained() 加载会失败,在 MAPPING_NAMES 中添加 ("InternLM3", "intern_lm3")

@@ -0,0 +1,110 @@
# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026

@@ -0,0 +1,44 @@
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026,其余涉及的地方同理

@@ -0,0 +1,347 @@
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
# Copyright 2020 The HuggingFace Team. All rights reserved.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是否有引用 hf 代码,如果没有的话,建议删掉这行

@a31413510

Copy link
Copy Markdown
Collaborator

1、Copyright 年份错误
2、需要去掉中文注释
3、bert_padding_delte.py是否有被引用,如果不需要,不应提交此文件;如果将来需要,应在intern/init.py 中导出。
4、InternLM3ForCausalLM 中添加了两个仅返回 True 的 @Property(chat、stream_chat),没有实际功能,属于无用代码,建议删除。

@liuhao2638 liuhao2638 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本轮已完成复查,发现 1 个需要修正后再合入的训练模板问题,另有 1 个配置默认值一致性问题。具体细节已放在行级评论里。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

register_template(
name="internlm3",
format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 优先级:P1

这里把 <|im_end|>\n 放进了 format_assistant,同时下面的 chat_sepsuffix 也都是同一个结束符。Template._encode() 会在非最后一轮 assistant 后追加 chat_sepSFTDataset 还会在末尾再追加 suffix,因此使用 template: internlm3 做 SFT 时会训练出重复的 <|im_end|> 分隔符,和上游 chat template 不一致。建议和已有 qwen/qwen3 模板一样,让 assistant formatter 只包含响应文本,结束符统一由 chat_sep/suffix 处理。

Suggested change
format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
format_assistant=StringFormatter(slots=["{{content}}"]),

Comment on lines +140 to +155
intermediate_size=11008,
num_hidden_layers=32,
num_attention_heads=32,
num_key_value_heads=32,
hidden_act="silu",
max_position_embeddings=32768,
initializer_range=0.02,
rms_norm_eps=1e-6,
use_cache=True,
tie_word_embeddings=False,
rope_theta=10000.0,
rope_scaling=None,
qkv_bias=False,
attention_dropout=0.0,
bias=False,
head_dim=None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 优先级:P2

InternLM3Config() 的默认值目前不是 InternLM3-8B 的结构:公开的 internlm/internlm3-8b-instruct 配置是 intermediate_size=10240num_hidden_layers=48num_key_value_heads=2rms_norm_eps=1e-5rope_theta=50000000、动态 RoPE factor 6、head_dim=128。这里的默认值仍接近 Llama/Qwen 形状,但类说明又写着默认会得到 InternLM3-8B;用户直接实例化默认 config 或缺省 config 转换时会得到不兼容的模型结构。请把默认值和文档默认值同步到实际 InternLM3-8B 配置。

Suggested change
intermediate_size=11008,
num_hidden_layers=32,
num_attention_heads=32,
num_key_value_heads=32,
hidden_act="silu",
max_position_embeddings=32768,
initializer_range=0.02,
rms_norm_eps=1e-6,
use_cache=True,
tie_word_embeddings=False,
rope_theta=10000.0,
rope_scaling=None,
qkv_bias=False,
attention_dropout=0.0,
bias=False,
head_dim=None,
intermediate_size=10240,
num_hidden_layers=48,
num_attention_heads=32,
num_key_value_heads=2,
hidden_act="silu",
max_position_embeddings=32768,
initializer_range=0.02,
rms_norm_eps=1e-5,
use_cache=True,
tie_word_embeddings=False,
rope_theta=50000000.0,
rope_scaling={"factor": 6.0, "rope_type": "dynamic"},
qkv_bias=False,
attention_dropout=0.0,
bias=False,
head_dim=128,

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已复查最新提交,上一轮关于 internlm3 SFT 模板结束符重复和默认配置不一致的问题已经修复;CI 当前通过,未发现新的阻塞问题。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

@a31413510

Copy link
Copy Markdown
Collaborator

需要解决ci报错的代码规范问题

@Paddle-CI-Bot

Copy link
Copy Markdown

PaddleFormers Log Analysis

Run #28773443513 · Attempt 1

日志分析报告

流水线名称 问题标签 修复建议 日志片段
Unittest GPU CI 图片下载网络中断 fetch_image 中加 retry 或将测试图片预下载到本地 cache,避免在 setUp 中实时拉取外部 URL 报错代码

失败的测试case:

tests/transformers/qwen3_vl_moe/test_modeling.py::Qwen3VLMoeIntegrationTest::test_model_tiny_logits_batch_wo_image
tests/transformers/qwen3_vl_moe/test_modeling.py::Qwen3VLMoeIntegrationTest::test_model_tiny_logits_with_video

根本原因分析:

两个 case 均在 setUp 中调用 process_vision_infofetch_image,从 https://paddlenlp.bj.bcebos.com/datasets/paddlemix/demo_images/example1.jpg 实时下载图片(552828 bytes),BOS 连接在传输中途断开(IncompleteRead(0 bytes read, 552828 more expected)),导致 requests.exceptions.ChunkedEncodingError,与 PR 改动的数据集 template 代码无直接关联,属于网络抖动引发的 setUp 失败。

修复建议:

  1. 短期:直接 rerun CI,网络恢复后大概率通过。
  2. 根本修复:在 paddleformers/transformers/qwen2_vl/vision_process.pyfetch_image 函数中对 requests.get 加 retry 逻辑(如 requests.adapters.HTTPAdapter(max_retries=3)),防止单次网络抖动导致整批 case 失败。
  3. 或将测试用图片预下载至 /home/models/ 本地路径,setUp 改为读取本地文件,彻底避免 CI 中外网 I/O 的不稳定性。

🔍 准确性记录:请点击评论底部 😊 图标,选择 👍(准确)或 👎(有误),将自动记录到 CI 监控系统

🔄 每次 Re-run 后自动更新

@a31413510

Copy link
Copy Markdown
Collaborator

仍然存在代码风格问题,建议本地在commit前运行pre-commit,修复代码风格问题再提交

Resolve conflicts in template.py, transformers/__init__.py and
auto/configuration.py by keeping both internlm3 and gemma4_moe/phi4 entries.

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已复查最新提交,上一轮 Nyanpasu 提出的 P1 模板问题仍保持已修复状态,默认配置问题也已同步。新提交里只发现一个非阻塞的 .gitignore 仓库级忽略项清理建议,细节见行级评论;当前 CI 仍在运行中,建议等检查完成后再合入。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

Comment thread .gitignore Outdated
Comment on lines +172 to +188
checkpoints
.claude
/test/
/tmp/
.roo/
.flake8
AGENTS.md
ms-swift-4.0.2
paconvert-lm3
phi4-code-convert
phi4-code-raw
raw-internlm2
raw-internlm2_5
raw-internlm3
log/
.agents/
.infcode No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 优先级:P3

处理要求:请针对该评论进行回复(同意并已修改请回复 Done,不同意请说明理由)。

这组新增 ignore 项看起来是本地开发/转换工具目录(如 .claude.rooAGENTS.mdraw-internlm3 等),而不是 PaddleFormers 项目通用产物。提交到仓库级 .gitignore 后,所有贡献者都会默认忽略同名根目录/文件,例如后续如果需要提交 AGENTS.md/test//tmp/ 下的项目文件会被 Git 隐藏。建议仓库级规则只保留项目通用产物;个人环境和临时模型目录放到本机 .git/info/exclude 或全局 gitignore。

Suggested change
checkpoints
.claude
/test/
/tmp/
.roo/
.flake8
AGENTS.md
ms-swift-4.0.2
paconvert-lm3
phi4-code-convert
phi4-code-raw
raw-internlm2
raw-internlm2_5
raw-internlm3
log/
.agents/
.infcode
checkpoints

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已看到最新提交把这些仓库级 ignore 项移除了,代码侧已经满足这条建议。按前面 P3 的处理要求,还请在本线程回复 Done,我就可以把这条视为已完成。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确认,代码侧和回复都已满足这条 P3 建议,我这边按已完成处理。

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已复查最新提交,上一轮 .gitignore 的代码侧修改已经到位;我已在原线程说明还需要作者回复 Done 作为 P3 处理完成信号。当前 CI 仍在运行中,本轮未新增行级问题。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已复查,前面提出的 .gitignore 清理建议已通过最新提交处理,并且作者已在对应线程回复确认。此前关于 internlm3 模板和默认配置的问题也保持已修复状态;本轮未发现新的阻塞问题。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants