|
| 1 | +## CoreML Backend |
| 2 | + |
| 3 | + |
| 4 | +For setting up the **CoreML** backend please follow the instructions described in `backend/coreml/setup.md`. |
| 5 | + |
| 6 | +## AOT |
| 7 | + |
| 8 | +For delegating the Program to the **CoreML** backend, the client must be responsible for calling `to_backend` with the **CoreMLBackend** tag. |
| 9 | + |
| 10 | +``` |
| 11 | +from executorch.backends.coreml.compiler import CoreMLBackend |
| 12 | +
|
| 13 | +# This will delegate the whole program to the CoreML backend. |
| 14 | +lowered_module = to_backend("CoreMLBackend", edge.exported_program, []) |
| 15 | +
|
| 16 | +``` |
| 17 | + |
| 18 | +Currently, the **CoreML** backend would delegate the whole module to **CoreML**. If a specific op is not supported by the **CoreML** backend then the `to_backend` call would throw an exception. We will be adding a **Partitioner** for **CoreML** soon to resolve the issue. |
| 19 | + |
| 20 | +The `preprocess` code is located at `backends/coreml/coreml_preprocess.py`. The implementation uses `coremltools` to convert the **ExportedProgram** to **MLPackage** which is flattened and returned as bytes to **ExecuTorch**. |
| 21 | + |
| 22 | +### Compute Units |
| 23 | + |
| 24 | +The `CoreML` runtime by default loads the model with compute units set to `MLComputeUnitsAll`. This can be overridden by providing `compute_units` at the preprocessing stage. |
| 25 | + |
| 26 | +**MLComputeUnitsCPUOnly** |
| 27 | + |
| 28 | +``` |
| 29 | +# cpu is equivalent to MLComputeUnitsCPUOnly. |
| 30 | +lowered_module = to_backend("CoreMLBackend", edge.exported_program, [CompileSpec("compute_units", bytes("cpu", 'utf-8'))]) |
| 31 | +
|
| 32 | +``` |
| 33 | + |
| 34 | +**MLComputeUnitsCPUAndGPU** |
| 35 | + |
| 36 | +``` |
| 37 | +# gpu is equivalent to MLComputeUnitsCPUAndGPU. |
| 38 | +lowered_module = to_backend("CoreMLBackend", edge.exported_program, [CompileSpec("compute_units", bytes("gpu", 'utf-8'))]) |
| 39 | +
|
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | +**MLComputeUnitsCPUAndNeuralEngine** |
| 44 | + |
| 45 | +``` |
| 46 | +# ane is equivalent to MLComputeUnitsCPUAndNeuralEngine. |
| 47 | +lowered_module = to_backend("CoreMLBackend", edge.exported_program, [CompileSpec("compute_units", bytes("ane", 'utf-8'))]) |
| 48 | +
|
| 49 | +``` |
| 50 | + |
| 51 | +**MLComputeUnitsAll** |
| 52 | + |
| 53 | +``` |
| 54 | +# all is equivalent to MLComputeUnitsAll. |
| 55 | +lowered_module = to_backend("CoreMLBackend", edge.exported_program, [CompileSpec("compute_units", bytes("all", 'utf-8'))]) |
| 56 | +
|
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | +## Runtime |
| 61 | + |
| 62 | +Directory Structure. |
| 63 | +``` |
| 64 | +
|
| 65 | +executorch |
| 66 | +├── backends |
| 67 | + ├── coreml # CoreML backend implementation. |
| 68 | + ├── runtime # CoreML runtime implementation. |
| 69 | + ├── inmemoryfs # In-Memory filesystem implementation. |
| 70 | + ├── kvstore # Persistent key-value store implementation. |
| 71 | + ├── delegate # Delegate implementation. |
| 72 | + ├── libraries # Linked libraries. |
| 73 | + ├── runner # Runner app implementation. |
| 74 | + ├── test # Test files and models. |
| 75 | +
|
| 76 | +``` |
| 77 | + |
| 78 | +## Integration |
| 79 | + |
| 80 | +There are two steps required to integrate the **CoreML** backend. |
| 81 | +- Exporting the Program: The client must call `to_backend` with the `CoreMLBackend` tag. |
| 82 | + |
| 83 | +``` |
| 84 | +from executorch.backends.coreml.compiler import CoreMLBackend |
| 85 | +
|
| 86 | +# This will delegate the whole program to the CoreML backend. |
| 87 | +lowered_module = to_backend("CoreMLBackend", edge.exported_program, []) |
| 88 | +
|
| 89 | +``` |
| 90 | +- Running the delegated program: The client application must link with the `coremldelegate` library. Please follow the instructions described in the `backends/coreml/setup.md` to link the `coremldelegate` library. Once linked, there are no additional steps required. **ExecuTorch** would automatically call the **CoreML** delegate to execute the **CoreML** delegated part of the exported **Program**. |
| 91 | + |
| 92 | + |
0 commit comments