Skip to content

Commit 2c3fd71

Browse files
authored
Expose ET errors as is
Differential Revision: D71585874 Pull Request resolved: #9497
1 parent 0c1c362 commit 2c3fd71

File tree

3 files changed

+24
-70
lines changed

3 files changed

+24
-70
lines changed

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
2121
modelMethodName:(NSString *)modelMethodName
2222
error:(NSError * _Nullable * _Nullable)error NS_DESIGNATED_INITIALIZER;
2323

24-
- (nullable NSArray<ExecutorchRuntimeValue *> *)infer:(NSArray<ExecutorchRuntimeValue *> *)input
24+
- (nullable NSArray<ExecutorchRuntimeValue *> *)infer:(NSArray<ExecutorchRuntimeValue *> *)values
2525
error:(NSError * _Nullable * _Nullable)error NS_SWIFT_NAME(infer(input:));
2626

2727
@end

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/Exported/ExecutorchRuntimeEngine.mm

Lines changed: 22 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,6 @@
1313

1414
#import <executorch/extension/module/module.h>
1515

16-
static int kInitFailed = 0;
17-
static int kInferenceFailed = 1;
18-
19-
static auto NSStringToString(NSString *string) -> std::string
20-
{
21-
const char *cStr = [string cStringUsingEncoding:NSUTF8StringEncoding];
22-
if (cStr) {
23-
return cStr;
24-
}
25-
26-
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
27-
return {reinterpret_cast<const char *>([data bytes]), [data length]};
28-
}
29-
30-
static auto StringToNSString(const std::string &string) -> NSString *
31-
{
32-
CFStringRef cfString = CFStringCreateWithBytes(
33-
kCFAllocatorDefault,
34-
reinterpret_cast<const UInt8 *>(string.c_str()),
35-
string.size(),
36-
kCFStringEncodingUTF8,
37-
false
38-
);
39-
return (__bridge_transfer NSString *)cfString;
40-
}
41-
4216
@implementation ExecutorchRuntimeEngine
4317
{
4418
NSString *_modelPath;
@@ -48,66 +22,47 @@ @implementation ExecutorchRuntimeEngine
4822

4923
- (instancetype)initWithModelPath:(NSString *)modelPath
5024
modelMethodName:(NSString *)modelMethodName
51-
error:(NSError * _Nullable * _Nullable)error
25+
error:(NSError **)error
5226
{
5327
if (self = [super init]) {
5428
_modelPath = modelPath;
5529
_modelMethodName = modelMethodName;
56-
try {
57-
_module = std::make_unique<torch::executor::Module>(NSStringToString(modelPath));
58-
const auto e = _module->load_method(NSStringToString(modelMethodName));
59-
if (e != executorch::runtime::Error::Ok) {
60-
if (error) {
61-
*error = [NSError errorWithDomain:@"ExecutorchRuntimeEngine"
62-
code:kInitFailed
63-
userInfo:@{NSDebugDescriptionErrorKey : StringToNSString(std::to_string(static_cast<uint32_t>(e)))}];
64-
}
65-
return nil;
66-
}
67-
} catch (...) {
30+
_module = std::make_unique<torch::executor::Module>(modelPath.UTF8String);
31+
const auto e = _module->load_method(modelMethodName.UTF8String);
32+
if (e != executorch::runtime::Error::Ok) {
6833
if (error) {
6934
*error = [NSError errorWithDomain:@"ExecutorchRuntimeEngine"
70-
code:kInitFailed
71-
userInfo:@{NSDebugDescriptionErrorKey : @"Unknown error"}];
35+
code:(NSInteger)e
36+
userInfo:nil];
7237
}
7338
return nil;
7439
}
7540
}
7641
return self;
7742
}
7843

79-
- (nullable NSArray<ExecutorchRuntimeValue *> *)infer:(NSArray<ExecutorchRuntimeValue *> *)input
80-
error:(NSError * _Nullable * _Nullable)error
44+
- (nullable NSArray<ExecutorchRuntimeValue *> *)infer:(NSArray<ExecutorchRuntimeValue *> *)values
45+
error:(NSError **)error
8146
{
82-
try {
83-
std::vector<torch::executor::EValue> inputEValues;
84-
inputEValues.reserve(input.count);
85-
for (ExecutorchRuntimeValue *inputValue in input) {
86-
inputEValues.push_back([inputValue getBackedValue]);
87-
}
88-
const auto result = _module->execute(NSStringToString(_modelMethodName), inputEValues);
89-
if (!result.ok()) {
90-
const auto executorchError = static_cast<uint32_t>(result.error());
91-
if (error) {
92-
*error = [NSError errorWithDomain:@"ExecutorchRuntimeEngine"
93-
code:kInferenceFailed
94-
userInfo:@{NSDebugDescriptionErrorKey : StringToNSString(std::to_string(executorchError))}];
95-
}
96-
return nil;
97-
}
98-
NSMutableArray<ExecutorchRuntimeValue *> *const resultValues = [NSMutableArray new];
99-
for (const auto &evalue : result.get()) {
100-
[resultValues addObject:[[ExecutorchRuntimeValue alloc] initWithEValue:evalue]];
101-
}
102-
return resultValues;
103-
} catch (...) {
47+
std::vector<torch::executor::EValue> inputEValues;
48+
inputEValues.reserve(values.count);
49+
for (ExecutorchRuntimeValue *inputValue in values) {
50+
inputEValues.push_back([inputValue getBackedValue]);
51+
}
52+
const auto result = _module->execute(_modelMethodName.UTF8String, inputEValues);
53+
if (!result.ok()) {
10454
if (error) {
10555
*error = [NSError errorWithDomain:@"ExecutorchRuntimeEngine"
106-
code:kInferenceFailed
107-
userInfo:@{NSDebugDescriptionErrorKey : @"Unknown error"}];
56+
code:(NSInteger)result.error()
57+
userInfo:nil];
10858
}
10959
return nil;
11060
}
61+
NSMutableArray<ExecutorchRuntimeValue *> *const resultValues = [NSMutableArray new];
62+
for (const auto &evalue : result.get()) {
63+
[resultValues addObject:[[ExecutorchRuntimeValue alloc] initWithEValue:evalue]];
64+
}
65+
return resultValues;
11166
}
11267

11368
@end

extension/apple/ExecutorchRuntimeBridge/ExecutorchRuntimeBridge/__tests__/ExecutorchRuntimeEngineTests.mm

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ - (void)testInvalidModel
2626
XCTAssertNil(engine);
2727
XCTAssertNotNil(runtimeInitError);
2828

29-
XCTAssertEqual(runtimeInitError.code, 0);
30-
XCTAssertEqualObjects(runtimeInitError.userInfo[NSDebugDescriptionErrorKey], @"34");
29+
XCTAssertEqual(runtimeInitError.code, 34);
3130
// 34 is the code for AccessFailed.
3231
}
3332

0 commit comments

Comments
 (0)