Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc: update output token logic #695

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions python/sglang/bench_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class RequestFuncOutput:
itl: List[float] = field(default_factory=list) # List of inter-token latencies
prompt_len: int = 0
error: str = ""
output_len: int = 0


def remove_prefix(text: str, prefix: str) -> str:
Expand Down Expand Up @@ -189,6 +190,7 @@ async def async_request_openai_completions(
output.generated_text = generated_text
output.success = True
output.latency = latency
output.output_len = request_func_input.output_len
else:
output.error = response.reason or ""
output.success = False
Expand Down Expand Up @@ -451,6 +453,7 @@ def calculate_metrics(
outputs: List[RequestFuncOutput],
dur_s: float,
tokenizer: PreTrainedTokenizerBase,
backend: str,
) -> Tuple[BenchmarkMetrics, List[int]]:
actual_output_lens: List[int] = []
total_input = 0
Expand All @@ -460,13 +463,16 @@ def calculate_metrics(
ttfts: List[float] = []
for i in range(len(outputs)):
if outputs[i].success:
# We use the tokenizer to count the number of output tokens for all
# serving backends instead of looking at len(outputs[i].itl) since
# multiple output tokens may be bundled together
# Note : this may inflate the output token count slightly
output_len = len(
tokenizer(outputs[i].generated_text, add_special_tokens=False).input_ids
)
# We use the tokenizer solely to count output tokens for the TensorRT LLM backend,
# as it lacks `ignore_eos` support.
if backend == "trt":
output_len = len(
tokenizer(
outputs[i].generated_text, add_special_tokens=False
).input_ids
)
else:
output_len = outputs[i].output_len
actual_output_lens.append(output_len)
total_input += input_requests[i][1]
if output_len > 1:
Expand Down Expand Up @@ -571,9 +577,11 @@ async def benchmark(
outputs=outputs,
dur_s=benchmark_duration,
tokenizer=tokenizer,
backend=backend,
)

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("Successful requests:", metrics.completed))
print("{:<40} {:<10.2f}".format("Benchmark duration (s):", benchmark_duration))
Expand Down
Loading