2525import torch
2626from executorch .exir import ExportedProgram
2727
28- from executorch .sdk .edir .base_schema import OperatorGraph , OperatorNode
28+ from executorch .sdk .edir .et_schema import OperatorNode
2929from executorch .sdk .etdump .schema_flatcc import ETDumpFlatCC , ProfileEvent
30+ from executorch .sdk .etrecord import parse_etrecord
3031from executorch .sdk .inspector ._inspector_utils import (
3132 create_debug_handle_to_op_node_mapping ,
3233 EDGE_DIALECT_GRAPH_KEY ,
3334 gen_etdump_object ,
34- gen_etrecord_object ,
3535 gen_graphs_from_etrecord ,
3636)
3737
@@ -368,7 +368,6 @@ class Inspector:
368368
369369 Private Attributes:
370370 _etrecord: Optional[ETRecord]. File under etrecord_path deserialized into an object.
371- _op_graph_dict: Mapping[str, OperatorGraphWithStats]. Graph objects parsed from etrecord matched with user defined graph names.
372371 """
373372
374373 def __init__ (
@@ -387,14 +386,18 @@ def __init__(
387386 defaults to milli (1000ms = 1s).
388387 """
389388
390- # TODO: etrecord_path can be optional, so need to support the case when it is not present
391- self ._etrecord = gen_etrecord_object (etrecord_path = etrecord_path )
389+ self ._etrecord = (
390+ parse_etrecord (etrecord_path = etrecord_path )
391+ if etrecord_path is not None
392+ else None
393+ )
394+
392395 etdump = gen_etdump_object (etdump_path = etdump_path )
393396 self .event_blocks = EventBlock ._gen_from_etdump (etdump , etdump_scale )
394397
395- self . _op_graph_dict : Mapping [ str , OperatorGraph ] = gen_graphs_from_etrecord (
396- etrecord = self ._etrecord
397- )
398+ # No additional data association can be done without ETRecord, so return early
399+ if self ._etrecord is None :
400+ return
398401
399402 # Use the delegate map from etrecord, associate debug handles with each event
400403 for event_block in self .event_blocks :
@@ -406,9 +409,10 @@ def __init__(
406409 )
407410
408411 # Traverse the edge dialect op graph to create mapping from debug_handle to op node
412+ op_graph_dict = gen_graphs_from_etrecord (etrecord = self ._etrecord )
409413 debug_handle_to_op_node_map = {}
410414 create_debug_handle_to_op_node_mapping (
411- self . _op_graph_dict [EDGE_DIALECT_GRAPH_KEY ],
415+ op_graph_dict [EDGE_DIALECT_GRAPH_KEY ],
412416 debug_handle_to_op_node_map ,
413417 )
414418
@@ -479,13 +483,22 @@ def write_tensorboard_artifact(self, path: str) -> None:
479483 # TODO: implement
480484 pass
481485
482- def get_exported_program (self , graph : Optional [str ] = None ) -> ExportedProgram :
486+ def get_exported_program (
487+ self , graph : Optional [str ] = None
488+ ) -> Optional [ExportedProgram ]:
483489 """
484490 Access helper for ETRecord, defaults to returning Edge Dialect Program
485491
486492 Args:
487493 graph: Name of the graph to access. If None, returns the Edge Dialect Program.
488494 """
489- if graph is None :
490- return self ._etrecord .edge_dialect_program
491- return self ._etrecord .graph_map .get (graph )
495+ if self ._etrecord is None :
496+ log .warning (
497+ "Exported program is only available when a valid etrecord_path was provided at the time of Inspector construction"
498+ )
499+ return None
500+ return (
501+ self ._etrecord .edge_dialect_program
502+ if graph is None
503+ else self ._etrecord .graph_map .get (graph )
504+ )
0 commit comments