Skip to content

Tensor constructor to create with a raw pointer w/o copying. #9670

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

@end

#pragma mark - BytesNoCopy Category

@interface ExecuTorchTensor (BytesNoCopy)

/**
* Initializes a tensor without copying the provided data.
*
* @param pointer A pointer to the 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.
* @param shapeDynamism An ExecuTorchShapeDynamism value indicating whether the shape is static or dynamic.
* @return An initialized ExecuTorchTensor instance using the provided data buffer.
*/
- (instancetype)initWithBytesNoCopy:(void *)pointer
shape:(NSArray<NSNumber *> *)shape
strides:(NSArray<NSNumber *> *)strides
dimensionOrder:(NSArray<NSNumber *> *)dimensionOrder
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism;

@end

NS_ASSUME_NONNULL_END
23 changes: 23 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#import <executorch/extension/tensor/tensor.h>

using namespace executorch::aten;
using namespace executorch::extension;

@implementation ExecuTorchTensor {
Expand Down Expand Up @@ -69,3 +70,25 @@ - (NSInteger)count {
}

@end

@implementation ExecuTorchTensor (BytesNoCopy)

- (instancetype)initWithBytesNoCopy:(void *)pointer
shape:(NSArray<NSNumber *> *)shape
strides:(NSArray<NSNumber *> *)strides
dimensionOrder:(NSArray<NSNumber *> *)dimensionOrder
dataType:(ExecuTorchDataType)dataType
shapeDynamism:(ExecuTorchShapeDynamism)shapeDynamism {
ET_CHECK(pointer);
auto tensor = make_tensor_ptr(
utils::toVector<SizesType>(shape),
pointer,
utils::toVector<DimOrderType>(dimensionOrder),
utils::toVector<StridesType>(strides),
static_cast<ScalarType>(dataType),
static_cast<TensorShapeDynamism>(shapeDynamism)
);
return [self initWithNativeInstance:&tensor];
}

@end
Loading