Présentation
llama.cpp est un moteur d'inférence C++ pour LLMs optimisé pour CPU et Apple Silicon. Exécute des modèles au format GGUF (Llama, Mistral, Qwen, Gemma, Phi, DeepSeek, etc.) avec quantization de Q2 à Q8. Supporte le GPU offloading, l'inférence par batch et un serveur compatible OpenAI.
Installation
uv pip install llama-cpp-python
# Pour CUDA : pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu124
Inférence basique
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"])
Format Chat
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
llm = Llama(
model_path="model.gguf",
n_gpu_layers=-1, # offload all layers to GPU
n_ctx=8192, # context window
n_threads=8,
)