Skip to content

Helpers to create zeros tensor. #10643

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 3 commits into from
May 2, 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
63 changes: 63 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,69 @@ __attribute__((deprecated("This API is experimental.")))

@end

#pragma mark - Zeros Category

@interface ExecuTorchTensor (Zeros)

/**
* Creates a tensor filled with zeros, 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 ExecuTorchTensor instance filled with zeros.
*/
+ (instancetype)zerosTensorWithShape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism
NS_SWIFT_NAME(zeros(shape:dataType:shapeDynamism:));

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

/**
* Creates a tensor filled with zeros similar to an existing 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 ExecuTorchTensor instance filled with zeros.
*/
+ (instancetype)zerosTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism
NS_SWIFT_NAME(zeros(like:dataType:shapeDynamism:));

/**
* Creates a tensor filled with zeros similar to an existing 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 ExecuTorchTensor instance filled with zeros.
*/
+ (instancetype)zerosTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType
NS_SWIFT_NAME(zeros(like:dataType:));

/**
* Creates a tensor filled with zeros similar to an existing tensor.
*
* @param tensor An existing ExecuTorchTensor instance.
* @return A new ExecuTorchTensor instance filled with zeros.
*/
+ (instancetype)zerosTensorLikeTensor:(ExecuTorchTensor *)tensor
NS_SWIFT_NAME(zeros(like:));

@end

#pragma mark - Random Category

@interface ExecuTorchTensor (Random)
Expand Down
50 changes: 50 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,56 @@ + (instancetype)onesTensorLikeTensor:(ExecuTorchTensor *)tensor {

@end

@implementation ExecuTorchTensor (Zeros)

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

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

+ (instancetype)zerosTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism {
return [self fullTensorWithShape:tensor.shape
scalar:@(0)
strides:tensor.strides
dataType:dataType
shapeDynamism:shapeDynamism];
}

+ (instancetype)zerosTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType {
return [self fullTensorWithShape:tensor.shape
scalar:@(0)
strides:tensor.strides
dataType:dataType
shapeDynamism:tensor.shapeDynamism];
}

+ (instancetype)zerosTensorLikeTensor:(ExecuTorchTensor *)tensor {
return [self fullTensorWithShape:tensor.shape
scalar:@(0)
strides:tensor.strides
dataType:tensor.dataType
shapeDynamism:tensor.shapeDynamism];
}

@end

@implementation ExecuTorchTensor (Random)

+ (instancetype)randomTensorWithShape:(NSArray<NSNumber *> *)shape
Expand Down
25 changes: 25 additions & 0 deletions extension/apple/ExecuTorch/__tests__/TensorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,31 @@ class TensorTest: XCTestCase {
}
}
}

func testZeros() {
let tensor = Tensor.zeros(shape: [2, 3], dataType: .double)
XCTAssertEqual(tensor.shape, [2, 3])
XCTAssertEqual(tensor.count, 6)
tensor.bytes { pointer, count, dataType in
XCTAssertEqual(dataType, .double)
let buffer = UnsafeBufferPointer(start: pointer.assumingMemoryBound(to: Double.self), count: count)
for value in buffer {
XCTAssertEqual(value, 0)
}
}
}

func testZerosLike() {
let other = Tensor.full(shape: [3, 2], scalar: 9, dataType: .int)
let tensor = Tensor.zeros(like: other)
XCTAssertEqual(tensor.shape, other.shape)
tensor.bytes { pointer, count, dataType in
let buffer = UnsafeBufferPointer(start: pointer.assumingMemoryBound(to: Int32.self), count: count)
for value in buffer {
XCTAssertEqual(value, 0)
}
}
}

func testRandom() {
let tensor = Tensor.rand(shape: [3, 3], dataType: .float)
Expand Down
Loading