Skip to content

Commit 65202af

Browse files
committed
Update
[ghstack-poisoned]
2 parents 4421272 + 9a18669 commit 65202af

File tree

14 files changed

+152
-38
lines changed

14 files changed

+152
-38
lines changed

.ci/scripts/test_model.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ test_model_with_coreml() {
222222

223223
DTYPE=float16
224224

225-
"${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision "${DTYPE}"
225+
"${PYTHON_EXECUTABLE}" -m examples.apple.coreml.scripts.export --model_name="${MODEL_NAME}" --compute_precision "${DTYPE}" --use_partitioner
226226
EXPORTED_MODEL=$(find "." -type f -name "${MODEL_NAME}*.pte" -print -quit)
227227

228228
if [ -n "$EXPORTED_MODEL" ]; then

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ let package = Package(
7777
name: "\(key)_dependencies",
7878
dependencies: [.target(name: key)],
7979
path: ".Package.swift/\(key)",
80-
linkerSettings:
80+
linkerSettings: [
81+
.linkedLibrary("c++")
82+
] +
8183
(value["frameworks"] as? [String] ?? []).map { .linkedFramework($0) } +
8284
(value["libraries"] as? [String] ?? []).map { .linkedLibrary($0) }
8385
),

backends/apple/coreml/runtime/delegate/coreml_backend_delegate.mm

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,17 @@
8888
ET_LOG(Error, "%s: DataType=%d is not supported", ETCoreMLStrings.delegateIdentifier.UTF8String, (int)tensor.scalar_type());
8989
return std::nullopt;
9090
}
91-
91+
9292
std::vector<ssize_t> strides(tensor.strides().begin(), tensor.strides().end());
9393
std::vector<size_t> shape(tensor.sizes().begin(), tensor.sizes().end());
94+
95+
// If tensor is rank 0, wrap in rank 1
96+
// See https://github.com/apple/coremltools/blob/8.2/coremltools/converters/mil/frontend/torch/exir_utils.py#L73
97+
if (shape.size() == 0) {
98+
shape.push_back(1);
99+
strides.push_back(1);
100+
}
101+
94102
MultiArray::MemoryLayout layout(dataType.value(), std::move(shape), std::move(strides));
95103
switch (argType) {
96104
case ArgType::Input: {
@@ -233,6 +241,12 @@ ModelLoggingOptions get_logging_options(BackendExecutionContext& context) {
233241
std::array<SizesType, kTensorDimensionLimit> new_shape;
234242
for (size_t i = nInputs; i < nInputs + nOutputs; i++) {
235243
Tensor& t = args[i]->toTensor();
244+
// If t has rank 0, do not resize. delegate_args[i] will have rank 1
245+
// because we resized it in get_multi_array
246+
if (t.dim() == 0) {
247+
continue;
248+
}
249+
236250
int rank = delegate_args[i].layout().rank();
237251
assert (rank <= new_shape.size());
238252
for (int d = 0; d < rank; d++) {

backends/apple/coreml/runtime/test/ETCoreMLModelManagerTests.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ - (void)testAddModelExecution {
113113
XCTAssertNotNil(inputs);
114114
MLMultiArray *output = [ETCoreMLTestUtils filledMultiArrayWithShape:inputs[0].shape dataType:inputs[0].dataType repeatedValue:@(0) error:&localError];
115115
NSArray<MLMultiArray *> *args = [inputs arrayByAddingObject:output];
116-
XCTAssertTrue([self.modelManager executeModelWithHandle:handle
116+
XCTAssertTrue([self.modelManager executeModelWithHandle:handle
117117
args:args
118118
loggingOptions:executorchcoreml::ModelLoggingOptions()
119119
eventLogger:nullptr

backends/apple/coreml/scripts/install_requirements.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SCRIPT_DIR_PATH="$(
1212

1313
# TODO(jathu): remove the need to fetch coremltools to build deps for coreml_executor_runner.
1414
# Keep this version in sync with: pyproject.toml
15-
COREMLTOOLS_VERSION="8.2"
15+
COREMLTOOLS_VERSION="8.3"
1616

1717
red=`tput setaf 1`
1818
green=`tput setaf 2`

backends/arm/operators/op_upsample_bilinear2d.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ def define_node(
3434
inputs: List[TosaArg],
3535
output: TosaArg,
3636
) -> None:
37-
assert (
38-
inputs[0].shape is not None and output.shape is not None
39-
), "Only static shapes are supported"
37+
if inputs[0].shape is None or output.shape is None:
38+
raise ValueError("Only static shapes are supported")
4039

4140
input_dtype = inputs[0].dtype
4241

@@ -55,9 +54,12 @@ def define_node(
5554
def in_int16_range(x):
5655
return torch.all(x >= -(2**15)) and torch.all(x <= 2**15 - 1)
5756

58-
assert in_int16_range(scale_n_yx)
59-
assert in_int16_range(scale_d_yx)
60-
assert in_int16_range(border_yx)
57+
if not in_int16_range(scale_n_yx):
58+
raise ValueError("scale_n_yx is out of the int16 range")
59+
if not in_int16_range(scale_d_yx):
60+
raise ValueError("scale_d_yx is out of the int16 range")
61+
if not in_int16_range(border_yx):
62+
raise ValueError("border_yx is out of the int16 range")
6163

6264
attr = ts.TosaSerializerAttribute()
6365
attr.ResizeAttribute(

backends/arm/operators/op_upsample_nearest2d.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ def define_node(
3636
) -> None:
3737
import tosa_tools.v0_80.serializer.tosa_serializer as ts # type: ignore
3838

39-
assert (
40-
inputs[0].shape is not None and output.shape is not None
41-
), "Only static shapes are supported"
39+
if inputs[0].shape is None or output.shape is None:
40+
raise ValueError("Only static shapes are supported")
4241

4342
# tosa_shape output is NHWC, take HW
4443
input_size_yx = torch.tensor(
@@ -55,9 +54,12 @@ def define_node(
5554
def in_int16_range(x):
5655
return torch.all(x >= -(2**15)) and torch.all(x <= 2**15 - 1)
5756

58-
assert in_int16_range(scale_n_yx)
59-
assert in_int16_range(scale_d_yx)
60-
assert in_int16_range(border_yx)
57+
if not in_int16_range(scale_n_yx):
58+
raise ValueError("scale_n_yx is out of the int16 range")
59+
if not in_int16_range(scale_d_yx):
60+
raise ValueError("scale_d_yx is out of the int16 range")
61+
if not in_int16_range(border_yx):
62+
raise ValueError("border_yx is out of the int16 range")
6163

6264
attr = ts.TosaSerializerAttribute()
6365
attr.ResizeAttribute(

docs/source/using-executorch-ios.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The prebuilt ExecuTorch runtime, backend, and kernels are available as a [Swift
2525

2626
#### Xcode
2727

28-
In Xcode, go to `File > Add Package Dependencies`. Paste the URL of the [ExecuTorch repo](https://github.com/pytorch/executorch) into the search bar and select it. Make sure to change the branch name to the desired ExecuTorch version in format "swiftpm-<version>", (e.g. "swiftpm-0.6.0"), or a branch name in format "swiftpm-<version>.<year_month_date>" (e.g. "swiftpm-0.7.0-20250401") for a nightly build on a specific date.
28+
In Xcode, go to `File > Add Package Dependencies`. Paste the URL of the [ExecuTorch repo](https://github.com/pytorch/executorch) into the search bar and select it. Make sure to change the branch name to the desired ExecuTorch version in format "swiftpm-<version>", (e.g. "swiftpm-0.6.0"), or a branch name in format "swiftpm-<version>.<year_month_date>" (e.g. "swiftpm-0.7.0-20250401") for a [nightly build](https://ossci-ios.s3.amazonaws.com/list.html) on a specific date.
2929

3030
![](_static/img/swiftpm_xcode1.png)
3131

extension/apple/ExecuTorch/Exported/ExecuTorchError.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,57 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
FOUNDATION_EXPORT NSErrorDomain const ExecuTorchErrorDomain NS_SWIFT_NAME(ErrorDomain);
1414

15+
/**
16+
* Enum to define the error codes.
17+
* Values can be a subset, but must numerically match exactly those defined in
18+
* runtime/core/error.h
19+
*/
20+
typedef NS_ERROR_ENUM(ExecuTorchErrorDomain, ExecuTorchErrorCode) {
21+
// System errors.
22+
ExecuTorchErrorCodeOk = 0,
23+
ExecuTorchErrorCodeInternal = 1,
24+
ExecuTorchErrorCodeInvalidState = 2,
25+
ExecuTorchErrorCodeEndOfMethod = 3,
26+
27+
// Logical errors.
28+
ExecuTorchErrorCodeNotSupported = 16,
29+
ExecuTorchErrorCodeNotImplemented = 17,
30+
ExecuTorchErrorCodeInvalidArgument = 18,
31+
ExecuTorchErrorCodeInvalidType = 19,
32+
ExecuTorchErrorCodeOperatorMissing = 20,
33+
34+
// Resource errors.
35+
ExecuTorchErrorCodeNotFound = 32,
36+
ExecuTorchErrorCodeMemoryAllocationFailed = 33,
37+
ExecuTorchErrorCodeAccessFailed = 34,
38+
ExecuTorchErrorCodeInvalidProgram = 35,
39+
ExecuTorchErrorCodeInvalidExternalData = 36,
40+
ExecuTorchErrorCodeOutOfResources = 37,
41+
42+
// Delegate errors.
43+
ExecuTorchErrorCodeDelegateInvalidCompatibility = 48,
44+
ExecuTorchErrorCodeDelegateMemoryAllocationFailed = 49,
45+
ExecuTorchErrorCodeDelegateInvalidHandle = 50,
46+
} NS_SWIFT_NAME(ErrorCode);
47+
48+
/**
49+
* Returns a brief error description for the given error code.
50+
*
51+
* @param code An ExecuTorchErrorCode value representing the error code.
52+
* @return An NSString containing the error description.
53+
*/
54+
FOUNDATION_EXPORT
55+
__attribute__((deprecated("This API is experimental.")))
56+
NSString *ExecuTorchErrorDescription(ExecuTorchErrorCode code)
57+
NS_SWIFT_NAME(ErrorDescription(_:));
58+
59+
/**
60+
* Create an NSError in the ExecuTorch domain for the given code.
61+
*
62+
* @param code The ExecuTorchErrorCode to wrap.
63+
* @return An NSError with ExecuTorchErrorDomain, the specified code, and a localized description.
64+
*/
65+
FOUNDATION_EXPORT NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code)
66+
NS_SWIFT_NAME(Error(code:));
67+
1568
NS_ASSUME_NONNULL_END

extension/apple/ExecuTorch/Exported/ExecuTorchError.m

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,54 @@
99
#import "ExecuTorchError.h"
1010

1111
NSErrorDomain const ExecuTorchErrorDomain = @"org.pytorch.executorch.error";
12+
13+
NSString *ExecuTorchErrorDescription(ExecuTorchErrorCode code) {
14+
switch (code) {
15+
case ExecuTorchErrorCodeOk:
16+
return @"";
17+
case ExecuTorchErrorCodeInternal:
18+
return @"Internal error";
19+
case ExecuTorchErrorCodeInvalidState:
20+
return @"Invalid executor state";
21+
case ExecuTorchErrorCodeEndOfMethod:
22+
return @"No more execution steps";
23+
case ExecuTorchErrorCodeNotSupported:
24+
return @"Operation not supported";
25+
case ExecuTorchErrorCodeNotImplemented:
26+
return @"Operation not implemented";
27+
case ExecuTorchErrorCodeInvalidArgument:
28+
return @"Invalid argument";
29+
case ExecuTorchErrorCodeInvalidType:
30+
return @"Invalid type";
31+
case ExecuTorchErrorCodeOperatorMissing:
32+
return @"Operator missing";
33+
case ExecuTorchErrorCodeNotFound:
34+
return @"Resource not found";
35+
case ExecuTorchErrorCodeMemoryAllocationFailed:
36+
return @"Memory allocation failed";
37+
case ExecuTorchErrorCodeAccessFailed:
38+
return @"Access failed";
39+
case ExecuTorchErrorCodeInvalidProgram:
40+
return @"Invalid program contents";
41+
case ExecuTorchErrorCodeInvalidExternalData:
42+
return @"Invalid external data";
43+
case ExecuTorchErrorCodeOutOfResources:
44+
return @"Out of resources";
45+
case ExecuTorchErrorCodeDelegateInvalidCompatibility:
46+
return @"Delegate version incompatible";
47+
case ExecuTorchErrorCodeDelegateMemoryAllocationFailed:
48+
return @"Delegate memory allocation failed";
49+
case ExecuTorchErrorCodeDelegateInvalidHandle:
50+
return @"Delegate handle invalid";
51+
default:
52+
return @"Unknown error";
53+
}
54+
}
55+
56+
NSError *ExecuTorchErrorWithCode(ExecuTorchErrorCode code) {
57+
return [NSError errorWithDomain:ExecuTorchErrorDomain
58+
code:code
59+
userInfo:@{
60+
NSLocalizedDescriptionKey : ExecuTorchErrorDescription(code)
61+
}];
62+
}

extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ - (BOOL)loadWithVerification:(ExecuTorchVerification)verification
8787
const auto errorCode = _module->load(static_cast<Program::Verification>(verification));
8888
if (errorCode != Error::Ok) {
8989
if (error) {
90-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
91-
code:(NSInteger)errorCode
92-
userInfo:nil];
90+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode);
9391
}
9492
return NO;
9593
}
@@ -110,9 +108,7 @@ - (BOOL)loadMethod:(NSString *)methodName
110108
const auto errorCode = _module->load_method(methodName.UTF8String);
111109
if (errorCode != Error::Ok) {
112110
if (error) {
113-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
114-
code:(NSInteger)errorCode
115-
userInfo:nil];
111+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode);
116112
}
117113
return NO;
118114
}
@@ -127,9 +123,7 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
127123
const auto result = _module->method_names();
128124
if (!result.ok()) {
129125
if (error) {
130-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
131-
code:(NSInteger)result.error()
132-
userInfo:nil];
126+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error());
133127
}
134128
return nil;
135129
}
@@ -151,9 +145,7 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
151145
const auto result = _module->execute(methodName.UTF8String, inputs);
152146
if (!result.ok()) {
153147
if (error) {
154-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
155-
code:(NSInteger)result.error()
156-
userInfo:nil];
148+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error());
157149
}
158150
return nil;
159151
}

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,10 @@ - (void)mutableBytesWithHandler:(void (^)(void *pointer, NSInteger count, ExecuT
112112

113113
- (BOOL)resizeToShape:(NSArray<NSNumber *> *)shape
114114
error:(NSError **)error {
115-
const auto resizeError = resize_tensor_ptr(
116-
_tensor, utils::toVector<SizesType>(shape)
117-
);
118-
if (resizeError != Error::Ok) {
115+
const auto errorCode = resize_tensor_ptr(_tensor, utils::toVector<SizesType>(shape));
116+
if (errorCode != Error::Ok) {
119117
if (error) {
120-
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
121-
code:(NSInteger)resizeError
122-
userInfo:nil];
118+
*error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)errorCode);
123119
}
124120
return NO;
125121
}

extension/benchmark/apple/Benchmark/Benchmark.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@
577577
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
578578
MACOSX_DEPLOYMENT_TARGET = 11.0;
579579
MARKETING_VERSION = 1.0;
580+
OTHER_CODE_SIGN_FLAGS = "--deep";
580581
PRODUCT_BUNDLE_IDENTIFIER = org.pytorch.executorch.Benchmark;
581582
PRODUCT_NAME = Benchmark;
582583
REGISTER_APP_GROUPS = NO;
@@ -611,6 +612,7 @@
611612
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
612613
MACOSX_DEPLOYMENT_TARGET = 11.0;
613614
MARKETING_VERSION = 1.0;
615+
OTHER_CODE_SIGN_FLAGS = "--deep";
614616
PRODUCT_BUNDLE_IDENTIFIER = org.pytorch.executorch.Benchmark;
615617
PRODUCT_NAME = Benchmark;
616618
REGISTER_APP_GROUPS = NO;

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ dependencies=[
6868
"tabulate",
6969
"typing-extensions",
7070
# Keep this version in sync with: ./backends/apple/coreml/scripts/install_requirements.sh
71-
"coremltools==8.2; platform_system == 'Darwin'",
71+
"coremltools==8.3; platform_system == 'Darwin'",
7272
]
7373

7474
[project.urls]

0 commit comments

Comments
 (0)