2023-12-06 03:02:52 +08:00
|
|
|
# Copyright © 2023 Apple Inc.
|
|
|
|
|
|
|
|
import argparse
|
2023-12-21 00:39:37 +08:00
|
|
|
import json
|
2023-12-13 00:36:40 +08:00
|
|
|
from pathlib import Path
|
2023-12-06 03:02:52 +08:00
|
|
|
|
2023-12-21 02:22:25 +08:00
|
|
|
import numpy as np
|
|
|
|
import torch
|
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-21 22:28:57 +08:00
|
|
|
"--model-path",
|
2023-12-06 03:02:52 +08:00
|
|
|
type=str,
|
2023-12-13 00:36:40 +08:00
|
|
|
default="mistral-7B-v0.1/",
|
|
|
|
help="The path to the Mistral model. The MLX weights will also be saved there.",
|
2023-12-06 03:02:52 +08:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2023-12-13 00:36:40 +08:00
|
|
|
model_path = Path(args.model_path)
|
|
|
|
state = torch.load(str(model_path / "consolidated.00.pth"))
|
2023-12-06 03:02:52 +08:00
|
|
|
np.savez(
|
2023-12-13 00:36:40 +08:00
|
|
|
str(model_path / "weights.npz"),
|
|
|
|
**{k: v.to(torch.float16).numpy() for k, v in state.items()}
|
2023-12-06 03:02:52 +08:00
|
|
|
)
|
2023-12-21 00:39:37 +08:00
|
|
|
|
|
|
|
# Save config.json with model_type
|
|
|
|
with open(model_path / "params.json", "r") as f:
|
|
|
|
config = json.loads(f.read())
|
|
|
|
config["model_type"] = "mistral"
|
|
|
|
with open(model_path / "config.json", "w") as f:
|
|
|
|
json.dump(config, f, indent=4)
|