Add InternLM model and tokenizer support#4495
Conversation
|
Thanks for your contribution! |
PaddleFormers Log Analysis
日志分析报告
失败的测试case: 根本原因分析: PR 仅新增 InternLM 模型实现( 修复建议:
🔄 每次 Re-run 后自动更新 |
Codecov Report❌ Patch coverage is ❌ Your patch status has failed because the patch coverage (64.74%) is below the target coverage (75.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## develop #4495 +/- ##
==========================================
Coverage ? 46.53%
==========================================
Files ? 480
Lines ? 91124
Branches ? 0
==========================================
Hits ? 42409
Misses ? 48715
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
/re-run all-failed |
1 similar comment
|
/re-run all-failed |
|
/re-run all-failed |
liuhao2638
left a comment
There was a problem hiding this comment.
已完成这轮代码审查,发现几处需要合入前修复的问题,具体细节已放在行级评论里。
优先级:P3 非行级:PR 标题/描述不是 diff 中的具体代码行。当前标题
internlm test只表达测试,但实际改动是新增 InternLM config/model/tokenizer/Auto 映射支持,容易让维护者误判变更范围。建议将标题改为Add InternLM model and tokenizer support,并在描述中补充支持范围、主要测试项、已知风险或 CI 依赖状态。
| "internlm.configuration": ["InternLMConfig"], | ||
| "internlm.modeling": [ | ||
| "InternLMModel", | ||
| "InternLMForCausalLM", | ||
| "InternLMForCausalLMPipe", | ||
| ], | ||
| "internlm.tokenizer": ["InternLMTokenizer"], |
There was a problem hiding this comment.
这里新增 InternLM 映射时把原来的 ernie4_5.tokenizer 条目删掉了。paddleformers.transformers.ernie4_5.__init__ 仍导出 Ernie4_5Tokenizer,删除顶层 import_structure 后,from paddleformers.transformers import Ernie4_5Tokenizer 以及 AutoTokenizer 的本地类查找都会找不到该已有公共类,属于兼容性回归。请保留 ERNIE 4.5 tokenizer 条目,再追加 InternLM 条目。
| "internlm.configuration": ["InternLMConfig"], | |
| "internlm.modeling": [ | |
| "InternLMModel", | |
| "InternLMForCausalLM", | |
| "InternLMForCausalLMPipe", | |
| ], | |
| "internlm.tokenizer": ["InternLMTokenizer"], | |
| "ernie4_5.tokenizer": ["Ernie4_5Tokenizer"], | |
| "internlm.configuration": ["InternLMConfig"], | |
| "internlm.modeling": [ | |
| "InternLMModel", | |
| "InternLMForCausalLM", | |
| "InternLMForCausalLMPipe", | |
| ], | |
| "internlm.tokenizer": ["InternLMTokenizer"], |
| if token_ids_1 is None: | ||
| return [1] + ([0] * len(token_ids_0)) + [1] | ||
| return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] |
There was a problem hiding this comment.
build_inputs_with_special_tokens() 会根据 add_bos_token/add_eos_token 决定实际插入哪些特殊 token,但这里固定返回 [BOS] + tokens + [EOS] 的 mask。默认 add_eos_token=False 时,单句输入实际长度是 len(token_ids_0)+1,这里却返回 len(token_ids_0)+2;pair 输入也同样多标了不存在的 EOS/BOS,return_special_tokens_mask=True 或数据 collator 使用该 mask 时会和 input_ids 长度不一致。请让 mask 按同一组开关构造。
| if token_ids_1 is None: | |
| return [1] + ([0] * len(token_ids_0)) + [1] | |
| return [1] + ([0] * len(token_ids_0)) + [1, 1] + ([0] * len(token_ids_1)) + [1] | |
| mask = ([1] if self.add_bos_token else []) + ([0] * len(token_ids_0)) | |
| if token_ids_1 is not None: | |
| mask += [0] * len(token_ids_1) | |
| if self.add_eos_token: | |
| mask += [1] | |
| return mask |
| if past_key_values is not None: | ||
| key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx) | ||
|
|
||
| cos, sin = position_embeddings | ||
| query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) |
There was a problem hiding this comment.
这里先把未加 RoPE 的 key_states 写入 DynamicCache.update(),再对 update() 返回的整段 key 统一按当前 position_ids 做 RoPE,会破坏缓存生成。DynamicLayer.update() 会把本次 key 拼到缓存并返回完整 self.keys,下一步 decode 时 key_states.shape[-2] 已经是 past+1,而 position_ids 只包含当前 token;apply_rotary_pos_emb() 的 fallback 会从 cos[:, :k_len] 取位置 0..past,导致历史 key 被重新用错误位置旋转,且缓存中保存的仍是未旋转 key。请按 Llama/Qwen 的顺序先只对本步 q/k 应用 RoPE,再把已经旋转后的 k 写入 cache。
| if past_key_values is not None: | |
| key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx) | |
| cos, sin = position_embeddings | |
| query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) | |
| cos, sin = position_embeddings | |
| query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) | |
| if past_key_values is not None: | |
| key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx) |
risemeup1111
left a comment
There was a problem hiding this comment.
已复查新提交,当前 diff 中可见的代码问题已修复,未发现新的阻塞性问题。本地环境缺少 paddle,无法运行新增 InternLM 单测;目前 CI 还有任务运行中,建议等待结果完成后再合入。
优先级:P3 非行级:PR 标题/描述不是 diff 中的具体代码行。当前标题仍是
internlm test,与实际新增 InternLM config/model/tokenizer/Auto 映射支持不够匹配。建议改为Add InternLM model and tokenizer support,并在描述中补充支持范围、主要测试项、已知风险或 CI 状态。
risemeup1111
left a comment
There was a problem hiding this comment.
已复查当前状态:PR 标题已更新为与变更内容匹配的描述,之前可见的代码问题已在后续提交中修复,当前检查均已通过。未发现需要继续阻塞合入的问题。
Before submitting
testsfolder. If there are codecov issues, please add tests cases first.PR types
PR changes
Description
Add InternLM model and tokenizer support
模型前向精度对齐,前10个token的logits diff为0.08
模型推理生成正常
模型sft loss为
[2026-05-20 01:25:59,358] [ INFO] - loss: 2.46186185, learning_rate: 5e-06, global_step: 1, current_memory_allocated: 13.973276853561401, current_memory_reserved: 26.491707801818848, max_memory_allocated: 26.48780107498169, max_memory_reserved: 26.491707801818848, interval_runtime: 12.092, interval_samples_per_second: 0.3308, interval_steps_per_second: 0.0827, ppl: 11.726624443222343, progress_or_epoch: 0.3333, cpu_used_memory: 16.1, cpu_available_memory: 30.93
[2026-05-20 01:25:59,358] [ INFO] - [DataLoad global_step: 1] data_load_time: 44.99 ms (accumulated over 4 micro-batches)
[2026-05-20 01:25:59,360] [ INFO] - Not using packing mode for data iteration.
[2026-05-20 01:26:00,303] [ INFO] - loss: 2.7151227, learning_rate: 1e-05, global_step: 2, current_memory_allocated: 13.973276853561401, current_memory_reserved: 26.491707801818848, max_memory_allocated: 26.48780107498169, max_memory_reserved: 26.491707801818848, interval_runtime: 0.9452, interval_samples_per_second: 4.2321, interval_steps_per_second: 1.058, ppl: 15.106463521258013, progress_or_epoch: 0.6667, cpu_used_memory: 16.1, cpu_available_memory: 30.92
[2026-05-20 01:26:00,303] [ INFO] - [DataLoad global_step: 2] data_load_time: 0.66 ms (accumulated over 4 micro-batches)
[2026-05-20 01:26:01,250] [ INFO] - loss: 2.56071281, learning_rate: 1.5e-05, global_step: 3, current_memory_allocated: 13.973276853561401, current_memory_reserved: 26.491707801818848, max_memory_allocated: 26.48780107498169, max_memory_reserved: 26.491707801818848, interval_runtime: 0.9468, interval_samples_per_second: 4.2245, interval_steps_per_second: 1.0561, ppl: 12.945041382596827, progress_or_epoch: 1.0, cpu_used_memory: 16.11, cpu_available_memory: 30.91