2023-12-06 03:02:52 +08:00
|
|
|
# Copyright © 2023 Apple Inc.
|
|
|
|
|
|
|
|
import argparse
|
2023-12-22 04:59:37 +08:00
|
|
|
import copy
|
2023-12-21 00:39:37 +08:00
|
|
|
import json
|
2023-12-22 04:59:37 +08:00
|
|
|
import shutil
|
2023-12-13 00:36:40 +08:00
|
|
|
from pathlib import Path
|
2023-12-06 03:02:52 +08:00
|
|
|
|
2023-12-22 04:59:37 +08:00
|
|
|
import mlx.core as mx
|
|
|
|
import mlx.nn as nn
|
2023-12-21 02:22:25 +08:00
|
|
|
import numpy as np
|
|
|
|
import torch
|
2023-12-22 04:59:37 +08:00
|
|
|
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:
|
2024-04-19 09:16:10 +08:00
|
|
|
nn.quantize(model, args.q_group_size, args.q_bits)
|
2023-12-22 04:59:37 +08:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2023-12-06 03:02:52 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(description="Convert Mistral weights to MLX.")
|
|
|
|
parser.add_argument(
|
2023-12-22 04:59:37 +08:00
|
|
|
"--torch-path",
|
2023-12-06 03:02:52 +08:00
|
|
|
type=str,
|
2023-12-22 04:59:37 +08:00
|
|
|
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(
|
2023-12-29 13:42:22 +08:00
|
|
|
"--q-group-size",
|
2023-12-22 04:59:37 +08:00
|
|
|
help="Group size for quantization.",
|
|
|
|
type=int,
|
|
|
|
default=64,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2023-12-29 13:42:22 +08:00
|
|
|
"--q-bits",
|
2023-12-22 04:59:37 +08:00
|
|
|
help="Bits per weight for quantization.",
|
|
|
|
type=int,
|
|
|
|
default=4,
|
2023-12-06 03:02:52 +08:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2023-12-22 04:59:37 +08:00
|
|
|
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"),
|
2023-12-06 03:02:52 +08:00
|
|
|
)
|
2023-12-21 00:39:37 +08:00
|
|
|
|
|
|
|
# Save config.json with model_type
|
2023-12-22 04:59:37 +08:00
|
|
|
with open(mlx_path / "config.json", "w") as f:
|
2023-12-21 00:39:37 +08:00
|
|
|
config["model_type"] = "mistral"
|
|
|
|
json.dump(config, f, indent=4)
|