Skip to content

Commit 1a187c4

Browse files
shoumikhinkirklandsign
authored andcommitted
Initialize Tensor with native TensorPtr.
Differential Revision: D71901449 Pull Request resolved: #9646
1 parent da41050 commit 1a187c4

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,32 @@ typedef NS_ENUM(uint8_t, ExecuTorchShapeDynamism) {
6161
ExecuTorchShapeDynamismDynamicUnbound,
6262
} NS_SWIFT_NAME(ShapeDynamism);
6363

64+
/**
65+
* A tensor class for ExecuTorch operations.
66+
*
67+
* This class encapsulates a native TensorPtr instance and provides a variety of
68+
* initializers and utility methods to work with tensor data.
69+
*/
6470
NS_SWIFT_NAME(Tensor)
6571
__attribute__((deprecated("This API is experimental.")))
6672
@interface ExecuTorchTensor : NSObject
6773

74+
/**
75+
* Pointer to the underlying native TensorPtr instance.
76+
*
77+
* @return A raw pointer to the native TensorPtr held by this Tensor class.
78+
*/
79+
@property(nonatomic, readonly) void *nativeInstance NS_SWIFT_UNAVAILABLE("");
80+
81+
/**
82+
* Initializes a tensor with a native TensorPtr instance.
83+
*
84+
* @param nativeInstance A pointer to a native TensorPtr instance.
85+
* @return An initialized ExecuTorchTensor instance.
86+
*/
87+
- (instancetype)initWithNativeInstance:(void *)nativeInstance
88+
NS_DESIGNATED_INITIALIZER NS_SWIFT_UNAVAILABLE("");
89+
6890
+ (instancetype)new NS_UNAVAILABLE;
6991
- (instancetype)init NS_UNAVAILABLE;
7092

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@
1212

1313
#import <executorch/extension/tensor/tensor.h>
1414

15+
using namespace executorch::extension;
16+
1517
@implementation ExecuTorchTensor {
16-
::executorch::extension::TensorPtr _tensor;
18+
TensorPtr _tensor;
19+
}
20+
21+
- (instancetype)initWithNativeInstance:(void *)nativeInstance {
22+
ET_CHECK(nativeInstance);
23+
if (self = [super init]) {
24+
_tensor = std::move(*reinterpret_cast<TensorPtr *>(nativeInstance));
25+
ET_CHECK(_tensor);
26+
}
27+
return self;
28+
}
29+
30+
- (void *)nativeInstance {
31+
return &_tensor;
1732
}
1833

1934
@end

0 commit comments

Comments
 (0)