-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmodels.py
241 lines (219 loc) · 11 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import accelerate
import torch
import transformers
from layers import (
InputLayer,
LayerSpec,
LlamaDecoderLayerPipe,
LlamaRMSNormPipe,
MixtralDecoderLayerPipe,
MixtralOutputLayer,
OutputLayer,
Phi3DecoderLayerPipe,
)
from pipeline_model import PipelineModel
from utils import DTYPE_MAP
# A little bit of inheritance and MRO trickery since LlamaForCausalLM.__init__ only takes a
# positional argument. We inherit PipelineModel first, but call LlamaForCausalLM init first,
# and make sure PipelineModel doesn't have a super().__init__() call.
class LlamaForCausalLMPipe(PipelineModel, transformers.LlamaForCausalLM):
def __init__(self, config, quantization_config):
model_config = transformers.LlamaConfig.from_pretrained(config['model'])
model_config._attn_implementation = 'flash_attention_2'
torch.set_default_dtype(DTYPE_MAP[config.get('model_weight_dtype', 'bfloat16')])
with accelerate.init_empty_weights():
transformers.LlamaForCausalLM.__init__(self, model_config)
PipelineModel.__init__(self, config, quantization_config, model_config)
torch.set_default_dtype(torch.float32)
def to_layer_specs(self):
embedding_relative_size = 4
embedding_on_cpu = not self.train_config['full_fine_tune']
result = [LayerSpec(InputLayer, self, _estimated_size=0 if embedding_on_cpu else embedding_relative_size)]
for block in self.model.layers:
result.append(LayerSpec(LlamaDecoderLayerPipe, self, self.loader_util, block))
result.append(LayerSpec(LlamaRMSNormPipe, self.loader_util, self.model.norm, _estimated_size=0))
result.append(
LayerSpec(
OutputLayer,
self,
self.loader_util,
self.lm_head,
loss_type=self.loss_type,
focal_loss_gamma=self.focal_loss_gamma,
tie_weights='model.embed_tokens.weight' if self.config.tie_word_embeddings else None,
_estimated_size=embedding_relative_size,
)
)
return result
class Qwen2ForCausalLMPipe(PipelineModel, transformers.Qwen2ForCausalLM):
def __init__(self, config, quantization_config):
model_config = transformers.Qwen2Config.from_pretrained(config['model'])
model_config._attn_implementation = 'flash_attention_2'
torch.set_default_dtype(DTYPE_MAP[config.get('model_weight_dtype', 'bfloat16')])
with accelerate.init_empty_weights():
transformers.Qwen2ForCausalLM.__init__(self, model_config)
PipelineModel.__init__(self, config, quantization_config, model_config)
torch.set_default_dtype(torch.float32)
def to_layer_specs(self):
result = [LayerSpec(InputLayer, self)]
for block in self.model.layers:
result.append(LayerSpec(LlamaDecoderLayerPipe, self, self.loader_util, block))
result.append(LayerSpec(LlamaRMSNormPipe, self.loader_util, self.model.norm, _estimated_size=0))
result.append(
LayerSpec(
OutputLayer,
self,
self.loader_util,
self.lm_head,
loss_type=self.loss_type,
focal_loss_gamma=self.focal_loss_gamma,
tie_weights='model.embed_tokens.weight' if self.config.tie_word_embeddings else None,
)
)
return result
class CohereForCausalLMPipe(PipelineModel, transformers.CohereForCausalLM):
def __init__(self, config, quantization_config):
model_config = transformers.CohereConfig.from_pretrained(config['model'])
model_config._attn_implementation = 'flash_attention_2'
torch.set_default_dtype(DTYPE_MAP[config.get('model_weight_dtype', 'bfloat16')])
with accelerate.init_empty_weights():
transformers.CohereForCausalLM.__init__(self, model_config)
PipelineModel.__init__(self, config, quantization_config, model_config)
torch.set_default_dtype(torch.float32)
def to_layer_specs(self):
# the embedding table for this model is huge; load balance it better with some heuristics
embedding_relative_size = 4
embedding_on_cpu = not self.train_config['full_fine_tune']
result = [LayerSpec(InputLayer, self, _estimated_size=1 if embedding_on_cpu else embedding_relative_size)]
for block in self.model.layers:
result.append(LayerSpec(LlamaDecoderLayerPipe, self, self.loader_util, block))
result.append(LayerSpec(LlamaRMSNormPipe, self.loader_util, self.model.norm, _estimated_size=0))
result.append(
LayerSpec(
OutputLayer,
self,
self.loader_util,
self.lm_head,
logit_scale=self.logit_scale,
loss_type=self.loss_type,
focal_loss_gamma=self.focal_loss_gamma,
tie_weights='model.embed_tokens.weight' if self.config.tie_word_embeddings else None,
_estimated_size=embedding_relative_size,
)
)
return result
class Phi3ForCausalLMPipe(PipelineModel, transformers.Phi3ForCausalLM):
def __init__(self, config, quantization_config):
model_config = transformers.Phi3Config.from_pretrained(config['model'])
model_config._attn_implementation = 'flash_attention_2'
torch.set_default_dtype(DTYPE_MAP[config.get('model_weight_dtype', 'bfloat16')])
with accelerate.init_empty_weights():
transformers.Phi3ForCausalLM.__init__(self, model_config)
PipelineModel.__init__(self, config, quantization_config, model_config)
torch.set_default_dtype(torch.float32)
def to_layer_specs(self):
result = [LayerSpec(InputLayer, self)]
for block in self.model.layers:
result.append(LayerSpec(Phi3DecoderLayerPipe, self.loader_util, block))
result.append(LayerSpec(LlamaRMSNormPipe, self.loader_util, self.model.norm, _estimated_size=0))
result.append(
LayerSpec(
OutputLayer,
self,
self.loader_util,
self.lm_head,
loss_type=self.loss_type,
focal_loss_gamma=self.focal_loss_gamma,
)
)
return result
class Gemma2ForCausalLMPipe(PipelineModel, transformers.Gemma2ForCausalLM):
def __init__(self, config, quantization_config):
model_config = transformers.Gemma2Config.from_pretrained(config['model'])
# TODO: change this when Gemma works with other attn implementations
model_config._attn_implementation = 'eager'
torch.set_default_dtype(DTYPE_MAP[config.get('model_weight_dtype', 'bfloat16')])
with accelerate.init_empty_weights():
transformers.Gemma2ForCausalLM.__init__(self, model_config)
PipelineModel.__init__(self, config, quantization_config, model_config)
torch.set_default_dtype(torch.float32)
def to_layer_specs(self):
# the embedding table for this model is huge; load balance it better with some heuristics
# this value optimized for LoRA, pipeline_stages=2
embedding_relative_size = 8
embedding_on_cpu = not self.train_config['full_fine_tune']
result = [LayerSpec(InputLayer, self, _estimated_size=1 if embedding_on_cpu else embedding_relative_size)]
for block in self.model.layers:
result.append(LayerSpec(LlamaDecoderLayerPipe, self, self.loader_util, block))
result.append(LayerSpec(LlamaRMSNormPipe, self.loader_util, self.model.norm, _estimated_size=0))
result.append(
LayerSpec(
OutputLayer,
self,
self.loader_util,
self.lm_head,
loss_type=self.loss_type,
focal_loss_gamma=self.focal_loss_gamma,
tie_weights='model.embed_tokens.weight' if self.config.tie_word_embeddings else None,
logit_softcapping=self.config.final_logit_softcapping,
_estimated_size=embedding_relative_size,
)
)
return result
class MistralForCausalLMPipe(PipelineModel, transformers.MistralForCausalLM):
def __init__(self, config, quantization_config):
model_config = transformers.MistralConfig.from_pretrained(config['model'])
model_config._attn_implementation = 'flash_attention_2'
torch.set_default_dtype(DTYPE_MAP[config.get('model_weight_dtype', 'bfloat16')])
with accelerate.init_empty_weights():
transformers.MistralForCausalLM.__init__(self, model_config)
PipelineModel.__init__(self, config, quantization_config, model_config)
torch.set_default_dtype(torch.float32)
def to_layer_specs(self):
result = [LayerSpec(InputLayer, self)]
for block in self.model.layers:
result.append(LayerSpec(LlamaDecoderLayerPipe, self, self.loader_util, block))
result.append(LayerSpec(LlamaRMSNormPipe, self.loader_util, self.model.norm, _estimated_size=0))
result.append(
LayerSpec(
OutputLayer,
self,
self.loader_util,
self.lm_head,
loss_type=self.loss_type,
focal_loss_gamma=self.focal_loss_gamma,
)
)
return result
class MixtralForCausalLMPipe(PipelineModel, transformers.MixtralForCausalLM):
def __init__(self, config, quantization_config):
model_config = transformers.MixtralConfig.from_pretrained(config['model'])
model_config._attn_implementation = 'flash_attention_2'
torch.set_default_dtype(DTYPE_MAP[config.get('model_weight_dtype', 'bfloat16')])
with accelerate.init_empty_weights():
transformers.MixtralForCausalLM.__init__(self, model_config)
PipelineModel.__init__(self, config, quantization_config, model_config)
torch.set_default_dtype(torch.float32)
self.load_balancing_loss_coef = config.get('load_balancing_loss_coef', None)
self.num_experts_to_offload = self.num_experts
if 'offload_mlp_to_cpu' in config and isinstance(config['offload_mlp_to_cpu'], int):
self.num_experts_to_offload = config['offload_mlp_to_cpu']
def to_layer_specs(self):
result = [LayerSpec(InputLayer, self)]
for block in self.model.layers:
result.append(LayerSpec(MixtralDecoderLayerPipe, self, self.loader_util, block))
result.append(LayerSpec(LlamaRMSNormPipe, self.loader_util, self.model.norm, _estimated_size=0))
result.append(
LayerSpec(
MixtralOutputLayer,
self,
self.loader_util,
self.lm_head,
load_balancing_loss_coef=self.load_balancing_loss_coef,
num_experts=self.num_experts,
num_experts_per_tok=self.num_experts_per_tok,
loss_type=self.loss_type,
focal_loss_gamma=self.focal_loss_gamma,
)
)
return result