Skip to content

Handle value and tensor errors as NSErrors keeping the underlying error codes #9503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 21, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ - (nullable instancetype)initWithTensor:(torch::executor::Tensor)tensor error:(N
{
if (tensor.scalar_type() != ScalarType::Float) {
if (error) {
*error = [ModelRuntimeValueErrorFactory invalidType:[NSString stringWithFormat:@"torch::executor::ScalarType::%hhd", tensor.scalar_type()] expectedType:@"torch::executor::ScalarType::Float"];
*error = [NSError
errorWithDomain:@"ExecutorchRuntimeEngine"
code:(NSInteger)executorch::runtime::Error::InvalidArgument
userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: torch::executor::ScalarType::%hhd, expected torch::executor::ScalarType::Float", tensor.scalar_type()]}];
}
return nil;
}
Expand Down Expand Up @@ -90,9 +93,10 @@ - (nullable ModelRuntimeTensorValueBridgingTuple *)floatRepresentationAndReturnE
}

if (error) {
*error = [ModelRuntimeValueErrorFactory
invalidType:[NSString stringWithFormat:@"torch::executor::ScalarType::%hhd", _tensor->scalar_type()]
expectedType:@"torch::executor::ScalarType::Float"];
*error = [NSError
errorWithDomain:@"ExecutorchRuntimeEngine"
code:(NSInteger)executorch::runtime::Error::InvalidArgument
userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: torch::executor::ScalarType::%hhd, expected torch::executor::ScalarType::Float", _tensor->scalar_type()]}];
}

return nil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#import "ExecutorchRuntimeValue.h"

#import <map>
Expand Down Expand Up @@ -48,9 +48,10 @@ - (nullable ExecutorchRuntimeTensorValue *)asTensorValueAndReturnError:(NSError
}

if (error) {
*error = [ModelRuntimeValueErrorFactory
invalidType:[NSString stringWithFormat:@"Tag::%d", _value.tag]
expectedType:@"Tag::Tensor"];
*error = [NSError
errorWithDomain:@"ExecutorchRuntimeEngine"
code:static_cast<uint32_t>(executorch::runtime::Error::InvalidArgument)
userInfo: @{NSDebugDescriptionErrorKey: [NSString stringWithFormat:@"Invalid type: Tag::%d, expected Tag::Tensor", _value.tag]}];
}
return nil;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ - (void)testTensorValueWithFloatArrayWithError
NSError *error = nil;
XCTAssertNil([[ExecutorchRuntimeTensorValue alloc] initWithTensor:*new torch::executor::Tensor(&tensorImpl) error:&error]);
XCTAssertNotNil(error);
XCTAssertEqualObjects([error description], @"Invalid type: torch::executor::ScalarType::3, expected torch::executor::ScalarType::Float");
XCTAssertEqual(error.code, static_cast<uint32_t>(executorch::runtime::Error::InvalidArgument));
XCTAssertEqualObjects(error.userInfo[NSDebugDescriptionErrorKey], @"Invalid type: torch::executor::ScalarType::3, expected torch::executor::ScalarType::Float");
}

- (void)testTensorValueWithError
Expand All @@ -56,7 +57,8 @@ - (void)testTensorValueWithError
NSError *error = nil;
XCTAssertNil([value asTensorValueAndReturnError:&error]);
XCTAssertNotNil(error);
XCTAssertEqualObjects([error description], @"Invalid type: Tag::4, expected Tag::Tensor");
XCTAssertEqual(error.code, static_cast<uint32_t>(executorch::runtime::Error::InvalidArgument));
XCTAssertEqualObjects(error.userInfo[NSDebugDescriptionErrorKey], @"Invalid type: Tag::4, expected Tag::Tensor");
}

@end

This file was deleted.

Loading