Skip to content

Commit 15e3b43

Browse files
cccclaifacebook-github-bot
authored andcommitted
use partitioner instance directly in to_backend (#2513)
Summary: to_backend either takes partitioner or a dict of partitioner `key: method_name, value: partitioner`. We shouldn't do key as the backend name and value as the partitioner. Reviewed By: mergennachin Differential Revision: D55078939
1 parent 9c20929 commit 15e3b43

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

examples/models/llama2/builder.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,7 @@ def export_to_edge(
286286
)
287287
return self
288288

289-
def to_backend(
290-
self, partitioner: Union[Partitioner, Dict[str, Partitioner]]
291-
) -> "LlamaEdgeManager":
289+
def to_backend(self, partitioner: Optional[Partitioner]) -> "LlamaEdgeManager":
292290
"""
293291
Partition the model and lower to different backends. The signature is
294292
aligned with the signature of `to_backend` method of EdgeManager.
@@ -297,19 +295,9 @@ def to_backend(
297295
partitioner to be sent to EdgeManager.to_backend().
298296
"""
299297
assert self.edge_manager is not None, "Need to run export_to_edge() first"
300-
if isinstance(partitioner, dict):
301-
for key, p in partitioner.items():
302-
assert self.edge_manager is not None
303-
self.edge_manager = self.edge_manager.to_backend(p)
304-
if self.verbose:
305-
logging.info(
306-
print_delegated_graph(
307-
self.edge_manager.exported_program().graph_module
308-
)
309-
)
310-
logging.info(f"Applied partitioners: {key}")
311-
elif isinstance(partitioner, Partitioner):
312-
assert self.edge_manager is not None
298+
if partitioner is None:
299+
logging.info("No partitioner provided, passing...")
300+
else:
313301
self.edge_manager = self.edge_manager.to_backend(partitioner)
314302
if self.verbose:
315303
logging.info(
@@ -318,8 +306,6 @@ def to_backend(
318306
)
319307
)
320308
logging.info(f"Applied partitioners: {partitioner}")
321-
else:
322-
logging.warning("Invalid partitioner, skipping...")
323309
return self
324310

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

examples/models/llama2/export_llama_lib.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -490,22 +490,18 @@ def _export_llama(modelname, args) -> str: # noqa: C901
490490
).export_to_edge(quantizers)
491491

492492
# to_backend
493-
partitioners = {}
493+
partitioner = None
494494
if pt2e_quant_params is not None and pt2e_quant_params.quantize_linear is not None:
495-
partitioners[XnnpackDynamicallyQuantizedPartitioner.__name__] = (
496-
XnnpackDynamicallyQuantizedPartitioner()
497-
)
495+
partitioner = XnnpackDynamicallyQuantizedPartitioner()
498496
modelname = f"xnnpack_dq_{modelname}"
499497

500498
if args.xnnpack:
501499
# Following changes due to.
502500
# 1. We need dynamically quantized partitioner for both pt2e_quantize options
503501
# as well as "qmode int4" which is also dynamic quantizes linear layers.
504502
# 2. XNNPACK partitioner seems to result in seg fault for non dqlinear ops.
505-
partitioners[XnnpackDynamicallyQuantizedPartitioner.__name__] = (
506-
XnnpackDynamicallyQuantizedPartitioner()
507-
)
508-
# partitioners[XnnpackPartitioner.__name__] = XnnpackPartitioner()
503+
partitioner = XnnpackDynamicallyQuantizedPartitioner()
504+
# partitioner = XnnpackPartitioner()
509505
modelname = f"xnnpack_{modelname}"
510506

511507
if args.vulkan:
@@ -516,7 +512,7 @@ def _export_llama(modelname, args) -> str: # noqa: C901
516512
args.quantization_mode is None
517513
), "Vulkan backend does not support quantization at the moment"
518514

519-
partitioners[VulkanPartitioner.__name__] = VulkanPartitioner()
515+
partitioner = VulkanPartitioner()
520516
modelname = f"vulkan_{modelname}"
521517

522518
if args.mps:
@@ -535,7 +531,7 @@ def _export_llama(modelname, args) -> str: # noqa: C901
535531

536532
compile_specs = [CompileSpec("use_fp16", bytes([True]))]
537533
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`.
538-
partitioners[MPSPartitioner.__name__] = MPSPartitioner(compile_specs)
534+
partitioner = MPSPartitioner(compile_specs)
539535
modelname = f"mps_{modelname}"
540536

541537
if args.generate_etrecord:
@@ -545,7 +541,7 @@ def _export_llama(modelname, args) -> str: # noqa: C901
545541
logging.info("Generating etrecord")
546542
# Copy the edge manager which will be serialized into etrecord. This is memory-wise expensive.
547543
edge_manager_copy = copy.deepcopy(builder_exported_to_edge.edge_manager)
548-
builder = builder_exported_to_edge.to_backend(partitioners).to_executorch()
544+
builder = builder_exported_to_edge.to_backend(partitioner).to_executorch()
549545

550546
# Generate ETRecord
551547
if edge_manager_copy:
@@ -556,7 +552,7 @@ def _export_llama(modelname, args) -> str: # noqa: C901
556552
)
557553
logging.info("Generated etrecord.bin")
558554
else:
559-
builder = builder_exported_to_edge.to_backend(partitioners).to_executorch()
555+
builder = builder_exported_to_edge.to_backend(partitioner).to_executorch()
560556

561557
if args.profile_memory:
562558
generate_memory_trace(builder.export_program, "memory_profile.json")

0 commit comments

Comments
 (0)