|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +# Implements profiling for vLLM on TPU VMs using the JAX profiler. |
| 4 | +# NOTE: you will need the tensorboard-plugin-profile python package to |
| 5 | +# visualize the results in TensorBoard. |
| 6 | +# Please see docs/profiler.md for more details. |
| 7 | +# Usage example for prefilling 1 request of 1024 tokens: |
| 8 | +# python3 examples/tpu_profiling.py --input-len 1024 --output-len 1 --batch-size 1 |
| 9 | +# Usage example for decoding 256 requests of 1 token each: |
| 10 | +# python3 examples/tpu_profiling.py --input-len 1 --output-len 1 --batch-size=256 |
| 11 | + |
| 12 | +import argparse |
| 13 | +import dataclasses |
| 14 | +import os |
| 15 | +import time |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +from tqdm import tqdm |
| 19 | +from vllm import LLM, SamplingParams |
| 20 | +from vllm.engine.arg_utils import EngineArgs |
| 21 | +from vllm.inputs import PromptType |
| 22 | +from vllm.utils import FlexibleArgumentParser |
| 23 | + |
| 24 | +DURATION_MS = int(os.getenv("VLLM_TPU_PROFILE_DURATION_MS", 3000)) |
| 25 | +DELAY_MS = int(os.getenv("VLLM_TPU_PROFILE_DELAY_MS", 0)) |
| 26 | + |
| 27 | + |
| 28 | +def main(args: argparse.Namespace): |
| 29 | + print(args) |
| 30 | + |
| 31 | + # Profile |
| 32 | + profile_dir = args.profile_result_dir |
| 33 | + print(f"Profiling (results will be saved to '{profile_dir}')...") |
| 34 | + os.environ["VLLM_TORCH_PROFILER_DIR"] = profile_dir |
| 35 | + |
| 36 | + engine_args = EngineArgs.from_cli_args(args) |
| 37 | + llm = LLM(**dataclasses.asdict(engine_args)) |
| 38 | + |
| 39 | + sampling_params = SamplingParams( |
| 40 | + temperature=0.0, |
| 41 | + ignore_eos=True, |
| 42 | + max_tokens=args.output_len, |
| 43 | + ) |
| 44 | + print(sampling_params) |
| 45 | + dummy_prompt_token_ids = np.random.randint(10000, |
| 46 | + size=(args.batch_size, |
| 47 | + args.input_len)) |
| 48 | + dummy_prompts: list[PromptType] = [{ |
| 49 | + "prompt_token_ids": batch |
| 50 | + } for batch in dummy_prompt_token_ids.tolist()] |
| 51 | + |
| 52 | + def run_to_completion(): |
| 53 | + start_time = time.perf_counter() |
| 54 | + llm.generate(dummy_prompts, |
| 55 | + sampling_params=sampling_params, |
| 56 | + use_tqdm=False) |
| 57 | + end_time = time.perf_counter() |
| 58 | + latency = end_time - start_time |
| 59 | + return latency |
| 60 | + |
| 61 | + # Warmup |
| 62 | + print("Warming up...") |
| 63 | + warmup_latencies = [] |
| 64 | + for _ in tqdm(range(args.num_iters_warmup), desc="Warmup iterations"): |
| 65 | + warmup_latencies.append(run_to_completion()) |
| 66 | + print(f"Average warmup latency: {np.mean(warmup_latencies):.4f}s") |
| 67 | + |
| 68 | + # Enable tracing on server |
| 69 | + llm.start_profile() |
| 70 | + if DELAY_MS == 0: |
| 71 | + time.sleep(1.0) |
| 72 | + profile_latencies = [] |
| 73 | + for _ in tqdm(range(args.num_iters), desc="Profile iterations"): |
| 74 | + profile_latencies.append(run_to_completion()) |
| 75 | + llm.stop_profile() |
| 76 | + print(f"Average profile latency: {np.mean(profile_latencies):.4f}s") |
| 77 | + |
| 78 | + return |
| 79 | + |
| 80 | + |
| 81 | +def parse_args(): |
| 82 | + parser = FlexibleArgumentParser( |
| 83 | + description="Benchmark the latency of processing a single batch of " |
| 84 | + "requests till completion.") |
| 85 | + parser.add_argument("--input-len", type=int, default=32) |
| 86 | + parser.add_argument("--output-len", type=int, default=128) |
| 87 | + parser.add_argument("--batch-size", type=int, default=8) |
| 88 | + parser.add_argument( |
| 89 | + "--num-iters-warmup", |
| 90 | + type=int, |
| 91 | + default=5, |
| 92 | + help="Number of iterations to run for warmup.", |
| 93 | + ) |
| 94 | + parser.add_argument( |
| 95 | + "--num-iters", |
| 96 | + type=int, |
| 97 | + default=1, |
| 98 | + help="Number of iterations to run for profiling.", |
| 99 | + ) |
| 100 | + parser.add_argument( |
| 101 | + "--profile-result-dir", |
| 102 | + type=str, |
| 103 | + default="profiles", |
| 104 | + help=("path to save the JAX profiler output. Can be visualized " |
| 105 | + "with ui.perfetto.dev, Tensorboard, or XProf"), |
| 106 | + ) |
| 107 | + |
| 108 | + parser = EngineArgs.add_cli_args(parser) |
| 109 | + return parser.parse_args() |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + args = parse_args() |
| 114 | + main(args) |
0 commit comments