2024-03-23 22:13:51 +08:00
|
|
|
# Copyright © 2023-2024 Apple Inc.
|
2023-12-01 03:08:53 +08:00
|
|
|
|
2023-11-30 06:14:11 +08:00
|
|
|
import argparse
|
2024-01-05 13:05:59 +08:00
|
|
|
import copy
|
2023-11-30 06:14:11 +08:00
|
|
|
|
2024-01-05 13:05:59 +08:00
|
|
|
import mlx.core as mx
|
|
|
|
import mlx.nn as nn
|
2024-03-23 22:13:51 +08:00
|
|
|
import models
|
2024-01-10 03:14:52 +08:00
|
|
|
import utils
|
|
|
|
from mlx.utils import tree_flatten
|
2024-01-05 13:05:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
def quantize(weights, config, args):
|
|
|
|
quantized_config = copy.deepcopy(config)
|
|
|
|
|
|
|
|
# Load the model:
|
2024-03-23 22:13:51 +08:00
|
|
|
model = models.Model(models.ModelArgs.from_dict(config))
|
2024-01-10 03:14:52 +08:00
|
|
|
model.load_weights(list(weights.items()))
|
2024-01-05 13:05:59 +08:00
|
|
|
|
|
|
|
# Quantize the model:
|
2024-04-23 09:12:52 +08:00
|
|
|
nn.quantize(
|
2024-01-20 22:07:45 +08:00
|
|
|
model,
|
|
|
|
args.q_group_size,
|
|
|
|
args.q_bits,
|
|
|
|
)
|
2024-01-05 13:05:59 +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-11-30 06:14:11 +08:00
|
|
|
|
2023-12-10 06:13:55 +08:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser(
|
2024-01-10 03:14:52 +08:00
|
|
|
description="Convert Hugging Face model to MLX format"
|
2023-12-10 06:13:55 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2024-01-10 03:14:52 +08:00
|
|
|
"--hf-path",
|
2023-12-10 06:13:55 +08:00
|
|
|
type=str,
|
2024-01-10 03:14:52 +08:00
|
|
|
help="Path to the Hugging Face model.",
|
2023-12-10 06:13:55 +08:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2024-01-05 13:05:59 +08:00
|
|
|
"--mlx-path",
|
2023-12-10 06:13:55 +08:00
|
|
|
type=str,
|
2024-01-10 03:14:52 +08:00
|
|
|
default="mlx_model",
|
|
|
|
help="Path to save the MLX model.",
|
2023-12-10 06:13:55 +08:00
|
|
|
)
|
2024-01-05 13:05:59 +08:00
|
|
|
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,
|
|
|
|
)
|
2024-01-10 03:14:52 +08:00
|
|
|
parser.add_argument(
|
|
|
|
"--dtype",
|
|
|
|
help="Type to save the parameters, ignored if -q is given.",
|
|
|
|
type=str,
|
|
|
|
choices=["float16", "bfloat16", "float32"],
|
|
|
|
default="float16",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--upload-name",
|
|
|
|
help="The name of model to upload to Hugging Face MLX Community",
|
|
|
|
type=str,
|
|
|
|
default=None,
|
2023-12-08 16:19:35 +08:00
|
|
|
)
|
2023-11-30 06:14:11 +08:00
|
|
|
|
2024-01-10 03:14:52 +08:00
|
|
|
args = parser.parse_args()
|
2023-12-19 11:33:17 +08:00
|
|
|
|
2024-01-10 03:14:52 +08:00
|
|
|
print("[INFO] Loading")
|
|
|
|
weights, config, tokenizer = utils.fetch_from_hub(args.hf_path)
|
2024-01-05 13:05:59 +08:00
|
|
|
|
2024-01-10 03:14:52 +08:00
|
|
|
dtype = mx.float16 if args.quantize else getattr(mx, args.dtype)
|
|
|
|
weights = {k: v.astype(dtype) for k, v in weights.items()}
|
2024-01-05 13:05:59 +08:00
|
|
|
if args.quantize:
|
|
|
|
print("[INFO] Quantizing")
|
|
|
|
weights, config = quantize(weights, config, args)
|
|
|
|
|
2024-01-10 03:14:52 +08:00
|
|
|
utils.save_model(args.mlx_path, weights, tokenizer, config)
|
|
|
|
if args.upload_name is not None:
|
|
|
|
utils.upload_to_hub(args.mlx_path, args.upload_name, args.hf_path)
|