Skip to content

Commit 2cc9715

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: 321fd6245ad4fa0da317f4cc209a832bb117c7d6
1 parent a6c7533 commit 2cc9715

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

examples/backend/TARGETS

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2+
3+
runtime.python_binary(
4+
name = "xnnpack_lowering_examples",
5+
main_src = "xnnpack_lowering_examples.py",
6+
deps = [
7+
"//caffe2:torch",
8+
"//executorch/backends/xnnpack:xnnpack_preprocess",
9+
"//executorch/backends/xnnpack/partition:xnnpack_partitioner",
10+
"//executorch/examples/models:models",
11+
"//executorch/exir/backend:backend_api",
12+
],
13+
)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
import copy
11+
12+
import executorch.exir as exir
13+
import torch._export as export
14+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import (
15+
XnnpackFloatingPointPartitioner,
16+
XnnpackQuantizedPartitioner2,
17+
)
18+
from executorch.exir.backend.backend_api import to_backend, validation_disabled
19+
20+
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
21+
from torch.ao.quantization.quantizer.xnnpack_quantizer import (
22+
get_symmetric_quantization_config,
23+
XNNPACKQuantizer,
24+
)
25+
26+
from ..models import MODEL_NAME_TO_MODEL
27+
28+
# Note: for mv3, the mul op is not supported in XNNPACKQuantizer, that could be supported soon
29+
XNNPACK_MODEL_NAME_TO_MODEL = {
30+
name: MODEL_NAME_TO_MODEL[name] for name in ["linear", "add", "add_mul", "mv2"]
31+
}
32+
33+
34+
def quantize(model, example_inputs):
35+
"""This is the official recommended flow for quantization in pytorch 2.0 export"""
36+
m = model.eval()
37+
m = export.capture_pre_autograd_graph(m, copy.deepcopy(example_inputs))
38+
quantizer = XNNPACKQuantizer()
39+
# if we set is_per_channel to True, we also need to add out_variant of quantize_per_channel/dequantize_per_channel
40+
operator_config = get_symmetric_quantization_config(is_per_channel=False)
41+
quantizer.set_global(operator_config)
42+
m = prepare_pt2e(m, quantizer)
43+
# calibration
44+
m(*example_inputs)
45+
m = convert_pt2e(m)
46+
return m
47+
48+
49+
if __name__ == "__main__":
50+
parser = argparse.ArgumentParser()
51+
parser.add_argument(
52+
"-m",
53+
"--model_name",
54+
required=True,
55+
help=f"Provide model name. Valid ones: {list(XNNPACK_MODEL_NAME_TO_MODEL.keys())}",
56+
)
57+
parser.add_argument(
58+
"-q",
59+
"--quantize",
60+
action="store_true",
61+
required=False,
62+
default=False,
63+
help="Flag for producing quantized or floating-point model",
64+
)
65+
args = parser.parse_args()
66+
67+
if args.model_name not in XNNPACK_MODEL_NAME_TO_MODEL:
68+
raise RuntimeError(
69+
f"Model {args.model_name} is not a valid name. or not quantizable right now, "
70+
"please contact executorch team if you want to learn why or how to support "
71+
"quantization for the requested model"
72+
f"Available models are {list(XNNPACK_MODEL_NAME_TO_MODEL.keys())}."
73+
)
74+
75+
model, example_inputs = MODEL_NAME_TO_MODEL[args.model_name]()
76+
model = model.eval()
77+
78+
partitioner = XnnpackFloatingPointPartitioner
79+
if args.quantize:
80+
print("Quantizing Model...")
81+
model = quantize(model, example_inputs)
82+
partitioner = XnnpackQuantizedPartitioner2
83+
84+
edge = exir.capture(
85+
model, example_inputs, exir.CaptureConfig(enable_aot=True, _unlift=True)
86+
).to_edge(exir.EdgeCompileConfig(_check_ir_validity=False))
87+
print("Exported graph:\n", edge.exported_program.graph)
88+
89+
with validation_disabled():
90+
edge.exported_program = to_backend(edge.exported_program, partitioner)
91+
print("Lowered graph:\n", edge.exported_program.graph)
92+
93+
exec_prog = edge.to_executorch()
94+
buffer = exec_prog.buffer
95+
quant_tag = "_quantize" if args.quantize else ""
96+
filename = f"xnnpack_{args.model_name}{quant_tag}.pte"
97+
print(f"Saving exported program to {filename}.")
98+
with open(filename, "wb") as f:
99+
f.write(buffer)

examples/executor_runner/targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def define_common_targets():
2121
name = "executor_runner",
2222
srcs = ["executor_runner.cpp"],
2323
deps = [
24+
"//executorch/backends/xnnpack:xnnpack_backend",
2425
"//executorch/runtime/executor/test:test_backend_compiler_lib",
2526
"//executorch/runtime/executor:program",
2627
"//executorch/extension/data_loader:file_data_loader",

0 commit comments

Comments
 (0)