-
Notifications
You must be signed in to change notification settings - Fork 543
Native Objective‑C/Swift Wrapper for the ExecuTorch Tensor #8366
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
Labels
module: ios
Issues related to iOS code, build, and execution
triaged
This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Comments
This was referenced Mar 26, 2025
facebook-github-bot
pushed a commit
that referenced
this issue
Mar 27, 2025
Summary: #8366 Reviewed By: mergennachin Differential Revision: D71908831
facebook-github-bot
pushed a commit
that referenced
this issue
Mar 27, 2025
Summary: #8366 Reviewed By: mergennachin Differential Revision: D71909752
facebook-github-bot
pushed a commit
that referenced
this issue
Mar 27, 2025
Summary: #8366 Reviewed By: mergennachin Differential Revision: D71910685
kirklandsign
pushed a commit
that referenced
this issue
Mar 27, 2025
Summary: #8366 Differential Revision: D71910685
kirklandsign
added a commit
that referenced
this issue
Mar 27, 2025
Summary: #8366 Reviewed By: mergennachin Differential Revision: D71910685 Co-authored-by: Anthony Shoumikhin <[email protected]>
facebook-github-bot
pushed a commit
that referenced
this issue
Mar 27, 2025
Summary: #8366 Reviewed By: mergennachin Differential Revision: D71929159
kirklandsign
added a commit
that referenced
this issue
Mar 27, 2025
Summary: #8366 Reviewed By: mergennachin Differential Revision: D71929159 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: mergennachin Differential Revision: D71910685 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: mergennachin Differential Revision: D71929159 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: bsoyluoglu Differential Revision: D71929194 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: bsoyluoglu Differential Revision: D71929714 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: bsoyluoglu Differential Revision: D71930240 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: bsoyluoglu Differential Revision: D71930535 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: bsoyluoglu Differential Revision: D71930917 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: bsoyluoglu Differential Revision: D71931436 Co-authored-by: Anthony Shoumikhin <[email protected]>
kirklandsign
added a commit
that referenced
this issue
Apr 11, 2025
Summary: #8366 Reviewed By: bsoyluoglu Differential Revision: D71932495 Co-authored-by: Anthony Shoumikhin <[email protected]>
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 1, 2025
Summary: pytorch#8366 Differential Revision: D74020941
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020935
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020935
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020938
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020937
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020936
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: Pull Request resolved: pytorch#10645 pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020936
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020939
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020940
shoumikhin
added a commit
to shoumikhin/executorch
that referenced
this issue
May 2, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020940
shoumikhin
added a commit
that referenced
this issue
May 2, 2025
Summary: #8366 Reviewed By: kirklandsign Differential Revision: D74020940
shoumikhin
added a commit
that referenced
this issue
May 2, 2025
Summary: #8366 Reviewed By: kirklandsign Differential Revision: D74020937
jhelsby
pushed a commit
to jhelsby/executorch
that referenced
this issue
May 9, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020940
jhelsby
pushed a commit
to jhelsby/executorch
that referenced
this issue
May 9, 2025
Summary: pytorch#8366 Reviewed By: kirklandsign Differential Revision: D74020937
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
module: ios
Issues related to iOS code, build, and execution
triaged
This issue has been looked at a team member, and triaged and prioritized into an appropriate module
🚀 The feature, motivation and pitch
ExecuTorchTensor
is a lightweight Objective‑C wrapper for the ExecuTorch C++Tensor
API. It provides a natural, efficient interface for creating, accessing, and manipulating tensor data without exposing the C++ complexity. With multiple initializers (from raw bytes,NSData
, scalar arrays, or single scalars), it allows users to create tensors in various ways. The wrapper also exposes key metadata - such as data type, shape, dimension order, strides, and element count - in Objective‑C types, which are bridged naturally to Swift. This enables seamless, high‑performance integration into iOS/macOS applications using ExecuTorch.Alternatives
See the parent issue #8360 for motivation and alternatives overview.
Additional context
NSArrays
for efficient repeated access.nativeInstance
accessor exposes a pointer to the underlying C++ tensor (wrapped as aTensorPtr
) and is intended for internal use (e.g. when the tensor must be passed to other C++ APIs). This pointer is not exposed to Swift.RFC (Optional)
The
ExecuTorchTensor
wrapper provides a robust, multi-faceted Objective‑C interface to manage tensor objects backed by a native C++TensorPtr
. It supports various initialization modes (from raw pointers,NSData
, arrays of scalars, or single scalar values), data access (both immutable and mutable), cloning, resizing, and creating patterned tensors (empty, full, ones, zeros, random, random normal, and random integer). Swift interoperability is achieved viaNS_SWIFT_NAME
annotations, ensuring a natural Swift API.Internal Representation
The tensor wrapper internally holds a C++
TensorPtr
:Additional members include:
Cached metadata for
shape
,strides
, anddimensionOrder
.An optional
NSData
reference used when initializing from data.The underlying tensor pointer is exposed through a read‑only property (unavailable in Swift).
Properties
The following properties provide read‑only access to tensor metadata:
Objective-C
Swift
Key Methods:
Creates a tensor wrapper from an existing native instance. This is used internally and is unavailable in Swift.
Objective-C
Constructs a new tensor by sharing the data that another Tensor already uses, where the metadata like shape can be further modified, which allows creating a "view" on the Tensor's data without modifying the original Tensor.
Objective-C
Swift
Tensor should conform to
NSCopying
and implement corresponding methods:Copy will use
clone_tensor_ptr
internally to duplicate both the data and metadata that the Tensor owns and return a clone.The API provides two methods to access the raw tensor data as immutable or mutable data:
Objective-C
Swift
The tensor can be resized by providing a new shape. If the underlying C++ resize function fails, an error is reported using the
ExecuTorchErrorDomain
.Objective-C
Swift:
Tensors are compared by checking the data type, element count, underlying raw data (via
std::memcmp
), and metadata (shape, strides, dimension order, and shape dynamism).Objective-C
- (BOOL)isEqualToTensor:(nullable ExecuTorchTensor *)other;
Swift:
These initializers wrap an external memory pointer without copying the data. Users can optionally supply strides, dimension order, and an explicit shape dynamism.
Objective-C
Swift:
These methods copy data from a raw pointer into an internal buffer. The total byte count is computed from the provided shape and data type.
Objective-C
Swift
Initialize a tensor directly from an
NSData
instance. The initializer ensures the data buffer is large enough for the given shape and data type.Objective-C
Swift
These initializers create a tensor from an array of scalar values (
NSNumber
). The number of scalars must match the element count implied by the shape. Under the hood, each scalar is converted to its corresponding C++ type.Objective-C
Swift
Convenience initializers for single scalar values are provided with overloads for different primitive types. These wrap a single value as an array of one element, using an empty shape.
Objective-C
Swift
Return a tensor without any predefined values. Overloads allow creation based on a given shape or by mirroring an existing tensor’s metadata.
Objective-C
Swift
Create tensors filled with a given scalar value. There are overloads to either specify strides explicitly or to mimic an existing tensor’s layout.
Objective-C
Swift
These are shortcuts for full tensors with a fill value of 1 or 0, respectively.
Objective-C
Swift
These methods create tensors with random values. Separate factory methods support uniform random values, normal distribution and random integers within a specified range.
Objective-C
Swift
Methods like
resizeToShape:error:
return a boolean value indicating success. If an error occurs, anNSError
is populated using the domainExecuTorchErrorDomain
. Accessor properties are read-only and rely on cached metadata; if such metadata is not available, they return nil or default values, following Objective‑C conventions. In Swift, these methods appear as throwing functions.The text was updated successfully, but these errors were encountered: