Skip to content

Commit a84353c

Browse files
Margarita Grinvaldfacebook-github-bot
authored andcommitted
Extract a function that selectively displays or prints DF (#6031)
Summary: The extracted function can be reused to selectively print or display other profiling events. Reviewed By: Olivia-liu Differential Revision: D63468910
1 parent cb3a546 commit a84353c

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

devtools/inspector/_inspector.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from executorch.devtools.inspector._inspector_utils import (
4444
calculate_time_scale_factor,
4545
create_debug_handle_to_op_node_mapping,
46+
display_or_print_df,
4647
EDGE_DIALECT_GRAPH_KEY,
4748
EXCLUDED_COLUMNS_WHEN_PRINTING,
4849
EXCLUDED_EVENTS_WHEN_PRINTING,
@@ -60,8 +61,6 @@
6061
)
6162
from executorch.exir import ExportedProgram
6263

63-
from tabulate import tabulate
64-
6564

6665
log: logging.Logger = logging.getLogger(__name__)
6766

@@ -1172,25 +1171,7 @@ def print_data_tabular(
11721171
]
11731172
filtered_column_df.reset_index(drop=True, inplace=True)
11741173

1175-
try:
1176-
from IPython import get_ipython
1177-
from IPython.display import display
1178-
1179-
def style_text_size(val, size=12):
1180-
return f"font-size: {size}px"
1181-
1182-
if get_ipython() is not None:
1183-
styled_df = filtered_column_df.style.applymap(style_text_size)
1184-
display(styled_df)
1185-
else:
1186-
raise Exception(
1187-
"Environment unable to support IPython. Fall back to print()."
1188-
)
1189-
except:
1190-
print(
1191-
tabulate(filtered_column_df, headers="keys", tablefmt="fancy_grid"),
1192-
file=file,
1193-
)
1174+
display_or_print_df(filtered_column_df, file)
11941175

11951176
# TODO: write unit test
11961177
def find_total_for_module(self, module_name: str) -> float:

devtools/inspector/_inspector_utils.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
# pyre-unsafe
88

99
import math
10+
import sys
1011
from enum import Enum
11-
from typing import Dict, List, Mapping, Optional, Tuple, TypeAlias, Union
12+
from typing import Dict, IO, List, Mapping, Optional, Tuple, TypeAlias, Union
1213

1314
import executorch.devtools.etdump.schema_flatcc as flatcc
1415

16+
import pandas as pd
17+
1518
import torch
1619

1720
from executorch.devtools.debug_format.base_schema import OperatorNode
@@ -30,6 +33,8 @@
3033
from executorch.devtools.etdump.serialize import deserialize_from_etdump_flatcc
3134
from executorch.devtools.etrecord import ETRecord
3235

36+
from tabulate import tabulate
37+
3338
FORWARD = "forward"
3439
EDGE_DIALECT_GRAPH_KEY = "edge_dialect_graph_module"
3540

@@ -295,6 +300,28 @@ def gen_etdump_object(
295300
return deserialize_from_etdump_flatcc(etdump_data)
296301

297302

303+
def display_or_print_df(df: pd.DataFrame, file: IO[str] = sys.stdout):
304+
try:
305+
from IPython import get_ipython
306+
from IPython.display import display
307+
308+
def style_text_size(val, size=12):
309+
return f"font-size: {size}px"
310+
311+
if get_ipython() is not None:
312+
styled_df = df.style.applymap(style_text_size)
313+
display(styled_df)
314+
else:
315+
raise Exception(
316+
"Environment unable to support IPython. Fall back to print()."
317+
)
318+
except:
319+
print(
320+
tabulate(df, headers="keys", tablefmt="fancy_grid"),
321+
file=file,
322+
)
323+
324+
298325
def plot_metric(result: List[float], metric_name: str):
299326
import matplotlib.pyplot as plt
300327
import numpy as np

0 commit comments

Comments
 (0)