|
| 1 | +from collections import defaultdict |
| 2 | +from math import ceil |
| 3 | +from pydantic import BaseModel |
| 4 | +import random |
| 5 | +from typing import List, Optional, Tuple |
| 6 | + |
| 7 | +from guidellm.benchmark.benchmark import GenerativeBenchmark |
| 8 | +from guidellm.objects.statistics import DistributionSummary |
| 9 | + |
| 10 | +__all__ = ["Bucket", "Model", "Dataset", "RunInfo", "TokenDistribution", "TokenDetails", "Server", "WorkloadDetails", "BenchmarkDatum"] |
| 11 | + |
| 12 | +class Bucket(BaseModel): |
| 13 | + value: float |
| 14 | + count: int |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def from_data( |
| 18 | + data: List[float], |
| 19 | + bucket_width: Optional[float] = None, |
| 20 | + n_buckets: Optional[int] = None |
| 21 | + ) -> Tuple[List["Bucket"], float]: |
| 22 | + if not data: |
| 23 | + return [], 1.0 |
| 24 | + |
| 25 | + min_v = min(data) |
| 26 | + max_v = max(data) |
| 27 | + range_v = max_v - min_v |
| 28 | + |
| 29 | + if bucket_width is None: |
| 30 | + if n_buckets is None: |
| 31 | + n_buckets = 10 |
| 32 | + bucket_width = range_v / n_buckets |
| 33 | + else: |
| 34 | + n_buckets = ceil(range_v / bucket_width) |
| 35 | + |
| 36 | + bucket_counts = defaultdict(int) |
| 37 | + for val in data: |
| 38 | + idx = int((val - min_v) // bucket_width) |
| 39 | + if idx >= n_buckets: |
| 40 | + idx = n_buckets - 1 |
| 41 | + bucket_start = min_v + idx * bucket_width |
| 42 | + bucket_counts[bucket_start] += 1 |
| 43 | + |
| 44 | + buckets = [Bucket(value=start, count=count) for start, count in sorted(bucket_counts.items())] |
| 45 | + return buckets, bucket_width |
| 46 | + |
| 47 | + |
| 48 | +class Model(BaseModel): |
| 49 | + name: str |
| 50 | + size: int |
| 51 | + |
| 52 | +class Dataset(BaseModel): |
| 53 | + name: str |
| 54 | + |
| 55 | +class RunInfo(BaseModel): |
| 56 | + model: Model |
| 57 | + task: str |
| 58 | + timestamp: float |
| 59 | + dataset: Dataset |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def from_benchmarks(cls, benchmarks: list[GenerativeBenchmark]): |
| 63 | + model = benchmarks[0].worker.backend_model or 'N/A' |
| 64 | + timestamp = max(bm.run_stats.start_time for bm in benchmarks if bm.start_time is not None) |
| 65 | + return cls( |
| 66 | + model=Model(name=model, size=0), |
| 67 | + task='N/A', |
| 68 | + timestamp=timestamp, |
| 69 | + dataset=Dataset(name="N/A") |
| 70 | + ) |
| 71 | + |
| 72 | +class TokenDistribution(BaseModel): |
| 73 | + statistics: Optional[DistributionSummary] = None |
| 74 | + buckets: list[Bucket] |
| 75 | + bucket_width: float |
| 76 | + |
| 77 | + |
| 78 | +class TokenDetails(BaseModel): |
| 79 | + samples: list[str] |
| 80 | + token_distributions: TokenDistribution |
| 81 | + |
| 82 | +class Server(BaseModel): |
| 83 | + target: str |
| 84 | + |
| 85 | +class RequestOverTime(BaseModel): |
| 86 | + num_benchmarks: int |
| 87 | + requests_over_time: TokenDistribution |
| 88 | + |
| 89 | +class WorkloadDetails(BaseModel): |
| 90 | + prompts: TokenDetails |
| 91 | + generations: TokenDetails |
| 92 | + requests_over_time: RequestOverTime |
| 93 | + rate_type: str |
| 94 | + server: Server |
| 95 | + @classmethod |
| 96 | + def from_benchmarks(cls, benchmarks: list[GenerativeBenchmark]): |
| 97 | + target = benchmarks[0].worker.backend_target |
| 98 | + rate_type = benchmarks[0].args.profile.type_ |
| 99 | + successful_requests = [req for bm in benchmarks for req in bm.requests.successful] |
| 100 | + sample_indices = random.sample(range(len(successful_requests)), min(5, len(successful_requests))) |
| 101 | + sample_prompts = [successful_requests[i].prompt.replace("\n", " ").replace("\"", "'") for i in sample_indices] |
| 102 | + sample_outputs = [successful_requests[i].output.replace("\n", " ").replace("\"", "'") for i in sample_indices] |
| 103 | + |
| 104 | + prompt_tokens = [req.prompt_tokens for bm in benchmarks for req in bm.requests.successful] |
| 105 | + output_tokens = [req.output_tokens for bm in benchmarks for req in bm.requests.successful] |
| 106 | + |
| 107 | + prompt_token_buckets, _prompt_token_bucket_width = Bucket.from_data(prompt_tokens, 1) |
| 108 | + output_token_buckets, _output_token_bucket_width = Bucket.from_data(output_tokens, 1) |
| 109 | + |
| 110 | + prompt_token_stats = DistributionSummary.from_values(prompt_tokens) |
| 111 | + output_token_stats = DistributionSummary.from_values(output_tokens) |
| 112 | + prompt_token_distributions = TokenDistribution(statistics=prompt_token_stats, buckets=prompt_token_buckets, bucket_width=1) |
| 113 | + output_token_distributions = TokenDistribution(statistics=output_token_stats, buckets=output_token_buckets, bucket_width=1) |
| 114 | + |
| 115 | + min_start_time = benchmarks[0].run_stats.start_time |
| 116 | + |
| 117 | + all_req_times = [ |
| 118 | + req.start_time - min_start_time |
| 119 | + for bm in benchmarks |
| 120 | + for req in bm.requests.successful |
| 121 | + if req.start_time is not None |
| 122 | + ] |
| 123 | + number_of_buckets = len(benchmarks) |
| 124 | + request_over_time_buckets, bucket_width = Bucket.from_data(all_req_times, None, number_of_buckets) |
| 125 | + request_over_time_distribution = TokenDistribution(buckets=request_over_time_buckets, bucket_width=bucket_width) |
| 126 | + return cls( |
| 127 | + prompts=TokenDetails(samples=sample_prompts, token_distributions=prompt_token_distributions), |
| 128 | + generations=TokenDetails(samples=sample_outputs, token_distributions=output_token_distributions), |
| 129 | + requests_over_time=RequestOverTime(requests_over_time=request_over_time_distribution, num_benchmarks=number_of_buckets), |
| 130 | + rate_type=rate_type, |
| 131 | + server=Server(target=target) |
| 132 | + ) |
| 133 | + |
| 134 | +class BenchmarkDatum(BaseModel): |
| 135 | + requests_per_second: float |
| 136 | + tpot: DistributionSummary |
| 137 | + ttft: DistributionSummary |
| 138 | + throughput: DistributionSummary |
| 139 | + time_per_request: DistributionSummary |
| 140 | + |
| 141 | + @classmethod |
| 142 | + def from_benchmark(cls, bm: GenerativeBenchmark): |
| 143 | + return cls( |
| 144 | + requests_per_second=bm.metrics.requests_per_second.successful.mean, |
| 145 | + tpot=bm.metrics.inter_token_latency_ms.successful, |
| 146 | + ttft=bm.metrics.time_to_first_token_ms.successful, |
| 147 | + throughput=bm.metrics.output_tokens_per_second.successful, |
| 148 | + time_per_request=bm.metrics.request_latency.successful, |
| 149 | + ) |
0 commit comments