Skip to content

add coreml backend option #2554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/models/llama2/export_llama_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ def build_args_parser() -> argparse.ArgumentParser:
parser.add_argument("-X", "--xnnpack", action="store_true")
parser.add_argument("-V", "--vulkan", action="store_true")
parser.add_argument("--mps", action="store_true")
parser.add_argument("--coreml", action="store_true")

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

if args.coreml:
assert (
args.use_kv_cache is True
), "CoreML backend currently only supports static shape and use_kv_cache=True is the only way to support it at the moment"
try:
# pyre-ignore: Undefined import [21]: Could not find a module corresponding to import `executorch.backends.apple.coreml.partition.coreml_partitioner`.
import coremltools as ct

# pyre-ignore: Undefined import [21]: Could not find a module corresponding to import `executorch.backends.apple.coreml.compiler`
from executorch.backends.apple.coreml.compiler import CoreMLBackend

# pyre-ignore: Undefined import [21]: Could not find a module corresponding to import `executorch.backends.apple.coreml.partition.coreml_partitioner`
from executorch.backends.apple.coreml.partition.coreml_partitioner import (
CoreMLPartitioner,
)
except ImportError:
raise ImportError(
"Please install the CoreML backend follwing https://pytorch.org/executorch/main/build-run-coreml.html"
)

# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`.
compile_specs = CoreMLBackend.generate_compile_specs(
compute_precision=ct.precision(ct.precision.FLOAT16.value),
compute_unit=ct.ComputeUnit[ct.ComputeUnit.ALL.name.upper()],
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`
model_type=CoreMLBackend.MODEL_TYPE.MODEL,
)
# pyre-ignore: Undefined attribute [16]: Module `executorch.backends` has no attribute `apple`
partitioners[CoreMLPartitioner.__name__] = CoreMLPartitioner(
skip_ops_for_coreml_delegation=None, compile_specs=compile_specs
)
modelname = f"coreml_{modelname}"

if args.generate_etrecord:
if not builder_exported_to_edge.edge_manager:
raise ValueError("Unable to generate etrecord due to missing edge manager.")
Expand Down