xref: /aosp_15_r20/external/executorch/docs/source/model-inspector.rst (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1Inspector APIs
2==============
3
4Overview
5--------
6
7The Inspector APIs provide a convenient interface for analyzing the
8contents of `ETRecord <etrecord.html>`__ and
9`ETDump <etdump.html>`__, helping developers get insights about model
10architecture and performance statistics. It’s built on top of the `EventBlock Class <#eventblock-class>`__ data structure,
11which organizes a group of `Event <#event-class>`__\ s for easy access to details of profiling events.
12
13There are multiple ways in which users can interact with the Inspector
14APIs:
15
16* By using `public methods <#inspector-methods>`__ provided by the ``Inspector`` class.
17* By accessing the `public attributes <#inspector-attributes>`__ of the ``Inspector``, ``EventBlock``, and ``Event`` classes.
18* By using a `CLI <#cli>`__ tool for basic functionalities.
19
20Please refer to the `e2e use case doc <tutorials/devtools-integration-tutorial.html>`__ get an understanding of how to use these in a real world example.
21
22
23Inspector Methods
24-----------------
25
26Constructor
27~~~~~~~~~~~
28
29.. autofunction:: executorch.devtools.Inspector.__init__
30
31**Example Usage:**
32
33.. code:: python
34
35    from executorch.devtools import Inspector
36
37    inspector = Inspector(etdump_path="/path/to/etdump.etdp", etrecord="/path/to/etrecord.bin")
38
39to_dataframe
40~~~~~~~~~~~~~~~~
41
42.. autofunction:: executorch.devtools.Inspector.to_dataframe
43
44
45print_data_tabular
46~~~~~~~~~~~~~~~~~~
47
48.. autofunction:: executorch.devtools.Inspector.print_data_tabular
49
50.. _example-usage-1:
51
52**Example Usage:**
53
54.. code:: python
55
56    inspector.print_data_tabular()
57
58.. image:: _static/img/print_data_tabular.png
59Note that the unit of delegate profiling events is "cycles". We're working on providing a way to set different units in the future.
60
61
62find_total_for_module
63~~~~~~~~~~~~~~~~~~~~~
64
65.. autofunction:: executorch.devtools.Inspector.find_total_for_module
66
67.. _example-usage-2:
68
69**Example Usage:**
70
71.. code:: python
72
73    print(inspector.find_total_for_module("L__self___conv_layer"))
74
75::
76
77    0.002
78
79
80get_exported_program
81~~~~~~~~~~~~~~~~~~~~
82
83.. autofunction:: executorch.devtools.Inspector.get_exported_program
84
85.. _example-usage-3:
86
87**Example Usage:**
88
89.. code:: python
90
91    print(inspector.get_exported_program())
92
93::
94
95    ExportedProgram:
96        class GraphModule(torch.nn.Module):
97            def forward(self, arg0_1: f32[4, 3, 64, 64]):
98                # No stacktrace found for following nodes
99                _param_constant0 = self._param_constant0
100                _param_constant1 = self._param_constant1
101
102                ### ... Omit part of the program for documentation readability ... ###
103
104    Graph signature: ExportGraphSignature(parameters=[], buffers=[], user_inputs=['arg0_1'], user_outputs=['aten_tan_default'], inputs_to_parameters={}, inputs_to_buffers={}, buffers_to_mutate={}, backward_signature=None, assertion_dep_token=None)
105    Range constraints: {}
106    Equality constraints: []
107
108
109Inspector Attributes
110--------------------
111
112``EventBlock`` Class
113~~~~~~~~~~~~~~~~~~~~
114
115Access ``EventBlock`` instances through the ``event_blocks`` attribute
116of an ``Inspector`` instance, for example:
117
118.. code:: python
119
120    inspector.event_blocks
121
122.. autoclass:: executorch.devtools.inspector.EventBlock
123
124``Event`` Class
125~~~~~~~~~~~~~~~
126
127Access ``Event`` instances through the ``events`` attribute of an
128``EventBlock`` instance.
129
130.. autoclass:: executorch.devtools.inspector.Event
131
132**Example Usage:**
133
134.. code:: python
135
136    for event_block in inspector.event_blocks:
137        for event in event_block.events:
138            if event.name == "Method::execute":
139                print(event.perf_data.raw)
140
141::
142
143    [175.748, 78.678, 70.429, 122.006, 97.495, 67.603, 70.2, 90.139, 66.344, 64.575, 134.135, 93.85, 74.593, 83.929, 75.859, 73.909, 66.461, 72.102, 84.142, 77.774, 70.038, 80.246, 59.134, 68.496, 67.496, 100.491, 81.162, 74.53, 70.709, 77.112, 59.775, 79.674, 67.54, 79.52, 66.753, 70.425, 71.703, 81.373, 72.306, 72.404, 94.497, 77.588, 79.835, 68.597, 71.237, 88.528, 71.884, 74.047, 81.513, 76.116]
144
145
146CLI
147---
148
149Execute the following command in your terminal to display the data
150table. This command produces the identical table output as calling the
151`print_data_tabular <#print-data-tabular>`__ mentioned earlier:
152
153.. code:: bash
154
155    python3 -m devtools.inspector.inspector_cli --etdump_path <path_to_etdump> --etrecord_path <path_to_etrecord>
156
157Note that the `etrecord_path` argument is optional.
158
159We plan to extend the capabilities of the CLI in the future.
160