ExecuTorch的Core ML后端配置与部署要点
ExecuTorch通过Core ML委托在苹果设备上运行模型,可动态调度到CPU、GPU和神经引擎,支持fp32与fp16计算。导出时需传入CoreMLPartitioner,并可通过minimum_deployment_target控制兼容范围。
## 核心 ML 后端[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#core-ml-backend>) 核心ML代表是利用苹果的ExecuTorch解决方案. [核心ML框架](<https://developer.apple.com/documentation/coreml>) 用于安装的 ML 。 使用 Core ML , 一个模型可以在 CPU , GPU , 以及 Apple 神经引擎( ANE ) 上运行 。 ## 特征[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#features>) - 动态发送至CPU,GPU,以及ANE. - 支持fp32和fp16计算. ## 目标所需资源[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#target-requirements>) 以下是运行核心ML授权的ExecuTorch型号的各类硬件的最低OS要求: - [macOS](<https://developer.apple.com/macos>) >= 13.0 - [iOS 系统](<https://developer.apple.com/ios/>) >= 16.0 - [iPadOS](<https://developer.apple.com/ipados/>) >= 16.0 - [tvOS](<https://developer.apple.com/tvos/>) >= 16.0 ## 发展要求[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#development-requirements>) 开发需要: - [macOS](<https://developer.apple.com/macos>) >= 13.0 - [Xcode 代码](<https://developer.apple.com/documentation/xcode>) >= 14.1 在开始前, 请确保您安装 Xcode 命令行工具 : ``` xcode-select --install ``` --- ## 使用核心 ML 后端[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#using-the-core-ml-backend>) 要在导出和降低过程中瞄准核心ML后端,请通过一个实例 `CoreMLPartitioner` 改为: `to_edge_transform_and_lower`下面的例子说明了使用火炬视觉中的MobileNet V2模型的这个过程. ``` import torch import torchvision.models as models from torchvision.models.mobilenetv2 import MobileNet_V2_Weights from executorch.backends.apple.coreml.partition import CoreMLPartitioner from executorch.exir import to_edge_transform_and_lower mobilenet_v2 = models.mobilenetv2.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).eval() sample_inputs = (torch.randn(1, 3, 224, 224), ) et_program = to_edge_transform_and_lower( torch.export.export(mobilenet_v2, sample_inputs), partitioner=[CoreMLPartitioner()], ).to_executorch() with open("mv2_coreml.pte", "wb") as file: et_program.write_to_file(file) ``` 见 [分区器 API](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-partitioner.html>) ,以获取可用分区器选项的引用。 --- ## 量化[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#quantization>) Core ML 代表器也可以用作执行量化模型的后端. See [核心 ML 量化](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-quantization.html>) 关于现有量化计划和API的更多信息。 ## 后向兼容性[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#backward-compatibility>) 核心 ML 支持通过 [`minimum_deployment_target`](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-partitioner.html#coreml-compilespec>) 选项。保证导出带有特定部署目标的模型能够对所有部署目标工作。例如,导出一个模型的模型 `coremltools.target.iOS17` 将工作在 iOS 17 或更高。 --- ## 运行时间集成[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#runtime-integration>) 要运行设备上的模型,请使用标准的 ExecuTorch 运行时间 API. See [在设备上运行](<https://docs.pytorch.org/executorch/stable/getting-started.html#running-on-device>) 以获取更多信息,包括构建iOS框架。 从源头建时,通过 `-DEXECUTORCH_BUILD_COREML=ON` 当配置 CMake 构建以编译 Core ML 后端时. 由于使用静态初始剂进行注册,因此可能有必要使用全管连接,以对抗静态初始剂。 `coremldelegate` 目标。这通常可以通过通过 `"$<LINK_LIBRARY:WHOLE_ARCHIVE,coremldelegate>"` 改为: `target_link_libraries`. ``` # CMakeLists.txt add_subdirectory("executorch") ... target_link_libraries( my_target PRIVATE executorch extension_module_static extension_tensor optimized_native_cpu_ops_lib $<LINK_LIBRARY:WHOLE_ARHIVE,coremldelegate>) ``` 不需要额外的步骤来使用连接目标以外的后端. Core ML 授权的.pte 文件将在注册后端上自动运行. ## 参考[#](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-overview.html#reference>) **→[解决问题](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-troubleshooting.html>) ——调试常见问题.** **→[分区器 API](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-partitioner.html>) - 分区选择。** **→[量化](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-quantization.html>) 支持的量化计划。** **→[操作支持](<https://docs.pytorch.org/executorch/stable/backends/coreml/coreml-op-support.html>) ——支持操作员.**
查看原始来源