CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
configuration_lfm2.py166 linesDownload Raw Back to lfm2
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 at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# limitations under the License.14from typing import Optional15 16from ...configuration_utils import PretrainedConfig17 18 19class Lfm2Config(PretrainedConfig):20    r"""21    This is the configuration class to store the configuration of a [`Lfm2Model`]. It is used to instantiate a LFM222    model according to the specified arguments, defining the model architecture. Instantiating a configuration with the23    defaults will yield a similar configuration to that of the LFM2-1.2B model.24    e.g. [LiquidAI/LFM2-1.2B](https://huggingface.co/LiquidAI/LFM2-1.2B)25 26    Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the27    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 the33            `inputs_ids` passed when calling [`Lfm2Model`]34        hidden_size (`int`, *optional*, defaults to 2560):35            Dimension of the hidden representations.36        intermediate_size (`int`, *optional*, defaults to 12288):37            Dimension of the MLP representations.38        num_hidden_layers (`int`, *optional*, defaults to 32):39            Number of hidden layers in the Transformer decoder.40        num_attention_heads (`int`, *optional*, defaults to 32):41            Number of attention heads for each attention layer in the Transformer decoder.42        num_key_value_heads (`int`, *optional*, defaults to 8):43            This is the number of key_value heads that should be used to implement Grouped Query Attention. If44            `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if45            `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When46            converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed47            by meanpooling all the original heads within that group. For more details, check out [this48            paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to49            `num_attention_heads`.50        max_position_embeddings (`int`, *optional*, defaults to 128000):51            The maximum sequence length that this model might ever be used with. Lfm2 1 supports up to 2048 tokens,52            Lfm2 2 up to 4096, CodeLfm2 up to 16384.53        initializer_range (`float`, *optional*, defaults to 0.02):54            The standard deviation of the truncated_normal_initializer for initializing all weight matrices.55        norm_eps (`float`, *optional*, defaults to 1e-05):56            The epsilon used by the rms normalization layers.57        use_cache (`bool`, *optional*, defaults to `True`):58            Whether or not the model should return the last key/values attentions (not used by all models). Only59            relevant if `config.is_decoder=True`.60        pad_token_id (`int`, *optional*, defaults to 0):61            Padding token id.62        bos_token_id (`int`, *optional*, defaults to 1):63            Beginning of stream token id.64        eos_token_id (`int`, *optional*, defaults to 2):65            End of stream token id.66        tie_word_embeddings (`bool`, *optional*, defaults to `True`):67            Whether to tie weight embeddings68        rope_theta (`float`, *optional*, defaults to 1000000.0):69            The base period of the RoPE embeddings.70        conv_bias (`bool`, *optional*, defaults to `False`):71            Whether to use bias in the conv layers.72        conv_L_cache (`int`, *optional*, defaults to 3):73            L_cache dim in the conv layers.74        block_multiple_of (`int`, *optional*, defaults to 256):75            Multiple for the `intermediate_size`.76        block_ffn_dim_multiplier (`float`, *optional*, defaults to 1.0):77            Multiplier for the `intermediate_size`.78        block_auto_adjust_ff_dim (`bool`, *optional*, defaults to `True`):79            Whether to adjust the dim of the `intermediate_size`.80        full_attn_idxs (`Optional`, *optional*):81            Index of the layers which use attention.82        layer_types (`Optional`, *optional*):83            Type of each layers.84 85    ```python86    >>> from transformers import Lfm2Model, Lfm2Config87 88    >>> # Initializing a LFM2 model89    >>> configuration = Lfm2Config()90 91    >>> # Initializing a model from the LFM2-1.2B style configuration92    >>> model = Lfm2Model(configuration)93 94    >>> # Accessing the model configuration95    >>> configuration = model.config96    ```"""97 98    model_type = "lfm2"99    keys_to_ignore_at_inference = ["past_key_values"]100 101    def __init__(102        self,103        vocab_size: int = 65536,104        hidden_size: int = 2560,105        intermediate_size: int = 12288,106        num_hidden_layers: int = 32,107        num_attention_heads: int = 32,108        num_key_value_heads: int = 8,109        max_position_embeddings: int = 128_000,110        initializer_range: float = 0.02,111        norm_eps: float = 0.00001,112        use_cache: bool = True,113        pad_token_id: int = 0,114        bos_token_id: int = 1,115        eos_token_id: int = 2,116        tie_word_embeddings: bool = True,117        rope_theta: float = 1000000.0,118        conv_bias: bool = False,119        conv_L_cache: int = 3,120        block_multiple_of: int = 256,121        block_ffn_dim_multiplier: float = 1.0,122        block_auto_adjust_ff_dim: bool = True,123        full_attn_idxs: Optional[list[int]] = None,124        layer_types: Optional[list[str]] = None,125        **kwargs,126    ):127        self.vocab_size = vocab_size128        self.hidden_size = hidden_size129        self.num_hidden_layers = num_hidden_layers130        self.rope_theta = kwargs.get("theta", rope_theta)  # to fit original config keys131        self.max_position_embeddings = max_position_embeddings132        self.use_cache = use_cache133        self.norm_eps = norm_eps134        self.initializer_range = initializer_range135 136        # attn operator config137        self.num_attention_heads = num_attention_heads138        self.num_key_value_heads = num_key_value_heads139 140        # custom operator config141        self.conv_bias = conv_bias142        self.conv_L_cache = conv_L_cache143 144        # MLP config145        self.intermediate_size = kwargs.get("block_ff_dim", intermediate_size)  # to fit original config keys146        self.block_multiple_of = block_multiple_of147        self.block_ffn_dim_multiplier = block_ffn_dim_multiplier148        self.block_auto_adjust_ff_dim = block_auto_adjust_ff_dim149 150        self.layer_types = layer_types151        if self.layer_types is None:152            full_attn_idxs = full_attn_idxs if full_attn_idxs is not None else list(range(num_hidden_layers))153            self.layer_types = ["full_attention" if i in full_attn_idxs else "conv" for i in range(num_hidden_layers)]154 155        tie_word_embeddings = kwargs.get("tie_embedding", tie_word_embeddings)  # to fit original config keys156        super().__init__(157            pad_token_id=pad_token_id,158            bos_token_id=bos_token_id,159            eos_token_id=eos_token_id,160            tie_word_embeddings=tie_word_embeddings,161            **kwargs,162        )163 164 165__all__ = ["Lfm2Config"]166 
Aluode/PerceptionLabPortable · CoolFace