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