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
18 changes: 18 additions & 0 deletions backends/aoti/slim/c10/core/ScalarType.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ inline bool isBoolType(ScalarType t) {
return t == ScalarType::Bool;
}

/// Checks if the scalar type is a valid/supported type.
/// @param t The scalar type to check.
/// @return true if the scalar type is valid, false otherwise.
inline bool isValidScalarType(ScalarType t) {
switch (t) {
case ScalarType::Char:
case ScalarType::Short:
case ScalarType::Int:
case ScalarType::Long:
case ScalarType::Float:
case ScalarType::Bool:
case ScalarType::BFloat16:
return true;
default:
return false;
}
}

inline std::ostream& operator<<(std::ostream& stream, ScalarType scalar_type) {
return stream << toString(scalar_type);
}
Expand Down
9 changes: 9 additions & 0 deletions backends/aoti/slim/core/slim_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SlimTensor {
storage_offset_(storage_offset),
dtype_(dtype) {
set_sizes_and_strides(sizes, strides);
check_supportive();
}

/**
Expand All @@ -65,6 +66,7 @@ class SlimTensor {
is_contiguous_(true) {
sizes_and_strides_.set_sizes({0});
sizes_and_strides_.set_strides({1});
check_supportive();
}

// Default copy/move operations
Expand Down Expand Up @@ -556,6 +558,13 @@ class SlimTensor {
static_cast<int64_t>(numel_));
}

void check_supportive() const {
ET_CHECK_MSG(
c10::isValidScalarType(dtype_),
"invalid dtype %d",
static_cast<int>(dtype_));
}

Storage storage_;
int64_t storage_offset_{0};
c10::SizesAndStrides sizes_and_strides_;
Expand Down
6 changes: 5 additions & 1 deletion backends/aoti/slim/core/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ class MaybeOwningStorage {
data_(data),
capacity_(nbytes),
deleter_(detail::noop),
is_owning_(false) {}
is_owning_(false) {
if (!device.is_cuda() && !device.is_cpu()) {
ET_CHECK_MSG(false, "Unsupported device type: %s", device.str().c_str());
}
}

/// Default constructor is deleted - storage must have a device.
MaybeOwningStorage() = delete;
Expand Down
34 changes: 34 additions & 0 deletions backends/aoti/slim/core/test/test_slimtensor_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,38 @@ TEST(SlimTensorBasicTest, DataPtrWithOffset) {
EXPECT_EQ(data, static_cast<char*>(base) + 5 * sizeof(float));
}

// =============================================================================
// Dtype and Device Type Validation Tests
// =============================================================================

TEST(SlimTensorValidationTest, InvalidDtypeUndefined) {
std::vector<int64_t> sizes = {2, 3};
std::vector<int64_t> strides = {3, 1};
size_t nbytes = 6 * sizeof(float);
Storage storage = make_cpu_storage(nbytes);

EXPECT_DEATH(
SlimTensor(
std::move(storage),
makeArrayRef(sizes),
makeArrayRef(strides),
c10::ScalarType::Undefined),
"");
}

TEST(SlimTensorValidationTest, InvalidDtypeDouble) {
std::vector<int64_t> sizes = {2, 3};
std::vector<int64_t> strides = {3, 1};
size_t nbytes = 6 * sizeof(double);
Storage storage = make_cpu_storage(nbytes);

EXPECT_DEATH(
SlimTensor(
std::move(storage),
makeArrayRef(sizes),
makeArrayRef(strides),
static_cast<c10::ScalarType>(7)), // Double = 7
"");
}

} // namespace executorch::backends::aoti::slim
59 changes: 59 additions & 0 deletions backends/aoti/slim/factory/test/test_empty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,65 @@ TEST(EmptyTest, CanWriteAndReadData) {
}
}

// =============================================================================
// Dtype and Device Type Validation Tests
// =============================================================================

TEST(EmptyStridedTest, InvalidDtypeUndefined) {
std::vector<int64_t> sizes = {2, 3};
std::vector<int64_t> strides = {3, 1};

EXPECT_DEATH(
empty_strided(
makeArrayRef(sizes),
makeArrayRef(strides),
c10::ScalarType::Undefined),
"");
}

TEST(EmptyStridedTest, InvalidDtypeDouble) {
std::vector<int64_t> sizes = {2, 3};
std::vector<int64_t> strides = {3, 1};

EXPECT_DEATH(
empty_strided(
makeArrayRef(sizes),
makeArrayRef(strides),
static_cast<c10::ScalarType>(7)), // Double = 7
"");
}

TEST(EmptyStridedTest, InvalidDeviceType) {
std::vector<int64_t> sizes = {2, 3};
std::vector<int64_t> strides = {3, 1};

c10::Device invalid_device(static_cast<c10::DeviceType>(100), 0);

EXPECT_DEATH(
empty_strided(
makeArrayRef(sizes),
makeArrayRef(strides),
c10::ScalarType::Float,
invalid_device),
"");
}

TEST(EmptyTest, InvalidDtypeUndefined) {
EXPECT_DEATH(empty({2, 3}, c10::ScalarType::Undefined), "");
}

TEST(EmptyTest, InvalidDtypeDouble) {
EXPECT_DEATH(
empty({2, 3}, static_cast<c10::ScalarType>(7)), // Double = 7
"");
}

TEST(EmptyTest, InvalidDeviceType) {
c10::Device invalid_device(static_cast<c10::DeviceType>(100), 0);

EXPECT_DEATH(empty({2, 3}, c10::ScalarType::Float, invalid_device), "");
}

#ifdef CUDA_AVAILABLE

// =============================================================================
Expand Down
31 changes: 31 additions & 0 deletions backends/aoti/slim/factory/test/test_from_blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,37 @@ TEST(FromBlobTest, WithArrayRef) {
EXPECT_TRUE(tensor.is_contiguous());
}

// =============================================================================
// Dtype and Device Type Validation Tests
// =============================================================================

TEST(FromBlobTest, InvalidDtypeUndefined) {
constexpr size_t kNumFloats = 6;
float external_data[kNumFloats];

EXPECT_DEATH(
from_blob(external_data, {2, 3}, c10::ScalarType::Undefined), "");
}

TEST(FromBlobTest, InvalidDtypeDouble) {
constexpr size_t kNumFloats = 6;
float external_data[kNumFloats];

EXPECT_DEATH(
from_blob(external_data, {2, 3}, static_cast<c10::ScalarType>(7)), "");
}

TEST(FromBlobTest, InvalidDeviceType) {
constexpr size_t kNumFloats = 6;
float external_data[kNumFloats];

c10::Device invalid_device(static_cast<c10::DeviceType>(100), 0);

EXPECT_DEATH(
from_blob(external_data, {2, 3}, c10::ScalarType::Float, invalid_device),
"");
}

// =============================================================================
// CUDA from_blob Tests
// Tests are skipped at runtime if CUDA hardware is not available.
Expand Down
Loading