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

Add formatting for GenAi-PA report #491

Merged
merged 7 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
66 changes: 66 additions & 0 deletions src/c++/perf_analyzer/genai-pa/genai_pa/llm_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

import numpy as np
from genai_pa.utils import load_json
from rich.console import Console
from rich.table import Table

# Silence tokenizer warning on import
with contextlib.redirect_stdout(io.StringIO()) as stdout, contextlib.redirect_stderr(
Expand Down Expand Up @@ -121,6 +123,69 @@ def __repr__(self):
attr_strs = ",".join([f"{k}={v}" for k, v in self.__dict__.items()])
return f"Statistics({attr_strs})"

def _is_time_field(self, field: str):
time_fields = [
"inter_token_latency",
"time_to_first_token",
"end_to_end_latency",
]
return field in time_fields

def _use_exact_length(self, value: str):
exact_value_length = 17
formatted_value = f"{value:.{exact_value_length}f}"
if len(formatted_value) < exact_value_length:
return formatted_value.ljust(exact_value_length)
return formatted_value[:exact_value_length]

def pretty_print(self):
table = Table(title="PA LLM Metrics")

table.add_column("Statistic", justify="right", style="cyan", no_wrap=True)
stats = ["avg", "min", "max", "p99", "p95", "p90", "p75", "p50", "p25"]
rmccorm4 marked this conversation as resolved.
Show resolved Hide resolved
for stat in stats:
table.add_column(stat, justify="right", style="green")

metrics = ["inter_token_latency", "time_to_first_token"]
for metric in metrics:
formatted_metric = metric.replace("_", " ").capitalize()
is_time_field = self._is_time_field(metric)
if is_time_field:
formatted_metric += " (ns)"
row_values = [formatted_metric]

for stat in stats:
value = self.__dict__.get(f"{stat}_{metric}", -1)
row_values.append("{:,.0f}".format(value))
table.add_row(*row_values)

console = Console()
console.print(table)

field_stats = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is field_stats used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this! I missed some of the old implementation code. It should be cleaner now. :)


for key, value in self.__dict__.items():
if key.startswith(
(
"p25_",
"p50_",
"p75_",
"p90_",
"p95_",
"p99_",
"avg_",
"min_",
"max_",
"std_",
)
):
stat, field = key.split("_", 1)
stat = stat.replace("_", " ")

if field not in field_stats:
field_stats[field] = {}
field_stats[field][stat] = value


class LLMProfileData:
"""A class that calculates and aggregates all the LLM performance statistics
Expand All @@ -141,6 +206,7 @@ class LLMProfileData:
>>> stats = pd.get_statistics(infer_mode="concurrency", level=10)
>>>
>>> print(stats) # output: Statistics(avg_time_to_first_token=...)
>>> stats.pretty_print() # Output: time_to_first_token_s: ...
"""

def __init__(self, filename: str, tokenizer: AutoTokenizer) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/c++/perf_analyzer/genai-pa/genai_pa/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def report_output(metrics: LLMProfileData, args):
raise GenAiPAException(
"Neither concurrency_range nor request_rate_range was found in args when reporting metrics"
)
# TODO: metrics reporter class that consumes Stats class for nicer formatting
print(metrics.get_statistics(infer_mode, int(load_level)))
stats = metrics.get_statistics(infer_mode, int(load_level))
stats.pretty_print()


# Separate function that can raise exceptions used for testing
Expand Down
Loading