File size: 16,147 Bytes
932bc69 | 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 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | #!/usr/bin/env python3
"""Unified evaluation CLI for speculative decoding benchmarking.
Modes:
throughput Max throughput run for acceptance rates
sweep Full pipeline (gen-len, sweep, CSV)
Examples:
python evaluate.py --target http://localhost:8000/v1 throughput
python evaluate.py --target http://localhost:8000/v1 sweep
python evaluate.py --target http://localhost:8000/v1 sweep \\
--subsets "HumanEval,qa" --gen-kwargs '{"temperature":0.6}'
# SPEED-Bench (run prepare_speedbench.py once first to split data):
python evaluate.py --target http://localhost:8000/v1 throughput \\
--dataset speedbench/qualitative \\
--speedbench-data-dir ./speedbench_data
python evaluate.py --target http://localhost:8000/v1 throughput \\
--dataset speedbench/qualitative/coding \\
--speedbench-data-dir ./speedbench_data
"""
from __future__ import annotations
import argparse
import json
import logging
import shlex
import sys
from datetime import datetime, timezone
from pathlib import Path
from urllib.error import URLError
from urllib.request import urlopen
from perf_utils import (
BASE_CSV_COLUMNS,
CsvWriter,
acceptance_csv_columns,
check_dependencies,
extract_spec_decode_metrics,
fetch_metrics,
parse_gen_kwargs,
parse_gen_len_results,
parse_prometheus_metrics,
parse_sweep_results,
print_acceptance_report,
run_guidellm,
)
from speculators.provenance import (
atomic_write,
find_package_repo,
git_sha,
package_versions,
)
logger = logging.getLogger("evaluate")
DEFAULT_DATASET = "RedHatAI/speculator_benchmarks"
DEFAULT_SUBSETS = (
"HumanEval,math_reasoning,qa,question,rag,"
"summarization,tool_call,translation,writing"
)
DEFAULT_MAX_CONCURRENCY = 128
DEFAULT_MAX_REQUESTS = 200
DEFAULT_MAX_OUTPUT_TOKENS = 4096
DEFAULT_GEN_LEN_RATE = 128
DEFAULT_SWEEP_RATE = 10
DEFAULT_DATA_COLUMN_MAPPER = (
"kind=generative_column_mapper,column_mappings.text_column=prompt"
)
# ---------------------------------------------------------------------------
# SPEED-Bench constants
# ---------------------------------------------------------------------------
_SPEEDBENCH_COLUMN_MAPPER = (
"kind=generative_column_mapper,column_mappings.text_column=turns"
)
def _fetch_model_name(target: str) -> str | None:
base = target.rstrip("/")
if not base.endswith("/v1"):
base += "/v1"
url = f"{base}/models"
try:
with urlopen(url, timeout=10) as resp: # noqa: S310
data = json.loads(resp.read())
models = data.get("data", [])
if models:
return models[0].get("id")
except (URLError, json.JSONDecodeError, OSError) as e:
logger.warning("Could not fetch model name from %s: %s", url, e)
return None
def _sanitize_dir_name(name: str) -> str:
return name.replace("/", "_").replace(" ", "_")
def save_eval_provenance(output_dir: Path) -> None:
"""Write ``eval_command.txt`` into *output_dir*.
Records the full command line, timestamp, and package versions so the
eval can be reproduced. Best-effort — never blocks the eval on failure.
"""
try:
sha = git_sha(find_package_repo("speculators"))
versions = package_versions()
header = "\n".join(
[
f"# Timestamp: {datetime.now(timezone.utc).isoformat()}",
f"# Git SHA: {sha}",
*versions,
]
)
atomic_write(
output_dir / "eval_command.txt",
f"{header}\n{shlex.join(sys.argv)}\n",
)
except OSError:
logger.warning("Failed to save eval_command.txt", exc_info=True)
def _require_metrics(metrics_url: str) -> list:
text = fetch_metrics(metrics_url)
if text is None:
logger.error("Failed to fetch metrics from %s", metrics_url)
sys.exit(1)
return parse_prometheus_metrics(text)
def _resolve_speedbench(
spec: str,
data_dir: Path,
) -> list[tuple[str, Path]]:
"""Resolve a ``speedbench/<config>[/<category>[/<subcategory>]]`` spec.
Returns ``(label, path)`` pairs for pre-split JSONL files produced by
``scripts/evaluate/prepare_speedbench.py``. Run that script once after
NVIDIA's ``prepare.py`` to create per-category/subcategory files.
"""
parts = spec.removeprefix("speedbench/").split("/")
config = parts[0]
rest = parts[1:]
# Build glob pattern matching prepare_speedbench.py's naming convention:
# qualitative/coding → qualitative_coding*.jsonl
# throughput_1k/high_entropy → throughput_1k_high_entropy_*.jsonl
# throughput_1k/high_entropy/code_completion
# → throughput_1k_high_entropy__code_completion*.jsonl
# Entropy level and subcategory are separated by "__" in the filename.
if not rest:
suffix = ""
elif len(rest) == 1:
suffix = rest[0]
else:
suffix = rest[0] + "__" + "_".join(rest[1:])
pattern = f"{config}_{suffix}*.jsonl" if suffix else f"{config}_*.jsonl"
files = sorted(data_dir.glob(pattern))
if not files:
logger.error(
"--speedbench-data-dir='%s': no files matching '%s'.\n"
"Run scripts/evaluate/prepare_speedbench.py first.",
data_dir,
pattern,
)
sys.exit(1)
results = []
for path in files:
# Derive label: qualitative_coding → speedbench/qualitative/coding
stem = path.stem.removeprefix(f"{config}_")
label = f"speedbench/{config}/{stem.replace('__', '/')}"
results.append((label, path))
logger.info(" %s: %s", label, path.name)
return results
def _run_subset(
subset: str,
args: argparse.Namespace,
*,
is_sweep: bool,
metrics_url: str,
artifacts_dir: Path,
output_dir: Path,
guidellm_common: dict,
acceptance_csv: CsvWriter | None,
perf_csv: CsvWriter | None,
) -> tuple[CsvWriter | None, CsvWriter | None, int | None]:
"""Run benchmark for one subset.
*subset* is the human-readable label used for output file names and the
acceptance CSV. When ``guidellm_common["dataset"]`` is a local file path
the ``--data-args`` flag is suppressed automatically; when it is an HF
dataset ID *subset* is also passed as the data-args filter so guidellm
loads just that split.
"""
logger.info("[%s] Starting", subset)
safe = subset.replace("/", "_").replace(" ", "_")
max_tokens = args.max_output_tokens
# For local JSONL files (SPEED-Bench) the dataset path IS the file —
# no --data-args needed. For HF datasets subset name doubles as the filter.
guidellm_subset = None if Path(guidellm_common["dataset"]).exists() else subset
if is_sweep:
gen_len_dir = artifacts_dir / "gen_len"
gen_len_dir.mkdir(parents=True, exist_ok=True)
gen_len_output = gen_len_dir / f"gen_len_{safe}.json"
run_guidellm(
**guidellm_common,
subset=guidellm_subset,
profile="throughput",
rate=args.gen_len_rate,
max_requests=None,
output_path=gen_len_output,
max_tokens=args.max_output_tokens,
gen_kwargs=parse_gen_kwargs(args.gen_kwargs),
)
mapping = parse_gen_len_results(
[gen_len_output],
gen_len_dir / f"max_tokens_{safe}.json",
)
key = guidellm_subset if guidellm_subset else safe
max_tokens = mapping.get(key, max_tokens)
logger.info("[%s] max_tokens=%d", subset, max_tokens)
baseline = _require_metrics(metrics_url)
profile = "sweep" if is_sweep else "throughput"
run_output = artifacts_dir / f"run_{safe}.json"
run_guidellm(
**guidellm_common,
subset=guidellm_subset,
rate=args.sweep_rate if is_sweep else args.gen_len_rate,
profile=profile,
max_requests=args.max_requests,
output_path=run_output,
max_tokens=max_tokens,
gen_kwargs=parse_gen_kwargs(args.gen_kwargs),
)
current = _require_metrics(metrics_url)
spec = extract_spec_decode_metrics(
current,
baseline_metrics=baseline,
)
has_spec = spec and spec.get("num_drafts", 0) > 0
if has_spec:
spec["subset"] = subset
print_acceptance_report(spec)
if acceptance_csv is None:
acceptance_csv = CsvWriter(
output_dir / "acceptance.csv",
["subset"] + acceptance_csv_columns(spec),
)
acceptance_csv.append(spec)
else:
logger.warning("[%s] No speculative decoding metrics found", subset)
rows = parse_sweep_results(
run_output,
spec if has_spec else None,
include_throughput=not is_sweep,
)
for row in rows:
row["subset"] = subset
if rows:
if perf_csv is None:
acc_cols = acceptance_csv_columns(spec) if has_spec else []
cols = BASE_CSV_COLUMNS + acc_cols
perf_csv = CsvWriter(
output_dir / "perf_results.csv",
cols,
)
perf_csv.append_rows(rows)
logger.info("[%s] Complete", subset)
return acceptance_csv, perf_csv, max_tokens if is_sweep else None
def run_benchmark(args: argparse.Namespace) -> None:
check_dependencies()
is_sweep = args.mode == "sweep"
metrics_url = args.target.rstrip("/").removesuffix("/v1") + "/metrics"
output_dir = Path(args.output_dir)
artifacts_dir = output_dir / "artifacts"
artifacts_dir.mkdir(parents=True, exist_ok=True)
save_eval_provenance(output_dir)
if not (output_dir / "vllm_command.txt").exists():
logger.info(
"No vllm_command.txt found. To co-locate vLLM provenance "
"with eval results: launch_vllm.py --provenance-dir %s",
output_dir,
)
acceptance_csv = None
perf_csv = None
all_max_tokens: dict[str, int] = {}
# Build a flat list of (label, guidellm_common) to run uniformly.
# _run_subset auto-detects whether --data-args is needed from the dataset path.
dataset_spec = args.dataset
run_items: list[tuple[str, dict]] = []
if dataset_spec.startswith("speedbench/"):
if not getattr(args, "speedbench_data_dir", None):
logger.error(
"--speedbench-data-dir is required for speedbench/ datasets.\n"
"Run scripts/evaluate/prepare_speedbench.py first, then add"
" --speedbench-data-dir <dir>.",
)
sys.exit(1)
pairs = _resolve_speedbench(dataset_spec, Path(args.speedbench_data_dir))
for label, local_path in pairs:
run_items.append(
(
label,
{
"target": args.target,
"dataset": str(local_path),
"data_column_mapper": _SPEEDBENCH_COLUMN_MAPPER,
"max_concurrency": args.max_concurrency,
},
)
)
else:
guidellm_common = {
"target": args.target,
"dataset": dataset_spec,
"data_column_mapper": args.data_column_mapper,
"max_concurrency": args.max_concurrency,
}
for subset in [s.strip() for s in args.subsets.split(",") if s.strip()]:
run_items.append((subset, guidellm_common))
logger.info(
"Mode: %s | %d subsets | Output: %s", args.mode, len(run_items), output_dir
)
for label, common in run_items:
acceptance_csv, perf_csv, mt = _run_subset(
label,
args,
is_sweep=is_sweep,
metrics_url=metrics_url,
artifacts_dir=artifacts_dir,
output_dir=output_dir,
guidellm_common=common,
acceptance_csv=acceptance_csv,
perf_csv=perf_csv,
)
if mt is not None:
all_max_tokens[label] = mt
if acceptance_csv is None:
logger.error("No acceptance metrics collected from any subset")
sys.exit(1)
if is_sweep and all_max_tokens:
with (output_dir / "max_tokens.json").open("w") as f:
json.dump(all_max_tokens, f, indent=2)
logger.info("Benchmarking complete! Results: %s", output_dir)
def main() -> None:
logging.basicConfig(
level=logging.INFO,
format="[%(levelname)s] %(message)s",
stream=sys.stderr,
)
parser = argparse.ArgumentParser(
prog="evaluate",
description="Speculative decoding performance evaluation toolkit.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=(
"examples:\n"
" python evaluate.py --target http://localhost:8000/v1 throughput\n"
" python evaluate.py --target http://localhost:8000/v1 sweep\n"
" python evaluate.py --target http://localhost:8000/v1 sweep "
'--subsets "HumanEval,qa"\n'
),
)
parser.add_argument(
"mode",
choices=["throughput", "sweep"],
help=(
"throughput: max-rate run for acceptance rates; "
"sweep: full benchmarking pipeline"
),
)
parser.add_argument(
"--target",
required=True,
help="vLLM server endpoint (e.g. http://localhost:8000/v1)",
)
parser.add_argument(
"--dataset",
default=DEFAULT_DATASET,
help=f"HF dataset ID or local directory (default: {DEFAULT_DATASET})",
)
parser.add_argument(
"--subsets",
default=DEFAULT_SUBSETS,
help="Comma-separated subset names (default: all 9 standard subsets)",
)
parser.add_argument(
"--output-dir",
default=None,
help="Output directory (default: <model_name>_TIMESTAMP)",
)
parser.add_argument(
"--max-concurrency",
type=int,
default=DEFAULT_MAX_CONCURRENCY,
help=f"Max concurrent requests (default: {DEFAULT_MAX_CONCURRENCY})",
)
parser.add_argument(
"--max-requests",
type=int,
default=DEFAULT_MAX_REQUESTS,
help=f"Max requests per sweep point (default: {DEFAULT_MAX_REQUESTS})",
)
parser.add_argument(
"--max-output-tokens",
type=int,
default=DEFAULT_MAX_OUTPUT_TOKENS,
help=(
"Maximum generated tokens per request "
f"(default: {DEFAULT_MAX_OUTPUT_TOKENS})"
),
)
parser.add_argument(
"--gen-len-rate",
type=int,
default=DEFAULT_GEN_LEN_RATE,
help=f"Request rate for gen-len estimation (default: {DEFAULT_GEN_LEN_RATE})",
)
parser.add_argument(
"--sweep-rate",
type=int,
default=DEFAULT_SWEEP_RATE,
help=f"Number of sweep rate points (default: {DEFAULT_SWEEP_RATE})",
)
parser.add_argument(
"--gen-kwargs",
default="",
help="Flat JSON with generation kwargs, e.g. '{\"temperature\":0.6}'",
)
parser.add_argument(
"--data-column-mapper",
default=DEFAULT_DATA_COLUMN_MAPPER,
help="Column mapping for guidellm in typed key=value format"
f" (default: {DEFAULT_DATA_COLUMN_MAPPER})",
)
parser.add_argument(
"--speedbench-data-dir",
default=None,
dest="speedbench_data_dir",
help=(
"Path to directory produced by SPEED-Bench prepare.py. "
"Required when --dataset is a speedbench/ spec."
),
)
args = parser.parse_args()
if args.output_dir is None:
model_name = _fetch_model_name(args.target)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
if model_name:
logger.info("Detected model: %s", model_name)
args.output_dir = f"{_sanitize_dir_name(model_name)}_{timestamp}"
else:
args.output_dir = f"perf_results_{timestamp}"
run_benchmark(args)
if __name__ == "__main__":
main()
|