|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright (C) 2025 THL A29 Limited, a Tencent company and the HuggingFace Inc. team. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""HunYuanDenseV1 model configuration""" |
| 16 | + |
| 17 | +from transformers.configuration_utils import PretrainedConfig |
| 18 | +from transformers.utils import logging |
| 19 | + |
| 20 | + |
| 21 | +logger = logging.get_logger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class HunYuanDenseV1Config(PretrainedConfig): |
| 25 | + r""" |
| 26 | + This is the configuration class to store the configuration of a [`HunYuanDenseV1Config`]. It is used to instantiate an |
| 27 | + HunYuan model according to the specified arguments, defining the model architecture. Instantiating a configuration |
| 28 | + with the defaults will yield a similar configuration to that of the HunYuan-7B. |
| 29 | + Hunyuan-7B-Instruct [tencent/Hunyuan-7B-Instruct](https://huggingface.co/tencent/Hunyuan-7B-Instruct). |
| 30 | +
|
| 31 | + Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
| 32 | + documentation from [`PretrainedConfig`] for more information. |
| 33 | +
|
| 34 | +
|
| 35 | + Args: |
| 36 | + vocab_size (`int`, *optional*, defaults to 290943): |
| 37 | + Vocabulary size of the HunYuan model. Defines the number of different tokens that can be represented by the |
| 38 | + `inputs_ids` passed when calling [`HunYuanDenseV1Config`] |
| 39 | + hidden_size (`int`, *optional*, defaults to 4096): |
| 40 | + Dimension of the hidden representations. |
| 41 | + intermediate_size (`int`, *optional*, defaults to 11008): |
| 42 | + Dimension of the MLP representations or shared MLP representations. |
| 43 | + num_hidden_layers (`int`, *optional*, defaults to 32): |
| 44 | + Number of hidden layers in the Transformer decoder. |
| 45 | + num_attention_heads (`int`, *optional*, defaults to 32): |
| 46 | + Number of attention heads for each attention layer in the Transformer decoder. |
| 47 | + num_key_value_heads (`int`, *optional*): |
| 48 | + This is the number of key_value heads that should be used to implement Grouped Query Attention. If |
| 49 | + `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if |
| 50 | + `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When |
| 51 | + converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed |
| 52 | + by meanpooling all the original heads within that group. For more details checkout [this |
| 53 | + paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to |
| 54 | + `num_attention_heads`. |
| 55 | + hidden_act (`str` or `function`, *optional*, defaults to `"silu"`): |
| 56 | + The non-linear activation function (function or string) in the decoder. |
| 57 | + max_position_embeddings (`int`, *optional*, defaults to 2048): |
| 58 | + The maximum sequence length that this model might ever be used with. |
| 59 | + initializer_range (`float`, *optional*, defaults to 0.02): |
| 60 | + The standard deviation of the truncated_normal_initializer for initializing all weight matrices. |
| 61 | + rms_norm_eps (`float`, *optional*, defaults to 1e-05): |
| 62 | + The epsilon used by the rms normalization layers. |
| 63 | + use_cache (`bool`, *optional*, defaults to `True`): |
| 64 | + Whether or not the model should return the last key/values attentions (not used by all models). Only |
| 65 | + relevant if `config.is_decoder=True`. |
| 66 | + pad_token_id (`int`, *optional*, defaults to 0): |
| 67 | + Padding token id. |
| 68 | + bos_token_id (`int`, *optional*, defaults to 1): |
| 69 | + Beginning of stream token id. |
| 70 | + eos_token_id (`int`, *optional*, defaults to 2): |
| 71 | + End of stream token id. |
| 72 | + eod_token_id (int, *optional*, defaults to 3): |
| 73 | + Token ID representing the end-of-document marker. Used to indicate the termination of a text sequence. |
| 74 | + Example: In multi-document processing, this token helps the model distinguish between separate documents. |
| 75 | + pretraining_tp (`int`, *optional*, defaults to 1): |
| 76 | + Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this |
| 77 | + document](https://huggingface.co/docs/transformers/parallelism) to understand more about it. This value is |
| 78 | + necessary to ensure exact reproducibility of the pretraining results. Please refer to [this |
| 79 | + issue](https://github.com/pytorch/pytorch/issues/76232). |
| 80 | + tie_word_embeddings (`bool`, *optional*, defaults to `False`): |
| 81 | + Whether to tie weight embeddings |
| 82 | + rope_theta (`float`, *optional*, defaults to 10000.0): |
| 83 | + The base period of the RoPE embeddings. |
| 84 | + rope_scaling (`Dict`, *optional*): |
| 85 | + Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling |
| 86 | + strategies: linear and dynamic. Their scaling factor must be a float greater than 1. The expected format is |
| 87 | + `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update |
| 88 | + `max_position_embeddings` to the expected new maximum. See the following thread for more information on how |
| 89 | + these scaling strategies behave: |
| 90 | + https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an |
| 91 | + experimental feature, subject to breaking API changes in future versions. |
| 92 | + attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`): |
| 93 | + Whether to use a bias in the query, key, value and output projection layers during self-attention. |
| 94 | + attention_dropout (`float`, *optional*, defaults to 0.0): |
| 95 | + The dropout ratio for the attention probabilities. |
| 96 | + head_dim (`int`, *optional*, defaults to 128): |
| 97 | + The attention head dimension. |
| 98 | + """ |
| 99 | + |
| 100 | + model_type = "hunyuan_v1_dense" |
| 101 | + keys_to_ignore_at_inference = ["past_key_values"] |
| 102 | + |
| 103 | + def __init__( |
| 104 | + self, |
| 105 | + vocab_size=290943, |
| 106 | + hidden_size=4096, |
| 107 | + intermediate_size: int = 11008, |
| 108 | + num_hidden_layers=32, |
| 109 | + num_attention_heads=32, |
| 110 | + num_key_value_heads=None, |
| 111 | + hidden_act="silu", |
| 112 | + max_position_embeddings=2048, |
| 113 | + initializer_range=0.02, |
| 114 | + rms_norm_eps=1e-5, |
| 115 | + use_cache=True, |
| 116 | + pad_token_id=0, |
| 117 | + bos_token_id=1, |
| 118 | + eos_token_id=2, |
| 119 | + eod_token_id=3, |
| 120 | + pretraining_tp=1, |
| 121 | + tie_word_embeddings=False, |
| 122 | + rope_theta=10000.0, |
| 123 | + rope_scaling=None, |
| 124 | + attention_bias=False, |
| 125 | + attention_dropout=0.0, |
| 126 | + head_dim=None, |
| 127 | + **kwargs, |
| 128 | + ): |
| 129 | + self.vocab_size = vocab_size |
| 130 | + self.max_position_embeddings = max_position_embeddings |
| 131 | + self.hidden_size = hidden_size |
| 132 | + self.intermediate_size = intermediate_size |
| 133 | + self.num_hidden_layers = num_hidden_layers |
| 134 | + self.num_attention_heads = num_attention_heads |
| 135 | + self.head_dim = head_dim |
| 136 | + # for backward compatibility |
| 137 | + if num_key_value_heads is None: |
| 138 | + num_key_value_heads = num_attention_heads |
| 139 | + |
| 140 | + self.num_key_value_heads = num_key_value_heads |
| 141 | + self.hidden_act = hidden_act |
| 142 | + self.initializer_range = initializer_range |
| 143 | + self.rms_norm_eps = rms_norm_eps |
| 144 | + self.pretraining_tp = pretraining_tp |
| 145 | + self.use_cache = use_cache |
| 146 | + self.rope_theta = rope_theta |
| 147 | + self.rope_scaling = rope_scaling |
| 148 | + # self._rope_scaling_validation() # TODO: Need validation? |
| 149 | + self.attention_bias = attention_bias |
| 150 | + self.attention_dropout = attention_dropout |
| 151 | + |
| 152 | + super().__init__( |
| 153 | + pad_token_id=pad_token_id, |
| 154 | + bos_token_id=bos_token_id, |
| 155 | + eos_token_id=eos_token_id, |
| 156 | + tie_word_embeddings=tie_word_embeddings, |
| 157 | + **kwargs, |
| 158 | + ) |
| 159 | + |
| 160 | + def _rope_scaling_validation(self): |
| 161 | + """ |
| 162 | + Validate the `rope_scaling` configuration. |
| 163 | + """ |
| 164 | + if self.rope_scaling is None: |
| 165 | + return |
| 166 | + |
| 167 | + if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2: |
| 168 | + raise ValueError( |
| 169 | + "`rope_scaling` must be a dictionary with with two fields, `type` and `factor` or `type` and `alpha`, " |
| 170 | + f"got {self.rope_scaling}" |
| 171 | + ) |
| 172 | + rope_scaling_type = self.rope_scaling.get("type", None) |
| 173 | + rope_scaling_factor = self.rope_scaling.get("factor", None) |
| 174 | + rope_scaling_alpha = self.rope_scaling.get("alpha", None) |
| 175 | + if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]: |
| 176 | + raise ValueError( |
| 177 | + f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}" |
| 178 | + ) |
| 179 | + if rope_scaling_factor is None and rope_scaling_alpha is None: |
| 180 | + raise ValueError("`rope_scaling`'s factor or alpha field must be have one, got both of none") |
| 181 | + if rope_scaling_factor is not None: |
| 182 | + if not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0: |
| 183 | + raise ValueError(f"`rope_scaling`'s factor field must be a float > 1.0, got {rope_scaling_factor}") |
| 184 | + if rope_scaling_alpha is not None: |
| 185 | + if not isinstance(rope_scaling_alpha, float) or rope_scaling_alpha <= 1.0: |
| 186 | + raise ValueError(f"`rope_scaling`'s alpha field must be a float > 1.0, got {rope_scaling_alpha}") |
| 187 | + |
| 188 | + |
| 189 | +__all__ = ["HunYuanDenseV1Config"] |
0 commit comments