Aluode/PerceptionLabPortable
0
1# coding=utf-82# Copyright 2024 weak-kajuma and the HuggingFace Inc. team. All rights reserved.3#4# This code is based on Llama implementations in this library and Microsoft's5# Differential Transformer implementations.6 7# Licensed under the Apache License, Version 2.0 (the "License");8# you may not use this file except in compliance with the License.9# You may obtain a copy of the License at10#11# http://www.apache.org/licenses/LICENSE-2.012#13# Unless required by applicable law or agreed to in writing, software14# distributed under the License is distributed on an "AS IS" BASIS,15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16# See the License for the specific language governing permissions and17# limitations under the License.18"""DiffLlama model configuration"""19 20from ...configuration_utils import PretrainedConfig21from ...modeling_rope_utils import rope_config_validation22 23 24class DiffLlamaConfig(PretrainedConfig):25 r"""26 This is the configuration class to store the configuration of a [`DiffLlamaModel`]. It is used to instantiate an DiffLlama27 model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults28 will yield a similar configuration to that of the [kajuma/DiffLlama-0.3B-handcut](https://huggingface.co/kajuma/DiffLlama-0.3B-handcut).29 30 Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the31 documentation from [`PretrainedConfig`] for more information.32 33 34 Args:35 vocab_size (`int`, *optional*, defaults to 32000):36 Vocabulary size of the DiffLlama model. Defines the number of different tokens that can be represented by the37 `inputs_ids` passed when calling [`DiffLlamaModel`]38 hidden_size (`int`, *optional*, defaults to 2048):39 Dimension of the hidden representations.40 intermediate_size (`int`, *optional*, defaults to 8192):41 Dimension of the MLP representations.42 num_hidden_layers (`int`, *optional*, defaults to 16):43 Number of hidden layers in the Transformer decoder.44 num_attention_heads (`int`, *optional*, defaults to 32):45 Number of attention heads for each attention layer in the Transformer decoder.46 num_key_value_heads (`int`, *optional*):47 This is the number of key_value heads that should be used to implement Grouped Query Attention. If48 `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if49 `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When50 converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed51 by meanpooling all the original heads within that group. For more details, check out [this52 paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to53 `num_attention_heads`.54 hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):55 The non-linear activation function (function or string) in the decoder.56 max_position_embeddings (`int`, *optional*, defaults to 2048):57 The maximum sequence length that this model might ever be used with.58 initializer_range (`float`, *optional*, defaults to 0.02):59 The standard deviation of the truncated_normal_initializer for initializing all weight matrices.60 rms_norm_eps (`float`, *optional*, defaults to 1e-05):61 The epsilon used by the rms normalization layers.62 use_cache (`bool`, *optional*, defaults to `True`):63 Whether or not the model should return the last key/values attentions (not used by all models). Only64 relevant if `config.is_decoder=True`.65 pad_token_id (`int`, *optional*):66 Padding token id.67 bos_token_id (`int`, *optional*, defaults to 1):68 Beginning of stream token id.69 eos_token_id (`int`, *optional*, defaults to 2):70 End of stream token id.71 tie_word_embeddings (`bool`, *optional*, defaults to `False`):72 Whether to tie weight embeddings73 rope_theta (`float`, *optional*, defaults to 10000.0):74 The base period of the RoPE embeddings.75 rope_scaling (`Dict`, *optional*):76 Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type77 and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value78 accordingly.79 Expected contents:80 `rope_type` (`str`):81 The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',82 'diffllama3'], with 'default' being the original RoPE implementation.83 `factor` (`float`, *optional*):84 Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In85 most scaling types, a `factor` of x will enable the model to handle sequences of length x *86 original maximum pre-trained length.87 `original_max_position_embeddings` (`int`, *optional*):88 Used with 'dynamic', 'longrope' and 'diffllama3'. The original max position embeddings used during89 pretraining.90 `attention_factor` (`float`, *optional*):91 Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention92 computation. If unspecified, it defaults to value recommended by the implementation, using the93 `factor` field to infer the suggested value.94 `beta_fast` (`float`, *optional*):95 Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear96 ramp function. If unspecified, it defaults to 32.97 `beta_slow` (`float`, *optional*):98 Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear99 ramp function. If unspecified, it defaults to 1.100 `short_factor` (`list[float]`, *optional*):101 Only used with 'longrope'. The scaling factor to be applied to short contexts (<102 `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden103 size divided by the number of attention heads divided by 2104 `long_factor` (`list[float]`, *optional*):105 Only used with 'longrope'. The scaling factor to be applied to long contexts (<106 `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden107 size divided by the number of attention heads divided by 2108 `low_freq_factor` (`float`, *optional*):109 Only used with 'diffllama3'. Scaling factor applied to low frequency components of the RoPE110 `high_freq_factor` (`float`, *optional*):111 Only used with 'diffllama3'. Scaling factor applied to high frequency components of the RoPE112 attention_bias (`bool`, *optional*, defaults to `False`):113 Whether to use a bias in the query, key, value and output projection layers during self-attention.114 attention_dropout (`float`, *optional*, defaults to 0.0):115 The dropout ratio for the attention probabilities.116 lambda_std_dev (`float`, *optional*, defaults to 0.1):117 The standard deviation for initialization of parameter lambda in attention layer.118 head_dim (`int`, *optional*):119 The attention head dimension. If None, it will default to hidden_size // num_heads120 121 ```python122 >>> from transformers import DiffLlamaModel, DiffLlamaConfig123 124 >>> # Initializing a DiffLlama diffllama-7b style configuration125 >>> configuration = DiffLlamaConfig()126 127 >>> # Initializing a model from the diffllama-7b style configuration128 >>> model = DiffLlamaModel(configuration)129 130 >>> # Accessing the model configuration131 >>> configuration = model.config132 ```"""133 134 model_type = "diffllama"135 keys_to_ignore_at_inference = ["past_key_values"]136 137 def __init__(138 self,139 vocab_size=32000,140 hidden_size=2048,141 intermediate_size=8192,142 num_hidden_layers=16,143 num_attention_heads=32,144 num_key_value_heads=None,145 hidden_act="silu",146 max_position_embeddings=2048,147 initializer_range=0.02,148 rms_norm_eps=1e-5,149 use_cache=True,150 pad_token_id=None,151 bos_token_id=1,152 eos_token_id=2,153 tie_word_embeddings=False,154 rope_theta=10000.0,155 rope_scaling=None,156 attention_bias=False,157 attention_dropout=0.0,158 lambda_std_dev=0.1,159 head_dim=None,160 **kwargs,161 ):162 self.vocab_size = vocab_size163 self.max_position_embeddings = max_position_embeddings164 self.hidden_size = hidden_size165 self.intermediate_size = intermediate_size166 self.num_hidden_layers = num_hidden_layers167 self.num_attention_heads = num_attention_heads168 169 # for backward compatibility170 if num_key_value_heads is None:171 num_key_value_heads = num_attention_heads172 173 self.num_key_value_heads = num_key_value_heads174 self.hidden_act = hidden_act175 self.initializer_range = initializer_range176 self.rms_norm_eps = rms_norm_eps177 self.use_cache = use_cache178 self.rope_theta = rope_theta179 self.rope_scaling = rope_scaling180 self.attention_bias = attention_bias181 self.attention_dropout = attention_dropout182 self.lambda_std_dev = lambda_std_dev183 self.head_dim = head_dim if head_dim is not None else self.hidden_size // self.num_attention_heads184 # Validate the correctness of rotary position embeddings parameters185 # BC: if there is a 'type' field, copy it it to 'rope_type'.186 if self.rope_scaling is not None and "type" in self.rope_scaling:187 self.rope_scaling["rope_type"] = self.rope_scaling["type"]188 rope_config_validation(self)189 190 super().__init__(191 pad_token_id=pad_token_id,192 bos_token_id=bos_token_id,193 eos_token_id=eos_token_id,194 tie_word_embeddings=tie_word_embeddings,195 **kwargs,196 )197 198 199__all__ = ["DiffLlamaConfig"]200 