-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathllamacpp_benchmark.py
More file actions
400 lines (338 loc) · 13.9 KB
/
Copy pathllamacpp_benchmark.py
File metadata and controls
400 lines (338 loc) · 13.9 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env python3
"""
Benchmark script for llama.cpp server using HTTP API.
This script connects to a running llama.cpp server and benchmarks its performance
across different context sizes.
Usage:
# Start llama.cpp server first:
# ./llama-server -m model.gguf --host 0.0.0.0 --port 8080
# Then run benchmark (default localhost:8080):
python llamacpp_benchmark.py gpt-oss:20b
# Or specify custom host and port:
python llamacpp_benchmark.py gpt-oss:20b --host localhost --port 9000
"""
import argparse
import sys
import time
from pathlib import Path
from typing import Dict, Optional
import requests
from benchmark_common import (
add_throughput_metrics,
create_output_directory,
find_context_files,
format_hardware_string,
get_hardware_info,
make_cache_buster,
print_benchmark_summary,
run_benchmark_peak,
run_benchmark_peak_per_run,
save_all_outputs,
save_generated_text,
setup_common_args,
)
def test_server_connection(server_url: str) -> bool:
"""Test if the llama.cpp server is running and accessible."""
try:
response = requests.get(f"{server_url}/health", timeout=5)
if response.status_code == 200:
return True
except requests.exceptions.RequestException as e:
print(f"Error connecting to server: {e}")
return False
def get_server_info(server_url: str) -> Dict:
"""Get model and server information from llama.cpp server."""
try:
# Try to get model metadata
response = requests.get(f"{server_url}/props", timeout=5)
if response.status_code == 200:
return response.json()
except (requests.exceptions.RequestException, Exception):
pass
# Return empty dict if no info available
return {}
def _hardware_folder_label(label: str) -> str:
"""Return a compact filesystem-safe hardware label."""
return "".join(ch for ch in label.replace("Apple ", "") if ch.isalnum())
def benchmark_llamacpp(
server_url: str,
context_file: Path,
max_tokens: int = 128,
timeout: int = 3600,
cold_prefill: bool = True,
_run_idx: Optional[int] = None,
) -> Optional[Dict]:
"""Benchmark llama.cpp server with a given context file.
Uses the OpenAI-compatible /v1/chat/completions endpoint so the server
applies the model's chat template. Hitting the raw /completion endpoint
with a big chunk of document text causes the model to occasionally sample
EOS on its first step (there is no "assistant is responding" framing), in
which case predicted_n=1 and predicted_ms≈0, producing a bogus
~1,000,000 tokens/sec reading.
Args:
server_url: URL of the llama.cpp server
context_file: Path to the context file
max_tokens: Maximum number of tokens to generate
timeout: Request timeout in seconds
cold_prefill: Prevent server-side KV cache reuse (default: True)
Returns:
Dictionary with benchmark results or None if failed
"""
with open(context_file, "r", encoding="utf-8") as f:
prompt = f.read()
if cold_prefill:
prompt = make_cache_buster() + prompt
elif _run_idx is not None:
prompt = make_cache_buster(run_idx=_run_idx) + prompt
# OpenAI-style chat payload. llama.cpp accepts `cache_prompt` as an
# extension field on this endpoint too.
payload = {
"model": "default",
"messages": [{"role": "user", "content": prompt}],
"max_tokens": max_tokens,
"temperature": 0.7,
"top_p": 0.95,
"stream": True,
"cache_prompt": not cold_prefill,
}
start_time = time.time()
first_token_time = None
generated_text = ""
finish_reason: Optional[str] = None
timings: Dict = {}
try:
import json
response = requests.post(
f"{server_url}/v1/chat/completions",
json=payload,
timeout=timeout,
stream=True,
)
response.raise_for_status()
for line in response.iter_lines():
if not line:
continue
line = line.decode("utf-8")
if not line.startswith("data: "):
continue
data_str = line[6:]
if data_str.strip() == "[DONE]":
break
try:
chunk = json.loads(data_str)
except json.JSONDecodeError:
continue
choices = chunk.get("choices") or []
if choices:
delta = choices[0].get("delta") or {}
content = delta.get("content") or ""
if content:
if first_token_time is None:
first_token_time = time.time()
generated_text += content
if choices[0].get("finish_reason"):
finish_reason = choices[0]["finish_reason"]
# llama.cpp-specific: final chunk includes a `timings` object.
if chunk.get("timings"):
timings = chunk["timings"]
except requests.exceptions.RequestException as e:
print(f"Error during benchmark: {e}")
return None
total_time = time.time() - start_time
time_to_first_token = (first_token_time - start_time) if first_token_time else 0.0
prompt_tokens = timings.get("prompt_n", 0)
generation_tokens = timings.get("predicted_n", 0)
prompt_ms = timings.get("prompt_ms", 0)
predict_ms = timings.get("predicted_ms", 0)
prompt_time = prompt_ms / 1000.0
predict_time = predict_ms / 1000.0
# Guard against pathological results (e.g. model sampled EOS on first
# step so predicted_n=1 and predicted_ms≈0). Require at least 2 decoded
# tokens and a non-trivial decode duration before trusting the TPS ratio.
if generation_tokens < 2 or predict_time < 0.01:
print(
f" Warning: degenerate run (predicted_n={generation_tokens}, "
f"predicted_ms={predict_ms}, finish_reason={finish_reason}); discarding"
)
return None
prompt_tps = prompt_tokens / prompt_time if prompt_time > 0 else 0
generation_tps = generation_tokens / predict_time if predict_time > 0 else 0
result = {
"context_size": context_file.stem,
"prompt_tokens": prompt_tokens,
"generation_tokens": generation_tokens,
"prompt_time": prompt_time,
"prompt_eval_duration": prompt_time,
"time_to_first_token": time_to_first_token,
"eval_duration": predict_time,
"total_time": total_time,
"prompt_tps": prompt_tps,
"generation_tps": generation_tps,
"generated_text": generated_text,
"wall_time": total_time,
}
return add_throughput_metrics(result, prompt_text=prompt)
def main() -> int:
"""Main function to run the benchmark."""
parser = argparse.ArgumentParser(description="Benchmark llama.cpp server across different context sizes")
parser.add_argument(
"model",
help="Model name or identifier (used for display purposes)",
)
parser.add_argument(
"--host",
default="localhost",
help="Host of the llama.cpp server (default: localhost)",
)
parser.add_argument(
"--port",
type=int,
default=8080,
help="Port of the llama.cpp server (default: 8080)",
)
parser.add_argument(
"--cold-prefill",
action=argparse.BooleanOptionalAction,
default=True,
help="Prevent server-side KV cache reuse for cold-prefill numbers (default: enabled; "
"use --no-cold-prefill for cached/warm-reuse numbers)",
)
parser.add_argument(
"--no-kl-capture",
action="store_true",
help="Skip top-K logprob capture used by compare_benchmarks --kl-baseline",
)
parser.add_argument(
"--server-hardware",
help="Remote server hardware label to store in results (e.g. 'GB10' or 'NVIDIA RTX 5090')",
)
parser.add_argument(
"--server-memory-gb",
type=float,
help="Remote server memory in GB to store in results",
)
parser.add_argument(
"--server-cores",
type=int,
help="Remote server CPU/core count to store in results",
)
# Add common arguments
setup_common_args(parser)
args = parser.parse_args()
# Construct server URL from host and port
server_url = f"http://{args.host}:{args.port}"
# Test server connection
print(f"Testing connection to llama.cpp server at {server_url}...")
if not test_server_connection(server_url):
print(f"Error: Cannot connect to llama.cpp server at {server_url}")
print("Please ensure the server is running:")
print(" ./llama-server -m model.gguf --host 0.0.0.0 --port 8080")
return 1
print("Successfully connected to llama.cpp server")
# Get server info
server_info = get_server_info(server_url)
# Use the model name from args, fallback to server info if available
model_name = args.model or server_info.get("default_generation_settings", {}).get("model", "llama.cpp model")
# Get hardware info
hardware_info = get_hardware_info()
if args.server_hardware:
hardware_info["client_chip"] = hardware_info.get("chip")
hardware_info["client_model"] = hardware_info.get("model")
hardware_info["chip"] = args.server_hardware
hardware_info["model"] = "Remote llama.cpp server"
hardware_info["api_endpoint"] = server_url
if args.server_memory_gb is not None:
hardware_info["memory_gb"] = args.server_memory_gb
if args.server_cores is not None:
hardware_info["total_cores"] = args.server_cores
hardware_str = format_hardware_string(hardware_info)
print(f"\nHardware: {hardware_str}")
print(f"Model: {model_name}")
print(f"Max tokens: {args.max_tokens}")
print(f"Cold prefill: {'enabled (cache_prompt=false)' if args.cold_prefill else 'disabled (cache reuse allowed)'}")
# Find context files
context_files = find_context_files(args.contexts)
if not context_files:
return 1
# Create output directory using common function
machine_name = _hardware_folder_label(args.server_hardware) if args.server_hardware else None
output_dir = create_output_directory("llamacpp", args.model, cold_prefill=args.cold_prefill, machine_name=machine_name)
# Capture top-K logprobs on a fixed reference text for later KL comparison
if not args.no_kl_capture:
from kl_capture import DEFAULT_REF_FILE, capture_logprobs_llamacpp, save_logprobs
ref_path = Path(DEFAULT_REF_FILE)
if not ref_path.exists():
print(f"\nKL capture skipped: reference file {DEFAULT_REF_FILE} not found")
else:
print(f"\nCapturing top-K logprobs from {DEFAULT_REF_FILE} for KL comparison...")
try:
kl_data = capture_logprobs_llamacpp(server_url, ref_path, timeout=args.timeout)
kl_data["engine"] = "llamacpp"
kl_data["model"] = args.model
save_logprobs(kl_data, output_dir / "logprobs.json")
print(
f" Captured {kl_data['num_positions']} positions × top-{kl_data['top_k']} "
f"→ {output_dir / 'logprobs.json'}"
)
except Exception as e:
print(f" KL capture failed (continuing): {e}")
results = []
# Run benchmarks
import time
start_time = time.time()
if args.cold_prefill:
for context_file in context_files:
print(f"\n{'=' * 50}")
print(f"Benchmarking {context_file.name}...")
print(f"{'=' * 50}")
result = run_benchmark_peak(
benchmark_llamacpp,
server_url,
context_file,
args.max_tokens,
args.timeout,
cold_prefill=args.cold_prefill,
n_runs=args.runs,
)
if result:
results.append(result)
# Print results
print(f"\nResults for {context_file.name}:")
print(f" Prompt tokens: {result['prompt_tokens']}")
print(f" Generated tokens: {result['generation_tokens']}")
print(f" Time to first token: {result['time_to_first_token']:.2f}s")
print(f" Prompt time: {result['prompt_time']:.2f}s")
print(f" Generation time: {result['eval_duration']:.2f}s")
print(f" Total time: {result['total_time']:.2f}s")
print(f" Prompt TPS: {result['prompt_tps']:.1f} tokens/sec")
print(f" Generation TPS: {result['generation_tps']:.1f} tokens/sec")
# Save response if requested
if args.save_responses:
response_file = output_dir / f"response_{context_file.stem}.txt"
save_generated_text(result, model_name, response_file, framework="llama.cpp")
else:
results = run_benchmark_peak_per_run(
benchmark_llamacpp,
context_files=context_files,
n_runs=args.runs,
server_url=server_url,
max_tokens=args.max_tokens,
timeout=args.timeout,
cold_prefill=args.cold_prefill,
)
if args.save_responses:
for result in results:
ctx_name = result.get("context_size", "unknown")
response_file = output_dir / f"response_{ctx_name}.txt"
save_generated_text(result, model_name, response_file, framework="llama.cpp")
total_benchmark_time = time.time() - start_time
if not results:
print("\nNo successful benchmark results")
return 1
# Save all outputs using common function
save_all_outputs(results, output_dir, model_name, "llama.cpp", hardware_info, args)
# Print summary using common function
print_benchmark_summary(results, model_name, "llama.cpp", hardware_info, output_dir, total_benchmark_time)
return 0
if __name__ == "__main__":
sys.exit(main())