Repository navigation
支持stablelm #4482
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
base: develop
Are you sure you want to change the base?
支持stablelm #4482
Changes from all commits
dbfc69d
f15c055
77270fb
10d75ff
7a61a62
2c5f01f
7aced95
6081ea7
67e3860
2dccff9
2fdc278
634b704
46a4e84
e646dc0
6bef121
876dce3
7ebba46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,7 @@ | |
| ("molmo", "MolmoConfig"), | ||
| ("qwen3_5", "Qwen3_5Config"), | ||
| ("qwen3_5_moe", "Qwen3_5MoEConfig"), | ||
| ("stablelm", "StableLmConfig"), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 下面的MODEL_NAMES_MAPPING也需要注册对应内容 同时也需要在docs/zh/model_capability.md和仓库README补一下该模型相关的内容 |
||
| ("olmo2", "Olmo2Config"), | ||
| ("internlm3", "InternLM3Config"), | ||
| ("internlm2", "InternLM2Config"), | ||
|
|
@@ -139,6 +140,7 @@ | |
| ("qwen3_5_moe", "Qwen3_5MoEForConditionalGeneration"), | ||
| ("qwen3_5", "Qwen3_5ForConditionalGeneration"), | ||
| ("minicpm3", "MiniCPM3Model"), | ||
| ("stablelm", "StableLmForCausalLM"), | ||
| ("olmo2", "Olmo2ForCausalLM"), | ||
| ("internlm3", "InternLM3ForCausalLM"), | ||
| ("internlm2", "InternLM2"), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # 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. | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ...utils.lazy_import import _LazyModule | ||
|
|
||
| import_structure = { | ||
| "configuration": ["StableLmConfig"], | ||
| "modeling": [ | ||
| "StableLmModel", | ||
| "StableLmForCausalLM", | ||
| "StableLmForCausalLMPipe", | ||
| "StableLmPretrainedModel", | ||
| "StableLmRotaryEmbedding", | ||
| "StableLmDecoderLayer", | ||
| "StableLmAttention", | ||
| ], | ||
| } | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .configuration import StableLmConfig | ||
| from .modeling import ( | ||
| StableLmAttention, | ||
| StableLmDecoderLayer, | ||
| StableLmForCausalLM, | ||
| StableLmForCausalLMPipe, | ||
| StableLmModel, | ||
| StableLmPretrainedModel, | ||
| StableLmRotaryEmbedding, | ||
| ) | ||
|
|
||
| else: | ||
| import sys | ||
|
|
||
| sys.modules[__name__] = _LazyModule( | ||
| __name__, | ||
| globals()["__file__"], | ||
| import_structure, | ||
| module_spec=__spec__, | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # 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. | ||
|
|
||
| import math | ||
|
|
||
| from ..configuration_utils import PretrainedConfig | ||
| from ..modeling_rope_utils import rope_config_validation, standardize_rope_params | ||
|
|
||
|
|
||
| class StableLmConfig(PretrainedConfig): | ||
| model_type = "stablelm" | ||
| keys_to_ignore_at_inference = ["past_key_values"] | ||
|
|
||
| def __init__( | ||
| self, | ||
| vocab_size=50304, | ||
| intermediate_size=6912, | ||
| hidden_size=2560, | ||
| num_hidden_layers=32, | ||
| num_attention_heads=32, | ||
| num_key_value_heads=32, | ||
| hidden_act="silu", | ||
| max_position_embeddings=4096, | ||
| initializer_range=0.02, | ||
| layer_norm_eps=1.0e-5, | ||
| use_cache=True, | ||
| tie_word_embeddings=False, | ||
| rope_theta=10000.0, | ||
| partial_rotary_factor=0.25, | ||
| rope_parameters=None, | ||
| use_qkv_bias=False, | ||
| qk_layernorm=False, | ||
| use_parallel_residual=False, | ||
| hidden_dropout=0.0, | ||
| attention_dropout=0.0, | ||
| bos_token_id=0, | ||
| eos_token_id=0, | ||
| pad_token_id=None, | ||
| **kwargs, | ||
| ): | ||
| for name, value in { | ||
| "vocab_size": vocab_size, | ||
| "hidden_size": hidden_size, | ||
| "intermediate_size": intermediate_size, | ||
| "num_hidden_layers": num_hidden_layers, | ||
| "num_attention_heads": num_attention_heads, | ||
| "num_key_value_heads": num_key_value_heads, | ||
| "max_position_embeddings": max_position_embeddings, | ||
| }.items(): | ||
| if value <= 0: | ||
| raise ValueError(f"{name} must be positive, got {value!r}") | ||
| if hidden_size % num_attention_heads: | ||
| raise ValueError("hidden_size must be divisible by num_attention_heads") | ||
| if num_attention_heads % num_key_value_heads: | ||
| raise ValueError("num_attention_heads must be divisible by num_key_value_heads") | ||
| for name, value in {"hidden_dropout": hidden_dropout, "attention_dropout": attention_dropout}.items(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 可维护性建议(AI 生成债务,不阻塞合入):整型参数的校验已按评审意见去掉 |
||
| if not isinstance(value, (int, float)) or not math.isfinite(value) or not 0 <= value < 1: | ||
| raise ValueError(f"{name} must be in [0, 1), got {value!r}") | ||
| if not isinstance(partial_rotary_factor, (int, float)) or not 0 < partial_rotary_factor <= 1: | ||
| raise ValueError("partial_rotary_factor must be in (0, 1]") | ||
| rotary_dim = int(hidden_size // num_attention_heads * partial_rotary_factor) | ||
| if rotary_dim <= 0 or rotary_dim % 2: | ||
| raise ValueError("partial RoPE dimension must be positive and even") | ||
| for name, value in { | ||
| "layer_norm_eps": layer_norm_eps, | ||
| "initializer_range": initializer_range, | ||
| "rope_theta": rope_theta, | ||
| }.items(): | ||
| if not isinstance(value, (int, float)) or not math.isfinite(value) or value <= 0: | ||
| raise ValueError(f"{name} must be positive and finite") | ||
| self.vocab_size = vocab_size | ||
| self.hidden_size = hidden_size | ||
| self.intermediate_size = intermediate_size | ||
| self.max_position_embeddings = max_position_embeddings | ||
| self.num_hidden_layers = num_hidden_layers | ||
| self.num_attention_heads = num_attention_heads | ||
| self.num_key_value_heads = num_key_value_heads | ||
| self.hidden_act = hidden_act | ||
| self.initializer_range = initializer_range | ||
| self.layer_norm_eps = layer_norm_eps | ||
| self.use_cache = use_cache | ||
| self.tie_word_embeddings = tie_word_embeddings | ||
| self.use_qkv_bias = use_qkv_bias | ||
| self.qk_layernorm = qk_layernorm | ||
| self.use_parallel_residual = use_parallel_residual | ||
| self.hidden_dropout = hidden_dropout | ||
| self.attention_dropout = attention_dropout | ||
|
|
||
| self.rope_theta = rope_theta | ||
| self.partial_rotary_factor = partial_rotary_factor | ||
| rope_scaling = kwargs.pop("rope_scaling", None) | ||
| if rope_parameters is None: | ||
| rope_parameters = rope_scaling | ||
| if rope_parameters is not None and not isinstance(rope_parameters, dict): | ||
| raise ValueError("rope_parameters and rope_scaling must be dicts or None") | ||
| self.rope_parameters = dict(rope_parameters) if rope_parameters is not None else None | ||
| standardize_rope_params(self, rope_theta=self.rope_theta) | ||
| effective_rope_theta = self.rope_parameters["rope_theta"] | ||
| if ( | ||
| not isinstance(effective_rope_theta, (int, float)) | ||
| or not math.isfinite(effective_rope_theta) | ||
| or effective_rope_theta <= 0 | ||
| ): | ||
| raise ValueError("rope_parameters.rope_theta must be positive and finite") | ||
| rope_config_validation(self, ignore_keys={"partial_rotary_factor"}) | ||
|
|
||
| self.norm_eps = self.layer_norm_eps | ||
| self.mlp_bias = False | ||
|
|
||
| if kwargs.get("architectures") is None: | ||
| kwargs["architectures"] = ["StableLmForCausalLM"] | ||
| 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, | ||
| ) | ||
| if getattr(self, "head_dim", None) not in (None, hidden_size // num_attention_heads): | ||
| raise ValueError("StableLM head_dim must equal hidden_size // num_attention_heads") | ||
| self.head_dim = hidden_size // num_attention_heads | ||
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.
上面可以再补一个"stablelm": [],风格对齐