Skip to content

Commit

Permalink
add object-c api
Browse files Browse the repository at this point in the history
  • Loading branch information
wejoncy committed Nov 19, 2024
1 parent f710c14 commit ece5e58
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
12 changes: 11 additions & 1 deletion objectivec/include/ort_coreml_execution_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,17 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (BOOL)appendCoreMLExecutionProviderWithOptions:(ORTCoreMLExecutionProviderOptions*)options
error:(NSError**)error;

/**
* Enables the CoreML execution provider in the session configuration options.
* It is appended to the execution provider list which is ordered by
* decreasing priority.
*
* @param provider_options The CoreML execution provider options in dict.
* @param error Optional error information set if an error occurs.
* @return Whether the provider was enabled successfully.
*/
- (BOOL)appendCoreMLExecutionProviderWithOptions_v2:(NSDictionary*)provider_options

Check warning on line 82 in objectivec/include/ort_coreml_execution_provider.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Using C-style cast. Use reinterpret_cast<NSDictionary*>(...) instead [readability/casting] [4] Raw Output: objectivec/include/ort_coreml_execution_provider.h:82: Using C-style cast. Use reinterpret_cast<NSDictionary*>(...) instead [readability/casting] [4]
error:(NSError**)error;

Check warning on line 83 in objectivec/include/ort_coreml_execution_provider.h

View workflow job for this annotation

GitHub Actions / Optional Lint C++

[cpplint] reported by reviewdog 🐶 Using C-style cast. Use reinterpret_cast<NSError**>(...) instead [readability/casting] [4] Raw Output: objectivec/include/ort_coreml_execution_provider.h:83: Using C-style cast. Use reinterpret_cast<NSError**>(...) instead [readability/casting] [4]
@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions objectivec/ort_coreml_execution_provider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ - (BOOL)appendCoreMLExecutionProviderWithOptions:(ORTCoreMLExecutionProviderOpti
#endif
}

- (BOOL)appendCoreMLExecutionProviderWithOptions_v2:(NSDictionary*)provider_options
error:(NSError**)error {
#if ORT_OBJC_API_COREML_EP_AVAILABLE
try {
return [self appendExecutionProvider:@"CoreML" providerOptions:provider_options error:error];
}
ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error);

#else // !ORT_OBJC_API_COREML_EP_AVAILABLE
static_cast<void>(provider_options);
ORTSaveCodeAndDescriptionToError(ORT_FAIL, "CoreML execution provider is not enabled.", error);
return NO;
#endif
}

@end

NS_ASSUME_NONNULL_END
23 changes: 23 additions & 0 deletions objectivec/test/ort_session_test.mm
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,29 @@ - (void)testAppendCoreMLEP {
ORTAssertNullableResultSuccessful(session, err);
}


- (void)testAppendCoreMLEP_v2 {
NSError* err = nil;
ORTSessionOptions* sessionOptions = [ORTSessionTest makeSessionOptions];
NSDictionary* provider_options = @{@"MLEnableOnSubgraphs" : @"1"};// set an arbitrary option

BOOL appendResult = [sessionOptions appendCoreMLExecutionProviderWithOptions_v2:provider_options
error:&err];

if (!ORTIsCoreMLExecutionProviderAvailable()) {
ORTAssertBoolResultUnsuccessful(appendResult, err);
return;
}

ORTAssertBoolResultSuccessful(appendResult, err);

ORTSession* session = [[ORTSession alloc] initWithEnv:self.ortEnv
modelPath:[ORTSessionTest getAddModelPath]
sessionOptions:sessionOptions
error:&err];
ORTAssertNullableResultSuccessful(session, err);
}

- (void)testAppendXnnpackEP {
NSError* err = nil;
ORTSessionOptions* sessionOptions = [ORTSessionTest makeSessionOptions];
Expand Down
6 changes: 3 additions & 3 deletions onnxruntime/core/providers/coreml/coreml_provider_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ CoreMLOptions ParseProviderOption(const ProviderOptions& options) {
} else {
coreml_options.coreml_flags |= available_modelformat_options.at(option.second);
}
} else if (okCoremlProviderOption_MLAllowStaticInputShapes == option.first) {
} else if (kCoremlProviderOption_MLAllowStaticInputShapes == option.first) {
coreml_options.coreml_flags |= COREML_FLAG_ONLY_ALLOW_STATIC_INPUT_SHAPES;
} else if (okCoremlProviderOption_MLEnableOnSubgraphs == option.first) {
} else if (kCoremlProviderOption_MLEnableOnSubgraphs == option.first) {
coreml_options.coreml_flags |= COREML_FLAG_ENABLE_ON_SUBGRAPH;
} else if (okCoremlProviderOption_MLModelCacheDir == option.first) {
} else if (kCoremlProviderOption_MLModelCacheDir == option.first) {
coreml_options.cache_path = option.second;
}
}
Expand Down

0 comments on commit ece5e58

Please sign in to comment.