AbstractPerspective commited on
Commit
5fc51c6
1 Parent(s): d948d68

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,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/kaggle/working/merge",
3
+ "activation_function": "gelu_new",
4
+ "architectures": [
5
+ "PhiForCausalLM"
6
+ ],
7
+ "attn_pdrop": 0.0,
8
+ "auto_map": {
9
+ "AutoConfig": "configuration_phi.PhiConfig",
10
+ "AutoModelForCausalLM": "modeling_phi.PhiForCausalLM"
11
+ },
12
+ "embd_pdrop": 0.0,
13
+ "flash_attn": false,
14
+ "flash_rotary": false,
15
+ "fused_dense": false,
16
+ "img_processor": null,
17
+ "initializer_range": 0.02,
18
+ "layer_norm_epsilon": 1e-05,
19
+ "model_type": "phi-msft",
20
+ "n_embd": 2560,
21
+ "n_head": 32,
22
+ "n_head_kv": null,
23
+ "n_inner": null,
24
+ "n_layer": 32,
25
+ "n_positions": 2048,
26
+ "num_experts_per_tok": 1,
27
+ "num_local_experts": 2,
28
+ "resid_pdrop": 0.1,
29
+ "rotary_dim": 32,
30
+ "tie_word_embeddings": false,
31
+ "torch_dtype": "float16",
32
+ "transformers_version": "4.37.0",
33
+ "vocab_size": 51200
34
+ }
configuration_phi.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Copyright (c) Microsoft Corporation.
3
+ # Licensed under the MIT license.
4
+
5
+ import math
6
+ from typing import Optional
7
+
8
+ from transformers import PretrainedConfig
9
+
10
+
11
+ class PhiConfig(PretrainedConfig):
12
+ #Phi configuration.
13
+
14
+ model_type = "phi-msft"
15
+ attribute_map = {
16
+ "max_position_embeddings": "n_positions",
17
+ "hidden_size": "n_embd",
18
+ "num_attention_heads": "n_head",
19
+ "num_hidden_layers": "n_layer",
20
+ }
21
+
22
+ def __init__(
23
+ self,
24
+ vocab_size: int = 50304,
25
+ n_positions: int = 2048,
26
+ n_embd: int = 1024,
27
+ n_layer: int = 20,
28
+ n_inner: Optional[int] = None,
29
+ n_head: int = 16,
30
+ n_head_kv: Optional[int] = None,
31
+ num_experts_per_tok: int = 2,
32
+ num_local_experts: int = 4,
33
+ rotary_dim: Optional[int] = 32,
34
+ activation_function: Optional[str] = "gelu_new",
35
+ flash_attn: bool = False,
36
+ flash_rotary: bool = False,
37
+ fused_dense: bool = False,
38
+ attn_pdrop: float = 0.0,
39
+ embd_pdrop: float = 0.0,
40
+ resid_pdrop: float = 0.0,
41
+ layer_norm_epsilon: float = 1e-5,
42
+ initializer_range: float = 0.02,
43
+ tie_word_embeddings: bool = False,
44
+ pad_vocab_size_multiple: int = 64,
45
+ **kwargs
46
+ ) -> None:
47
+ self.vocab_size = int(math.ceil(vocab_size / pad_vocab_size_multiple) * pad_vocab_size_multiple)
48
+ self.n_positions = n_positions
49
+ self.n_embd = n_embd
50
+ self.n_layer = n_layer
51
+ self.n_inner = n_inner
52
+ self.n_head = n_head
53
+ self.n_head_kv = n_head_kv
54
+ self.num_experts_per_tok = num_experts_per_tok
55
+ self.num_local_experts = num_local_experts
56
+ self.rotary_dim = min(rotary_dim, n_embd // n_head)
57
+ self.activation_function = activation_function
58
+ self.flash_attn = flash_attn
59
+ self.flash_rotary = flash_rotary
60
+ self.fused_dense = fused_dense
61
+ self.attn_pdrop = attn_pdrop
62
+ self.embd_pdrop = embd_pdrop
63
+ self.resid_pdrop = resid_pdrop
64
+ self.layer_norm_epsilon = layer_norm_epsilon
65
+ self.initializer_range = initializer_range
66
+
67
+ super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
generation_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "transformers_version": "4.37.0"
4
+ }
model-00001-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c130aefdc0ad3b05fb2253793957a4ad31de29075deac39e8aaa97f665e033a9
3
+ size 4982434792
model-00002-of-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0c310edd320ad55051c9867ede0b68b15d1997644830e02715392ded7887ec1f
3
+ size 3933575752
model.safetensors.index.json ADDED
@@ -0,0 +1,492 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 8915957760
4
+ },
5
+ "weight_map": {
6
+ "lm_head.linear.bias": "model-00002-of-00002.safetensors",
7
+ "lm_head.linear.weight": "model-00002-of-00002.safetensors",
8
+ "lm_head.ln.bias": "model-00002-of-00002.safetensors",
9
+ "lm_head.ln.weight": "model-00002-of-00002.safetensors",
10
+ "transformer.embd.wte.weight": "model-00001-of-00002.safetensors",
11
+ "transformer.h.0.ln.bias": "model-00001-of-00002.safetensors",
12
+ "transformer.h.0.ln.weight": "model-00001-of-00002.safetensors",
13
+ "transformer.h.0.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
14
+ "transformer.h.0.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
15
+ "transformer.h.0.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
16
+ "transformer.h.0.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
17
+ "transformer.h.0.moe.gate.weight": "model-00001-of-00002.safetensors",
18
+ "transformer.h.0.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
19
+ "transformer.h.0.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
20
+ "transformer.h.0.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
21
+ "transformer.h.0.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
22
+ "transformer.h.0.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
23
+ "transformer.h.0.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
24
+ "transformer.h.0.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
25
+ "transformer.h.0.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
26
+ "transformer.h.1.ln.bias": "model-00001-of-00002.safetensors",
27
+ "transformer.h.1.ln.weight": "model-00001-of-00002.safetensors",
28
+ "transformer.h.1.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
29
+ "transformer.h.1.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
30
+ "transformer.h.1.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
31
+ "transformer.h.1.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
32
+ "transformer.h.1.moe.gate.weight": "model-00001-of-00002.safetensors",
33
+ "transformer.h.1.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
34
+ "transformer.h.1.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
35
+ "transformer.h.1.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
36
+ "transformer.h.1.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
37
+ "transformer.h.1.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
38
+ "transformer.h.1.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
39
+ "transformer.h.1.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
40
+ "transformer.h.1.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
41
+ "transformer.h.10.ln.bias": "model-00001-of-00002.safetensors",
42
+ "transformer.h.10.ln.weight": "model-00001-of-00002.safetensors",
43
+ "transformer.h.10.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
44
+ "transformer.h.10.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
45
+ "transformer.h.10.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
46
+ "transformer.h.10.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
47
+ "transformer.h.10.moe.gate.weight": "model-00001-of-00002.safetensors",
48
+ "transformer.h.10.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
49
+ "transformer.h.10.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
50
+ "transformer.h.10.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
51
+ "transformer.h.10.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
52
+ "transformer.h.10.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
53
+ "transformer.h.10.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
54
+ "transformer.h.10.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
55
+ "transformer.h.10.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
56
+ "transformer.h.11.ln.bias": "model-00001-of-00002.safetensors",
57
+ "transformer.h.11.ln.weight": "model-00001-of-00002.safetensors",
58
+ "transformer.h.11.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
59
+ "transformer.h.11.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
60
+ "transformer.h.11.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
61
+ "transformer.h.11.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
62
+ "transformer.h.11.moe.gate.weight": "model-00001-of-00002.safetensors",
63
+ "transformer.h.11.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
64
+ "transformer.h.11.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
65
+ "transformer.h.11.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
66
+ "transformer.h.11.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
67
+ "transformer.h.11.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
68
+ "transformer.h.11.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
69
+ "transformer.h.11.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
70
+ "transformer.h.11.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
71
+ "transformer.h.12.ln.bias": "model-00001-of-00002.safetensors",
72
+ "transformer.h.12.ln.weight": "model-00001-of-00002.safetensors",
73
+ "transformer.h.12.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
74
+ "transformer.h.12.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
75
+ "transformer.h.12.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
76
+ "transformer.h.12.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
77
+ "transformer.h.12.moe.gate.weight": "model-00001-of-00002.safetensors",
78
+ "transformer.h.12.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
79
+ "transformer.h.12.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
80
+ "transformer.h.12.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
81
+ "transformer.h.12.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
82
+ "transformer.h.12.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
83
+ "transformer.h.12.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
84
+ "transformer.h.12.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
85
+ "transformer.h.12.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
86
+ "transformer.h.13.ln.bias": "model-00001-of-00002.safetensors",
87
+ "transformer.h.13.ln.weight": "model-00001-of-00002.safetensors",
88
+ "transformer.h.13.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
89
+ "transformer.h.13.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
90
+ "transformer.h.13.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
91
+ "transformer.h.13.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
92
+ "transformer.h.13.moe.gate.weight": "model-00001-of-00002.safetensors",
93
+ "transformer.h.13.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
94
+ "transformer.h.13.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
95
+ "transformer.h.13.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
96
+ "transformer.h.13.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
97
+ "transformer.h.13.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
98
+ "transformer.h.13.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
99
+ "transformer.h.13.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
100
+ "transformer.h.13.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
101
+ "transformer.h.14.ln.bias": "model-00001-of-00002.safetensors",
102
+ "transformer.h.14.ln.weight": "model-00001-of-00002.safetensors",
103
+ "transformer.h.14.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
104
+ "transformer.h.14.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
105
+ "transformer.h.14.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
106
+ "transformer.h.14.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
107
+ "transformer.h.14.moe.gate.weight": "model-00001-of-00002.safetensors",
108
+ "transformer.h.14.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
109
+ "transformer.h.14.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
110
+ "transformer.h.14.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
111
+ "transformer.h.14.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
112
+ "transformer.h.14.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
113
+ "transformer.h.14.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
114
+ "transformer.h.14.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
115
+ "transformer.h.14.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
116
+ "transformer.h.15.ln.bias": "model-00001-of-00002.safetensors",
117
+ "transformer.h.15.ln.weight": "model-00001-of-00002.safetensors",
118
+ "transformer.h.15.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
119
+ "transformer.h.15.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
120
+ "transformer.h.15.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
121
+ "transformer.h.15.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
122
+ "transformer.h.15.moe.gate.weight": "model-00001-of-00002.safetensors",
123
+ "transformer.h.15.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
124
+ "transformer.h.15.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
125
+ "transformer.h.15.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
126
+ "transformer.h.15.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
127
+ "transformer.h.15.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
128
+ "transformer.h.15.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
129
+ "transformer.h.15.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
130
+ "transformer.h.15.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
131
+ "transformer.h.16.ln.bias": "model-00001-of-00002.safetensors",
132
+ "transformer.h.16.ln.weight": "model-00001-of-00002.safetensors",
133
+ "transformer.h.16.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
134
+ "transformer.h.16.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
135
+ "transformer.h.16.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
136
+ "transformer.h.16.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
137
+ "transformer.h.16.moe.gate.weight": "model-00001-of-00002.safetensors",
138
+ "transformer.h.16.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
139
+ "transformer.h.16.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
140
+ "transformer.h.16.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
141
+ "transformer.h.16.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
142
+ "transformer.h.16.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
143
+ "transformer.h.16.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
144
+ "transformer.h.16.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
145
+ "transformer.h.16.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
146
+ "transformer.h.17.ln.bias": "model-00001-of-00002.safetensors",
147
+ "transformer.h.17.ln.weight": "model-00001-of-00002.safetensors",
148
+ "transformer.h.17.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
149
+ "transformer.h.17.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
150
+ "transformer.h.17.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
151
+ "transformer.h.17.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
152
+ "transformer.h.17.moe.gate.weight": "model-00001-of-00002.safetensors",
153
+ "transformer.h.17.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
154
+ "transformer.h.17.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
155
+ "transformer.h.17.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
156
+ "transformer.h.17.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
157
+ "transformer.h.17.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
158
+ "transformer.h.17.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
159
+ "transformer.h.17.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
160
+ "transformer.h.17.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
161
+ "transformer.h.18.ln.bias": "model-00001-of-00002.safetensors",
162
+ "transformer.h.18.ln.weight": "model-00001-of-00002.safetensors",
163
+ "transformer.h.18.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
164
+ "transformer.h.18.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
165
+ "transformer.h.18.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
166
+ "transformer.h.18.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
167
+ "transformer.h.18.moe.gate.weight": "model-00002-of-00002.safetensors",
168
+ "transformer.h.18.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
169
+ "transformer.h.18.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
170
+ "transformer.h.18.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
171
+ "transformer.h.18.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
172
+ "transformer.h.18.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
173
+ "transformer.h.18.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
174
+ "transformer.h.18.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
175
+ "transformer.h.18.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
176
+ "transformer.h.19.ln.bias": "model-00002-of-00002.safetensors",
177
+ "transformer.h.19.ln.weight": "model-00002-of-00002.safetensors",
178
+ "transformer.h.19.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
179
+ "transformer.h.19.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
180
+ "transformer.h.19.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
181
+ "transformer.h.19.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
182
+ "transformer.h.19.moe.gate.weight": "model-00002-of-00002.safetensors",
183
+ "transformer.h.19.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
184
+ "transformer.h.19.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
185
+ "transformer.h.19.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
186
+ "transformer.h.19.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
187
+ "transformer.h.19.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
188
+ "transformer.h.19.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
189
+ "transformer.h.19.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
190
+ "transformer.h.19.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
191
+ "transformer.h.2.ln.bias": "model-00001-of-00002.safetensors",
192
+ "transformer.h.2.ln.weight": "model-00001-of-00002.safetensors",
193
+ "transformer.h.2.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
194
+ "transformer.h.2.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
195
+ "transformer.h.2.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
196
+ "transformer.h.2.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
197
+ "transformer.h.2.moe.gate.weight": "model-00001-of-00002.safetensors",
198
+ "transformer.h.2.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
199
+ "transformer.h.2.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
200
+ "transformer.h.2.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
201
+ "transformer.h.2.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
202
+ "transformer.h.2.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
203
+ "transformer.h.2.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
204
+ "transformer.h.2.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
205
+ "transformer.h.2.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
206
+ "transformer.h.20.ln.bias": "model-00002-of-00002.safetensors",
207
+ "transformer.h.20.ln.weight": "model-00002-of-00002.safetensors",
208
+ "transformer.h.20.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
209
+ "transformer.h.20.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
210
+ "transformer.h.20.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
211
+ "transformer.h.20.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
212
+ "transformer.h.20.moe.gate.weight": "model-00002-of-00002.safetensors",
213
+ "transformer.h.20.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
214
+ "transformer.h.20.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
215
+ "transformer.h.20.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
216
+ "transformer.h.20.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
217
+ "transformer.h.20.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
218
+ "transformer.h.20.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
219
+ "transformer.h.20.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
220
+ "transformer.h.20.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
221
+ "transformer.h.21.ln.bias": "model-00002-of-00002.safetensors",
222
+ "transformer.h.21.ln.weight": "model-00002-of-00002.safetensors",
223
+ "transformer.h.21.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
224
+ "transformer.h.21.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
225
+ "transformer.h.21.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
226
+ "transformer.h.21.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
227
+ "transformer.h.21.moe.gate.weight": "model-00002-of-00002.safetensors",
228
+ "transformer.h.21.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
229
+ "transformer.h.21.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
230
+ "transformer.h.21.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
231
+ "transformer.h.21.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
232
+ "transformer.h.21.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
233
+ "transformer.h.21.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
234
+ "transformer.h.21.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
235
+ "transformer.h.21.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
236
+ "transformer.h.22.ln.bias": "model-00002-of-00002.safetensors",
237
+ "transformer.h.22.ln.weight": "model-00002-of-00002.safetensors",
238
+ "transformer.h.22.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
239
+ "transformer.h.22.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
240
+ "transformer.h.22.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
241
+ "transformer.h.22.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
242
+ "transformer.h.22.moe.gate.weight": "model-00002-of-00002.safetensors",
243
+ "transformer.h.22.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
244
+ "transformer.h.22.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
245
+ "transformer.h.22.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
246
+ "transformer.h.22.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
247
+ "transformer.h.22.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
248
+ "transformer.h.22.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
249
+ "transformer.h.22.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
250
+ "transformer.h.22.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
251
+ "transformer.h.23.ln.bias": "model-00002-of-00002.safetensors",
252
+ "transformer.h.23.ln.weight": "model-00002-of-00002.safetensors",
253
+ "transformer.h.23.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
254
+ "transformer.h.23.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
255
+ "transformer.h.23.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
256
+ "transformer.h.23.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
257
+ "transformer.h.23.moe.gate.weight": "model-00002-of-00002.safetensors",
258
+ "transformer.h.23.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
259
+ "transformer.h.23.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
260
+ "transformer.h.23.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
261
+ "transformer.h.23.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
262
+ "transformer.h.23.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
263
+ "transformer.h.23.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
264
+ "transformer.h.23.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
265
+ "transformer.h.23.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
266
+ "transformer.h.24.ln.bias": "model-00002-of-00002.safetensors",
267
+ "transformer.h.24.ln.weight": "model-00002-of-00002.safetensors",
268
+ "transformer.h.24.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
269
+ "transformer.h.24.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
270
+ "transformer.h.24.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
271
+ "transformer.h.24.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
272
+ "transformer.h.24.moe.gate.weight": "model-00002-of-00002.safetensors",
273
+ "transformer.h.24.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
274
+ "transformer.h.24.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
275
+ "transformer.h.24.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
276
+ "transformer.h.24.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
277
+ "transformer.h.24.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
278
+ "transformer.h.24.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
279
+ "transformer.h.24.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
280
+ "transformer.h.24.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
281
+ "transformer.h.25.ln.bias": "model-00002-of-00002.safetensors",
282
+ "transformer.h.25.ln.weight": "model-00002-of-00002.safetensors",
283
+ "transformer.h.25.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
284
+ "transformer.h.25.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
285
+ "transformer.h.25.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
286
+ "transformer.h.25.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
287
+ "transformer.h.25.moe.gate.weight": "model-00002-of-00002.safetensors",
288
+ "transformer.h.25.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
289
+ "transformer.h.25.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
290
+ "transformer.h.25.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
291
+ "transformer.h.25.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
292
+ "transformer.h.25.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
293
+ "transformer.h.25.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
294
+ "transformer.h.25.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
295
+ "transformer.h.25.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
296
+ "transformer.h.26.ln.bias": "model-00002-of-00002.safetensors",
297
+ "transformer.h.26.ln.weight": "model-00002-of-00002.safetensors",
298
+ "transformer.h.26.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
299
+ "transformer.h.26.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
300
+ "transformer.h.26.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
301
+ "transformer.h.26.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
302
+ "transformer.h.26.moe.gate.weight": "model-00002-of-00002.safetensors",
303
+ "transformer.h.26.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
304
+ "transformer.h.26.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
305
+ "transformer.h.26.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
306
+ "transformer.h.26.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
307
+ "transformer.h.26.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
308
+ "transformer.h.26.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
309
+ "transformer.h.26.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
310
+ "transformer.h.26.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
311
+ "transformer.h.27.ln.bias": "model-00002-of-00002.safetensors",
312
+ "transformer.h.27.ln.weight": "model-00002-of-00002.safetensors",
313
+ "transformer.h.27.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
314
+ "transformer.h.27.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
315
+ "transformer.h.27.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
316
+ "transformer.h.27.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
317
+ "transformer.h.27.moe.gate.weight": "model-00002-of-00002.safetensors",
318
+ "transformer.h.27.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
319
+ "transformer.h.27.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
320
+ "transformer.h.27.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
321
+ "transformer.h.27.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
322
+ "transformer.h.27.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
323
+ "transformer.h.27.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
324
+ "transformer.h.27.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
325
+ "transformer.h.27.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
326
+ "transformer.h.28.ln.bias": "model-00002-of-00002.safetensors",
327
+ "transformer.h.28.ln.weight": "model-00002-of-00002.safetensors",
328
+ "transformer.h.28.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
329
+ "transformer.h.28.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
330
+ "transformer.h.28.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
331
+ "transformer.h.28.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
332
+ "transformer.h.28.moe.gate.weight": "model-00002-of-00002.safetensors",
333
+ "transformer.h.28.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
334
+ "transformer.h.28.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
335
+ "transformer.h.28.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
336
+ "transformer.h.28.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
337
+ "transformer.h.28.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
338
+ "transformer.h.28.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
339
+ "transformer.h.28.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
340
+ "transformer.h.28.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
341
+ "transformer.h.29.ln.bias": "model-00002-of-00002.safetensors",
342
+ "transformer.h.29.ln.weight": "model-00002-of-00002.safetensors",
343
+ "transformer.h.29.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
344
+ "transformer.h.29.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
345
+ "transformer.h.29.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
346
+ "transformer.h.29.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
347
+ "transformer.h.29.moe.gate.weight": "model-00002-of-00002.safetensors",
348
+ "transformer.h.29.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
349
+ "transformer.h.29.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
350
+ "transformer.h.29.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
351
+ "transformer.h.29.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
352
+ "transformer.h.29.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
353
+ "transformer.h.29.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
354
+ "transformer.h.29.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
355
+ "transformer.h.29.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
356
+ "transformer.h.3.ln.bias": "model-00001-of-00002.safetensors",
357
+ "transformer.h.3.ln.weight": "model-00001-of-00002.safetensors",
358
+ "transformer.h.3.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
359
+ "transformer.h.3.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
360
+ "transformer.h.3.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
361
+ "transformer.h.3.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
362
+ "transformer.h.3.moe.gate.weight": "model-00001-of-00002.safetensors",
363
+ "transformer.h.3.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
364
+ "transformer.h.3.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
365
+ "transformer.h.3.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
366
+ "transformer.h.3.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
367
+ "transformer.h.3.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
368
+ "transformer.h.3.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
369
+ "transformer.h.3.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
370
+ "transformer.h.3.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
371
+ "transformer.h.30.ln.bias": "model-00002-of-00002.safetensors",
372
+ "transformer.h.30.ln.weight": "model-00002-of-00002.safetensors",
373
+ "transformer.h.30.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
374
+ "transformer.h.30.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
375
+ "transformer.h.30.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
376
+ "transformer.h.30.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
377
+ "transformer.h.30.moe.gate.weight": "model-00002-of-00002.safetensors",
378
+ "transformer.h.30.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
379
+ "transformer.h.30.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
380
+ "transformer.h.30.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
381
+ "transformer.h.30.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
382
+ "transformer.h.30.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
383
+ "transformer.h.30.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
384
+ "transformer.h.30.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
385
+ "transformer.h.30.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
386
+ "transformer.h.31.ln.bias": "model-00002-of-00002.safetensors",
387
+ "transformer.h.31.ln.weight": "model-00002-of-00002.safetensors",
388
+ "transformer.h.31.mixer.Wqkv.bias": "model-00002-of-00002.safetensors",
389
+ "transformer.h.31.mixer.Wqkv.weight": "model-00002-of-00002.safetensors",
390
+ "transformer.h.31.mixer.out_proj.bias": "model-00002-of-00002.safetensors",
391
+ "transformer.h.31.mixer.out_proj.weight": "model-00002-of-00002.safetensors",
392
+ "transformer.h.31.moe.gate.weight": "model-00002-of-00002.safetensors",
393
+ "transformer.h.31.moe.mlp.0.fc1.bias": "model-00002-of-00002.safetensors",
394
+ "transformer.h.31.moe.mlp.0.fc1.weight": "model-00002-of-00002.safetensors",
395
+ "transformer.h.31.moe.mlp.0.fc2.bias": "model-00002-of-00002.safetensors",
396
+ "transformer.h.31.moe.mlp.0.fc2.weight": "model-00002-of-00002.safetensors",
397
+ "transformer.h.31.moe.mlp.1.fc1.bias": "model-00002-of-00002.safetensors",
398
+ "transformer.h.31.moe.mlp.1.fc1.weight": "model-00002-of-00002.safetensors",
399
+ "transformer.h.31.moe.mlp.1.fc2.bias": "model-00002-of-00002.safetensors",
400
+ "transformer.h.31.moe.mlp.1.fc2.weight": "model-00002-of-00002.safetensors",
401
+ "transformer.h.4.ln.bias": "model-00001-of-00002.safetensors",
402
+ "transformer.h.4.ln.weight": "model-00001-of-00002.safetensors",
403
+ "transformer.h.4.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
404
+ "transformer.h.4.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
405
+ "transformer.h.4.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
406
+ "transformer.h.4.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
407
+ "transformer.h.4.moe.gate.weight": "model-00001-of-00002.safetensors",
408
+ "transformer.h.4.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
409
+ "transformer.h.4.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
410
+ "transformer.h.4.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
411
+ "transformer.h.4.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
412
+ "transformer.h.4.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
413
+ "transformer.h.4.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
414
+ "transformer.h.4.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
415
+ "transformer.h.4.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
416
+ "transformer.h.5.ln.bias": "model-00001-of-00002.safetensors",
417
+ "transformer.h.5.ln.weight": "model-00001-of-00002.safetensors",
418
+ "transformer.h.5.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
419
+ "transformer.h.5.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
420
+ "transformer.h.5.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
421
+ "transformer.h.5.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
422
+ "transformer.h.5.moe.gate.weight": "model-00001-of-00002.safetensors",
423
+ "transformer.h.5.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
424
+ "transformer.h.5.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
425
+ "transformer.h.5.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
426
+ "transformer.h.5.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
427
+ "transformer.h.5.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
428
+ "transformer.h.5.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
429
+ "transformer.h.5.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
430
+ "transformer.h.5.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
431
+ "transformer.h.6.ln.bias": "model-00001-of-00002.safetensors",
432
+ "transformer.h.6.ln.weight": "model-00001-of-00002.safetensors",
433
+ "transformer.h.6.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
434
+ "transformer.h.6.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
435
+ "transformer.h.6.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
436
+ "transformer.h.6.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
437
+ "transformer.h.6.moe.gate.weight": "model-00001-of-00002.safetensors",
438
+ "transformer.h.6.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
439
+ "transformer.h.6.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
440
+ "transformer.h.6.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
441
+ "transformer.h.6.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
442
+ "transformer.h.6.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
443
+ "transformer.h.6.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
444
+ "transformer.h.6.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
445
+ "transformer.h.6.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
446
+ "transformer.h.7.ln.bias": "model-00001-of-00002.safetensors",
447
+ "transformer.h.7.ln.weight": "model-00001-of-00002.safetensors",
448
+ "transformer.h.7.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
449
+ "transformer.h.7.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
450
+ "transformer.h.7.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
451
+ "transformer.h.7.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
452
+ "transformer.h.7.moe.gate.weight": "model-00001-of-00002.safetensors",
453
+ "transformer.h.7.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
454
+ "transformer.h.7.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
455
+ "transformer.h.7.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
456
+ "transformer.h.7.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
457
+ "transformer.h.7.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
458
+ "transformer.h.7.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
459
+ "transformer.h.7.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
460
+ "transformer.h.7.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
461
+ "transformer.h.8.ln.bias": "model-00001-of-00002.safetensors",
462
+ "transformer.h.8.ln.weight": "model-00001-of-00002.safetensors",
463
+ "transformer.h.8.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
464
+ "transformer.h.8.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
465
+ "transformer.h.8.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
466
+ "transformer.h.8.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
467
+ "transformer.h.8.moe.gate.weight": "model-00001-of-00002.safetensors",
468
+ "transformer.h.8.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
469
+ "transformer.h.8.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
470
+ "transformer.h.8.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
471
+ "transformer.h.8.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
472
+ "transformer.h.8.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
473
+ "transformer.h.8.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
474
+ "transformer.h.8.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
475
+ "transformer.h.8.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors",
476
+ "transformer.h.9.ln.bias": "model-00001-of-00002.safetensors",
477
+ "transformer.h.9.ln.weight": "model-00001-of-00002.safetensors",
478
+ "transformer.h.9.mixer.Wqkv.bias": "model-00001-of-00002.safetensors",
479
+ "transformer.h.9.mixer.Wqkv.weight": "model-00001-of-00002.safetensors",
480
+ "transformer.h.9.mixer.out_proj.bias": "model-00001-of-00002.safetensors",
481
+ "transformer.h.9.mixer.out_proj.weight": "model-00001-of-00002.safetensors",
482
+ "transformer.h.9.moe.gate.weight": "model-00001-of-00002.safetensors",
483
+ "transformer.h.9.moe.mlp.0.fc1.bias": "model-00001-of-00002.safetensors",
484
+ "transformer.h.9.moe.mlp.0.fc1.weight": "model-00001-of-00002.safetensors",
485
+ "transformer.h.9.moe.mlp.0.fc2.bias": "model-00001-of-00002.safetensors",
486
+ "transformer.h.9.moe.mlp.0.fc2.weight": "model-00001-of-00002.safetensors",
487
+ "transformer.h.9.moe.mlp.1.fc1.bias": "model-00001-of-00002.safetensors",
488
+ "transformer.h.9.moe.mlp.1.fc1.weight": "model-00001-of-00002.safetensors",
489
+ "transformer.h.9.moe.mlp.1.fc2.bias": "model-00001-of-00002.safetensors",
490
+ "transformer.h.9.moe.mlp.1.fc2.weight": "model-00001-of-00002.safetensors"
491
+ }
492
+ }
modeling_phi.py ADDED
@@ -0,0 +1,963 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Copyright (c) Microsoft Corporation.
3
+ # Licensed under the MIT license.
4
+ #
5
+ # Copyright (c) 2022, Tri Dao, [email protected].
6
+ # Licensed under the BSD 3-Clause License.
7
+
8
+ from __future__ import annotations
9
+
10
+ import math
11
+ from dataclasses import dataclass, field
12
+ from typing import Any, Dict, Optional, Tuple, Union
13
+
14
+ import torch
15
+ import torch.nn as nn
16
+ from einops import rearrange, repeat
17
+ from transformers import PretrainedConfig, PreTrainedModel
18
+ from transformers.activations import ACT2FN
19
+ from transformers.modeling_outputs import CausalLMOutputWithPast
20
+
21
+ from .configuration_phi import PhiConfig
22
+
23
+ try:
24
+ from flash_attn.bert_padding import pad_input, unpad_input
25
+ from flash_attn.layers.rotary import RotaryEmbedding as FlashRotaryEmbedding
26
+ from flash_attn.modules.mha import FlashCrossAttention, FlashSelfAttention
27
+ from flash_attn.ops.fused_dense import FusedDense
28
+ except:
29
+ pad_input, unpad_input = None, None
30
+ FlashRotaryEmbedding = None
31
+ FlashSelfAttention, FlashCrossAttention = None, None
32
+ FusedDense = None
33
+
34
+
35
+ @dataclass
36
+ class InferenceParams:
37
+ #Inference parameters passed to model to efficiently calculate
38
+ #and store context during inference.
39
+ #Reference:
40
+ # https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/utils/generation.py.
41
+ #Args:
42
+ # max_seqlen: Maximum sequence length.
43
+ # max_batch_size: Maximum batch size.
44
+ # seqlen_offset: Sequence length offset.
45
+ # batch_size_offset: Batch size offset.
46
+ # key_value_memory_dict: Key value memory dictionary.
47
+ # lengths_per_sample: Lengths per sample.
48
+
49
+ max_seqlen: int = field(metadata={"help": "Maximum sequence length."})
50
+
51
+ max_batch_size: int = field(metadata={"help": "Maximum batch size."})
52
+
53
+ seqlen_offset: int = field(default=0, metadata={"help": "Sequence length offset."})
54
+
55
+ batch_size_offset: int = field(default=0, metadata={"help": "Batch size offset."})
56
+
57
+ key_value_memory_dict: Dict[str, Any] = field(
58
+ default_factory=dict, metadata={"help": "Key value memory dictionary."}
59
+ )
60
+
61
+ lengths_per_sample: torch.Tensor = field(default=None, metadata={"help": "Lengths per sample."})
62
+
63
+
64
+ class Embedding(nn.Module):
65
+ #Token embedding with dropout.
66
+
67
+ def __init__(self, config: PretrainedConfig) -> None:
68
+ super().__init__()
69
+
70
+ self.wte = nn.Embedding(config.vocab_size, config.n_embd)
71
+ self.drop = nn.Dropout(config.embd_pdrop)
72
+
73
+ def forward(self, input_ids: torch.LongTensor) -> torch.FloatTensor:
74
+ input_shape = input_ids.size()
75
+ input_ids = input_ids.view(-1, input_shape[-1])
76
+
77
+ hidden_states = self.wte(input_ids)
78
+ hidden_states = self.drop(hidden_states)
79
+
80
+ return hidden_states
81
+
82
+
83
+ def _apply_rotary_emb(
84
+ x: torch.FloatTensor,
85
+ cos: torch.FloatTensor,
86
+ sin: torch.FloatTensor,
87
+ ) -> torch.FloatTensor:
88
+ _, seqlen, _, _ = x.shape
89
+ _, rotary_dim = cos.shape
90
+ rotary_dim *= 2
91
+
92
+ x_rot = x[:, :, :, :rotary_dim]
93
+ x_pass = x[:, :, :, rotary_dim:]
94
+
95
+ x1, x2 = x_rot.chunk(2, dim=-1)
96
+ c, s = rearrange(cos[:seqlen], "s d -> s 1 d"), rearrange(sin[:seqlen], "s d -> s 1 d")
97
+ x1, x2, c, s = [t.to(dtype=torch.float32) for t in [x1, x2, c, s]]
98
+
99
+ x_rot = torch.cat([x1 * c - x2 * s, x1 * s + x2 * c], axis=-1).to(x.dtype)
100
+
101
+ return torch.cat([x_rot, x_pass], axis=-1)
102
+
103
+
104
+ def _apply_rotary_emb_kv(
105
+ kv: torch.FloatTensor,
106
+ cos: torch.FloatTensor,
107
+ sin: torch.FloatTensor,
108
+ cos_k: Optional[torch.FloatTensor] = None,
109
+ sin_k: Optional[torch.FloatTensor] = None,
110
+ ) -> torch.FloatTensor:
111
+ _, seqlen, _, _, _ = kv.shape
112
+ _, rotary_dim = cos.shape
113
+ rotary_dim *= 2
114
+
115
+ k_rot = kv[:, :, 0, :, :rotary_dim]
116
+ k_pass = kv[:, :, 0, :, rotary_dim:]
117
+
118
+ k1, k2 = k_rot.chunk(2, dim=-1)
119
+ c, s = rearrange(cos[:seqlen], "s d -> s 1 d"), rearrange(sin[:seqlen], "s d -> s 1 d")
120
+ k1, k2, c, s = [t.to(dtype=torch.float32) for t in [k1, k2, c, s]]
121
+
122
+ k_rot = torch.cat([k1 * c - k2 * s, k1 * s + k2 * c], axis=-1).to(kv.dtype)
123
+
124
+ return torch.cat(
125
+ [
126
+ torch.cat([k_rot, k_pass], axis=-1).unsqueeze(2),
127
+ kv[:, :, 1:2, :, :],
128
+ ],
129
+ axis=2,
130
+ )
131
+
132
+
133
+ def _apply_rotary_emb_qkv(
134
+ qkv: torch.FloatTensor,
135
+ cos: torch.FloatTensor,
136
+ sin: torch.FloatTensor,
137
+ cos_k: Optional[torch.FloatTensor] = None,
138
+ sin_k: Optional[torch.FloatTensor] = None,
139
+ ) -> torch.FloatTensor:
140
+ _, seqlen, _, _, _ = qkv.shape
141
+ _, rotary_dim = cos.shape
142
+ rotary_dim *= 2
143
+
144
+ q_rot = qkv[:, :, 0, :, :rotary_dim]
145
+ q_pass = qkv[:, :, 0, :, rotary_dim:]
146
+
147
+ k_rot = qkv[:, :, 1, :, :rotary_dim]
148
+ k_pass = qkv[:, :, 1, :, rotary_dim:]
149
+
150
+ q1, q2 = q_rot.chunk(2, dim=-1)
151
+ k1, k2 = k_rot.chunk(2, dim=-1)
152
+ c, s = rearrange(cos[:seqlen], "s d -> s 1 d"), rearrange(sin[:seqlen], "s d -> s 1 d")
153
+ q1, q2, k1, k2, c, s = [t.to(dtype=torch.float32) for t in [q1, q2, k1, k2, c, s]]
154
+
155
+ q_rot = torch.cat([q1 * c - q2 * s, q1 * s + q2 * c], axis=-1).to(qkv.dtype)
156
+ k_rot = torch.cat([k1 * c - k2 * s, k1 * s + k2 * c], axis=-1).to(qkv.dtype)
157
+
158
+ return torch.cat(
159
+ [
160
+ torch.cat([q_rot, q_pass], axis=-1).unsqueeze(2),
161
+ torch.cat([k_rot, k_pass], axis=-1).unsqueeze(2),
162
+ qkv[:, :, 2:3, :, :],
163
+ ],
164
+ axis=2,
165
+ )
166
+
167
+
168
+ class RotaryEmbedding(nn.Module):
169
+ #Rotary positional embedding (RoPE).
170
+ #Reference:
171
+ # RoFormer: Enhanced Transformer with Rotary Position Embedding.
172
+ # https://arxiv.org/pdf/2104.09864.pdf.
173
+
174
+ def __init__(
175
+ self,
176
+ dim: int,
177
+ base: int = 10000,
178
+ scale_base: Optional[float] = None,
179
+ pos_idx_in_fp32: bool = True,
180
+ max_position_embeddings: int = 2048,
181
+ device: Optional[str] = None,
182
+ **kwargs,
183
+ ) -> None:
184
+ super().__init__()
185
+
186
+ if scale_base is not None:
187
+ raise NotImplementedError
188
+
189
+ self.dim = dim
190
+ self.base = float(base)
191
+ self.scale_base = scale_base
192
+ self.pos_idx_in_fp32 = pos_idx_in_fp32
193
+ self.max_position_embeddings = max_position_embeddings
194
+ self.device = device
195
+
196
+ # Generate and save the inverse frequency buffer (non-trainable)
197
+ inv_freq = self._compute_inv_freq(device)
198
+ self.register_buffer("inv_freq", inv_freq, persistent=False)
199
+
200
+ # Generate and save the scale buffer (non-trainable)
201
+ scale = (
202
+ (torch.arange(0, dim, 2, device=device, dtype=torch.float32) + 0.4 * dim) / (1.4 * dim)
203
+ if scale_base is not None
204
+ else None
205
+ )
206
+ self.register_buffer("scale", scale, persistent=False)
207
+
208
+ # Initialize cached attributes since ONNX can't rely on dynamic initialization
209
+ self._update_cos_sin_cache(max_position_embeddings, device=device, dtype=torch.float32)
210
+
211
+ def _compute_inv_freq(self, device: Optional[str] = None) -> torch.FloatTensor:
212
+ return 1.0 / (self.base ** (torch.arange(0, self.dim, 2, device=device, dtype=torch.float32) / self.dim))
213
+
214
+ def _update_cos_sin_cache(
215
+ self,
216
+ seqlen: int,
217
+ device: Optional[str] = None,
218
+ dtype: Optional[torch.dtype] = None,
219
+ ) -> None:
220
+ self._seq_len_cached = seqlen
221
+
222
+ # fp32 is preferred since the output of `torch.arange` can be quite large
223
+ # and bf16 would lose a lot of precision
224
+ if self.pos_idx_in_fp32:
225
+ t = torch.arange(seqlen, device=device, dtype=torch.float32)
226
+ if self.inv_freq.dtype != torch.float32:
227
+ inv_freq = self._compute_inv_freq(device=device)
228
+ else:
229
+ inv_freq = self.inv_freq
230
+ else:
231
+ t = torch.arange(seqlen, device=device, dtype=self.inv_freq.dtype)
232
+ inv_freq = self.inv_freq
233
+
234
+ # `torch.outer` is preferred since `torch.einsum` converts from fp32 to fp16 if used with AMP
235
+ freqs = torch.outer(t, inv_freq)
236
+ if self.scale is None:
237
+ self._cos_cached = torch.cos(freqs).to(dtype)
238
+ self._sin_cached = torch.sin(freqs).to(dtype)
239
+ else:
240
+ power = (
241
+ torch.arange(seqlen, dtype=self.scale.dtype, device=self.scale.device) - seqlen // 2
242
+ ) / self.scale_base
243
+ scale = self.scale.to(device=power.device) ** rearrange(power, "s -> s 1")
244
+
245
+ # Force the scale multiplication to happen in fp32
246
+ self._cos_cached = (torch.cos(freqs) * scale).to(dtype)
247
+ self._sin_cached = (torch.sin(freqs) * scale).to(dtype)
248
+ self._cos_k_cached = (torch.cos(freqs) / scale).to(dtype)
249
+ self._sin_k_cached = (torch.sin(freqs) / scale).to(dtype)
250
+
251
+ def forward(
252
+ self,
253
+ qkv: torch.Tensor,
254
+ kv: Optional[torch.Tensor] = None,
255
+ seqlen_offset: int = 0,
256
+ **kwargs,
257
+ ) -> Tuple[torch.Tensor, torch.Tensor]:
258
+ if (
259
+ self._seq_len_cached < qkv.shape[1] + seqlen_offset
260
+ or self._cos_cached.device != qkv.device
261
+ or self._cos_cached.dtype != qkv.dtype
262
+ or (self.training and self._cos_cached.is_inference())
263
+ ):
264
+ self._update_cos_sin_cache(qkv.shape[1] + seqlen_offset, device=qkv.device, dtype=qkv.dtype)
265
+
266
+ if kv is None:
267
+ return _apply_rotary_emb_qkv(
268
+ qkv,
269
+ self._cos_cached[seqlen_offset:],
270
+ self._sin_cached[seqlen_offset:],
271
+ )
272
+ else:
273
+ q = _apply_rotary_emb(
274
+ qkv,
275
+ self._cos_cached[seqlen_offset:],
276
+ self._sin_cached[seqlen_offset:],
277
+ )
278
+ kv = _apply_rotary_emb_kv(
279
+ kv,
280
+ self._cos_cached[seqlen_offset:],
281
+ self._sin_cached[seqlen_offset:],
282
+ )
283
+
284
+ return q, kv
285
+
286
+
287
+ class MoE(nn.Module):
288
+ def __init__(
289
+ self,
290
+ config: PretrainedConfig,
291
+ ):
292
+ super().__init__()
293
+ self.mlp = nn.ModuleList([MLP(config) for i in range(config.num_local_experts)])
294
+ self.gate = nn.Linear(config.n_embd, config.num_local_experts, bias=False)
295
+ self.num_experts_per_tok = config.num_experts_per_tok
296
+
297
+ def forward(self, x):
298
+ orig_shape = x.shape
299
+ x = x.view(-1, x.shape[-1])
300
+
301
+ scores = self.gate(x)
302
+ expert_weights, expert_indices = torch.topk(scores, self.num_experts_per_tok, dim=-1)
303
+ expert_weights = expert_weights.softmax(dim=-1)
304
+ flat_expert_indices = expert_indices.view(-1)
305
+
306
+ x = x.repeat_interleave(self.num_experts_per_tok, dim=0)
307
+ y = torch.empty_like(x)
308
+ for i, expert in enumerate(self.mlp):
309
+ y[flat_expert_indices == i] = expert(x[flat_expert_indices == i])
310
+ y = (y.view(*expert_weights.shape, -1) * expert_weights.unsqueeze(-1)).sum(dim=1)
311
+ return y.view(*orig_shape)
312
+
313
+
314
+ class MLP(nn.Module):
315
+ #Multi-Layer Perceptron.
316
+ #Reference:
317
+ # Attention Is All You Need.
318
+ # https://arxiv.org/pdf/1706.03762.pdf.
319
+
320
+ def __init__(
321
+ self,
322
+ config: PretrainedConfig,
323
+ n_inner: Optional[int] = None,
324
+ act_fn: Optional[str] = None,
325
+ ) -> None:
326
+ super().__init__()
327
+
328
+ act_fn = config.activation_function if act_fn is None else act_fn
329
+
330
+ n_inner = getattr(config, "n_inner", None) if n_inner is None else n_inner
331
+ n_inner = n_inner if n_inner is not None else 4 * config.n_embd
332
+
333
+ self.fc1 = nn.Linear(config.n_embd, n_inner)
334
+ self.fc2 = nn.Linear(n_inner, config.n_embd)
335
+ self.act = ACT2FN[act_fn]
336
+
337
+ def forward(self, hidden_states: torch.FloatTensor) -> torch.FloatTensor:
338
+ hidden_states = self.fc1(hidden_states)
339
+ hidden_states = self.act(hidden_states)
340
+ hidden_states = self.fc2(hidden_states)
341
+
342
+ return hidden_states
343
+
344
+
345
+ class SelfAttention(nn.Module):
346
+ #Self-attention layer (compatible with PyTorch).
347
+ #Reference:
348
+ # https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/modules/mha.py.
349
+
350
+ def __init__(
351
+ self,
352
+ causal: bool = True,
353
+ softmax_scale: Optional[float] = None,
354
+ attention_dropout: float = 0.0,
355
+ ) -> None:
356
+ super().__init__()
357
+
358
+ self.causal = causal
359
+ self.softmax_scale = softmax_scale
360
+ self.drop = nn.Dropout(attention_dropout)
361
+
362
+ @torch.autocast("cpu", enabled=False)
363
+ @torch.autocast("cuda", enabled=False)
364
+ def forward(
365
+ self,
366
+ qkv: torch.FloatTensor,
367
+ causal: bool = None,
368
+ key_padding_mask: Optional[torch.BoolTensor] = None,
369
+ **kwargs,
370
+ ) -> torch.FloatTensor:
371
+ batch_size, seqlen = qkv.shape[0], qkv.shape[1]
372
+ q, k, v = qkv.unbind(dim=2)
373
+
374
+ q = q.to(torch.float32)
375
+ k = k.to(torch.float32)
376
+
377
+ causal = self.causal if causal is None else causal
378
+ softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1])
379
+
380
+ # Autocast is manually disabled to avoid `torch.einsum` performing the operation
381
+ # using float16, which might lead to overflow
382
+ scores = torch.einsum("bthd,bshd->bhts", q, k * softmax_scale)
383
+
384
+ if key_padding_mask is not None:
385
+ padding_mask = torch.full((batch_size, seqlen), -10000.0, dtype=scores.dtype, device=scores.device)
386
+ padding_mask.masked_fill_(key_padding_mask, 0.0)
387
+
388
+ scores = scores + rearrange(padding_mask, "b s -> b 1 1 s")
389
+
390
+ if causal:
391
+ causal_mask = torch.triu(torch.full((seqlen, seqlen), -10000.0, device=scores.device), 1)
392
+ scores = scores + causal_mask.to(dtype=scores.dtype)
393
+
394
+ attention = torch.softmax(scores, dim=-1).to(v.dtype)
395
+ attention = self.drop(attention)
396
+
397
+ output = torch.einsum("bhts,bshd->bthd", attention, v)
398
+
399
+ return output
400
+
401
+
402
+ class CrossAttention(nn.Module):
403
+ #Cross-attention layer (compatible with PyTorch).
404
+ #Reference:
405
+ # https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/modules/mha.py.
406
+
407
+ def __init__(
408
+ self,
409
+ causal: bool = True,
410
+ softmax_scale: Optional[float] = None,
411
+ attention_dropout: float = 0.0,
412
+ ) -> None:
413
+ super().__init__()
414
+
415
+ self.causal = causal
416
+ self.softmax_scale = softmax_scale
417
+ self.drop = nn.Dropout(attention_dropout)
418
+
419
+ @torch.autocast("cpu", enabled=False)
420
+ @torch.autocast("cuda", enabled=False)
421
+ def forward(
422
+ self,
423
+ q: torch.FloatTensor,
424
+ kv: torch.FloatTensor,
425
+ causal: bool = None,
426
+ key_padding_mask: Optional[torch.BoolTensor] = None,
427
+ **kwargs,
428
+ ) -> torch.FloatTensor:
429
+ batch_size, seqlen_q = q.shape[0], q.shape[1]
430
+ seqlen_k = kv.shape[1]
431
+
432
+ if kv.shape[3] != q.shape[2]:
433
+ kv = repeat(kv, "... hkv d -> ... (hkv g) d", g=q.shape[2] // kv.shape[3])
434
+ k, v = kv.unbind(dim=2)
435
+
436
+ q = q.to(torch.float32)
437
+ k = k.to(torch.float32)
438
+
439
+ causal = self.causal if causal is None else causal
440
+ softmax_scale = self.softmax_scale or 1.0 / math.sqrt(q.shape[-1])
441
+
442
+ # Autocast is manually disabled to avoid `torch.einsum` performing the operation
443
+ # using float16, which might lead to overflow
444
+ scores = torch.einsum("bthd,bshd->bhts", q, k * softmax_scale)
445
+
446
+ if key_padding_mask is not None:
447
+ padding_mask = torch.full(
448
+ (batch_size, seqlen_k),
449
+ -10000.0,
450
+ dtype=scores.dtype,
451
+ device=scores.device,
452
+ )
453
+ padding_mask.masked_fill_(key_padding_mask, 0.0)
454
+
455
+ scores = scores + rearrange(padding_mask, "b s -> b 1 1 s")
456
+
457
+ if causal:
458
+ rows = rearrange(torch.arange(seqlen_q, device=q.device, dtype=torch.long), "s -> s 1")
459
+ cols = torch.arange(seqlen_k, device=k.device, dtype=torch.long)
460
+ causal_mask = cols > rows + seqlen_k - seqlen_q
461
+
462
+ scores = scores.masked_fill(causal_mask, -10000.0)
463
+
464
+ attention = torch.softmax(scores, dim=-1).to(v.dtype)
465
+ attention = self.drop(attention)
466
+
467
+ output = torch.einsum("bhts,bshd->bthd", attention, v)
468
+
469
+ return output
470
+
471
+
472
+ def _find_mha_dims(
473
+ config: PretrainedConfig,
474
+ n_head: Optional[int] = None,
475
+ n_head_kv: Optional[int] = None,
476
+ head_dim: Optional[int] = None,
477
+ ) -> Tuple[int, int]:
478
+ if n_head is None and head_dim is None:
479
+ head_dim = config.n_embd // config.n_head
480
+ n_head = config.n_head
481
+ elif n_head is None or head_dim is None:
482
+ raise ValueError("`n_head` and `head_dim` must be both specified or `None`.")
483
+
484
+ if n_head_kv is None:
485
+ n_head_kv = getattr(config, "n_head_kv", None) or n_head
486
+
487
+ return n_head, n_head_kv, head_dim
488
+
489
+
490
+ def _update_kv_cache(kv: torch.FloatTensor, inference_params: InferenceParams, layer_idx: int) -> torch.FloatTensor:
491
+ num_heads, head_dim = kv.shape[-2:]
492
+
493
+ if layer_idx not in inference_params.key_value_memory_dict:
494
+ inference_params.key_value_memory_dict[layer_idx] = torch.empty(
495
+ inference_params.max_batch_size,
496
+ inference_params.max_seqlen,
497
+ 2,
498
+ num_heads,
499
+ head_dim,
500
+ dtype=kv.dtype,
501
+ device=kv.device,
502
+ )
503
+
504
+ batch_start = inference_params.batch_size_offset
505
+ batch_end = batch_start + kv.shape[0]
506
+
507
+ sequence_start = inference_params.seqlen_offset
508
+ sequence_end = sequence_start + kv.shape[1]
509
+
510
+ # When the current sequence length is equal to or larger than the maximum sequence length,
511
+ # we need to concatenate the current `kv` with the cached `kv` to expand its length
512
+ if sequence_end >= inference_params.max_seqlen:
513
+ inference_params.key_value_memory_dict[layer_idx] = torch.concatenate((inference_params.key_value_memory_dict[layer_idx], kv), dim=1)
514
+
515
+ inference_params.key_value_memory_dict[layer_idx][batch_start:batch_end, sequence_start:sequence_end, ...] = kv
516
+ kv = inference_params.key_value_memory_dict[layer_idx][batch_start:batch_end, :sequence_end, ...]
517
+
518
+ return kv
519
+
520
+
521
+ class MHA(nn.Module):
522
+ #Multi-head attention layer.
523
+
524
+ def __init__(
525
+ self,
526
+ config: PretrainedConfig,
527
+ dtype: Optional[torch.dtype] = None,
528
+ device: Optional[str] = None,
529
+ rotary_dim: Optional[int] = None,
530
+ rotary_base: float = 10000.0,
531
+ rotary_scale_base: Optional[float] = None,
532
+ n_head: Optional[int] = None,
533
+ n_head_kv: Optional[int] = None,
534
+ head_dim: Optional[int] = None,
535
+ bias: bool = True,
536
+ causal: bool = True,
537
+ softmax_scale: Optional[float] = None,
538
+ layer_idx: Optional[int] = None,
539
+ return_residual: bool = False,
540
+ checkpointing: bool = False,
541
+ ) -> None:
542
+ super().__init__()
543
+
544
+ # Rotary embedding
545
+ self.rotary_dim = rotary_dim if rotary_dim is not None else getattr(config, "rotary_dim", 0)
546
+ if self.rotary_dim > 0:
547
+ rotary_cls = FlashRotaryEmbedding if config.flash_rotary else RotaryEmbedding
548
+ if rotary_cls is None:
549
+ rotary_cls = RotaryEmbedding
550
+
551
+ rotary_kwargs = {}
552
+ if rotary_cls is RotaryEmbedding:
553
+ rotary_kwargs["max_position_embeddings"] = config.n_positions
554
+
555
+ self.rotary_emb = rotary_cls(
556
+ self.rotary_dim,
557
+ base=rotary_base,
558
+ scale_base=rotary_scale_base,
559
+ device=device,
560
+ **rotary_kwargs,
561
+ )
562
+
563
+ # MLP
564
+ self.n_head, self.n_head_kv, self.head_dim = _find_mha_dims(
565
+ config, n_head=n_head, n_head_kv=n_head_kv, head_dim=head_dim
566
+ )
567
+ op_size = self.head_dim * (self.n_head + 2 * self.n_head_kv)
568
+ hidden_size = config.n_embd
569
+
570
+ linear_cls = FusedDense if config.fused_dense else nn.Linear
571
+ if linear_cls is None:
572
+ linear_cls = nn.Linear
573
+
574
+ self.Wqkv = linear_cls(hidden_size, op_size, bias=bias, device=device, dtype=dtype)
575
+ self.out_proj = linear_cls(hidden_size, hidden_size, bias=bias, device=device, dtype=dtype)
576
+
577
+ # Attention
578
+ attn_cls = FlashSelfAttention if config.flash_attn else SelfAttention
579
+ if attn_cls is None:
580
+ attn_cls = SelfAttention
581
+
582
+ cross_attn_cls = FlashCrossAttention if config.flash_attn else CrossAttention
583
+ if cross_attn_cls is None:
584
+ cross_attn_cls = CrossAttention
585
+
586
+ self.inner_attn = attn_cls(
587
+ causal=causal,
588
+ softmax_scale=softmax_scale,
589
+ attention_dropout=config.attn_pdrop,
590
+ )
591
+ self.inner_cross_attn = cross_attn_cls(
592
+ causal=causal,
593
+ softmax_scale=softmax_scale,
594
+ attention_dropout=config.attn_pdrop,
595
+ )
596
+
597
+ self.flash_attn = config.flash_attn and attn_cls is FlashSelfAttention
598
+ self.layer_idx = layer_idx
599
+ self.return_residual = return_residual
600
+ self.checkpointing = checkpointing
601
+
602
+ def _forward_self_attn(
603
+ self, x: torch.FloatTensor, key_padding_mask: Optional[torch.BoolTensor]
604
+ ) -> torch.FloatTensor:
605
+ qkv = self.Wqkv(x)
606
+ qkv = rearrange(qkv, "... (three h d) -> ... three h d", three=3, d=self.head_dim)
607
+
608
+ if self.rotary_dim > 0:
609
+ qkv = self.rotary_emb(qkv)
610
+
611
+ if self.flash_attn:
612
+ batch_size, seqlen = qkv.shape[0], qkv.shape[1]
613
+
614
+ cu_seqlens, max_seqlen = None, None
615
+ if key_padding_mask is not None:
616
+ # If `key_padding_mask` is supplied, we need to unpad the input and retrieve
617
+ # the `cu_seqlens` and `max_seqlen` to be used by `flash-attn`
618
+ qkv, indices, cu_seqlens, max_seqlen = unpad_input(qkv, key_padding_mask)
619
+
620
+ if self.checkpointing:
621
+ attn_output = torch.utils.checkpoint.checkpoint(
622
+ self.inner_attn, qkv, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen
623
+ )
624
+ else:
625
+ attn_output = self.inner_attn(qkv, cu_seqlens=cu_seqlens, max_seqlen=max_seqlen).to(qkv.device)
626
+
627
+ # If `key_padding_mask` is supplied, we need to pad the output back to the original shape
628
+ return pad_input(attn_output, indices, batch_size, seqlen) if key_padding_mask is not None else attn_output
629
+
630
+ if self.checkpointing:
631
+ return torch.utils.checkpoint.checkpoint(self.inner_attn, qkv, key_padding_mask=key_padding_mask)
632
+
633
+ return self.inner_attn(qkv, key_padding_mask=key_padding_mask)
634
+
635
+ def _forward_cross_attn(
636
+ self,
637
+ x: torch.FloatTensor,
638
+ past_key_values: Optional[InferenceParams],
639
+ key_padding_mask: Optional[torch.BoolTensor],
640
+ ) -> torch.FloatTensor:
641
+ batch_size = x.shape[0]
642
+
643
+ qkv = self.Wqkv(x)
644
+
645
+ q = qkv[..., : self.n_head * self.head_dim]
646
+ q = rearrange(q, "... (h d) -> ... h d", d=self.head_dim)
647
+
648
+ kv = qkv[..., self.n_head * self.head_dim :]
649
+ kv = rearrange(kv, "... (two hkv d) -> ... two hkv d", two=2, d=self.head_dim)
650
+
651
+ seqlen_offset = past_key_values.seqlen_offset if past_key_values is not None else 0
652
+ causal = None if seqlen_offset == 0 else False
653
+ if self.rotary_dim > 0:
654
+ q, kv = self.rotary_emb(q, kv=kv, seqlen_offset=seqlen_offset)
655
+
656
+ if past_key_values is not None:
657
+ kv = _update_kv_cache(kv, past_key_values, self.layer_idx)
658
+
659
+ if self.flash_attn:
660
+ batch_size, seqlen_q = q.shape[0], q.shape[1]
661
+ seqlen_k = kv.shape[1]
662
+
663
+ cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k = (
664
+ None,
665
+ None,
666
+ None,
667
+ None,
668
+ )
669
+ if key_padding_mask is not None:
670
+ kv, _, cu_seqlens_k, max_seqlen_k = unpad_input(kv, key_padding_mask)
671
+
672
+ if seqlen_q == 1:
673
+ key_padding_mask = torch.ones(batch_size, 1, device=q.device)
674
+ elif seqlen_q != seqlen_k:
675
+ key_padding_mask = key_padding_mask[:, -seqlen_q:]
676
+
677
+ q, indices_q, cu_seqlens_q, max_seqlen_q = unpad_input(q, key_padding_mask)
678
+
679
+ if self.checkpointing:
680
+ attn_output = torch.utils.checkpoint.checkpoint(
681
+ self.inner_cross_attn,
682
+ q,
683
+ kv,
684
+ causal=causal,
685
+ cu_seqlens=cu_seqlens_q,
686
+ max_seqlen=max_seqlen_q,
687
+ cu_seqlens_k=cu_seqlens_k,
688
+ max_seqlen_k=max_seqlen_k,
689
+ )
690
+ else:
691
+ attn_output = self.inner_cross_attn(
692
+ q,
693
+ kv,
694
+ causal=causal,
695
+ cu_seqlens=cu_seqlens_q,
696
+ max_seqlen=max_seqlen_q,
697
+ cu_seqlens_k=cu_seqlens_k,
698
+ max_seqlen_k=max_seqlen_k,
699
+ )
700
+
701
+ return (
702
+ pad_input(attn_output, indices_q, batch_size, max_seqlen_q)
703
+ if key_padding_mask is not None
704
+ else attn_output
705
+ )
706
+
707
+ if self.checkpointing:
708
+ return torch.utils.checkpoint.checkpoint(
709
+ self.inner_cross_attn,
710
+ q,
711
+ kv,
712
+ key_padding_mask=key_padding_mask,
713
+ causal=causal,
714
+ )
715
+
716
+ return self.inner_cross_attn(q, kv, key_padding_mask=key_padding_mask, causal=causal)
717
+
718
+ def forward(
719
+ self,
720
+ x: torch.FloatTensor,
721
+ past_key_values: Optional[InferenceParams] = None,
722
+ attention_mask: Optional[Union[torch.LongTensor, torch.BoolTensor]] = None,
723
+ **kwargs,
724
+ ) -> Tuple[torch.FloatTensor, torch.FloatTensor]:
725
+ if attention_mask is not None:
726
+ attention_mask = attention_mask.bool()
727
+ else:
728
+ attention_mask = None
729
+
730
+ # MHA
731
+ if self.n_head == self.n_head_kv:
732
+ if past_key_values is None:
733
+ # If `past_key_values` are not supplied, we run self-attention
734
+ attn_output = self._forward_self_attn(x, attention_mask)
735
+ else:
736
+ # If `past_key_values` are supplied, it means that we might have cached values and
737
+ # could take advantage of cross-attention
738
+ attn_output = self._forward_cross_attn(x, past_key_values, attention_mask)
739
+ # MQA / GQA
740
+ else:
741
+ # Regardless of `past_key_values` being supplied or not, it always use cross-attention
742
+ # because `q` and `kv` lengths might be different
743
+ attn_output = self._forward_cross_attn(x, past_key_values, attention_mask)
744
+
745
+ output = rearrange(attn_output, "... h d -> ... (h d)")
746
+ output = self.out_proj(output)
747
+
748
+ return output if not self.return_residual else (output, x)
749
+
750
+
751
+ class ParallelBlock(nn.Module):
752
+ #Parallel block.
753
+ #This block applies parallel mixer and MLP layers to the input (used in GPT-J and CodeGen).
754
+
755
+ def __init__(
756
+ self,
757
+ config: PretrainedConfig,
758
+ block_idx: Optional[int] = None,
759
+ ) -> None:
760
+ super().__init__()
761
+
762
+ self.ln = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)
763
+ self.resid_dropout = nn.Dropout(config.resid_pdrop)
764
+ self.block_idx = block_idx
765
+
766
+ self.mixer = MHA(config, layer_idx=block_idx)
767
+ self.moe = MoE(config)
768
+
769
+ def forward(
770
+ self,
771
+ hidden_states: torch.FloatTensor,
772
+ past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
773
+ attention_mask: Optional[torch.BoolTensor] = None,
774
+ **kwargs,
775
+ ) -> torch.FloatTensor:
776
+ residual = hidden_states
777
+ hidden_states = self.ln(hidden_states)
778
+
779
+ attn_outputs = self.mixer(
780
+ hidden_states,
781
+ past_key_values=past_key_values,
782
+ attention_mask=attention_mask,
783
+ )
784
+ if isinstance(attn_outputs, tuple):
785
+ attn_outputs = attn_outputs[0]
786
+
787
+ attn_outputs = self.resid_dropout(attn_outputs)
788
+ feed_forward_hidden_states = self.resid_dropout(self.moe(hidden_states))
789
+
790
+ hidden_states = attn_outputs + feed_forward_hidden_states + residual
791
+
792
+ return hidden_states
793
+
794
+
795
+ class CausalLMHead(nn.Module):
796
+ #Causal Language Modeling head.
797
+ #Reference:
798
+ # Improving Language Understanding by Generative Pre-Training.
799
+ # https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf.
800
+
801
+ def __init__(self, config: PretrainedConfig) -> None:
802
+ super().__init__()
803
+
804
+ self.ln = nn.LayerNorm(config.n_embd, eps=config.layer_norm_epsilon)
805
+ self.linear = nn.Linear(config.n_embd, config.vocab_size)
806
+
807
+ def forward(self, hidden_states: torch.FloatTensor) -> torch.FloatTensor:
808
+ hidden_states = self.ln(hidden_states)
809
+ logits = self.linear(hidden_states).to(torch.float32)
810
+
811
+ return logits
812
+
813
+
814
+ class CausalLMLoss(nn.Module):
815
+ #Causal Language Modeling loss.
816
+ #Reference:
817
+ # Improving Language Understanding by Generative Pre-Training.
818
+ # https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf.
819
+
820
+ def __init__(self, shift_labels: bool = True) -> None:
821
+ super().__init__()
822
+
823
+ self.shift_labels = shift_labels
824
+ self.loss_fct = nn.CrossEntropyLoss()
825
+
826
+ def forward(self, logits: torch.FloatTensor, labels: torch.LongTensor) -> torch.FloatTensor:
827
+ if self.shift_labels:
828
+ logits = logits[..., :-1, :].contiguous()
829
+ labels = labels[..., 1:].contiguous()
830
+
831
+ loss = self.loss_fct(logits.view(-1, logits.size(-1)), labels.view(-1))
832
+
833
+ return loss
834
+
835
+
836
+ class PhiPreTrainedModel(PreTrainedModel):
837
+ #Phi pre-trained model.
838
+
839
+ config_class = PhiConfig
840
+ base_model_prefix = "transformer"
841
+ supports_gradient_checkpointing = False
842
+ _no_split_modules = ["ParallelBlock"]
843
+
844
+ def __init__(self, *inputs, **kwargs) -> None:
845
+ super().__init__(*inputs, **kwargs)
846
+
847
+ def _init_weights(self, module: nn.Module) -> None:
848
+ if isinstance(module, (nn.Linear,)):
849
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
850
+ if module.bias is not None:
851
+ module.bias.data.zero_()
852
+ elif isinstance(module, nn.Embedding):
853
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
854
+ if module.padding_idx is not None:
855
+ module.weight.data[module.padding_idx].zero_()
856
+ elif isinstance(module, nn.LayerNorm):
857
+ if module.bias is not None:
858
+ module.bias.data.zero_()
859
+ module.weight.data.fill_(1.0)
860
+
861
+ def prepare_inputs_for_generation(
862
+ self,
863
+ input_ids: torch.LongTensor,
864
+ past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
865
+ attention_mask: Optional[Union[torch.LongTensor, torch.BoolTensor]] = None,
866
+ **kwargs,
867
+ ) -> Dict[str, Any]:
868
+ if past_key_values is None or not (isinstance(past_key_values, InferenceParams)):
869
+ past_key_values = InferenceParams(
870
+ max_seqlen=self.config.n_positions,
871
+ max_batch_size=input_ids.shape[0],
872
+ seqlen_offset=0,
873
+ batch_size_offset=0,
874
+ key_value_memory_dict={},
875
+ lengths_per_sample=None,
876
+ )
877
+ else:
878
+ # Assume that `past_key_values` has cached all tokens up to the last token in `input_ids`
879
+ past_key_values.seqlen_offset = input_ids.shape[1] - 1
880
+ input_ids = input_ids[:, -1].unsqueeze(-1)
881
+
882
+ return {
883
+ "input_ids": input_ids,
884
+ "past_key_values": past_key_values,
885
+ "attention_mask": attention_mask,
886
+ }
887
+
888
+
889
+ class PhiModel(PhiPreTrainedModel):
890
+ #Phi model.
891
+
892
+ _keys_to_ignore_on_load_missing = [""]
893
+ _keys_to_ignore_on_load_unexpected = [r"h\.\d+\.mlp.(fc_in|fc_out)\.(weight|bias)"]
894
+
895
+ def __init__(self, config: PhiConfig) -> None:
896
+ super().__init__(config)
897
+
898
+ self.embd = Embedding(config)
899
+ self.h = nn.ModuleList([ParallelBlock(config, block_idx=i) for i in range(config.n_layer)])
900
+ self.gradient_checkpointing = False
901
+ self.post_init()
902
+
903
+ def get_input_embeddings(self) -> nn.Embedding:
904
+ return self.embd.wte
905
+
906
+ def set_input_embeddings(self, new_embeddings: nn.Embedding) -> None:
907
+ self.embd.wte = new_embeddings
908
+
909
+ def forward(
910
+ self,
911
+ input_ids: torch.LongTensor,
912
+ past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
913
+ attention_mask: Optional[torch.BoolTensor] = None,
914
+ ) -> torch.FloatTensor:
915
+ hidden_states = self.embd(input_ids)
916
+
917
+ for layer in self.h:
918
+ hidden_states = layer(
919
+ hidden_states,
920
+ past_key_values=past_key_values,
921
+ attention_mask=attention_mask,
922
+ )
923
+
924
+ return hidden_states
925
+
926
+
927
+ class PhiForCausalLM(PhiPreTrainedModel):
928
+ #Phi for Causal Language Modeling.
929
+
930
+ _keys_to_ignore_on_load_missing = [""]
931
+ _keys_to_ignore_on_load_unexpected = [r"transformer\.h\.\d+\.mlp.(fc_in|fc_out)\.(weight|bias)"]
932
+
933
+ def __init__(self, config: PhiConfig) -> None:
934
+ super().__init__(config)
935
+
936
+ self.transformer = PhiModel(config)
937
+ self.lm_head = CausalLMHead(config)
938
+ self.loss = CausalLMLoss()
939
+
940
+ self.post_init()
941
+
942
+ def get_output_embeddings(self) -> nn.Linear:
943
+ return self.lm_head.linear
944
+
945
+ def set_output_embeddings(self, new_embeddings: nn.Linear) -> None:
946
+ self.lm_head.linear = new_embeddings
947
+
948
+ def forward(
949
+ self,
950
+ input_ids: torch.LongTensor,
951
+ past_key_values: Optional[Union[torch.FloatTensor, InferenceParams]] = None,
952
+ attention_mask: Optional[torch.BoolTensor] = None,
953
+ labels: Optional[torch.LongTensor] = None,
954
+ **kwargs,
955
+ ) -> CausalLMOutputWithPast:
956
+ hidden_states = self.transformer(input_ids, past_key_values=past_key_values, attention_mask=attention_mask)
957
+ lm_logits = self.lm_head(hidden_states)
958
+
959
+ loss = None
960
+ if labels is not None:
961
+ loss = self.loss(lm_logits, labels)
962
+
963
+ return CausalLMOutputWithPast(loss=loss, logits=lm_logits, past_key_values=past_key_values)