Skip to content

Commit 93c3b2f

Browse files
authored
Overloads for the bytesNoCopy Tensor constrcutor.
Differential Revision: D71902713 Pull Request resolved: #9672
1 parent d5898b7 commit 93c3b2f

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,48 @@ __attribute__((deprecated("This API is experimental.")))
156156
dataType:(ExecuTorchDataType)dataType
157157
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism;
158158

159+
/**
160+
* Initializes a tensor without copying data using dynamic bound shape (default strides and dimension order).
161+
*
162+
* @param pointer A pointer to the data buffer.
163+
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
164+
* @param strides An NSArray of NSNumber objects representing the tensor's strides.
165+
* @param dimensionOrder An NSArray of NSNumber objects indicating the order of dimensions.
166+
* @param dataType An ExecuTorchDataType value specifying the element type.
167+
* @return An initialized ExecuTorchTensor instance.
168+
*/
169+
- (instancetype)initWithBytesNoCopy:(void *)pointer
170+
shape:(NSArray<NSNumber *> *)shape
171+
strides:(NSArray<NSNumber *> *)strides
172+
dimensionOrder:(NSArray<NSNumber *> *)dimensionOrder
173+
dataType:(ExecuTorchDataType)dataType;
174+
175+
/**
176+
* Initializes a tensor without copying data, with an explicit shape dynamism.
177+
*
178+
* @param pointer A pointer to the data buffer.
179+
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
180+
* @param dataType An ExecuTorchDataType value specifying the element type.
181+
* @param shapeDynamism An ExecuTorchShapeDynamism value indicating the shape dynamism.
182+
* @return An initialized ExecuTorchTensor instance.
183+
*/
184+
- (instancetype)initWithBytesNoCopy:(void *)pointer
185+
shape:(NSArray<NSNumber *> *)shape
186+
dataType:(ExecuTorchDataType)dataType
187+
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism;
188+
189+
/**
190+
* Initializes a tensor without copying data, specifying only the shape and data type.
191+
*
192+
* @param pointer A pointer to the data buffer.
193+
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
194+
* @param dataType An ExecuTorchDataType value specifying the element type.
195+
* @return An initialized ExecuTorchTensor instance.
196+
*/
197+
- (instancetype)initWithBytesNoCopy:(void *)pointer
198+
shape:(NSArray<NSNumber *> *)shape
199+
dataType:(ExecuTorchDataType)dataType;
200+
159201
@end
160202

161203
NS_ASSUME_NONNULL_END

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,40 @@ - (instancetype)initWithBytesNoCopy:(void *)pointer
9191
return [self initWithNativeInstance:&tensor];
9292
}
9393

94+
- (instancetype)initWithBytesNoCopy:(void *)pointer
95+
shape:(NSArray<NSNumber *> *)shape
96+
strides:(NSArray<NSNumber *> *)strides
97+
dimensionOrder:(NSArray<NSNumber *> *)dimensionOrder
98+
dataType:(ExecuTorchDataType)dataType {
99+
return [self initWithBytesNoCopy:pointer
100+
shape:shape
101+
strides:strides
102+
dimensionOrder:dimensionOrder
103+
dataType:dataType
104+
shapeDynamism:ExecuTorchShapeDynamismDynamicBound];
105+
}
106+
107+
- (instancetype)initWithBytesNoCopy:(void *)pointer
108+
shape:(NSArray<NSNumber *> *)shape
109+
dataType:(ExecuTorchDataType)dataType
110+
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism {
111+
return [self initWithBytesNoCopy:pointer
112+
shape:shape
113+
strides:@[]
114+
dimensionOrder:@[]
115+
dataType:dataType
116+
shapeDynamism:shapeDynamism];
117+
}
118+
119+
- (instancetype)initWithBytesNoCopy:(void *)pointer
120+
shape:(NSArray<NSNumber *> *)shape
121+
dataType:(ExecuTorchDataType)dataType {
122+
return [self initWithBytesNoCopy:pointer
123+
shape:shape
124+
strides:@[]
125+
dimensionOrder:@[]
126+
dataType:dataType
127+
shapeDynamism:ExecuTorchShapeDynamismDynamicBound];
128+
}
129+
94130
@end

extension/apple/ExecuTorch/__tests__/TensorTest.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
import XCTest
1212

1313
class TensorTest: XCTestCase {
14-
func test() {
14+
func testInitBytesNoCopy() {
15+
var data: [Float] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
16+
let tensor = data.withUnsafeMutableBytes {
17+
Tensor(bytesNoCopy: $0.baseAddress!, shape: [2, 3], dataType: .float)
18+
}
19+
XCTAssertEqual(tensor.dataType, .float)
20+
XCTAssertEqual(tensor.shape, [2, 3])
21+
XCTAssertEqual(tensor.strides, [3, 1])
22+
XCTAssertEqual(tensor.dimensionOrder, [0, 1])
23+
XCTAssertEqual(tensor.shapeDynamism, .dynamicBound)
24+
XCTAssertEqual(tensor.count, 6)
1525
}
1626
}

0 commit comments

Comments
 (0)