Views
No views yet
[!WARNING] This model outputs gibberish as it was not trained under the dense configuration. Finetuning or merging is needed to make this model useful.
| Mistral weight | Mixtral weight |
|---|---|
gate_proj | experts.3.w1 |
down_proj | experts.3.w2 |
up_proj | experts.3.w3 |
| Expert | Source | Wikitext perplexity |
|---|---|---|
| Unmixtraled-22B-v0.1-expert-0 | Mixtral 8x22B embed, attn, layernorm, lm_head + expert 0 MLPs | 696.6932983398438 |
| Unmixtraled-22B-v0.1-expert-1 | Mixtral 8x22B embed, attn, layernorm, lm_head + expert 1 MLPs | 6853.04248046875 |
| Unmixtraled-22B-v0.1-expert-2 | Mixtral 8x22B embed, attn, layernorm, lm_head + expert 2 MLPs | 4689.181640625 |
| Unmixtraled-22B-v0.1-expert-3 | Mixtral 8x22B embed, attn, layernorm, lm_head + expert 3 MLPs | 782.3755493164062 |
| Unmixtraled-22B-v0.1-expert-4 | Mixtral 8x22B embed, attn, layernorm, lm_head + expert 4 MLPs | 2844.943603515625 |
| Unmixtraled-22B-v0.1-expert-5 | Mixtral 8x22B embed, attn, layernorm, lm_head + expert 5 MLPs | 1099.32373046875 |
| Unmixtraled-22B-v0.1-expert-6 | Mixtral 8x22B embed, attn, layernorm, lm_head + expert 6 MLPs | 341.5309753417969 |
| Unmixtraled-22B-v0.1-expert-7 | Mixtral 8x22B embed, attn, layernorm, lm_head + expert 7 MLPs | 2099.63818359375 |
| Unmixtraled-22B-v0.1-lerp | Mixtral 8x22B embed, attn, layernorm, lm_head + linear merge of expert 0-7 MLPs | 1873.9874267578125 |
1# pip install -U transformers huggingface_hub "git+https://github.com/arcee-ai/mergekit@7467108c05d56ef2bb4b8f33936d437dc448f7dd"
2
3import fnmatch
4import json
5import os
6import re
7import shutil
8
9import torch
10from huggingface_hub import snapshot_download
11from mergekit.architecture import get_architecture_info
12from mergekit.common import ModelReference
13from mergekit.io import LazyTensorLoader, TensorWriter
14from tqdm import tqdm
15
16MIXTRAL_MODEL_ID = "mistral-community/Mixtral-8x22B-v0.1"
17MIXTRAL_PATH = snapshot_download(repo_id=MIXTRAL_MODEL_ID)
18print(f"Mixtral downloaded to: {MIXTRAL_PATH}")
19
20MISTRAL_PATH = snapshot_download(
21 repo_id="mistralai/Mistral-7B-v0.1", allow_patterns=["config.json"]
22)
23print(f"Mistral config downloaded to: {MISTRAL_PATH}")
24
25with open(os.path.join(MISTRAL_PATH, "config.json"), "r") as f:
26 mistral_config = json.load(f)
27
28with open(os.path.join(MIXTRAL_PATH, "config.json"), "r") as f:
29 mixtral_config = json.load(f)
30
31combined_config = {
32 key: mixtral_config[key] for key in mistral_config if key in mixtral_config
33}
34combined_config["architectures"] = ["MistralForCausalLM"]
35combined_config["model_type"] = "mistral"
36
37mixtral_model_ref = ModelReference.parse(MIXTRAL_PATH)
38mixtral_architecture_info = get_architecture_info(mixtral_model_ref.config())
39mixtral_loader = LazyTensorLoader(mixtral_model_ref.tensor_index(), lazy_unpickle=True)
40
41ALLOW_LIST = ["generation_config.json", "tokenizer.model", "tokenizer_config.json"]
42
43def copy_directory(src, dest, allowed_patterns):
44 os.makedirs(dest, exist_ok=True)
45 for root, dirs, files in os.walk(src):
46 # Only keep directories that match at least one of the allowed patterns
47 dirs[:] = [d for d in dirs if any(fnmatch.fnmatch(d, pattern) for pattern in allowed_patterns)]
48 for file in files:
49 # Only copy files that match at least one of the allowed patterns
50 if any(fnmatch.fnmatch(file, pattern) for pattern in allowed_patterns):
51 src_path = os.path.join(root, file)
52 dest_path = os.path.join(dest, os.path.relpath(src_path, src))
53 os.makedirs(os.path.dirname(dest_path), exist_ok=True)
54 shutil.copy2(src_path, dest_path)
55
56def get_tensor(layer_num, expert_num, tensor_type):
57 weight_name = f"model.layers.{layer_num}.block_sparse_moe.experts.{expert_num}.{tensor_type}.weight"
58 return mixtral_loader.get_tensor(weight_name)
59
60
61def extract_layer_number(string):
62 match = re.search(r"layers\.(\d+)\.", string)
63 return int(match.group(1)) if match else None
64
65
66def save_expert_as_dense(output_path, expert_num):
67 dense_model_ref = ModelReference.parse(output_path)
68 dense_architecture_info = get_architecture_info(dense_model_ref.config())
69
70 writer = TensorWriter(output_path, safe_serialization=True)
71
72 for weight_info in tqdm(dense_architecture_info.all_weights(dense_model_ref.config())):
73 if weight_info.name.endswith(".up_proj.weight"):
74 layer_num = extract_layer_number(weight_info.name)
75 writer.save_tensor(weight_info.name, get_tensor(layer_num, expert_num, "w3"))
76 elif weight_info.name.endswith(".down_proj.weight"):
77 layer_num = extract_layer_number(weight_info.name)
78 writer.save_tensor(weight_info.name, get_tensor(layer_num, expert_num, "w2"))
79 elif weight_info.name.endswith(".gate_proj.weight"):
80 layer_num = extract_layer_number(weight_info.name)
81 writer.save_tensor(weight_info.name, get_tensor(layer_num, expert_num, "w1"))
82 else:
83 writer.save_tensor(weight_info.name, mixtral_loader.get_tensor(weight_info.name))
84
85 writer.finalize()
86
87
88num_experts = mixtral_config["num_local_experts"]
89
90for expert_num in range(num_experts):
91 dense_path = f"./dense_expert_{expert_num}"
92 copy_directory(MIXTRAL_PATH, dense_path, ALLOW_LIST)
93
94 with open(os.path.join(dense_path, "config.json"), "w") as f:
95 json.dump(combined_config, f, indent=2)
96
97 save_expert_as_dense(dense_path, expert_num)
98 print(f"Dense model #{expert_num} saved to {os.path.abspath(dense_path)}")