İçeriğe geç

GGUF K-Quants Block Structure: Q2_K → Q8_K + llama-quantize Perplexity Tablosu

GGUF — llama.cpp'nin native format'ı, CPU/edge inference için yaygın. K-quants block structure (Q2_K → Q8_K), her bit-width için ayrı struct, llama-quantize ile dönüşüm, perplexity-vs-size eğrisi. RTX 4090'da bf16 → Q4_K_M conversion 5 dakika, Q4 GGUF 4.6 GB → CPU/Pi/iPhone deploy.

Şükrü Yusuf KAYA
32 dakikalık okuma
İleri
GGUF K-Quants Block Structure: Q2_K → Q8_K + llama-quantize Perplexity Tablosu

1. K-Quants Tablosu#

QuantBit/weightBlock sizeLlama 8B sizePPL delta (WikiText-2)Use case
Q2_K2.5163.2 GB+%8-12mobile, edge
Q3_K_S3.4164.0 GB+%4-6edge
Q3_K_M3.9164.1 GB+%3-5balanced
Q3_K_L4.3164.3 GB+%2-4quality
Q4_K_S4.5324.5 GB+%1-3balanced
Q4_K_M4.85324.6 GB+%0.8-2cookbook default
Q5_K_S5.5325.3 GB+%0.4-1high quality
Q5_K_M5.7325.4 GB+%0.3-0.7very high
Q6_K6.6166.2 GB+%0.1-0.3near-lossless
Q8_K8.5168.5 GB<%0.1~bf16 equivalent
Cookbook'un kuralı: Q4_K_M mobile/edge için sweet spot. Q5_K_M kalite kritikse. Q8_K test/dev için.

2. K-Quants Block Structure#

Q4_K_M
block (32 weight × ~5 byte):
struct ggml_block_q4_K { ggml_half d; // 16-bit super-block delta ggml_half dmin; // 16-bit super-block dmin uint8_t scales[12]; // 12 bytes: per-block 6-bit scales (8 blocks) uint8_t qs[QK_K / 2]; // 256/2 = 128 bytes: 4-bit packed weights };
  • Super-block: 256 weight
  • Within super-block: 8 sub-blocks × 32 weight
  • Per sub-block: 6-bit scale (eight scales in 12 bytes = 96 bits)
  • Per weight: 4-bit value (sub-block scale ile dequantize)
Anatomik anlam: GGUF K-quants çok katmanlı — supre-block delta + sub-block scale + weight. Bu hiyerarşi sayesinde per-tensor değil per-256-weight scale → dynamic range korunur.
bash
# === bf16 → GGUF → Q4_K_M dönüşüm ===
# 1. Merge LoRA adapter (eğer fine-tuned ise)
python -c "
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
base = AutoModelForCausalLM.from_pretrained('meta-llama/Meta-Llama-3.1-8B-Instruct',
torch_dtype='bfloat16')
m = PeftModel.from_pretrained(base, 'llama-3.1-8b-tr-instruct/final')
m.merge_and_unload().save_pretrained('llama-3.1-8b-tr-merged')
"
 
# 2. HF → GGUF (llama.cpp)
cd llama.cpp
python convert_hf_to_gguf.py ../llama-3.1-8b-tr-merged \
--outfile llama-8b-tr.fp16.gguf
 
# 3. Quantize Q4_K_M
./llama-quantize llama-8b-tr.fp16.gguf llama-8b-tr.Q4_K_M.gguf Q4_K_M
# Output: 4.6 GB (-71% vs bf16)
 
# 4. Test
./llama-cli -m llama-8b-tr.Q4_K_M.gguf -p "İstanbul'un nüfusu nedir?" -n 200
 
# 5. Perplexity ölç (WikiText-2)
./llama-perplexity -m llama-8b-tr.Q4_K_M.gguf -f wikitext-2.test.txt -c 2048
 
# Tipik sonuçlar (Llama 3.1 8B-Instruct):
# bf16 baseline: PPL = 5.93
# Q4_K_M: PPL = 6.04 (+1.9%)
# Q5_K_M: PPL = 5.96 (+0.5%)
# Q8_K: PPL = 5.93 (~0%)
bf16 → GGUF Q4_K_M conversion pipeline

3. CPU Inference Bench#

Llama 3.1 8B Q4_K_M (4.6 GB):
Cihaztok/s
Apple M2 Pro (10-core)28-35
Apple M3 Max (40-core GPU)65-75
Ryzen 7950X (16-core, 64GB DDR5)18-22
iPhone 15 Pro (A17)11-14
Pixel 8 Pro7-10
Raspberry Pi 5 (8GB)2-3
Karar: CPU inference için Q4_K_M sweet — daha aşağıya inmen kalite kaybı, yukarı çıkmak speed kaybı.
✅ Teslim
  1. Kendi FT modelini Q4_K_M'e dönüştür. 2) llama-perplexity ile PPL ölç. 3) CPU/laptop'unda llama-cli ile test et. 4) Sonraki ders: 10.5 — EXL2 Variable Bitrate.

Yorumlar & Soru-Cevap

(0)
Yorum yazmak için giriş yap.
Yorumlar yükleniyor...

İlgili İçerikler