Skip to content

Commit 246009b

Browse files
authored
Test xnnpack with pybindings (#13133)
Make sure we run xnnpack delegated model using pybindings, in `test_models.sh`.
1 parent d873063 commit 246009b

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

.ci/scripts/test_model.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ test_model_with_xnnpack() {
131131
return 0
132132
fi
133133

134-
# Delegation
134+
# Delegation and test with pybindings
135135
if [[ ${WITH_QUANTIZATION} == true ]]; then
136136
SUFFIX="q8"
137-
"${PYTHON_EXECUTABLE}" -m examples.xnnpack.aot_compiler --model_name="${MODEL_NAME}" --delegate --quantize
137+
"${PYTHON_EXECUTABLE}" -m examples.xnnpack.aot_compiler --model_name="${MODEL_NAME}" --delegate --quantize --test_after_export
138138
else
139139
SUFFIX="fp32"
140-
"${PYTHON_EXECUTABLE}" -m examples.xnnpack.aot_compiler --model_name="${MODEL_NAME}" --delegate
140+
"${PYTHON_EXECUTABLE}" -m examples.xnnpack.aot_compiler --model_name="${MODEL_NAME}" --delegate --test_after_export
141141
fi
142142

143143
OUTPUT_MODEL_PATH="${MODEL_NAME}_xnnpack_${SUFFIX}.pte"

examples/xnnpack/aot_compiler.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
default="",
6262
help="Generate and save an ETRecord to the given file location",
6363
)
64+
parser.add_argument(
65+
"-t",
66+
"--test_after_export",
67+
action="store_true",
68+
required=False,
69+
default=False,
70+
help="Test the pte with pybindings",
71+
)
6472
parser.add_argument("-o", "--output_dir", default=".", help="output directory")
6573

6674
args = parser.parse_args()
@@ -117,3 +125,24 @@
117125
quant_tag = "q8" if args.quantize else "fp32"
118126
model_name = f"{args.model_name}_xnnpack_{quant_tag}"
119127
save_pte_program(exec_prog, model_name, args.output_dir)
128+
129+
if args.test_after_export:
130+
logging.info("Testing the pte with pybind")
131+
from executorch.extension.pybindings.portable_lib import (
132+
_load_for_executorch_from_buffer,
133+
)
134+
135+
# Import custom ops. This requires portable_lib to be loaded first.
136+
from executorch.extension.llm.custom_ops import ( # noqa: F401, F403
137+
custom_ops,
138+
) # usort: skip
139+
140+
# Import quantized ops. This requires portable_lib to be loaded first.
141+
from executorch.kernels import quantized # usort: skip # noqa: F401, F403
142+
from torch.utils._pytree import tree_flatten
143+
144+
m = _load_for_executorch_from_buffer(exec_prog.buffer)
145+
logging.info("Successfully loaded the model")
146+
flattened = tree_flatten(example_inputs)[0]
147+
res = m.run_method("forward", flattened)
148+
logging.info("Successfully ran the model")

0 commit comments

Comments
 (0)