13
13
14
14
#import < executorch/extension/module/module.h>
15
15
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
-
42
16
@implementation ExecutorchRuntimeEngine
43
17
{
44
18
NSString *_modelPath;
@@ -48,66 +22,47 @@ @implementation ExecutorchRuntimeEngine
48
22
49
23
- (instancetype )initWithModelPath : (NSString *)modelPath
50
24
modelMethodName : (NSString *)modelMethodName
51
- error : (NSError * _Nullable * _Nullable )error
25
+ error : (NSError ** )error
52
26
{
53
27
if (self = [super init ]) {
54
28
_modelPath = modelPath;
55
29
_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) {
68
33
if (error) {
69
34
*error = [NSError errorWithDomain: @" ExecutorchRuntimeEngine"
70
- code: kInitFailed
71
- userInfo: @{ NSDebugDescriptionErrorKey : @" Unknown error " } ];
35
+ code: ( NSInteger )e
36
+ userInfo: nil ];
72
37
}
73
38
return nil ;
74
39
}
75
40
}
76
41
return self;
77
42
}
78
43
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
81
46
{
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 ()) {
104
54
if (error) {
105
55
*error = [NSError errorWithDomain: @" ExecutorchRuntimeEngine"
106
- code: kInferenceFailed
107
- userInfo: @{ NSDebugDescriptionErrorKey : @" Unknown error " } ];
56
+ code: ( NSInteger )result. error ()
57
+ userInfo: nil ];
108
58
}
109
59
return nil ;
110
60
}
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;
111
66
}
112
67
113
68
@end
0 commit comments