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
2 changes: 1 addition & 1 deletion extension/apple/ExecuTorch/Exported/ExecuTorchTensor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ - (NSString *)description {
ET_CHECK_MSG(false, "Unsupported dtype in description");
}
} ctx;
ET_SWITCH_REALHBBF16_TYPES(
ET_SWITCH_REALHBBF16_AND_UINT_TYPES(
static_cast<ScalarType>(_tensor->scalar_type()),
ctx,
"description",
Expand Down
15 changes: 8 additions & 7 deletions extension/tensor/tensor_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ inline TensorPtr make_tensor_ptr(
}
} ctx;

ET_SWITCH_REALHBBF16_TYPES(type, ctx, "make_tensor_ptr", CTYPE, [&] {
std::transform(
data.begin(),
data.end(),
reinterpret_cast<CTYPE*>(casted_data.data()),
[](const T& val) { return static_cast<CTYPE>(val); });
});
ET_SWITCH_REALHBBF16_AND_UINT_TYPES(
type, ctx, "make_tensor_ptr", CTYPE, [&] {
std::transform(
data.begin(),
data.end(),
reinterpret_cast<CTYPE*>(casted_data.data()),
[](const T& val) { return static_cast<CTYPE>(val); });
});
const auto raw_data_ptr = casted_data.data();
auto data_ptr =
std::make_shared<std::vector<uint8_t>>(std::move(casted_data));
Expand Down
4 changes: 2 additions & 2 deletions extension/tensor/tensor_ptr_maker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ TensorPtr random_strided(
}
} ctx;

ET_SWITCH_REALHBBF16_TYPES(type, ctx, "random_strided", CTYPE, [&] {
ET_SWITCH_REALHBBF16_AND_UINT_TYPES(type, ctx, "random_strided", CTYPE, [&] {
std::generate_n(tensor->mutable_data_ptr<CTYPE>(), tensor->numel(), [&]() {
return static_cast<CTYPE>(distribution(gen));
});
Expand Down Expand Up @@ -138,7 +138,7 @@ TensorPtr full_strided(
}
} ctx;

ET_SWITCH_REALHBBF16_TYPES(type, ctx, "full_strided", CTYPE, [&] {
ET_SWITCH_REALHBBF16_AND_UINT_TYPES(type, ctx, "full_strided", CTYPE, [&] {
CTYPE value;
ET_EXTRACT_SCALAR(fill_value, value);
std::fill(
Expand Down
164 changes: 164 additions & 0 deletions extension/tensor/test/tensor_ptr_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1073,3 +1073,167 @@ TEST_F(TensorPtrTest, TensorDataCastingInvalidCast) {
},
"");
}

TEST_F(TensorPtrTest, TensorDataOnlyUInt16Type) {
std::vector<uint16_t> data = {1u, 65535u, 42u, 0u};
auto tensor = make_tensor_ptr(std::move(data));
EXPECT_EQ(tensor->dim(), 1);
EXPECT_EQ(tensor->size(0), 4);
EXPECT_EQ(tensor->strides()[0], 1);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::UInt16);
auto ptr = tensor->const_data_ptr<uint16_t>();
EXPECT_EQ(ptr[0], 1u);
EXPECT_EQ(ptr[1], 65535u);
EXPECT_EQ(ptr[2], 42u);
EXPECT_EQ(ptr[3], 0u);
}

TEST_F(TensorPtrTest, TensorDataOnlyUInt32Type) {
std::vector<uint32_t> data = {0u, 123u, 4000000000u};
auto tensor = make_tensor_ptr(std::move(data));
EXPECT_EQ(tensor->dim(), 1);
EXPECT_EQ(tensor->size(0), 3);
EXPECT_EQ(tensor->strides()[0], 1);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::UInt32);
auto ptr = tensor->const_data_ptr<uint32_t>();
EXPECT_EQ(ptr[0], 0u);
EXPECT_EQ(ptr[1], 123u);
EXPECT_EQ(ptr[2], 4000000000u);
}

TEST_F(TensorPtrTest, TensorDataOnlyUInt64Type) {
std::vector<uint64_t> data = {0ull, 1ull, 9000000000000000000ull};
auto tensor = make_tensor_ptr(std::move(data));
EXPECT_EQ(tensor->dim(), 1);
EXPECT_EQ(tensor->size(0), 3);
EXPECT_EQ(tensor->strides()[0], 1);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::UInt64);
auto ptr = tensor->const_data_ptr<uint64_t>();
EXPECT_EQ(ptr[0], 0ull);
EXPECT_EQ(ptr[1], 1ull);
EXPECT_EQ(ptr[2], 9000000000000000000ull);
}

TEST_F(TensorPtrTest, TensorUint8dataUInt32Type) {
std::vector<uint32_t> values = {1u, 4000000000u, 123u};
const auto* bytes = reinterpret_cast<const uint8_t*>(values.data());
std::vector<uint8_t> raw(bytes, bytes + values.size() * sizeof(uint32_t));
auto tensor = make_tensor_ptr(
{3}, std::move(raw), executorch::aten::ScalarType::UInt32);
EXPECT_EQ(tensor->dim(), 1);
EXPECT_EQ(tensor->size(0), 3);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::UInt32);
auto ptr = tensor->const_data_ptr<uint32_t>();
EXPECT_EQ(ptr[0], 1u);
EXPECT_EQ(ptr[1], 4000000000u);
EXPECT_EQ(ptr[2], 123u);
}

TEST_F(TensorPtrTest, TensorUint8dataUInt64Type) {
std::vector<uint64_t> values = {0ull, 42ull, 9000000000000000000ull};
const auto* bytes = reinterpret_cast<const uint8_t*>(values.data());
std::vector<uint8_t> raw(bytes, bytes + values.size() * sizeof(uint64_t));
auto tensor = make_tensor_ptr(
{3}, std::move(raw), executorch::aten::ScalarType::UInt64);
EXPECT_EQ(tensor->dim(), 1);
EXPECT_EQ(tensor->size(0), 3);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::UInt64);
auto ptr = tensor->const_data_ptr<uint64_t>();
EXPECT_EQ(ptr[0], 0ull);
EXPECT_EQ(ptr[1], 42ull);
EXPECT_EQ(ptr[2], 9000000000000000000ull);
}

TEST_F(TensorPtrTest, TensorUint8dataSizeMismatchUInt32ExpectDeath) {
std::vector<uint8_t> data(
3 * executorch::aten::elementSize(executorch::aten::ScalarType::UInt32) -
1);
ET_EXPECT_DEATH({ auto _ = make_tensor_ptr({3}, std::move(data)); }, "");
}

TEST_F(TensorPtrTest, TensorUint8dataSizeMismatchUInt64ExpectDeath) {
std::vector<uint8_t> data(
2 * executorch::aten::elementSize(executorch::aten::ScalarType::UInt64) +
1);
ET_EXPECT_DEATH({ auto _ = make_tensor_ptr({2}, std::move(data)); }, "");
}

TEST_F(TensorPtrTest, TensorDataCastingFromInt32ToUInt16) {
std::vector<int32_t> data = {-1, 65535, 65536, -65536};
auto tensor =
make_tensor_ptr(std::move(data), executorch::aten::ScalarType::UInt16);
EXPECT_EQ(tensor->dim(), 1);
EXPECT_EQ(tensor->size(0), 4);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::UInt16);
auto ptr = tensor->const_data_ptr<uint16_t>();
EXPECT_EQ(ptr[0], static_cast<uint16_t>(-1));
EXPECT_EQ(ptr[1], static_cast<uint16_t>(65535));
EXPECT_EQ(ptr[2], static_cast<uint16_t>(65536));
EXPECT_EQ(ptr[3], static_cast<uint16_t>(-65536));
}

TEST_F(TensorPtrTest, TensorDataCastingFromUInt32ToFloat) {
std::vector<uint32_t> data = {0u, 123u, 4000000000u};
auto tensor =
make_tensor_ptr(std::move(data), executorch::aten::ScalarType::Float);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::Float);
auto ptr = tensor->const_data_ptr<float>();
EXPECT_FLOAT_EQ(ptr[0], 0.0f);
EXPECT_FLOAT_EQ(ptr[1], 123.0f);
EXPECT_FLOAT_EQ(ptr[2], 4000000000.0f);
}

TEST_F(TensorPtrTest, TensorDataCastingFromFloatToUInt32) {
std::vector<float> data = {1.0f, 2.0f};
auto tensor =
make_tensor_ptr(std::move(data), executorch::aten::ScalarType::UInt32);

EXPECT_EQ(tensor->dim(), 1);
EXPECT_EQ(tensor->size(0), 2);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::UInt32);

auto ptr = tensor->const_data_ptr<uint32_t>();
EXPECT_EQ(ptr[0], 1u);
EXPECT_EQ(ptr[1], 2u);
}

TEST_F(TensorPtrTest, MakeTensorPtrFromExistingTensorUInt32) {
std::vector<uint32_t> data = {10u, 20u, 30u, 40u};
auto tensor = make_tensor_ptr({2, 2}, data);
auto alias = make_tensor_ptr(tensor);
EXPECT_EQ(alias->dim(), 2);
EXPECT_EQ(alias->size(0), 2);
EXPECT_EQ(alias->size(1), 2);
EXPECT_EQ(alias->scalar_type(), executorch::aten::ScalarType::UInt32);
EXPECT_EQ(
alias->const_data_ptr<uint32_t>(), tensor->const_data_ptr<uint32_t>());
}

TEST_F(TensorPtrTest, CloneTensorPtrFromExistingTensorUInt32) {
std::vector<uint32_t> data = {10u, 20u, 30u, 40u};
auto tensor = make_tensor_ptr({2, 2}, std::move(data));
auto cloned = clone_tensor_ptr(tensor);
EXPECT_EQ(cloned->dim(), 2);
EXPECT_EQ(cloned->size(0), 2);
EXPECT_EQ(cloned->size(1), 2);
EXPECT_EQ(cloned->scalar_type(), executorch::aten::ScalarType::UInt32);
EXPECT_NE(
cloned->const_data_ptr<uint32_t>(), tensor->const_data_ptr<uint32_t>());
auto ptr = cloned->const_data_ptr<uint32_t>();
EXPECT_EQ(ptr[0], 10u);
EXPECT_EQ(ptr[3], 40u);
}

TEST_F(TensorPtrTest, Tensor2DUInt16OwningData) {
std::vector<uint16_t> data = {1u, 2u, 3u, 4u, 5u, 6u};
auto tensor = make_tensor_ptr({2, 3}, std::move(data));
EXPECT_EQ(tensor->dim(), 2);
EXPECT_EQ(tensor->size(0), 2);
EXPECT_EQ(tensor->size(1), 3);
EXPECT_EQ(tensor->strides()[0], 3);
EXPECT_EQ(tensor->strides()[1], 1);
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::UInt16);
auto ptr = tensor->const_data_ptr<uint16_t>();
EXPECT_EQ(ptr[0], 1u);
EXPECT_EQ(ptr[5], 6u);
}
Loading