---
name: llama-cpp-799a1cec919d
description: "LLM inference in C/C++ with Python bindings. GPU acceleration via CUDA/Metal/Vulkan, 2-8 bit quantization (GGUF), KV cache, and grammar-based sampling. Run Llama, Mistral, Gemma, Phi locally."
tags: [llama-cpp, gguf, quantization, local-llm, inference, python, zorai]
---
## Overview

llama.cpp is a C++ inference engine for LLMs optimized for CPU and Apple Silicon. Runs GGUF-format models (Llama, Mistral, Qwen, Gemma, Phi, DeepSeek, etc.) with quantization from Q2 to Q8. Supports GPU offloading, batch inference, and OpenAI-compatible server.

## Installation

```bash
uv pip install llama-cpp-python
# For CUDA: pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu124
```

## Basic Inference

```python
from llama_cpp import Llama

llm = Llama(model_path="qwen2.5-1.5b-instruct-q4_k_m.gguf")
output = llm("Q: What is machine learning?
A:", max_tokens=128)
print(output["choices"][0]["text"])
```

## Chat Format

```python
llm = Llama(model_path="model.gguf", chat_format="chatml")
response = llm.create_chat_completion(
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain gradient descent."},
    ]
)
print(response["choices"][0]["message"]["content"])
```

## GPU Offloading

```python
llm = Llama(
    model_path="model.gguf",
    n_gpu_layers=-1,  # offload all layers to GPU
    n_ctx=8192,       # context window
    n_threads=8,
)
```

## References
- [llama.cpp GitHub](https://github.com/ggerganov/llama.cpp)
- [llama-cpp-python docs](https://llama-cpp-python.readthedocs.io/)

---
Source attribution: mkurman
Original project: https://github.com/mkurman/zorai/blob/d06ee7597049567dab92db22d41b2764cf89094a/skills/scientific-skills/llama-cpp/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Dave AI.

## Upstream license notice

MIT License

Copyright (c) 2026 zorai contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Dave AI resource metadata

Creator / publisher: mkurman
Original project: https://github.com/mkurman/zorai/blob/d06ee7597049567dab92db22d41b2764cf89094a/skills/scientific-skills/llama-cpp/SKILL.md
Attribution basis: Source repository record

Fields of work: Development & Engineering
Roles: Data Engineer, ML / AI Engineer
Specialties: Backend
Tools: Cuda, Metal, Vulkan
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 9c993485cc676af472e1ac8d86ec8a2cbd8622275c21e4f2fa8dc469bdeabbe0
