Skip to content

Commit 5ed0561

Browse files
cccclaifacebook-github-bot
authored andcommitted
add coreml backend option
Summary: As title Reviewed By: shoumikhin Differential Revision: D55172755
1 parent 7bff771 commit 5ed0561

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

examples/models/llama2/export_llama_lib.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ def build_args_parser() -> argparse.ArgumentParser:
396396
parser.add_argument("-X", "--xnnpack", action="store_true")
397397
parser.add_argument("-V", "--vulkan", action="store_true")
398398
parser.add_argument("--mps", action="store_true")
399+
parser.add_argument("--coreml", action="store_true")
399400

400401
parser.add_argument(
401402
"--expand_rope_table",
@@ -576,6 +577,39 @@ def _export_llama(modelname, args) -> str: # noqa: C901
576577
partitioners[MPSPartitioner.__name__] = MPSPartitioner(compile_specs)
577578
modelname = f"mps_{modelname}"
578579

580+
if args.coreml:
581+
assert (
582+
args.use_kv_cache is True
583+
), "CoreML backend currently only supports static shape and use_kv_cache=True is the only way to support it at the moment"
584+
try:
585+
# pyre-ignore: Undefined import [21]: Could not find a module corresponding to import `executorch.backends.apple.coreml.partition.coreml_partitioner`.
586+
import coremltools as ct
587+
588+
# pyre-ignore: Undefined import [21]: Could not find a module corresponding to import `executorch.backends.apple.coreml.compiler`
589+
from executorch.backends.apple.coreml.compiler import CoreMLBackend
590+
591+
# pyre-ignore: Undefined import [21]: Could not find a module corresponding to import `executorch.backends.apple.coreml.partition.coreml_partitioner`
592+
from executorch.backends.apple.coreml.partition.coreml_partitioner import (
593+
CoreMLPartitioner,
594+
)
595+
except ImportError:
596+
raise ImportError(
597+
"Please install the CoreML backend follwing https://pytorch.org/executorch/main/build-run-coreml.html"
598+
)
599+
600+
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`.
601+
compile_specs = CoreMLBackend.generate_compile_specs(
602+
compute_precision=ct.precision(ct.precision.FLOAT16.value),
603+
compute_unit=ct.ComputeUnit[ct.ComputeUnit.ALL.name.upper()],
604+
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`
605+
model_type=CoreMLBackend.MODEL_TYPE.MODEL,
606+
)
607+
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`
608+
partitioners[CoreMLPartitioner.__name__] = CoreMLPartitioner(
609+
skip_ops_for_coreml_delegation=None, compile_specs=compile_specs
610+
)
611+
modelname = f"coreml_{modelname}"
612+
579613
if args.generate_etrecord:
580614
if not builder_exported_to_edge.edge_manager:
581615
raise ValueError("Unable to generate etrecord due to missing edge manager.")

0 commit comments

Comments
 (0)