[xnnpack-ep] NEW EP API in objc (#13941)

### Description
<!-- Describe your changes. -->



### 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 <18449977+edgchen1@users.noreply.github.com>
This commit is contained in:
JiCheng 2022-12-15 20:12:02 +08:00 committed by GitHub
parent a9b1fb032b
commit f4cd35f9b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 124 additions and 0 deletions

View file

@ -5,6 +5,7 @@
// the headers below can also be imported individually
#import "ort_coreml_execution_provider.h"
#import "ort_xnnpack_execution_provider.h"
#import "ort_enums.h"
#import "ort_env.h"
#import "ort_session.h"

View file

@ -105,6 +105,18 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (nullable instancetype)initWithError:(NSError**)error NS_SWIFT_NAME(init());
/**
* Available since 1.14.
* Appends an execution provider to the session configuration options.
* The execution provider list is ordered by decreasing priority
* @param providerName Provider name. For example, "xnnpack".
* @param providerOptions Provider-specific options. For example, for provider "xnnpack", {"intra_op_num_threads": "2"}.
* @param error Optional error information set if an error occurs.
* @return Whether the execution provider was appended successfully
*/
- (BOOL)appendExecutionProvider:(NSString*)providerName
providerOptions:(NSDictionary<NSString*, NSString*>*)providerOptions
error:(NSError**)error;
/**
* Sets the number of threads used to parallelize the execution within nodes.
* A value of 0 means ORT will pick a default value.

View file

@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#import <Foundation/Foundation.h>
#import "ort_session.h"
NS_ASSUME_NONNULL_BEGIN
/**
* Options for configuring the Xnnpack execution provider.
*/
@interface ORTXnnpackExecutionProviderOptions : NSObject
/**
* How many threads used for the Xnnpack execution provider.
*/
@property int intra_op_num_threads;
@end
@interface ORTSessionOptions (ORTSessionOptionsXnnpackEP)
/**
* Available since 1.14.
* Enables the Xnnpack execution provider in the session configuration options.
* It is appended to the execution provider list which is ordered by
* decreasing priority.
*
* @param options The Xnnpack execution provider configuration options.
* @param error Optional error information set if an error occurs.
* @return Whether the provider was enabled successfully.
*/
- (BOOL)appendXnnpackExecutionProviderWithOptions:(ORTXnnpackExecutionProviderOptions*)options
error:(NSError**)error;
@end
NS_ASSUME_NONNULL_END

View file

@ -213,6 +213,24 @@ NS_ASSUME_NONNULL_BEGIN
ORT_OBJC_API_IMPL_CATCH_RETURNING_NULLABLE(error)
}
- (BOOL)appendExecutionProvider:(NSString*)providerName
providerOptions:(NSDictionary<NSString*, NSString*>*)providerOptions
error:(NSError**)error {
try {
std::unordered_map<std::string, std::string> options;
NSArray* keys = [providerOptions allKeys];
for (NSString* key in keys) {
NSString* value = [providerOptions objectForKey:key];
options.emplace(key.UTF8String, value.UTF8String);
}
_sessionOptions->AppendExecutionProvider(providerName.UTF8String, options);
return YES;
}
ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error);
}
- (BOOL)setIntraOpNumThreads:(int)intraOpNumThreads
error:(NSError**)error {
try {

View file

@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#import "ort_xnnpack_execution_provider.h"
#import "src/cxx_api.h"
#import "src/error_utils.h"
#import "src/ort_session_internal.h"
NS_ASSUME_NONNULL_BEGIN
@implementation ORTXnnpackExecutionProviderOptions
@end
@implementation ORTSessionOptions (ORTSessionOptionsXnnpackEP)
- (BOOL)appendXnnpackExecutionProviderWithOptions:(ORTXnnpackExecutionProviderOptions*)options
error:(NSError**)error {
try {
NSDictionary* provider_options = @{
@"intra_op_num_threads" : [NSString stringWithFormat:@"%d", options.intra_op_num_threads]
};
return [self appendExecutionProvider:@"XNNPACK" providerOptions:provider_options error:error];
}
ORT_OBJC_API_IMPL_CATCH_RETURNING_BOOL(error);
}
@end
NS_ASSUME_NONNULL_END

View file

@ -4,6 +4,7 @@
#import <XCTest/XCTest.h>
#import "ort_coreml_execution_provider.h"
#import "ort_xnnpack_execution_provider.h"
#import "ort_env.h"
#import "ort_session.h"
#import "ort_value.h"
@ -215,6 +216,28 @@ NS_ASSUME_NONNULL_BEGIN
ORTAssertNullableResultSuccessful(session, err);
}
- (void)testAppendXnnpackEP {
NSError* err = nil;
ORTSessionOptions* sessionOptions = [ORTSessionTest makeSessionOptions];
ORTXnnpackExecutionProviderOptions* XnnpackOptions = [[ORTXnnpackExecutionProviderOptions alloc] init];
XnnpackOptions.intra_op_num_threads = 2;
BOOL appendResult = [sessionOptions appendXnnpackExecutionProviderWithOptions:XnnpackOptions
error:&err];
// Without xnnpack EP in building also can pass the test
NSString* err_msg = [err localizedDescription];
if (!appendResult && [err_msg containsString:@"XNNPACK execution provider is not supported in this build. "]) {
return;
}
ORTAssertBoolResultSuccessful(appendResult, err);
ORTSession* session = [[ORTSession alloc] initWithEnv:self.ortEnv
modelPath:[ORTSessionTest getAddModelPath]
sessionOptions:sessionOptions
error:&err];
ORTAssertNullableResultSuccessful(session, err);
}
@end
NS_ASSUME_NONNULL_END