forked from AlexCheema/mlx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
98 lines (82 loc) · 2.56 KB
/
convert.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
# Copyright © 2023 Apple Inc.
import argparse
import copy
import json
import shutil
from pathlib import Path
import mlx.core as mx
import mlx.nn as nn
import numpy as np
import torch
from mistral import Mistral, ModelArgs
from mlx.utils import tree_flatten, tree_map, tree_unflatten
def quantize(weights, config, args):
quantized_config = copy.deepcopy(config)
# Load the model:
config.pop("sliding_window", None)
model = Mistral(ModelArgs(**config))
weights = tree_map(mx.array, weights)
model.update(tree_unflatten(list(weights.items())))
# Quantize the model:
nn.quantize(model, args.q_group_size, args.q_bits)
# Update the config:
quantized_config["quantization"] = {
"group_size": args.q_group_size,
"bits": args.q_bits,
}
quantized_weights = dict(tree_flatten(model.parameters()))
return quantized_weights, quantized_config
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert Mistral weights to MLX.")
parser.add_argument(
"--torch-path",
type=str,
default="mistral-7B-v0.1",
help="The path to the PyTorch model.",
)
parser.add_argument(
"--mlx-path",
type=str,
default="mlx_model",
help="The path to save the MLX model.",
)
parser.add_argument(
"-q",
"--quantize",
help="Generate a quantized model.",
action="store_true",
)
parser.add_argument(
"--q-group-size",
help="Group size for quantization.",
type=int,
default=64,
)
parser.add_argument(
"--q-bits",
help="Bits per weight for quantization.",
type=int,
default=4,
)
args = parser.parse_args()
torch_path = Path(args.torch_path)
state = torch.load(str(torch_path / "consolidated.00.pth"))
mlx_path = Path(args.mlx_path)
mlx_path.mkdir(parents=True, exist_ok=True)
weights = {k: v.to(torch.float16).numpy() for k, v in state.items()}
with open(torch_path / "params.json", "r") as f:
config = json.loads(f.read())
if args.quantize:
print("[INFO] Quantizing")
weights, config = quantize(weights, config, args)
# Save weights
np.savez(str(mlx_path / "weights.npz"), **weights)
# Copy tokenizer
shutil.copyfile(
str(torch_path / "tokenizer.model"),
str(mlx_path / "tokenizer.model"),
)
# Save config.json with model_type
with open(mlx_path / "config.json", "w") as f:
config["model_type"] = "mistral"
json.dump(config, f, indent=4)