OsakanaTeishoku
commited on
Commit
•
75a06ef
1
Parent(s):
a170752
Upload CustomMixtralForCausalLM
Browse files- README.md +199 -0
- config.json +33 -0
- custom_mixtral.py +131 -0
- generation_config.json +6 -0
- model.safetensors +3 -0
- noisy_gate.py +25 -0
README.md
ADDED
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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]
|
config.json
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "None",
|
3 |
+
"architectures": [
|
4 |
+
"CustomMixtralForCausalLM"
|
5 |
+
],
|
6 |
+
"attention_dropout": 0.0,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoModelForCausalLM": "custom_mixtral.CustomMixtralForCausalLM"
|
9 |
+
},
|
10 |
+
"bos_token_id": 1,
|
11 |
+
"eos_token_id": 2,
|
12 |
+
"hidden_act": "silu",
|
13 |
+
"hidden_size": 1024,
|
14 |
+
"initializer_range": 0.02,
|
15 |
+
"intermediate_size": 2400,
|
16 |
+
"max_position_embeddings": 131072,
|
17 |
+
"model_type": "mixtral",
|
18 |
+
"num_attention_heads": 16,
|
19 |
+
"num_experts_per_tok": 1,
|
20 |
+
"num_hidden_layers": 24,
|
21 |
+
"num_key_value_heads": 8,
|
22 |
+
"num_local_experts": 4,
|
23 |
+
"output_router_logits": true,
|
24 |
+
"rms_norm_eps": 1e-05,
|
25 |
+
"rope_theta": 10000.0,
|
26 |
+
"router_aux_loss_coef": 0.01,
|
27 |
+
"sliding_window": 1024,
|
28 |
+
"tie_word_embeddings": false,
|
29 |
+
"torch_dtype": "float16",
|
30 |
+
"transformers_version": "4.39.1",
|
31 |
+
"use_cache": true,
|
32 |
+
"vocab_size": 32000
|
33 |
+
}
|
custom_mixtral.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import MixtralForCausalLM, MixtralConfig
|
2 |
+
from transformers.modeling_outputs import MoeCausalLMOutputWithPast, MoECausalLMOutputWithPast
|
3 |
+
from transformers.models.mixtral.modeling_mixtral import MixtralDecoderLayer, MixtralSparseMoeBlock, load_balancing_loss_func
|
4 |
+
from .noisy_gate import NoisyGate
|
5 |
+
import torch
|
6 |
+
import torch.nn as nn
|
7 |
+
from typing import List, Optional, Tuple, Union
|
8 |
+
|
9 |
+
def router_z_loss_func(
|
10 |
+
gate_logits: torch.Tensor, num_experts: torch.Tensor = None, top_k=2
|
11 |
+
) -> float:
|
12 |
+
"""Router z-loss used in ST-MoE."""
|
13 |
+
if gate_logits is None or not isinstance(gate_logits, tuple):
|
14 |
+
return 0
|
15 |
+
|
16 |
+
if isinstance(gate_logits, tuple):
|
17 |
+
compute_device = gate_logits[0].device
|
18 |
+
concatenated_gate_logits = torch.cat([layer_gate.to(compute_device) for layer_gate in gate_logits], dim=0)
|
19 |
+
|
20 |
+
router_z_loss = torch.logsumexp(concatenated_gate_logits, dim = -1)
|
21 |
+
router_z_loss = torch.square(router_z_loss)
|
22 |
+
router_z_loss = router_z_loss.mean()
|
23 |
+
|
24 |
+
return router_z_loss
|
25 |
+
|
26 |
+
class CustomMixtralConfig(MixtralConfig):
|
27 |
+
def __init__(self, **kwargs):
|
28 |
+
super().__init__(**kwargs)
|
29 |
+
|
30 |
+
class CustomMixtralForCausalLM(MixtralForCausalLM):
|
31 |
+
"""Mixtral with z-loss. Gating improvement based on ST-MoE."""
|
32 |
+
def __init__(self, config):
|
33 |
+
super().__init__(config)
|
34 |
+
self.router_z_loss_coef = 1e-3
|
35 |
+
for layer in self.model.layers:
|
36 |
+
layer.block_sparse_moe.gate = NoisyGate(config.hidden_size, config.num_local_experts, noise_mult=1.0, bias=False)
|
37 |
+
|
38 |
+
|
39 |
+
def forward(
|
40 |
+
self,
|
41 |
+
input_ids: torch.LongTensor = None,
|
42 |
+
attention_mask: Optional[torch.Tensor] = None,
|
43 |
+
position_ids: Optional[torch.LongTensor] = None,
|
44 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
45 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
46 |
+
labels: Optional[torch.LongTensor] = None,
|
47 |
+
use_cache: Optional[bool] = None,
|
48 |
+
output_attentions: Optional[bool] = None,
|
49 |
+
output_hidden_states: Optional[bool] = None,
|
50 |
+
output_router_logits: Optional[bool] = None,
|
51 |
+
return_dict: Optional[bool] = None,
|
52 |
+
):
|
53 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
54 |
+
output_router_logits = (
|
55 |
+
output_router_logits if output_router_logits is not None else self.config.output_router_logits
|
56 |
+
)
|
57 |
+
|
58 |
+
output_hidden_states = (
|
59 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
60 |
+
)
|
61 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
62 |
+
|
63 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
64 |
+
outputs = self.model(
|
65 |
+
input_ids=input_ids,
|
66 |
+
attention_mask=attention_mask,
|
67 |
+
position_ids=position_ids,
|
68 |
+
past_key_values=past_key_values,
|
69 |
+
inputs_embeds=inputs_embeds,
|
70 |
+
use_cache=use_cache,
|
71 |
+
output_attentions=output_attentions,
|
72 |
+
output_hidden_states=output_hidden_states,
|
73 |
+
output_router_logits=output_router_logits,
|
74 |
+
return_dict=return_dict,
|
75 |
+
)
|
76 |
+
|
77 |
+
hidden_states = outputs[0]
|
78 |
+
logits = self.lm_head(hidden_states)
|
79 |
+
logits = logits.float()
|
80 |
+
|
81 |
+
loss = None
|
82 |
+
if labels is not None:
|
83 |
+
# Shift so that tokens < n predict n
|
84 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
85 |
+
shift_labels = labels[..., 1:].contiguous()
|
86 |
+
# Flatten the tokens
|
87 |
+
loss_fct = nn.CrossEntropyLoss()
|
88 |
+
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
89 |
+
shift_labels = shift_labels.view(-1)
|
90 |
+
# Enable model parallelism
|
91 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
92 |
+
loss = loss_fct(shift_logits, shift_labels)
|
93 |
+
|
94 |
+
aux_loss = None
|
95 |
+
if output_router_logits:
|
96 |
+
aux_loss = load_balancing_loss_func(
|
97 |
+
outputs.router_logits if return_dict else outputs[-1],
|
98 |
+
self.num_experts,
|
99 |
+
self.num_experts_per_tok,
|
100 |
+
attention_mask,
|
101 |
+
)
|
102 |
+
if labels is not None:
|
103 |
+
loss += self.router_aux_loss_coef * aux_loss.to(loss.device) # make sure to reside in the same device
|
104 |
+
|
105 |
+
router_z_loss = None
|
106 |
+
if output_router_logits:
|
107 |
+
router_z_loss = router_z_loss_func(
|
108 |
+
outputs.router_logits if return_dict else outputs[-1],
|
109 |
+
self.num_experts,
|
110 |
+
self.num_experts_per_tok,
|
111 |
+
)
|
112 |
+
if labels is not None:
|
113 |
+
loss += self.router_z_loss_coef * router_z_loss.to(loss.device)
|
114 |
+
|
115 |
+
if not return_dict:
|
116 |
+
output = (logits,) + outputs[1:]
|
117 |
+
if output_router_logits:
|
118 |
+
output = (router_z_loss,) + output
|
119 |
+
output = (aux_loss,) + output
|
120 |
+
return (loss,) + output if loss is not None else output
|
121 |
+
|
122 |
+
return MoECausalLMOutputWithPast(
|
123 |
+
loss=loss,
|
124 |
+
aux_loss=aux_loss,
|
125 |
+
z_loss=router_z_loss,
|
126 |
+
logits=logits,
|
127 |
+
past_key_values=outputs.past_key_values,
|
128 |
+
hidden_states=outputs.hidden_states,
|
129 |
+
attentions=outputs.attentions,
|
130 |
+
router_logits=outputs.router_logits,
|
131 |
+
)
|
generation_config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 1,
|
4 |
+
"eos_token_id": 2,
|
5 |
+
"transformers_version": "4.39.1"
|
6 |
+
}
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c4c0f6b25756d417521bbb4f4d240eddcfde7209b4e482af40d6ac63457004f3
|
3 |
+
size 1697998272
|
noisy_gate.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
|
4 |
+
def log(t, eps = 1e-20):
|
5 |
+
return torch.log(t.clamp(min = eps))
|
6 |
+
|
7 |
+
def gumbel_noise(t):
|
8 |
+
noise = torch.zeros_like(t).uniform_(0, 1)
|
9 |
+
return -log(-log(noise))
|
10 |
+
|
11 |
+
class NoisyGate(nn.Module):
|
12 |
+
def __init__(self, hidden_dim, num_experts, noise_mult=1.0, bias=False):
|
13 |
+
super().__init__()
|
14 |
+
self.hidden_dim = hidden_dim
|
15 |
+
self.num_experts = num_experts
|
16 |
+
self.noise_mult = noise_mult
|
17 |
+
self.bias = bias
|
18 |
+
self.gate = nn.Linear(self.hidden_dim, self.num_experts, bias=self.bias)
|
19 |
+
|
20 |
+
def forward(self, x):
|
21 |
+
x = self.gate(x)
|
22 |
+
noise = gumbel_noise(x)
|
23 |
+
out = x + noise * self.noise_mult
|
24 |
+
return out
|
25 |
+
|