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
0 commit comments