mirror of
https://github.com/ml-explore/mlx-examples.git
synced 2025-06-24 09:21:18 +08:00

* add llms subdir + update README * nits * use same pre-commit as mlx * update readmes a bit * format
25 lines
566 B
Python
25 lines
566 B
Python
import numpy as np
|
|
from transformers import AutoModelForCausalLM
|
|
|
|
|
|
def replace_key(key: str) -> str:
|
|
if "wte.weight" in key:
|
|
key = "wte.weight"
|
|
|
|
if ".mlp" in key:
|
|
key = key.replace(".mlp", "")
|
|
return key
|
|
|
|
|
|
def convert():
|
|
model = AutoModelForCausalLM.from_pretrained(
|
|
"microsoft/phi-2", torch_dtype="auto", trust_remote_code=True
|
|
)
|
|
state_dict = model.state_dict()
|
|
weights = {replace_key(k): v.numpy() for k, v in state_dict.items()}
|
|
np.savez("weights.npz", **weights)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
convert()
|