-
Notifications
You must be signed in to change notification settings - Fork 539
[RFC] Objective-C/Swift APIs for ExecuTorch #8360
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
Comments
@shoumikhin - Thanks for creating this issue. Regarding error code propagation:
|
cc @swolchok - what are your thoughts on @shoumikhin's design for ObjC/Swift bindings above? |
@mergennachin ObjC/Swift use |
this in particular is fixable with std::variant now that we have C++17
I haven't paid much attention to Swift in the past 5 to 7 years. With that disclaimer out of the way, I'm mildly surprised anybody still wants Objective-C. |
@swolchok for us the main issue is bridging C++ into Swift properly. About a year ago Swift introduced some interoperability with C++, but it's still pretty limited to be able to automatically convert our user-facing C++ API into Swift (e.g. |
Yeah, this is a good point! cc @dbort - if you have comments regarding our API footprint in both core and extensions. |
This will definitely help a segment of iOS developers, so I think it's worth doing. Question the wrappers be in ExecuTorch or in a separate repo? I could go either way but I love what PyTorch Lightning did having a separate abstractions library. |
This should just be part of |
This seems like a good step. And since it doesn't seem to require modifying the core runtime, there's room to experiment here. Consider marking these new APIs as EXPERIMENTAL to begin with if you think they might change. |
I agree this would be a good step. |
Yes, it is a swift convention. ObjC error parameters are translated as throwing to Swift. https://developer.apple.com/documentation/swift/handling-cocoa-errors-in-swift#Catch-Errors |
@bsoyluoglu @shoumikhin do you have updates on this? It'd be great to have this for 0.6 release, which the branch cut for code is on March 18 |
@mergennachin I am actively working on it, and I having it for that date isn't looking risky right now. If anything changes I will keep you posted |
@bsoyluoglu Great thank you. Just for completeness, as part of this work, it would be good to also update the documentation with the new bindings too in our iOS page. https://pytorch.org/executorch/main/using-executorch-ios.html#runtime-api |
First skeleton PR: #8826 |
@shoumikhin okay to close or keep it for any missing features? |
@mergennachin not everything is implemented there, so we could keep those RFC open for now |
🚀 The feature, motivation and pitch
Problem
Currently, ExecuTorch provides a C++ API for running exported models on Apple platforms. While functional, this presents a significant hurdle for developers working primarily in Objective-C and Swift. The documentation suggests bridging the C++ APIs manually, which is complex, error-prone, and introduces a significant maintenance burden. It also requires deep knowledge of both C++ and the intricacies of ExecuTorch's internals. This negatively impacts developer productivity and increases the barrier to entry for using ExecuTorch within the Apple ecosystem. The only currently supported API in Objective-C/Swift are the logging functions.
Motivation
We aim to make ExecuTorch easily accessible to the vast majority of iOS, macOS, and other Apple platform developers who work primarily in Objective-C and Swift. A native API will significantly improve the developer experience, making integration simpler, safer, and more efficient.
Pitch
We propose creating a set of native Objective-C wrappers around the core ExecuTorch C++ APIs. These wrappers will be designed for seamless bridging into Swift, providing a natural and idiomatic experience for Swift developers. This will drastically simplify the process of loading and executing ExecuTorch models within Apple platform apps. The user experience will be similar to the current one, but offering the native API: users will still integrate ExecuTorch using Swift Package Manager, import the
ExecuTorch
module, but will now have access to classes likeModule
,Tensor
, andValue
directly in Objective-C and Swift.Example
Objective-C:
Swift:
Alternatives
Additional context
ExecuTorchModule
(bridged asModule
in Swift): For loading and executing models.ExecuTorchValue
(bridged asValue
in Swift): For representing inputs and outputs (including tensors, scalars, and lists).ExecuTorchTensor
(bridged asTensor
in Swift): For handling tensor data.We'll use ExecuTorch prefix for Objective-C classes to avoid naming collisions and follow the naming scheme we've already adopted for the logging API.
RFC (Optional)
Proposed Design
Wrapper Implementation:
We will create Objective‑C wrapper classes that internally own (or reference) the native C++ objects. For example, the
ExecuTorchModule
wrapper will internally hold astd::unique_ptr<executorch::extension::Module>
. Similar patterns will be applied for Value (wrapping the C++EValue
variant) and Tensor (wrapping aTensorPtr
).Data Conversion:
Conversion between Objective‑C types (e.g.
NSArray
,NSNumber
,NSString
) and their C++ counterparts will be designed to be lightweight, using direct pointer references or minimal copying where possible.Swift Bridging:
Using
NS_SWIFT_NAME
annotations will allow the Objective‑C APIs to be automatically imported into Swift with natural naming (e.g. the method- (BOOL)load:(NSError **)error
will become a Swift throwing method). This guarantees a native feel on both languages.Future Extensions:
While the initial implementation will cover the main execution APIs (loading models, executing methods, managing inputs/outputs via Value and Tensor), the design will be flexible enough to later include support for more specialized features (such as detailed event tracing or method metadata).
This RFC lays out the overall scope and rationale for the bindings. More detailed design and API refinements (for each of the
Module
,Value
, andTensor
interfaces) will be proposed in follow‑up issues.Expected files layout:

cc @cbilgin @mergennachin @byjlw
The text was updated successfully, but these errors were encountered: