Yixing-Li commited on
Commit
770bfdf
·
1 Parent(s): 9ee787e
hybrid_model_lv_mixed_moe/config.json ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "modelglue/which-4/ffn_mixed_moe",
3
+ "architectures": [
4
+ "MixtralForCausalLM"
5
+ ],
6
+ "attention_bias": false,
7
+ "attention_dropout": 0.0,
8
+ "auto_map": {
9
+ "AutoConfig": "configuration_mixtral_model_level_hybrid.MixtralModelLevelHybridConfig",
10
+ "AutoModelForCausalLM": "modeling_mixtral_model_level_hybrid.MixtralModelLevelHybridForCausalLM"
11
+ },
12
+ "badwordsids": "[[29961], [14352], [24630], [29962], [11759], [15974], [5519], [25473], [18899], [25901], [7110], [9341], [13531], [518], [9310], [2636], [3366], [21069], [11970], [23098], [16733], [21298], [18173], [10846], [3816], [28513], [15625], [23192], [28166], [10062], [1385], [11724], [3108], [15555], [10834], [10370], [14330], [1822], [12436], [5262], [17094], [10725], [17077], [11424], [4197], [24406], [13359], [17531], [24566], [23076], [4514], [13192], [19942], [16261], [7072], [6024], [1402], [1839], [2033], [13970], [850], [5913], [28895], [5387], [8308], [24927], [5691], [12940], [19997], [18959], [11287], [16862], [4638], [22322], [29861], [21251], [14704], [17548], [12452], [17288], [23160], [24960], [8219], [18024], [5539], [7464], [27865], [29588], [20068], [19660], [27706], [22896], [24264], [12258], [2314], [4400], [5586], [12622], [6796], [7226], [21939], [18456], [14178], [21540], [21945], [14664], [16215], [10338], [17361], [7503], [13769], [26073], [9601], [26909], [7961], [8999], [20840], [16272], [21545], [3199], [10514], [5159], [22689], [6525], [20526], [27077], [18017]]",
13
+ "bos_token_id": 1,
14
+ "eos_token_id": 2,
15
+ "hidden_act": "silu",
16
+ "hidden_size": 4096,
17
+ "initializer_range": 0.02,
18
+ "intermediate_size": 11008,
19
+ "max_position_embeddings": 4096,
20
+ "model_type": "mixtral_block_level",
21
+ "num_attention_heads": 32,
22
+ "num_dense_layers": 16,
23
+ "num_experts_per_tok": 2,
24
+ "num_hidden_layers": 32,
25
+ "num_key_value_heads": 32,
26
+ "num_local_experts": 4,
27
+ "output_router_logits": false,
28
+ "pretraining_tp": 1,
29
+ "rms_norm_eps": 1e-05,
30
+ "rope_scaling": null,
31
+ "rope_theta": 10000.0,
32
+ "router_aux_loss_coef": 0.001,
33
+ "router_jitter_noise": 0.0,
34
+ "sequence_pooler": "mean",
35
+ "sliding_window": null,
36
+ "tie_word_embeddings": false,
37
+ "torch_dtype": "bfloat16",
38
+ "transformers_version": "4.40.1",
39
+ "use_cache": false,
40
+ "vocab_size": 32000
41
+ }
hybrid_model_lv_mixed_moe/configuration_mixtral_model_level_hybrid.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2023 Mixtral AI 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
+ """ Mixtral model configuration"""
16
+
17
+ from transformers.configuration_utils import PretrainedConfig
18
+ from transformers.utils import logging
19
+
20
+ logger = logging.get_logger(__name__)
21
+
22
+
23
+ class MixtralModelLevelHybridConfig(PretrainedConfig):
24
+ r"""
25
+ This is the configuration class to store the configuration of a [`MixtralModel`]. It is used to instantiate an
26
+ Mixtral model according to the specified arguments, defining the model architecture. Instantiating a configuration
27
+ with the defaults will yield a similar configuration to that of the Mixtral-7B-v0.1 or Mixtral-7B-Instruct-v0.1.
28
+
29
+ [mixtralai/Mixtral-8x7B](https://huggingface.co/mixtralai/Mixtral-8x7B)
30
+ [mixtralai/Mixtral-7B-Instruct-v0.1](https://huggingface.co/mixtralai/Mixtral-7B-Instruct-v0.1)
31
+
32
+ Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
33
+ documentation from [`PretrainedConfig`] for more information.
34
+
35
+
36
+ Args:
37
+ vocab_size (`int`, *optional*, defaults to 32000):
38
+ Vocabulary size of the Mixtral model. Defines the number of different tokens that can be represented by the
39
+ `inputs_ids` passed when calling [`MixtralModel`]
40
+ hidden_size (`int`, *optional*, defaults to 4096):
41
+ Dimension of the hidden representations.
42
+ intermediate_size (`int`, *optional*, defaults to 14336):
43
+ Dimension of the MLP representations.
44
+ num_hidden_layers (`int`, *optional*, defaults to 32):
45
+ Number of hidden layers in the Transformer encoder.
46
+ num_attention_heads (`int`, *optional*, defaults to 32):
47
+ Number of attention heads for each attention layer in the Transformer encoder.
48
+ num_key_value_heads (`int`, *optional*, defaults to 8):
49
+ This is the number of key_value heads that should be used to implement Grouped Query Attention. If
50
+ `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
51
+ `num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
52
+ converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
53
+ by meanpooling all the original heads within that group. For more details checkout [this
54
+ paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to `8`.
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 `4096*32`):
58
+ The maximum sequence length that this model might ever be used with. Mixtral's sliding window attention
59
+ allows sequence of up to 4096*32 tokens.
60
+ initializer_range (`float`, *optional*, defaults to 0.02):
61
+ The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
62
+ rms_norm_eps (`float`, *optional*, defaults to 1e-05):
63
+ The epsilon used by the rms normalization layers.
64
+ use_cache (`bool`, *optional*, defaults to `True`):
65
+ Whether or not the model should return the last key/values attentions (not used by all models). Only
66
+ relevant if `config.is_decoder=True`.
67
+ pad_token_id (`int`, *optional*):
68
+ The id of the padding token.
69
+ bos_token_id (`int`, *optional*, defaults to 1):
70
+ The id of the "beginning-of-sequence" token.
71
+ eos_token_id (`int`, *optional*, defaults to 2):
72
+ The id of the "end-of-sequence" token.
73
+ tie_word_embeddings (`bool`, *optional*, defaults to `False`):
74
+ Whether the model's input and output word embeddings should be tied.
75
+ rope_theta (`float`, *optional*, defaults to 1000000.0):
76
+ The base period of the RoPE embeddings.
77
+ sliding_window (`int`, *optional*):
78
+ Sliding window attention window size. If not specified, will default to `4096`.
79
+ attention_dropout (`float`, *optional*, defaults to 0.0):
80
+ The dropout ratio for the attention probabilities.
81
+ num_experts_per_tok (`int`, *optional*, defaults to 2):
82
+ The number of experts to root per-token, can be also interpreted as the `top-p` routing
83
+ parameter
84
+ num_local_experts (`int`, *optional*, defaults to 8):
85
+ Number of experts per Sparse MLP layer.
86
+ output_router_logits (`bool`, *optional*, defaults to `False`):
87
+ Whether or not the router logits should be returned by the model. Enabeling this will also
88
+ allow the model to output the auxiliary loss. See [here]() for more details
89
+ router_aux_loss_coef (`float`, *optional*, defaults to 0.001):
90
+ The aux loss factor for the total loss.
91
+
92
+ ```python
93
+ >>> from transformers import MixtralModel, MixtralConfig
94
+
95
+ >>> # Initializing a Mixtral 7B style configuration
96
+ >>> configuration = MixtralConfig()
97
+
98
+ >>> # Initializing a model from the Mixtral 7B style configuration
99
+ >>> model = MixtralModel(configuration)
100
+
101
+ >>> # Accessing the model configuration
102
+ >>> configuration = model.config
103
+ ```"""
104
+
105
+ model_type = "mixtral_block_level"
106
+ keys_to_ignore_at_inference = ["past_key_values"]
107
+
108
+ def __init__(
109
+ self,
110
+ vocab_size=32000,
111
+ hidden_size=4096,
112
+ intermediate_size=14336,
113
+ num_hidden_layers=32,
114
+ num_attention_heads=32,
115
+ num_key_value_heads=8,
116
+ hidden_act="silu",
117
+ max_position_embeddings=4096 * 32,
118
+ initializer_range=0.02,
119
+ rms_norm_eps=1e-5,
120
+ use_cache=True,
121
+ pad_token_id=None,
122
+ bos_token_id=1,
123
+ eos_token_id=2,
124
+ tie_word_embeddings=False,
125
+ rope_theta=1e6,
126
+ sliding_window=None,
127
+ attention_dropout=0.0,
128
+ num_experts_per_tok=2,
129
+ num_local_experts=8,
130
+ output_router_logits=False,
131
+ router_aux_loss_coef=0.001,
132
+ num_dense_layers=0,
133
+ sequence_pooler="mean",
134
+ **kwargs,
135
+ ):
136
+ self.vocab_size = vocab_size
137
+ self.max_position_embeddings = max_position_embeddings
138
+ self.hidden_size = hidden_size
139
+ self.intermediate_size = intermediate_size
140
+ self.num_hidden_layers = num_hidden_layers
141
+ self.num_attention_heads = num_attention_heads
142
+ self.sliding_window = sliding_window
143
+
144
+ # for backward compatibility
145
+ if num_key_value_heads is None:
146
+ num_key_value_heads = num_attention_heads
147
+
148
+ self.num_key_value_heads = num_key_value_heads
149
+ self.hidden_act = hidden_act
150
+ self.initializer_range = initializer_range
151
+ self.rms_norm_eps = rms_norm_eps
152
+ self.use_cache = use_cache
153
+ self.rope_theta = rope_theta
154
+ self.attention_dropout = attention_dropout
155
+
156
+ self.num_experts_per_tok = num_experts_per_tok
157
+ self.num_local_experts = num_local_experts
158
+ self.output_router_logits = output_router_logits
159
+ self.router_aux_loss_coef = router_aux_loss_coef
160
+ self.num_dense_layers = num_dense_layers
161
+ self.sequence_pooler = sequence_pooler
162
+ super().__init__(
163
+ pad_token_id=pad_token_id,
164
+ bos_token_id=bos_token_id,
165
+ eos_token_id=eos_token_id,
166
+ tie_word_embeddings=tie_word_embeddings,
167
+ **kwargs,
168
+ )
169
+
170
+
171
+ MixtralModelLevelHybridConfig.register_for_auto_class("AutoConfig")
hybrid_model_lv_mixed_moe/model-00001-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9c1d875e0455beb95f81854296b81453b60b7ce91d3a06f79b6de802d0801681
3
+ size 9993615456
hybrid_model_lv_mixed_moe/model-00002-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1f4c9f969554aae738862aecd4276adc1a1cc16108f90f0719c38bc413143fd4
3
+ size 9919542664
hybrid_model_lv_mixed_moe/model-00003-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0fbaa877dff3c1348cd8160389996df782efe5f7f8a6edf8f602055191bb48a
3
+ size 9972655456
hybrid_model_lv_mixed_moe/model-00004-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:61784c5bbefa0e5966b822a73e108f8def6ab1ecdfe6d72ab5c582e84ff3f7e7
3
+ size 3019943056
hybrid_model_lv_mixed_moe/model.safetensors.index.json ADDED
@@ -0,0 +1 @@
 
 
1
+ {"metadata": {"mergekit_version": "0.0.4"}, "weight_map": {"model.embed_tokens.weight": "model-00001-of-00004.safetensors", "model.norm.weight": "model-00001-of-00004.safetensors", "lm_head.weight": "model-00001-of-00004.safetensors", "model.layers.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.4.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.5.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.6.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.7.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.8.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.9.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.10.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.11.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.12.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.13.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.14.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.15.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.19.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.19.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.19.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.19.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.20.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.20.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.20.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.20.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.21.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.21.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.21.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.21.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.22.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.22.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.22.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.22.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.23.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.23.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.23.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.23.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.24.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.24.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.24.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.24.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.25.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.25.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.25.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.25.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.26.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.26.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.26.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.26.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.27.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.27.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.27.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.27.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.28.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.28.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.28.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.28.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.29.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.29.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.29.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.29.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.30.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.30.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.30.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.30.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.31.experts.0.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.31.experts.1.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.31.experts.2.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.31.experts.3.input_layernorm.weight": "model-00001-of-00004.safetensors", "model.layers.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.4.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.9.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.10.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.11.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.12.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.13.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.14.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.15.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.19.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.19.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.19.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.19.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.20.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.20.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.20.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.20.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.21.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.21.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.21.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.21.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.22.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.22.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.22.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.22.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.23.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.23.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.23.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.23.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.24.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.24.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.24.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.24.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.25.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.25.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.25.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.25.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.26.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.26.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.26.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.26.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.27.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.27.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.27.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.27.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.28.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.28.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.28.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.28.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.29.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.29.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.29.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.29.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.30.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.30.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.30.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.30.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.31.experts.0.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.31.experts.1.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.31.experts.2.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.31.experts.3.mlp.up_proj.weight": "model-00001-of-00004.safetensors", "model.layers.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.1.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.2.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.3.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.4.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.5.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.6.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.7.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.8.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.9.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.10.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.11.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.12.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.13.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.14.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.15.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.1.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.2.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.16.experts.3.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.1.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.2.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.17.experts.3.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.0.mlp.down_proj.weight": "model-00001-of-00004.safetensors", "model.layers.18.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.23.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.23.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.23.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.23.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.24.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.24.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.24.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.24.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.25.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.25.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.25.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.25.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.26.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.26.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.26.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.26.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.27.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.27.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.27.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.27.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.28.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.28.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.28.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.28.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.29.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.29.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.29.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.29.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.30.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.30.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.30.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.30.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.31.experts.0.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.31.experts.1.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.31.experts.2.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.31.experts.3.mlp.down_proj.weight": "model-00002-of-00004.safetensors", "model.layers.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.4.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.5.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.6.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.7.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.8.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.9.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.10.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.11.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.12.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.13.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.14.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.15.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.16.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.17.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.18.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.19.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.20.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.21.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.22.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.23.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.23.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.23.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.23.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.24.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.24.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.24.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.24.experts.3.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.25.experts.0.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.25.experts.1.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.25.experts.2.mlp.gate_proj.weight": "model-00002-of-00004.safetensors", "model.layers.25.experts.3.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.0.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.1.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.2.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.3.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.0.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.1.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.2.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.3.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.0.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.1.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.2.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.3.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.0.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.1.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.2.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.3.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.0.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.1.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.2.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.3.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.0.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.1.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.2.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.3.mlp.gate_proj.weight": "model-00003-of-00004.safetensors", "model.layers.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.4.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.5.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.6.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.7.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.8.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.9.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.10.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.11.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.12.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.13.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.14.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.15.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.0.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.1.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.2.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.3.post_attention_layernorm.weight": "model-00003-of-00004.safetensors", "model.layers.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.4.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.5.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.6.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.7.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.8.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.9.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.10.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.11.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.12.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.13.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.14.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.15.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.0.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.1.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.2.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.3.self_attn.q_proj.weight": "model-00003-of-00004.safetensors", "model.layers.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.4.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.5.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.6.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.7.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.8.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.9.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.10.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.11.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.12.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.13.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.14.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.15.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.30.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.0.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.1.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.2.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.31.experts.3.self_attn.k_proj.weight": "model-00003-of-00004.safetensors", "model.layers.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.4.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.5.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.6.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.7.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.8.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.9.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.10.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.11.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.12.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.13.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.14.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.15.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.16.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.17.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.18.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.19.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.20.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.21.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.22.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.23.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.24.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.25.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.26.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.27.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.2.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.28.experts.3.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.0.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.1.self_attn.v_proj.weight": "model-00003-of-00004.safetensors", "model.layers.29.experts.2.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.29.experts.3.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.30.experts.0.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.30.experts.1.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.30.experts.2.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.30.experts.3.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.31.experts.0.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.31.experts.1.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.31.experts.2.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.31.experts.3.self_attn.v_proj.weight": "model-00004-of-00004.safetensors", "model.layers.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.4.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.5.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.6.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.7.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.8.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.9.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.10.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.11.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.12.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.13.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.14.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.15.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.16.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.16.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.16.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.16.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.17.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.17.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.17.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.17.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.18.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.18.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.18.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.18.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.19.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.19.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.19.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.19.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.20.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.20.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.20.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.20.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.21.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.21.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.21.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.21.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.22.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.22.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.22.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.22.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.23.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.23.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.23.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.23.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.24.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.24.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.24.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.24.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.25.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.25.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.25.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.25.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.26.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.26.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.26.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.26.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.27.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.27.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.27.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.27.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.28.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.28.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.28.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.28.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.29.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.29.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.29.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.29.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.30.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.30.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.30.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.30.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.31.experts.0.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.31.experts.1.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.31.experts.2.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.layers.31.experts.3.self_attn.o_proj.weight": "model-00004-of-00004.safetensors", "model.gate.weight": "model-00004-of-00004.safetensors"}}
hybrid_model_lv_mixed_moe/modeling_mixtral_model_level_hybrid.py ADDED
@@ -0,0 +1,1332 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2023 MixtralModelLevelHybrid AI and the HuggingFace Inc. team. All rights reserved.
3
+ #
4
+ # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
5
+ # and OPT implementations in this library. It has been modified from its
6
+ # original forms to accommodate minor architectural differences compared
7
+ # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
8
+ #
9
+ # Licensed under the Apache License, Version 2.0 (the "License");
10
+ # you may not use this file except in compliance with the License.
11
+ # You may obtain a copy of the License at
12
+ #
13
+ # http://www.apache.org/licenses/LICENSE-2.0
14
+ #
15
+ # Unless required by applicable law or agreed to in writing, software
16
+ # distributed under the License is distributed on an "AS IS" BASIS,
17
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ # See the License for the specific language governing permissions and
19
+ # limitations under the License.
20
+ """ PyTorch MixtralModelLevelHybrid model."""
21
+ import inspect
22
+ import math
23
+ import warnings
24
+ from typing import List, Optional, Tuple, Union
25
+
26
+ import torch
27
+ import torch.nn.functional as F
28
+ import torch.utils.checkpoint
29
+ from torch import nn
30
+ from torch.nn import CrossEntropyLoss
31
+ from transformers.activations import ACT2FN
32
+ from transformers.cache_utils import Cache, DynamicCache
33
+ from transformers.modeling_attn_mask_utils import (
34
+ _prepare_4d_causal_attention_mask,
35
+ _prepare_4d_causal_attention_mask_for_sdpa
36
+ )
37
+ from transformers.modeling_outputs import (
38
+ BaseModelOutputWithPast,
39
+ CausalLMOutputWithPast
40
+ )
41
+ from transformers.modeling_utils import PreTrainedModel
42
+ from transformers.utils import (
43
+ add_start_docstrings,
44
+ add_start_docstrings_to_model_forward,
45
+ is_flash_attn_2_available,
46
+ is_flash_attn_greater_or_equal_2_10,
47
+ logging,
48
+ )
49
+
50
+ from .configuration_mixtral_model_level_hybrid import MixtralModelLevelHybridConfig
51
+
52
+ if is_flash_attn_2_available():
53
+ from flash_attn import flash_attn_func, flash_attn_varlen_func
54
+ from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
55
+
56
+ _flash_supports_window_size = "window_size" in list(inspect.signature(flash_attn_func).parameters)
57
+
58
+ logger = logging.get_logger(__name__)
59
+
60
+ _CONFIG_FOR_DOC = "MixtralModelLevelHybridConfig"
61
+
62
+
63
+ # Copied from transformers.models.llama.modeling_llama._get_unpad_data
64
+ def _get_unpad_data(attention_mask):
65
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
66
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
67
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
68
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0))
69
+ return (
70
+ indices,
71
+ cu_seqlens,
72
+ max_seqlen_in_batch,
73
+ )
74
+
75
+
76
+ # Copied from transformers.models.llama.modeling_llama.LlamaRMSNorm with Llama->MixtralModelLevelHybrid
77
+ class MixtralModelLevelHybridRMSNorm(nn.Module):
78
+ def __init__(self, hidden_size, eps=1e-6):
79
+ """
80
+ MixtralModelLevelHybridRMSNorm is equivalent to T5LayerNorm
81
+ """
82
+ super().__init__()
83
+ self.weight = nn.Parameter(torch.ones(hidden_size))
84
+ self.variance_epsilon = eps
85
+
86
+ def forward(self, hidden_states):
87
+ input_dtype = hidden_states.dtype
88
+ hidden_states = hidden_states.to(torch.float32)
89
+ variance = hidden_states.pow(2).mean(-1, keepdim=True)
90
+ hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
91
+ return self.weight * hidden_states.to(input_dtype)
92
+
93
+
94
+ # copied from transformers.models.llama.modeling_llama.LlamaRotaryEmbedding with Llama->MixtralModelLevelHybrid
95
+ # TODO @Arthur no longer copied from LLama after static cache
96
+ class MixtralModelLevelHybridRotaryEmbedding(nn.Module):
97
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
98
+ super().__init__()
99
+
100
+ self.dim = dim
101
+ self.max_position_embeddings = max_position_embeddings
102
+ self.base = base
103
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim))
104
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
105
+
106
+ # Build here to make `torch.jit.trace` work.
107
+ self._set_cos_sin_cache(
108
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
109
+ )
110
+
111
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
112
+ self.max_seq_len_cached = seq_len
113
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq)
114
+
115
+ freqs = torch.outer(t, self.inv_freq)
116
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
117
+ emb = torch.cat((freqs, freqs), dim=-1)
118
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
119
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
120
+
121
+ def forward(self, x, seq_len=None):
122
+ # x: [bs, num_attention_heads, seq_len, head_size]
123
+ if seq_len > self.max_seq_len_cached:
124
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
125
+
126
+ return (
127
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
128
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
129
+ )
130
+
131
+
132
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
133
+ def rotate_half(x):
134
+ """Rotates half the hidden dims of the input."""
135
+ x1 = x[..., : x.shape[-1] // 2]
136
+ x2 = x[..., x.shape[-1] // 2:]
137
+ return torch.cat((-x2, x1), dim=-1)
138
+
139
+
140
+ # copied from transformers.models.llama.modeling_llama.apply_rotary_pos_emb
141
+ # TODO @Arthur no longer copied from LLama after static cache
142
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
143
+ """Applies Rotary Position Embedding to the query and key tensors.
144
+
145
+ Args:
146
+ q (`torch.Tensor`): The query tensor.
147
+ k (`torch.Tensor`): The key tensor.
148
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
149
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
150
+ position_ids (`torch.Tensor`):
151
+ The position indices of the tokens corresponding to the query and key tensors. For example, this can be
152
+ used to pass offsetted position ids when working with a KV-cache.
153
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
154
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
155
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
156
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
157
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
158
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
159
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
160
+ Returns:
161
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
162
+ """
163
+ cos = cos[position_ids].unsqueeze(unsqueeze_dim)
164
+ sin = sin[position_ids].unsqueeze(unsqueeze_dim)
165
+ q_embed = (q * cos) + (rotate_half(q) * sin)
166
+ k_embed = (k * cos) + (rotate_half(k) * sin)
167
+ return q_embed, k_embed
168
+
169
+
170
+ class MixtralModelLevelHybridMLP(nn.Module):
171
+ def __init__(self, config):
172
+ super().__init__()
173
+ self.config = config
174
+ self.hidden_size = config.hidden_size
175
+ self.intermediate_size = config.intermediate_size
176
+ self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
177
+ self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
178
+ self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
179
+ self.act_fn = ACT2FN[config.hidden_act]
180
+
181
+ def forward(self, x):
182
+ return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
183
+
184
+
185
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv
186
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
187
+ """
188
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
189
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
190
+ """
191
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
192
+ if n_rep == 1:
193
+ return hidden_states
194
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
195
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
196
+
197
+
198
+ class MixtralModelLevelHybridAttention(nn.Module):
199
+ """
200
+ Multi-headed attention from 'Attention Is All You Need' paper. Modified to use sliding window attention: Longformer
201
+ and "Generating Long Sequences with Sparse Transformers".
202
+ """
203
+
204
+ def __init__(self, config: MixtralModelLevelHybridConfig, layer_idx: Optional[int] = None):
205
+ super().__init__()
206
+ self.config = config
207
+ self.layer_idx = layer_idx
208
+ if layer_idx is None:
209
+ logger.warning_once(
210
+ f"Instantiating {self.__class__.__name__} without passing a `layer_idx` is not recommended and will "
211
+ "lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` "
212
+ "when creating this class."
213
+ )
214
+
215
+ self.hidden_size = config.hidden_size
216
+ self.num_heads = config.num_attention_heads
217
+ self.head_dim = self.hidden_size // self.num_heads
218
+ self.num_key_value_heads = config.num_key_value_heads
219
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
220
+ self.max_position_embeddings = config.max_position_embeddings
221
+ self.rope_theta = config.rope_theta
222
+ self.is_causal = True
223
+ self.attention_dropout = config.attention_dropout
224
+
225
+ if (self.head_dim * self.num_heads) != self.hidden_size:
226
+ raise ValueError(
227
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
228
+ f" and `num_heads`: {self.num_heads})."
229
+ )
230
+ self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=False)
231
+ self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False)
232
+ self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False)
233
+ self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
234
+
235
+ self.rotary_emb = MixtralModelLevelHybridRotaryEmbedding(
236
+ self.head_dim,
237
+ max_position_embeddings=self.max_position_embeddings,
238
+ base=self.rope_theta,
239
+ )
240
+
241
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
242
+ return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
243
+
244
+ def forward(
245
+ self,
246
+ hidden_states: torch.Tensor,
247
+ attention_mask: Optional[torch.Tensor] = None,
248
+ position_ids: Optional[torch.LongTensor] = None,
249
+ past_key_value: Optional[Cache] = None,
250
+ output_attentions: bool = False,
251
+ use_cache: bool = False,
252
+ **kwargs,
253
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
254
+ if "padding_mask" in kwargs:
255
+ warnings.warn(
256
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
257
+ )
258
+ bsz, q_len, _ = hidden_states.size()
259
+
260
+ query_states = self.q_proj(hidden_states)
261
+ key_states = self.k_proj(hidden_states)
262
+ value_states = self.v_proj(hidden_states)
263
+
264
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
265
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
266
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
267
+
268
+ kv_seq_len = key_states.shape[-2]
269
+ if past_key_value is not None:
270
+ if self.layer_idx is None:
271
+ raise ValueError(
272
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
273
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
274
+ "with a layer index."
275
+ )
276
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
277
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
278
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
279
+
280
+ if past_key_value is not None:
281
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
282
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
283
+
284
+ # repeat k/v heads if n_kv_heads < n_heads
285
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
286
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
287
+
288
+ attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
289
+
290
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
291
+ raise ValueError(
292
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
293
+ f" {attn_weights.size()}"
294
+ )
295
+
296
+ if attention_mask is not None:
297
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
298
+ raise ValueError(
299
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
300
+ )
301
+
302
+ attn_weights = attn_weights + attention_mask
303
+
304
+ # upcast attention to fp32
305
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
306
+ attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training)
307
+ attn_output = torch.matmul(attn_weights, value_states)
308
+
309
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
310
+ raise ValueError(
311
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
312
+ f" {attn_output.size()}"
313
+ )
314
+
315
+ attn_output = attn_output.transpose(1, 2).contiguous()
316
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
317
+
318
+ attn_output = self.o_proj(attn_output)
319
+
320
+ if not output_attentions:
321
+ attn_weights = None
322
+
323
+ return attn_output, attn_weights, past_key_value
324
+
325
+
326
+ class MixtralModelLevelHybridFlashAttention2(MixtralModelLevelHybridAttention):
327
+ """
328
+ MixtralModelLevelHybrid flash attention module. This module inherits from `MixtralModelLevelHybridAttention` as the weights of the module stays
329
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
330
+ flash attention and deal with padding tokens in case the input contains any of them.
331
+ """
332
+
333
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
334
+ def __init__(self, *args, **kwargs):
335
+ super().__init__(*args, **kwargs)
336
+
337
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
338
+ # flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
339
+ # Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
340
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
341
+
342
+ def forward(
343
+ self,
344
+ hidden_states: torch.Tensor,
345
+ attention_mask: Optional[torch.Tensor] = None,
346
+ position_ids: Optional[torch.LongTensor] = None,
347
+ past_key_value: Optional[Cache] = None,
348
+ output_attentions: bool = False,
349
+ use_cache: bool = False,
350
+ **kwargs,
351
+ ):
352
+ if "padding_mask" in kwargs:
353
+ warnings.warn(
354
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
355
+ )
356
+
357
+ # overwrite attention_mask with padding_mask
358
+ attention_mask = kwargs.pop("padding_mask")
359
+ bsz, q_len, _ = hidden_states.size()
360
+
361
+ query_states = self.q_proj(hidden_states)
362
+ key_states = self.k_proj(hidden_states)
363
+ value_states = self.v_proj(hidden_states)
364
+
365
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
366
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
367
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
368
+
369
+ kv_seq_len = key_states.shape[-2]
370
+ if past_key_value is not None:
371
+ if self.layer_idx is None:
372
+ raise ValueError(
373
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
374
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
375
+ "with a layer index."
376
+ )
377
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
378
+
379
+ # Because the input can be padded, the absolute sequence length depends on the max position id.
380
+ rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()) + 1
381
+ cos, sin = self.rotary_emb(value_states, seq_len=rotary_seq_len)
382
+
383
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
384
+
385
+ use_sliding_windows = (
386
+ _flash_supports_window_size
387
+ and getattr(self.config, "sliding_window", None) is not None
388
+ and kv_seq_len > self.config.sliding_window
389
+ )
390
+
391
+ if not _flash_supports_window_size:
392
+ logger.warning_once(
393
+ "The current flash attention version does not support sliding window attention, for a more memory efficient implementation"
394
+ " make sure to upgrade flash-attn library."
395
+ )
396
+
397
+ if past_key_value is not None:
398
+ # Activate slicing cache only if the config has a value `sliding_windows` attribute
399
+ cache_has_contents = past_key_value.get_seq_length(self.layer_idx) > 0
400
+ if (
401
+ getattr(self.config, "sliding_window", None) is not None
402
+ and kv_seq_len > self.config.sliding_window
403
+ and cache_has_contents
404
+ ):
405
+ slicing_tokens = 1 - self.config.sliding_window
406
+
407
+ past_key = past_key_value[self.layer_idx][0]
408
+ past_value = past_key_value[self.layer_idx][1]
409
+
410
+ past_key = past_key[:, :, slicing_tokens:, :].contiguous()
411
+ past_value = past_value[:, :, slicing_tokens:, :].contiguous()
412
+
413
+ if past_key.shape[-2] != self.config.sliding_window - 1:
414
+ raise ValueError(
415
+ f"past key must have a shape of (`batch_size, num_heads, self.config.sliding_window-1, head_dim`), got"
416
+ f" {past_key.shape}"
417
+ )
418
+
419
+ if attention_mask is not None:
420
+ attention_mask = attention_mask[:, slicing_tokens:]
421
+ attention_mask = torch.cat([attention_mask, torch.ones_like(attention_mask[:, -1:])], dim=-1)
422
+
423
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
424
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
425
+
426
+ # repeat k/v heads if n_kv_heads < n_heads
427
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
428
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
429
+ dropout_rate = 0.0 if not self.training else self.attention_dropout
430
+
431
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
432
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
433
+ # cast them back in float16 just to be sure everything works as expected.
434
+ input_dtype = query_states.dtype
435
+ if input_dtype == torch.float32:
436
+ if torch.is_autocast_enabled():
437
+ target_dtype = torch.get_autocast_gpu_dtype()
438
+ # Handle the case where the model is quantized
439
+ elif hasattr(self.config, "_pre_quantization_dtype"):
440
+ target_dtype = self.config._pre_quantization_dtype
441
+ else:
442
+ target_dtype = self.q_proj.weight.dtype
443
+
444
+ logger.warning_once(
445
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
446
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
447
+ f" {target_dtype}."
448
+ )
449
+
450
+ query_states = query_states.to(target_dtype)
451
+ key_states = key_states.to(target_dtype)
452
+ value_states = value_states.to(target_dtype)
453
+
454
+ # Reashape to the expected shape for Flash Attention
455
+ query_states = query_states.transpose(1, 2)
456
+ key_states = key_states.transpose(1, 2)
457
+ value_states = value_states.transpose(1, 2)
458
+
459
+ attn_output = self._flash_attention_forward(
460
+ query_states,
461
+ key_states,
462
+ value_states,
463
+ attention_mask,
464
+ q_len,
465
+ dropout=dropout_rate,
466
+ use_sliding_windows=use_sliding_windows,
467
+ )
468
+
469
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
470
+ attn_output = self.o_proj(attn_output)
471
+
472
+ if not output_attentions:
473
+ attn_weights = None
474
+
475
+ return attn_output, attn_weights, past_key_value
476
+
477
+ def _flash_attention_forward(
478
+ self,
479
+ query_states,
480
+ key_states,
481
+ value_states,
482
+ attention_mask,
483
+ query_length,
484
+ dropout=0.0,
485
+ softmax_scale=None,
486
+ use_sliding_windows=False,
487
+ ):
488
+ """
489
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
490
+ first unpad the input, then computes the attention scores and pad the final attention scores.
491
+
492
+ Args:
493
+ query_states (`torch.Tensor`):
494
+ Input query states to be passed to Flash Attention API
495
+ key_states (`torch.Tensor`):
496
+ Input key states to be passed to Flash Attention API
497
+ value_states (`torch.Tensor`):
498
+ Input value states to be passed to Flash Attention API
499
+ attention_mask (`torch.Tensor`):
500
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
501
+ position of padding tokens and 1 for the position of non-padding tokens.
502
+ dropout (`int`, *optional*):
503
+ Attention dropout
504
+ softmax_scale (`float`, *optional*):
505
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
506
+ use_sliding_windows (`bool`, *optional*):
507
+ Whether to activate sliding window attention.
508
+ """
509
+ if not self._flash_attn_uses_top_left_mask:
510
+ causal = self.is_causal
511
+ else:
512
+ # TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in LlamaFlashAttention2 __init__.
513
+ causal = self.is_causal and query_length != 1
514
+
515
+ # Contains at least one padding token in the sequence
516
+ if attention_mask is not None:
517
+ batch_size = query_states.shape[0]
518
+ query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
519
+ query_states, key_states, value_states, attention_mask, query_length
520
+ )
521
+
522
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
523
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
524
+
525
+ if not use_sliding_windows:
526
+ attn_output_unpad = flash_attn_varlen_func(
527
+ query_states,
528
+ key_states,
529
+ value_states,
530
+ cu_seqlens_q=cu_seqlens_q,
531
+ cu_seqlens_k=cu_seqlens_k,
532
+ max_seqlen_q=max_seqlen_in_batch_q,
533
+ max_seqlen_k=max_seqlen_in_batch_k,
534
+ dropout_p=dropout,
535
+ softmax_scale=softmax_scale,
536
+ causal=causal,
537
+ )
538
+ else:
539
+ attn_output_unpad = flash_attn_varlen_func(
540
+ query_states,
541
+ key_states,
542
+ value_states,
543
+ cu_seqlens_q=cu_seqlens_q,
544
+ cu_seqlens_k=cu_seqlens_k,
545
+ max_seqlen_q=max_seqlen_in_batch_q,
546
+ max_seqlen_k=max_seqlen_in_batch_k,
547
+ dropout_p=dropout,
548
+ softmax_scale=softmax_scale,
549
+ causal=causal,
550
+ window_size=(self.config.sliding_window, self.config.sliding_window),
551
+ )
552
+
553
+ attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
554
+ else:
555
+ if not use_sliding_windows:
556
+ attn_output = flash_attn_func(
557
+ query_states,
558
+ key_states,
559
+ value_states,
560
+ dropout,
561
+ softmax_scale=softmax_scale,
562
+ causal=causal,
563
+ )
564
+ else:
565
+ attn_output = flash_attn_func(
566
+ query_states,
567
+ key_states,
568
+ value_states,
569
+ dropout,
570
+ softmax_scale=softmax_scale,
571
+ causal=causal,
572
+ window_size=(self.config.sliding_window, self.config.sliding_window),
573
+ )
574
+
575
+ return attn_output
576
+
577
+ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
578
+ batch_size, kv_seq_len, num_heads, head_dim = key_layer.shape
579
+
580
+ # On the first iteration we need to properly re-create the padding mask
581
+ # by slicing it on the proper place
582
+ if kv_seq_len != attention_mask.shape[-1]:
583
+ attention_mask_num_tokens = attention_mask.shape[-1]
584
+ attention_mask = attention_mask[:, attention_mask_num_tokens - kv_seq_len:]
585
+
586
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
587
+
588
+ key_layer = index_first_axis(key_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k)
589
+ value_layer = index_first_axis(value_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k)
590
+
591
+ if query_length == kv_seq_len:
592
+ query_layer = index_first_axis(
593
+ query_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k
594
+ )
595
+ cu_seqlens_q = cu_seqlens_k
596
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
597
+ indices_q = indices_k
598
+ elif query_length == 1:
599
+ max_seqlen_in_batch_q = 1
600
+ cu_seqlens_q = torch.arange(
601
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
602
+ ) # There is a memcpy here, that is very bad.
603
+ indices_q = cu_seqlens_q[:-1]
604
+ query_layer = query_layer.squeeze(1)
605
+ else:
606
+ # The -q_len: slice assumes left padding.
607
+ attention_mask = attention_mask[:, -query_length:]
608
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
609
+
610
+ return (
611
+ query_layer,
612
+ key_layer,
613
+ value_layer,
614
+ indices_q,
615
+ (cu_seqlens_q, cu_seqlens_k),
616
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
617
+ )
618
+
619
+
620
+ # copied from transformers.models.llama.modeling_llama.LlamaSdpaAttention with Llama->MixtralModelLevelHybrid
621
+ # TODO @Arthur no longer copied from LLama after static cache
622
+ class MixtralModelLevelHybridSdpaAttention(MixtralModelLevelHybridAttention):
623
+ """
624
+ MixtralModelLevelHybrid attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
625
+ `MixtralModelLevelHybridAttention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
626
+ SDPA API.
627
+ """
628
+
629
+ # Adapted from MixtralModelLevelHybridAttention.forward
630
+ def forward(
631
+ self,
632
+ hidden_states: torch.Tensor,
633
+ attention_mask: Optional[torch.Tensor] = None,
634
+ position_ids: Optional[torch.LongTensor] = None,
635
+ past_key_value: Optional[Cache] = None,
636
+ output_attentions: bool = False,
637
+ use_cache: bool = False,
638
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
639
+ if output_attentions:
640
+ # TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
641
+ logger.warning_once(
642
+ "MixtralModelLevelHybridModel is using MixtralModelLevelHybridSdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
643
+ 'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
644
+ )
645
+ return super().forward(
646
+ hidden_states=hidden_states,
647
+ attention_mask=attention_mask,
648
+ position_ids=position_ids,
649
+ past_key_value=past_key_value,
650
+ output_attentions=output_attentions,
651
+ use_cache=use_cache,
652
+ )
653
+
654
+ bsz, q_len, _ = hidden_states.size()
655
+
656
+ query_states = self.q_proj(hidden_states)
657
+ key_states = self.k_proj(hidden_states)
658
+ value_states = self.v_proj(hidden_states)
659
+
660
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
661
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
662
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
663
+
664
+ kv_seq_len = key_states.shape[-2]
665
+ if past_key_value is not None:
666
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
667
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
668
+
669
+ query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
670
+
671
+ if past_key_value is not None:
672
+ cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
673
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
674
+
675
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
676
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
677
+
678
+ if attention_mask is not None:
679
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
680
+ raise ValueError(
681
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
682
+ )
683
+
684
+ # SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
685
+ # Reference: https://github.com/pytorch/pytorch/issues/112577.
686
+ if query_states.device.type == "cuda" and attention_mask is not None:
687
+ query_states = query_states.contiguous()
688
+ key_states = key_states.contiguous()
689
+ value_states = value_states.contiguous()
690
+
691
+ attn_output = torch.nn.functional.scaled_dot_product_attention(
692
+ query_states,
693
+ key_states,
694
+ value_states,
695
+ attn_mask=attention_mask,
696
+ dropout_p=self.attention_dropout if self.training else 0.0,
697
+ # The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
698
+ is_causal=self.is_causal and attention_mask is None and q_len > 1,
699
+ )
700
+
701
+ attn_output = attn_output.transpose(1, 2).contiguous()
702
+ attn_output = attn_output.view(bsz, q_len, self.hidden_size)
703
+
704
+ attn_output = self.o_proj(attn_output)
705
+
706
+ return attn_output, None, past_key_value
707
+
708
+
709
+ MIXTRAL_ATTENTION_CLASSES = {
710
+ "eager": MixtralModelLevelHybridAttention,
711
+ "flash_attention_2": MixtralModelLevelHybridFlashAttention2,
712
+ "sdpa": MixtralModelLevelHybridSdpaAttention,
713
+ }
714
+
715
+
716
+ class MixtralModelLevelHybridDenseDecoderLayer(nn.Module):
717
+ def __init__(self, config: MixtralModelLevelHybridConfig, layer_idx: int):
718
+ super().__init__()
719
+ self.hidden_size = config.hidden_size
720
+
721
+ self.self_attn = MIXTRAL_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx)
722
+
723
+ self.mlp = MixtralModelLevelHybridMLP(config)
724
+ self.input_layernorm = MixtralModelLevelHybridRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
725
+ self.post_attention_layernorm = MixtralModelLevelHybridRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
726
+
727
+ def forward(
728
+ self,
729
+ hidden_states: torch.Tensor,
730
+ attention_mask: Optional[torch.Tensor] = None,
731
+ position_ids: Optional[torch.LongTensor] = None,
732
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
733
+ output_attentions: Optional[bool] = False,
734
+ use_cache: Optional[bool] = False,
735
+ **kwargs,
736
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
737
+ if "padding_mask" in kwargs:
738
+ warnings.warn(
739
+ "Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
740
+ )
741
+
742
+ residual = hidden_states
743
+
744
+ hidden_states = self.input_layernorm(hidden_states)
745
+
746
+ # Self Attention
747
+ hidden_states, self_attn_weights, present_key_value = self.self_attn(
748
+ hidden_states=hidden_states,
749
+ attention_mask=attention_mask,
750
+ position_ids=position_ids,
751
+ past_key_value=past_key_value,
752
+ output_attentions=output_attentions,
753
+ use_cache=use_cache,
754
+ )
755
+ hidden_states = residual + hidden_states
756
+
757
+ # Fully Connected
758
+ residual = hidden_states
759
+ hidden_states = self.post_attention_layernorm(hidden_states)
760
+ hidden_states = self.mlp(hidden_states)
761
+ hidden_states = residual + hidden_states
762
+
763
+ outputs = (hidden_states,)
764
+
765
+ if output_attentions:
766
+ outputs += (self_attn_weights,)
767
+
768
+ if use_cache:
769
+ outputs += (present_key_value,)
770
+
771
+ return outputs
772
+
773
+
774
+ class MixtralModelLevelHybridSparseDecoderLayer(nn.Module):
775
+ """This is a transformer model level hybrid sparse moe (i.e. DecoderLayer) with top-2 gating."""
776
+
777
+ def __init__(self, config: MixtralModelLevelHybridConfig, layer_idx: int):
778
+ super().__init__()
779
+ self.sequence_pooler = config.sequence_pooler
780
+ self.hidden_dim = config.hidden_size
781
+ self.num_experts = config.num_local_experts
782
+ self.top_k = config.num_experts_per_tok
783
+
784
+ self.experts = nn.ModuleList([
785
+ MixtralModelLevelHybridDenseDecoderLayer(config, layer_idx) for _ in range(self.num_experts)
786
+ ])
787
+
788
+ def forward(
789
+ self,
790
+ hidden_states: torch.Tensor,
791
+ router_logits: Optional[torch.Tensor],
792
+ attention_mask: Optional[torch.Tensor] = None,
793
+ position_ids: Optional[torch.LongTensor] = None,
794
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
795
+ output_attentions: Optional[bool] = False,
796
+ output_router_logits: Optional[bool] = None,
797
+ use_cache: Optional[bool] = False,
798
+ **kwargs,
799
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
800
+ batch_size, sequence_length, hidden_dim = hidden_states.shape
801
+
802
+ # sentence routing
803
+ # design space: first token / mean without padding tokens / mean with padding tokens
804
+ if self.sequence_pooler == "mean":
805
+ sent_states = hidden_states.mean(dim=1, keepdim=True).view(-1, hidden_dim)
806
+ elif self.sequence_pooler == "first_token":
807
+ sent_states = hidden_states[:, 0].view(-1, hidden_dim)
808
+ else:
809
+ raise NotImplementedError(f"{self.sequence_pooler} not implemented yet.")
810
+
811
+ routing_weights = F.softmax(router_logits, dim=1, dtype=torch.float)
812
+ routing_weights, selected_experts = torch.topk(routing_weights, self.top_k, dim=-1)
813
+ routing_weights /= routing_weights.sum(dim=-1, keepdim=True)
814
+ routing_weights = routing_weights.unsqueeze(1).repeat(1, sequence_length, 1)
815
+ routing_weights = routing_weights.to(hidden_states.dtype)
816
+
817
+ final_hidden_states = torch.zeros(
818
+ (batch_size, sequence_length, hidden_dim), dtype=hidden_states.dtype, device=hidden_states.device
819
+ )
820
+ expert_mask = torch.nn.functional.one_hot(selected_experts, num_classes=self.num_experts).permute(2, 1, 0)
821
+
822
+ for expert_idx in range(self.num_experts):
823
+ expert_layer = self.experts[expert_idx]
824
+ idx, top_x = torch.where(expert_mask[expert_idx])
825
+
826
+ if top_x.shape[0] == 0:
827
+ continue
828
+ # in torch it is faster to index using lists than torch tensors
829
+ top_x_list = top_x.tolist()
830
+ idx_list = idx.tolist()
831
+
832
+ expert_hidden_states = hidden_states[top_x_list]
833
+ expert_position_ids = torch.arange(sequence_length).unsqueeze(0).repeat(expert_hidden_states.shape[0], 1)
834
+ expert_attention_mask = attention_mask[top_x_list] if attention_mask is not None else None
835
+
836
+ expert_state = expert_layer(
837
+ expert_hidden_states,
838
+ attention_mask=expert_attention_mask,
839
+ position_ids=expert_position_ids,
840
+ past_key_value=past_key_value,
841
+ output_attentions=output_attentions,
842
+ output_router_logits=output_router_logits,
843
+ use_cache=use_cache,
844
+ )[0]
845
+ current_hidden_states = expert_state * routing_weights[top_x_list, :, idx_list, None]
846
+
847
+ final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype))
848
+
849
+ outputs = (final_hidden_states,)
850
+
851
+ # assert not use_cache, "use_cache is not supported yet for model level hybrid Sparse MoE"
852
+ # assert not output_attentions, "output_attentions is not supported yet for model level hybrid Sparse MoE"
853
+ # if output_attentions:
854
+ # outputs += (self_attn_weights,)
855
+
856
+ # if use_cache:
857
+ # outputs += (present_key_value,)
858
+
859
+ if output_router_logits:
860
+ outputs += (router_logits,)
861
+
862
+ return outputs
863
+
864
+
865
+ MIXTRAL_START_DOCSTRING = r"""
866
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
867
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
868
+ etc.)
869
+
870
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
871
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
872
+ and behavior.
873
+
874
+ Parameters:
875
+ config ([`MixtralModelLevelHybridConfig`]):
876
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
877
+ load the weights associated with the model, only the configuration. Check out the
878
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
879
+ """
880
+
881
+
882
+ @add_start_docstrings(
883
+ "The bare MixtralModelLevelHybrid Model outputting raw hidden-states without any specific head on top.",
884
+ MIXTRAL_START_DOCSTRING,
885
+ )
886
+ class MixtralModelLevelHybridPreTrainedModel(PreTrainedModel):
887
+ config_class = MixtralModelLevelHybridConfig
888
+ base_model_prefix = "model"
889
+ supports_gradient_checkpointing = True
890
+ _no_split_modules = ["MixtralModelLevelHybridDecoderLayer"]
891
+ _skip_keys_device_placement = "past_key_values"
892
+ _supports_flash_attn_2 = True
893
+ _supports_sdpa = True
894
+ _supports_cache_class = True
895
+
896
+ def _init_weights(self, module):
897
+ std = self.config.initializer_range
898
+ if isinstance(module, nn.Linear):
899
+ module.weight.data.normal_(mean=0.0, std=std)
900
+ if module.bias is not None:
901
+ module.bias.data.zero_()
902
+ elif isinstance(module, nn.Embedding):
903
+ module.weight.data.normal_(mean=0.0, std=std)
904
+ if module.padding_idx is not None:
905
+ module.weight.data[module.padding_idx].zero_()
906
+
907
+
908
+ MIXTRAL_INPUTS_DOCSTRING = r"""
909
+ Args:
910
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
911
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
912
+ it.
913
+
914
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
915
+ [`PreTrainedTokenizer.__call__`] for details.
916
+
917
+ [What are input IDs?](../glossary#input-ids)
918
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
919
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
920
+
921
+ - 1 for tokens that are **not masked**,
922
+ - 0 for tokens that are **masked**.
923
+
924
+ [What are attention masks?](../glossary#attention-mask)
925
+
926
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
927
+ [`PreTrainedTokenizer.__call__`] for details.
928
+
929
+ If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
930
+ `past_key_values`).
931
+
932
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
933
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
934
+ information on the default strategy.
935
+
936
+ - 1 indicates the head is **not masked**,
937
+ - 0 indicates the head is **masked**.
938
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
939
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
940
+ config.n_positions - 1]`.
941
+
942
+ [What are position IDs?](../glossary#position-ids)
943
+ past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
944
+ Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
945
+ blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
946
+ returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
947
+
948
+ Two formats are allowed:
949
+ - a [`~cache_utils.Cache`] instance;
950
+ - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
951
+ shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
952
+ cache format.
953
+
954
+ The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
955
+ legacy cache format will be returned.
956
+
957
+ If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
958
+ have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
959
+ of shape `(batch_size, sequence_length)`.
960
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
961
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
962
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
963
+ model's internal embedding lookup matrix.
964
+ use_cache (`bool`, *optional*):
965
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
966
+ `past_key_values`).
967
+ output_attentions (`bool`, *optional*):
968
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
969
+ tensors for more detail.
970
+ output_hidden_states (`bool`, *optional*):
971
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
972
+ more detail.
973
+ return_dict (`bool`, *optional*):
974
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
975
+ """
976
+
977
+
978
+ @add_start_docstrings(
979
+ "The bare MixtralModelLevelHybrid Model outputting raw hidden-states without any specific head on top.",
980
+ MIXTRAL_START_DOCSTRING,
981
+ )
982
+ class MixtralModelLevelHybridModel(MixtralModelLevelHybridPreTrainedModel):
983
+ """
984
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MixtralModelLevelHybridDecoderLayer`]
985
+
986
+ Args:
987
+ config: MixtralModelLevelHybridConfig
988
+ """
989
+
990
+ def __init__(self, config: MixtralModelLevelHybridConfig):
991
+ super().__init__(config)
992
+ self.padding_idx = config.pad_token_id
993
+ self.vocab_size = config.vocab_size
994
+
995
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
996
+ self.layers = nn.ModuleList(
997
+ [MixtralModelLevelHybridSparseDecoderLayer(
998
+ config, layer_idx
999
+ ) if layer_idx >= config.num_dense_layers else MixtralModelLevelHybridDenseDecoderLayer(config, layer_idx)
1000
+ for layer_idx in range(config.num_hidden_layers)]
1001
+ )
1002
+ self.num_dense_layers = config.num_dense_layers
1003
+ self.sequence_pooler = config.sequence_pooler
1004
+ self.gate = nn.Linear(config.hidden_size, config.num_local_experts, bias=False)
1005
+ self._attn_implementation = config._attn_implementation
1006
+ self.norm = MixtralModelLevelHybridRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
1007
+
1008
+ self.gradient_checkpointing = False
1009
+ # Initialize weights and apply final processing
1010
+ self.post_init()
1011
+
1012
+ def get_input_embeddings(self):
1013
+ return self.embed_tokens
1014
+
1015
+ def set_input_embeddings(self, value):
1016
+ self.embed_tokens = value
1017
+
1018
+ @add_start_docstrings_to_model_forward(MIXTRAL_INPUTS_DOCSTRING)
1019
+ def forward(
1020
+ self,
1021
+ input_ids: torch.LongTensor = None,
1022
+ attention_mask: Optional[torch.Tensor] = None,
1023
+ position_ids: Optional[torch.LongTensor] = None,
1024
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1025
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1026
+ use_cache: Optional[bool] = None,
1027
+ output_attentions: Optional[bool] = None,
1028
+ output_hidden_states: Optional[bool] = None,
1029
+ return_dict: Optional[bool] = None,
1030
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
1031
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1032
+ output_hidden_states = (
1033
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1034
+ )
1035
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
1036
+
1037
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1038
+
1039
+ # retrieve input_ids and inputs_embeds
1040
+ if input_ids is not None and inputs_embeds is not None:
1041
+ raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
1042
+ elif input_ids is not None:
1043
+ batch_size, seq_length = input_ids.shape
1044
+ elif inputs_embeds is not None:
1045
+ batch_size, seq_length, _ = inputs_embeds.shape
1046
+ else:
1047
+ raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
1048
+
1049
+ if self.gradient_checkpointing and self.training:
1050
+ if use_cache:
1051
+ logger.warning_once(
1052
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
1053
+ )
1054
+ use_cache = False
1055
+
1056
+ past_key_values_length = 0
1057
+
1058
+ if use_cache:
1059
+ use_legacy_cache = not isinstance(past_key_values, Cache)
1060
+ if use_legacy_cache:
1061
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
1062
+ past_key_values_length = past_key_values.get_usable_length(seq_length)
1063
+
1064
+ if position_ids is None:
1065
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
1066
+ position_ids = torch.arange(
1067
+ past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
1068
+ )
1069
+ position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
1070
+ else:
1071
+ position_ids = position_ids.view(-1, seq_length).long()
1072
+
1073
+ if inputs_embeds is None:
1074
+ inputs_embeds = self.embed_tokens(input_ids)
1075
+
1076
+ if attention_mask is not None and self._attn_implementation == "flash_attention_2" and use_cache:
1077
+ is_padding_right = attention_mask[:, -1].sum().item() != batch_size
1078
+ if is_padding_right:
1079
+ raise ValueError(
1080
+ "You are attempting to perform batched generation with padding_side='right'"
1081
+ " this may lead to unexpected behaviour for Flash Attention version of MixtralModelLevelHybrid. Make sure to "
1082
+ " call `tokenizer.padding_side = 'left'` before tokenizing the input. "
1083
+ )
1084
+
1085
+ if self._attn_implementation == "flash_attention_2":
1086
+ # 2d mask is passed through the layers
1087
+ attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
1088
+ elif self._attn_implementation == "sdpa" and not output_attentions:
1089
+ # output_attentions=True can not be supported when using SDPA, and we fall back on
1090
+ # the manual implementation that requires a 4D causal mask in all cases.
1091
+ attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
1092
+ attention_mask,
1093
+ (batch_size, seq_length),
1094
+ inputs_embeds,
1095
+ past_key_values_length,
1096
+ )
1097
+ else:
1098
+ # 4d mask is passed through the layers
1099
+ attention_mask = _prepare_4d_causal_attention_mask(
1100
+ attention_mask,
1101
+ (batch_size, seq_length),
1102
+ inputs_embeds,
1103
+ past_key_values_length,
1104
+ sliding_window=self.config.sliding_window,
1105
+ )
1106
+
1107
+ hidden_states = inputs_embeds
1108
+
1109
+ # decoder layers
1110
+ all_hidden_states = () if output_hidden_states else None
1111
+ all_self_attns = () if output_attentions else None
1112
+ router_logits = None
1113
+
1114
+ for layer_idx, decoder_layer in enumerate(self.layers):
1115
+ if output_hidden_states:
1116
+ all_hidden_states += (hidden_states,)
1117
+
1118
+ if layer_idx == self.num_dense_layers:
1119
+ # Calculate router logits
1120
+ batch_size, sequence_length, hidden_dim = hidden_states.shape
1121
+ if self.sequence_pooler == "mean":
1122
+ sent_states = hidden_states.mean(dim=1, keepdim=True).view(-1, hidden_dim)
1123
+ elif self.sequence_pooler == "first_token":
1124
+ sent_states = hidden_states[:, 0].view(-1, hidden_dim)
1125
+ else:
1126
+ raise NotImplementedError(f"{self.sequence_pooler} not implemented yet.")
1127
+
1128
+ router_logits = self.gate(sent_states)
1129
+
1130
+ if self.gradient_checkpointing and self.training:
1131
+ raise NotImplementedError("Gradient checkpointing is not supported in MixtralModelLevelHybrid yet.")
1132
+ else:
1133
+ layer_outputs = decoder_layer(
1134
+ hidden_states,
1135
+ router_logits=router_logits,
1136
+ attention_mask=attention_mask,
1137
+ position_ids=position_ids,
1138
+ past_key_value=past_key_values,
1139
+ output_attentions=output_attentions,
1140
+ use_cache=use_cache,
1141
+ )
1142
+
1143
+ hidden_states = layer_outputs[0]
1144
+
1145
+ # if use_cache:
1146
+ # next_decoder_cache = layer_outputs[2 if output_attentions else 1]
1147
+
1148
+ if output_attentions:
1149
+ all_self_attns += (layer_outputs[1],)
1150
+
1151
+ hidden_states = self.norm(hidden_states)
1152
+
1153
+ # add hidden states from the last decoder layer
1154
+ if output_hidden_states:
1155
+ all_hidden_states += (hidden_states,)
1156
+
1157
+ next_cache = None
1158
+
1159
+ if not return_dict:
1160
+ return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
1161
+ return BaseModelOutputWithPast(
1162
+ last_hidden_state=hidden_states,
1163
+ past_key_values=next_cache,
1164
+ hidden_states=all_hidden_states,
1165
+ attentions=all_self_attns,
1166
+ )
1167
+
1168
+
1169
+ class MixtralModelLevelHybridForCausalLM(MixtralModelLevelHybridPreTrainedModel):
1170
+ """
1171
+ Examples
1172
+ --------
1173
+ >>> model = MixtralModelLevelHybridForCausalLM(MixtralModelLevelHybridConfig(vocab_size=100, hidden_size=16, intermediate_size=32, num_dense_layers=0, num_attention_heads=4))
1174
+ >>> outputs = model(input_ids=torch.randint(10, 50, (3,5)))
1175
+ >>> outputs.logits.shape
1176
+ """
1177
+ _tied_weights_keys = ["lm_head.weight"]
1178
+
1179
+ def __init__(self, config):
1180
+ super().__init__(config)
1181
+ self.model = MixtralModelLevelHybridModel(config)
1182
+ self.vocab_size = config.vocab_size
1183
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
1184
+
1185
+ # Initialize weights and apply final processing
1186
+ self.post_init()
1187
+
1188
+ def get_input_embeddings(self):
1189
+ return self.model.embed_tokens
1190
+
1191
+ def set_input_embeddings(self, value):
1192
+ self.model.embed_tokens = value
1193
+
1194
+ def get_output_embeddings(self):
1195
+ return self.lm_head
1196
+
1197
+ def set_output_embeddings(self, new_embeddings):
1198
+ self.lm_head = new_embeddings
1199
+
1200
+ def set_decoder(self, decoder):
1201
+ self.model = decoder
1202
+
1203
+ def get_decoder(self):
1204
+ return self.model
1205
+
1206
+ def forward(
1207
+ self,
1208
+ input_ids: torch.LongTensor = None,
1209
+ attention_mask: Optional[torch.Tensor] = None,
1210
+ position_ids: Optional[torch.LongTensor] = None,
1211
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1212
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1213
+ labels: Optional[torch.LongTensor] = None,
1214
+ use_cache: Optional[bool] = None,
1215
+ output_attentions: Optional[bool] = None,
1216
+ output_hidden_states: Optional[bool] = None,
1217
+ return_dict: Optional[bool] = None,
1218
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1219
+
1220
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1221
+ output_hidden_states = (
1222
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1223
+ )
1224
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1225
+
1226
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1227
+ outputs = self.model(
1228
+ input_ids=input_ids,
1229
+ attention_mask=attention_mask,
1230
+ position_ids=position_ids,
1231
+ past_key_values=past_key_values,
1232
+ inputs_embeds=inputs_embeds,
1233
+ use_cache=use_cache,
1234
+ output_attentions=output_attentions,
1235
+ output_hidden_states=output_hidden_states,
1236
+ return_dict=return_dict,
1237
+ )
1238
+
1239
+ hidden_states = outputs[0]
1240
+ logits = self.lm_head(hidden_states)
1241
+ logits = logits.float()
1242
+
1243
+ loss = None
1244
+ if labels is not None:
1245
+ # Shift so that tokens < n predict n
1246
+ shift_logits = logits[..., :-1, :].contiguous()
1247
+ shift_labels = labels[..., 1:].contiguous()
1248
+ # Flatten the tokens
1249
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1250
+ shift_labels = shift_labels.view(-1)
1251
+ # Ensure tensors are on the same device
1252
+ shift_labels = shift_labels.to(shift_logits.device)
1253
+ loss_fct = CrossEntropyLoss()
1254
+ loss = loss_fct(shift_logits, shift_labels)
1255
+
1256
+ if not return_dict:
1257
+ output = (logits,) + outputs[1:]
1258
+ return (loss,) + output if loss is not None else output
1259
+
1260
+ return CausalLMOutputWithPast(
1261
+ loss=loss,
1262
+ logits=logits,
1263
+ past_key_values=outputs.past_key_values,
1264
+ hidden_states=outputs.hidden_states,
1265
+ attentions=outputs.attentions,
1266
+ )
1267
+
1268
+ def prepare_inputs_for_generation(
1269
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
1270
+ ):
1271
+ # Omit tokens covered by past_key_values
1272
+ if past_key_values is not None:
1273
+ if isinstance(past_key_values, Cache):
1274
+ cache_length = past_key_values.get_seq_length()
1275
+ past_length = past_key_values.seen_tokens
1276
+ max_cache_length = past_key_values.get_max_length()
1277
+ else:
1278
+ cache_length = past_length = past_key_values[0][0].shape[2]
1279
+ max_cache_length = None
1280
+
1281
+ # Keep only the unprocessed tokens:
1282
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1283
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
1284
+ # input)
1285
+ if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
1286
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length):]
1287
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1288
+ # input_ids based on the past_length.
1289
+ elif past_length < input_ids.shape[1]:
1290
+ input_ids = input_ids[:, past_length:]
1291
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1292
+
1293
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1294
+ if (
1295
+ max_cache_length is not None
1296
+ and attention_mask is not None
1297
+ and cache_length + input_ids.shape[1] > max_cache_length
1298
+ ):
1299
+ attention_mask = attention_mask[:, -max_cache_length:]
1300
+
1301
+ position_ids = kwargs.get("position_ids", None)
1302
+ if attention_mask is not None and position_ids is None:
1303
+ # create position_ids on the fly for batch generation
1304
+ position_ids = attention_mask.long().cumsum(-1) - 1
1305
+ position_ids.masked_fill_(attention_mask == 0, 1)
1306
+ if past_key_values:
1307
+ position_ids = position_ids[:, -input_ids.shape[1]:]
1308
+
1309
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1310
+ if inputs_embeds is not None and past_key_values is None:
1311
+ model_inputs = {"inputs_embeds": inputs_embeds}
1312
+ else:
1313
+ model_inputs = {"input_ids": input_ids}
1314
+
1315
+ model_inputs.update(
1316
+ {
1317
+ "position_ids": position_ids,
1318
+ "past_key_values": past_key_values,
1319
+ "use_cache": kwargs.get("use_cache"),
1320
+ "attention_mask": attention_mask,
1321
+ }
1322
+ )
1323
+ return model_inputs
1324
+
1325
+ @staticmethod
1326
+ def _reorder_cache(past_key_values, beam_idx):
1327
+ reordered_past = ()
1328
+ for layer_past in past_key_values:
1329
+ reordered_past += (
1330
+ tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
1331
+ )
1332
+ return reordered_past
hybrid_model_lv_mixed_moe/special_tokens_map.json ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "eos_token": {
10
+ "content": "</s>",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": "<s>",
17
+ "unk_token": {
18
+ "content": "<unk>",
19
+ "lstrip": false,
20
+ "normalized": false,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ }
24
+ }
hybrid_model_lv_mixed_moe/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
hybrid_model_lv_mixed_moe/tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e556afd44213b6bd1be2b850ebbbd98f5481437a8021afaf58ee7fb1818d347
3
+ size 499723
hybrid_model_lv_mixed_moe/tokenizer_config.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_bos_token": true,
3
+ "add_eos_token": false,
4
+ "added_tokens_decoder": {
5
+ "0": {
6
+ "content": "<unk>",
7
+ "lstrip": false,
8
+ "normalized": false,
9
+ "rstrip": false,
10
+ "single_word": false,
11
+ "special": true
12
+ },
13
+ "1": {
14
+ "content": "<s>",
15
+ "lstrip": false,
16
+ "normalized": false,
17
+ "rstrip": false,
18
+ "single_word": false,
19
+ "special": true
20
+ },
21
+ "2": {
22
+ "content": "</s>",
23
+ "lstrip": false,
24
+ "normalized": false,
25
+ "rstrip": false,
26
+ "single_word": false,
27
+ "special": true
28
+ }
29
+ },
30
+ "bos_token": "<s>",
31
+ "clean_up_tokenization_spaces": false,
32
+ "eos_token": "</s>",
33
+ "legacy": true,
34
+ "model_max_length": 1000000000000000019884624838656,
35
+ "pad_token": "<s>",
36
+ "padding_side": "left",
37
+ "sp_model_kwargs": {},
38
+ "spaces_between_special_tokens": false,
39
+ "tokenizer_class": "LlamaTokenizer",
40
+ "trust_remote_code": false,
41
+ "unk_token": "<unk>",
42
+ "use_default_system_prompt": true,
43
+ "use_fast": true
44
+ }