Skip to content

Commit 57e73a8

Browse files
Jack-Khuufacebook-github-bot
authored andcommitted
Update tutorial to reference dummy model instead of MV2
Summary: SDK Tutorial moved to using an inline model instead of mv2 for cleaner imports - This diff aligns the rest of the tutorial to the inline model **Aside:** - Adds a link to bundled_program for users to reference - Mocks outputs for website (to compensate for runtime steps occurring outside of notebook) Differential Revision: D50515890
1 parent 08fe6b2 commit 57e73a8

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

docs/source/tutorials_source/sdk-integration-tutorial.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
# edge dialect model (``EdgeProgramManager``), the ExecuTorch dialect model
4545
# (``ExecutorchProgramManager``), and an optional dictionary of additional models
4646
#
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.
4848

4949
import copy
5050

@@ -124,26 +124,61 @@ def forward(self, x):
124124
# ---------------
125125
#
126126
# 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``:
128165
#
129166
# **Option 1:**
130167
#
131168
# Use Buck (follow `these instructions <../getting-started-setup.html#building-a-runtime>`__ to set up buck)::
132169
#
133170
# 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>
136172
#
137173
# **Option 2:**
138174
#
139175
# Use CMake (follow `these instructions <../runtime-build-and-cross-compilation.html#configure-the-cmake-build>`__ to set up cmake)::
140176
#
141177
# cd executorch
142-
# python3 -m examples.sdk.scripts.export_bundled_program -m mv2
143178
# rm -rf cmake-out && mkdir cmake-out && cd cmake-out && cmake -DBUCK2=buck2 -DEXECUTORCH_BUILD_SDK=1 -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=1 ..
144179
# cd ..
145180
# 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>
147182

148183
######################################################################
149184
# Creating an Inspector
@@ -153,7 +188,7 @@ def forward(self, x):
153188
# Inspector takes the runtime results from ``ETDump`` and correlates them to
154189
# the operators of the Edge Dialect Graph.
155190
#
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,
157192
# the Inspector will show runtime results without operator correlation.
158193
#
159194
# To visualize all runtime events, call Inspector's ``print_data_tabular``.
@@ -162,7 +197,7 @@ def forward(self, x):
162197

163198
# sphinx_gallery_start_ignore
164199
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="")
166201
inspector_patch.start()
167202
inspector_patch_print.start()
168203
# sphinx_gallery_end_ignore
@@ -246,8 +281,8 @@ def forward(self, x):
246281
# If a user wants the total runtime of a module, they can use
247282
# ``find_total_for_module``.
248283

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"))
251286

252287
######################################################################
253288
# Note: ``find_total_for_module`` is a special first class method of

0 commit comments

Comments
 (0)