-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add InternLM model and tokenizer support #4495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fjaijf
wants to merge
10
commits into
PaddlePaddle:develop
Choose a base branch
from
fjaijf:feat/internlm_test
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c69eea5
internlm test
fjaijf 8422401
bug fixed
fjaijf c97dd88
fix internlm bug
fjaijf 88d1605
fix a little bug
fjaijf 36a74e0
Merge remote-tracking branch 'origin/develop' into feat/internlm_test
fjaijf c5eb83f
trigger ci
fjaijf b0ff5d8
trigger ci
fjaijf 011e501
trigger ci
fjaijf 8467ff3
fix according to reveiw
fjaijf 543dd67
trigger ci
fjaijf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Package""" | ||
|
|
||
| import sys | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils.lazy_import import _LazyModule | ||
|
|
||
| import_structure = { | ||
| "tokenizer": ["InternLMTokenizer"], | ||
| "configuration": ["InternLMConfig"], | ||
| "modeling": [ | ||
| "InternLMDecoderLayer", | ||
| "InternLMModel", | ||
| "InternLMForCausalLM", | ||
| "InternLMForCausalLMPipe", | ||
| ], | ||
| } | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .configuration import * | ||
| from .modeling import * | ||
| else: | ||
| sys.modules[__name__] = _LazyModule( | ||
| __name__, | ||
| globals()["__file__"], | ||
| import_structure, | ||
| module_spec=__spec__, | ||
| ) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import paddle.distributed as dist | ||
|
|
||
|
|
||
| def get_dist_config(model, prefix=""): | ||
| """Generate distributed configuration for InternLM model""" | ||
| if prefix != "": | ||
| assert prefix.endswith(".") | ||
|
|
||
| config = { | ||
| "mp_config": { | ||
| "parallelize_plan": { | ||
| f"{prefix}model.embed_tokens": dist.ColWiseParallel(gather_output=True), | ||
| f"{prefix}model.layers.*.self_attn.q_proj": dist.ColWiseParallel(), | ||
| f"{prefix}model.layers.*.self_attn.k_proj": dist.ColWiseParallel(), | ||
| f"{prefix}model.layers.*.self_attn.v_proj": dist.ColWiseParallel(), | ||
| f"{prefix}model.layers.*.self_attn.o_proj": dist.RowWiseParallel(), | ||
| f"{prefix}model.layers.*.mlp.gate_proj": dist.ColWiseParallel(), | ||
| f"{prefix}model.layers.*.mlp.up_proj": dist.ColWiseParallel(), | ||
| f"{prefix}model.layers.*.mlp.down_proj": dist.RowWiseParallel(), | ||
| f"{prefix}lm_head.weight": dist.ColWiseParallel(), | ||
| } | ||
| }, | ||
| } | ||
| return config |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. | ||
| # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """InternLM model configuration.""" | ||
|
|
||
| from ..configuration_utils import PretrainedConfig | ||
|
|
||
|
|
||
| class InternLMConfig(PretrainedConfig): | ||
| model_type = "internlm" | ||
|
|
||
| def __init__( | ||
| self, | ||
| vocab_size=103168, | ||
| hidden_size=4096, | ||
| intermediate_size=11008, | ||
| num_hidden_layers=32, | ||
| num_attention_heads=32, | ||
| hidden_act="silu", | ||
| max_position_embeddings=2048, | ||
| initializer_range=0.02, | ||
| rms_norm_eps=1e-6, | ||
| use_cache=True, | ||
| recompute_use_reentrant=False, | ||
| pad_token_id=0, | ||
| bos_token_id=1, | ||
| eos_token_id=2, | ||
| tie_word_embeddings=False, | ||
| bias=True, | ||
| rotary=None, | ||
| attention_dropout=0.0, | ||
| ignored_index=-100, | ||
| micro_batch_size=-1, | ||
| pp_seg_method="layer:InternLMDecoderLayer|EmptyLayer", | ||
| dpo_config=None, | ||
| kto_config=None, | ||
| recompute_granularity=None, | ||
| recompute_method=None, | ||
| recompute_modules=None, | ||
| recompute_num_layers=None, | ||
| recompute_mtp_granularity=None, | ||
| recompute_mtp_method=None, | ||
| recompute_mtp_modules=None, | ||
| **kwargs, | ||
| ): | ||
| self.vocab_size = vocab_size | ||
| self.max_position_embeddings = max_position_embeddings | ||
| self.hidden_size = hidden_size | ||
| self.intermediate_size = intermediate_size | ||
| self.num_hidden_layers = num_hidden_layers | ||
| self.num_attention_heads = num_attention_heads | ||
| self.num_key_value_heads = num_attention_heads | ||
| self.hidden_act = hidden_act | ||
| self.initializer_range = initializer_range | ||
| self.rms_norm_eps = rms_norm_eps | ||
| self.use_cache = use_cache | ||
| self.bias = bias | ||
| self.attention_bias = bias | ||
| self.mlp_bias = False | ||
| self.attention_dropout = attention_dropout | ||
| self.head_dim = self.hidden_size // self.num_attention_heads | ||
|
|
||
| rotary = {"base": 10000, "type": "dynamic"} if rotary is None else rotary | ||
| self.rotary = rotary | ||
| self.rope_theta = rotary.get("base", 10000) | ||
| rotary_type = rotary.get("type", "dynamic") | ||
| if rotary_type == "origin": | ||
| self.rope_parameters = {"rope_theta": self.rope_theta, "rope_type": "default"} | ||
| elif rotary_type == "dynamic": | ||
| self.rope_parameters = { | ||
| "rope_theta": self.rope_theta, | ||
| "rope_type": "dynamic", | ||
| "factor": rotary.get("scaling_factor", 1.0), | ||
| } | ||
| else: | ||
| raise ValueError("InternLM only supports rotary type 'origin' or 'dynamic'.") | ||
|
|
||
| super().__init__( | ||
| pad_token_id=pad_token_id, | ||
| bos_token_id=bos_token_id, | ||
| eos_token_id=eos_token_id, | ||
| tie_word_embeddings=tie_word_embeddings, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| self.recompute_use_reentrant = recompute_use_reentrant | ||
| self.ignored_index = ignored_index | ||
| self.micro_batch_size = micro_batch_size | ||
| self.pp_seg_method = pp_seg_method | ||
| self.dpo_config = dpo_config | ||
| self.kto_config = kto_config | ||
| self.recompute_granularity = recompute_granularity | ||
| self.recompute_method = recompute_method | ||
| self.recompute_modules = recompute_modules | ||
| self.recompute_num_layers = recompute_num_layers | ||
| self.recompute_mtp_granularity = recompute_mtp_granularity | ||
| self.recompute_mtp_method = recompute_mtp_method | ||
| self.recompute_mtp_modules = recompute_mtp_modules | ||
| self.register_unsavable_keys( | ||
| [ | ||
| "ignored_index", | ||
| "recompute_use_reentrant", | ||
| "pp_seg_method", | ||
| "micro_batch_size", | ||
| "dpo_config", | ||
| "kto_config", | ||
| "recompute_granularity", | ||
| "recompute_method", | ||
| "recompute_modules", | ||
| "recompute_num_layers", | ||
| "recompute_mtp_granularity", | ||
| "recompute_mtp_method", | ||
| "recompute_mtp_modules", | ||
| ] | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里新增 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 条目。