Upload convert_survival_gguf.py with huggingface_hub
Browse files- convert_survival_gguf.py +78 -0
convert_survival_gguf.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# /// script
|
| 3 |
+
# dependencies = ["peft", "transformers", "torch", "huggingface_hub", "sentencepiece"]
|
| 4 |
+
# ///
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import subprocess
|
| 8 |
+
from peft import PeftModel
|
| 9 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 10 |
+
from huggingface_hub import HfApi, create_repo
|
| 11 |
+
|
| 12 |
+
# Configuration
|
| 13 |
+
BASE_MODEL_ID = "meta-llama/Llama-3.2-3B-Instruct"
|
| 14 |
+
ADAPTER_ID = "sunkencity/survival-expert-llama-3b"
|
| 15 |
+
OUTPUT_REPO = "sunkencity/survival-expert-3b-gguf"
|
| 16 |
+
MERGED_DIR = "merged_model"
|
| 17 |
+
GGUF_FILE = "survival-expert-llama-3b.Q4_K_M.gguf"
|
| 18 |
+
|
| 19 |
+
print(f"Loading base model: {BASE_MODEL_ID}")
|
| 20 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
+
BASE_MODEL_ID,
|
| 22 |
+
device_map="auto",
|
| 23 |
+
torch_dtype="auto",
|
| 24 |
+
trust_remote_code=True
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
print(f"Loading adapter: {ADAPTER_ID}")
|
| 28 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_ID)
|
| 29 |
+
|
| 30 |
+
print("Merging model...")
|
| 31 |
+
model = model.merge_and_unload()
|
| 32 |
+
|
| 33 |
+
print(f"Saving merged model to {MERGED_DIR}...")
|
| 34 |
+
model.save_pretrained(MERGED_DIR)
|
| 35 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID)
|
| 36 |
+
tokenizer.save_pretrained(MERGED_DIR)
|
| 37 |
+
|
| 38 |
+
print("Cloning llama.cpp...")
|
| 39 |
+
subprocess.run(["git", "clone", "https://github.com/ggerganov/llama.cpp"], check=True)
|
| 40 |
+
|
| 41 |
+
print("Installing llama.cpp requirements...")
|
| 42 |
+
subprocess.run(["pip", "install", "-r", "llama.cpp/requirements.txt"], check=True)
|
| 43 |
+
|
| 44 |
+
print("Converting to GGUF (Q4_K_M)...")
|
| 45 |
+
# Note: Newer llama.cpp uses convert_hf_to_gguf.py
|
| 46 |
+
# We perform quantization in two steps: convert to fp16 gguf, then quantize
|
| 47 |
+
# Or if convert script supports outtype...
|
| 48 |
+
|
| 49 |
+
# Step 1: Convert to FP16 GGUF
|
| 50 |
+
subprocess.run([
|
| 51 |
+
"python", "llama.cpp/convert_hf_to_gguf.py",
|
| 52 |
+
MERGED_DIR,
|
| 53 |
+
"--outfile", "merged_fp16.gguf",
|
| 54 |
+
"--outtype", "f16"
|
| 55 |
+
], check=True)
|
| 56 |
+
|
| 57 |
+
# Step 2: Quantize to Q4_K_M
|
| 58 |
+
subprocess.run(["make", "-C", "llama.cpp", "llama-quantize"], check=True)
|
| 59 |
+
subprocess.run([
|
| 60 |
+
"./llama.cpp/llama-quantize",
|
| 61 |
+
"merged_fp16.gguf",
|
| 62 |
+
GGUF_FILE,
|
| 63 |
+
"Q4_K_M"
|
| 64 |
+
], check=True)
|
| 65 |
+
|
| 66 |
+
print(f"Creating repo {OUTPUT_REPO}...")
|
| 67 |
+
api = HfApi()
|
| 68 |
+
create_repo(OUTPUT_REPO, repo_type="model", exist_ok=True)
|
| 69 |
+
|
| 70 |
+
print(f"Uploading {GGUF_FILE}...")
|
| 71 |
+
api.upload_file(
|
| 72 |
+
path_or_fileobj=GGUF_FILE,
|
| 73 |
+
path_in_repo=GGUF_FILE,
|
| 74 |
+
repo_id=OUTPUT_REPO,
|
| 75 |
+
repo_type="model"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
print("Done! GGUF available at:", f"https://huggingface.co/{OUTPUT_REPO}")
|