Skip to content

Commit 20a45dd

Browse files
wejoncyedgchen1
andauthored
[CoreML ML Program] support acclerators selector (#22383)
### Description For no, CoreML only support run mlmodels on CPU/ALL, However, sometimes CPU_GPU would be faster a lot. We support the option to select different hardware to boost performance in this PR. ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> --------- Co-authored-by: Edward Chen <[email protected]>
1 parent 8c21680 commit 20a45dd

File tree

12 files changed

+46
-8
lines changed

12 files changed

+46
-8
lines changed

csharp/src/Microsoft.ML.OnnxRuntime/ProviderOptions.shared.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ public enum CoreMLFlags : uint
330330
COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE = 0x004,
331331
COREML_FLAG_ONLY_ALLOW_STATIC_INPUT_SHAPES = 0x008,
332332
COREML_FLAG_CREATE_MLPROGRAM = 0x010,
333-
COREML_FLAG_LAST = COREML_FLAG_CREATE_MLPROGRAM,
333+
COREML_FLAG_USE_CPU_AND_GPU = 0x020,
334+
COREML_FLAG_LAST = COREML_FLAG_USE_CPU_AND_GPU,
334335
}
335336

336337
/// <summary>

include/onnxruntime/core/providers/coreml/coreml_provider_factory.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ enum COREMLFlags {
3131
// Create an MLProgram. By default it will create a NeuralNetwork model. Requires Core ML 5 or later.
3232
COREML_FLAG_CREATE_MLPROGRAM = 0x010,
3333

34+
// Exclude ANE as sometimes this decrease performance
35+
// https://developer.apple.com/documentation/coreml/mlcomputeunits?language=objc
36+
// there are four compute units:
37+
// MLComputeUnitsCPUAndNeuralEngine|MLComputeUnitsCPUAndGPU|MLComputeUnitsCPUOnly|MLComputeUnitsAll
38+
COREML_FLAG_USE_CPU_AND_GPU = 0x020,
3439
// Keep COREML_FLAG_LAST at the end of the enum definition
3540
// And assign the last COREMLFlag to it
36-
COREML_FLAG_LAST = COREML_FLAG_CREATE_MLPROGRAM,
41+
COREML_FLAG_LAST = COREML_FLAG_USE_CPU_AND_GPU,
3742
};
3843

3944
#ifdef __cplusplus

java/src/main/java/ai/onnxruntime/providers/CoreMLFlags.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ public enum CoreMLFlags implements OrtFlags {
2525
* Create an MLProgram. By default it will create a NeuralNetwork model. Requires Core ML 5 or
2626
* later.
2727
*/
28-
CREATE_MLPROGRAM(16); // COREML_FLAG_CREATE_MLPROGRAM(0x010)
28+
CREATE_MLPROGRAM(16), // COREML_FLAG_CREATE_MLPROGRAM(0x010)
29+
/** exclude ANE */
30+
CPU_AND_GPU(32); // COREML_FLAG_USE_CPU_AND_GPU(0x020)
2931

3032
/** The native value of the enum. */
3133
public final int value;

js/common/lib/inference-session.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ export declare namespace InferenceSession {
320320
* COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE = 0x004
321321
* COREML_FLAG_ONLY_ALLOW_STATIC_INPUT_SHAPES = 0x008
322322
* COREML_FLAG_CREATE_MLPROGRAM = 0x010
323+
* COREML_FLAG_USE_CPU_AND_GPU = 0x020
323324
* ```
324325
*
325326
* See include/onnxruntime/core/providers/coreml/coreml_provider_factory.h for more details.
@@ -333,6 +334,7 @@ export declare namespace InferenceSession {
333334
* This setting is available only in ONNXRuntime (react-native).
334335
*/
335336
useCPUOnly?: boolean;
337+
useCPUAndGPU?: boolean;
336338
/**
337339
* Specify whether to enable CoreML EP on subgraph.
338340
*

js/react_native/ios/OnnxruntimeModule.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ - (NSDictionary*)run:(NSString*)url
389389
if (useOptions) {
390390
if ([[executionProvider objectForKey:@"useCPUOnly"] boolValue]) {
391391
coreml_flags |= COREML_FLAG_USE_CPU_ONLY;
392+
} else if ([[executionProvider objectForKey:@"useCPUAndGPU"] boolValue]) {
393+
coreml_flags |= COREML_FLAG_USE_CPU_AND_GPU;
392394
}
393395
if ([[executionProvider objectForKey:@"enableOnSubgraph"] boolValue]) {
394396
coreml_flags |= COREML_FLAG_ENABLE_ON_SUBGRAPH;

objectivec/include/ort_coreml_execution_provider.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ NS_ASSUME_NONNULL_BEGIN
2929
* Whether the CoreML execution provider should run on CPU only.
3030
*/
3131
@property BOOL useCPUOnly;
32-
32+
/**
33+
* exclude ANE in CoreML.
34+
*/
35+
@property BOOL useCPUAndGPU;
3336
/**
3437
* Whether the CoreML execution provider is enabled on subgraphs.
3538
*/

objectivec/ort_coreml_execution_provider.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ - (BOOL)appendCoreMLExecutionProviderWithOptions:(ORTCoreMLExecutionProviderOpti
2525
try {
2626
const uint32_t flags =
2727
(options.useCPUOnly ? COREML_FLAG_USE_CPU_ONLY : 0) |
28+
(options.useCPUAndGPU ? COREML_FLAG_USE_CPU_AND_GPU : 0) |
2829
(options.enableOnSubgraphs ? COREML_FLAG_ENABLE_ON_SUBGRAPH : 0) |
2930
(options.onlyEnableForDevicesWithANE ? COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE : 0) |
3031
(options.onlyAllowStaticInputShapes ? COREML_FLAG_ONLY_ALLOW_STATIC_INPUT_SHAPES : 0) |

onnxruntime/core/providers/coreml/coreml_execution_provider.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ CoreMLExecutionProvider::CoreMLExecutionProvider(uint32_t coreml_flags)
3232
LOGS_DEFAULT(ERROR) << "CoreML EP is not supported on this platform.";
3333
}
3434

35+
// check if only one flag is set
36+
if ((coreml_flags & COREML_FLAG_USE_CPU_ONLY) && (coreml_flags & COREML_FLAG_USE_CPU_AND_GPU)) {
37+
// multiple device options selected
38+
ORT_THROW(
39+
"Multiple device options selected, you should use at most one of the following options:"
40+
"COREML_FLAG_USE_CPU_ONLY or COREML_FLAG_USE_CPU_AND_GPU or not set");
41+
}
42+
3543
#if defined(COREML_ENABLE_MLPROGRAM)
3644
if (coreml_version_ < MINIMUM_COREML_MLPROGRAM_VERSION &&
3745
(coreml_flags_ & COREML_FLAG_CREATE_MLPROGRAM) != 0) {

onnxruntime/core/providers/coreml/model/model.mm

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,15 @@ Status Predict(const std::unordered_map<std::string, OnnxTensorData>& inputs,
395395
compiled_model_path_ = [compileUrl path];
396396

397397
MLModelConfiguration* config = [[MLModelConfiguration alloc] init];
398-
config.computeUnits = (coreml_flags_ & COREML_FLAG_USE_CPU_ONLY)
399-
? MLComputeUnitsCPUOnly
400-
: MLComputeUnitsAll;
398+
399+
if (coreml_flags_ & COREML_FLAG_USE_CPU_ONLY) {
400+
config.computeUnits = MLComputeUnitsCPUOnly;
401+
} else if (coreml_flags_ & COREML_FLAG_USE_CPU_AND_GPU) {
402+
config.computeUnits = MLComputeUnitsCPUAndGPU;
403+
} else {
404+
config.computeUnits = MLComputeUnitsAll;
405+
}
406+
401407
model_ = [MLModel modelWithContentsOfURL:compileUrl configuration:config error:&error];
402408

403409
if (error != nil || model_ == nil) {

onnxruntime/python/onnxruntime_pybind_state.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,8 @@ std::unique_ptr<IExecutionProvider> CreateExecutionProviderInstance(
12131213

12141214
if (flags_str.find("COREML_FLAG_USE_CPU_ONLY") != std::string::npos) {
12151215
coreml_flags |= COREMLFlags::COREML_FLAG_USE_CPU_ONLY;
1216+
} else if (flags_str.find("COREML_FLAG_USE_CPU_AND_GPU") != std::string::npos) {
1217+
coreml_flags |= COREMLFlags::COREML_FLAG_USE_CPU_AND_GPU;
12161218
}
12171219

12181220
if (flags_str.find("COREML_FLAG_ONLY_ALLOW_STATIC_INPUT_SHAPES") != std::string::npos) {

0 commit comments

Comments
 (0)