diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.h b/extension/apple/ExecuTorch/Exported/ExecuTorchError.h index cdf52051d05..f144cc70bb5 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.h +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.h @@ -12,4 +12,57 @@ 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(_:)); + +/** + * Create an NSError in the ExecuTorch domain for the given code. + * + * @param code The ExecuTorchErrorCode to wrap. + * @return An NSError with ExecuTorchErrorDomain, the specified code, and a localized description. + */ +FOUNDATION_EXPORT NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) + NS_SWIFT_NAME(Error(code:)); + NS_ASSUME_NONNULL_END diff --git a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m index 43996dc213e..2ecef863a2d 100644 --- a/extension/apple/ExecuTorch/Exported/ExecuTorchError.m +++ b/extension/apple/ExecuTorch/Exported/ExecuTorchError.m @@ -9,3 +9,54 @@ #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 @"Unknown error"; + } +} + +NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) { + return [NSError errorWithDomain:ExecuTorchErrorDomain + code:code + userInfo:@{ + NSLocalizedDescriptionKey : ExecuTorchErrorDescription(code) + }]; +} 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; } 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; }