Skip to content

Commit 7c49f3c

Browse files
Merge branch 'main' into delete-exir-capture
2 parents 8067435 + 6a28370 commit 7c49f3c

3 files changed

Lines changed: 56 additions & 7 deletions

File tree

‎examples/models/llama/export_llama_lib.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,11 @@ def build_args_parser() -> argparse.ArgumentParser:
462462
)
463463
parser.add_argument(
464464
"--xnnpack-extended-ops",
465-
action="store_true",
466-
help="Delegate more operators beyond DQLinear to the xnnpack backend. Requires -X or --xnnpack to be set.",
465+
action=argparse.BooleanOptionalAction,
466+
default=True,
467+
help="Delegate more operators beyond DQLinear to the xnnpack backend. On by "
468+
"default; pass --no-xnnpack-extended-ops to delegate DQLinear only. Requires "
469+
"-X or --xnnpack to be set.",
467470
)
468471
parser.add_argument(
469472
"--xnnpack-enable-bf16",
@@ -1165,7 +1168,7 @@ def _to_edge_and_lower_llama_xnnpack(
11651168
pt2e_quant_params,
11661169
quantizers,
11671170
quant_dtype,
1168-
xnnpack_extended_ops: bool = False,
1171+
xnnpack_extended_ops: bool = True,
11691172
generate_etrecord: bool = False,
11701173
verbose: bool = False,
11711174
gen_tag_fn: Optional[Callable[[torch.fx.Node], Optional[str]]] = None,
@@ -1180,15 +1183,16 @@ def _to_edge_and_lower_llama_xnnpack(
11801183
)
11811184
)
11821185

1183-
modelname = f"xnnpack_dq_{modelname}"
1184-
11851186
if xnnpack_extended_ops:
11861187
partitioners.append(
11871188
get_xnnpack_partitioner(
11881189
dynamic_quant_only_partitioner=False, enable_bf16=enable_bf16
11891190
)
11901191
)
1191-
modelname = f"xnnpack_{modelname}"
1192+
1193+
modelname = (
1194+
f"xnnpack_{modelname}" if xnnpack_extended_ops else f"xnnpack_dq_{modelname}"
1195+
)
11921196

11931197
logging.info("Lowering model using following partitioner(s): ")
11941198
for partitioner in partitioners:

‎examples/models/llama/tests/test_export_llama_lib.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,50 @@ def test_has_expected_ops_and_op_counts(self):
293293
for op, _op_info in delegation_info.delegation_by_operator.items():
294294
self.assertTrue(op not in UNWANTED_OPS)
295295

296+
def test_xnnpack_extended_ops_defaults_on(self):
297+
args = build_args_parser().parse_args([])
298+
self.assertTrue(args.xnnpack_extended_ops)
299+
self.assertTrue(LlmConfig.from_args(args).backend.xnnpack.extended_ops)
300+
301+
def test_no_xnnpack_extended_ops_opts_out(self):
302+
args = build_args_parser().parse_args(["--no-xnnpack-extended-ops"])
303+
self.assertFalse(args.xnnpack_extended_ops)
304+
self.assertFalse(LlmConfig.from_args(args).backend.xnnpack.extended_ops)
305+
306+
def test_no_xnnpack_extended_ops_builds_dq_partitioner_only(self):
307+
# Covers the multimethod path. _to_edge_and_lower_llama_xnnpack gates on
308+
# the same flag and is exercised by the full-export tests in this file.
309+
from executorch.backends.xnnpack.partition.config.xnnpack_config import (
310+
ConfigPrecisionType,
311+
)
312+
from executorch.backends.xnnpack.partition.xnnpack_partitioner import (
313+
XnnpackDynamicallyQuantizedPartitioner,
314+
)
315+
316+
def build(argv):
317+
args = build_args_parser().parse_args(["-X", *argv])
318+
return export_llama_lib._get_xnnpack_partitioners(LlmConfig.from_args(args))
319+
320+
def precisions(partitioner):
321+
return {
322+
precision
323+
for config in partitioner.target_partitioner_configs.values()
324+
for precision in config.enabled_precision_types
325+
}
326+
327+
dq_only = build(["--no-xnnpack-extended-ops"])
328+
self.assertEqual(len(dq_only), 1)
329+
self.assertIsInstance(dq_only[0], XnnpackDynamicallyQuantizedPartitioner)
330+
self.assertEqual(precisions(dq_only[0]), {ConfigPrecisionType.DYNAMIC_QUANT})
331+
332+
# The default adds a second, general partitioner. It is not a subclass
333+
# instance, and it enables precisions the dynamic-quant one cannot.
334+
both = build([])
335+
self.assertEqual(len(both), 2)
336+
self.assertIsInstance(both[0], XnnpackDynamicallyQuantizedPartitioner)
337+
self.assertNotIsInstance(both[1], XnnpackDynamicallyQuantizedPartitioner)
338+
self.assertIn(ConfigPrecisionType.FP32, precisions(both[1]))
339+
296340
def test_bf16_xnnpack_delegates_linears_when_enabled(self):
297341
parser = build_args_parser()
298342
args = parser.parse_args([])

‎extension/llm/export/config/llm_config.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,13 @@ class XNNPackConfig:
531531
Attributes:
532532
enabled: :)
533533
extended_ops: Whether to match more types of ops to delegates to XNNPack.
534+
On by default; set to False to restrict the delegate to DQLinear.
534535
enable_bf16: Whether to delegate BF16 ops to XNNPack. The target runtime
535536
must have hardware support for XNNPACK's BF16 kernels.
536537
"""
537538

538539
enabled: bool = False
539-
extended_ops: bool = False
540+
extended_ops: bool = True
540541
enable_bf16: bool = False
541542

542543

0 commit comments

Comments
 (0)