AbstractPerspective commited on
Commit
b819c70
·
verified ·
1 Parent(s): 1156556

Upload PhiForCausalLM

Browse files
README.md ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags: []
4
+ ---
5
+
6
+ # Model Card for Model ID
7
+
8
+ <!-- Provide a quick summary of what the model is/does. -->
9
+
10
+
11
+
12
+ ## Model Details
13
+
14
+ ### Model Description
15
+
16
+ <!-- Provide a longer summary of what this model is. -->
17
+
18
+ This is the model card of a 🤗 transformers model that has been pushed on the Hub. This model card has been automatically generated.
19
+
20
+ - **Developed by:** [More Information Needed]
21
+ - **Funded by [optional]:** [More Information Needed]
22
+ - **Shared by [optional]:** [More Information Needed]
23
+ - **Model type:** [More Information Needed]
24
+ - **Language(s) (NLP):** [More Information Needed]
25
+ - **License:** [More Information Needed]
26
+ - **Finetuned from model [optional]:** [More Information Needed]
27
+
28
+ ### Model Sources [optional]
29
+
30
+ <!-- Provide the basic links for the model. -->
31
+
32
+ - **Repository:** [More Information Needed]
33
+ - **Paper [optional]:** [More Information Needed]
34
+ - **Demo [optional]:** [More Information Needed]
35
+
36
+ ## Uses
37
+
38
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
39
+
40
+ ### Direct Use
41
+
42
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
43
+
44
+ [More Information Needed]
45
+
46
+ ### Downstream Use [optional]
47
+
48
+ <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
49
+
50
+ [More Information Needed]
51
+
52
+ ### Out-of-Scope Use
53
+
54
+ <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
55
+
56
+ [More Information Needed]
57
+
58
+ ## Bias, Risks, and Limitations
59
+
60
+ <!-- This section is meant to convey both technical and sociotechnical limitations. -->
61
+
62
+ [More Information Needed]
63
+
64
+ ### Recommendations
65
+
66
+ <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
67
+
68
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
69
+
70
+ ## How to Get Started with the Model
71
+
72
+ Use the code below to get started with the model.
73
+
74
+ [More Information Needed]
75
+
76
+ ## Training Details
77
+
78
+ ### Training Data
79
+
80
+ <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
81
+
82
+ [More Information Needed]
83
+
84
+ ### Training Procedure
85
+
86
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
87
+
88
+ #### Preprocessing [optional]
89
+
90
+ [More Information Needed]
91
+
92
+
93
+ #### Training Hyperparameters
94
+
95
+ - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
96
+
97
+ #### Speeds, Sizes, Times [optional]
98
+
99
+ <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
100
+
101
+ [More Information Needed]
102
+
103
+ ## Evaluation
104
+
105
+ <!-- This section describes the evaluation protocols and provides the results. -->
106
+
107
+ ### Testing Data, Factors & Metrics
108
+
109
+ #### Testing Data
110
+
111
+ <!-- This should link to a Dataset Card if possible. -->
112
+
113
+ [More Information Needed]
114
+
115
+ #### Factors
116
+
117
+ <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
118
+
119
+ [More Information Needed]
120
+
121
+ #### Metrics
122
+
123
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
124
+
125
+ [More Information Needed]
126
+
127
+ ### Results
128
+
129
+ [More Information Needed]
130
+
131
+ #### Summary
132
+
133
+
134
+
135
+ ## Model Examination [optional]
136
+
137
+ <!-- Relevant interpretability work for the model goes here -->
138
+
139
+ [More Information Needed]
140
+
141
+ ## Environmental Impact
142
+
143
+ <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
144
+
145
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
146
+
147
+ - **Hardware Type:** [More Information Needed]
148
+ - **Hours used:** [More Information Needed]
149
+ - **Cloud Provider:** [More Information Needed]
150
+ - **Compute Region:** [More Information Needed]
151
+ - **Carbon Emitted:** [More Information Needed]
152
+
153
+ ## Technical Specifications [optional]
154
+
155
+ ### Model Architecture and Objective
156
+
157
+ [More Information Needed]
158
+
159
+ ### Compute Infrastructure
160
+
161
+ [More Information Needed]
162
+
163
+ #### Hardware
164
+
165
+ [More Information Needed]
166
+
167
+ #### Software
168
+
169
+ [More Information Needed]
170
+
171
+ ## Citation [optional]
172
+
173
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
174
+
175
+ **BibTeX:**
176
+
177
+ [More Information Needed]
178
+
179
+ **APA:**
180
+
181
+ [More Information Needed]
182
+
183
+ ## Glossary [optional]
184
+
185
+ <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
186
+
187
+ [More Information Needed]
188
+
189
+ ## More Information [optional]
190
+
191
+ [More Information Needed]
192
+
193
+ ## Model Card Authors [optional]
194
+
195
+ [More Information Needed]
196
+
197
+ ## Model Card Contact
198
+
199
+ [More Information Needed]
200
+
201
+
config.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/kaggle/working/merge",
3
+ "architectures": [
4
+ "PhiForCausalLM"
5
+ ],
6
+ "attention_dropout": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_phi.PhiConfig",
9
+ "AutoModelForCausalLM": "modeling_phi.PhiForCausalLM"
10
+ },
11
+ "bos_token_id": 50256,
12
+ "embd_pdrop": 0.0,
13
+ "eos_token_id": 50256,
14
+ "hidden_act": "gelu_new",
15
+ "hidden_size": 2560,
16
+ "initializer_range": 0.02,
17
+ "intermediate_size": 10240,
18
+ "layer_norm_eps": 1e-05,
19
+ "max_position_embeddings": 2048,
20
+ "model_type": "phi",
21
+ "num_attention_heads": 32,
22
+ "num_experts_per_tok": 2,
23
+ "num_hidden_layers": 32,
24
+ "num_key_value_heads": 32,
25
+ "num_local_experts": 2,
26
+ "partial_rotary_factor": 0.4,
27
+ "qk_layernorm": false,
28
+ "resid_pdrop": 0.1,
29
+ "rope_scaling": null,
30
+ "rope_theta": 10000.0,
31
+ "sliding_window": null,
32
+ "tie_word_embeddings": false,
33
+ "torch_dtype": "float16",
34
+ "transformers_version": "4.37.0",
35
+ "use_cache": true,
36
+ "vocab_size": 51200
37
+ }
configuration_phi.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # coding=utf-8
3
+ # Copyright 2023 Microsoft and the HuggingFace Inc. team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ #Phi model configuration
18
+
19
+
20
+ from transformers.configuration_utils import PretrainedConfig
21
+ from transformers.utils import logging
22
+
23
+
24
+ logger = logging.get_logger(__name__)
25
+
26
+ PHI_PRETRAINED_CONFIG_ARCHIVE_MAP = {
27
+ "microsoft/phi-2": "https://huggingface.co/microsoft/phi-2/resolve/main/config.json",
28
+ }
29
+
30
+
31
+ class PhiConfig(PretrainedConfig):
32
+
33
+
34
+ model_type = "phi"
35
+ keys_to_ignore_at_inference = ["past_key_values"]
36
+
37
+ def __init__(
38
+ self,
39
+ vocab_size=51200,
40
+ hidden_size=2048,
41
+ intermediate_size=8192,
42
+ num_hidden_layers=24,
43
+ num_experts_per_tok=2, ###ADDED
44
+ num_attention_heads=32,
45
+ num_key_value_heads=None,
46
+ num_local_experts=2, ###ADDED
47
+ resid_pdrop=0.0,
48
+ embd_pdrop=0.0,
49
+ attention_dropout=0.0,
50
+ hidden_act="gelu_new",
51
+ max_position_embeddings=2048,
52
+ initializer_range=0.02,
53
+ layer_norm_eps=1e-5,
54
+ use_cache=True,
55
+ tie_word_embeddings=False,
56
+ rope_theta=10000.0,
57
+ rope_scaling=None,
58
+ partial_rotary_factor=0.5,
59
+ qk_layernorm=False,
60
+ bos_token_id=1,
61
+ eos_token_id=2,
62
+ **kwargs,
63
+ ):
64
+ self.vocab_size = vocab_size
65
+ self.hidden_size = hidden_size
66
+ self.intermediate_size = intermediate_size
67
+ self.num_hidden_layers = num_hidden_layers
68
+ self.num_experts_per_tok = num_experts_per_tok ###ADDED
69
+ self.num_attention_heads = num_attention_heads
70
+
71
+ if num_key_value_heads is None:
72
+ num_key_value_heads = num_attention_heads
73
+
74
+ self.num_key_value_heads = num_key_value_heads
75
+ self.num_local_experts = num_local_experts ###ADDED
76
+ self.resid_pdrop = resid_pdrop
77
+ self.embd_pdrop = embd_pdrop
78
+ self.attention_dropout = attention_dropout
79
+ self.hidden_act = hidden_act
80
+ self.max_position_embeddings = max_position_embeddings
81
+ self.initializer_range = initializer_range
82
+ self.layer_norm_eps = layer_norm_eps
83
+ self.use_cache = use_cache
84
+ self.rope_theta = rope_theta
85
+ self.rope_scaling = rope_scaling
86
+ self.partial_rotary_factor = partial_rotary_factor
87
+ self.qk_layernorm = qk_layernorm
88
+ self._rope_scaling_validation()
89
+
90
+ super().__init__(
91
+ bos_token_id=bos_token_id,
92
+ eos_token_id=eos_token_id,
93
+ tie_word_embeddings=tie_word_embeddings,
94
+ **kwargs,
95
+ )
96
+
97
+ # Copied from transformers.models.llama.configuration_llama.LlamaConfig._rope_scaling_validation
98
+ def _rope_scaling_validation(self):
99
+ #Validate the `rope_scaling` configuration.
100
+
101
+ if self.rope_scaling is None:
102
+ return
103
+
104
+ if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 2:
105
+ raise ValueError(
106
+ "`rope_scaling` must be a dictionary with with two fields, `type` and `factor`, "
107
+ f"got {self.rope_scaling}"
108
+ )
109
+ rope_scaling_type = self.rope_scaling.get("type", None)
110
+ rope_scaling_factor = self.rope_scaling.get("factor", None)
111
+ if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
112
+ raise ValueError(
113
+ f"`rope_scaling`'s type field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"
114
+ )
115
+ if rope_scaling_factor is None or not isinstance(rope_scaling_factor, float) or rope_scaling_factor <= 1.0:
116
+ raise ValueError(f"`rope_scaling`'s factor field must be a float > 1, got {rope_scaling_factor}")
117
+
generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 50256,
4
+ "eos_token_id": 50256,
5
+ "transformers_version": "4.37.0"
6
+ }
model-00001-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d6a829066de5089f033296faad2be5526c2d7643836ae8e680a6f70ca02bcdfc
3
+ size 4995545472
model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5b6967147c850ffa71821b1129b7bd03991004bff62481ec13db309c32dafd23
3
+ size 3920480344
model.safetensors.index.json ADDED
@@ -0,0 +1,620 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 8915957760
4
+ },
5
+ "weight_map": {
6
+ "lm_head.bias": "model-00002-of-00002.safetensors",
7
+ "lm_head.weight": "model-00002-of-00002.safetensors",
8
+ "model.embed_tokens.weight": "model-00001-of-00002.safetensors",
9
+ "model.final_layernorm.bias": "model-00002-of-00002.safetensors",
10
+ "model.final_layernorm.weight": "model-00002-of-00002.safetensors",
11
+ "model.layers.0.input_layernorm.bias": "model-00001-of-00002.safetensors",
12
+ "model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors",
13
+ "model.layers.0.moe.gate.weight": "model-00001-of-00002.safetensors",
14
+ "model.layers.0.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
15
+ "model.layers.0.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
16
+ "model.layers.0.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
17
+ "model.layers.0.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
18
+ "model.layers.0.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
19
+ "model.layers.0.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
20
+ "model.layers.0.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
21
+ "model.layers.0.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
22
+ "model.layers.0.self_attn.dense.bias": "model-00001-of-00002.safetensors",
23
+ "model.layers.0.self_attn.dense.weight": "model-00001-of-00002.safetensors",
24
+ "model.layers.0.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
25
+ "model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
26
+ "model.layers.0.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
27
+ "model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
28
+ "model.layers.0.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
29
+ "model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
30
+ "model.layers.1.input_layernorm.bias": "model-00001-of-00002.safetensors",
31
+ "model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors",
32
+ "model.layers.1.moe.gate.weight": "model-00001-of-00002.safetensors",
33
+ "model.layers.1.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
34
+ "model.layers.1.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
35
+ "model.layers.1.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
36
+ "model.layers.1.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
37
+ "model.layers.1.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
38
+ "model.layers.1.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
39
+ "model.layers.1.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
40
+ "model.layers.1.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
41
+ "model.layers.1.self_attn.dense.bias": "model-00001-of-00002.safetensors",
42
+ "model.layers.1.self_attn.dense.weight": "model-00001-of-00002.safetensors",
43
+ "model.layers.1.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
44
+ "model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
45
+ "model.layers.1.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
46
+ "model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
47
+ "model.layers.1.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
48
+ "model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
49
+ "model.layers.10.input_layernorm.bias": "model-00001-of-00002.safetensors",
50
+ "model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors",
51
+ "model.layers.10.moe.gate.weight": "model-00001-of-00002.safetensors",
52
+ "model.layers.10.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
53
+ "model.layers.10.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
54
+ "model.layers.10.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
55
+ "model.layers.10.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
56
+ "model.layers.10.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
57
+ "model.layers.10.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
58
+ "model.layers.10.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
59
+ "model.layers.10.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
60
+ "model.layers.10.self_attn.dense.bias": "model-00001-of-00002.safetensors",
61
+ "model.layers.10.self_attn.dense.weight": "model-00001-of-00002.safetensors",
62
+ "model.layers.10.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
63
+ "model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
64
+ "model.layers.10.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
65
+ "model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
66
+ "model.layers.10.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
67
+ "model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
68
+ "model.layers.11.input_layernorm.bias": "model-00001-of-00002.safetensors",
69
+ "model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors",
70
+ "model.layers.11.moe.gate.weight": "model-00001-of-00002.safetensors",
71
+ "model.layers.11.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
72
+ "model.layers.11.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
73
+ "model.layers.11.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
74
+ "model.layers.11.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
75
+ "model.layers.11.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
76
+ "model.layers.11.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
77
+ "model.layers.11.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
78
+ "model.layers.11.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
79
+ "model.layers.11.self_attn.dense.bias": "model-00001-of-00002.safetensors",
80
+ "model.layers.11.self_attn.dense.weight": "model-00001-of-00002.safetensors",
81
+ "model.layers.11.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
82
+ "model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
83
+ "model.layers.11.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
84
+ "model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
85
+ "model.layers.11.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
86
+ "model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
87
+ "model.layers.12.input_layernorm.bias": "model-00001-of-00002.safetensors",
88
+ "model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors",
89
+ "model.layers.12.moe.gate.weight": "model-00001-of-00002.safetensors",
90
+ "model.layers.12.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
91
+ "model.layers.12.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
92
+ "model.layers.12.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
93
+ "model.layers.12.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
94
+ "model.layers.12.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
95
+ "model.layers.12.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
96
+ "model.layers.12.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
97
+ "model.layers.12.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
98
+ "model.layers.12.self_attn.dense.bias": "model-00001-of-00002.safetensors",
99
+ "model.layers.12.self_attn.dense.weight": "model-00001-of-00002.safetensors",
100
+ "model.layers.12.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
101
+ "model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
102
+ "model.layers.12.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
103
+ "model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
104
+ "model.layers.12.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
105
+ "model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
106
+ "model.layers.13.input_layernorm.bias": "model-00001-of-00002.safetensors",
107
+ "model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors",
108
+ "model.layers.13.moe.gate.weight": "model-00001-of-00002.safetensors",
109
+ "model.layers.13.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
110
+ "model.layers.13.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
111
+ "model.layers.13.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
112
+ "model.layers.13.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
113
+ "model.layers.13.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
114
+ "model.layers.13.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
115
+ "model.layers.13.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
116
+ "model.layers.13.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
117
+ "model.layers.13.self_attn.dense.bias": "model-00001-of-00002.safetensors",
118
+ "model.layers.13.self_attn.dense.weight": "model-00001-of-00002.safetensors",
119
+ "model.layers.13.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
120
+ "model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
121
+ "model.layers.13.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
122
+ "model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
123
+ "model.layers.13.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
124
+ "model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
125
+ "model.layers.14.input_layernorm.bias": "model-00001-of-00002.safetensors",
126
+ "model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors",
127
+ "model.layers.14.moe.gate.weight": "model-00001-of-00002.safetensors",
128
+ "model.layers.14.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
129
+ "model.layers.14.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
130
+ "model.layers.14.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
131
+ "model.layers.14.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
132
+ "model.layers.14.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
133
+ "model.layers.14.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
134
+ "model.layers.14.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
135
+ "model.layers.14.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
136
+ "model.layers.14.self_attn.dense.bias": "model-00001-of-00002.safetensors",
137
+ "model.layers.14.self_attn.dense.weight": "model-00001-of-00002.safetensors",
138
+ "model.layers.14.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
139
+ "model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
140
+ "model.layers.14.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
141
+ "model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
142
+ "model.layers.14.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
143
+ "model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
144
+ "model.layers.15.input_layernorm.bias": "model-00001-of-00002.safetensors",
145
+ "model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors",
146
+ "model.layers.15.moe.gate.weight": "model-00001-of-00002.safetensors",
147
+ "model.layers.15.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
148
+ "model.layers.15.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
149
+ "model.layers.15.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
150
+ "model.layers.15.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
151
+ "model.layers.15.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
152
+ "model.layers.15.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
153
+ "model.layers.15.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
154
+ "model.layers.15.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
155
+ "model.layers.15.self_attn.dense.bias": "model-00001-of-00002.safetensors",
156
+ "model.layers.15.self_attn.dense.weight": "model-00001-of-00002.safetensors",
157
+ "model.layers.15.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
158
+ "model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
159
+ "model.layers.15.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
160
+ "model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
161
+ "model.layers.15.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
162
+ "model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
163
+ "model.layers.16.input_layernorm.bias": "model-00001-of-00002.safetensors",
164
+ "model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors",
165
+ "model.layers.16.moe.gate.weight": "model-00001-of-00002.safetensors",
166
+ "model.layers.16.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
167
+ "model.layers.16.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
168
+ "model.layers.16.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
169
+ "model.layers.16.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
170
+ "model.layers.16.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
171
+ "model.layers.16.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
172
+ "model.layers.16.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
173
+ "model.layers.16.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
174
+ "model.layers.16.self_attn.dense.bias": "model-00001-of-00002.safetensors",
175
+ "model.layers.16.self_attn.dense.weight": "model-00001-of-00002.safetensors",
176
+ "model.layers.16.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
177
+ "model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
178
+ "model.layers.16.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
179
+ "model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
180
+ "model.layers.16.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
181
+ "model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
182
+ "model.layers.17.input_layernorm.bias": "model-00001-of-00002.safetensors",
183
+ "model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors",
184
+ "model.layers.17.moe.gate.weight": "model-00001-of-00002.safetensors",
185
+ "model.layers.17.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
186
+ "model.layers.17.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
187
+ "model.layers.17.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
188
+ "model.layers.17.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
189
+ "model.layers.17.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
190
+ "model.layers.17.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
191
+ "model.layers.17.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
192
+ "model.layers.17.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
193
+ "model.layers.17.self_attn.dense.bias": "model-00001-of-00002.safetensors",
194
+ "model.layers.17.self_attn.dense.weight": "model-00001-of-00002.safetensors",
195
+ "model.layers.17.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
196
+ "model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
197
+ "model.layers.17.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
198
+ "model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
199
+ "model.layers.17.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
200
+ "model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
201
+ "model.layers.18.input_layernorm.bias": "model-00002-of-00002.safetensors",
202
+ "model.layers.18.input_layernorm.weight": "model-00002-of-00002.safetensors",
203
+ "model.layers.18.moe.gate.weight": "model-00002-of-00002.safetensors",
204
+ "model.layers.18.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
205
+ "model.layers.18.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
206
+ "model.layers.18.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
207
+ "model.layers.18.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
208
+ "model.layers.18.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
209
+ "model.layers.18.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
210
+ "model.layers.18.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
211
+ "model.layers.18.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
212
+ "model.layers.18.self_attn.dense.bias": "model-00002-of-00002.safetensors",
213
+ "model.layers.18.self_attn.dense.weight": "model-00002-of-00002.safetensors",
214
+ "model.layers.18.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
215
+ "model.layers.18.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
216
+ "model.layers.18.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
217
+ "model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
218
+ "model.layers.18.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
219
+ "model.layers.18.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
220
+ "model.layers.19.input_layernorm.bias": "model-00002-of-00002.safetensors",
221
+ "model.layers.19.input_layernorm.weight": "model-00002-of-00002.safetensors",
222
+ "model.layers.19.moe.gate.weight": "model-00002-of-00002.safetensors",
223
+ "model.layers.19.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
224
+ "model.layers.19.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
225
+ "model.layers.19.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
226
+ "model.layers.19.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
227
+ "model.layers.19.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
228
+ "model.layers.19.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
229
+ "model.layers.19.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
230
+ "model.layers.19.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
231
+ "model.layers.19.self_attn.dense.bias": "model-00002-of-00002.safetensors",
232
+ "model.layers.19.self_attn.dense.weight": "model-00002-of-00002.safetensors",
233
+ "model.layers.19.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
234
+ "model.layers.19.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
235
+ "model.layers.19.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
236
+ "model.layers.19.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
237
+ "model.layers.19.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
238
+ "model.layers.19.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
239
+ "model.layers.2.input_layernorm.bias": "model-00001-of-00002.safetensors",
240
+ "model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors",
241
+ "model.layers.2.moe.gate.weight": "model-00001-of-00002.safetensors",
242
+ "model.layers.2.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
243
+ "model.layers.2.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
244
+ "model.layers.2.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
245
+ "model.layers.2.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
246
+ "model.layers.2.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
247
+ "model.layers.2.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
248
+ "model.layers.2.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
249
+ "model.layers.2.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
250
+ "model.layers.2.self_attn.dense.bias": "model-00001-of-00002.safetensors",
251
+ "model.layers.2.self_attn.dense.weight": "model-00001-of-00002.safetensors",
252
+ "model.layers.2.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
253
+ "model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
254
+ "model.layers.2.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
255
+ "model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
256
+ "model.layers.2.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
257
+ "model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
258
+ "model.layers.20.input_layernorm.bias": "model-00002-of-00002.safetensors",
259
+ "model.layers.20.input_layernorm.weight": "model-00002-of-00002.safetensors",
260
+ "model.layers.20.moe.gate.weight": "model-00002-of-00002.safetensors",
261
+ "model.layers.20.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
262
+ "model.layers.20.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
263
+ "model.layers.20.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
264
+ "model.layers.20.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
265
+ "model.layers.20.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
266
+ "model.layers.20.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
267
+ "model.layers.20.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
268
+ "model.layers.20.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
269
+ "model.layers.20.self_attn.dense.bias": "model-00002-of-00002.safetensors",
270
+ "model.layers.20.self_attn.dense.weight": "model-00002-of-00002.safetensors",
271
+ "model.layers.20.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
272
+ "model.layers.20.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
273
+ "model.layers.20.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
274
+ "model.layers.20.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
275
+ "model.layers.20.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
276
+ "model.layers.20.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
277
+ "model.layers.21.input_layernorm.bias": "model-00002-of-00002.safetensors",
278
+ "model.layers.21.input_layernorm.weight": "model-00002-of-00002.safetensors",
279
+ "model.layers.21.moe.gate.weight": "model-00002-of-00002.safetensors",
280
+ "model.layers.21.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
281
+ "model.layers.21.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
282
+ "model.layers.21.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
283
+ "model.layers.21.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
284
+ "model.layers.21.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
285
+ "model.layers.21.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
286
+ "model.layers.21.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
287
+ "model.layers.21.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
288
+ "model.layers.21.self_attn.dense.bias": "model-00002-of-00002.safetensors",
289
+ "model.layers.21.self_attn.dense.weight": "model-00002-of-00002.safetensors",
290
+ "model.layers.21.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
291
+ "model.layers.21.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
292
+ "model.layers.21.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
293
+ "model.layers.21.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
294
+ "model.layers.21.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
295
+ "model.layers.21.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
296
+ "model.layers.22.input_layernorm.bias": "model-00002-of-00002.safetensors",
297
+ "model.layers.22.input_layernorm.weight": "model-00002-of-00002.safetensors",
298
+ "model.layers.22.moe.gate.weight": "model-00002-of-00002.safetensors",
299
+ "model.layers.22.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
300
+ "model.layers.22.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
301
+ "model.layers.22.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
302
+ "model.layers.22.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
303
+ "model.layers.22.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
304
+ "model.layers.22.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
305
+ "model.layers.22.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
306
+ "model.layers.22.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
307
+ "model.layers.22.self_attn.dense.bias": "model-00002-of-00002.safetensors",
308
+ "model.layers.22.self_attn.dense.weight": "model-00002-of-00002.safetensors",
309
+ "model.layers.22.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
310
+ "model.layers.22.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
311
+ "model.layers.22.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
312
+ "model.layers.22.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
313
+ "model.layers.22.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
314
+ "model.layers.22.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
315
+ "model.layers.23.input_layernorm.bias": "model-00002-of-00002.safetensors",
316
+ "model.layers.23.input_layernorm.weight": "model-00002-of-00002.safetensors",
317
+ "model.layers.23.moe.gate.weight": "model-00002-of-00002.safetensors",
318
+ "model.layers.23.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
319
+ "model.layers.23.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
320
+ "model.layers.23.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
321
+ "model.layers.23.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
322
+ "model.layers.23.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
323
+ "model.layers.23.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
324
+ "model.layers.23.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
325
+ "model.layers.23.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
326
+ "model.layers.23.self_attn.dense.bias": "model-00002-of-00002.safetensors",
327
+ "model.layers.23.self_attn.dense.weight": "model-00002-of-00002.safetensors",
328
+ "model.layers.23.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
329
+ "model.layers.23.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
330
+ "model.layers.23.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
331
+ "model.layers.23.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
332
+ "model.layers.23.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
333
+ "model.layers.23.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
334
+ "model.layers.24.input_layernorm.bias": "model-00002-of-00002.safetensors",
335
+ "model.layers.24.input_layernorm.weight": "model-00002-of-00002.safetensors",
336
+ "model.layers.24.moe.gate.weight": "model-00002-of-00002.safetensors",
337
+ "model.layers.24.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
338
+ "model.layers.24.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
339
+ "model.layers.24.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
340
+ "model.layers.24.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
341
+ "model.layers.24.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
342
+ "model.layers.24.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
343
+ "model.layers.24.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
344
+ "model.layers.24.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
345
+ "model.layers.24.self_attn.dense.bias": "model-00002-of-00002.safetensors",
346
+ "model.layers.24.self_attn.dense.weight": "model-00002-of-00002.safetensors",
347
+ "model.layers.24.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
348
+ "model.layers.24.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
349
+ "model.layers.24.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
350
+ "model.layers.24.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
351
+ "model.layers.24.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
352
+ "model.layers.24.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
353
+ "model.layers.25.input_layernorm.bias": "model-00002-of-00002.safetensors",
354
+ "model.layers.25.input_layernorm.weight": "model-00002-of-00002.safetensors",
355
+ "model.layers.25.moe.gate.weight": "model-00002-of-00002.safetensors",
356
+ "model.layers.25.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
357
+ "model.layers.25.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
358
+ "model.layers.25.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
359
+ "model.layers.25.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
360
+ "model.layers.25.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
361
+ "model.layers.25.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
362
+ "model.layers.25.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
363
+ "model.layers.25.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
364
+ "model.layers.25.self_attn.dense.bias": "model-00002-of-00002.safetensors",
365
+ "model.layers.25.self_attn.dense.weight": "model-00002-of-00002.safetensors",
366
+ "model.layers.25.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
367
+ "model.layers.25.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
368
+ "model.layers.25.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
369
+ "model.layers.25.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
370
+ "model.layers.25.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
371
+ "model.layers.25.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
372
+ "model.layers.26.input_layernorm.bias": "model-00002-of-00002.safetensors",
373
+ "model.layers.26.input_layernorm.weight": "model-00002-of-00002.safetensors",
374
+ "model.layers.26.moe.gate.weight": "model-00002-of-00002.safetensors",
375
+ "model.layers.26.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
376
+ "model.layers.26.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
377
+ "model.layers.26.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
378
+ "model.layers.26.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
379
+ "model.layers.26.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
380
+ "model.layers.26.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
381
+ "model.layers.26.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
382
+ "model.layers.26.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
383
+ "model.layers.26.self_attn.dense.bias": "model-00002-of-00002.safetensors",
384
+ "model.layers.26.self_attn.dense.weight": "model-00002-of-00002.safetensors",
385
+ "model.layers.26.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
386
+ "model.layers.26.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
387
+ "model.layers.26.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
388
+ "model.layers.26.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
389
+ "model.layers.26.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
390
+ "model.layers.26.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
391
+ "model.layers.27.input_layernorm.bias": "model-00002-of-00002.safetensors",
392
+ "model.layers.27.input_layernorm.weight": "model-00002-of-00002.safetensors",
393
+ "model.layers.27.moe.gate.weight": "model-00002-of-00002.safetensors",
394
+ "model.layers.27.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
395
+ "model.layers.27.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
396
+ "model.layers.27.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
397
+ "model.layers.27.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
398
+ "model.layers.27.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
399
+ "model.layers.27.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
400
+ "model.layers.27.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
401
+ "model.layers.27.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
402
+ "model.layers.27.self_attn.dense.bias": "model-00002-of-00002.safetensors",
403
+ "model.layers.27.self_attn.dense.weight": "model-00002-of-00002.safetensors",
404
+ "model.layers.27.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
405
+ "model.layers.27.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
406
+ "model.layers.27.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
407
+ "model.layers.27.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
408
+ "model.layers.27.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
409
+ "model.layers.27.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
410
+ "model.layers.28.input_layernorm.bias": "model-00002-of-00002.safetensors",
411
+ "model.layers.28.input_layernorm.weight": "model-00002-of-00002.safetensors",
412
+ "model.layers.28.moe.gate.weight": "model-00002-of-00002.safetensors",
413
+ "model.layers.28.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
414
+ "model.layers.28.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
415
+ "model.layers.28.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
416
+ "model.layers.28.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
417
+ "model.layers.28.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
418
+ "model.layers.28.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
419
+ "model.layers.28.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
420
+ "model.layers.28.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
421
+ "model.layers.28.self_attn.dense.bias": "model-00002-of-00002.safetensors",
422
+ "model.layers.28.self_attn.dense.weight": "model-00002-of-00002.safetensors",
423
+ "model.layers.28.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
424
+ "model.layers.28.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
425
+ "model.layers.28.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
426
+ "model.layers.28.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
427
+ "model.layers.28.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
428
+ "model.layers.28.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
429
+ "model.layers.29.input_layernorm.bias": "model-00002-of-00002.safetensors",
430
+ "model.layers.29.input_layernorm.weight": "model-00002-of-00002.safetensors",
431
+ "model.layers.29.moe.gate.weight": "model-00002-of-00002.safetensors",
432
+ "model.layers.29.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
433
+ "model.layers.29.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
434
+ "model.layers.29.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
435
+ "model.layers.29.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
436
+ "model.layers.29.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
437
+ "model.layers.29.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
438
+ "model.layers.29.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
439
+ "model.layers.29.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
440
+ "model.layers.29.self_attn.dense.bias": "model-00002-of-00002.safetensors",
441
+ "model.layers.29.self_attn.dense.weight": "model-00002-of-00002.safetensors",
442
+ "model.layers.29.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
443
+ "model.layers.29.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
444
+ "model.layers.29.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
445
+ "model.layers.29.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
446
+ "model.layers.29.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
447
+ "model.layers.29.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
448
+ "model.layers.3.input_layernorm.bias": "model-00001-of-00002.safetensors",
449
+ "model.layers.3.input_layernorm.weight": "model-00001-of-00002.safetensors",
450
+ "model.layers.3.moe.gate.weight": "model-00001-of-00002.safetensors",
451
+ "model.layers.3.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
452
+ "model.layers.3.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
453
+ "model.layers.3.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
454
+ "model.layers.3.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
455
+ "model.layers.3.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
456
+ "model.layers.3.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
457
+ "model.layers.3.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
458
+ "model.layers.3.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
459
+ "model.layers.3.self_attn.dense.bias": "model-00001-of-00002.safetensors",
460
+ "model.layers.3.self_attn.dense.weight": "model-00001-of-00002.safetensors",
461
+ "model.layers.3.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
462
+ "model.layers.3.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
463
+ "model.layers.3.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
464
+ "model.layers.3.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
465
+ "model.layers.3.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
466
+ "model.layers.3.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
467
+ "model.layers.30.input_layernorm.bias": "model-00002-of-00002.safetensors",
468
+ "model.layers.30.input_layernorm.weight": "model-00002-of-00002.safetensors",
469
+ "model.layers.30.moe.gate.weight": "model-00002-of-00002.safetensors",
470
+ "model.layers.30.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
471
+ "model.layers.30.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
472
+ "model.layers.30.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
473
+ "model.layers.30.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
474
+ "model.layers.30.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
475
+ "model.layers.30.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
476
+ "model.layers.30.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
477
+ "model.layers.30.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
478
+ "model.layers.30.self_attn.dense.bias": "model-00002-of-00002.safetensors",
479
+ "model.layers.30.self_attn.dense.weight": "model-00002-of-00002.safetensors",
480
+ "model.layers.30.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
481
+ "model.layers.30.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
482
+ "model.layers.30.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
483
+ "model.layers.30.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
484
+ "model.layers.30.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
485
+ "model.layers.30.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
486
+ "model.layers.31.input_layernorm.bias": "model-00002-of-00002.safetensors",
487
+ "model.layers.31.input_layernorm.weight": "model-00002-of-00002.safetensors",
488
+ "model.layers.31.moe.gate.weight": "model-00002-of-00002.safetensors",
489
+ "model.layers.31.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
490
+ "model.layers.31.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
491
+ "model.layers.31.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
492
+ "model.layers.31.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
493
+ "model.layers.31.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
494
+ "model.layers.31.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
495
+ "model.layers.31.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
496
+ "model.layers.31.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
497
+ "model.layers.31.self_attn.dense.bias": "model-00002-of-00002.safetensors",
498
+ "model.layers.31.self_attn.dense.weight": "model-00002-of-00002.safetensors",
499
+ "model.layers.31.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
500
+ "model.layers.31.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
501
+ "model.layers.31.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
502
+ "model.layers.31.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
503
+ "model.layers.31.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
504
+ "model.layers.31.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
505
+ "model.layers.4.input_layernorm.bias": "model-00001-of-00002.safetensors",
506
+ "model.layers.4.input_layernorm.weight": "model-00001-of-00002.safetensors",
507
+ "model.layers.4.moe.gate.weight": "model-00001-of-00002.safetensors",
508
+ "model.layers.4.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
509
+ "model.layers.4.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
510
+ "model.layers.4.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
511
+ "model.layers.4.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
512
+ "model.layers.4.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
513
+ "model.layers.4.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
514
+ "model.layers.4.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
515
+ "model.layers.4.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
516
+ "model.layers.4.self_attn.dense.bias": "model-00001-of-00002.safetensors",
517
+ "model.layers.4.self_attn.dense.weight": "model-00001-of-00002.safetensors",
518
+ "model.layers.4.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
519
+ "model.layers.4.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
520
+ "model.layers.4.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
521
+ "model.layers.4.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
522
+ "model.layers.4.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
523
+ "model.layers.4.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
524
+ "model.layers.5.input_layernorm.bias": "model-00001-of-00002.safetensors",
525
+ "model.layers.5.input_layernorm.weight": "model-00001-of-00002.safetensors",
526
+ "model.layers.5.moe.gate.weight": "model-00001-of-00002.safetensors",
527
+ "model.layers.5.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
528
+ "model.layers.5.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
529
+ "model.layers.5.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
530
+ "model.layers.5.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
531
+ "model.layers.5.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
532
+ "model.layers.5.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
533
+ "model.layers.5.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
534
+ "model.layers.5.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
535
+ "model.layers.5.self_attn.dense.bias": "model-00001-of-00002.safetensors",
536
+ "model.layers.5.self_attn.dense.weight": "model-00001-of-00002.safetensors",
537
+ "model.layers.5.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
538
+ "model.layers.5.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
539
+ "model.layers.5.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
540
+ "model.layers.5.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
541
+ "model.layers.5.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
542
+ "model.layers.5.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
543
+ "model.layers.6.input_layernorm.bias": "model-00001-of-00002.safetensors",
544
+ "model.layers.6.input_layernorm.weight": "model-00001-of-00002.safetensors",
545
+ "model.layers.6.moe.gate.weight": "model-00001-of-00002.safetensors",
546
+ "model.layers.6.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
547
+ "model.layers.6.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
548
+ "model.layers.6.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
549
+ "model.layers.6.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
550
+ "model.layers.6.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
551
+ "model.layers.6.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
552
+ "model.layers.6.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
553
+ "model.layers.6.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
554
+ "model.layers.6.self_attn.dense.bias": "model-00001-of-00002.safetensors",
555
+ "model.layers.6.self_attn.dense.weight": "model-00001-of-00002.safetensors",
556
+ "model.layers.6.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
557
+ "model.layers.6.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
558
+ "model.layers.6.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
559
+ "model.layers.6.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
560
+ "model.layers.6.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
561
+ "model.layers.6.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
562
+ "model.layers.7.input_layernorm.bias": "model-00001-of-00002.safetensors",
563
+ "model.layers.7.input_layernorm.weight": "model-00001-of-00002.safetensors",
564
+ "model.layers.7.moe.gate.weight": "model-00001-of-00002.safetensors",
565
+ "model.layers.7.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
566
+ "model.layers.7.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
567
+ "model.layers.7.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
568
+ "model.layers.7.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
569
+ "model.layers.7.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
570
+ "model.layers.7.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
571
+ "model.layers.7.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
572
+ "model.layers.7.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
573
+ "model.layers.7.self_attn.dense.bias": "model-00001-of-00002.safetensors",
574
+ "model.layers.7.self_attn.dense.weight": "model-00001-of-00002.safetensors",
575
+ "model.layers.7.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
576
+ "model.layers.7.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
577
+ "model.layers.7.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
578
+ "model.layers.7.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
579
+ "model.layers.7.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
580
+ "model.layers.7.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
581
+ "model.layers.8.input_layernorm.bias": "model-00001-of-00002.safetensors",
582
+ "model.layers.8.input_layernorm.weight": "model-00001-of-00002.safetensors",
583
+ "model.layers.8.moe.gate.weight": "model-00001-of-00002.safetensors",
584
+ "model.layers.8.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
585
+ "model.layers.8.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
586
+ "model.layers.8.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
587
+ "model.layers.8.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
588
+ "model.layers.8.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
589
+ "model.layers.8.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
590
+ "model.layers.8.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
591
+ "model.layers.8.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
592
+ "model.layers.8.self_attn.dense.bias": "model-00001-of-00002.safetensors",
593
+ "model.layers.8.self_attn.dense.weight": "model-00001-of-00002.safetensors",
594
+ "model.layers.8.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
595
+ "model.layers.8.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
596
+ "model.layers.8.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
597
+ "model.layers.8.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
598
+ "model.layers.8.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
599
+ "model.layers.8.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
600
+ "model.layers.9.input_layernorm.bias": "model-00001-of-00002.safetensors",
601
+ "model.layers.9.input_layernorm.weight": "model-00001-of-00002.safetensors",
602
+ "model.layers.9.moe.gate.weight": "model-00001-of-00002.safetensors",
603
+ "model.layers.9.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
604
+ "model.layers.9.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
605
+ "model.layers.9.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
606
+ "model.layers.9.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
607
+ "model.layers.9.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
608
+ "model.layers.9.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
609
+ "model.layers.9.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
610
+ "model.layers.9.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
611
+ "model.layers.9.self_attn.dense.bias": "model-00001-of-00002.safetensors",
612
+ "model.layers.9.self_attn.dense.weight": "model-00001-of-00002.safetensors",
613
+ "model.layers.9.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
614
+ "model.layers.9.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
615
+ "model.layers.9.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
616
+ "model.layers.9.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
617
+ "model.layers.9.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
618
+ "model.layers.9.self_attn.v_proj.weight": "model-00001-of-00002.safetensors"
619
+ }
620
+ }
modeling_phi.py ADDED
@@ -0,0 +1,1380 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # coding=utf-8
3
+ # Copyright 2023 Microsoft and the HuggingFace Inc. team. All rights reserved.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ """ PyTorch Phi model."""
18
+
19
+
20
+ import math
21
+ from typing import List, Optional, Tuple, Union
22
+
23
+ import torch
24
+ import torch.nn.functional as F
25
+ import torch.utils.checkpoint
26
+ from torch import nn
27
+ from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
28
+
29
+ from transformers import PretrainedConfig
30
+ from transformers.activations import ACT2FN
31
+ from transformers.cache_utils import Cache, DynamicCache
32
+ from transformers.modeling_attn_mask_utils import _prepare_4d_causal_attention_mask
33
+ from transformers.modeling_outputs import (
34
+ BaseModelOutputWithPast,
35
+ CausalLMOutputWithPast,
36
+ SequenceClassifierOutputWithPast,
37
+ TokenClassifierOutput,
38
+ )
39
+ from transformers.modeling_utils import PreTrainedModel
40
+ from transformers.utils import (
41
+ add_code_sample_docstrings,
42
+ add_start_docstrings,
43
+ add_start_docstrings_to_model_forward,
44
+ is_flash_attn_2_available,
45
+ is_flash_attn_greater_or_equal_2_10,
46
+ logging,
47
+ replace_return_docstrings,
48
+ )
49
+ from .configuration_phi import PhiConfig
50
+
51
+
52
+ try:
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
+ except:
56
+ pass
57
+
58
+
59
+ logger = logging.get_logger(__name__)
60
+
61
+ _CHECKPOINT_FOR_DOC = "microsoft/phi-2"
62
+ _CONFIG_FOR_DOC = "PhiConfig"
63
+
64
+ PHI_PRETRAINED_MODEL_ARCHIVE_LIST = [
65
+ "microsoft/phi-2",
66
+ # See all Phi models at https://huggingface.co/models?filter=phi
67
+ ]
68
+
69
+
70
+ # Copied from transformers.models.llama.modeling_llama._get_unpad_data
71
+ def _get_unpad_data(attention_mask):
72
+ seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
73
+ indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
74
+ max_seqlen_in_batch = seqlens_in_batch.max().item()
75
+ cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.torch.int32), (1, 0))
76
+ return (
77
+ indices,
78
+ cu_seqlens,
79
+ max_seqlen_in_batch,
80
+ )
81
+
82
+
83
+ # Copied from transformers.models.llama.modeling_llama.LlamaRotaryEmbedding with Llama->Phi
84
+ class PhiRotaryEmbedding(nn.Module):
85
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
86
+ super().__init__()
87
+
88
+ self.dim = dim
89
+ self.max_position_embeddings = max_position_embeddings
90
+ self.base = base
91
+ inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
92
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
93
+
94
+ # Build here to make `torch.jit.trace` work.
95
+ self._set_cos_sin_cache(
96
+ seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
97
+ )
98
+
99
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
100
+ self.max_seq_len_cached = seq_len
101
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
102
+
103
+ freqs = torch.outer(t, self.inv_freq)
104
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
105
+ emb = torch.cat((freqs, freqs), dim=-1)
106
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
107
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
108
+
109
+ def forward(self, x, seq_len=None):
110
+ # x: [bs, num_attention_heads, seq_len, head_size]
111
+ if seq_len > self.max_seq_len_cached:
112
+ self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
113
+
114
+ return (
115
+ self.cos_cached[:seq_len].to(dtype=x.dtype),
116
+ self.sin_cached[:seq_len].to(dtype=x.dtype),
117
+ )
118
+
119
+
120
+ # Copied from transformers.models.llama.modeling_llama.LlamaLinearScalingRotaryEmbedding with Llama->Phi
121
+ class PhiLinearScalingRotaryEmbedding(PhiRotaryEmbedding):
122
+ """PhiRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev"""
123
+
124
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
125
+ self.scaling_factor = scaling_factor
126
+ super().__init__(dim, max_position_embeddings, base, device)
127
+
128
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
129
+ self.max_seq_len_cached = seq_len
130
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
131
+ t = t / self.scaling_factor
132
+
133
+ freqs = torch.outer(t, self.inv_freq)
134
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
135
+ emb = torch.cat((freqs, freqs), dim=-1)
136
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
137
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
138
+
139
+
140
+ # Copied from transformers.models.llama.modeling_llama.LlamaDynamicNTKScalingRotaryEmbedding with Llama->Phi
141
+ class PhiDynamicNTKScalingRotaryEmbedding(PhiRotaryEmbedding):
142
+ """PhiRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla"""
143
+
144
+ def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0):
145
+ self.scaling_factor = scaling_factor
146
+ super().__init__(dim, max_position_embeddings, base, device)
147
+
148
+ def _set_cos_sin_cache(self, seq_len, device, dtype):
149
+ self.max_seq_len_cached = seq_len
150
+
151
+ if seq_len > self.max_position_embeddings:
152
+ base = self.base * (
153
+ (self.scaling_factor * seq_len / self.max_position_embeddings) - (self.scaling_factor - 1)
154
+ ) ** (self.dim / (self.dim - 2))
155
+ inv_freq = 1.0 / (base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
156
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
157
+
158
+ t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
159
+
160
+ freqs = torch.outer(t, self.inv_freq)
161
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
162
+ emb = torch.cat((freqs, freqs), dim=-1)
163
+ self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
164
+ self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
165
+
166
+
167
+ # Copied from transformers.models.llama.modeling_llama.rotate_half
168
+ def rotate_half(x):
169
+ """Rotates half the hidden dims of the input."""
170
+ x1 = x[..., : x.shape[-1] // 2]
171
+ x2 = x[..., x.shape[-1] // 2 :]
172
+ return torch.cat((-x2, x1), dim=-1)
173
+
174
+
175
+ # Copied from transformers.models.llama.modeling_llama.apply_rotary_pos_emb
176
+ def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
177
+ """Applies Rotary Position Embedding to the query and key tensors.
178
+ Args:
179
+ q (`torch.Tensor`): The query tensor.
180
+ k (`torch.Tensor`): The key tensor.
181
+ cos (`torch.Tensor`): The cosine part of the rotary embedding.
182
+ sin (`torch.Tensor`): The sine part of the rotary embedding.
183
+ position_ids (`torch.Tensor`):
184
+ The position indices of the tokens corresponding to the query and key tensors. For example, this can be
185
+ used to pass offsetted position ids when working with a KV-cache.
186
+ unsqueeze_dim (`int`, *optional*, defaults to 1):
187
+ The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
188
+ sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
189
+ that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
190
+ k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
191
+ cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
192
+ the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
193
+ Returns:
194
+ `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
195
+ """
196
+ cos = cos[position_ids].unsqueeze(unsqueeze_dim)
197
+ sin = sin[position_ids].unsqueeze(unsqueeze_dim)
198
+ q_embed = (q * cos) + (rotate_half(q) * sin)
199
+ k_embed = (k * cos) + (rotate_half(k) * sin)
200
+ return q_embed, k_embed
201
+
202
+ ######################ADDED###############################################################
203
+ class MoE(nn.Module):
204
+ def __init__(
205
+ self,
206
+ config: PretrainedConfig,
207
+ ):
208
+ super().__init__()
209
+ self.mlp = nn.ModuleList([PhiMLP(config) for i in range(config.num_local_experts)])
210
+ self.gate = nn.Linear(config.hidden_size, config.num_local_experts, bias=False)
211
+ self.num_experts_per_tok = config.num_experts_per_tok
212
+
213
+ def forward(self, x):
214
+ orig_shape = x.shape
215
+ x = x.view(-1, x.shape[-1])
216
+
217
+ scores = self.gate(x)
218
+ expert_weights, expert_indices = torch.topk(scores, self.num_experts_per_tok, dim=-1)
219
+ expert_weights = expert_weights.softmax(dim=-1)
220
+ flat_expert_indices = expert_indices.view(-1)
221
+
222
+ x = x.repeat_interleave(self.num_experts_per_tok, dim=0)
223
+ y = torch.empty_like(x)
224
+ for i, expert in enumerate(self.mlp):
225
+ y[flat_expert_indices == i] = expert(x[flat_expert_indices == i])
226
+ y = (y.view(*expert_weights.shape, -1) * expert_weights.unsqueeze(-1)).sum(dim=1)
227
+ return y.view(*orig_shape)
228
+
229
+ ############################################################################################
230
+
231
+ # Copied from transformers.models.clip.modeling_clip.CLIPMLP with CLIP->Phi
232
+ class PhiMLP(nn.Module):
233
+ def __init__(self, config):
234
+ super().__init__()
235
+ self.config = config
236
+ self.activation_fn = ACT2FN[config.hidden_act]
237
+ self.fc1 = nn.Linear(config.hidden_size, config.intermediate_size)
238
+ self.fc2 = nn.Linear(config.intermediate_size, config.hidden_size)
239
+
240
+ def forward(self, hidden_states: torch.Tensor) -> torch.Tensor:
241
+ hidden_states = self.fc1(hidden_states)
242
+ hidden_states = self.activation_fn(hidden_states)
243
+ hidden_states = self.fc2(hidden_states)
244
+ return hidden_states
245
+
246
+
247
+ # Copied from transformers.models.llama.modeling_llama.repeat_kv with llama->phi
248
+ def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
249
+ """
250
+ This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
251
+ num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
252
+ """
253
+ batch, num_key_value_heads, slen, head_dim = hidden_states.shape
254
+ if n_rep == 1:
255
+ return hidden_states
256
+ hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
257
+ return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
258
+
259
+
260
+ class PhiAttention(nn.Module):
261
+ """Multi-headed attention from 'Attention Is All You Need' paper"""
262
+
263
+ def __init__(self, config: PhiConfig, layer_idx: Optional[int] = None):
264
+ super().__init__()
265
+ self.config = config
266
+ self.layer_idx = layer_idx
267
+ if layer_idx is None:
268
+ logger.warning_once(
269
+ f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
270
+ "to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
271
+ "when creating this class."
272
+ )
273
+
274
+ self.attention_dropout = config.attention_dropout
275
+ self.hidden_size = config.hidden_size
276
+ self.num_heads = config.num_attention_heads
277
+ self.head_dim = self.hidden_size // self.num_heads
278
+ self.num_key_value_heads = config.num_key_value_heads
279
+ self.num_key_value_groups = self.num_heads // self.num_key_value_heads
280
+ self.max_position_embeddings = config.max_position_embeddings
281
+ self.rope_theta = config.rope_theta
282
+ self.partial_rotary_factor = config.partial_rotary_factor
283
+ self.is_causal = True
284
+
285
+ if (self.head_dim * self.num_heads) != self.hidden_size:
286
+ raise ValueError(
287
+ f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
288
+ f" and `num_heads`: {self.num_heads})."
289
+ )
290
+
291
+ self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=True)
292
+ self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=True)
293
+ self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=True)
294
+ self.dense = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=True)
295
+
296
+ self.qk_layernorm = config.qk_layernorm
297
+ if self.qk_layernorm:
298
+ self.q_layernorm = nn.LayerNorm(
299
+ config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True
300
+ )
301
+ self.k_layernorm = nn.LayerNorm(
302
+ config.hidden_size // self.num_heads, eps=config.layer_norm_eps, elementwise_affine=True
303
+ )
304
+
305
+ self._init_rope()
306
+
307
+ def _init_rope(self):
308
+ if self.config.rope_scaling is None:
309
+ self.rotary_emb = PhiRotaryEmbedding(
310
+ int(self.partial_rotary_factor * self.head_dim),
311
+ max_position_embeddings=self.max_position_embeddings,
312
+ base=self.rope_theta,
313
+ )
314
+ else:
315
+ scaling_type = self.config.rope_scaling["type"]
316
+ scaling_factor = self.config.rope_scaling["factor"]
317
+ if scaling_type == "linear":
318
+ self.rotary_emb = PhiLinearScalingRotaryEmbedding(
319
+ int(self.partial_rotary_factor * self.head_dim),
320
+ max_position_embeddings=self.max_position_embeddings,
321
+ scaling_factor=scaling_factor,
322
+ base=self.rope_theta,
323
+ )
324
+ elif scaling_type == "dynamic":
325
+ self.rotary_emb = PhiDynamicNTKScalingRotaryEmbedding(
326
+ int(self.partial_rotary_factor * self.head_dim),
327
+ max_position_embeddings=self.max_position_embeddings,
328
+ scaling_factor=scaling_factor,
329
+ base=self.rope_theta,
330
+ )
331
+ else:
332
+ raise ValueError(f"Unknown RoPE scaling type {scaling_type}")
333
+
334
+ # Phi-2 has an attention overflow issue (with FP16) and requires autocast to be disabled
335
+ @torch.autocast("cpu", enabled=False)
336
+ @torch.autocast("cuda", enabled=False)
337
+ def forward(
338
+ self,
339
+ hidden_states: torch.Tensor,
340
+ attention_mask: Optional[torch.Tensor] = None,
341
+ position_ids: Optional[torch.LongTensor] = None,
342
+ past_key_value: Optional[Cache] = None,
343
+ output_attentions: bool = False,
344
+ use_cache: bool = False,
345
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
346
+ bsz, q_len, _ = hidden_states.size()
347
+
348
+ query_states = self.q_proj(hidden_states)
349
+ key_states = self.k_proj(hidden_states)
350
+ value_states = self.v_proj(hidden_states)
351
+
352
+ if self.qk_layernorm:
353
+ query_states = self.q_layernorm(query_states)
354
+ key_states = self.k_layernorm(key_states)
355
+
356
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
357
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
358
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
359
+
360
+ kv_seq_len = key_states.shape[-2]
361
+ if past_key_value is not None:
362
+ if self.layer_idx is None:
363
+ raise ValueError(
364
+ f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
365
+ "for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
366
+ "with a layer index."
367
+ )
368
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
369
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
370
+
371
+ # Partial rotary embedding
372
+ query_rot, query_pass = (
373
+ query_states[..., : self.rotary_emb.dim],
374
+ query_states[..., self.rotary_emb.dim :],
375
+ )
376
+ key_rot, key_pass = (
377
+ key_states[..., : self.rotary_emb.dim],
378
+ key_states[..., self.rotary_emb.dim :],
379
+ )
380
+ # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
381
+ query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
382
+
383
+ # [batch_size, seq_length, num_heads, head_dim]
384
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
385
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
386
+
387
+ if past_key_value is not None:
388
+ cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim}
389
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
390
+
391
+ key_states = repeat_kv(key_states, self.num_key_value_groups)
392
+ value_states = repeat_kv(value_states, self.num_key_value_groups)
393
+
394
+ # Queries and keys upcast to fp32 is required by Phi-2 to avoid overflow
395
+ attn_weights = torch.matmul(
396
+ query_states.to(torch.float32), key_states.to(torch.float32).transpose(2, 3)
397
+ ) / math.sqrt(self.head_dim)
398
+
399
+ if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
400
+ raise ValueError(
401
+ f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
402
+ f" {attn_weights.size()}"
403
+ )
404
+
405
+ if attention_mask is not None:
406
+ if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
407
+ raise ValueError(
408
+ f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
409
+ )
410
+ attn_weights = attn_weights + attention_mask
411
+
412
+ # upcast attention to fp32
413
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(value_states.dtype)
414
+ attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training)
415
+
416
+ attn_output = torch.matmul(attn_weights, value_states)
417
+
418
+ if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
419
+ raise ValueError(
420
+ f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
421
+ f" {attn_output.size()}"
422
+ )
423
+
424
+ attn_output = attn_output.transpose(1, 2).contiguous()
425
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
426
+
427
+ attn_output = self.dense(attn_output)
428
+
429
+ if not output_attentions:
430
+ attn_weights = None
431
+
432
+ return attn_output, attn_weights, past_key_value
433
+
434
+
435
+ class PhiFlashAttention2(PhiAttention):
436
+ """
437
+ Phi flash attention module. This module inherits from `PhiAttention` as the weights of the module stays
438
+ untouched. The only required change would be on the forward pass where it needs to correctly call the public API of
439
+ flash attention and deal with padding tokens in case the input contains any of them.
440
+ """
441
+
442
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
443
+ def __init__(self, *args, **kwargs):
444
+ super().__init__(*args, **kwargs)
445
+
446
+ # TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
447
+ # 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.
448
+ # 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).
449
+ self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
450
+
451
+ def forward(
452
+ self,
453
+ hidden_states: torch.Tensor,
454
+ attention_mask: Optional[torch.LongTensor] = None,
455
+ position_ids: Optional[torch.LongTensor] = None,
456
+ past_key_value: Optional[Cache] = None,
457
+ output_attentions: bool = False,
458
+ use_cache: bool = False,
459
+ **kwargs,
460
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
461
+ # PhiFlashAttention2 attention does not support output_attentions
462
+
463
+ output_attentions = False
464
+
465
+ bsz, q_len, _ = hidden_states.size()
466
+
467
+ query_states = self.q_proj(hidden_states)
468
+ key_states = self.k_proj(hidden_states)
469
+ value_states = self.v_proj(hidden_states)
470
+
471
+ if self.qk_layernorm:
472
+ query_states = self.q_layernorm(query_states)
473
+ key_states = self.k_layernorm(key_states)
474
+
475
+ # Flash attention requires the input to have the shape
476
+ # batch_size x seq_length x head_dim x hidden_dim
477
+ # therefore we just need to keep the original shape
478
+ query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
479
+ key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
480
+ value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
481
+
482
+ kv_seq_len = key_states.shape[-2]
483
+ if past_key_value is not None:
484
+ kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
485
+ cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
486
+
487
+ # Partial rotary embedding
488
+ query_rot, query_pass = (
489
+ query_states[..., : self.rotary_emb.dim],
490
+ query_states[..., self.rotary_emb.dim :],
491
+ )
492
+ key_rot, key_pass = (
493
+ key_states[..., : self.rotary_emb.dim],
494
+ key_states[..., self.rotary_emb.dim :],
495
+ )
496
+ # [batch_size, seq_length, num_heads, head_dim // config.partial_rotary_factor]
497
+ query_rot, key_rot = apply_rotary_pos_emb(query_rot, key_rot, cos, sin, position_ids)
498
+
499
+ # [batch_size, seq_length, num_heads, head_dim]
500
+ query_states = torch.cat((query_rot, query_pass), dim=-1)
501
+ key_states = torch.cat((key_rot, key_pass), dim=-1)
502
+
503
+ if past_key_value is not None:
504
+ cache_kwargs = {"sin": sin, "cos": cos, "partial_rotation_size": self.rotary_emb.dim}
505
+ key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
506
+
507
+ # TODO: These transpose are quite inefficient but Flash Attention requires the layout [batch_size, sequence_length, num_heads, head_dim]. We would need to refactor the KV cache
508
+ # to be able to avoid many of these transpose/reshape/view.
509
+ query_states = query_states.transpose(1, 2)
510
+ key_states = key_states.transpose(1, 2)
511
+ value_states = value_states.transpose(1, 2)
512
+
513
+ attn_dropout = self.attention_dropout if self.training else 0.0
514
+
515
+ # In PEFT, usually we cast the layer norms in float32 for training stability reasons
516
+ # therefore the input hidden states gets silently casted in float32. Hence, we need
517
+ # cast them back in the correct dtype just to be sure everything works as expected.
518
+ # This might slowdown training & inference so it is recommended to not cast the LayerNorms
519
+ # in fp32.
520
+
521
+ if query_states.dtype == torch.float32:
522
+ if torch.is_autocast_enabled():
523
+ target_dtype = torch.get_autocast_gpu_dtype()
524
+ # Handle the case where the model is quantized
525
+ elif hasattr(self.config, "_pre_quantization_dtype"):
526
+ target_dtype = self.config._pre_quantization_dtype
527
+ else:
528
+ target_dtype = self.q_proj.weight.dtype
529
+
530
+ logger.warning_once(
531
+ f"The input hidden states seems to be silently casted in float32, this might be related to"
532
+ f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
533
+ f" {target_dtype}."
534
+ )
535
+
536
+ query_states = query_states.to(target_dtype)
537
+ key_states = key_states.to(target_dtype)
538
+ value_states = value_states.to(target_dtype)
539
+
540
+ attn_output = self._flash_attention_forward(
541
+ query_states, key_states, value_states, attention_mask, q_len, dropout=attn_dropout, softmax_scale=None
542
+ )
543
+
544
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
545
+ attn_output = self.dense(attn_output)
546
+
547
+ if not output_attentions:
548
+ attn_weights = None
549
+
550
+ return attn_output, attn_weights, past_key_value
551
+
552
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._flash_attention_forward
553
+ def _flash_attention_forward(
554
+ self, query_states, key_states, value_states, attention_mask, query_length, dropout=0.0, softmax_scale=None
555
+ ):
556
+ """
557
+ Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
558
+ first unpad the input, then computes the attention scores and pad the final attention scores.
559
+ Args:
560
+ query_states (`torch.Tensor`):
561
+ Input query states to be passed to Flash Attention API
562
+ key_states (`torch.Tensor`):
563
+ Input key states to be passed to Flash Attention API
564
+ value_states (`torch.Tensor`):
565
+ Input value states to be passed to Flash Attention API
566
+ attention_mask (`torch.Tensor`):
567
+ The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
568
+ position of padding tokens and 1 for the position of non-padding tokens.
569
+ dropout (`int`, *optional*):
570
+ Attention dropout
571
+ softmax_scale (`float`, *optional*):
572
+ The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
573
+ """
574
+ if not self._flash_attn_uses_top_left_mask:
575
+ causal = self.is_causal
576
+ else:
577
+ # 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__.
578
+ causal = self.is_causal and query_length != 1
579
+
580
+ # Contains at least one padding token in the sequence
581
+ if attention_mask is not None:
582
+ batch_size = query_states.shape[0]
583
+ query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
584
+ query_states, key_states, value_states, attention_mask, query_length
585
+ )
586
+
587
+ cu_seqlens_q, cu_seqlens_k = cu_seq_lens
588
+ max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
589
+
590
+ attn_output_unpad = flash_attn_varlen_func(
591
+ query_states,
592
+ key_states,
593
+ value_states,
594
+ cu_seqlens_q=cu_seqlens_q,
595
+ cu_seqlens_k=cu_seqlens_k,
596
+ max_seqlen_q=max_seqlen_in_batch_q,
597
+ max_seqlen_k=max_seqlen_in_batch_k,
598
+ dropout_p=dropout,
599
+ softmax_scale=softmax_scale,
600
+ causal=causal,
601
+ )
602
+
603
+ attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
604
+ else:
605
+ attn_output = flash_attn_func(
606
+ query_states, key_states, value_states, dropout, softmax_scale=softmax_scale, causal=causal
607
+ )
608
+
609
+ return attn_output
610
+
611
+ # Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2._upad_input
612
+ def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
613
+ indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
614
+ batch_size, kv_seq_len, num_key_value_heads, head_dim = key_layer.shape
615
+
616
+ key_layer = index_first_axis(
617
+ key_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
618
+ )
619
+ value_layer = index_first_axis(
620
+ value_layer.reshape(batch_size * kv_seq_len, num_key_value_heads, head_dim), indices_k
621
+ )
622
+ if query_length == kv_seq_len:
623
+ query_layer = index_first_axis(
624
+ query_layer.reshape(batch_size * kv_seq_len, self.num_heads, head_dim), indices_k
625
+ )
626
+ cu_seqlens_q = cu_seqlens_k
627
+ max_seqlen_in_batch_q = max_seqlen_in_batch_k
628
+ indices_q = indices_k
629
+ elif query_length == 1:
630
+ max_seqlen_in_batch_q = 1
631
+ cu_seqlens_q = torch.arange(
632
+ batch_size + 1, dtype=torch.int32, device=query_layer.device
633
+ ) # There is a memcpy here, that is very bad.
634
+ indices_q = cu_seqlens_q[:-1]
635
+ query_layer = query_layer.squeeze(1)
636
+ else:
637
+ # The -q_len: slice assumes left padding.
638
+ attention_mask = attention_mask[:, -query_length:]
639
+ query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
640
+
641
+ return (
642
+ query_layer,
643
+ key_layer,
644
+ value_layer,
645
+ indices_q,
646
+ (cu_seqlens_q, cu_seqlens_k),
647
+ (max_seqlen_in_batch_q, max_seqlen_in_batch_k),
648
+ )
649
+
650
+
651
+ PHI_ATTENTION_CLASSES = {
652
+ "eager": PhiAttention,
653
+ "flash_attention_2": PhiFlashAttention2,
654
+ }
655
+
656
+
657
+ class PhiDecoderLayer(nn.Module):
658
+ def __init__(self, config: PhiConfig, layer_idx: int):
659
+ super().__init__()
660
+ self.self_attn = PHI_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx=layer_idx)
661
+ self.moe = MoE(config) ######################## self.mlp = PhiMLP(config)
662
+ self.input_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
663
+ self.resid_dropout = nn.Dropout(config.resid_pdrop)
664
+
665
+ def forward(
666
+ self,
667
+ hidden_states: torch.Tensor,
668
+ attention_mask: Optional[torch.Tensor] = None,
669
+ position_ids: Optional[torch.LongTensor] = None,
670
+ output_attentions: Optional[bool] = False,
671
+ use_cache: Optional[bool] = False,
672
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
673
+ ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
674
+ """
675
+ Args:
676
+ hidden_states (`torch.FloatTensor`):
677
+ input to the layer of shape `(batch, seq_len, embed_dim)`
678
+ attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
679
+ `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
680
+ position_ids (`torch.LongTensor` of shape `({0})`, *optional*):
681
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range
682
+ `[0, config.n_positions - 1]`. [What are position IDs?](../glossary#position-ids)
683
+ output_attentions (`bool`, *optional*):
684
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under
685
+ returned tensors for more detail.
686
+ use_cache (`bool`, *optional*):
687
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
688
+ (see `past_key_values`).
689
+ past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
690
+ """
691
+
692
+ residual = hidden_states
693
+
694
+ hidden_states = self.input_layernorm(hidden_states)
695
+
696
+ # Self Attention
697
+ attn_outputs, self_attn_weights, present_key_value = self.self_attn(
698
+ hidden_states=hidden_states,
699
+ attention_mask=attention_mask,
700
+ position_ids=position_ids,
701
+ past_key_value=past_key_value,
702
+ output_attentions=output_attentions,
703
+ use_cache=use_cache,
704
+ )
705
+ attn_outputs = self.resid_dropout(attn_outputs)
706
+
707
+ feed_forward_hidden_states = self.resid_dropout(self.mlp(hidden_states))
708
+ hidden_states = attn_outputs + feed_forward_hidden_states + residual
709
+ outputs = (hidden_states,)
710
+
711
+ if output_attentions:
712
+ outputs += (self_attn_weights,)
713
+
714
+ if use_cache:
715
+ outputs += (present_key_value,)
716
+
717
+ return outputs
718
+
719
+
720
+ PHI_START_DOCSTRING = r"""
721
+ This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
722
+ library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
723
+ etc.)
724
+ This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
725
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
726
+ and behavior.
727
+ Parameters:
728
+ config ([`PhiConfig`]):
729
+ Model configuration class with all the parameters of the model. Initializing with a config file does not
730
+ load the weights associated with the model, only the configuration. Check out the
731
+ [`~PreTrainedModel.from_pretrained`] method to load the model weights.
732
+ """
733
+
734
+
735
+ @add_start_docstrings(
736
+ "The bare Phi Model outputting raw hidden-states without any specific head on top.",
737
+ PHI_START_DOCSTRING,
738
+ )
739
+ class PhiPreTrainedModel(PreTrainedModel):
740
+ config_class = PhiConfig
741
+ base_model_prefix = "model"
742
+ supports_gradient_checkpointing = True
743
+ _no_split_modules = ["PhiDecoderLayer"]
744
+ _skip_keys_device_placement = "past_key_values"
745
+ _supports_flash_attn_2 = True
746
+ _supports_cache_class = True
747
+
748
+ def _init_weights(self, module):
749
+ std = self.config.initializer_range
750
+ if isinstance(module, nn.Linear):
751
+ module.weight.data.normal_(mean=0.0, std=std)
752
+ if module.bias is not None:
753
+ module.bias.data.zero_()
754
+ elif isinstance(module, nn.Embedding):
755
+ module.weight.data.normal_(mean=0.0, std=std)
756
+ if module.padding_idx is not None:
757
+ module.weight.data[module.padding_idx].zero_()
758
+
759
+
760
+ PHI_INPUTS_DOCSTRING = r"""
761
+ Args:
762
+ input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
763
+ Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
764
+ it.
765
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
766
+ [`PreTrainedTokenizer.__call__`] for details.
767
+ [What are input IDs?](../glossary#input-ids)
768
+ attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
769
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
770
+ - 1 for tokens that are **not masked**,
771
+ - 0 for tokens that are **masked**.
772
+ [What are attention masks?](../glossary#attention-mask)
773
+ Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
774
+ [`PreTrainedTokenizer.__call__`] for details.
775
+ If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
776
+ `past_key_values`).
777
+ If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
778
+ and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
779
+ information on the default strategy.
780
+ - 1 indicates the head is **not masked**,
781
+ - 0 indicates the head is **masked**.
782
+ position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
783
+ Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
784
+ config.n_positions - 1]`.
785
+ [What are position IDs?](../glossary#position-ids)
786
+ past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
787
+ Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
788
+ blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
789
+ returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
790
+ Two formats are allowed:
791
+ - a [`~cache_utils.Cache`] instance;
792
+ - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
793
+ shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
794
+ cache format.
795
+ The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
796
+ legacy cache format will be returned.
797
+ If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
798
+ have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
799
+ of shape `(batch_size, sequence_length)`.
800
+ inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
801
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
802
+ is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
803
+ model's internal embedding lookup matrix.
804
+ use_cache (`bool`, *optional*):
805
+ If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
806
+ `past_key_values`).
807
+ output_attentions (`bool`, *optional*):
808
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
809
+ tensors for more detail.
810
+ output_hidden_states (`bool`, *optional*):
811
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
812
+ more detail.
813
+ return_dict (`bool`, *optional*):
814
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
815
+ """
816
+
817
+
818
+ @add_start_docstrings(
819
+ "The bare Phi Model outputting raw hidden-states without any specific head on top.",
820
+ PHI_START_DOCSTRING,
821
+ )
822
+ class PhiModel(PhiPreTrainedModel):
823
+ """
824
+ Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`PhiDecoderLayer`]
825
+ Args:
826
+ config: PhiConfig
827
+ """
828
+
829
+ def __init__(self, config: PhiConfig):
830
+ super().__init__(config)
831
+ self.padding_idx = config.pad_token_id
832
+ self.vocab_size = config.vocab_size
833
+
834
+ self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
835
+ self.embed_dropout = nn.Dropout(config.embd_pdrop)
836
+ self.layers = nn.ModuleList(
837
+ [PhiDecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
838
+ )
839
+ self.final_layernorm = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_eps)
840
+ self._use_flash_attention_2 = config._attn_implementation == "flash_attention_2"
841
+
842
+ self.gradient_checkpointing = False
843
+ # Initialize weights and apply final processing
844
+ self.post_init()
845
+
846
+ def get_input_embeddings(self):
847
+ return self.embed_tokens
848
+
849
+ def set_input_embeddings(self, value):
850
+ self.embed_tokens = value
851
+
852
+ @add_start_docstrings_to_model_forward(PHI_INPUTS_DOCSTRING)
853
+ def forward(
854
+ self,
855
+ input_ids: torch.LongTensor = None,
856
+ attention_mask: Optional[torch.Tensor] = None,
857
+ position_ids: Optional[torch.LongTensor] = None,
858
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
859
+ inputs_embeds: Optional[torch.FloatTensor] = None,
860
+ use_cache: Optional[bool] = None,
861
+ output_attentions: Optional[bool] = None,
862
+ output_hidden_states: Optional[bool] = None,
863
+ return_dict: Optional[bool] = None,
864
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
865
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
866
+ output_hidden_states = (
867
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
868
+ )
869
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
870
+
871
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
872
+
873
+ # retrieve input_ids and inputs_embeds
874
+ if input_ids is not None and inputs_embeds is not None:
875
+ raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
876
+ elif input_ids is not None:
877
+ batch_size, seq_length = input_ids.shape[:2]
878
+ elif inputs_embeds is not None:
879
+ batch_size, seq_length = inputs_embeds.shape[:2]
880
+ else:
881
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
882
+
883
+ past_key_values_length = 0
884
+
885
+ if self.gradient_checkpointing and self.training:
886
+ if use_cache:
887
+ logger.warning_once(
888
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
889
+ )
890
+ use_cache = False
891
+
892
+ if use_cache:
893
+ use_legacy_cache = not isinstance(past_key_values, Cache)
894
+ if use_legacy_cache:
895
+ past_key_values = DynamicCache.from_legacy_cache(past_key_values)
896
+ past_key_values_length = past_key_values.get_usable_length(seq_length)
897
+
898
+ if position_ids is None:
899
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
900
+ position_ids = torch.arange(
901
+ past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
902
+ )
903
+ position_ids = position_ids.unsqueeze(0)
904
+
905
+ if inputs_embeds is None:
906
+ inputs_embeds = self.embed_tokens(input_ids)
907
+
908
+ inputs_embeds = self.embed_dropout(inputs_embeds)
909
+
910
+ # Attention mask.
911
+ if self._use_flash_attention_2:
912
+ # 2d mask is passed through the layers
913
+ attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
914
+ else:
915
+ # 4d mask is passed through the layers
916
+ attention_mask = _prepare_4d_causal_attention_mask(
917
+ attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
918
+ )
919
+
920
+ hidden_states = inputs_embeds
921
+
922
+ # decoder layers
923
+ all_hidden_states = () if output_hidden_states else None
924
+ all_self_attns = () if output_attentions else None
925
+ next_decoder_cache = None
926
+
927
+ for decoder_layer in self.layers:
928
+ if output_hidden_states:
929
+ all_hidden_states += (hidden_states,)
930
+
931
+ if self.gradient_checkpointing and self.training:
932
+ layer_outputs = self._gradient_checkpointing_func(
933
+ decoder_layer.__call__,
934
+ hidden_states,
935
+ attention_mask,
936
+ position_ids,
937
+ past_key_values,
938
+ output_attentions,
939
+ )
940
+ else:
941
+ layer_outputs = decoder_layer(
942
+ hidden_states,
943
+ attention_mask=attention_mask,
944
+ position_ids=position_ids,
945
+ past_key_value=past_key_values,
946
+ output_attentions=output_attentions,
947
+ use_cache=use_cache,
948
+ )
949
+
950
+ hidden_states = layer_outputs[0]
951
+
952
+ if use_cache:
953
+ next_decoder_cache = layer_outputs[2 if output_attentions else 1]
954
+
955
+ if output_attentions:
956
+ all_self_attns += (layer_outputs[1],)
957
+
958
+ hidden_states = self.final_layernorm(hidden_states)
959
+
960
+ # add hidden states from the last decoder layer
961
+ if output_hidden_states:
962
+ all_hidden_states += (hidden_states,)
963
+
964
+ next_cache = None
965
+ if use_cache:
966
+ next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
967
+ if not return_dict:
968
+ return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
969
+ return BaseModelOutputWithPast(
970
+ last_hidden_state=hidden_states,
971
+ past_key_values=next_cache,
972
+ hidden_states=all_hidden_states,
973
+ attentions=all_self_attns,
974
+ )
975
+
976
+
977
+ class PhiForCausalLM(PhiPreTrainedModel):
978
+ _tied_weights_keys = ["lm_head.weight"]
979
+
980
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.__init__ with Llama->Phi,bias=False->bias=True
981
+ def __init__(self, config):
982
+ super().__init__(config)
983
+ self.model = PhiModel(config)
984
+ self.vocab_size = config.vocab_size
985
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=True)
986
+
987
+ # Initialize weights and apply final processing
988
+ self.post_init()
989
+
990
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_input_embeddings
991
+ def get_input_embeddings(self):
992
+ return self.model.embed_tokens
993
+
994
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_input_embeddings
995
+ def set_input_embeddings(self, value):
996
+ self.model.embed_tokens = value
997
+
998
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_output_embeddings
999
+ def get_output_embeddings(self):
1000
+ return self.lm_head
1001
+
1002
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_output_embeddings
1003
+ def set_output_embeddings(self, new_embeddings):
1004
+ self.lm_head = new_embeddings
1005
+
1006
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.set_decoder
1007
+ def set_decoder(self, decoder):
1008
+ self.model = decoder
1009
+
1010
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.get_decoder
1011
+ def get_decoder(self):
1012
+ return self.model
1013
+
1014
+ @add_start_docstrings_to_model_forward(PHI_INPUTS_DOCSTRING)
1015
+ @replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
1016
+ def forward(
1017
+ self,
1018
+ input_ids: torch.LongTensor = None,
1019
+ attention_mask: Optional[torch.Tensor] = None,
1020
+ position_ids: Optional[torch.LongTensor] = None,
1021
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1022
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1023
+ labels: Optional[torch.LongTensor] = None,
1024
+ use_cache: Optional[bool] = None,
1025
+ output_attentions: Optional[bool] = None,
1026
+ output_hidden_states: Optional[bool] = None,
1027
+ return_dict: Optional[bool] = None,
1028
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
1029
+ r"""
1030
+ Args:
1031
+ labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
1032
+ Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
1033
+ config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
1034
+ (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
1035
+ Returns:
1036
+ Example:
1037
+ ```python
1038
+ >>> from transformers import AutoTokenizer, PhiForCausalLM
1039
+ >>> model = PhiForCausalLM.from_pretrained("microsoft/phi-1")
1040
+ >>> tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1")
1041
+ >>> prompt = "This is an example script ."
1042
+ >>> inputs = tokenizer(prompt, return_tensors="pt")
1043
+ >>> # Generate
1044
+ >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
1045
+ >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
1046
+ 'This is an example script .
1047
+
1048
+
1049
+
1050
+ from typing import List
1051
+
1052
+ def find_most_common_letter(words: List[str'
1053
+ ```"""
1054
+
1055
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
1056
+ output_hidden_states = (
1057
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
1058
+ )
1059
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1060
+
1061
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
1062
+ outputs = self.model(
1063
+ input_ids=input_ids,
1064
+ attention_mask=attention_mask,
1065
+ position_ids=position_ids,
1066
+ past_key_values=past_key_values,
1067
+ inputs_embeds=inputs_embeds,
1068
+ use_cache=use_cache,
1069
+ output_attentions=output_attentions,
1070
+ output_hidden_states=output_hidden_states,
1071
+ return_dict=return_dict,
1072
+ )
1073
+
1074
+ hidden_states = outputs[0]
1075
+ logits = self.lm_head(hidden_states)
1076
+ logits = logits.float()
1077
+
1078
+ loss = None
1079
+ if labels is not None:
1080
+ # Shift so that tokens < n predict n
1081
+ shift_logits = logits[..., :-1, :].contiguous()
1082
+ shift_labels = labels[..., 1:].contiguous()
1083
+ # Flatten the tokens
1084
+ loss_fct = CrossEntropyLoss()
1085
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
1086
+ shift_labels = shift_labels.view(-1)
1087
+ # Enable model parallelism
1088
+ shift_labels = shift_labels.to(shift_logits.device)
1089
+ loss = loss_fct(shift_logits, shift_labels)
1090
+
1091
+ if not return_dict:
1092
+ output = (logits,) + outputs[1:]
1093
+ return (loss,) + output if loss is not None else output
1094
+
1095
+ return CausalLMOutputWithPast(
1096
+ loss=loss,
1097
+ logits=logits,
1098
+ past_key_values=outputs.past_key_values,
1099
+ hidden_states=outputs.hidden_states,
1100
+ attentions=outputs.attentions,
1101
+ )
1102
+
1103
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM.prepare_inputs_for_generation
1104
+ def prepare_inputs_for_generation(
1105
+ self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
1106
+ ):
1107
+ if past_key_values is not None:
1108
+ if isinstance(past_key_values, Cache):
1109
+ cache_length = past_key_values.get_seq_length()
1110
+ past_length = past_key_values.seen_tokens
1111
+ max_cache_length = past_key_values.get_max_length()
1112
+ else:
1113
+ cache_length = past_length = past_key_values[0][0].shape[2]
1114
+ max_cache_length = None
1115
+
1116
+ # Keep only the unprocessed tokens:
1117
+ # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
1118
+ # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
1119
+ # input)
1120
+ if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
1121
+ input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
1122
+ # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
1123
+ # input_ids based on the past_length.
1124
+ elif past_length < input_ids.shape[1]:
1125
+ input_ids = input_ids[:, past_length:]
1126
+ # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
1127
+
1128
+ # If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
1129
+ if (
1130
+ max_cache_length is not None
1131
+ and attention_mask is not None
1132
+ and cache_length + input_ids.shape[1] > max_cache_length
1133
+ ):
1134
+ attention_mask = attention_mask[:, -max_cache_length:]
1135
+
1136
+ position_ids = kwargs.get("position_ids", None)
1137
+ if attention_mask is not None and position_ids is None:
1138
+ # create position_ids on the fly for batch generation
1139
+ position_ids = attention_mask.long().cumsum(-1) - 1
1140
+ position_ids.masked_fill_(attention_mask == 0, 1)
1141
+ if past_key_values:
1142
+ position_ids = position_ids[:, -input_ids.shape[1] :]
1143
+
1144
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
1145
+ if inputs_embeds is not None and past_key_values is None:
1146
+ model_inputs = {"inputs_embeds": inputs_embeds}
1147
+ else:
1148
+ model_inputs = {"input_ids": input_ids}
1149
+
1150
+ model_inputs.update(
1151
+ {
1152
+ "position_ids": position_ids,
1153
+ "past_key_values": past_key_values,
1154
+ "use_cache": kwargs.get("use_cache"),
1155
+ "attention_mask": attention_mask,
1156
+ }
1157
+ )
1158
+ return model_inputs
1159
+
1160
+ @staticmethod
1161
+ # Copied from transformers.models.llama.modeling_llama.LlamaForCausalLM._reorder_cache
1162
+ def _reorder_cache(past_key_values, beam_idx):
1163
+ reordered_past = ()
1164
+ for layer_past in past_key_values:
1165
+ reordered_past += (
1166
+ tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
1167
+ )
1168
+ return reordered_past
1169
+
1170
+
1171
+ @add_start_docstrings(
1172
+ """
1173
+ The PhiModel with a sequence classification head on top (linear layer).
1174
+ [`PhiForSequenceClassification`] uses the last token in order to do the classification, as other causal models
1175
+ (e.g. GPT-2) do.
1176
+ Since it does classification on the last token, it requires to know the position of the last token. If a
1177
+ `pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
1178
+ no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
1179
+ padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
1180
+ each row of the batch).
1181
+ """,
1182
+ PHI_START_DOCSTRING,
1183
+ )
1184
+ # Copied from transformers.models.llama.modeling_llama.LlamaForSequenceClassification with LLAMA->PHI,Llama->Phi with self.transformer->self.model, transformer_outputs->model_outputs
1185
+ class PhiForSequenceClassification(PhiPreTrainedModel):
1186
+ def __init__(self, config):
1187
+ super().__init__(config)
1188
+ self.num_labels = config.num_labels
1189
+ self.model = PhiModel(config)
1190
+ self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
1191
+
1192
+ # Initialize weights and apply final processing
1193
+ self.post_init()
1194
+
1195
+ def get_input_embeddings(self):
1196
+ return self.model.embed_tokens
1197
+
1198
+ def set_input_embeddings(self, value):
1199
+ self.model.embed_tokens = value
1200
+
1201
+ @add_start_docstrings_to_model_forward(PHI_INPUTS_DOCSTRING)
1202
+ def forward(
1203
+ self,
1204
+ input_ids: torch.LongTensor = None,
1205
+ attention_mask: Optional[torch.Tensor] = None,
1206
+ position_ids: Optional[torch.LongTensor] = None,
1207
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
1208
+ inputs_embeds: Optional[torch.FloatTensor] = None,
1209
+ labels: Optional[torch.LongTensor] = None,
1210
+ use_cache: Optional[bool] = None,
1211
+ output_attentions: Optional[bool] = None,
1212
+ output_hidden_states: Optional[bool] = None,
1213
+ return_dict: Optional[bool] = None,
1214
+ ) -> Union[Tuple, SequenceClassifierOutputWithPast]:
1215
+ r"""
1216
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1217
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1218
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1219
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1220
+ """
1221
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1222
+
1223
+ model_outputs = self.model(
1224
+ input_ids,
1225
+ attention_mask=attention_mask,
1226
+ position_ids=position_ids,
1227
+ past_key_values=past_key_values,
1228
+ inputs_embeds=inputs_embeds,
1229
+ use_cache=use_cache,
1230
+ output_attentions=output_attentions,
1231
+ output_hidden_states=output_hidden_states,
1232
+ return_dict=return_dict,
1233
+ )
1234
+ hidden_states = model_outputs[0]
1235
+ logits = self.score(hidden_states)
1236
+
1237
+ if input_ids is not None:
1238
+ batch_size = input_ids.shape[0]
1239
+ else:
1240
+ batch_size = inputs_embeds.shape[0]
1241
+
1242
+ if self.config.pad_token_id is None and batch_size != 1:
1243
+ raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
1244
+ if self.config.pad_token_id is None:
1245
+ sequence_lengths = -1
1246
+ else:
1247
+ if input_ids is not None:
1248
+ # if no pad token found, use modulo instead of reverse indexing for ONNX compatibility
1249
+ sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
1250
+ sequence_lengths = sequence_lengths % input_ids.shape[-1]
1251
+ sequence_lengths = sequence_lengths.to(logits.device)
1252
+ else:
1253
+ sequence_lengths = -1
1254
+
1255
+ pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
1256
+
1257
+ loss = None
1258
+ if labels is not None:
1259
+ labels = labels.to(logits.device)
1260
+ if self.config.problem_type is None:
1261
+ if self.num_labels == 1:
1262
+ self.config.problem_type = "regression"
1263
+ elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
1264
+ self.config.problem_type = "single_label_classification"
1265
+ else:
1266
+ self.config.problem_type = "multi_label_classification"
1267
+
1268
+ if self.config.problem_type == "regression":
1269
+ loss_fct = MSELoss()
1270
+ if self.num_labels == 1:
1271
+ loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
1272
+ else:
1273
+ loss = loss_fct(pooled_logits, labels)
1274
+ elif self.config.problem_type == "single_label_classification":
1275
+ loss_fct = CrossEntropyLoss()
1276
+ loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
1277
+ elif self.config.problem_type == "multi_label_classification":
1278
+ loss_fct = BCEWithLogitsLoss()
1279
+ loss = loss_fct(pooled_logits, labels)
1280
+ if not return_dict:
1281
+ output = (pooled_logits,) + model_outputs[1:]
1282
+ return ((loss,) + output) if loss is not None else output
1283
+
1284
+ return SequenceClassifierOutputWithPast(
1285
+ loss=loss,
1286
+ logits=pooled_logits,
1287
+ past_key_values=model_outputs.past_key_values,
1288
+ hidden_states=model_outputs.hidden_states,
1289
+ attentions=model_outputs.attentions,
1290
+ )
1291
+
1292
+
1293
+ @add_start_docstrings(
1294
+ """
1295
+ PhiModel with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for
1296
+ Named-Entity-Recognition (NER) tasks.
1297
+ """,
1298
+ PHI_START_DOCSTRING,
1299
+ )
1300
+ # Copied from transformers.models.mpt.modeling_mpt.MptForTokenClassification with MPT->PHI,Mpt->Phi,self.transformer->self.model,transformer_outputs->model_outputs
1301
+ class PhiForTokenClassification(PhiPreTrainedModel):
1302
+ def __init__(self, config: PhiConfig):
1303
+ super().__init__(config)
1304
+ self.num_labels = config.num_labels
1305
+
1306
+ self.model = PhiModel(config)
1307
+ if hasattr(config, "classifier_dropout") and config.classifier_dropout is not None:
1308
+ classifier_dropout = config.classifier_dropout
1309
+ elif hasattr(config, "hidden_dropout") and config.hidden_dropout is not None:
1310
+ classifier_dropout = config.hidden_dropout
1311
+ else:
1312
+ classifier_dropout = 0.1
1313
+ self.dropout = nn.Dropout(classifier_dropout)
1314
+ self.classifier = nn.Linear(config.hidden_size, config.num_labels)
1315
+
1316
+ # Initialize weights and apply final processing
1317
+ self.post_init()
1318
+
1319
+ @add_start_docstrings_to_model_forward(PHI_INPUTS_DOCSTRING)
1320
+ @add_code_sample_docstrings(
1321
+ checkpoint=_CHECKPOINT_FOR_DOC,
1322
+ output_type=TokenClassifierOutput,
1323
+ config_class=_CONFIG_FOR_DOC,
1324
+ )
1325
+ def forward(
1326
+ self,
1327
+ input_ids: Optional[torch.LongTensor] = None,
1328
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
1329
+ attention_mask: Optional[torch.Tensor] = None,
1330
+ inputs_embeds: Optional[torch.Tensor] = None,
1331
+ labels: Optional[torch.Tensor] = None,
1332
+ use_cache: Optional[bool] = None,
1333
+ output_attentions: Optional[bool] = None,
1334
+ output_hidden_states: Optional[bool] = None,
1335
+ return_dict: Optional[bool] = None,
1336
+ **deprecated_arguments,
1337
+ ) -> Union[Tuple[torch.Tensor], TokenClassifierOutput]:
1338
+ r"""
1339
+ labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1340
+ Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
1341
+ config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
1342
+ `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
1343
+ """
1344
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1345
+
1346
+ model_outputs = self.model(
1347
+ input_ids,
1348
+ past_key_values=past_key_values,
1349
+ attention_mask=attention_mask,
1350
+ inputs_embeds=inputs_embeds,
1351
+ use_cache=use_cache,
1352
+ output_attentions=output_attentions,
1353
+ output_hidden_states=output_hidden_states,
1354
+ return_dict=return_dict,
1355
+ )
1356
+
1357
+ hidden_states = model_outputs[0]
1358
+ hidden_states = self.dropout(hidden_states)
1359
+ logits = self.classifier(hidden_states)
1360
+
1361
+ loss = None
1362
+ if labels is not None:
1363
+ # move labels to correct device to enable model parallelism
1364
+ labels = labels.to(logits.device)
1365
+ batch_size, seq_length = labels.shape
1366
+ loss_fct = CrossEntropyLoss()
1367
+ loss = loss_fct(
1368
+ logits.view(batch_size * seq_length, self.num_labels), labels.view(batch_size * seq_length)
1369
+ )
1370
+
1371
+ if not return_dict:
1372
+ output = (logits,) + model_outputs[2:]
1373
+ return ((loss,) + output) if loss is not None else output
1374
+
1375
+ return TokenClassifierOutput(
1376
+ loss=loss,
1377
+ logits=logits,
1378
+ hidden_states=model_outputs.hidden_states,
1379
+ attentions=model_outputs.attentions,
1380
+ )