@@ -396,6 +396,7 @@ def build_args_parser() -> argparse.ArgumentParser:
396
396
parser .add_argument ("-X" , "--xnnpack" , action = "store_true" )
397
397
parser .add_argument ("-V" , "--vulkan" , action = "store_true" )
398
398
parser .add_argument ("--mps" , action = "store_true" )
399
+ parser .add_argument ("--coreml" , action = "store_true" )
399
400
400
401
parser .add_argument (
401
402
"--expand_rope_table" ,
@@ -576,6 +577,39 @@ def _export_llama(modelname, args) -> str: # noqa: C901
576
577
partitioners [MPSPartitioner .__name__ ] = MPSPartitioner (compile_specs )
577
578
modelname = f"mps_{ modelname } "
578
579
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
+
579
613
if args .generate_etrecord :
580
614
if not builder_exported_to_edge .edge_manager :
581
615
raise ValueError ("Unable to generate etrecord due to missing edge manager." )
0 commit comments