|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import os |
| 8 | +from typing import Mapping, Optional |
| 9 | + |
| 10 | +from executorch.sdk.edir.et_schema import InferenceRun, OperatorGraphWithStats |
| 11 | +from pandas import DataFrame |
| 12 | + |
| 13 | + |
| 14 | +class Inspector: |
| 15 | + |
| 16 | + """ |
| 17 | + APIs for examining model architecture and performance stats |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + op_graph_dict: Mapping[str, OperatorGraphWithStats], |
| 23 | + show_stack_trace: Optional[bool] = False, |
| 24 | + verbose: Optional[bool] = False, |
| 25 | + ) -> None: |
| 26 | + """ |
| 27 | + Constructor that returns a Debugger instance from a dict of OperatorGraph instances and some optional parameters |
| 28 | + """ |
| 29 | + # Save the parameters into class members |
| 30 | + self.op_graph_dict = op_graph_dict |
| 31 | + self.show_stack_trace = show_stack_trace |
| 32 | + self.verbose = verbose |
| 33 | + |
| 34 | + # Construct the initial tables and save in class members |
| 35 | + self.architecture_table = self._gen_architecture_table() |
| 36 | + self.high_level_profile_info_table = self._gen_high_level_profile_info_table() |
| 37 | + |
| 38 | + def attach_etdump(self, etdump_path: str) -> None: |
| 39 | + """ |
| 40 | + API that attaches ETDump to this inspector instance |
| 41 | + """ |
| 42 | + op_graph = self.op_graph_dict["et_dialect_graph_module/forward"] |
| 43 | + |
| 44 | + if os.path.exists(etdump_path): |
| 45 | + op_graph.attach_metadata( |
| 46 | + inference_run=InferenceRun.extract_runs_from_path( |
| 47 | + file_path=etdump_path |
| 48 | + )[0] |
| 49 | + ) |
| 50 | + else: |
| 51 | + raise Exception("Invalid ET Dump path") |
| 52 | + |
| 53 | + def get_high_level_profile_info_table(self) -> DataFrame: |
| 54 | + """ |
| 55 | + API that returns the high level profile information table from class member |
| 56 | + """ |
| 57 | + return self.high_level_profile_info_table |
| 58 | + |
| 59 | + def get_architecture_table(self) -> DataFrame: |
| 60 | + """ |
| 61 | + API that returns the architecture table from class member, filtered by user options (e.g. self.show_stack_trace) |
| 62 | + """ |
| 63 | + # TODO: filter based on user options (self.show_stack_trace) before return |
| 64 | + return self.architecture_table |
| 65 | + |
| 66 | + def cli_flow(self): |
| 67 | + """ |
| 68 | + API that enters the CLI debugging flow |
| 69 | + """ |
| 70 | + print("Entering the CLI debugging flow...") |
| 71 | + |
| 72 | + print("High level profile information table:") |
| 73 | + print(self.get_high_level_profile_info_table()) |
| 74 | + print("Architecture table:") |
| 75 | + print(self.get_architecture_table()) |
| 76 | + |
| 77 | + # TODO: Take user commands, process by calling other APIs in the Inspector class |
| 78 | + |
| 79 | + def select_by_instance_id(self, instance_id: str) -> DataFrame: |
| 80 | + """ |
| 81 | + API that returns a DataFrame containing information for a specific instance id |
| 82 | + """ |
| 83 | + # TODO: filter the architecture table by the instance id before return |
| 84 | + return self.architecture_table |
| 85 | + |
| 86 | + def select_by_instance_type(self, instance_type: str) -> DataFrame: |
| 87 | + """ |
| 88 | + API that returns a DataFrame containing instances with the given instance type |
| 89 | + """ |
| 90 | + # TODO: filter the architecture table by the instance type before return |
| 91 | + return self.architecture_table |
| 92 | + |
| 93 | + def _gen_high_level_profile_info_table(self) -> DataFrame: |
| 94 | + """ |
| 95 | + Private helper function that generates the high level profile information table |
| 96 | + """ |
| 97 | + # TODO: implement |
| 98 | + pass |
| 99 | + |
| 100 | + def _gen_architecture_table(self) -> DataFrame: |
| 101 | + """ |
| 102 | + Private helper function that generates the architecture table |
| 103 | + """ |
| 104 | + # TODO: implement |
| 105 | + pass |
0 commit comments