From 484b17ee9e53663457879ae3dbe7ffc0dbea6431 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Tue, 29 Apr 2025 22:36:04 -0700 Subject: [PATCH 1/7] Add helpers to create errors in ObjC/Swift --- .../ExecuTorch/Exported/ExecuTorchError.h | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.h b/extension/apple/ExecuTorch/Exported/ExecuTorchError.h index cdf52051d05..1b225fdb33e 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.h +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.h @@ -12,4 +12,62 @@ NS_ASSUME_NONNULL_BEGIN FOUNDATION_EXPORT NSErrorDomain const ExecuTorchErrorDomain NS_SWIFT_NAME(ErrorDomain); +/** + * Enum to define the error codes. + * Values can be a subset, but must numerically match exactly those defined in + * runtime/core/error.h + */ +typedef NS_ERROR_ENUM(ExecuTorchErrorDomain, ExecuTorchErrorCode) { + // System errors. + ExecuTorchErrorCodeOk = 0, + ExecuTorchErrorCodeInternal = 1, + ExecuTorchErrorCodeInvalidState = 2, + ExecuTorchErrorCodeEndOfMethod = 3, + + // Logical errors. + ExecuTorchErrorCodeNotSupported = 16, + ExecuTorchErrorCodeNotImplemented = 17, + ExecuTorchErrorCodeInvalidArgument = 18, + ExecuTorchErrorCodeInvalidType = 19, + ExecuTorchErrorCodeOperatorMissing = 20, + + // Resource errors. + ExecuTorchErrorCodeNotFound = 32, + ExecuTorchErrorCodeMemoryAllocationFailed = 33, + ExecuTorchErrorCodeAccessFailed = 34, + ExecuTorchErrorCodeInvalidProgram = 35, + ExecuTorchErrorCodeInvalidExternalData = 36, + ExecuTorchErrorCodeOutOfResources = 37, + + // Delegate errors. + ExecuTorchErrorCodeDelegateInvalidCompatibility = 48, + ExecuTorchErrorCodeDelegateMemoryAllocationFailed = 49, + ExecuTorchErrorCodeDelegateInvalidHandle = 50, +} NS_SWIFT_NAME(ErrorCode); + +/** + * Returns a brief error description for the given error code. + * + * @param code An ExecuTorchErrorCode value representing the error code. + * @return An NSString containing the error description. + */ +FOUNDATION_EXPORT +__attribute__((deprecated("This API is experimental."))) +NSString *ExecuTorchErrorDescription(ExecuTorchErrorCode code) + NS_SWIFT_NAME(ErrorDescription(_:)); + +/** + * NSError convenience initializer for ExecuTorch errors. + * + * @param code The ExecuTorchErrorCode to wrap into an NSError. + * @return An NSError with ExecuTorchErrorDomain, the specified code, and a localized description. + */ + @interface NSError (ExecuTorch) + + + (instancetype)errorWithExecuTorchCode:(ExecuTorchErrorCode)code + __attribute__((deprecated("This API is experimental."))) + NS_SWIFT_NAME(init(code:)); + +@end + NS_ASSUME_NONNULL_END From 1a4d97233baa4cd3bf509f144bbe2b31d6eb37f6 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Tue, 29 Apr 2025 22:37:14 -0700 Subject: [PATCH 2/7] Update ExecuTorchError.m --- .../ExecuTorch/Exported/ExecuTorchError.m | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m index 43996dc213e..53afa4188a1 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m @@ -9,3 +9,58 @@ #import "ExecuTorchError.h" NSErrorDomain const ExecuTorchErrorDomain = @"org.pytorch.executorch.error"; + +NSString *ExecuTorchErrorDescription(ExecuTorchErrorCode code) { + switch (code) { + case ExecuTorchErrorCodeOk: + return @""; + case ExecuTorchErrorCodeInternal: + return @"Internal error"; + case ExecuTorchErrorCodeInvalidState: + return @"Invalid executor state"; + case ExecuTorchErrorCodeEndOfMethod: + return @"No more execution steps"; + case ExecuTorchErrorCodeNotSupported: + return @"Operation not supported"; + case ExecuTorchErrorCodeNotImplemented: + return @"Operation not implemented"; + case ExecuTorchErrorCodeInvalidArgument: + return @"Invalid argument"; + case ExecuTorchErrorCodeInvalidType: + return @"Invalid type"; + case ExecuTorchErrorCodeOperatorMissing: + return @"Operator missing"; + case ExecuTorchErrorCodeNotFound: + return @"Resource not found"; + case ExecuTorchErrorCodeMemoryAllocationFailed: + return @"Memory allocation failed"; + case ExecuTorchErrorCodeAccessFailed: + return @"Access failed"; + case ExecuTorchErrorCodeInvalidProgram: + return @"Invalid program contents"; + case ExecuTorchErrorCodeInvalidExternalData: + return @"Invalid external data"; + case ExecuTorchErrorCodeOutOfResources: + return @"Out of resources"; + case ExecuTorchErrorCodeDelegateInvalidCompatibility: + return @"Delegate version incompatible"; + case ExecuTorchErrorCodeDelegateMemoryAllocationFailed: + return @"Delegate memory allocation failed"; + case ExecuTorchErrorCodeDelegateInvalidHandle: + return @"Delegate handle invalid"; + default: + return [NSString stringWithFormat:@"Unknown error (%ld)", (long)code]; + } +} + +@implementation NSError (ExecuTorch) + ++ (instancetype)errorWithExecuTorchCode:(ExecuTorchErrorCode)code { + return [NSError errorWithDomain:ExecuTorchErrorDomain + code:code + userInfo:@{ + NSLocalizedDescriptionKey : ExecuTorchErrorDescription(code) + }]; +} + +@end From f762ea21e30526328702cbba720ad84eca5a011c Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Tue, 29 Apr 2025 22:43:31 -0700 Subject: [PATCH 3/7] Update ExecuTorchError.m --- extension/apple/ExecuTorch/Exported/ExecuTorchError.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m index 53afa4188a1..6dc70c6fbf9 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m @@ -49,7 +49,7 @@ case ExecuTorchErrorCodeDelegateInvalidHandle: return @"Delegate handle invalid"; default: - return [NSString stringWithFormat:@"Unknown error (%ld)", (long)code]; + return [NSString stringWithFormat:@"Unknown error %zd", code]; } } From edbc7b624ae347392c80b3cdb5b34e214925c0be Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Wed, 30 Apr 2025 00:07:10 -0700 Subject: [PATCH 4/7] Update ExecuTorchError.h --- .../apple/ExecuTorch/Exported/ExecuTorchError.h | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.h b/extension/apple/ExecuTorch/Exported/ExecuTorchError.h index 1b225fdb33e..f144cc70bb5 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.h +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.h @@ -57,17 +57,12 @@ NSString *ExecuTorchErrorDescription(ExecuTorchErrorCode code) NS_SWIFT_NAME(ErrorDescription(_:)); /** - * NSError convenience initializer for ExecuTorch errors. + * Create an NSError in the ExecuTorch domain for the given code. * - * @param code The ExecuTorchErrorCode to wrap into an NSError. + * @param code The ExecuTorchErrorCode to wrap. * @return An NSError with ExecuTorchErrorDomain, the specified code, and a localized description. */ - @interface NSError (ExecuTorch) - - + (instancetype)errorWithExecuTorchCode:(ExecuTorchErrorCode)code - __attribute__((deprecated("This API is experimental."))) - NS_SWIFT_NAME(init(code:)); - -@end +FOUNDATION_EXPORT NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) + NS_SWIFT_NAME(Error(code:)); NS_ASSUME_NONNULL_END From f03698107223b6f063181c1090181d146154e300 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Wed, 30 Apr 2025 00:07:29 -0700 Subject: [PATCH 5/7] Update ExecuTorchError.m --- extension/apple/ExecuTorch/Exported/ExecuTorchError.m | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m index 6dc70c6fbf9..2ecef863a2d 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m @@ -49,18 +49,14 @@ case ExecuTorchErrorCodeDelegateInvalidHandle: return @"Delegate handle invalid"; default: - return [NSString stringWithFormat:@"Unknown error %zd", code]; + return @"Unknown error"; } } -@implementation NSError (ExecuTorch) - -+ (instancetype)errorWithExecuTorchCode:(ExecuTorchErrorCode)code { +NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) { return [NSError errorWithDomain:ExecuTorchErrorDomain code:code userInfo:@{ NSLocalizedDescriptionKey : ExecuTorchErrorDescription(code) }]; } - -@end From 871356ce260158b61473971e9721c25f7939f07f Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Wed, 30 Apr 2025 00:09:30 -0700 Subject: [PATCH 6/7] Update ExecuTorchModule.mm --- .../ExecuTorch/Exported/ExecuTorchModule.mm | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm b/extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm index 98f0f555991..0c649382603 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm @@ -87,9 +87,7 @@ - (BOOL)loadWithVerification:(ExecuTorchVerification)verification const auto errorCode = _module->load(static_cast(verification)); if (errorCode != Error::Ok) { if (error) { - *error = [NSError errorWithDomain:ExecuTorchErrorDomain - code:(NSInteger)errorCode - userInfo:nil]; + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode); } return NO; } @@ -110,9 +108,7 @@ - (BOOL)loadMethod:(NSString *)methodName const auto errorCode = _module->load_method(methodName.UTF8String); if (errorCode != Error::Ok) { if (error) { - *error = [NSError errorWithDomain:ExecuTorchErrorDomain - code:(NSInteger)errorCode - userInfo:nil]; + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode); } return NO; } @@ -127,9 +123,7 @@ - (BOOL)isMethodLoaded:(NSString *)methodName { const auto result = _module->method_names(); if (!result.ok()) { if (error) { - *error = [NSError errorWithDomain:ExecuTorchErrorDomain - code:(NSInteger)result.error() - userInfo:nil]; + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error()); } return nil; } @@ -151,9 +145,7 @@ - (BOOL)isMethodLoaded:(NSString *)methodName { const auto result = _module->execute(methodName.UTF8String, inputs); if (!result.ok()) { if (error) { - *error = [NSError errorWithDomain:ExecuTorchErrorDomain - code:(NSInteger)result.error() - userInfo:nil]; + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error()); } return nil; } From 2b3faca469afdd64fc240742e80dbc529d4931e5 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Wed, 30 Apr 2025 00:09:49 -0700 Subject: [PATCH 7/7] Update ExecuTorchTensor.mm --- .../apple/ExecuTorch/Exported/ExecuTorchTensor.mm | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm b/extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm index 547fc6f1950..acb88e2a8cc 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm @@ -112,14 +112,10 @@ - (void)mutableBytesWithHandler:(void (^)(void *pointer, NSInteger count, ExecuT - (BOOL)resizeToShape:(NSArray *)shape error:(NSError **)error { - const auto resizeError = resize_tensor_ptr( - _tensor, utils::toVector(shape) - ); - if (resizeError != Error::Ok) { + const auto errorCode = resize_tensor_ptr(_tensor, utils::toVector(shape)); + if (errorCode != Error::Ok) { if (error) { - *error = [NSError errorWithDomain:ExecuTorchErrorDomain - code:(NSInteger)resizeError - userInfo:nil]; + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode); } return NO; }