Skip to content

Commit

Permalink
Add concurrency option for benchmark (#2136)
Browse files Browse the repository at this point in the history
  • Loading branch information
cermeng authored Nov 23, 2024
1 parent a78d8f8 commit 60769be
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion python/sglang/bench_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ async def benchmark(
tokenizer: PreTrainedTokenizerBase,
input_requests: List[Tuple[str, int, int]],
request_rate: float,
max_concurrency: Optional[int],
disable_tqdm: bool,
extra_request_body: Dict[str, Any],
profile: bool,
Expand All @@ -868,6 +869,15 @@ async def benchmark(
else:
raise ValueError(f"Unknown backend: {backend}")

# From https://github.com/vllm-project/vllm/pull/9390
semaphore = asyncio.Semaphore(max_concurrency) if max_concurrency else None

async def limited_request_func(request_func_input, pbar):
if semaphore is None:
return await request_func(request_func_input=request_func_input, pbar=pbar)
async with semaphore:
return await request_func(request_func_input=request_func_input, pbar=pbar)

print("Starting initial single prompt test run...")
test_prompt, test_prompt_len, test_output_len = input_requests[0]
test_input = RequestFuncInput(
Expand Down Expand Up @@ -913,7 +923,7 @@ async def benchmark(
)
tasks.append(
asyncio.create_task(
request_func(request_func_input=request_func_input, pbar=pbar)
limited_request_func(request_func_input=request_func_input, pbar=pbar)
)
)
outputs: List[RequestFuncOutput] = await asyncio.gather(*tasks)
Expand All @@ -940,6 +950,12 @@ async def benchmark(
print("\n{s:{c}^{n}}".format(s=" Serving Benchmark Result ", n=50, c="="))
print("{:<40} {:<10}".format("Backend:", backend))
print("{:<40} {:<10}".format("Traffic request rate:", request_rate))
print(
"{:<40} {:<10}".format(
"Max reqeuest concurrency:",
max_concurrency if max_concurrency else "not set",
)
)
print("{:<40} {:<10}".format("Successful requests:", metrics.completed))
print("{:<40} {:<10.2f}".format("Benchmark duration (s):", benchmark_duration))
print("{:<40} {:<10}".format("Total input tokens:", metrics.total_input))
Expand Down Expand Up @@ -1003,6 +1019,7 @@ async def benchmark(
"backend": args.backend,
"dataset_name": args.dataset_name,
"request_rate": request_rate,
"max_concurrency": max_concurrency,
"total_input_tokens": metrics.total_input,
"total_output_tokens": metrics.total_output,
"total_output_tokens_retokenized": metrics.total_output_retokenized,
Expand Down Expand Up @@ -1201,6 +1218,7 @@ def run_benchmark(args_: argparse.Namespace):
tokenizer=tokenizer,
input_requests=input_requests,
request_rate=args.request_rate,
max_concurrency=args.max_concurrency,
disable_tqdm=args.disable_tqdm,
extra_request_body=extra_request_body,
profile=args.profile,
Expand All @@ -1220,6 +1238,7 @@ def run_benchmark(args_: argparse.Namespace):
tokenizer=tokenizer,
input_requests=input_requests,
request_rate=rate,
max_concurrency=args.max_concurrency,
disable_tqdm=args.disable_tqdm,
extra_request_body=extra_request_body,
profile=args.profile,
Expand Down Expand Up @@ -1319,6 +1338,19 @@ def set_ulimit(target_soft_limit=65535):
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. Default is inf.",
)
parser.add_argument(
"--max-concurrency",
type=int,
default=None,
help="Maximum number of concurrent requests. This can be used "
"to help simulate an environment where a higher level component "
"is enforcing a maximum number of concurrent requests. While the "
"--request-rate argument controls the rate at which requests are "
"initiated, this argument will control how many are actually allowed "
"to execute at a time. This means that when used in combination, the "
"actual request rate may be lower than specified with --request-rate, "
"if the server is not processing requests fast enough to keep up.",
)
parser.add_argument("--seed", type=int, default=1, help="The random seed.")
parser.add_argument(
"--multi",
Expand Down

0 comments on commit 60769be

Please sign in to comment.