Skip to content

Helpers to create random normal tensor. #10648

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 1 commit 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
83 changes: 83 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -980,4 +980,87 @@ __attribute__((deprecated("This API is experimental.")))

@end

#pragma mark - RandomNormal Category

@interface ExecuTorchTensor (RandomNormal)

/**
* Creates a tensor with random values drawn from a normal distribution,
* with full specification of 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 ExecuTorchTensor instance filled with values from a normal distribution.
*/
+ (instancetype)randomNormalTensorWithShape:(NSArray<NSNumber *> *)shape
strides:(NSArray<NSNumber *> *)strides
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism
NS_SWIFT_NAME(randn(shape:strides:dataType:shapeDynamism:));

/**
* Creates a tensor with random values drawn from a normal distribution,
* 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.
* @param shapeDynamism An ExecuTorchShapeDynamism value specifying whether the shape is static or dynamic.
* @return A new ExecuTorchTensor instance filled with values from a normal distribution.
*/
+ (instancetype)randomNormalTensorWithShape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism
NS_SWIFT_NAME(randn(shape:dataType:shapeDynamism:));

/**
* Creates a tensor with random values drawn from a normal distribution,
* with the specified shape (using dynamic bound 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 values from a normal distribution.
*/
+ (instancetype)randomNormalTensorWithShape:(NSArray<NSNumber *> *)shape
dataType:(ExecuTorchDataType)dataType
NS_SWIFT_NAME(randn(shape:dataType:));

/**
* Creates a tensor with random normal values 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 values from a normal distribution.
*/
+ (instancetype)randomNormalTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism
NS_SWIFT_NAME(randn(like:dataType:shapeDynamism:));

/**
* Creates a tensor with random normal values 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 values from a normal distribution.
*/
+ (instancetype)randomNormalTensorLikeTensor:(ExecuTorchTensor *)tensor
dataType:(ExecuTorchDataType)dataType
NS_SWIFT_NAME(randn(like:dataType:));

/**
* Creates a tensor with random normal values similar to an existing tensor.
*
* @param tensor An existing ExecuTorchTensor instance.
* @return A new ExecuTorchTensor instance filled with values from a normal distribution.
*/
+ (instancetype)randomNormalTensorLikeTensor:(ExecuTorchTensor *)tensor
NS_SWIFT_NAME(randn(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 @@ -833,3 +833,61 @@ + (instancetype)randomTensorLikeTensor:(ExecuTorchTensor *)tensor {
}

@end

@implementation ExecuTorchTensor (RandomNormal)

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

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

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

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

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

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

@end
18 changes: 18 additions & 0 deletions extension/apple/ExecuTorch/__tests__/TensorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,22 @@ class TensorTest: XCTestCase {
XCTAssertEqual(tensor.shape, other.shape)
XCTAssertEqual(tensor.count, other.count)
}

func testRandomNormal() {
let tensor = Tensor.randn(shape: [4], dataType: .double)
XCTAssertEqual(tensor.shape, [4])
XCTAssertEqual(tensor.count, 4)
tensor.bytes { pointer, count, dataType in
XCTAssertEqual(dataType, .double)
let buffer = UnsafeBufferPointer(start: pointer.assumingMemoryBound(to: Double.self), count: count)
XCTAssertEqual(buffer.count, 4)
}
}

func testRandomNormalLike() {
let other = Tensor.zeros(shape: [4], dataType: .float)
let tensor = Tensor.randn(like: other)
XCTAssertEqual(tensor.shape, other.shape)
XCTAssertEqual(tensor.count, other.count)
}
}
Loading