Repository navigation
Expose CoreML EP via OrtEpFactory - #31975
Conversation
Register an internal CoreMLEpFactory with the plugin-EP infrastructure so that, on supported Apple systems with discovered accelerator hardware, the CoreML EP appears in GetEpDevices(), can be added to a session with SessionOptionsAppendExecutionProvider_V2, and is eligible for EP selection policies, notably PREFER_NPU, which can now select the Apple Neural Engine on supported systems instead of silently falling back to the CPU EP. Fixes microsoft#29066 The factory registers with vendor ID OrtDevice::VendorIds::APPLE, matching the vendor ID reported by Apple device discovery, so device ordering during EP selection prefers CoreML for Apple hardware via the vendor-affinity rule. In builds that also register the internal WebGPU EP, including the official macOS packages, PREFER_GPU and MAX_PERFORMANCE now select CoreML instead of WebGPU when both EPs claim the same Apple GPU, because CoreML has matching vendor affinity. The factory advertises at most one NPU and one GPU from device discovery. It advertises no devices below Core ML 5, the EP's minimum supported version. The NPU additionally requires Core ML 6 (iOS 16 / macOS 13), because earlier Core ML versions have no compute-units mode that enables the Neural Engine without also enabling the GPU. MLComputeUnitsCPUAndNeuralEngine requires Core ML 6, so an NPU selection could not otherwise be honored. The CPU device is deliberately left to the CPU EP. CPU-selecting policies choose a single preferred EP for the CPU device, so advertising it would displace the ORT CPU EP and other CPU-based EPs. CoreML-on-CPU remains available through an explicit MLComputeUnits=CPUOnly option, and Core ML can use its internal CPU path for operations unsupported by the selected compute units. When MLComputeUnits is not specified, the factory derives it from the selected devices: CPUAndNeuralEngine for NPU, CPUAndGPU for GPU, and ALL for NPU+GPU. User options may narrow this set, but may not enable an accelerator that was not selected. Tests in onnxruntime/test/autoep/test_selection.cc cover factory registration, explicit GPU and NPU+GPU selection, inference, PREFER_NPU/PREFER_GPU and their policy aliases, compute-unit defaulting and conflict validation, version checks, device validation and selection limits, CPU-only narrowing, and transactional cleanup after partial device-creation failures.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
@microsoft-github-policy-service agree company="Parallels" |
|
Hi Edward Chen (@edgchen1), would you be the right person to review this PR, or could you suggest an appropriate reviewer? It exposes the CoreML EP via OrtEpFactory and enables device-based EP selection. Thanks! |
The CoreML EP is implemented as part of ONNX Runtime, not provided by Apple, so its factory now reports "Microsoft" and VendorIds::MICROSOFT, like the other internal EP factories (CPU, DML, and WebGPU). Discovered Apple hardware devices keep their "Apple" / VendorIds::APPLE identifiers. VendorIds::APPLE remains the named constant for the vendor ID reported by Apple platform device discovery. Because the factory vendor no longer matches the Apple hardware vendor, CoreML is no longer preferred based on vendor matching during device ordering. When another EP claims the same hardware device, ordering falls back to the EP name tie-breaker. The CoreMLVendorAffinityPrecedesEpNameOrdering test was deleted because it tested the vendor affinity behavior removed by this change. The remaining vendor assertions now expect Microsoft identifiers for the factory and Apple identifiers for the hardware device.
Rename kCoremlComputeUnits_* to kCoremlProviderOption_MLComputeUnits_*.
Call the native binding through the _pybind_state module.
There was a problem hiding this comment.
Pull request overview
Registers CoreML with device-based EP selection on supported Apple systems.
Changes:
- Adds an internal CoreML EP factory for NPU/GPU discovery and selection.
- Derives and validates
MLComputeUnitsfrom selected devices. - Adds Apple vendor IDs and comprehensive AutoEP tests.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
onnxruntime/test/python/onnxruntime_test_python.py |
Tests Apple vendor IDs. |
onnxruntime/test/autoep/test_selection.cc |
Tests CoreML discovery, selection, validation, and cleanup. |
onnxruntime/python/onnxruntime_inference_collection.py |
Exposes Apple vendor ID to Python. |
onnxruntime/core/session/plugin_ep/ep_library_internal.h |
Declares CoreML factory creation. |
onnxruntime/core/session/plugin_ep/ep_library_internal.cc |
Registers the CoreML factory. |
onnxruntime/core/session/plugin_ep/ep_factory_coreml.h |
Defines the internal factory interface. |
onnxruntime/core/session/plugin_ep/ep_factory_coreml.cc |
Implements discovery, validation, and provider creation. |
onnxruntime/core/providers/coreml/model/host_utils.h |
Defines the NPU-selection version requirement. |
onnxruntime/core/providers/coreml/coreml_options.cc |
Uses shared compute-unit constants. |
onnxruntime/core/platform/apple/device_discovery.cc |
Uses the shared Apple vendor ID. |
include/onnxruntime/core/providers/coreml/coreml_provider_factory.h |
Publishes compute-unit option constants. |
include/onnxruntime/core/framework/ortdevice.h |
Adds the Apple vendor ID. |
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
After the CoreML factory switched to the Microsoft vendor ID, OrtDevice::VendorIds::APPLE was no longer used. Apple device discovery keeps its local kApplePciVendorId constant (0x106B) as before.
The CoreML EP factory is an internal EP: there is no library boundary between it and ORT, so its entry points do not need to convert exceptions into OrtStatus.
The device claiming and option validation helpers of CoreMLEpFactory were public static members only so that unit tests could call them directly. Move them into an anonymous namespace in ep_factory_coreml.cc, fold OrtEpDevice creation and publication into GetSupportedDevices, and remove the tests that called the helpers directly.
Move the CoreML EP selection tests from test_selection.cc to a dedicated CoreML test file.
There was a problem hiding this comment.
🟡 Changes recommended
Core ML version-boundary behavior lacks deterministic test coverage.
Get a fresh assessment by requesting another Copilot review.
Review details
- Files reviewed: 10/10 changed files
- Comments generated: 1
- Review effort level: Balanced
|
Edward Chen (@edgchen1), I'd like to discuss one more topic. It does not expand the scope of this PR. I tried to add a CPU device to the CoreML factory, but two existing tests then failed: 1. OrtEpLibrary.PluginEp_Custom_Op_Inference_With_Prefer_Cpu This test registers example_ep, which implements Custom_Mul, and uses PREFER_CPU. In this configuration, the CPU candidates were ordered as: CoreMLExecutionProvider -> example_ep -> CPUExecutionProvider The policy selected CoreML and the ORT CPU EP. Consequently, example_ep was not added to the session. CoreML could not execute Custom_Mul, and the ORT CPU EP could not execute it either. Session creation failed with “Could not find an implementation for Custom_Mul(1) node”. 2. ModelPackageTest.LoadModelPackageAndRunInference_PreferCpu This test creates a package containing CPU and NPU variants targeting example_ep, then uses PREFER_CPU. The policy again selected CoreML and the ORT CPU EP. GetVariantSelectionEpInfo captures only provider_list[0], and VariantSelector::SelectVariant considers only ep_infos[0]. Since the first EP was CoreML and the package had no CoreML variant, the test failed with “No suitable model variant found for the configured execution providers”. This is a package-variant selection failure and keeping more EPs in the policy result would not fix it on its own: the resolver must also consider later candidates. There is already a TODO for this in model_package_variant_selector.cc. ================================== As a possible solution, I would propose supporting a ranked chain of CPU EPs, which would give another CPU EP the opportunity to handle the nodes the first one does not support. For ordinary ONNX sessions, explicit registration already provides the relevant behavior: applications can append different EPs through separate AppendExecutionProvider_V2 calls, and graph partitioning considers them in registration order. Nodes not assigned to one EP remain available to later EPs, with the ORT CPU EP last. A policy could keep multiple eligible CPU EPs in ranked order instead of reducing them to one primary EP plus the ORT CPU EP. This would reuse existing graph-partitioning behavior. Model packages would need a corresponding extension: consider EPs in priority order and choose the first one with a compatible variant. I also measured CoreML CPU against the ORT CPU EP on an Apple M3 Pro using static, batch-1 models. These are latencies in milliseconds, each pair is two separate runs:
CoreML on the CPU is up to 4× faster than the ORT CPU EP here. I should also note that these numbers are for MLProgram, not the current NeuralNetwork default, which has known gaps in FP16 support. With NeuralNetwork and CPUOnly, both FP16 models ran entirely on the ORT CPU EP. What do you think? |
Updated [Microsoft.ML.OnnxRuntime](https://github.com/Microsoft/onnxruntime) from 1.30.0 to 1.31.0. <details> <summary>Release notes</summary> _Sourced from [Microsoft.ML.OnnxRuntime's releases](https://github.com/Microsoft/onnxruntime/releases)._ ## 1.31.0 ONNX Runtime 1.31.0 stabilizes model-package and EPContext data APIs, improves CPU model loading and quantized MoE inference, expands device-based execution-provider selection, and strengthens model-loading and runtime reliability. These notes cover changes since ONNX Runtime 1.30.0. CUDA and WebGPU kernel updates are summarized here; detailed provider notes are covered by their separate plugin EP releases. ## Highlights - Promoted the model-package API and EPContext data callbacks to stable C and C++ APIs, with EPContext callback support added across language bindings ([#33166](microsoft/onnxruntime#33166), [#32265](microsoft/onnxruntime#32265)). - Reduced CPU session-initialization overhead by parallelizing eligible weight prepacking, and accelerated block-wise INT4/INT8 QMoE experts with MLAS QNBit kernels and grouped expert dispatch ([#31691](microsoft/onnxruntime#31691), [#32644](microsoft/onnxruntime#32644), [#32668](microsoft/onnxruntime#32668)). - Added x86 FP16 LayerNorm/RMSNorm acceleration, Arm KleidiAI SVE2.1 FP16 GEMM support, and additional RISC-V vector kernels ([#32715](microsoft/onnxruntime#32715), [#32670](microsoft/onnxruntime#32670), [#32710](microsoft/onnxruntime#32710)). - Enabled CoreML participation in device-based EP selection, including Apple Neural Engine selection through `PREFER_NPU` ([#31975](microsoft/onnxruntime#31975)). - Added workspace-memory accounting and verification for constrained-memory graph partitioning, alongside broader model, graph, and tensor validation ([#31962](microsoft/onnxruntime#31962), [#32189](microsoft/onnxruntime#32189)). ## Announcements & Compatibility - [CUDA Plugin EP v0.3.0](https://github.com/microsoft/onnxruntime/releases/tag/plugin-ep-cuda/v0.3.0) and WebGPU Plugin EP v0.5.0 are matched with ONNX Runtime 1.31.0. For **contrib-op compatibility**, core and the plugin must use the same contributed-operator schemas; building both from the **same ONNX Runtime revision** is recommended. Schema mismatches can cause incorrect execution or crashes and may not be detected during plugin registration. - **`GatherBlockQuantized` behavior change:** out-of-range indices now produce zero output slices across CPU, CUDA, and WebGPU for both integer and floating-point quantized formats. This deliberately differs from ONNX `Gather` error semantics ([#32480](microsoft/onnxruntime#32480)). - **Experimental API migration:** model-package consumers should use `OrtApi::GetModelPackageApi` and the stable `OrtModelPackageApi` table. EPContext callback registration now uses stable APIs; the corresponding experimental lookup entries were removed. The stable native callback contract carries callback/state pointers, with payload limits enforced by application callbacks, bindings, or EPs rather than a native read-options policy ([#33166](microsoft/onnxruntime#33166), [#32265](microsoft/onnxruntime#32265)). - **ACL EP is deprecated** and will be removed in a future release. Source builds using `--use_acl` now receive a deprecation warning ([#32564](microsoft/onnxruntime#32564)). - The ONNX dependency remains at **1.22.0**. The initial ONNX 1.23 integration was reverted pending upstream fixes; this release does not ship that integration's new opset or FLOAT6 support ([#33073](microsoft/onnxruntime#33073)). - Supported native builds now default to **1DS telemetry**. Windows TraceLogging remains selectable with `--use_windows_telemetry` or `onnxruntime_USE_WINDOWS_TELEMETRY=ON`; telemetry can be disabled with `--no_telemetry` ([#32384](microsoft/onnxruntime#32384)). ## New Features ### Core APIs & Runtime - Stabilized the model-package C and C++ APIs for loading packages, selecting compatible model variants, and creating sessions ([#33166](microsoft/onnxruntime#33166)). - Stabilized named EPContext data read/write callbacks for application-managed storage. Added EP support negotiation so registered callbacks do not silently fall back to filesystem I/O ([#32265](microsoft/onnxruntime#32265)). - Extended the Compile API to write external initializers and EP context data to buffers, enabling in-memory compilation workflows for models larger than 2 GB ([#31347](microsoft/onnxruntime#31347)). - Added workspace-memory accounting, source reporting, and post-partition reservation verification. Set `session.strict_workspace_verification=1` to fail initialization on workspace declaration overruns or nonzero orphaned reservations; strict verification is not supported for ORT-format model loads ([#31962](microsoft/onnxruntime#31962), [#32189](microsoft/onnxruntime#32189)). - Added the opt-in source-build option `onnxruntime_DISABLE_DEVICE_DISCOVERY=ON` to skip platform accelerator probing and report only the CPU device ([#32592](microsoft/onnxruntime#32592)). ### Shared Contrib Operators These updates affect core operator schemas and cross-provider behavior, not only the separately released GPU plugins. - Extended `EngramGate` and `NGramHashMapping` for Qwen4-Exp, and added `VarlenNGramHashMapping` for packed DeepSeek Engram batching with request-local history. Packed hashing also supports Qwen4-Exp head offsets, EOS resets, and segment boundaries ([#32285](microsoft/onnxruntime#32285), [#32358](microsoft/onnxruntime#32358), [#32467](microsoft/onnxruntime#32467)). - Added the `activation` attribute to `GatedRMSNorm` for SiLU, its Swish alias, and Sigmoid gating across CPU, CUDA, and WebGPU. SiLU remains the default ([#32512](microsoft/onnxruntime#32512)). - Added weightless hyper-connection operators `BranchwiseRMSNorm`, `ScaledSiLU`, `HyperConnectionPreMix`, and `HyperConnectionPostMix`, with CPU, CUDA, and WebGPU implementations ([#32687](microsoft/onnxruntime#32687)). - Extended `GatherBlockQuantized` to FP8/FP4 data, full-axis blocks, and scale broadcasting, and improved CPU UINT8 dequantization. See the out-of-range-index behavior change above ([#32480](microsoft/onnxruntime#32480), [#32621](microsoft/onnxruntime#32621)). ### Plugin Execution Provider Infrastructure - Added support for statically linked plugin EPs, with a static WebGPU plugin build configuration ([#32395](microsoft/onnxruntime#32395)). - Preserved caller streams in single-tensor plugin data transfers and handled null plugin allocator returns ([#32666](microsoft/onnxruntime#32666), [#32683](microsoft/onnxruntime#32683)). - Deduplicated AutoEP custom-op domains and corrected EP assignment reporting after NHWC layout transformation ([#32711](microsoft/onnxruntime#32711), [#33184](microsoft/onnxruntime#33184)). - Added reference guidance for compiled-model compatibility validation, including multi-device scenarios ([#29168](microsoft/onnxruntime#29168)). ## Execution Provider Updates ### CUDA EP CUDA updates include sparse attention and packed continuous batching, expanded paged speculative decoding, INT2 and mixed-width QMoE execution, opt-in MatMul auto-tuning, improved MXFP4 support on Blackwell consumer GPUs, and reliability fixes. ... (truncated) Commits viewable in [compare view](microsoft/onnxruntime@v1.30.0...v1.31.0). </details> Updated [Swashbuckle.AspNetCore](https://github.com/domaindrivendev/Swashbuckle.AspNetCore) from 10.2.3 to 10.3.0. <details> <summary>Release notes</summary> _Sourced from [Swashbuckle.AspNetCore's releases](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases)._ ## 10.3.0 ## What's Changed * Change nullability handling for schemas with `anyOf`, `allOf`, `oneOf` or `type = null` by @ldeluigi in domaindrivendev/Swashbuckle.AspNetCore#3976 * Handle leading slash on route prefix during index.html GET by @superstator in domaindrivendev/Swashbuckle.AspNetCore#4084 * Move NRT handling to DataContractResolver by @desjoerd in domaindrivendev/Swashbuckle.AspNetCore#3998 * Clarify nullable behaviour by @martincostello in domaindrivendev/Swashbuckle.AspNetCore#4094 * Fix review issues for caching and encoding by @martincostello in domaindrivendev/Swashbuckle.AspNetCore#4093 * Support multiple response types per status code by @DeagleGross in domaindrivendev/Swashbuckle.AspNetCore#4100 * Add coverage for `AddServer` across multiple documents by @RaphaelFakhri in domaindrivendev/Swashbuckle.AspNetCore#4115 * Fix Swagger sort key fallback for empty endpoint tags metadata by @AleksaMCode in domaindrivendev/Swashbuckle.AspNetCore#4125 * Fix parameter matching to consider location when names collide by @nonsoikem in domaindrivendev/Swashbuckle.AspNetCore#4126 * Emit form encoding for properties of a complex form parameter by @RaphaelFakhri in domaindrivendev/Swashbuckle.AspNetCore#4128 * Keep a property's own summary when comments come from several XML files by @RaphaelFakhri in domaindrivendev/Swashbuckle.AspNetCore#4129 * Strip query string from relative document URLs in Swagger UI by @GabeSilvaDev in domaindrivendev/Swashbuckle.AspNetCore#4152 * Document that System.Text.Json ignores `[EnumMember]` by @RaphaelFakhri in domaindrivendev/Swashbuckle.AspNetCore#4127 * Drop the regex route constraint from the default Swagger route by @RaphaelFakhri in domaindrivendev/Swashbuckle.AspNetCore#4130 * Serialize custom values in the UI `ConfigObject.AdditionalItems` by @RaphaelFakhri in domaindrivendev/Swashbuckle.AspNetCore#4132 * Use Minimal APIs JSON options when MVC is not registered by @RaphaelFakhri in domaindrivendev/Swashbuckle.AspNetCore#4116 * Resolve enum contracts against the underlying type under source generation by @RaphaelFakhri in domaindrivendev/Swashbuckle.AspNetCore#4131 ## New Contributors * @ldeluigi made their first contribution in domaindrivendev/Swashbuckle.AspNetCore#3976 * @superstator made their first contribution in domaindrivendev/Swashbuckle.AspNetCore#4084 * @desjoerd made their first contribution in domaindrivendev/Swashbuckle.AspNetCore#3998 * @RaphaelFakhri made their first contribution in domaindrivendev/Swashbuckle.AspNetCore#4115 * @AleksaMCode made their first contribution in domaindrivendev/Swashbuckle.AspNetCore#4125 * @nonsoikem made their first contribution in domaindrivendev/Swashbuckle.AspNetCore#4126 * @GabeSilvaDev made their first contribution in domaindrivendev/Swashbuckle.AspNetCore#4152 * @JulianThoms made their first contribution in domaindrivendev/Swashbuckle.AspNetCore#4182 **Full Changelog**: domaindrivendev/Swashbuckle.AspNetCore@v10.2.3...v10.3.0 Commits viewable in [compare view](domaindrivendev/Swashbuckle.AspNetCore@v10.2.3...v10.3.0). </details> Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore <dependency name> major version` will close this group update PR and stop Dependabot creating any more for the specific dependency's major version (unless you unignore this specific dependency's major version or upgrade to it yourself) - `@dependabot ignore <dependency name> minor version` will close this group update PR and stop Dependabot creating any more for the specific dependency's minor version (unless you unignore this specific dependency's minor version or upgrade to it yourself) - `@dependabot ignore <dependency name>` will close this group update PR and stop Dependabot creating any more for the specific dependency (unless you unignore this specific dependency or upgrade to it yourself) - `@dependabot unignore <dependency name>` will remove all of the ignore conditions of the specified dependency - `@dependabot unignore <dependency name> <ignore condition>` will remove the ignore condition of the specified dependency and ignore conditions </details> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Description
This PR registers an internal CoreMLEpFactory with the plugin-EP infrastructure so that, on
supported Apple systems, the CoreML EP participates in device-based EP selection.
The CoreML EP now:
of silently falling back to the CPU EP.
The implementation follows the existing internal-factory pattern used by the CPU, DML, and
WebGPU EPs.
The factory reports "Microsoft" as its vendor name and OrtDevice::VendorIds::MICROSOFT as its
vendor ID, matching the other internal factories: the factory vendor identifies who provides the
EP implementation, not the hardware vendor. Discovered Apple hardware devices keep their existing
"Apple" vendor name and PCI vendor ID (pci_vendor_ids::kApple, 0x106B).
Fixes #29066.
The factory advertises at most one NPU and one GPU from Apple device discovery. Core ML selects
compute-unit classes rather than individual physical devices, so additional devices of the same
type would not be individually addressable and are intentionally ignored.
No devices are advertised below Core ML 5 (macOS 12 / iOS 15), the minimum version accepted by
the current CoreML EP.
The NPU additionally requires Core ML 6 (iOS 16 / macOS 13). Earlier versions do not provide
MLComputeUnitsCPUAndNeuralEngine, the only mode that enables the Neural Engine without also
enabling the GPU. Core ML 5 can still use the ANE through "ALL", but it cannot honor an NPU-only
device selection.
The CPU device is deliberately not advertised. CPU-selection policies select one primary CPU EP
and add the ORT CPU EP as a fallback when needed. If CoreML advertised the CPU, it would compete
with the ORT CPU EP and other CPU-based plugin EPs to become the primary CPU EP. Models that
depend on operators provided by an EP that was not selected could then fail. CoreML-on-CPU
remains available through MLComputeUnits=CPUOnly, and Core ML may use its internal CPU path for
operations unsupported by the selected compute units.
When the caller does not provide MLComputeUnits, the factory derives one value from the complete
device selection: CPUAndNeuralEngine for NPU, CPUAndGPU for GPU, and ALL for NPU + GPU. A
user-provided MLComputeUnits value may narrow which selected accelerators CoreML uses, or disable
them all with CPUOnly, but it may not enable an accelerator that was not selected.
The CoreML EP now implements IExecutionProvider::GetProviderOptions() so its effective options can be
checked. It calls CoreMLOptions::ToProviderOptions(), which reports these options using the public option
names defined in coreml_provider_factory.h.
PREFER_NPU, MAX_EFFICIENCY, and MIN_OVERALL_POWER can now select CoreML on systems where the
factory advertises the Neural Engine.
In builds that register both CoreML and WebGPU, including the official macOS packages, both EPs
advertise the same Apple GPU. Both factories report "Microsoft" as their vendor, which does not
match the Apple hardware vendor. Device ordering therefore falls back to the EP name tie-breaker,
where "CoreMLExecutionProvider" sorts before "WebGpuExecutionProvider". As a result, PREFER_GPU
and MAX_PERFORMANCE select CoreML instead of WebGPU. This ordering comes from the EP names rather
than an explicit preference for CoreML. CoreML uses MLComputeUnits=CPUAndGPU for that selection,
which excludes the Neural Engine. WebGPU remains available through explicit device selection.
Tests
This patch adds 12 CoreML AutoEP tests covering: