Skip to content

Commit dd3122d

Browse files
Overloads for data Tensor constructor. (#9691)
Summary: #8366 Reviewed By: bsoyluoglu Differential Revision: D71929194 Co-authored-by: Anthony Shoumikhin <[email protected]>
1 parent 05696f8 commit dd3122d

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h

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

370+
/**
371+
* Initializes a tensor using an NSData object as the underlying data buffer with dynamic bound shape.
372+
*
373+
* @param data An NSData object containing the tensor data.
374+
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
375+
* @param strides An NSArray of NSNumber objects representing the tensor's strides.
376+
* @param dimensionOrder An NSArray of NSNumber objects indicating the order of dimensions.
377+
* @param dataType An ExecuTorchDataType value specifying the element type.
378+
* @return An initialized ExecuTorchTensor instance using the provided data.
379+
*/
380+
- (instancetype)initWithData:(NSData *)data
381+
shape:(NSArray<NSNumber *> *)shape
382+
strides:(NSArray<NSNumber *> *)strides
383+
dimensionOrder:(NSArray<NSNumber *> *)dimensionOrder
384+
dataType:(ExecuTorchDataType)dataType;
385+
386+
/**
387+
* Initializes a tensor using an NSData object as the underlying data buffer, specifying shape, data type, and explicit shape dynamism.
388+
*
389+
* @param data An NSData object containing the tensor data.
390+
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
391+
* @param dataType An ExecuTorchDataType value specifying the element type.
392+
* @param shapeDynamism An ExecuTorchShapeDynamism value indicating the shape dynamism.
393+
* @return An initialized ExecuTorchTensor instance using the provided data.
394+
*/
395+
- (instancetype)initWithData:(NSData *)data
396+
shape:(NSArray<NSNumber *> *)shape
397+
dataType:(ExecuTorchDataType)dataType
398+
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism;
399+
400+
/**
401+
* Initializes a tensor using an NSData object as the underlying data buffer, specifying only the shape and data type.
402+
*
403+
* @param data An NSData object containing the tensor data.
404+
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
405+
* @param dataType An ExecuTorchDataType value specifying the element type.
406+
* @return An initialized ExecuTorchTensor instance using the provided data.
407+
*/
408+
- (instancetype)initWithData:(NSData *)data
409+
shape:(NSArray<NSNumber *> *)shape
410+
dataType:(ExecuTorchDataType)dataType;
411+
370412
@end
371413

372414
NS_ASSUME_NONNULL_END

extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,40 @@ - (instancetype)initWithData:(NSData *)data
298298
return self;
299299
}
300300

301+
- (instancetype)initWithData:(NSData *)data
302+
shape:(NSArray<NSNumber *> *)shape
303+
strides:(NSArray<NSNumber *> *)strides
304+
dimensionOrder:(NSArray<NSNumber *> *)dimensionOrder
305+
dataType:(ExecuTorchDataType)dataType {
306+
return [self initWithData:data
307+
shape:shape
308+
strides:strides
309+
dimensionOrder:dimensionOrder
310+
dataType:dataType
311+
shapeDynamism:ExecuTorchShapeDynamismDynamicBound];
312+
}
313+
314+
- (instancetype)initWithData:(NSData *)data
315+
shape:(NSArray<NSNumber *> *)shape
316+
dataType:(ExecuTorchDataType)dataType
317+
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism {
318+
return [self initWithData:data
319+
shape:shape
320+
strides:@[]
321+
dimensionOrder:@[]
322+
dataType:dataType
323+
shapeDynamism:shapeDynamism];
324+
}
325+
326+
- (instancetype)initWithData:(NSData *)data
327+
shape:(NSArray<NSNumber *> *)shape
328+
dataType:(ExecuTorchDataType)dataType {
329+
return [self initWithData:data
330+
shape:shape
331+
strides:@[]
332+
dimensionOrder:@[]
333+
dataType:dataType
334+
shapeDynamism:ExecuTorchShapeDynamismDynamicBound];
335+
}
336+
301337
@end

extension/apple/ExecuTorch/__tests__/TensorTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class TensorTest: XCTestCase {
104104
func testInitData() {
105105
let dataArray: [Float] = [1.0, 2.0, 3.0, 4.0]
106106
let data = Data(bytes: dataArray, count: dataArray.count * MemoryLayout<Float>.size)
107-
let tensor = Tensor(data: data, shape: [4], strides: [1], dimensionOrder: [0], dataType: .float, shapeDynamism: .static)
107+
let tensor = Tensor(data: data, shape: [4], dataType: .float)
108108
XCTAssertEqual(tensor.count, 4)
109109
tensor.bytes { pointer, count, dataType in
110110
XCTAssertEqual(Array(UnsafeBufferPointer(start: pointer.assumingMemoryBound(to: Float.self), count: count)), dataArray)

0 commit comments

Comments
 (0)