Create phi_convert_hf_gguf
Browse files- phi_convert_hf_gguf +56 -0
phi_convert_hf_gguf
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
import sys
|
3 |
+
import os
|
4 |
+
import logging
|
5 |
+
import json
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
|
11 |
+
def read_config(model_dir: str) -> dict:
|
12 |
+
"""Read and return the model config"""
|
13 |
+
config_path = Path(model_dir) / "config.json"
|
14 |
+
with open(config_path, 'r') as f:
|
15 |
+
return json.load(f)
|
16 |
+
|
17 |
+
def convert_phi_model(model_dir: str, output_file: str) -> None:
|
18 |
+
"""Convert Phi model with proper handling for metadata"""
|
19 |
+
script_dir = Path("/tmp/git/llama.cpp")
|
20 |
+
|
21 |
+
# Read config to get proper vocab size
|
22 |
+
config = read_config(model_dir)
|
23 |
+
vocab_size = config.get("vocab_size") # phi-4 config: 100352
|
24 |
+
|
25 |
+
cmd = [
|
26 |
+
f"{script_dir}/convert_hf_to_gguf.py",
|
27 |
+
model_dir,
|
28 |
+
f"--outfile {output_file}",
|
29 |
+
f"--outtype bf16",
|
30 |
+
"--vocab-only" # First create vocab only
|
31 |
+
]
|
32 |
+
|
33 |
+
logger.info("Converting vocabulary...")
|
34 |
+
result = os.system(" ".join(cmd))
|
35 |
+
if result != 0:
|
36 |
+
raise RuntimeError("Vocabulary conversion failed")
|
37 |
+
|
38 |
+
# Now convert the full model
|
39 |
+
cmd = [
|
40 |
+
f"{script_dir}/convert_hf_to_gguf.py",
|
41 |
+
model_dir,
|
42 |
+
f"--outfile {output_file}",
|
43 |
+
f"--outtype bf16",
|
44 |
+
f'--metadata "{{\\"model\\":\\"phi-4\\",\\"vocab_size\\":{vocab_size}}}"'
|
45 |
+
]
|
46 |
+
|
47 |
+
logger.info("Converting model...")
|
48 |
+
result = os.system(" ".join(cmd))
|
49 |
+
if result != 0:
|
50 |
+
raise RuntimeError("Model conversion failed")
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
model_dir = "/mnt/llm/models/phi-4/model"
|
54 |
+
output_file = "/mnt/llm/models/phi-4.bf16.bin"
|
55 |
+
|
56 |
+
convert_phi_model(model_dir, output_file)
|