File size: 11,222 Bytes
c5e73ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Taken and modified from vllm: https://github.com/vllm-project/vllm/blob/93b38bea5dd03e1b140ca997dfaadef86f8f1855/benchmarks/benchmark_serving.py
"""

import argparse
import asyncio
import json
import random
import time
import torch
from typing import AsyncGenerator, List, Tuple

import aiohttp
import numpy as np
from dataclasses import asdict, dataclass, field
from tqdm.asyncio import tqdm
from zeus.monitor import ZeusMonitor


SYSTEM_PROMPT = "A chat between a human user (prompter) and an artificial intelligence (AI) assistant. The assistant gives helpful, detailed, and polite answers to the user's questions. "


@dataclass
class Results:
    model: str
    backend: str
    request_rate: float
    num_failures: int = 0
    system_prompt: str = SYSTEM_PROMPT
    total_time: float = 0.0
    throughput: float = 0.0
    total_prompt_tokens: int = 0.0
    total_completion_tokens: int = 0.0
    avg_latency: float = 0.0
    avg_latency_per_token: float = 0.0
    avg_latency_per_output_token: float = 0.0
    server_total_energy: float = 0.0
    server_energy_per_request: float = 0.0
    server_energy_per_output_token: float = 0.0
    local_zeus_total_energy: float = 0.0
    local_zeus_energy_per_request: float = 0.0
    local_zeus_energy_per_output_token: float = 0.0
    results: list["Result"] = field(default_factory=list)


@dataclass
class Result:
    success: bool = True
    latency: float = 0.0
    prompt: str = ""
    response: str = ""
    num_prompt_tokens: int = 0
    num_completion_tokens: int = 0
    energy: float = 0.0


def get_requests(
    dataset_path: str,
) -> List[str]:
    # Load the dataset.
    with open(dataset_path) as f:
        dataset = json.load(f)
    # Only keep the first turn of each conversation.
    dataset = [data["conversations"][0]["value"] for data in dataset]

    return dataset


async def get_request(
    input_requests: List[str],
    request_rate: float,
) -> AsyncGenerator[Tuple[str, int, int], None]:
    input_requests = iter(input_requests)
    for i, request in enumerate(input_requests):
        yield i, request

        if request_rate == float("inf"):
            # If the request rate is infinity, then we don't need to wait.
            continue
        # Sample the request interval from the exponential distribution.
        interval = np.random.exponential(1.0 / request_rate)
        # The next request will be sent after the interval.
        await asyncio.sleep(interval)


async def send_request(
    result: Result,
    backend: str,
    model: str,
    api_url: str,
    prompt: str,
    pbar: tqdm,
) -> None:
    request_start_time = time.perf_counter()

    headers = {"Content-Type": "application/json"}
    # OpenAI Chat Completions API request format
    pload = {
        "model": model,
        "messages": [
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": prompt},
        ],
        "stream": False,
        "max_tokens": 1000,
    }

    timeout = aiohttp.ClientTimeout(total=3 * 3600)
    async with aiohttp.ClientSession(timeout=timeout) as session:
        async with session.post(api_url, headers=headers, json=pload) as response:
            # Request failed
            if response.status // 100 != 2:
                print('request failed')
                print(f"response.status {response.status}")
                result.prompt = prompt
                result.success = False
                return
            chunks = []
            async for chunk, _ in response.content.iter_chunks():
                chunks.append(chunk)
        request_end_time = time.perf_counter()
        output = b"".join(chunks).decode("utf-8")
        output = json.loads(output)

    result.latency = request_end_time - request_start_time
    result.prompt = prompt
    result.response = output["choices"][0]["message"]["content"]
    result.num_prompt_tokens = output["usage"]["prompt_tokens"]
    result.num_completion_tokens = output["usage"]["completion_tokens"]
    result.energy = output["usage"]["energy"]

    pbar.update(1)


async def benchmark(
    results: Results,
    backend: str,
    model: str,
    api_url: str,
    input_requests: List[str],
    request_rate: float,
) -> None:
    tasks: List[asyncio.Task] = []
    pbar = tqdm(total=len(input_requests))
    async for i, request in get_request(input_requests, request_rate):
        prompt = request
        task = asyncio.create_task(
            # Ensures results has same ordering as the input dataset
            send_request(
                results.results[i],
                backend,
                model,
                api_url,
                prompt,
                pbar,
            )
        )
        tasks.append(task)
    await asyncio.gather(*tasks)
    pbar.close()


def run_benchmark(
    args: argparse.Namespace, api_url: str, input_requests: List[str], out_filename: str
):
    results = Results(
        model=args.model,
        backend=args.backend,
        request_rate=args.request_rate,
        results=[Result() for _ in input_requests],
    )

    zeus_monitor = ZeusMonitor()
    zeus_monitor.begin_window(out_filename)
    benchmark_start_time = time.perf_counter()
    asyncio.run(
        benchmark(
            results,
            args.backend,
            args.model,
            api_url,
            input_requests,
            args.request_rate,
        )
    )
    benchmark_end_time = time.perf_counter()
    measurements = zeus_monitor.end_window(out_filename)
    zeus_total_energy = measurements.total_energy

    # Store aggregated results
    total_prompt_tokens = 0
    total_completion_tokens = 0
    total_latency = 0
    total_latency_per_token = 0
    total_latency_per_output_token = 0
    server_total_energy = 0
    for result in results.results:
        if not result.success:
            results.num_failures += 1
            continue
        total_prompt_tokens += result.num_prompt_tokens
        total_completion_tokens += result.num_completion_tokens
        total_latency += result.latency
        total_latency_per_token += result.latency / (
            result.num_prompt_tokens + result.num_completion_tokens
        )
        total_latency_per_output_token += result.latency / result.num_completion_tokens
        server_total_energy += result.energy

    num_results = len(results.results) - results.num_failures
    if num_results == 0:
        print(f"{out_filename} not generated. All requests in this run failed.")
        return

    results.total_time = benchmark_end_time - benchmark_start_time
    results.throughput = num_results / results.total_time
    results.total_prompt_tokens = total_prompt_tokens
    results.total_completion_tokens = total_completion_tokens
    results.avg_latency = total_latency / num_results
    results.avg_latency_per_token = total_latency_per_token / num_results
    results.avg_latency_per_output_token = total_latency_per_output_token / num_results
    results.server_total_energy = server_total_energy
    results.server_energy_per_request = results.server_total_energy / num_results
    results.server_energy_per_output_token = (
        results.server_total_energy / results.total_completion_tokens
    )
    results.local_zeus_total_energy = zeus_total_energy
    results.local_zeus_energy_per_request = zeus_total_energy / num_results
    results.local_zeus_energy_per_output_token = (
        zeus_total_energy / results.total_completion_tokens
    )

    with open(out_filename, "w") as f:
        f.write(json.dumps(asdict(results), indent=2))

    if args.verbose:
        print("Benchmark results:")
        print(f"Model: {results.model}")
        print(f"Backend: {results.backend}")
        print(f"Request rate: {results.request_rate} requests/s")
        print()
        print(f"Total time: {results.total_time:.2f} s")
        print(f"Throughput: {results.throughput:.2f} requests/s")
        print(f"Average latency: {results.avg_latency:.2f} s")
        print(f"Average latency per token: {results.avg_latency_per_token:.2f} s")
        print(f"Average latency per output token: {results.avg_latency_per_output_token:.2f} s")
        print(f"(Zeus) Total energy: {results.local_zeus_total_energy:.2f} J")
        print(f"(Zeus) Energy per request: {results.local_zeus_energy_per_request:.2f} J")
        print(f"(Zeus) Energy per token: {results.local_zeus_energy_per_output_token:.2f} J")
        print(f"(Server) Total energy: {results.server_total_energy:.2f} J")
        print(f"(Server) Energy per request: {results.server_energy_per_request:.2f} J")
        print(f"(Server) Energy per token: {results.server_energy_per_output_token:.2f} J")

    print("Benchmark results written to", out_filename)


def main(args: argparse.Namespace):
    if args.backend not in ["tgi", "vllm"]:
        raise ValueError(f"Unknown backend: {args.backend}")

    arg_out_filename = f"{args.out_name}-args.json"
    with open(arg_out_filename, "w") as f:
        f.write(json.dumps(vars(args), indent=2))
    if args.verbose:
        print(args)
    print("Benchmark args written to", arg_out_filename)

    random.seed(args.seed)
    np.random.seed(args.seed)

    out_name = args.out_name
    api_url = f"{args.protocol}://{args.host}:{args.port}{args.endpoint}"
    input_requests = get_requests(args.dataset)

    # Note: output filenames are 1-indexed
    for i in range(1, args.num_runs + 1):
        run_benchmark(args, api_url, input_requests, out_name + f"-run{i}.json")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Benchmark the online serving throughput."
    )
    parser.add_argument("--backend", type=str, default="vllm", choices=["vllm", "tgi"])
    parser.add_argument(
        "--protocol", type=str, default="http", choices=["http", "https"]
    )
    parser.add_argument("--host", type=str, default="localhost")
    parser.add_argument("--port", type=int, default=8000)
    parser.add_argument("--endpoint", type=str, default="/v1/chat/completions")
    parser.add_argument("--model", type=str, default=None)
    parser.add_argument(
        "--dataset", type=str, required=True, help="Path to the dataset."
    )
    parser.add_argument(
        "--num-runs",
        type=int,
        default=3,
        help="Runs the benchmark num-runs times, writing results to 3 separate files.",
    )
    parser.add_argument(
        "--request-rate",
        type=float,
        default=float("inf"),
        help="Number of requests per second. If this is inf, "
        "then all the requests are sent at time 0. "
        "Otherwise, we use Poisson process to synthesize "
        "the request arrival times.",
    )
    parser.add_argument(
        "--out-name",
        type=str,
        default="benchmark_result",
        help="Name of file to write benchmark results. Note: '-run{i}.json' will be appended for actual outputted files.",
    )
    parser.add_argument(
        "--verbose",
        type=bool,
        default=True,
        help="Set to true to print out benchmark results. Otherwise, only write to file.",
    )
    parser.add_argument("--seed", type=int, default=0)
    args = parser.parse_args()
    main(args)