Repository navigation
Expand file tree
/
Copy pathmodelutils.py
More file actions
52 lines (41 loc) · 1.45 KB
/
Copy pathmodelutils.py
File metadata and controls
52 lines (41 loc) · 1.45 KB
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
import torch
import torch.nn as nn
import transformers
from transformers import AutoModelForCausalLM, AutoConfig
def load_mixtral_model(model_name: str,
attn_implementation: str,
):
model_map = {
"Mixtral8x7B": "mistralai/Mixtral-8x7B-v0.1",
"Mixtral8x22B": "mistralai/Mixtral-8x22B-v0.1",
}
if model_name not in model_map:
raise ValueError("model_name must be 'Mixtral8x7B' or 'Mixtral8x22B'")
config = AutoConfig.from_pretrained(
model_map[model_name], attn_implementation=attn_implementation
)
model = AutoModelForCausalLM.from_pretrained(
model_map[model_name],
config=config,
torch_dtype=torch.float16,
device_map="cpu"
)
model.seqlen = 2048
# Architecture constants
if model_name == "Mixtral8x7B":
num_layers = 32
num_experts = 8
elif model_name == "Mixtral8x22B":
num_layers = 56
num_experts = 8
return model, num_layers, num_experts
def find_layers(module, layers=[nn.Conv2d, nn.Linear], name=''):
if not layers:
layers = [transformers.pytorch_utils.Conv1D, nn.Conv2d, nn.Linear]
for layer in layers:
if isinstance(module, layer):
return {name: module}
res = {}
for name1, child in module.named_children():
res.update(find_layers(child, layers=layers, name=name + "." + name1 if name != "" else name1))
return res