Skip to content

Commit fca61b8

Browse files
mcr229facebook-github-bot
authored andcommitted
example (#64)
Summary: Pull Request resolved: #64 Adding export example for XNNPACK delegated models, also adding to executor runner to run Differential Revision: D48371417 fbshipit-source-id: 00c7e069dc91771d70eadb007319694b3fe25504
1 parent bad6ccb commit fca61b8

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

examples/executor_runner/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def define_common_targets():
1616
name = "executor_runner",
1717
srcs = ["executor_runner.cpp"],
1818
deps = [
19+
"//executorch/backends/xnnpack:xnnpack_backend",
1920
"//executorch/runtime/executor/test:test_backend_compiler_lib",
2021
"//executorch/runtime/executor:program",
2122
"//executorch/extension/data_loader:file_data_loader",
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
# Example script for exporting simple models to flatbuffer
8+
9+
import argparse
10+
11+
import executorch.exir as exir
12+
import torch
13+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import (
14+
XnnpackFloatingPointPartitioner,
15+
)
16+
from executorch.exir.backend.backend_api import to_backend, validation_disabled
17+
18+
from ..models import MODEL_NAME_TO_MODEL
19+
20+
21+
def export_add_module_with_lower_graph():
22+
class AddModule(torch.nn.Module):
23+
def __init__(self):
24+
super().__init__()
25+
26+
def forward(self, x):
27+
return x + x
28+
29+
capture_config = exir.CaptureConfig(pt2_mode=True, enable_dynamic_shape=False)
30+
edge_compile_config = exir.EdgeCompileConfig()
31+
sample_inputs = (torch.ones(1, 2, 3, 4),)
32+
print("Running the example to export a composite module with lowered graph...")
33+
edge = exir.capture(AddModule(), sample_inputs, capture_config).to_edge(
34+
edge_compile_config
35+
)
36+
print("Exported graph:\n", edge.exported_program.graph)
37+
38+
# Lower AddMulModule to the demo backend
39+
print("Lowering to the demo backend...")
40+
edge.exported_program = to_backend(
41+
edge.exported_program, XnnpackFloatingPointPartitioner
42+
)
43+
44+
# The graph module is still runnerable
45+
edge.exported_program.graph_module(*sample_inputs)
46+
47+
print("Lowered graph:\n", edge.exported_program.graph)
48+
49+
exec_prog = edge.to_executorch()
50+
buffer = exec_prog.buffer
51+
52+
model_name = "xnnpack_add"
53+
filename = f"{model_name}.ff"
54+
print(f"Saving exported program to {filename}")
55+
with open(filename, "wb") as file:
56+
file.write(buffer)
57+
58+
59+
def export_mv2_with_lower_graph():
60+
mv2, example_inputs = MODEL_NAME_TO_MODEL["mv2"]()
61+
mv2 = mv2.eval()
62+
capture_config = exir.CaptureConfig(pt2_mode=True, enable_dynamic_shape=False)
63+
edge_compile_config = exir.EdgeCompileConfig(_check_ir_validity=False)
64+
65+
edge = exir.capture(mv2, example_inputs, capture_config).to_edge(
66+
edge_compile_config
67+
)
68+
with validation_disabled():
69+
edge.exported_program = to_backend(
70+
edge.exported_program, XnnpackFloatingPointPartitioner
71+
)
72+
73+
edge.exported_program.graph_module(*example_inputs)
74+
print("Lowered graph: \n", edge.exported_program.graph)
75+
76+
exec_prog = edge.to_executorch()
77+
buffer = exec_prog.buffer
78+
79+
model_name = "xnnpack_mv2"
80+
filename = f"{model_name}.ff"
81+
print(f"Saving exported program to {filename}")
82+
with open(filename, "wb") as file:
83+
file.write(buffer)
84+
85+
86+
OPTIONS_TO_LOWER = {
87+
"add": export_add_module_with_lower_graph,
88+
"mv2": export_mv2_with_lower_graph,
89+
}
90+
91+
if __name__ == "__main__":
92+
parser = argparse.ArgumentParser()
93+
parser.add_argument(
94+
"--option",
95+
required=True,
96+
choices=list(OPTIONS_TO_LOWER.keys()),
97+
help=f"Provide the flow name. Valid ones: {list(OPTIONS_TO_LOWER.keys())}",
98+
)
99+
100+
args = parser.parse_args()
101+
102+
# Choose one option
103+
option = OPTIONS_TO_LOWER[args.option]
104+
105+
# Run the example flow
106+
option()

0 commit comments

Comments
 (0)