File size: 2,717 Bytes
6da8440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a171f9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6da8440
 
 
 
 
 
a171f9a
6da8440
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Merges the trained LoRA into Gemma and exports GGUF files for Ollama / LM Studio,
then pushes them to Hugging Face.

    python scripts/export.py --adapter runs/bloxy/lora --hub YOURNAME/Bloxy-12B-v1
      -> YOURNAME/Bloxy-12B-v1-GGUF  (q4_k_m ~7 GB, runs on an 8 GB GPU; q8_0 ~13 GB)
"""

from __future__ import annotations

import argparse
from pathlib import Path

MODELFILE = """FROM ./{gguf}
PARAMETER temperature 0.4
PARAMETER num_ctx 8192
SYSTEM \"\"\"{system}\"\"\"
"""


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--adapter", required=True, help="local LoRA dir or HF repo of the adapter")
    ap.add_argument("--hub", default=None, help="base name; GGUFs go to <hub>-GGUF")
    ap.add_argument("--out", default="runs/export")
    ap.add_argument("--quants", default="q4_k_m,q8_0")
    args = ap.parse_args()

    import sys

    sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
    from bloxy_data.common import SYSTEM_PROMPT
    from unsloth import FastModel

    model, tokenizer = FastModel.from_pretrained(args.adapter, max_seq_length=4096, load_in_4bit=False)
    quants = [q.strip() for q in args.quants.split(",") if q.strip()]
    out = Path(args.out)
    out.mkdir(parents=True, exist_ok=True)
    model.save_pretrained_gguf(str(out), tokenizer, quantization_method=quants)
    # Newer Unsloth writes GGUFs to "<out>_gguf/", older ones to "<out>/". Collect both,
    # move them into <out>, and rename them to Bloxy names. Skip the bf16 (24 GB) and the
    # vision projector (text-only model).
    found = list(out.glob("*.gguf")) + list(Path(str(out) + "_gguf").glob("*.gguf"))
    for p in found:
        low = p.name.lower()
        if "bf16" in low or "f16" in low or "mmproj" in low:
            continue
        q = next((x for x in quants if x.lower() in low), low.rsplit(".", 2)[-2])
        dest = out / f"bloxy-12b-v1.{q.upper()}.gguf"
        if p != dest:
            p.replace(dest)
    gguf = sorted(p.name for p in out.glob("bloxy-*.gguf") if "q4_k_m" in p.name.lower()) or sorted(p.name for p in out.glob("bloxy-*.gguf"))
    if not gguf:
        raise SystemExit(f"no GGUF files found in {out} or {out}_gguf")
    (out / "Modelfile").write_text(MODELFILE.format(gguf=gguf[0], system=SYSTEM_PROMPT))
    print("GGUF files:", [p.name for p in out.glob("bloxy-*.gguf")])
    if args.hub:
        from huggingface_hub import HfApi

        repo = f"{args.hub}-GGUF"
        api = HfApi()
        api.create_repo(repo, private=True, exist_ok=True)
        api.upload_folder(repo_id=repo, folder_path=str(out), allow_patterns=["bloxy-*.gguf", "Modelfile"])
        print(f"https://huggingface.co/{repo}")


if __name__ == "__main__":
    main()