[rocprofiler-compute] Refactor to add type annotation and misc (#787)

This commit is contained in:
xuchen-amd
2025-09-12 13:53:24 -04:00
committed by GitHub
parent 37f8da676a
commit 7ed6000e32
62 changed files with 5145 additions and 4849 deletions
@@ -23,16 +23,19 @@
##############################################################################
from typing import Any, Union
import dash_bootstrap_components as dbc
import pandas as pd
from dash import dcc, html
from utils import schema
avail_normalizations = ["per_wave", "per_cycle", "per_second", "per_kernel"]
AVAIL_NORMALIZATIONS = ["per_wave", "per_cycle", "per_second", "per_kernel"]
# List all the unique column values for desired column in df, 'target_col'
def list_unique(orig_list, is_numeric):
def list_unique(orig_list: list[str], is_numeric: bool) -> list[str]:
list_set = set(orig_list)
unique_list = list(list_set)
if is_numeric:
@@ -40,18 +43,23 @@ def list_unique(orig_list, is_numeric):
return unique_list
def create_span(input):
return {"label": html.Span(str(input), title=str(input)), "value": str(input)}
def create_span(input_value: str) -> dict[str, Union[html.Span, str]]:
return {
"label": html.Span(str(input_value), title=str(input_value)),
"value": str(input_value),
}
def get_header(raw_pmc, input_filters, kernel_names):
kernel_names = list(
map(
str,
raw_pmc[schema.pmc_perf_file_prefix]["Kernel_Name"],
)
)
kernel_names = [x.strip() for x in kernel_names]
def get_header(
raw_pmc: pd.DataFrame, input_filters: dict[str, Any], kernel_names: list[str]
) -> html.Header:
pmc_data = raw_pmc[schema.PMC_PERF_FILE_PREFIX]
kernel_names = [str(name).strip() for name in pmc_data["Kernel_Name"]]
# Extract GPU and Dispatch IDs
gpu_ids = [str(gpu_id) for gpu_id in pmc_data["GPU_ID"]]
dispatch_ids = [str(dispatch_id) for dispatch_id in pmc_data["Dispatch_ID"]]
return html.Header(
id="home",
children=[
@@ -175,7 +183,7 @@ def get_header(raw_pmc, input_filters, kernel_names):
children=["Normalization:"],
),
dcc.Dropdown(
avail_normalizations,
AVAIL_NORMALIZATIONS,
id="norm-filt",
value=input_filters["normalization"],
clearable=False,
@@ -196,14 +204,7 @@ def get_header(raw_pmc, input_filters, kernel_names):
),
dcc.Dropdown(
list_unique(
list(
map(
str,
raw_pmc[
schema.pmc_perf_file_prefix
]["GPU_ID"],
)
),
gpu_ids,
True,
), # list avail gcd ids
id="gcd-filt",
@@ -229,14 +230,7 @@ def get_header(raw_pmc, input_filters, kernel_names):
children=["Dispatch Filter:"],
),
dcc.Dropdown(
list(
map(
str,
raw_pmc[
schema.pmc_perf_file_prefix
]["Dispatch_ID"],
)
),
dispatch_ids,
id="disp-filt",
multi=True,
# default to any dispatch
@@ -282,15 +276,12 @@ def get_header(raw_pmc, input_filters, kernel_names):
children=["Kernels:"],
),
dcc.Dropdown(
list(
map(
create_span,
list_unique(
orig_list=kernel_names,
is_numeric=False,
), # list avail kernel names
[
create_span(name)
for name in list_unique(
kernel_names, False
)
),
],
id="kernel-filt",
multi=True,
value=input_filters["kernel"],
@@ -22,29 +22,34 @@
# THE SOFTWARE.
##############################################################################
from typing import Any
from dash import html
from dash_svg import G, Path, Rect, Svg, Text
from utils import schema
from utils.logger import console_error
from utils.utils import format_scientific_notation_if_needed
# Constants for display formatting
DEFAULT_MAX_LENGTH = 6
DEFAULT_PRECISION = 1
DEFAULT_SCIENTIFIC_WIDTH = 8
def insert_chart_data(mem_data, base_data):
def insert_chart_data(mem_data: list[dict[str, Any]], base_data: schema.Workload) -> G:
if len(mem_data) != 1:
console_error("Memory Chart config doesn't follow expected formatting")
table_config = mem_data[0]["metric_table"]
original_df = base_data.dfs[table_config["id"]]
display_columns = original_df.columns.values.tolist().copy()
display_df = original_df[display_columns]
alias = display_df["Metric"].values
values = display_df["Value"].values
memchart_values = {}
memchart_values: dict[str, Any] = {}
for i in range(0, len(alias)):
memchart_values[alias[i]] = values[i]
@@ -521,7 +526,9 @@ def insert_chart_data(mem_data, base_data):
)
def get_memchart(mem_data, base_data):
def get_memchart(
mem_data: list[dict[str, Any]], base_data: schema.Workload
) -> html.Section:
return html.Section(
id="memchart",
children=[
@@ -2039,7 +2046,7 @@ def get_memchart(mem_data, base_data):
)
def format_value_for_display(value, max_length=6):
def format_value_for_display(value: Any, max_length: int = DEFAULT_MAX_LENGTH) -> str: # noqa: ANN401
"""
Format a value (int, float, or str) into a concise string suitable for display.
@@ -2097,8 +2104,8 @@ def format_value_for_display(value, max_length=6):
sci = format_scientific_notation_if_needed(
abs_val,
align=">",
width_align=8,
precision=1,
width_align=DEFAULT_SCIENTIFIC_WIDTH,
precision=DEFAULT_PRECISION,
fmt_type_align="e",
max_length=max_length,
).strip()
@@ -2110,7 +2117,7 @@ def format_value_for_display(value, max_length=6):
value = normal
if is_negative:
value = "-" + value
value = f"-{value}"
else:
value = str(value)
@@ -2123,11 +2130,11 @@ def format_value_for_display(value, max_length=6):
exponent = value[e_index:]
max_mantissa_len = max_length - len(exponent)
if max_mantissa_len < 1:
value = exponent[: max_length - 1] + ""
value = f"{exponent[: max_length - 1]}"
else:
truncated_mantissa = mantissa[:max_mantissa_len]
value = truncated_mantissa + exponent
else:
value = value[: max_length - 1] + ""
value = f"{value[: max_length - 1]}"
return value