Skip to content

Commit f4b97b2

Browse files
cccclaifacebook-github-bot
authored andcommitted
taking list of partitioners (#2628)
Summary: bypass-github-pytorch-ci-checks Pull Request resolved: #2628 as comments from D55078939 Reviewed By: kimishpatel Differential Revision: D55232842 fbshipit-source-id: bd8ed7a6edd8561f4f33b5cc5f4fb1f3bd1de20f
1 parent 5b3f748 commit f4b97b2

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

examples/models/llama2/builder.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,26 +283,36 @@ def export_to_edge(
283283
)
284284
return self
285285

286-
def to_backend(self, partitioner: Optional[Partitioner]) -> "LlamaEdgeManager":
286+
def to_backend(
287+
self, partitioners: Optional[List[Partitioner]]
288+
) -> "LlamaEdgeManager":
287289
"""
288290
Partition the model and lower to different backends. The signature is
289291
aligned with the signature of `to_backend` method of EdgeManager.
290292
Args:
291293
partitioner (Optional[Partitioner]): One or more
292294
partitioner to be sent to EdgeManager.to_backend().
293295
"""
294-
assert self.edge_manager is not None, "Need to run export_to_edge() first"
295-
if partitioner is None:
296+
if partitioners is None:
296297
logging.info("No partitioner provided, passing...")
297298
else:
298-
self.edge_manager = self.edge_manager.to_backend(partitioner)
299-
if self.verbose:
300-
logging.info(
301-
print_delegated_graph(
302-
self.edge_manager.exported_program().graph_module
303-
)
304-
)
305-
logging.info(f"Applied partitioners: {partitioner}")
299+
for partitioner in partitioners:
300+
if partitioner is not None:
301+
assert (
302+
self.edge_manager is not None
303+
), "Need to run export_to_edge() first"
304+
self.edge_manager = self.edge_manager.to_backend(partitioner)
305+
if self.verbose:
306+
logging.info(
307+
print_delegated_graph(
308+
self.edge_manager.exported_program().graph_module
309+
)
310+
)
311+
logging.info(f"Applied partitioners: {partitioner}")
312+
else:
313+
logging.info("No partitioner provided, passing...")
314+
continue
315+
306316
return self
307317

308318
def to_executorch(self) -> "LlamaEdgeManager":

examples/models/llama2/export_llama_lib.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -602,18 +602,18 @@ def _export_llama(modelname, args) -> str: # noqa: C901
602602
).export_to_edge(quantizers)
603603

604604
# to_backend
605-
partitioner = None
605+
partitioners = []
606606
if pt2e_quant_params is not None and pt2e_quant_params.quantize_linear is not None:
607-
partitioner = XnnpackDynamicallyQuantizedPartitioner()
607+
partitioners.append(XnnpackDynamicallyQuantizedPartitioner())
608608
modelname = f"xnnpack_dq_{modelname}"
609609

610610
if args.xnnpack:
611611
# Following changes due to.
612612
# 1. We need dynamically quantized partitioner for both pt2e_quantize options
613613
# as well as "qmode 8da4w" which is also dynamic quantizes linear layers.
614614
# 2. XNNPACK partitioner seems to result in seg fault for non dqlinear ops.
615-
partitioner = XnnpackDynamicallyQuantizedPartitioner()
616-
# partitioner = XnnpackPartitioner()
615+
partitioners.append(XnnpackDynamicallyQuantizedPartitioner())
616+
# partitioners.append(XnnpackPartitioner())
617617
modelname = f"xnnpack_{modelname}"
618618

619619
if args.vulkan:
@@ -624,7 +624,7 @@ def _export_llama(modelname, args) -> str: # noqa: C901
624624
args.quantization_mode is None
625625
), "Vulkan backend does not support quantization at the moment"
626626

627-
partitioner = VulkanPartitioner()
627+
partitioners.append(VulkanPartitioner())
628628
modelname = f"vulkan_{modelname}"
629629

630630
if args.mps:
@@ -643,7 +643,7 @@ def _export_llama(modelname, args) -> str: # noqa: C901
643643

644644
compile_specs = [CompileSpec("use_fp16", bytes([True]))]
645645
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`.
646-
partitioner = MPSPartitioner(compile_specs)
646+
partitioners.append(MPSPartitioner(compile_specs))
647647
modelname = f"mps_{modelname}"
648648

649649
if args.coreml:
@@ -673,9 +673,11 @@ def _export_llama(modelname, args) -> str: # noqa: C901
673673
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`
674674
model_type=CoreMLBackend.MODEL_TYPE.MODEL,
675675
)
676-
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`
677-
partitioner = CoreMLPartitioner(
678-
skip_ops_for_coreml_delegation=None, compile_specs=compile_specs
676+
partitioners.append(
677+
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`
678+
CoreMLPartitioner(
679+
skip_ops_for_coreml_delegation=None, compile_specs=compile_specs
680+
)
679681
)
680682
modelname = f"coreml_{modelname}"
681683

@@ -730,7 +732,7 @@ def _export_llama(modelname, args) -> str: # noqa: C901
730732
logging.info("Generating etrecord")
731733
# Copy the edge manager which will be serialized into etrecord. This is memory-wise expensive.
732734
edge_manager_copy = copy.deepcopy(builder_exported_to_edge.edge_manager)
733-
builder = builder_exported_to_edge.to_backend(partitioner).to_executorch()
735+
builder = builder_exported_to_edge.to_backend(partitioners).to_executorch()
734736

735737
# Generate ETRecord
736738
if edge_manager_copy:
@@ -741,7 +743,7 @@ def _export_llama(modelname, args) -> str: # noqa: C901
741743
)
742744
logging.info("Generated etrecord.bin")
743745
else:
744-
builder = builder_exported_to_edge.to_backend(partitioner).to_executorch()
746+
builder = builder_exported_to_edge.to_backend(partitioners).to_executorch()
745747

746748
if args.profile_memory:
747749
generate_memory_trace(builder.export_program, "memory_profile.json")

0 commit comments

Comments
 (0)