Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchError.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
51 changes: 51 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchError.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}];
}
16 changes: 4 additions & 12 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ - (BOOL)loadWithVerification:(ExecuTorchVerification)verification
const auto errorCode = _module->load(static_cast<Program::Verification>(verification));
if (errorCode != Error::Ok) {
if (error) {
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
code:(NSInteger)errorCode
userInfo:nil];
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode);
}
return NO;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
10 changes: 3 additions & 7 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,10 @@ - (void)mutableBytesWithHandler:(void (^)(void *pointer, NSInteger count, ExecuT

- (BOOL)resizeToShape:(NSArray<NSNumber *> *)shape
error:(NSError **)error {
const auto resizeError = resize_tensor_ptr(
_tensor, utils::toVector<SizesType>(shape)
);
if (resizeError != Error::Ok) {
const auto errorCode = resize_tensor_ptr(_tensor, utils::toVector<SizesType>(shape));
if (errorCode != Error::Ok) {
if (error) {
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
code:(NSInteger)resizeError
userInfo:nil];
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode);
}
return NO;
}
Expand Down
Loading