|
| 1 | +# (c) Meta Platforms, Inc. and affiliates. Confidential and proprietary. |
| 2 | + |
| 3 | +# pyre-strict |
| 4 | +import torch |
| 5 | + |
| 6 | +from executorch.exir import ExportRecipe |
| 7 | +from executorch.exir import EdgeCompileConfig, ExecutorchBackendConfig |
| 8 | + |
| 9 | +def iphone_coreml_et_recipe(ios: int = 17, compute_unit: str = "CPU_ONLY") -> ExportRecipe: |
| 10 | + import coremltools as ct |
| 11 | + from coremltools.optimize.torch.quantization.quantization_config import ( |
| 12 | + LinearQuantizerConfig, |
| 13 | + QuantizationScheme, |
| 14 | + ) |
| 15 | + from executorch.backends.apple.coreml.compiler import CoreMLBackend |
| 16 | + from executorch.backends.apple.coreml.partition import CoreMLPartitioner |
| 17 | + from executorch.backends.apple.coreml.quantizer import CoreMLQuantizer |
| 18 | + |
| 19 | + # TODO: Add compute precision, compute unit, and model type |
| 20 | + quantization_config = LinearQuantizerConfig.from_dict( |
| 21 | + { |
| 22 | + "global_config": { |
| 23 | + "quantization_scheme": QuantizationScheme.affine, |
| 24 | + "activation_dtype": torch.quint8, |
| 25 | + "weight_dtype": torch.qint8, |
| 26 | + "weight_per_channel": True, |
| 27 | + } |
| 28 | + } |
| 29 | + ) |
| 30 | + minimum_deployment_target = { |
| 31 | + 15: ct.target.iOS15, |
| 32 | + 16: ct.target.iOS16, |
| 33 | + 17: ct.target.iOS17, |
| 34 | + 18: ct.target.iOS18, |
| 35 | + }[ios] |
| 36 | + compute_unit_types = ["CPU_ONLY", "CPU_AND_NE", "CPU_AND_GPU", "ALL"] |
| 37 | + assert ( |
| 38 | + compute_unit in compute_unit_types |
| 39 | + ), f"Invalid compute unit: {compute_unit}, should be one of {compute_unit_types}" |
| 40 | + if compute_unit == "CPU_ONLY": |
| 41 | + compute_unit_specs = ct.ComputeUnit[ct.ComputeUnit.CPU_ONLY.name.upper()] |
| 42 | + elif compute_unit == "CPU_AND_NE": |
| 43 | + compute_unit_specs = ct.ComputeUnit[ct.ComputeUnit.CPU_AND_NE.name.upper()] |
| 44 | + elif compute_unit == "CPU_AND_GPU": |
| 45 | + compute_unit_specs = ct.ComputeUnit[ct.ComputeUnit.CPU_AND_GPU.name.upper()] |
| 46 | + else: # compute_unit == "ALL" |
| 47 | + compute_unit_specs = ct.ComputeUnit[ct.ComputeUnit.ALL.name.upper()] |
| 48 | + compile_specs = CoreMLBackend.generate_compile_specs( |
| 49 | + minimum_deployment_target=minimum_deployment_target, |
| 50 | + compute_precision=ct.precision(ct.precision.FLOAT16.value), |
| 51 | + compute_unit=compute_unit_specs, |
| 52 | + model_type=CoreMLBackend.MODEL_TYPE.MODEL, |
| 53 | + ) |
| 54 | + take_over_mutable_buffer = minimum_deployment_target >= ct.target.iOS18 |
| 55 | + partitioner = CoreMLPartitioner( |
| 56 | + compile_specs=compile_specs, |
| 57 | + take_over_mutable_buffer=take_over_mutable_buffer, |
| 58 | + ) |
| 59 | + return ExportRecipe( |
| 60 | + "iphone_coreml", |
| 61 | + quantizer=CoreMLQuantizer(quantization_config), |
| 62 | + partitioners=[partitioner], |
| 63 | + edge_compile_config=EdgeCompileConfig( |
| 64 | + _check_ir_validity=False, |
| 65 | + _skip_dim_order=True, |
| 66 | + ), |
| 67 | + edge_transform_passes=[], |
| 68 | + executorch_backend_config=ExecutorchBackendConfig(), |
| 69 | + ) |
0 commit comments