-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllama_cpp_benchmark.py
More file actions
47 lines (38 loc) · 1.21 KB
/
Copy pathllama_cpp_benchmark.py
File metadata and controls
47 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# llama_cpp_benchmark.py
import time
from llama_cpp import Llama
# === Config ===
MODEL_PATH = "./shared/models/mistral-7b-instruct-v0.1.Q4_K_M.gguf"
PROMPT = "Q: What is Hugging Face?\nA:"
MAX_TOKENS = 32
N_GPU_LAYERS = 16 # Try 16, 24, 33
N_THREADS = 4
N_BATCH = 32
# === Load model ===
print(f"Loading model: {MODEL_PATH}")
llm = Llama(
model_path=MODEL_PATH,
n_ctx=2048,
n_gpu_layers=N_GPU_LAYERS,
n_threads=N_THREADS,
n_batch=N_BATCH,
use_mmap=True,
use_mlock=True,
verbose=False
)
# === Run benchmark ===
print(f"\n>>> Running with n_gpu_layers={N_GPU_LAYERS}, max_tokens={MAX_TOKENS}")
start_time = time.time()
response = llm(PROMPT, max_tokens=MAX_TOKENS, echo=True, stream=False)
end_time = time.time()
tokens_generated = len(response["choices"][0]["text"].split())
total_time = end_time - start_time
tokens_per_sec = tokens_generated / total_time
# === Output result ===
print("\n>>> Output:")
print(response["choices"][0]["text"])
print("\n>>> Benchmark Results:")
print(f" Total time: {total_time:.2f} sec")
print(f" Tokens generated: {tokens_generated}")
print(f" Speed: {tokens_per_sec:.2f} tokens/sec")
print(f" Prompt length: {len(PROMPT.split())} tokens")