44
44
# edge dialect model (``EdgeProgramManager``), the ExecuTorch dialect model
45
45
# (``ExecutorchProgramManager``), and an optional dictionary of additional models
46
46
#
47
- # In this tutorial, the mobilenet v2 example model is used to demonstrate.
47
+ # In this tutorial, an example model (shown below) is used to demonstrate.
48
48
49
49
import copy
50
50
@@ -124,26 +124,61 @@ def forward(self, x):
124
124
# ---------------
125
125
#
126
126
# Next step is to generate an ``ETDump``. ``ETDump`` contains runtime results
127
- # from executing the model. To generate, users have two options:
127
+ # from executing a `Bundled Program Model <../sdk-bundled-io.html>`__.
128
+ #
129
+ # In this tutorial, a `Bundled Program` is created from the example model above.
130
+
131
+ import torch
132
+
133
+ from executorch .bundled_program .config import BundledConfig
134
+ from executorch .bundled_program .core import create_bundled_program
135
+ from executorch .bundled_program .serialize import (
136
+ serialize_from_bundled_program_to_flatbuffer ,
137
+ )
138
+
139
+ from executorch .exir import to_edge
140
+ from torch .export import export
141
+
142
+ # Step 1: ExecuTorch Program Export
143
+ m_name = "forward"
144
+ method_graphs = {m_name : export (getattr (model , m_name ), (torch .randn (1 , 1 , 32 , 32 ),))}
145
+
146
+ # Step 2: Construct BundledConfig
147
+ inputs = [[torch .randn (1 , 1 , 32 , 32 )] for _ in range (2 )]
148
+ expected_outputs = [[[getattr (model , m_name )(* x )] for x in inputs ]]
149
+ bundled_config = BundledConfig ([m_name ], [inputs ], expected_outputs )
150
+
151
+ # Step 3: Generate BundledProgram
152
+ program = to_edge (method_graphs ).to_executorch ().executorch_program
153
+ bundled_program = create_bundled_program (program , bundled_config )
154
+
155
+ # Step 4: Serialize BundledProgram to flatbuffer.
156
+ serialized_bundled_program = serialize_from_bundled_program_to_flatbuffer (
157
+ bundled_program
158
+ )
159
+ save_path = "bundled_program.bp"
160
+ with open (save_path , "wb" ) as f :
161
+ f .write (serialized_bundled_program )
162
+
163
+ ######################################################################
164
+ # We provide 2 ways of executing the Bundled Model to generate the ``ETDump``:
128
165
#
129
166
# **Option 1:**
130
167
#
131
168
# Use Buck (follow `these instructions <../getting-started-setup.html#building-a-runtime>`__ to set up buck)::
132
169
#
133
170
# cd executorch
134
- # python3 -m examples.sdk.scripts.export_bundled_program -m mv2
135
- # buck2 run -c executorch.event_tracer_enabled=true examples/sdk/sdk_example_runner:sdk_example_runner -- --bundled_program_path mv2_bundled.bpte
171
+ # buck2 run -c executorch.event_tracer_enabled=true examples/sdk/sdk_example_runner:sdk_example_runner -- --bundled_program_path <bundled_program>
136
172
#
137
173
# **Option 2:**
138
174
#
139
175
# Use CMake (follow `these instructions <../runtime-build-and-cross-compilation.html#configure-the-cmake-build>`__ to set up cmake)::
140
176
#
141
177
# cd executorch
142
- # python3 -m examples.sdk.scripts.export_bundled_program -m mv2
143
178
# rm -rf cmake-out && mkdir cmake-out && cd cmake-out && cmake -DBUCK2=buck2 -DEXECUTORCH_BUILD_SDK=1 -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=1 ..
144
179
# cd ..
145
180
# cmake --build cmake-out -j8 -t sdk_example_runner
146
- # ./cmake-out/examples/sdk/sdk_example_runner --bundled_program_path mv2_bundled.bpte
181
+ # ./cmake-out/examples/sdk/sdk_example_runner --bundled_program_path <bundled_program>
147
182
148
183
######################################################################
149
184
# Creating an Inspector
@@ -153,7 +188,7 @@ def forward(self, x):
153
188
# Inspector takes the runtime results from ``ETDump`` and correlates them to
154
189
# the operators of the Edge Dialect Graph.
155
190
#
156
- # Note : An ``ETRecord`` is not required. If an ``ETRecord`` is not provided,
191
+ # Recall : An ``ETRecord`` is not required. If an ``ETRecord`` is not provided,
157
192
# the Inspector will show runtime results without operator correlation.
158
193
#
159
194
# To visualize all runtime events, call Inspector's ``print_data_tabular``.
@@ -162,7 +197,7 @@ def forward(self, x):
162
197
163
198
# sphinx_gallery_start_ignore
164
199
inspector_patch = patch .object (Inspector , "__init__" , return_value = None )
165
- inspector_patch_print = patch .object (Inspector , "print_data_tabular" , return_value = None )
200
+ inspector_patch_print = patch .object (Inspector , "print_data_tabular" , return_value = "" )
166
201
inspector_patch .start ()
167
202
inspector_patch_print .start ()
168
203
# sphinx_gallery_end_ignore
@@ -246,8 +281,8 @@ def forward(self, x):
246
281
# If a user wants the total runtime of a module, they can use
247
282
# ``find_total_for_module``.
248
283
249
- print (inspector .find_total_for_module ("L__self___features " ))
250
- print (inspector .find_total_for_module ("L__self___features_14 " ))
284
+ print (inspector .find_total_for_module ("L__self__ " ))
285
+ print (inspector .find_total_for_module ("L__self___conv2 " ))
251
286
252
287
######################################################################
253
288
# Note: ``find_total_for_module`` is a special first class method of
0 commit comments