Skip to content

Commit 520ad70

Browse files
shoumikhinkirklandsign
authored andcommitted
Tensor properties to get shape, etc.
Differential Revision: D71901794 Pull Request resolved: #9668
1 parent f50b590 commit 520ad70

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,48 @@ __attribute__((deprecated("This API is experimental.")))
7878
*/
7979
@property(nonatomic, readonly) void *nativeInstance NS_SWIFT_UNAVAILABLE("");
8080

81+
/**
82+
* The data type of the tensor.
83+
*
84+
* @return An ExecuTorchDataType value representing the tensor's element type.
85+
*/
86+
@property(nonatomic, readonly) ExecuTorchDataType dataType;
87+
88+
/**
89+
* The shape of the tensor.
90+
*
91+
* @return An NSArray of NSNumber objects representing the size of each dimension.
92+
*/
93+
@property(nonatomic, readonly) NSArray<NSNumber *> *shape;
94+
95+
/**
96+
* The order of dimensions in the tensor.
97+
*
98+
* @return An NSArray of NSNumber objects representing the tensor’s dimension order.
99+
*/
100+
@property(nonatomic, readonly) NSArray<NSNumber *> *dimensionOrder;
101+
102+
/**
103+
* The strides of the tensor.
104+
*
105+
* @return An NSArray of NSNumber objects representing the step sizes for each dimension.
106+
*/
107+
@property(nonatomic, readonly) NSArray<NSNumber *> *strides;
108+
109+
/**
110+
* The dynamism of the tensor's shape.
111+
*
112+
* @return An ExecuTorchShapeDynamism value indicating whether the tensor shape is static or dynamic.
113+
*/
114+
@property(nonatomic, readonly) ExecuTorchShapeDynamism shapeDynamism;
115+
116+
/**
117+
* The total number of elements in the tensor.
118+
*
119+
* @return An NSInteger representing the total element count.
120+
*/
121+
@property(nonatomic, readonly) NSInteger count;
122+
81123
/**
82124
* Initializes a tensor with a native TensorPtr instance.
83125
*

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
#import "ExecuTorchTensor.h"
1010

1111
#import "ExecuTorchError.h"
12+
#import "ExecuTorchUtils.h"
1213

1314
#import <executorch/extension/tensor/tensor.h>
1415

1516
using namespace executorch::extension;
1617

1718
@implementation ExecuTorchTensor {
1819
TensorPtr _tensor;
20+
NSArray<NSNumber *> *_shape;
21+
NSArray<NSNumber *> *_strides;
22+
NSArray<NSNumber *> *_dimensionOrder;
1923
}
2024

2125
- (instancetype)initWithNativeInstance:(void *)nativeInstance {
@@ -31,4 +35,37 @@ - (void *)nativeInstance {
3135
return &_tensor;
3236
}
3337

38+
- (ExecuTorchDataType)dataType {
39+
return static_cast<ExecuTorchDataType>(_tensor->scalar_type());
40+
}
41+
42+
- (NSArray<NSNumber *> *)shape {
43+
if (!_shape) {
44+
_shape = utils::toNSArray(_tensor->sizes());
45+
}
46+
return _shape;
47+
}
48+
49+
- (NSArray<NSNumber *> *)dimensionOrder {
50+
if (!_dimensionOrder) {
51+
_dimensionOrder = utils::toNSArray(_tensor->dim_order());
52+
}
53+
return _dimensionOrder;
54+
}
55+
56+
- (NSArray<NSNumber *> *)strides {
57+
if (!_strides) {
58+
_strides = utils::toNSArray(_tensor->strides());
59+
}
60+
return _strides;
61+
}
62+
63+
- (ExecuTorchShapeDynamism)shapeDynamism {
64+
return static_cast<ExecuTorchShapeDynamism>(_tensor->shape_dynamism());
65+
}
66+
67+
- (NSInteger)count {
68+
return _tensor->numel();
69+
}
70+
3471
@end

0 commit comments

Comments
 (0)