Skip to content

Commit e3eaf1a

Browse files
committed
Add AR calculation to benchmark_serving
Signed-off-by: Zero Zeng <[email protected]>
1 parent 48ddc3d commit e3eaf1a

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

tensorrt_llm/serve/scripts/backend_request_func.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class RequestFuncOutput:
4444
tpot: float = 0.0 # avg next-token latencies
4545
prompt_len: int = 0
4646
error: str = ""
47+
decode_iteration: int = 0 # Number of decoding iterations
4748

4849

4950
async def async_request_trt_llm(
@@ -77,6 +78,7 @@ async def async_request_trt_llm(
7778
ttft = 0.0
7879
st = time.perf_counter()
7980
most_recent_timestamp = st
81+
decode_iteration_count = 0 # Track decoding iterations
8082
try:
8183
async with request_session.post(url=api_url, json=payload) as response:
8284
if response.status == 200:
@@ -102,16 +104,21 @@ async def async_request_trt_llm(
102104
else:
103105
output.itl.append(timestamp - most_recent_timestamp)
104106

107+
# Increment decode iteration for each chunk
108+
decode_iteration_count += 1
105109
most_recent_timestamp = timestamp
106110

107111
output.latency = most_recent_timestamp - st
112+
output.decode_iteration = decode_iteration_count
108113
else:
109114
content = await response.content.read()
110115
data = json.loads(content.decode())
111116
output.ttft = -1
112117
output.itl = []
113118
output.generated_text = data["text_output"]
114119
output.latency = time.perf_counter() - st
120+
# For non-streaming, estimate decode_iteration as number of output tokens
121+
output.decode_iteration = len(output.generated_text.split()) if output.generated_text else 1
115122

116123
else:
117124
output.error = response.reason or ""
@@ -170,6 +177,7 @@ async def async_request_openai_completions(
170177
generated_text = ""
171178
st = time.perf_counter()
172179
most_recent_timestamp = st
180+
decode_iteration_count = 0 # Track decoding iterations
173181
try:
174182
async with request_session.post(url=api_url,
175183
json=payload,
@@ -206,6 +214,9 @@ async def async_request_openai_completions(
206214
output.itl.append(timestamp -
207215
most_recent_timestamp)
208216

217+
# Increment decode iteration for each chunk with text
218+
if text is not None:
219+
decode_iteration_count += 1
209220
most_recent_timestamp = timestamp
210221
generated_text += text or ""
211222
elif usage := data.get("usage"):
@@ -220,6 +231,7 @@ async def async_request_openai_completions(
220231
"This response will be marked as failed!")
221232
output.generated_text = generated_text
222233
output.latency = most_recent_timestamp - st
234+
output.decode_iteration = decode_iteration_count
223235
else:
224236
content = await response.content.read()
225237
data = json.loads(content.decode())
@@ -230,6 +242,8 @@ async def async_request_openai_completions(
230242
output.ttft = -1
231243
output.itl = []
232244
output.output_tokens = data["usage"]["completion_tokens"]
245+
# For non-streaming, estimate decode_iteration as number of output tokens
246+
output.decode_iteration = output.output_tokens if output.output_tokens > 0 else 1
233247
else:
234248
output.error = response.reason or ""
235249
output.success = False
@@ -306,6 +320,7 @@ async def async_request_openai_chat_completions(
306320
ttft = 0.0
307321
st = time.perf_counter()
308322
most_recent_timestamp = st
323+
decode_iteration_count = 0 # Track decoding iterations
309324
try:
310325
async with request_session.post(url=api_url,
311326
json=payload,
@@ -336,6 +351,9 @@ async def async_request_openai_chat_completions(
336351
output.itl.append(timestamp -
337352
most_recent_timestamp)
338353

354+
# Increment decode iteration for each chunk with content
355+
if content is not None:
356+
decode_iteration_count += 1
339357
generated_text += content or ""
340358
elif usage := data.get("usage"):
341359
output.output_tokens = usage.get(
@@ -345,6 +363,7 @@ async def async_request_openai_chat_completions(
345363

346364
output.generated_text = generated_text
347365
output.latency = most_recent_timestamp - st
366+
output.decode_iteration = decode_iteration_count
348367
else:
349368
content = await response.content.read()
350369
data = json.loads(content.decode())
@@ -354,6 +373,8 @@ async def async_request_openai_chat_completions(
354373
output.itl = []
355374
output.latency = time.perf_counter() - st
356375
output.ttft = -1
376+
# For non-streaming, estimate decode_iteration as number of output tokens
377+
output.decode_iteration = output.output_tokens if output.output_tokens > 0 else 1
357378

358379
else:
359380
output.error = response.reason or ""

tensorrt_llm/serve/scripts/benchmark_serving.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ class BenchmarkMetrics:
7979
std_e2el_ms: float
8080
percentiles_e2el_ms: list[tuple[float, float]]
8181
tput_user: list[float]
82+
# Request accuracy rate metrics
83+
mean_request_ar: float
84+
median_request_ar: float
85+
std_request_ar: float
86+
percentiles_request_ar: list[tuple[float, float]]
8287

8388

8489
async def get_request(
@@ -131,7 +136,7 @@ def calculate_metrics(
131136
selected_percentile_metrics: list[str],
132137
selected_percentiles: list[float],
133138
goodput_config_dict: dict[str, float],
134-
) -> tuple[BenchmarkMetrics, list[int]]:
139+
) -> tuple[BenchmarkMetrics, list[int], list[float]]:
135140
actual_output_lens: list[int] = []
136141
total_input = 0
137142
completed = 0
@@ -142,6 +147,7 @@ def calculate_metrics(
142147
ttfts: list[float] = []
143148
e2els: list[float] = []
144149
tput_user: list[float] = []
150+
request_ars: list[float] = [] # Request accuracy rates
145151
for i in range(len(outputs)):
146152
if outputs[i].success:
147153
output_len = outputs[i].output_tokens
@@ -167,9 +173,22 @@ def calculate_metrics(
167173
ttfts.append(outputs[i].ttft)
168174
e2els.append(outputs[i].latency)
169175
tput_user.append(output_len / (outputs[i].latency))
176+
177+
# Calculate request accuracy rate (num_generated_tokens / (decode_iteration + 1))
178+
decode_iter = outputs[i].decode_iteration
179+
if decode_iter >= 0:
180+
# For generated tokens, we use output_len - 1 (excluding the first token if needed)
181+
# But according to the reference, it should be num_generated_tokens
182+
num_generated_tokens = max(0, output_len - 1) if output_len > 1 else output_len
183+
request_ar = num_generated_tokens / (decode_iter + 1) if decode_iter >= 0 else 0.0
184+
request_ars.append(request_ar)
185+
else:
186+
request_ars.append(0.0)
187+
170188
completed += 1
171189
else:
172190
actual_output_lens.append(0)
191+
request_ars.append(0.0)
173192

174193
if goodput_config_dict:
175194
valid_metrics = []
@@ -228,8 +247,13 @@ def calculate_metrics(
228247
percentiles_e2el_ms=[(p, np.percentile(e2els or 0, p) * 1000)
229248
for p in selected_percentiles],
230249
tput_user=np.mean(tput_user or 0),
250+
mean_request_ar=np.mean(request_ars or 0),
251+
median_request_ar=np.median(request_ars or 0),
252+
std_request_ar=np.std(request_ars or 0),
253+
percentiles_request_ar=[(p, np.percentile(request_ars or 0, p))
254+
for p in selected_percentiles],
231255
)
232-
return metrics, actual_output_lens
256+
return metrics, actual_output_lens, request_ars
233257

234258

235259
async def benchmark(
@@ -403,7 +427,7 @@ async def limited_request_func(request_func_input, streaming, pbar,
403427
# Close the session
404428
await session.close()
405429

406-
metrics, actual_output_lens = calculate_metrics(
430+
metrics, actual_output_lens, request_ars = calculate_metrics(
407431
input_requests=input_requests,
408432
outputs=outputs,
409433
dur_s=benchmark_duration,
@@ -431,6 +455,10 @@ async def limited_request_func(request_func_input, streaming, pbar,
431455
metrics.total_token_throughput))
432456
print("{:<40} {:<10.2f}".format("User throughput (tok/s):",
433457
metrics.tput_user))
458+
print("{:<40} {:<10.4f}".format("Mean Request AR:",
459+
metrics.mean_request_ar))
460+
print("{:<40} {:<10.4f}".format("Median Request AR:",
461+
metrics.median_request_ar))
434462

435463
result = {
436464
"duration": benchmark_duration,
@@ -443,12 +471,16 @@ async def limited_request_func(request_func_input, streaming, pbar,
443471
"output_throughput": metrics.output_throughput,
444472
"total_token_throughput": metrics.total_token_throughput,
445473
"user_throughput": metrics.tput_user,
474+
"mean_request_ar": metrics.mean_request_ar,
475+
"median_request_ar": metrics.median_request_ar,
446476
"input_lens": [output.prompt_len for output in outputs],
447477
"output_lens": actual_output_lens,
448478
"ttfts": [output.ttft for output in outputs],
449479
"itls": [output.itl for output in outputs],
450480
"generated_texts": [output.generated_text for output in outputs],
451481
"errors": [output.error for output in outputs],
482+
"request_ars": request_ars,
483+
"decode_iterations": [output.decode_iteration for output in outputs],
452484
}
453485

454486
def process_one_metric(
@@ -534,11 +566,12 @@ def save_to_pytorch_benchmark_format(args: argparse.Namespace,
534566
metrics = [
535567
"median_ttft_ms", "mean_ttft_ms", "std_ttft_ms", "p99_ttft_ms",
536568
"mean_tpot_ms", "median_tpot_ms", "std_tpot_ms", "p99_tpot_ms",
537-
"median_itl_ms", "mean_itl_ms", "std_itl_ms", "p99_itl_ms"
569+
"median_itl_ms", "mean_itl_ms", "std_itl_ms", "p99_itl_ms",
570+
"mean_request_ar", "median_request_ar", "std_request_ar"
538571
]
539572
# These raw data might be useful, but they are rather big. They can be added
540573
# later if needed
541-
ignored_metrics = ["ttfts", "itls", "generated_texts", "errors"]
574+
ignored_metrics = ["ttfts", "itls", "generated_texts", "errors", "request_ars", "decode_iterations"]
542575
pt_records = convert_to_pytorch_benchmark_format(
543576
args=args,
544577
metrics={k: [results[k]]
@@ -762,7 +795,7 @@ def main(args: argparse.Namespace):
762795
# Remove fields with too many data points
763796
for field in [
764797
"input_lens", "output_lens", "ttfts", "itls",
765-
"generated_texts", "errors"
798+
"generated_texts", "errors", "request_ars", "decode_iterations"
766799
]:
767800
if field in result_json:
768801
del result_json[field]
@@ -963,11 +996,11 @@ def main(args: argparse.Namespace):
963996
parser.add_argument(
964997
"--percentile-metrics",
965998
type=str,
966-
default="ttft,tpot,itl",
999+
default="ttft,tpot,itl,request_ar",
9671000
help="Comma-separated list of selected metrics to report percentils. "
9681001
"This argument specifies the metrics to report percentiles. "
969-
"Allowed metric names are \"ttft\", \"tpot\", \"itl\", \"e2el\". "
970-
"Default value is \"ttft,tpot,itl\".")
1002+
"Allowed metric names are \"ttft\", \"tpot\", \"itl\", \"e2el\", \"request_ar\". "
1003+
"Default value is \"ttft,tpot,itl,request_ar\".")
9711004
parser.add_argument(
9721005
"--metric-percentiles",
9731006
type=str,

0 commit comments

Comments
 (0)