|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Single-node vLLM baseline benchmark for Ray Data LLM batch inference. |
| 4 | +
|
| 5 | +Measures throughput and supports env-driven thresholds and |
| 6 | +JSON artifact output. |
| 7 | +""" |
| 8 | +import json |
| 9 | +import os |
| 10 | +import sys |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +import ray |
| 15 | +from ray.llm._internal.batch.benchmark.dataset import ShareGPTDataset |
| 16 | +from ray.llm._internal.batch.benchmark.benchmark_processor import ( |
| 17 | + Mode, |
| 18 | + VLLM_SAMPLING_PARAMS, |
| 19 | + benchmark, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +# Benchmark constants |
| 24 | +NUM_REQUESTS = 1000 |
| 25 | +MODEL_ID = "facebook/opt-1.3b" |
| 26 | +BATCH_SIZE = 64 |
| 27 | +CONCURRENCY = 1 |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(autouse=True) |
| 31 | +def disable_vllm_compile_cache(monkeypatch): |
| 32 | + """Disable vLLM compile cache to avoid cache corruption.""" |
| 33 | + monkeypatch.setenv("VLLM_DISABLE_COMPILE_CACHE", "1") |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture(autouse=True) |
| 37 | +def cleanup_ray_resources(): |
| 38 | + """Cleanup Ray resources between tests.""" |
| 39 | + yield |
| 40 | + ray.shutdown() |
| 41 | + |
| 42 | + |
| 43 | +def _get_float_env(name: str, default: float | None = None) -> float | None: |
| 44 | + value = os.getenv(name) |
| 45 | + if value is None or value == "": |
| 46 | + return default |
| 47 | + try: |
| 48 | + return float(value) |
| 49 | + except ValueError: |
| 50 | + raise AssertionError(f"Invalid float for {name}: {value}") |
| 51 | + |
| 52 | + |
| 53 | +def test_single_node_baseline_benchmark(): |
| 54 | + """ |
| 55 | + Single-node baseline benchmark: facebook/opt-1.3b, TP=1, PP=1, 1000 prompts. |
| 56 | +
|
| 57 | + Logs BENCHMARK_* metrics and optionally asserts perf thresholds from env: |
| 58 | + - RAY_DATA_LLM_BENCHMARK_MIN_THROUGHPUT (req/s) |
| 59 | + - RAY_DATA_LLM_BENCHMARK_MAX_LATENCY_S (seconds) |
| 60 | + Writes JSON artifact to RAY_LLM_BENCHMARK_ARTIFACT_PATH if set. |
| 61 | + """ |
| 62 | + # Dataset setup |
| 63 | + dataset_path = os.getenv( |
| 64 | + "RAY_LLM_BENCHMARK_DATASET_PATH", "/tmp/ray_llm_benchmark_dataset" |
| 65 | + ) |
| 66 | + |
| 67 | + dataset = ShareGPTDataset( |
| 68 | + dataset_path=dataset_path, |
| 69 | + seed=0, |
| 70 | + hf_dataset_id="Crystalcareai/Code-feedback-sharegpt-renamed", |
| 71 | + hf_split="train", |
| 72 | + truncate_prompt=2048, |
| 73 | + ) |
| 74 | + |
| 75 | + print(f"Loading {NUM_REQUESTS} prompts from ShareGPT dataset...") |
| 76 | + prompts = dataset.sample(num_requests=NUM_REQUESTS) |
| 77 | + print(f"Loaded {len(prompts)} prompts") |
| 78 | + |
| 79 | + ds = ray.data.from_items(prompts) |
| 80 | + |
| 81 | + # Benchmark config (single node, TP=1, PP=1) |
| 82 | + print( |
| 83 | + f"\nBenchmark: {MODEL_ID}, batch={BATCH_SIZE}, concurrency={CONCURRENCY}, TP=1, PP=1" |
| 84 | + ) |
| 85 | + |
| 86 | + # Use benchmark processor to run a single-node vLLM benchmark |
| 87 | + result = benchmark( |
| 88 | + Mode.VLLM_ENGINE, |
| 89 | + ds, |
| 90 | + batch_size=BATCH_SIZE, |
| 91 | + concurrency=CONCURRENCY, |
| 92 | + model=MODEL_ID, |
| 93 | + sampling_params=VLLM_SAMPLING_PARAMS, |
| 94 | + pipeline_parallel_size=1, |
| 95 | + tensor_parallel_size=1, |
| 96 | + distributed_executor_backend="mp", |
| 97 | + ) |
| 98 | + |
| 99 | + result.show() |
| 100 | + |
| 101 | + # Assertions and metrics |
| 102 | + assert result.samples == len(prompts) |
| 103 | + assert result.throughput > 0 |
| 104 | + |
| 105 | + print("\n" + "=" * 60) |
| 106 | + print("BENCHMARK METRICS") |
| 107 | + print("=" * 60) |
| 108 | + print(f"BENCHMARK_THROUGHPUT: {result.throughput:.4f} req/s") |
| 109 | + print(f"BENCHMARK_LATENCY: {result.elapsed_s:.4f} s") |
| 110 | + print(f"BENCHMARK_SAMPLES: {result.samples}") |
| 111 | + print("=" * 60) |
| 112 | + |
| 113 | + # Optional thresholds to fail on regressions |
| 114 | + min_throughput = _get_float_env("RAY_DATA_LLM_BENCHMARK_MIN_THROUGHPUT", 5) |
| 115 | + max_latency_s = _get_float_env("RAY_DATA_LLM_BENCHMARK_MAX_LATENCY_S", 120) |
| 116 | + if min_throughput is not None: |
| 117 | + assert ( |
| 118 | + result.throughput >= min_throughput |
| 119 | + ), f"Throughput regression: {result.throughput:.4f} < {min_throughput:.4f} req/s" |
| 120 | + if max_latency_s is not None: |
| 121 | + assert ( |
| 122 | + result.elapsed_s <= max_latency_s |
| 123 | + ), f"Latency regression: {result.elapsed_s:.4f} > {max_latency_s:.4f} s" |
| 124 | + |
| 125 | + # Optional JSON artifact emission for downstream ingestion |
| 126 | + artifact_path = os.getenv("RAY_LLM_BENCHMARK_ARTIFACT_PATH") |
| 127 | + if artifact_path: |
| 128 | + metrics = { |
| 129 | + "model": MODEL_ID, |
| 130 | + "batch_size": BATCH_SIZE, |
| 131 | + "concurrency": CONCURRENCY, |
| 132 | + "samples": int(result.samples), |
| 133 | + "throughput_req_per_s": float(result.throughput), |
| 134 | + "elapsed_s": float(result.elapsed_s), |
| 135 | + } |
| 136 | + try: |
| 137 | + os.makedirs(os.path.dirname(artifact_path), exist_ok=True) |
| 138 | + with open(artifact_path, "w", encoding="utf-8") as f: |
| 139 | + json.dump(metrics, f, indent=2, sort_keys=True) |
| 140 | + print(f"Wrote benchmark artifact to: {artifact_path}") |
| 141 | + except Exception as e: # noqa: BLE001 |
| 142 | + print( |
| 143 | + f"Warning: failed to write benchmark artifact to {artifact_path}: {e}" |
| 144 | + ) |
| 145 | + |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + sys.exit(pytest.main(["-v", "-s", __file__])) |
0 commit comments