#!/bin/sh

if [ -z "$1" ]; then
    echo "Error: no model name"
    echo "Usage: $0 <model_name> [quantize_level: Q8_0 | Q4_K_M | Q4_K_S | Q3_K_M | Q3_K_S | Q2_K]"
    echo "Demo: $0 Qwen2.5-0.5B-Instruct Q4_K_M"
    exit 1
fi

BASE_MODEL="$1"
QUANTIZE_LEVEL="${2:-Q4_K_M}"

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd "$SCRIPT_DIR/../." || { echo "找不到 . 目录"; exit 1; }

BASE_MODEL_PATH="./models/${BASE_MODEL}"
GGUF_MODEL_PATH="./outputs/quant/${BASE_MODEL}.gguf"
FINAL_MODEL_PATH="./outputs/quant/${BASE_MODEL}-${QUANTIZE_LEVEL}.gguf"

./llama.cpp/convert_hf_to_gguf.py ${BASE_MODEL_PATH} --outfile ${GGUF_MODEL_PATH}

./llama.cpp/build/bin/llama-quantize \
    --leave-output-tensor --token-embedding-type bf16 \
    ${GGUF_MODEL_PATH} \
    ${FINAL_MODEL_PATH} \
    ${QUANTIZE_LEVEL}