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
26 changes: 22 additions & 4 deletions extension/tensor/tensor_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,37 @@ inline TensorPtr make_tensor_ptr(const executorch::aten::Tensor& tensor) {
#ifndef USE_ATEN_LIB
std::vector<executorch::aten::DimOrderType>(
tensor.dim_order().begin(), tensor.dim_order().end()),
std::vector<executorch::aten::StridesType>(
tensor.strides().begin(), tensor.strides().end()),
tensor.scalar_type(),
tensor.shape_dynamism()
#else // USE_ATEN_LIB
{},
#endif // USE_ATEN_LIB
std::vector<executorch::aten::StridesType>(
tensor.strides().begin(), tensor.strides().end()),
tensor.scalar_type()
#ifndef USE_ATEN_LIB
,
tensor.shape_dynamism()
#endif // USE_ATEN_LIB
);
}

/**
* Creates a TensorPtr to manage a new Tensor with the same properties
* as the Tensor referenced by the given TensorPtr, sharing the same data
* without owning it.
*
* This is a convenience overload equivalent to make_tensor_ptr(*tensor_ptr).
* It does not extend the lifetime of the underlying buffer; if the original
* owner releases the storage, all views aliasing it become dangling.
*
* @param tensor_ptr The TensorPtr whose underlying Tensor is used to initialize
* the returned view.
* @return A new TensorPtr managing a Tensor with the same properties as the
* original.
*/
inline TensorPtr make_tensor_ptr(const TensorPtr& tensor_ptr) {
return make_tensor_ptr(*tensor_ptr);
}

/**
* Creates a TensorPtr that manages a new Tensor with the same properties
* as the given Tensor, but with a copy of the data owned by the returned
Expand Down
62 changes: 56 additions & 6 deletions extension/tensor/test/tensor_ptr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ TEST_F(TensorPtrTest, TensorSharingImplResizingAffectsBothVector) {
TEST_F(TensorPtrTest, MakeTensorPtrFromExistingTensorInt32) {
std::vector<int32_t> data = {1, 2, 3, 4};
auto tensor = make_tensor_ptr({2, 2}, data);
auto new_tensor = make_tensor_ptr(*tensor);
auto new_tensor = make_tensor_ptr(tensor);

EXPECT_EQ(new_tensor->dim(), tensor->dim());
EXPECT_EQ(new_tensor->size(0), tensor->size(0));
Expand All @@ -360,7 +360,7 @@ TEST_F(TensorPtrTest, MakeTensorPtrFromExistingTensorInt32) {
TEST_F(TensorPtrTest, CloneTensorPtrFromExistingTensorInt32) {
std::vector<int32_t> data = {1, 2, 3, 4};
auto tensor = make_tensor_ptr({2, 2}, std::move(data));
auto cloned_tensor = clone_tensor_ptr(*tensor);
auto cloned_tensor = clone_tensor_ptr(tensor);

EXPECT_EQ(cloned_tensor->dim(), tensor->dim());
EXPECT_EQ(cloned_tensor->size(0), tensor->size(0));
Expand All @@ -373,6 +373,56 @@ TEST_F(TensorPtrTest, CloneTensorPtrFromExistingTensorInt32) {
EXPECT_EQ(cloned_tensor->scalar_type(), executorch::aten::ScalarType::Int);
}

TEST_F(TensorPtrTest, MakeTensorPtrFromTensorPtrInt32) {
std::vector<int32_t> data = {1, 2, 3, 4};
auto tensor = make_tensor_ptr({2, 2}, data);
auto new_tensor = make_tensor_ptr(tensor);

EXPECT_EQ(new_tensor->dim(), tensor->dim());
EXPECT_EQ(new_tensor->size(0), tensor->size(0));
EXPECT_EQ(new_tensor->size(1), tensor->size(1));
EXPECT_EQ(
new_tensor->const_data_ptr<int32_t>(), tensor->const_data_ptr<int32_t>());
EXPECT_EQ(new_tensor->scalar_type(), executorch::aten::ScalarType::Int);
}

TEST_F(TensorPtrTest, MakeTensorPtrFromTensorPtrDouble) {
std::vector<double> data = {1.0, 2.0, 3.0, 4.0};
auto tensor = make_tensor_ptr({2, 2}, data);
auto new_tensor = make_tensor_ptr(tensor);

EXPECT_EQ(new_tensor->dim(), tensor->dim());
EXPECT_EQ(new_tensor->size(0), tensor->size(0));
EXPECT_EQ(new_tensor->size(1), tensor->size(1));
EXPECT_EQ(
new_tensor->const_data_ptr<double>(), tensor->const_data_ptr<double>());
EXPECT_EQ(new_tensor->scalar_type(), executorch::aten::ScalarType::Double);
}

TEST_F(TensorPtrTest, MakeTensorPtrFromTensorPtrInt64) {
std::vector<int64_t> data = {100, 200, 300, 400};
auto tensor = make_tensor_ptr({2, 2}, data);
auto new_tensor = make_tensor_ptr(tensor);

EXPECT_EQ(new_tensor->dim(), tensor->dim());
EXPECT_EQ(new_tensor->size(0), tensor->size(0));
EXPECT_EQ(new_tensor->size(1), tensor->size(1));
EXPECT_EQ(
new_tensor->const_data_ptr<int64_t>(), tensor->const_data_ptr<int64_t>());
EXPECT_EQ(new_tensor->scalar_type(), executorch::aten::ScalarType::Long);
}

TEST_F(TensorPtrTest, MakeTensorPtrFromTensorPtrNull) {
auto tensor = make_tensor_ptr({2, 2}, nullptr);
auto new_tensor = make_tensor_ptr(tensor);

EXPECT_EQ(new_tensor->dim(), tensor->dim());
EXPECT_EQ(new_tensor->size(0), tensor->size(0));
EXPECT_EQ(new_tensor->size(1), tensor->size(1));
EXPECT_EQ(new_tensor->const_data_ptr(), tensor->const_data_ptr());
EXPECT_EQ(new_tensor->const_data_ptr(), nullptr);
}

TEST_F(TensorPtrTest, CloneTensorPtrFromTensorPtrInt32) {
std::vector<int32_t> data = {1, 2, 3, 4};
auto tensor = make_tensor_ptr({2, 2}, std::move(data));
Expand All @@ -392,7 +442,7 @@ TEST_F(TensorPtrTest, CloneTensorPtrFromTensorPtrInt32) {
TEST_F(TensorPtrTest, MakeTensorPtrFromExistingTensorDouble) {
std::vector<double> data = {1.0, 2.0, 3.0, 4.0};
auto tensor = make_tensor_ptr({2, 2}, data);
auto new_tensor = make_tensor_ptr(*tensor);
auto new_tensor = make_tensor_ptr(tensor);

EXPECT_EQ(new_tensor->dim(), tensor->dim());
EXPECT_EQ(new_tensor->size(0), tensor->size(0));
Expand All @@ -405,7 +455,7 @@ TEST_F(TensorPtrTest, MakeTensorPtrFromExistingTensorDouble) {
TEST_F(TensorPtrTest, CloneTensorPtrFromExistingTensorDouble) {
std::vector<double> data = {1.0, 2.0, 3.0, 4.0};
auto tensor = make_tensor_ptr({2, 2}, std::move(data));
auto cloned_tensor = clone_tensor_ptr(*tensor);
auto cloned_tensor = clone_tensor_ptr(tensor);

EXPECT_EQ(cloned_tensor->dim(), tensor->dim());
EXPECT_EQ(cloned_tensor->size(0), tensor->size(0));
Expand Down Expand Up @@ -437,7 +487,7 @@ TEST_F(TensorPtrTest, CloneTensorPtrFromTensorPtrDouble) {
TEST_F(TensorPtrTest, MakeTensorPtrFromExistingTensorInt64) {
std::vector<int64_t> data = {100, 200, 300, 400};
auto tensor = make_tensor_ptr({2, 2}, data);
auto new_tensor = make_tensor_ptr(*tensor);
auto new_tensor = make_tensor_ptr(tensor);

EXPECT_EQ(new_tensor->dim(), tensor->dim());
EXPECT_EQ(new_tensor->size(0), tensor->size(0));
Expand All @@ -450,7 +500,7 @@ TEST_F(TensorPtrTest, MakeTensorPtrFromExistingTensorInt64) {
TEST_F(TensorPtrTest, CloneTensorPtrFromExistingTensorInt64) {
std::vector<int64_t> data = {100, 200, 300, 400};
auto tensor = make_tensor_ptr({2, 2}, std::move(data));
auto cloned_tensor = clone_tensor_ptr(*tensor);
auto cloned_tensor = clone_tensor_ptr(tensor);

EXPECT_EQ(cloned_tensor->dim(), tensor->dim());
EXPECT_EQ(cloned_tensor->size(0), tensor->size(0));
Expand Down
Loading