appleeji commited on
Commit
a123412
·
verified ·
1 Parent(s): ecef0fd

Upload configs.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. configs.py +145 -0
configs.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers.configuration_utils import PretrainedConfig
2
+ class EConfig(PretrainedConfig):
3
+ r"""
4
+ This is the configuration class to store the configuration of a [`LlamaModel`]. It is used to instantiate an LLaMA
5
+ model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
6
+ defaults will yield a similar configuration to that of the LLaMA-7B.
7
+
8
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
9
+ documentation from [`PretrainedConfig`] for more information.
10
+
11
+
12
+ Args:
13
+ vocab_size (`int`, *optional*, defaults to 32000):
14
+ Vocabulary size of the LLaMA model. Defines the number of different tokens that can be represented by the
15
+ `inputs_ids` passed when calling [`LlamaModel`]
16
+ hidden_size (`int`, *optional*, defaults to 4096):
17
+ Dimension of the hidden representations.
18
+ intermediate_size (`int`, *optional*, defaults to 11008):
19
+ Dimension of the MLP representations.
20
+ num_hidden_layers (`int`, *optional*, defaults to 32):
21
+ Number of hidden layers in the Transformer encoder.
22
+ num_attention_heads (`int`, *optional*, defaults to 32):
23
+ Number of attention heads for each attention layer in the Transformer encoder.
24
+ num_key_value_heads (`int`, *optional*):
25
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
26
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
27
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
28
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
29
+ by meanpooling all the original heads within that group. For more details checkout [this
30
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
31
+ `num_attention_heads`.
32
+ pretraining_tp (`int`, *optional*, defaults to `1`):
33
+ Experimental feature. Tensor parallelism rank used during pretraining. Please refer to [this
34
+ document](https://huggingface.co/docs/transformers/parallelism) to understand more about it. This value is
35
+ necessary to ensure exact reproducibility of the pretraining results. Please refer to [this
36
+ issue](https://github.com/pytorch/pytorch/issues/76232).
37
+ hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
38
+ The non-linear activation function (function or string) in the decoder.
39
+ max_position_embeddings (`int`, *optional*, defaults to 2048):
40
+ The maximum sequence length that this model might ever be used with. Typically set this to something large
41
+ just in case (e.g., 512 or 1024 or 2048).
42
+ initializer_range (`float`, *optional*, defaults to 0.02):
43
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
44
+ rms_norm_eps (`float`, *optional*, defaults to 1e-12):
45
+ The epsilon used by the rms normalization layers.
46
+ use_cache (`bool`, *optional*, defaults to `True`):
47
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
48
+ relevant if `config.is_decoder=True`.
49
+ tie_word_embeddings(`bool`, *optional*, defaults to `False`):
50
+ Whether to tie weight embeddings
51
+ rope_scaling (`Dict`, *optional*):
52
+ Dictionary containing the scaling configuration for the RoPE embeddings. Currently supports two scaling
53
+ strategies: linear and dynamic. Their scaling factor must be an float greater than 1. The expected format
54
+ is `{"type": strategy name, "factor": scaling factor}`. When using this flag, don't update
55
+ `max_position_embeddings` to the expected new maximum. See the following thread for more information on how
56
+ these scaling strategies behave:
57
+ https://www.reddit.com/r/LocalLLaMA/comments/14mrgpr/dynamically_scaled_rope_further_increases/. This is an
58
+ experimental feature, subject to breaking API changes in future versions.
59
+
60
+ Example:
61
+
62
+ ```python
63
+ >>> from transformers import LlamaModel, LlamaConfig
64
+
65
+ >>> # Initializing a LLaMA llama-7b style configuration
66
+ >>> configuration = LlamaConfig()
67
+
68
+ >>> # Initializing a model from the llama-7b style configuration
69
+ >>> model = LlamaModel(configuration)
70
+
71
+ >>> # Accessing the model configuration
72
+ >>> configuration = model.config
73
+ ```"""
74
+ model_type = "llama"
75
+ keys_to_ignore_at_inference = ["past_key_values"]
76
+
77
+ def __init__(
78
+ self,
79
+ vocab_size=32000,
80
+ hidden_size=4096,
81
+ intermediate_size=11008,
82
+ num_hidden_layers=32,
83
+ num_attention_heads=32,
84
+ num_key_value_heads=None,
85
+ hidden_act="silu",
86
+ max_position_embeddings=2048,
87
+ initializer_range=0.02,
88
+ rms_norm_eps=1e-6,
89
+ use_cache=True,
90
+ pad_token_id=None,
91
+ bos_token_id=1,
92
+ eos_token_id=2,
93
+ pretraining_tp=1,
94
+ tie_word_embeddings=False,
95
+ rope_scaling=None,
96
+ **kwargs,
97
+ ):
98
+ self.vocab_size = vocab_size
99
+ self.max_position_embeddings = max_position_embeddings
100
+ self.hidden_size = hidden_size
101
+ self.intermediate_size = intermediate_size
102
+ self.num_hidden_layers = num_hidden_layers
103
+ self.num_attention_heads = num_attention_heads
104
+
105
+ # for backward compatibility
106
+ if num_key_value_heads is None:
107
+ num_key_value_heads = num_attention_heads
108
+
109
+ self.num_key_value_heads = num_key_value_heads
110
+ self.hidden_act = hidden_act
111
+ self.initializer_range = initializer_range
112
+ self.rms_norm_eps = rms_norm_eps
113
+ self.pretraining_tp = pretraining_tp
114
+ self.use_cache = use_cache
115
+ self.rope_scaling = rope_scaling
116
+ self._rope_scaling_validation()
117
+
118
+ super().__init__(
119
+ pad_token_id=pad_token_id,
120
+ bos_token_id=bos_token_id,
121
+ eos_token_id=eos_token_id,
122
+ tie_word_embeddings=tie_word_embeddings,
123
+ **kwargs,
124
+ )
125
+
126
+ def _rope_scaling_validation(self):
127
+ """
128
+ Validate the `rope_scaling` configuration.
129
+ """
130
+ if self.rope_scaling is None:
131
+ return
132
+
133
+ if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
134
+ raise ValueError(
135
+ "`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, "
136
+ f"got {self.rope_scaling}"
137
+ )
138
+ rope_scaling_type = self.rope_scaling.get("type", None)
139
+ rope_scaling_factor = self.rope_scaling.get("factor", None)
140
+ if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
141
+ raise ValueError(
142
+ f"`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
143
+ )
144
+ if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
145
+ raise ValueError(f"`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}")