Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,48 @@ __attribute__((deprecated("This API is experimental.")))
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism;

/**
* Initializes a tensor by copying bytes from the provided pointer with dynamic bound shape.
*
* @param pointer A pointer to the source data buffer.
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
* @param strides An NSArray of NSNumber objects representing the tensor's strides.
* @param dimensionOrder An NSArray of NSNumber objects indicating the order of dimensions.
* @param dataType An ExecuTorchDataType value specifying the element type.
* @return An initialized ExecuTorchTensor instance with its own copy of the data.
*/
- (instancetype)initWithBytes:(const void *)pointer
shape:(NSArray<NSNumber *> *)shape
strides:(NSArray<NSNumber *> *)strides
dimensionOrder:(NSArray<NSNumber *> *)dimensionOrder
dataType:(ExecuTorchDataType)dataType;

/**
* Initializes a tensor by copying bytes from the provided pointer, specifying shape, data type, and explicit shape dynamism.
*
* @param pointer A pointer to the source data buffer.
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
* @param dataType An ExecuTorchDataType value specifying the element type.
* @param shapeDynamism An ExecuTorchShapeDynamism value indicating the shape dynamism.
* @return An initialized ExecuTorchTensor instance with its own copy of the data.
*/
- (instancetype)initWithBytes:(const void *)pointer
shape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism;

/**
* Initializes a tensor by copying bytes from the provided pointer, specifying only the shape and data type.
*
* @param pointer A pointer to the source data buffer.
* @param shape An NSArray of NSNumber objects representing the tensor's shape.
* @param dataType An ExecuTorchDataType value specifying the element type.
* @return An initialized ExecuTorchTensor instance with its own copy of the data.
*/
- (instancetype)initWithBytes:(const void *)pointer
shape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType;

@end

NS_ASSUME_NONNULL_END
36 changes: 36 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,40 @@ - (instancetype)initWithBytes:(const void *)pointer
return [self initWithNativeInstance:&tensor];
}

- (instancetype)initWithBytes:(const void *)pointer
shape:(NSArray<NSNumber *> *)shape
strides:(NSArray<NSNumber *> *)strides
dimensionOrder:(NSArray<NSNumber *> *)dimensionOrder
dataType:(ExecuTorchDataType)dataType {
return [self initWithBytes:pointer
shape:shape
strides:strides
dimensionOrder:dimensionOrder
dataType:dataType
shapeDynamism:ExecuTorchShapeDynamismDynamicBound];
}

- (instancetype)initWithBytes:(const void *)pointer
shape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism {
return [self initWithBytes:pointer
shape:shape
strides:@[]
dimensionOrder:@[]
dataType:dataType
shapeDynamism:shapeDynamism];
}

- (instancetype)initWithBytes:(const void *)pointer
shape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType {
return [self initWithBytes:pointer
shape:shape
strides:@[]
dimensionOrder:@[]
dataType:dataType
shapeDynamism:ExecuTorchShapeDynamismDynamicBound];
}

@end
28 changes: 28 additions & 0 deletions extension/apple/ExecuTorch/__tests__/TensorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,32 @@ class TensorTest: XCTestCase {
XCTAssertEqual(tensor.shapeDynamism, .dynamicBound)
XCTAssertEqual(tensor.count, 6)
}

func testInitBytes() {
var data: [Double] = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
let tensor = data.withUnsafeMutableBytes {
Tensor(bytes: $0.baseAddress!, shape: [2, 3], dataType: .double)
}
XCTAssertEqual(tensor.dataType, .double)
XCTAssertEqual(tensor.shape, [2, 3])
XCTAssertEqual(tensor.strides, [3, 1])
XCTAssertEqual(tensor.dimensionOrder, [0, 1])
XCTAssertEqual(tensor.shapeDynamism, .dynamicBound)
XCTAssertEqual(tensor.count, 6)
}

func testWithCustomStridesAndDimensionOrder() {
let data: [Float] = [1.0, 2.0, 3.0, 4.0]
let tensor = Tensor(
bytes: data.withUnsafeBytes { $0.baseAddress! },
shape: [2, 2],
strides: [1, 2],
dimensionOrder: [1, 0],
dataType: .float
)
XCTAssertEqual(tensor.shape, [2, 2])
XCTAssertEqual(tensor.strides, [1, 2])
XCTAssertEqual(tensor.dimensionOrder, [1, 0])
XCTAssertEqual(tensor.count, 4)
}
}
Loading