benchmark_latency.py 5.28 KB
Newer Older
1
"""Benchmark the latency of processing a single batch of requests."""
2
import argparse
3
import dataclasses
4
import json
5
import time
6
from pathlib import Path
7
from typing import List, Optional
8
9
10

import numpy as np
import torch
11
from tqdm import tqdm
12

Woosuk Kwon's avatar
Woosuk Kwon committed
13
from vllm import LLM, SamplingParams
14
from vllm.engine.arg_utils import EngineArgs
15
from vllm.inputs import PromptType
16
from vllm.sampling_params import BeamSearchParams
17
from vllm.utils import FlexibleArgumentParser
18
19
20


def main(args: argparse.Namespace):
21
22
    print(args)

23
24
    engine_args = EngineArgs.from_cli_args(args)

25
    # NOTE(woosuk): If the request cannot be processed in a single batch,
Zhuohan Li's avatar
Zhuohan Li committed
26
    # the engine will automatically process the request in multiple batches.
27
    llm = LLM(**dataclasses.asdict(engine_args))
28

Woosuk Kwon's avatar
Woosuk Kwon committed
29
30
    sampling_params = SamplingParams(
        n=args.n,
31
        temperature=1.0,
Woosuk Kwon's avatar
Woosuk Kwon committed
32
        top_p=1.0,
33
        ignore_eos=True,
Woosuk Kwon's avatar
Woosuk Kwon committed
34
35
        max_tokens=args.output_len,
    )
36
    print(sampling_params)
37
38
39
    dummy_prompt_token_ids = np.random.randint(10000,
                                               size=(args.batch_size,
                                                     args.input_len))
40
    dummy_prompts: List[PromptType] = [{
41
42
        "prompt_token_ids": batch
    } for batch in dummy_prompt_token_ids.tolist()]
43

44
45
46
47
48
49
50
51
52
53
54
55
56
57
    def llm_generate():
        if not args.use_beam_search:
            llm.generate(dummy_prompts,
                         sampling_params=sampling_params,
                         use_tqdm=False)
        else:
            llm.beam_search(
                dummy_prompts,
                BeamSearchParams(
                    beam_width=args.n,
                    max_tokens=args.output_len,
                    ignore_eos=True,
                ))

58
59
60
61
62
63
64
65
66
    def run_to_completion(profile_dir: Optional[str] = None):
        if profile_dir:
            with torch.profiler.profile(
                    activities=[
                        torch.profiler.ProfilerActivity.CPU,
                        torch.profiler.ProfilerActivity.CUDA,
                    ],
                    on_trace_ready=torch.profiler.tensorboard_trace_handler(
                        str(profile_dir))) as p:
67
                llm_generate()
68
            print(p.key_averages().table(sort_by="self_cuda_time_total"))
69
70
        else:
            start_time = time.perf_counter()
71
            llm_generate()
72
73
74
            end_time = time.perf_counter()
            latency = end_time - start_time
            return latency
75

76
    print("Warming up...")
77
78
    for _ in tqdm(range(args.num_iters_warmup), desc="Warmup iterations"):
        run_to_completion(profile_dir=None)
79

80
    if args.profile:
81
82
        profile_dir = args.profile_result_dir
        if not profile_dir:
83
84
85
            profile_dir = Path(
                "."
            ) / "vllm_benchmark_result" / f"latency_result_{time.time()}"
86
        print(f"Profiling (results will be saved to '{profile_dir}')...")
87
        run_to_completion(profile_dir=profile_dir)
88
89
        return

90
91
    # Benchmark.
    latencies = []
92
    for _ in tqdm(range(args.num_iters), desc="Profiling iterations"):
93
        latencies.append(run_to_completion(profile_dir=None))
94
    latencies = np.array(latencies)
95
    percentages = [10, 25, 50, 75, 90, 99]
96
    percentiles = np.percentile(latencies, percentages)
97
    print(f'Avg latency: {np.mean(latencies)} seconds')
98
99
    for percentage, percentile in zip(percentages, percentiles):
        print(f'{percentage}% percentile latency: {percentile} seconds')
100

101
102
103
104
105
106
107
108
109
110
    # Output JSON results if specified
    if args.output_json:
        results = {
            "avg_latency": np.mean(latencies),
            "latencies": latencies.tolist(),
            "percentiles": dict(zip(percentages, percentiles.tolist())),
        }
        with open(args.output_json, "w") as f:
            json.dump(results, f, indent=4)

111
112

if __name__ == '__main__':
113
    parser = FlexibleArgumentParser(
114
        description='Benchmark the latency of processing a single batch of '
115
        'requests till completion.')
116
117
118
    parser.add_argument('--input-len', type=int, default=32)
    parser.add_argument('--output-len', type=int, default=128)
    parser.add_argument('--batch-size', type=int, default=8)
119
120
121
    parser.add_argument('--n',
                        type=int,
                        default=1,
122
                        help='Number of generated sequences per prompt.')
123
    parser.add_argument('--use-beam-search', action='store_true')
124
125
126
127
    parser.add_argument('--num-iters-warmup',
                        type=int,
                        default=10,
                        help='Number of iterations to run for warmup.')
128
129
    parser.add_argument('--num-iters',
                        type=int,
130
                        default=30,
131
                        help='Number of iterations to run.')
132
133
134
135
    parser.add_argument(
        '--profile',
        action='store_true',
        help='profile the generation process of a single batch')
136
137
138
139
    parser.add_argument(
        '--profile-result-dir',
        type=str,
        default=None,
140
141
        help=('path to save the pytorch profiler output. Can be visualized '
              'with ui.perfetto.dev or Tensorboard.'))
142
143
144
145
146
    parser.add_argument(
        '--output-json',
        type=str,
        default=None,
        help='Path to save the latency results in JSON format.')
147
148

    parser = EngineArgs.add_cli_args(parser)
149
    args = parser.parse_args()
zhuwenwen's avatar
zhuwenwen committed
150
    main(args)