"""Write the release layout and convert it to GGUF with llama.cpp's own converter. python -m tk.export --projector runs//projector.safetensors --text models/ --out out/ out// HF repo layout (Ultravox format) config.json architectures UltravoxModel, audio_config (Whisper-large-v3-turbo), text_config, stack 8 model.safetensors audio_tower.* (frozen Whisper encoder) + multi_modal_projector.* (trained) text/ the text model (Qwen3.5 layout, chat template set), loadable on its own out/-gguf/ -.gguf (text) + mmproj--.gguf """ from __future__ import annotations import argparse import json import shutil import subprocess import sys from pathlib import Path from safetensors import safe_open from safetensors.torch import load_file, save_file from tk import paths from tk.prompt import CHAT_TEMPLATE LLAMA = Path.home() / ".cache/spot-snake/llama.cpp" GGUF_PY = Path.home() / ".venvs/spot-snake-gguf/bin/python" def write_text_dir(src: Path, dst: Path) -> None: if dst.exists(): shutil.rmtree(dst) shutil.copytree(src, dst, ignore=shutil.ignore_patterns("._*", "README.md")) tc = json.loads((dst / "tokenizer_config.json").read_text()) tc["chat_template"] = CHAT_TEMPLATE (dst / "tokenizer_config.json").write_text(json.dumps(tc, indent=2, ensure_ascii=False)) (dst / "chat_template.jinja").write_text(CHAT_TEMPLATE) def write_ultravox_dir(projector: Path, text_dir: Path, out: Path) -> None: out.mkdir(parents=True, exist_ok=True) wcfg = json.loads((paths.WHISPER / "config.json").read_text()) tcfg = json.loads((text_dir / "config.json").read_text())["text_config"] proj = load_file(projector) hidden = proj["multi_modal_projector.linear_1.weight"].shape[0] cfg = { "architectures": ["UltravoxModel"], "model_type": "ultravox", "audio_config": wcfg, "audio_model_id": None, "text_config": {"hidden_size": tcfg["hidden_size"], "model_type": tcfg["model_type"], "vocab_size": tcfg["vocab_size"]}, "text_model_id": None, "text_model_subfolder": "text", "hidden_size": hidden, "stack_factor": 8, "projector_act": "swiglu", "projector_ln_mid": True, "norm_init": 0.4, "torch_dtype": "float16", } (out / "config.json").write_text(json.dumps(cfg, indent=2)) tensors = {} with safe_open(paths.WHISPER / "model.safetensors", "pt") as f: for k in f.keys(): if k.startswith("model.encoder."): tensors["audio_tower." + k[len("model.encoder."):]] = f.get_tensor(k).half().contiguous() for k, v in proj.items(): assert k.startswith("multi_modal_projector."), k tensors[k] = v.float().contiguous() save_file(tensors, out / "model.safetensors", metadata={"format": "pt"}) def convert(out: Path, name: str, quants: list[str], mm_types: list[str]) -> Path: gdir = out.parent / (out.name + "-gguf") gdir.mkdir(parents=True, exist_ok=True) conv = [str(GGUF_PY), str(LLAMA / "convert_hf_to_gguf.py")] bf16 = gdir / f"{name}-BF16.gguf" if not bf16.exists(): subprocess.run([*conv, str(out / "text"), "--outtype", "bf16", "--outfile", str(bf16)], check=True) for q in quants: dst = gdir / f"{name}-{q}.gguf" if q != "BF16" and not dst.exists(): subprocess.run(["llama-quantize", str(bf16), str(dst), q], check=True, capture_output=True) for t in mm_types: dst = gdir / f"mmproj-{name}-{t}.gguf" if not dst.exists(): subprocess.run([*conv, str(out), "--mmproj", "--outtype", t, "--outfile", str(dst)], check=True) return gdir def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--projector", required=True) ap.add_argument("--text", required=True) ap.add_argument("--out", required=True) ap.add_argument("--name", default="Turbo-Kev-Dictate") ap.add_argument("--quants", nargs="*", default=["Q8_0"]) ap.add_argument("--mm", nargs="*", default=["f16"]) a = ap.parse_args() out = Path(a.out) write_text_dir(Path(a.text), out / "text") write_ultravox_dir(Path(a.projector), out / "text", out) print("gguf ->", convert(out, a.name, a.quants, a.mm)) if __name__ == "__main__": sys.exit(main())