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
6 changes: 5 additions & 1 deletion extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ - (instancetype)initWithTensor:(ExecuTorchTensor *)otherTensor {
auto tensor = make_tensor_ptr(
*reinterpret_cast<TensorPtr *>(otherTensor.nativeInstance)
);
return [self initWithNativeInstance:&tensor];
self = [self initWithNativeInstance:&tensor];
if (self) {
_data = otherTensor->_data;
}
return self;
}

- (instancetype)copy {
Expand Down
13 changes: 13 additions & 0 deletions extension/apple/ExecuTorch/__tests__/TensorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ class TensorTest: XCTestCase {
XCTAssertEqual(tensor.scalars(), dataArray)
}

func testInitDataViewSurvivesSourceScopeEnd() {
let dataArray: [Float] = [1.0, 2.0, 3.0, 4.0]
var view: Tensor<Float>!
autoreleasepool {
let data = Data(bytes: dataArray, count: dataArray.count * MemoryLayout<Float>.size)
let tensor = Tensor<Float>(data: data, shape: [4])
view = Tensor<Float>(tensor)
XCTAssertEqual(view.scalars(), dataArray)
}
XCTAssertEqual(view.count, 4)
XCTAssertEqual(view.scalars(), dataArray)
}

func testWithCustomStridesAndDimensionOrder() {
let data: [Float] = [1.0, 2.0, 3.0, 4.0]
let tensor = Tensor<Float>(
Expand Down
Loading