{"id":"gguf","name":"gguf-quantization","summary":"GGUFフォーマットとllama.cpp量子化による効率的なCPU/GPU推論。消費者向けハードウェアやApple Siliconでのモデル展開や、GPUが不要で2ビットから8ビットまでの柔軟な量子化が必要な場合に使用されます。","body":"# GGUF - Quantization Format for llama.cpp\n\nThe GGUF (GPT-Generated Unified Format) is the standard file format for llama.cpp, enabling efficient inference on CPUs, Apple Silicon, and GPUs with flexible quantization options.\n\n## When to use GGUF\n\n**Use GGUF when:**\n- Deploying on consumer hardware (laptops, desktops)\n- Running on Apple Silicon (M1/M2/M3) with Metal acceleration\n- Need CPU inference without GPU requirements\n- Want flexible quantization (Q2_K to Q8_0)\n- Using local AI tools (LM Studio, Ollama, text-generation-webui)\n\n**Key advantages:**\n- **Universal hardware**: CPU, Apple Silicon, NVIDIA, AMD support\n- **No Python runtime**: Pure C/C++ inference\n- **Flexible quantization**: 2-8 bit with various methods (K-quants)\n- **Ecosystem support**: LM Studio, Ollama, koboldcpp, and more\n- **imatrix**: Importance matrix for better low-bit quality\n\n**Use alternatives instead:**\n- **AWQ/GPTQ**: Maximum accuracy with calibration on NVIDIA GPUs\n- **HQQ**: Fast calibration-free quantization for HuggingFace\n- **bitsandbytes**: Simple integration with transformers library\n- **TensorRT-LLM**: Production NVIDIA deployment with maximum speed\n\n## Quick start\n\n### Installation\n\n```bash\n# Clone llama.cpp\ngit clone https://github.com/ggml-org/llama.cpp\ncd llama.cpp\n\n# Build (CPU)\nmake\n\n# Build with CUDA (NVIDIA)\nmake GGML_CUDA=1\n\n# Build with Metal (Apple Silicon)\nmake GGML_METAL=1\n\n# Install Python bindings (optional)\npip install llama-cpp-python\n```\n\n### Convert model to GGUF\n\n```bash\n# Install requirements\npip install -r requirements.txt\n\n# Convert HuggingFace model to GGUF (FP16)\npython convert_hf_to_gguf.py ./path/to/model --outfile model-f16.gguf\n\n# Or specify output type\npython convert_hf_to_gguf.py ./path/to/model \\\n    --outfile model-f16.gguf \\\n    --outtype f16\n```\n\n### Quantize model\n\n```bash\n# Basic quantization to Q4_K_M\n./llama-quantize model-f16.gguf model-q4_k_m.gguf Q4_K_M\n\n# Quantize with importance matrix (better quality)\n./llama-imatrix -m model-f16.gguf -f calibration.txt -o model.imatrix\n./llama-quantize --imatrix model.imatrix model-f16.gguf model-q4_k_m.gguf Q4_K_M\n```\n\n### Run inference\n\n```bash\n# CLI inference\n./llama-cli -m model-q4_k_m.gguf -p \"Hello, how are you?\"\n\n# Interactive mode\n./llama-cli -m model-q4_k_m.gguf --interactive\n\n# With GPU offload\n./llama-cli -m model-q4_k_m.gguf -ngl 35 -p \"Hello!\"\n```\n\n## Quantization types\n\n### K-quant methods (recommended)\n\n| Type | Bits | Size (7B) | Quality | Use Case |\n|------|------|-----------|---------|----------|\n| Q2_K | 2.5 | ~2.8 GB | Low | Extreme compression |\n| Q3_K_S | 3.0 | ~3.0 GB | Low-Med | Memory constrained |\n| Q3_K_M | 3.3 | ~3.3 GB | Medium | Balance |\n| Q4_K_S | 4.0 | ~3.8 GB | Med-High | Good balance |\n| Q4_K_M | 4.5 | ~4.1 GB | High | **Recommended default** |\n| Q5_K_S | 5.0 | ~4.6 GB | High | Quality focused |\n| Q5_K_M | 5.5 | ~4.8 GB | Very High | High quality |\n| Q6_K | 6.0 | ~5.5 GB | Excellent | Near-original |\n| Q8_0 | 8.0 | ~7.2 GB | Best | Maximum quality |\n\n### Legacy methods\n\n| Type | Description |\n|------|-------------|\n| Q4_0 | 4-bit, basic |\n| Q4_1 | 4-bit with delta |\n| Q5_0 | 5-bit, basic |\n| Q5_1 | 5-bit with delta |\n\n**Recommendation**: Use K-quant methods (Q4_K_M, Q5_K_M) for best quality/size ratio.\n\n## Conversion workflows\n\n### Workflow 1: HuggingFace to GGUF\n\n```bash\n# 1. Download model\nhuggingface-cli download meta-llama/Llama-3.1-8B --local-dir ./llama-3.1-8b\n\n# 2. Convert to GGUF (FP16)\npython convert_hf_to_gguf.py ./llama-3.1-8b \\\n    --outfile llama-3.1-8b-f16.gguf \\\n    --outtype f16\n\n# 3. Quantize\n./llama-quantize llama-3.1-8b-f16.gguf llama-3.1-8b-q4_k_m.gguf Q4_K_M\n\n# 4. Test\n./llama-cli -m llama-3.1-8b-q4_k_m.gguf -p \"Hello!\" -n 50\n```\n\n### Workflow 2: With importance matrix (better quality)\n\n```bash\n# 1. Convert to GGUF\npython convert_hf_to_gguf.py ./model --outfile model-f16.gguf\n\n# 2. Create calibration text (diverse samples)\ncat > calibration.txt << 'EOF'\nThe quick brown fox jumps over the lazy dog.\nMachine learning is a subset of artificial intelligence.\nPython is a popular programming language.\n# Add more diverse text samples...\nEOF\n\n# 3. Generate importance matrix\n./llama-imatrix -m model-f16.gguf \\\n    -f calibration.txt \\\n    --chunk 512 \\\n    -o model.imatrix \\\n    -ngl 35  # GPU layers if available\n\n# 4. Quantize with imatrix\n./llama-quantize --imatrix model.imatrix \\\n    model-f16.gguf \\\n    model-q4_k_m.gguf \\\n    Q4_K_M\n```\n\n### Workflow 3: Multiple quantizations\n\n```bash\n#!/bin/bash\nMODEL=\"llama-3.1-8b-f16.gguf\"\nIMATRIX=\"llama-3.1-8b.imatrix\"\n\n# Generate imatrix once\n./llama-imatrix -m $MODEL -f wiki.txt -o $IMATRIX -ngl 35\n\n# Create multiple quantizations\nfor QUANT in Q4_K_M Q5_K_M Q6_K Q8_0; do\n    OUTPUT=\"llama-3.1-8b-${QUANT,,}.gguf\"\n    ./llama-quantize --imatrix $IMATRIX $MODEL $OUTPUT $QUANT\n    echo \"Created: $OUTPUT ($(du -h $OUTPUT | cut -f1))\"\ndone\n```\n\n## Python usage\n\n### llama-cpp-python\n\n```python\nfrom llama_cpp import Llama\n\n# Load model\nllm = Llama(\n    model_path=\"./model-q4_k_m.gguf\",\n    n_ctx=4096,          # Context window\n    n_gpu_layers=35,     # GPU offload (0 for CPU only)\n    n_threads=8          # CPU threads\n)\n\n# Generate\noutput = llm(\n    \"What is machine learning?\",\n    max_tokens=256,\n    temperature=0.7,\n    stop=[\"</s>\", \"\\n\\n\"]\n)\nprint(output[\"choices\"][0][\"text\"])\n```\n\n### Chat completion\n\n```python\nfrom llama_cpp import Llama\n\nllm = Llama(\n    model_path=\"./model-q4_k_m.gguf\",\n    n_ctx=4096,\n    n_gpu_layers=35,\n    chat_format=\"llama-3\"  # Or \"chatml\", \"mistral\", etc.\n)\n\nmessages = [\n    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n    {\"role\": \"user\", \"content\": \"What is Python?\"}\n]\n\nresponse = llm.create_chat_completion(\n    messages=messages,\n    max_tokens=256,\n    temperature=0.7\n)\nprint(response[\"choices\"][0][\"message\"][\"content\"])\n```\n\n### Streaming\n\n```python\nfrom llama_cpp import Llama\n\nllm = Llama(model_path=\"./model-q4_k_m.gguf\", n_gpu_layers=35)\n\n# Stream tokens\nfor chunk in llm(\n    \"Explain quantum computing:\",\n    max_tokens=256,\n    stream=True\n):\n    print(chunk[\"choices\"][0][\"text\"], end=\"\", flush=True)\n```\n\n## Server mode\n\n### Start OpenAI-compatible server\n\n```bash\n# Start server\n./llama-server -m model-q4_k_m.gguf \\\n    --host 0.0.0.0 \\\n    --port 8080 \\\n    -ngl 35 \\\n    -c 4096\n\n# Or with Python bindings\npython -m llama_cpp.server \\\n    --model model-q4_k_m.gguf \\\n    --n_gpu_layers 35 \\\n    --host 0.0.0.0 \\\n    --port 8080\n```\n\n### Use with OpenAI client\n\n```python\nfrom openai import OpenAI\n\nclient = OpenAI(\n    base_url=\"http://localhost:8080/v1\",\n    api_key=\"not-needed\"\n)\n\nresponse = client.chat.completions.create(\n    model=\"local-model\",\n    messages=[{\"role\": \"user\", \"content\": \"Hello!\"}],\n    max_tokens=256\n)\nprint(response.choices[0].message.content)\n```\n\n## Hardware optimization\n\n### Apple Silicon (Metal)\n\n```bash\n# Build with Metal\nmake clean && make GGML_METAL=1\n\n# Run with Metal acceleration\n./llama-cli -m model.gguf -ngl 99 -p \"Hello\"\n\n# Python with Metal\nllm = Llama(\n    model_path=\"model.gguf\",\n    n_gpu_layers=99,     # Offload all layers\n    n_threads=1          # Metal handles parallelism\n)\n```\n\n### NVIDIA CUDA\n\n```bash\n# Build with CUDA\nmake clean && make GGML_CUDA=1\n\n# Run with CUDA\n./llama-cli -m model.gguf -ngl 35 -p \"Hello\"\n\n# Specify GPU\nCUDA_VISIBLE_DEVICES=0 ./llama-cli -m model.gguf -ngl 35\n```\n\n### CPU optimization\n\n```bash\n# Build with AVX2/AVX512\nmake clean && make\n\n# Run with optimal threads\n./llama-cli -m model.gguf -t 8 -p \"Hello\"\n\n# Python CPU config\nllm = Llama(\n    model_path=\"model.gguf\",\n    n_gpu_layers=0,      # CPU only\n    n_threads=8,         # Match physical cores\n    n_batch=512          # Batch size for prompt processing\n)\n```\n\n## Integration with tools\n\n### Ollama\n\n```bash\n# Create Modelfile\ncat > Modelfile << 'EOF'\nFROM ./model-q4_k_m.gguf\nTEMPLATE \"\"\"{{ .System }}\n{{ .Prompt }}\"\"\"\nPARAMETER temperature 0.7\nPARAMETER num_ctx 4096\nEOF\n\n# Create Ollama model\nollama create mymodel -f Modelfile\n\n# Run\nollama run mymodel \"Hello!\"\n```\n\n### LM Studio\n\n1. Place GGUF file in `~/.cache/lm-studio/models/`\n2. Open LM Studio and select the model\n3. Configure context length and GPU offload\n4. Start inference\n\n### text-generation-webui\n\n```bash\n# Place in models folder\ncp model-q4_k_m.gguf text-generation-webui/models/\n\n# Start with llama.cpp loader\npython server.py --model model-q4_k_m.gguf --loader llama.cpp --n-gpu-layers 35\n```\n\n## Best practices\n\n1. **Use K-quants**: Q4_K_M offers best quality/size balance\n2. **Use imatrix**: Always use importance matrix for Q4 and below\n3. **GPU offload**: Offload as many layers as VRAM allows\n4. **Context length**: Start with 4096, increase if needed\n5. **Thread count**: Match physical CPU cores, not logical\n6. **Batch size**: Increase n_batch for faster prompt processing\n\n## Common issues\n\n**Model loads slowly:**\n```bash\n# Use mmap for faster loading\n./llama-cli -m model.gguf --mmap\n```\n\n**Out of memory:**\n```bash\n# Reduce GPU layers\n./llama-cli -m model.gguf -ngl 20  # Reduce from 35\n\n# Or use smaller quantization\n./llama-quantize model-f16.gguf model-q3_k_m.gguf Q3_K_M\n```\n\n**Poor quality at low bits:**\n```bash\n# Always use imatrix for Q4 and below\n./llama-imatrix -m model-f16.gguf -f calibration.txt -o model.imatrix\n./llama-quantize --imatrix model.imatrix model-f16.gguf model-q4_k_m.gguf Q4_K_M\n```\n\n## References\n\n- **[Advanced Usage](references/advanced-usage.md)** - Batching, speculative decoding, custom builds\n- **[Troubleshooting](references/troubleshooting.md)** - Common issues, debugging, benchmarks\n\n## Resources\n\n- **Repository**: https://github.com/ggml-org/llama.cpp\n- **Python Bindings**: https://github.com/abetlen/llama-cpp-python\n- **Pre-quantized Models**: https://huggingface.co/TheBloke\n- **GGUF Converter**: https://huggingface.co/spaces/ggml-org/gguf-my-repo\n- **License**: MIT","author":"@Orchestra-Research","ownerProfile":null,"authorContacts":null,"sourceUrl":"https://github.com/Orchestra-Research/AI-Research-SKILLs/tree/main/10-optimization/gguf","license":"MIT","category":"coding","lang":"en","tokens":3035,"stars":0,"calls30d":2,"claimed":false,"visibility":"public","origin":"crawler","version":"0.1.0","createdAt":"2026-08-22","updatedAt":"2026-08-22","files":[{"path":"references/advanced-usage.md","size":10887,"sha256":"ee7ed3b7c06b393b85109d5aa4816cf7d6e6fd2f0a8fdbb90db5eb03ad301f88"},{"path":"references/troubleshooting.md","size":8904,"sha256":"a83f9df40a6db7857b4b3ce3a982da7e939680ee89079170d4d258392804e09c"}],"requires":{"mcp":[],"tools":[]},"safety":{"flags":[],"scannedAt":"2026-08-22","hasScripts":false,"networkEndpoints":["huggingface.co"]}}