Skip to content

Commit 252508b

Browse files
robellfacebook-github-bot
authored andcommitted
Regression fixes and tidyup for Arm backend (#2372)
Summary: Fixes for a few things: - Move C++ std to 17 to enable changes for #1733 on Arm builds - fix after #2307 regressed save_pte_program on aot_arm_compiler - add aot_arm_compiler support for examples/models (they don't all compile but can be consumed) - Move the Vela pin in anticipation of MNV2 tosa fixes in #2371 Pull Request resolved: #2372 Reviewed By: mcr229 Differential Revision: D54817009 Pulled By: digantdesai fbshipit-source-id: 5ca3c7a03e8ae9f6a784335d53654b8db05a3cb8
1 parent 5bff24e commit 252508b

File tree

5 files changed

+74
-19
lines changed

5 files changed

+74
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ buck-out/
33
cmake-out/
44
cmake-android-out/
55
cmake-ios-out/
6+
ethos-u-scratch/
67
executorch.egg-info
78
__pycache__/
89
build/lib/

examples/arm/aot_arm_compiler.py

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,43 @@
1111
import logging
1212

1313
import torch
14-
from executorch.backends.arm.arm_backend import generate_ethosu_compile_spec
1514

15+
from executorch.backends.arm.arm_backend import generate_ethosu_compile_spec
1616
from executorch.backends.arm.arm_partitioner import ArmPartitioner
1717
from executorch.exir import EdgeCompileConfig, ExecutorchBackendConfig
1818

19+
from ..models import MODEL_NAME_TO_MODEL
20+
from ..models.model_factory import EagerModelFactory
1921
from ..portable.utils import export_to_edge, save_pte_program
2022

2123
FORMAT = "[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s"
22-
logging.basicConfig(level=logging.INFO, format=FORMAT)
23-
24-
# TODO: When we have a more reliable quantization flow through to
25-
# Vela, and use the models in their original form with a
26-
# quantization step in our example. This will take the models
27-
# from examples/models/ and quantize then export to delegate.
24+
logging.basicConfig(level=logging.WARNING, format=FORMAT)
25+
26+
# Quantize model if required using the standard export quantizaion flow.
27+
# For now we're using the xnnpack quantizer as this produces reasonable
28+
# output for our arithmetic behaviour.
29+
from torch.ao.quantization.quantize_pt2e import convert_pt2e, prepare_pt2e
30+
from torch.ao.quantization.quantizer.xnnpack_quantizer import (
31+
get_symmetric_quantization_config,
32+
XNNPACKQuantizer,
33+
)
34+
35+
36+
def quantize(model, example_inputs):
37+
"""This is the official recommended flow for quantization in pytorch 2.0 export"""
38+
logging.info("Quantizing Model...")
39+
logging.debug(f"Original model: {model}")
40+
quantizer = XNNPACKQuantizer()
41+
# if we set is_per_channel to True, we also need to add out_variant of quantize_per_channel/dequantize_per_channel
42+
operator_config = get_symmetric_quantization_config(is_per_channel=False)
43+
quantizer.set_global(operator_config)
44+
m = prepare_pt2e(model, quantizer)
45+
# calibration
46+
m(*example_inputs)
47+
m = convert_pt2e(m)
48+
logging.debug(f"Quantized model: {m}")
49+
# make sure we can export to flat buffer
50+
return m
2851

2952

3053
# Two simple models
@@ -93,7 +116,7 @@ def forward(self, x):
93116
"-m",
94117
"--model_name",
95118
required=True,
96-
help=f"Provide model name. Valid ones: {list(models.keys())}",
119+
help=f"Provide model name. Valid ones: {set(list(models.keys())+list(MODEL_NAME_TO_MODEL.keys()))}",
97120
)
98121
parser.add_argument(
99122
"-d",
@@ -103,10 +126,22 @@ def forward(self, x):
103126
default=False,
104127
help="Flag for producing ArmBackend delegated model",
105128
)
129+
parser.add_argument(
130+
"-q",
131+
"--quantize",
132+
action="store_true",
133+
required=False,
134+
default=False,
135+
help="Produce a quantized model",
136+
)
106137

107138
args = parser.parse_args()
108139

109-
if args.model_name not in models.keys():
140+
# support models defined within this file or examples/models/ lists
141+
if (
142+
args.model_name not in models.keys()
143+
and args.model_name not in MODEL_NAME_TO_MODEL.keys()
144+
):
110145
raise RuntimeError(f"Model {args.model_name} is not a valid name.")
111146

112147
if (
@@ -116,28 +151,47 @@ def forward(self, x):
116151
):
117152
raise RuntimeError(f"Model {args.model_name} cannot be delegated.")
118153

119-
model = models[args.model_name]()
120-
example_inputs = models[args.model_name].example_input
154+
# 1. pick model from one of the supported lists
155+
model = None
156+
example_inputs = None
157+
158+
# 1.a. models in this file
159+
if args.model_name in models.keys():
160+
model = models[args.model_name]()
161+
example_inputs = models[args.model_name].example_input
162+
# 1.b. models in the examples/models/
163+
# IFF the model is not in our local models
164+
elif args.model_name in MODEL_NAME_TO_MODEL.keys():
165+
logging.warning(
166+
"Using a model from examples/models not all of these are currently supported"
167+
)
168+
model, example_inputs, _ = EagerModelFactory.create_model(
169+
*MODEL_NAME_TO_MODEL[args.model_name]
170+
)
121171

122172
model = model.eval()
123173

124174
# pre-autograd export. eventually this will become torch.export
125175
model = torch._export.capture_pre_autograd_graph(model, example_inputs)
126176

177+
# Quantize if required
178+
if args.quantize:
179+
model = quantize(model, example_inputs)
180+
127181
edge = export_to_edge(
128182
model,
129183
example_inputs,
130184
edge_compile_config=EdgeCompileConfig(
131185
_check_ir_validity=False,
132186
),
133187
)
134-
logging.info(f"Exported graph:\n{edge.exported_program().graph}")
188+
logging.debug(f"Exported graph:\n{edge.exported_program().graph}")
135189

136190
if args.delegate is True:
137191
edge = edge.to_backend(
138192
ArmPartitioner(generate_ethosu_compile_spec("ethos-u55-128"))
139193
)
140-
logging.info(f"Lowered graph:\n{edge.exported_program().graph}")
194+
logging.debug(f"Lowered graph:\n{edge.exported_program().graph}")
141195

142196
exec_prog = edge.to_executorch(
143197
config=ExecutorchBackendConfig(extract_constant_segment=False)
@@ -146,4 +200,4 @@ def forward(self, x):
146200
model_name = f"{args.model_name}" + (
147201
"_arm_delegate" if args.delegate is True else ""
148202
)
149-
save_pte_program(exec_prog.buffer, model_name)
203+
save_pte_program(exec_prog, model_name)

examples/arm/ethos-u-setup/arm-none-eabi-gcc.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
3636

3737
# Select C/C++ version
3838
set(CMAKE_C_STANDARD 11)
39-
set(CMAKE_CXX_STANDARD 14)
39+
set(CMAKE_CXX_STANDARD 17)
4040

4141
set(GCC_CPU ${CMAKE_SYSTEM_PROCESSOR})
4242
string(REPLACE "cortex-m85" "cortex-m55" GCC_CPU ${GCC_CPU})

examples/arm/run.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function build_executorch_runner() {
105105
cd ${script_dir}/executor_runner
106106
cmake -DCMAKE_TOOLCHAIN_FILE=${toolchain_cmake} \
107107
-DTARGET_CPU=cortex-m55 \
108-
-B build \
108+
-B cmake-out \
109109
-DETHOS_SDK_PATH:PATH=${ethos_u_root_dir} \
110110
-DET_DIR_PATH:PATH=${et_root_dir} \
111111
-DET_BUILD_DIR_PATH:PATH=${et_build_dir} \
@@ -114,9 +114,9 @@ function build_executorch_runner() {
114114
echo "[${FUNCNAME[0]}] Configured CMAKE"
115115

116116
n=$(nproc)
117-
cmake --build build -- -j"$((n - 5))" arm_executor_runner
117+
cmake --build cmake-out -- -j"$((n - 5))" arm_executor_runner
118118
echo "[${FUNCNAME[0]}] Generated baremetal elf file:"
119-
find build -name "arm_executor_runner"
119+
find cmake-out -name "arm_executor_runner"
120120
}
121121

122122
# Execute the executor_runner on FVP Simulator

examples/arm/setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function setup_vela() {
215215
if [[ ! -e ethos-u-vela ]]; then
216216
git clone https://review.mlplatform.org/ml/ethos-u/ethos-u-vela
217217
repo_dir="${root_dir}/ethos-u-vela"
218-
base_rev=78b9412b07e0a46e58e8ecb9da8d661399c006a5
218+
base_rev=b90666d9b43f4b5223bb4dcecdbee87b2ad757c2
219219
patch_repo
220220
fi
221221
cd "${root_dir}/ethos-u-vela"

0 commit comments

Comments
 (0)