|
| 1 | +# Copyright (c) 2026 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +InternLM2 Common Configuration |
| 17 | +
|
| 18 | +This module provides a unified configuration for both InternLM2 2.0 and 2.5 models. |
| 19 | +It detects the version based on the configuration fields and routes accordingly. |
| 20 | +""" |
| 21 | + |
| 22 | +from paddleformers.transformers.configuration_utils import PretrainedConfig |
| 23 | + |
| 24 | + |
| 25 | +class InternLM2Config(PretrainedConfig): |
| 26 | + """ |
| 27 | + InternLM2 configuration. This is a unified config that handles both 2.0 and 2.5 versions. |
| 28 | +
|
| 29 | + When loading from HuggingFace, the `model_type` will be "internlm2" (not "internlm2_5"). |
| 30 | + This config detects the actual version and routes to the appropriate implementation. |
| 31 | + """ |
| 32 | + |
| 33 | + model_type = "internlm2" # Important: must match HuggingFace config |
| 34 | + _auto_class = "AutoConfig" |
| 35 | + keys_to_ignore_at_inference = ["past_key_values"] |
| 36 | + |
| 37 | + def __init__( |
| 38 | + self, |
| 39 | + vocab_size=92550, |
| 40 | + hidden_size=4096, |
| 41 | + intermediate_size=11008, |
| 42 | + num_hidden_layers=32, |
| 43 | + num_attention_heads=32, |
| 44 | + num_key_value_heads=None, |
| 45 | + hidden_act="silu", |
| 46 | + max_position_embeddings=2048, |
| 47 | + initializer_range=0.02, |
| 48 | + rms_norm_eps=1e-6, |
| 49 | + use_cache=True, |
| 50 | + pad_token_id=0, |
| 51 | + bos_token_id=1, |
| 52 | + eos_token_id=2, |
| 53 | + pretraining_tp=1, |
| 54 | + tie_word_embeddings=False, |
| 55 | + bias=True, |
| 56 | + rope_theta=10000, |
| 57 | + rope_scaling=None, |
| 58 | + attn_implementation=None, |
| 59 | + dtype="bfloat16", |
| 60 | + **kwargs, |
| 61 | + ): |
| 62 | + self.vocab_size = vocab_size |
| 63 | + self.max_position_embeddings = max_position_embeddings |
| 64 | + self.hidden_size = hidden_size |
| 65 | + self.intermediate_size = intermediate_size |
| 66 | + self.num_hidden_layers = num_hidden_layers |
| 67 | + self.num_attention_heads = num_attention_heads |
| 68 | + self.bias = bias |
| 69 | + |
| 70 | + import paddle |
| 71 | + |
| 72 | + if isinstance(dtype, str): |
| 73 | + dtype_map = { |
| 74 | + "float32": paddle.float32, |
| 75 | + "float16": paddle.float16, |
| 76 | + "bfloat16": paddle.bfloat16, |
| 77 | + } |
| 78 | + self.dtype = dtype_map.get(dtype.lower(), paddle.float32) |
| 79 | + else: |
| 80 | + self.dtype = dtype |
| 81 | + |
| 82 | + if num_key_value_heads is None: |
| 83 | + num_key_value_heads = num_attention_heads |
| 84 | + self.num_key_value_heads = num_key_value_heads |
| 85 | + |
| 86 | + self.hidden_act = hidden_act |
| 87 | + self.initializer_range = initializer_range |
| 88 | + self.rms_norm_eps = rms_norm_eps |
| 89 | + self.pretraining_tp = pretraining_tp |
| 90 | + self.use_cache = use_cache |
| 91 | + self.rope_theta = rope_theta |
| 92 | + self.rope_scaling = rope_scaling |
| 93 | + self._rope_scaling_validation() |
| 94 | + self.attn_implementation = attn_implementation |
| 95 | + if self.attn_implementation is None: |
| 96 | + self.attn_implementation = "eager" |
| 97 | + |
| 98 | + super().__init__( |
| 99 | + pad_token_id=pad_token_id, |
| 100 | + bos_token_id=bos_token_id, |
| 101 | + eos_token_id=eos_token_id, |
| 102 | + tie_word_embeddings=tie_word_embeddings, |
| 103 | + **kwargs, |
| 104 | + ) |
| 105 | + |
| 106 | + def _rope_scaling_validation(self): |
| 107 | + if self.rope_scaling is None: |
| 108 | + return |
| 109 | + |
| 110 | + if not isinstance(self.rope_scaling, dict): |
| 111 | + raise ValueError(f"`rope_scaling` must be a dictionary, got {self.rope_scaling}") |
| 112 | + rope_scaling_type = self.rope_scaling.get("type", None) |
| 113 | + rope_scaling_factor = self.rope_scaling.get("factor", None) |
| 114 | + if rope_scaling_type is None or rope_scaling_factor is None: |
| 115 | + raise ValueError("`rope_scaling` must contain 'type' and 'factor' keys, " f"got {self.rope_scaling}") |
| 116 | + if rope_scaling_type not in ["linear", "dynamic"]: |
| 117 | + raise ValueError(f"`rope_scaling` type must be 'linear' or 'dynamic', got '{rope_scaling_type}'") |
| 118 | + if not isinstance(rope_scaling_factor, (int, float)) or rope_scaling_factor < 1.0: |
| 119 | + raise ValueError(f"`rope_scaling` factor must be a number >= 1, got {rope_scaling_factor}") |
| 120 | + |
| 121 | + @property |
| 122 | + def is_version_2_5(self): |
| 123 | + if hasattr(self, "auto_map") and self.auto_map is not None: |
| 124 | + if "AutoModelForSequenceClassification" in self.auto_map: |
| 125 | + return True |
| 126 | + return False |
0 commit comments