Skip to content

Helpers to create empty tensors. #10621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
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
78 changes: 78 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -670,4 +670,82 @@ __attribute__((deprecated("This API is experimental.")))

@end

#pragma mark - Empty Category

@interface ExecuTorchTensor (Empty)

/**
* Creates an empty tensor with the specified shape, strides, data type, and shape dynamism.
*
* @param shape An NSArray of NSNumber objects representing the desired shape.
* @param strides An NSArray of NSNumber objects representing the desired strides.
* @param dataType An ExecuTorchDataType value specifying the element type.
* @param shapeDynamism An ExecuTorchShapeDynamism value specifying whether the shape is static or dynamic.
* @return A new, empty ExecuTorchTensor instance.
*/
+ (instancetype)emptyTensorWithShape:(NSArray<NSNumber *> *)shape
strides:(NSArray<NSNumber *> *)strides
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism
NS_SWIFT_NAME(empty(shape:strides:dataType:shapeDynamism:));

/**
* Creates an empty tensor with the specified shape, data type, and shape dynamism.
*
* @param shape An NSArray of NSNumber objects representing the desired shape.
* @param dataType An ExecuTorchDataType value specifying the element type.
* @param shapeDynamism An ExecuTorchShapeDynamism value specifying whether the shape is static or dynamic.
* @return A new, empty ExecuTorchTensor instance.
*/
+ (instancetype)emptyTensorWithShape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism
NS_SWIFT_NAME(empty(shape:dataType:shapeDynamism:));

/**
* Creates an empty tensor with the specified shape and data type, using dynamic bound shape.
*
* @param shape An NSArray of NSNumber objects representing the desired shape.
* @param dataType An ExecuTorchDataType value specifying the element type.
* @return A new, empty ExecuTorchTensor instance.
*/
+ (instancetype)emptyTensorWithShape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType
NS_SWIFT_NAME(empty(shape:dataType:));

/**
* Creates an empty tensor similar to the given tensor, with the specified data type and shape dynamism.
*
* @param tensor An existing ExecuTorchTensor instance whose shape and strides are used.
* @param dataType An ExecuTorchDataType value specifying the desired element type.
* @param shapeDynamism An ExecuTorchShapeDynamism value specifying whether the shape is static or dynamic.
* @return A new, empty ExecuTorchTensor instance with the same shape as the provided tensor.
*/
+ (instancetype)emptyTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism
NS_SWIFT_NAME(empty(like:dataType:shapeDynamism:));

/**
* Creates an empty tensor similar to the given tensor, with the specified data type.
*
* @param tensor An existing ExecuTorchTensor instance whose shape and strides are used.
* @param dataType An ExecuTorchDataType value specifying the desired element type.
* @return A new, empty ExecuTorchTensor instance with the same shape as the provided tensor.
*/
+ (instancetype)emptyTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType
NS_SWIFT_NAME(empty(like:dataType:));

/**
* Creates an empty tensor similar to the given tensor.
*
* @param tensor An existing ExecuTorchTensor instance.
* @return A new, empty ExecuTorchTensor instance with the same properties as the provided tensor.
*/
+ (instancetype)emptyTensorLikeTensor:(ExecuTorchTensor *)tensor
NS_SWIFT_NAME(empty(like:));

@end

NS_ASSUME_NONNULL_END
58 changes: 58 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,61 @@ - (instancetype)initWithUnsignedInteger:(NSUInteger)scalar {
}

@end

@implementation ExecuTorchTensor (Empty)

+ (instancetype)emptyTensorWithShape:(NSArray<NSNumber *> *)shape
strides:(NSArray<NSNumber *> *)strides
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism {
auto tensor = empty_strided(
utils::toVector<SizesType>(shape),
utils::toVector<StridesType>(strides),
static_cast<ScalarType>(dataType),
static_cast<TensorShapeDynamism>(shapeDynamism)
);
return [[self alloc] initWithNativeInstance:&tensor];
}

+ (instancetype)emptyTensorWithShape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism {
return [self emptyTensorWithShape:shape
strides:@[]
dataType:dataType
shapeDynamism:shapeDynamism];
}

+ (instancetype)emptyTensorWithShape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType {
return [self emptyTensorWithShape:shape
strides:@[]
dataType:dataType
shapeDynamism:ExecuTorchShapeDynamismDynamicBound];
}

+ (instancetype)emptyTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism {
return [self emptyTensorWithShape:tensor.shape
strides:tensor.strides
dataType:dataType
shapeDynamism:shapeDynamism];
}

+ (instancetype)emptyTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType {
return [self emptyTensorWithShape:tensor.shape
strides:tensor.strides
dataType:dataType
shapeDynamism:tensor.shapeDynamism];
}

+ (instancetype)emptyTensorLikeTensor:(ExecuTorchTensor *)tensor {
return [self emptyTensorWithShape:tensor.shape
strides:tensor.strides
dataType:tensor.dataType
shapeDynamism:tensor.shapeDynamism];
}

@end
20 changes: 20 additions & 0 deletions extension/apple/ExecuTorch/__tests__/TensorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,24 @@ class TensorTest: XCTestCase {
XCTAssertEqual(UnsafeBufferPointer(start: pointer.assumingMemoryBound(to: UInt.self), count: count).first, 42)
}
}

func testEmpty() {
let tensor = Tensor.empty(shape: [3, 4], dataType: .float)
XCTAssertEqual(tensor.shape, [3, 4])
XCTAssertEqual(tensor.count, 12)
tensor.bytes { pointer, count, dataType in
XCTAssertNotNil(pointer)
XCTAssertEqual(count, 12)
XCTAssertEqual(dataType, .float)
}
}

func testEmptyLike() {
let other = Tensor.empty(shape: [2, 2], dataType: .int)
let tensor = Tensor.empty(like: other)
XCTAssertEqual(tensor.shape, other.shape)
XCTAssertEqual(tensor.strides, other.strides)
XCTAssertEqual(tensor.dimensionOrder, other.dimensionOrder)
XCTAssertEqual(tensor.dataType, other.dataType)
}
}
Loading