Skip to content

Commit 0ece196

Browse files
larryliu0820facebook-github-bot
authored andcommitted
Register quantized ops into quantization example (#85)
Summary: Pull Request resolved: #85 Enables the ability to `export_to_pte`. Previously quantized models can't run `export_to_pte` because some quantized ops are missing out variants. Recently we added support for custom ops and register them into EXIR by loading shared library. This diff adds support for registering quantized ops out variants and add it into CI. Reviewed By: huydhn Differential Revision: D48541611 fbshipit-source-id: e8740383abb38704a6450de200572400498ce4d5
1 parent 511a85d commit 0ece196

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

.ci/scripts/test.sh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,13 @@ test_model() {
3535
buck2 run //examples/executor_runner:executor_runner -- --model_path "./${MODEL_NAME}.pte"
3636
elif [[ "${BUILD_TOOL}" == "cmake" ]]; then
3737
CMAKE_OUTPUT_DIR=cmake-out
38-
./"${CMAKE_OUTPUT_DIR}"/executor_runner --model_path "./${MODEL_NAME}.pte"
38+
./${CMAKE_OUTPUT_DIR}/executor_runner --model_path "./${MODEL_NAME}.pte"
3939
else
4040
echo "Invalid build tool ${BUILD_TOOL}. Only buck2 and cmake are supported atm"
4141
exit 1
4242
fi
4343
}
4444

45-
test_quantized_model() {
46-
python -m examples.quantization.example --model_name="${MODEL_NAME}"
47-
}
4845

4946
which python
5047

@@ -53,7 +50,7 @@ echo "Testing ${MODEL_NAME} with ${BUILD_TOOL}..."
5350
test_model
5451

5552
if [[ "${QUANTIZATION}" == true ]]; then
56-
test_quantized_model
53+
bash examples/quantization/test_quantize.sh "${MODEL_NAME}"
5754
else
5855
echo "The model ${MODEL_NAME} doesn't support quantization yet"
5956
fi

examples/quantization/example.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
XNNPACKQuantizer,
2626
)
2727

28-
# TODO: maybe move this to examples/export/utils.py?
29-
# from ..export.export_example import export_to_ff
28+
from ..export.export_example import export_to_pte
3029

3130
from ..models import MODEL_NAME_TO_MODEL
3231

@@ -51,8 +50,7 @@ def quantize(model_name, model, example_inputs):
5150
m = convert_pt2e(m)
5251
print("quantized model:", m)
5352
# make sure we can export to flat buffer
54-
# Note: this is not working yet due to missing out variant ops for quantize_per_tensor/dequantize_per_tensor ops
55-
# aten = export_to_ff(model_name, m, copy.deepcopy(example_inputs))
53+
export_to_pte(model_name, m, copy.deepcopy(example_inputs))
5654

5755

5856
def verify_xnnpack_quantizer_matching_fx_quant_model(model_name, model, example_inputs):
@@ -114,9 +112,16 @@ def verify_xnnpack_quantizer_matching_fx_quant_model(model_name, model, example_
114112
default=False,
115113
help="flag for verifying XNNPACKQuantizer against fx graph mode quantization",
116114
)
115+
parser.add_argument(
116+
"-s",
117+
"--so_library",
118+
required=False,
119+
help="shared library for quantized operators",
120+
)
117121

118122
args = parser.parse_args()
119-
123+
if args.so_library:
124+
torch.ops.load_library(args.so_library)
120125
if not args.verify and args.model_name not in QUANT_MODEL_NAME_TO_MODEL:
121126
raise RuntimeError(
122127
f"Model {args.model_name} is not a valid name. or not quantizable right now, "
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
# Test the end-to-end quantization flow.
9+
10+
set -e
11+
12+
# TODO(larryliu0820): Add CMake build
13+
test_buck2_quantization() {
14+
echo "Building quantized ops shared library"
15+
SO_LIB=$(buck2 build //kernels/quantized:aot_lib --show-output | grep "buck-out" | cut -d" " -f2)
16+
17+
echo "Run example.py"
18+
python -m "examples.quantization.example" --so_library="$SO_LIB" --model_name="$1"
19+
}
20+
21+
test_buck2_quantization "$1"

kernels/quantized/targets.bzl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@fbsource//xplat/executorch/build:runtime_wrapper.bzl", "runtime")
2-
load("@fbsource//xplat/executorch/codegen:codegen.bzl", "et_operator_library", "executorch_generated_lib")
2+
load("@fbsource//xplat/executorch/codegen:codegen.bzl", "et_operator_library", "executorch_generated_lib", "exir_custom_ops_aot_lib")
33

44
def define_common_targets():
55
runtime.export_file(
@@ -15,6 +15,17 @@ def define_common_targets():
1515
define_static_targets = True,
1616
)
1717

18+
# lib used to register quantized ops into EXIR
19+
exir_custom_ops_aot_lib(
20+
name = "aot_lib",
21+
yaml_target = ":quantized.yaml",
22+
visibility = ["//executorch/..."],
23+
kernels = [":quantized_operators_aten"],
24+
deps = [
25+
":all_quantized_ops",
26+
],
27+
)
28+
1829
for aten_mode in (True, False):
1930
aten_suffix = "_aten" if aten_mode else ""
2031

shim/xplat/executorch/kernels/portable/op_registration_util.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ def define_op_library(name, deps, android_deps, aten_target, _allow_third_party_
123123
deps = [
124124
"//executorch/runtime/kernel:kernel_includes" + aten_suffix,
125125
] + deps,
126+
# WARNING: using a deprecated API to avoid being built into a shared
127+
# library. In the case of dynamically loading so library we don't want
128+
# it to depend on other so libraries because that way we have to
129+
# specify library directory path.
130+
force_static = True,
126131
# link_whole is necessary because the operators register themselves
127132
# via static initializers that run at program startup.
128133
# @lint-ignore BUCKLINT link_whole

0 commit comments

Comments
 (0)