|
| 1 | +# Copyright 2025 The HuggingFace Team. 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 | +from typing import Optional |
| 15 | + |
| 16 | +from ...configuration_utils import PretrainedConfig |
| 17 | + |
| 18 | + |
| 19 | +class Lfm2MoeConfig(PretrainedConfig): |
| 20 | + r""" |
| 21 | + This is the configuration class to store the configuration of a [`Lfm2MoeModel`]. It is used to instantiate a LFM2 Moe |
| 22 | + model according to the specified arguments, defining the model architecture. Instantiating a configuration with the |
| 23 | + defaults will yield a similar configuration to that of the LFM2-8B-A1B model. |
| 24 | + e.g. [LiquidAI/LFM2-8B-A1B](https://huggingface.co/LiquidAI/LFM2-8B-A1B) |
| 25 | +
|
| 26 | + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
| 27 | + documentation from [`PretrainedConfig`] for more information. |
| 28 | +
|
| 29 | +
|
| 30 | + Args: |
| 31 | + vocab_size (`int`, *optional*, defaults to 65536): |
| 32 | + Vocabulary size of the LLaMA model. Defines the number of different tokens that can be represented by the |
| 33 | + `inputs_ids` passed when calling [`Lfm2Model`] |
| 34 | + hidden_size (`int`, *optional*, defaults to 2048): |
| 35 | + Dimension of the hidden representations. |
| 36 | + intermediate_size (`int`, *optional*, defaults to 7168): |
| 37 | + Dimension of the MLP representations. |
| 38 | + moe_intermediate_size (`int`, *optional*, defaults to 1792): |
| 39 | + Intermediate size of the routed expert. |
| 40 | + num_hidden_layers (`int`, *optional*, defaults to 32): |
| 41 | + Number of hidden layers in the Transformer decoder. |
| 42 | + pad_token_id (`int`, *optional*, defaults to 0): |
| 43 | + Padding token id. |
| 44 | + bos_token_id (`int`, *optional*, defaults to 1): |
| 45 | + Beginning of stream token id. |
| 46 | + eos_token_id (`int`, *optional*, defaults to 2): |
| 47 | + End of stream token id. |
| 48 | + tie_word_embeddings (`bool`, *optional*, defaults to `True`): |
| 49 | + Whether to tie weight embeddings |
| 50 | + rope_theta (`float`, *optional*, defaults to 1000000.0): |
| 51 | + The base period of the RoPE embeddings. |
| 52 | + max_position_embeddings (`int`, *optional*, defaults to 128000): |
| 53 | + The maximum sequence length that this model might ever be used with. |
| 54 | + use_cache (`bool`, *optional*, defaults to `True`): |
| 55 | + Whether or not the model should return the last key/values attentions (not used by all models). Only |
| 56 | + relevant if `config.is_decoder=True`. |
| 57 | + norm_eps (`float`, *optional*, defaults to 1e-05): |
| 58 | + The epsilon used by the rms normalization layers. |
| 59 | + num_attention_heads (`int`, *optional*, defaults to 32): |
| 60 | + Number of attention heads for each attention layer in the Transformer decoder. |
| 61 | + num_key_value_heads (`int`, *optional*, defaults to 8): |
| 62 | + This is the number of key_value heads that should be used to implement Grouped Query Attention. If |
| 63 | + `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if |
| 64 | + `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When |
| 65 | + converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed |
| 66 | + by meanpooling all the original heads within that group. For more details, check out [this |
| 67 | + paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to |
| 68 | + `num_attention_heads`. |
| 69 | + conv_bias (`bool`, *optional*, defaults to `False`): |
| 70 | + Whether to use bias in the conv layers. |
| 71 | + conv_L_cache (`int`, *optional*, defaults to 3): |
| 72 | + L_cache dim in the conv layers. |
| 73 | + num_dense_layers (`int`, *optional*, defaults to 2): |
| 74 | + Number of dense Lfm2MoeMLP layers in shallow layers(embed->dense->dense->...->dense->moe->moe...->lm_head). |
| 75 | + num_experts_per_tok (`int`, *optional*, defaults to 4): |
| 76 | + Number of selected experts. |
| 77 | + num_experts (`int`, *optional*, defaults to 32): |
| 78 | + Number of routed experts. |
| 79 | + use_expert_bias (`bool`, *optional*, defaults to `True`): |
| 80 | + Whether to use the expert bias on the routing weights. |
| 81 | + routed_scaling_factor (`float`, *optional*, defaults to 1.0): |
| 82 | + Scaling factor for routed experts in MoE models. |
| 83 | + norm_topk_prob (`bool`, *optional*, defaults to `True`): |
| 84 | + Whether to normalize the topk probabilities. |
| 85 | + layer_types (`Optional`, *optional*): |
| 86 | + Type of each layers. |
| 87 | +
|
| 88 | + ```python |
| 89 | + >>> from transformers import Lfm2MoeModel, Lfm2MoeConfig |
| 90 | +
|
| 91 | + >>> # Initializing a LFM2 Moe model |
| 92 | + >>> configuration = Lfm2MoeConfig() |
| 93 | +
|
| 94 | + >>> # Initializing a model from the LFM2-8B-A1B style configuration |
| 95 | + >>> model = Lfm2MoeModel(configuration) |
| 96 | +
|
| 97 | + >>> # Accessing the model configuration |
| 98 | + >>> configuration = model.config |
| 99 | + ```""" |
| 100 | + |
| 101 | + model_type = "lfm2_moe" |
| 102 | + keys_to_ignore_at_inference = ["past_key_values"] |
| 103 | + |
| 104 | + def __init__( |
| 105 | + self, |
| 106 | + vocab_size: int = 65536, |
| 107 | + hidden_size: int = 2048, |
| 108 | + intermediate_size: int = 7168, |
| 109 | + moe_intermediate_size: int = 1792, |
| 110 | + num_hidden_layers: int = 32, |
| 111 | + pad_token_id: int = 0, |
| 112 | + bos_token_id: int = 1, |
| 113 | + eos_token_id: int = 2, |
| 114 | + tie_word_embeddings: bool = True, |
| 115 | + rope_theta: float = 1000000.0, |
| 116 | + max_position_embeddings: int = 128_000, |
| 117 | + use_cache: bool = True, |
| 118 | + norm_eps: float = 0.00001, |
| 119 | + num_attention_heads: int = 32, |
| 120 | + num_key_value_heads: int = 8, |
| 121 | + conv_bias: bool = False, |
| 122 | + conv_L_cache: int = 3, |
| 123 | + num_dense_layers: int = 2, |
| 124 | + num_experts_per_tok: int = 4, |
| 125 | + num_experts: int = 32, |
| 126 | + use_expert_bias: bool = True, |
| 127 | + routed_scaling_factor: float = 1.0, |
| 128 | + norm_topk_prob: bool = True, |
| 129 | + layer_types: Optional[list[str]] = None, |
| 130 | + **kwargs, |
| 131 | + ): |
| 132 | + self.vocab_size = vocab_size |
| 133 | + self.hidden_size = hidden_size |
| 134 | + self.intermediate_size = intermediate_size |
| 135 | + self.num_hidden_layers = num_hidden_layers |
| 136 | + self.rope_theta = rope_theta |
| 137 | + self.max_position_embeddings = max_position_embeddings |
| 138 | + self.use_cache = use_cache |
| 139 | + self.norm_eps = norm_eps |
| 140 | + |
| 141 | + # attn operator config |
| 142 | + self.num_attention_heads = num_attention_heads |
| 143 | + self.num_key_value_heads = num_key_value_heads |
| 144 | + |
| 145 | + # custom operator config |
| 146 | + self.conv_bias = conv_bias |
| 147 | + self.conv_L_cache = conv_L_cache |
| 148 | + |
| 149 | + # moe config |
| 150 | + self.num_dense_layers = num_dense_layers |
| 151 | + self.moe_intermediate_size = moe_intermediate_size |
| 152 | + self.num_experts_per_tok = num_experts_per_tok |
| 153 | + self.num_experts = num_experts |
| 154 | + self.use_expert_bias = use_expert_bias |
| 155 | + self.routed_scaling_factor = routed_scaling_factor |
| 156 | + self.norm_topk_prob = norm_topk_prob |
| 157 | + self.layer_types = layer_types |
| 158 | + |
| 159 | + tie_word_embeddings = kwargs.get("tie_embedding", tie_word_embeddings) # to fit original config keys |
| 160 | + super().__init__( |
| 161 | + pad_token_id=pad_token_id, |
| 162 | + bos_token_id=bos_token_id, |
| 163 | + eos_token_id=eos_token_id, |
| 164 | + tie_word_embeddings=tie_word_embeddings, |
| 165 | + **kwargs, |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +__all__ = ["Lfm2MoeConfig"] |
0 commit comments