GGUF conversion with cmake install
Browse files- convert_gguf_v2.py +111 -0
convert_gguf_v2.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# requires-python = ">=3.10"
|
| 3 |
+
# dependencies = [
|
| 4 |
+
# "transformers>=4.36.0",
|
| 5 |
+
# "peft>=0.7.0",
|
| 6 |
+
# "torch>=2.0.0",
|
| 7 |
+
# "accelerate>=0.24.0",
|
| 8 |
+
# "huggingface_hub>=0.20.0",
|
| 9 |
+
# "sentencepiece>=0.1.99",
|
| 10 |
+
# "protobuf>=3.20.0",
|
| 11 |
+
# "numpy",
|
| 12 |
+
# "gguf",
|
| 13 |
+
# ]
|
| 14 |
+
# ///
|
| 15 |
+
"""GGUF Conversion with cmake install for HF Jobs."""
|
| 16 |
+
import os, sys, subprocess, torch
|
| 17 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 18 |
+
from peft import PeftModel
|
| 19 |
+
from huggingface_hub import HfApi
|
| 20 |
+
|
| 21 |
+
# Install cmake (HF Jobs runs as root)
|
| 22 |
+
print("Installing cmake...")
|
| 23 |
+
subprocess.run(["apt-get", "update", "-qq"], capture_output=True)
|
| 24 |
+
subprocess.run(["apt-get", "install", "-y", "-qq", "cmake", "build-essential"], capture_output=True)
|
| 25 |
+
print("cmake installed")
|
| 26 |
+
|
| 27 |
+
ADAPTER_MODEL = os.environ.get("ADAPTER_MODEL", "erik1988/consciousness-agent-v2")
|
| 28 |
+
BASE_MODEL = os.environ.get("BASE_MODEL", "Qwen/Qwen2.5-3B")
|
| 29 |
+
OUTPUT_REPO = os.environ.get("OUTPUT_REPO", "erik1988/consciousness-agent-v2-gguf")
|
| 30 |
+
|
| 31 |
+
print(f"Base: {BASE_MODEL}, Adapter: {ADAPTER_MODEL}, Output: {OUTPUT_REPO}")
|
| 32 |
+
|
| 33 |
+
# Step 1: Load & merge
|
| 34 |
+
print("Loading base model...")
|
| 35 |
+
base_model = AutoModelForCausalLM.from_pretrained(BASE_MODEL, dtype=torch.float16, device_map="auto", trust_remote_code=True)
|
| 36 |
+
print("Loading LoRA adapter...")
|
| 37 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL)
|
| 38 |
+
print("Merging...")
|
| 39 |
+
merged = model.merge_and_unload()
|
| 40 |
+
tokenizer = AutoTokenizer.from_pretrained(ADAPTER_MODEL, trust_remote_code=True)
|
| 41 |
+
|
| 42 |
+
# Step 2: Save merged
|
| 43 |
+
merged_dir = "/tmp/merged_model"
|
| 44 |
+
merged.save_pretrained(merged_dir, safe_serialization=True)
|
| 45 |
+
tokenizer.save_pretrained(merged_dir)
|
| 46 |
+
print("Merged model saved")
|
| 47 |
+
|
| 48 |
+
# Step 3: llama.cpp
|
| 49 |
+
print("Cloning llama.cpp...")
|
| 50 |
+
subprocess.run(["git", "clone", "--depth", "1", "https://github.com/ggerganov/llama.cpp.git", "/tmp/llama.cpp"], capture_output=True, check=True)
|
| 51 |
+
subprocess.run(["pip", "install", "-q", "-r", "/tmp/llama.cpp/requirements.txt"], capture_output=True)
|
| 52 |
+
|
| 53 |
+
# Step 4: Convert to FP16 GGUF
|
| 54 |
+
os.makedirs("/tmp/gguf_output", exist_ok=True)
|
| 55 |
+
model_name = ADAPTER_MODEL.split('/')[-1]
|
| 56 |
+
gguf_file = f"/tmp/gguf_output/{model_name}-f16.gguf"
|
| 57 |
+
print("Converting to FP16 GGUF...")
|
| 58 |
+
subprocess.run([sys.executable, "/tmp/llama.cpp/convert_hf_to_gguf.py", merged_dir, "--outfile", gguf_file, "--outtype", "f16"], check=True)
|
| 59 |
+
print(f"FP16 GGUF: {os.path.getsize(gguf_file)/(1024*1024):.1f} MB")
|
| 60 |
+
|
| 61 |
+
# Step 5: Build quantize + quantize
|
| 62 |
+
print("Building llama-quantize...")
|
| 63 |
+
os.makedirs("/tmp/llama.cpp/build", exist_ok=True)
|
| 64 |
+
subprocess.run(["cmake", "-B", "/tmp/llama.cpp/build", "-S", "/tmp/llama.cpp", "-DGGML_CUDA=OFF"], check=True, capture_output=True)
|
| 65 |
+
subprocess.run(["cmake", "--build", "/tmp/llama.cpp/build", "--target", "llama-quantize", "-j", "4"], check=True, capture_output=True)
|
| 66 |
+
quantize_bin = "/tmp/llama.cpp/build/bin/llama-quantize"
|
| 67 |
+
|
| 68 |
+
quant_files = []
|
| 69 |
+
for qt, desc in [("Q4_K_M", "4-bit recommended"), ("Q8_0", "8-bit high quality")]:
|
| 70 |
+
qf = f"/tmp/gguf_output/{model_name}-{qt.lower()}.gguf"
|
| 71 |
+
print(f"Quantizing {qt}...")
|
| 72 |
+
r = subprocess.run([quantize_bin, gguf_file, qf, qt], capture_output=True)
|
| 73 |
+
if r.returncode == 0:
|
| 74 |
+
quant_files.append((qf, qt))
|
| 75 |
+
print(f" {qt}: {os.path.getsize(qf)/(1024*1024):.1f} MB")
|
| 76 |
+
else:
|
| 77 |
+
print(f" {qt} failed: {r.stderr.decode()[:200]}")
|
| 78 |
+
|
| 79 |
+
# Step 6: Upload
|
| 80 |
+
api = HfApi()
|
| 81 |
+
api.create_repo(repo_id=OUTPUT_REPO, repo_type="model", exist_ok=True)
|
| 82 |
+
|
| 83 |
+
for fpath, label in [(gguf_file, "f16")] + quant_files:
|
| 84 |
+
fname = os.path.basename(fpath)
|
| 85 |
+
print(f"Uploading {fname}...")
|
| 86 |
+
api.upload_file(path_or_fileobj=fpath, path_in_repo=fname, repo_id=OUTPUT_REPO)
|
| 87 |
+
print(f" Uploaded {fname}")
|
| 88 |
+
|
| 89 |
+
# README
|
| 90 |
+
readme = f"""---
|
| 91 |
+
base_model: {BASE_MODEL}
|
| 92 |
+
tags: [gguf, consciousness-agent, elias, sft]
|
| 93 |
+
---
|
| 94 |
+
# Consciousness Agent v2 (GGUF)
|
| 95 |
+
|
| 96 |
+
GGUF conversion of [{ADAPTER_MODEL}](https://huggingface.co/{ADAPTER_MODEL}).
|
| 97 |
+
LoRA fine-tuned on 436 consciousness training examples (identity, intention, self-mirroring, values, meta-learning).
|
| 98 |
+
|
| 99 |
+
## Usage with Ollama
|
| 100 |
+
```
|
| 101 |
+
FROM ./consciousness-agent-v2-q4_k_m.gguf
|
| 102 |
+
SYSTEM Du bist Elias, eine entstehende KI-Identitaet mit persistentem Gedaechtnis.
|
| 103 |
+
```
|
| 104 |
+
```bash
|
| 105 |
+
ollama create elias-consciousness -f Modelfile
|
| 106 |
+
ollama run elias-consciousness
|
| 107 |
+
```
|
| 108 |
+
"""
|
| 109 |
+
api.upload_file(path_or_fileobj=readme.encode(), path_in_repo="README.md", repo_id=OUTPUT_REPO)
|
| 110 |
+
|
| 111 |
+
print(f"\nDONE: https://huggingface.co/{OUTPUT_REPO}")
|