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
11 changes: 11 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ __attribute__((deprecated("This API is experimental.")))
- (void)mutableBytesWithHandler:(void (^)(void *pointer, NSInteger count, ExecuTorchDataType dataType))handler
NS_SWIFT_NAME(mutableBytes(_:));

/**
* Resizes the tensor to a new shape.
*
* @param shape An NSArray of NSNumber objects representing the desired new shape.
* @param error A pointer to an NSError pointer that is set if an error occurs.
* @return YES if the tensor was successfully resized; otherwise, NO.
*/
- (BOOL)resizeToShape:(NSArray<NSNumber *> *)shape
error:(NSError **)error
NS_SWIFT_NAME(resize(to:));

+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

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

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

NSInteger ExecuTorchSizeOfDataType(ExecuTorchDataType dataType) {
return elementSize(static_cast<ScalarType>(dataType));
Expand Down Expand Up @@ -108,6 +109,25 @@ - (void)mutableBytesWithHandler:(void (^)(void *pointer, NSInteger count, ExecuT
handler(_tensor->unsafeGetTensorImpl()->mutable_data(), self.count, self.dataType);
}

- (BOOL)resizeToShape:(NSArray<NSNumber *> *)shape
error:(NSError **)error {
const auto resizeError = resize_tensor_ptr(
_tensor, utils::toVector<SizesType>(shape)
);
if (resizeError != Error::Ok) {
if (error) {
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
code:(NSInteger)resizeError
userInfo:nil];
}
return NO;
}
_shape = nil;
_strides = nil;
_dimensionOrder = nil;
return YES;
}

@end

@implementation ExecuTorchTensor (BytesNoCopy)
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 @@ -165,4 +165,29 @@ class TensorTest: XCTestCase {
XCTAssertEqual(tensor1.dimensionOrder, tensor2.dimensionOrder)
XCTAssertEqual(tensor1.count, tensor2.count)
}

func testResize() {
var data: [Int] = [1, 2, 3, 4]
let tensor = data.withUnsafeMutableBytes {
Tensor(bytesNoCopy: $0.baseAddress!, shape: [4, 1], dataType: .int)
}
XCTAssertNoThrow(try tensor.resize(to: [2, 2]))
XCTAssertEqual(tensor.dataType, .int)
XCTAssertEqual(tensor.shape, [2, 2])
XCTAssertEqual(tensor.strides, [2, 1])
XCTAssertEqual(tensor.dimensionOrder, [0, 1])
XCTAssertEqual(tensor.count, 4)

tensor.bytes { pointer, count, dataType in
XCTAssertEqual(Array(UnsafeBufferPointer(start: pointer.assumingMemoryBound(to: Int.self), count: count)), data)
}
}

func testResizeError() {
var data: [Int] = [1, 2, 3, 4]
let tensor = data.withUnsafeMutableBytes {
Tensor(bytesNoCopy: $0.baseAddress!, shape: [4, 1], dataType: .int)
}
XCTAssertThrowsError(try tensor.resize(to: [2, 3]))
}
}
Loading