awkweave-0.5b / export_gguf.sh
asterbraid's picture
Awkweave 0.5B: low-memory offline natural-language-to-awk with GGUF and Ollama support
709f2bf verified
Raw History Blame Contribute Delete
2 kB
#!/bin/bash
set -euo pipefail
MODEL_DIR=$1
OUTPUT_DIR=$2
BASENAME=${3:-${BASENAME}}
VENV=/tmp/awkweave-venv
python3 -m venv --system-site-packages "$VENV"
"$VENV/bin/pip" install -q --upgrade 'transformers>=4.46,<5' 'gguf>=0.14' 'sentencepiece'
if [ ! -d /tmp/llama.cpp ]; then
git clone --depth 1 https://github.com/ggml-org/llama.cpp.git /tmp/llama.cpp
fi
cmake -S /tmp/llama.cpp -B /tmp/llama.cpp/build -DGGML_NATIVE=OFF -DLLAMA_CURL=OFF -DCMAKE_BUILD_TYPE=Release
cmake --build /tmp/llama.cpp/build --target llama-quantize llama-completion -j 8
mkdir -p "$OUTPUT_DIR"
"$VENV/bin/python" /tmp/llama.cpp/convert_hf_to_gguf.py "$MODEL_DIR" --outtype f16 --outfile "$OUTPUT_DIR/${BASENAME}.f16.gguf"
/tmp/llama.cpp/build/bin/llama-quantize "$OUTPUT_DIR/${BASENAME}.f16.gguf" "$OUTPUT_DIR/${BASENAME}.Q8_0.gguf" Q8_0
/tmp/llama.cpp/build/bin/llama-quantize "$OUTPUT_DIR/${BASENAME}.f16.gguf" "$OUTPUT_DIR/${BASENAME}.Q4_K_M.gguf" Q4_K_M
rm "$OUTPUT_DIR/${BASENAME}.f16.gguf"
"$VENV/bin/python" - "$MODEL_DIR" <<'PY'
import sys
from pathlib import Path
from transformers import AutoTokenizer
from awkweave.prompts import messages
tokenizer = AutoTokenizer.from_pretrained(sys.argv[1])
prompt = tokenizer.apply_chat_template(messages("Print the last field of every input line.", "a 1\nb 2\n"), tokenize=False, add_generation_prompt=True)
Path("/tmp/awkweave-gguf-prompt.txt").write_text(prompt, encoding="utf-8")
PY
/tmp/llama.cpp/build/bin/llama-completion -m "$OUTPUT_DIR/${BASENAME}.Q4_K_M.gguf" -f /tmp/awkweave-gguf-prompt.txt -n 64 --temp 0 -no-cnv > "$OUTPUT_DIR/gguf-smoke.txt" 2>&1
"$VENV/bin/python" - "$OUTPUT_DIR/gguf-smoke.txt" <<'PY'
import sys
from pathlib import Path
output = Path(sys.argv[1]).read_text(encoding="utf-8")
if "$NF" not in output:
raise SystemExit("GGUF smoke generation did not contain the expected $NF program")
PY
git -C /tmp/llama.cpp rev-parse HEAD > "$OUTPUT_DIR/llama-cpp-commit.txt"
(cd "$OUTPUT_DIR" && sha256sum *.gguf > SHA256SUMS)
ls -lh "$OUTPUT_DIR"