Skip to content
Open
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: 4 additions & 2 deletions onnxruntime/core/framework/onnxruntime_map_type_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ constexpr ONNXTensorElementDataType ToONNXTensorElementDataType(
consteval bool IsTensorProtoToOrtElementTypeMapBijective() {
constexpr size_t ort_element_type_count =
static_cast<size_t>(ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT8E8M0) + 1;
constexpr size_t onnx_element_type_count = ONNX_NAMESPACE::TensorProto_DataType_DataType_ARRAYSIZE;
// Newer ONNX versions may define types that do not have an ORT counterpart yet.
constexpr size_t onnx_element_type_count =
static_cast<size_t>(ONNX_NAMESPACE::TensorProto_DataType_INT2) + 1;
if constexpr (onnx_element_type_count != ort_element_type_count) {
return false;
}
Expand All @@ -55,7 +57,7 @@ consteval bool IsTensorProtoToOrtElementTypeMapBijective() {
}

static_assert(IsTensorProtoToOrtElementTypeMapBijective(),
"ONNX and ORT tensor element type enums must have a complete one-to-one mapping.");
"Supported ONNX and ORT tensor element types must have a complete one-to-one mapping.");

} // namespace type_info_internal
} // namespace onnxruntime
Expand Down
5 changes: 3 additions & 2 deletions onnxruntime/core/framework/tensorprotoutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace onnxruntime {
namespace utils {
namespace detail {

inline constexpr std::array<uint8_t, ONNX_NAMESPACE::TensorProto_DataType_DataType_ARRAYSIZE>
// Newer ONNX versions may define types that ONNX Runtime does not support yet.
inline constexpr std::array<uint8_t, static_cast<size_t>(ONNX_NAMESPACE::TensorProto_DataType_INT2) + 1>
kTensorProtoDataTypeElementSizes{
0, // UNDEFINED
sizeof(float), // FLOAT
Expand Down Expand Up @@ -74,7 +75,7 @@ consteval bool IsTensorProtoDataTypeElementSizeMapComplete() {
}

static_assert(IsTensorProtoDataTypeElementSizeMapComplete(),
"TensorProto data type element-size map must cover every numeric data type.");
"TensorProto data type element-size map must cover every supported numeric data type.");

} // namespace detail

Expand Down
8 changes: 7 additions & 1 deletion onnxruntime/test/framework/tensorutils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace onnxruntime {
namespace test {

constexpr bool TensorProtoElementSizesAreConstexpr() {
constexpr std::array<size_t, TensorProto_DataType_DataType_ARRAYSIZE> expected_sizes{
constexpr std::array<size_t, static_cast<size_t>(TensorProto_DataType_INT2) + 1> expected_sizes{
0, // UNDEFINED
sizeof(float), // FLOAT
sizeof(uint8_t), // UINT8
Expand Down Expand Up @@ -69,6 +69,12 @@ constexpr bool TensorProtoElementSizesAreConstexpr() {
}
}

for (size_t index = expected_sizes.size(); index < TensorProto_DataType_DataType_ARRAYSIZE; ++index) {
if (GetElementSizeOfTensor(static_cast<TensorProto_DataType>(index)) != 0) {
return false;
}
}

return GetElementSizeOfTensor(static_cast<TensorProto_DataType>(-1)) == 0 &&
GetElementSizeOfTensor(static_cast<TensorProto_DataType>(expected_sizes.size())) == 0;
}
Expand Down
15 changes: 13 additions & 2 deletions onnxruntime/test/framework/type_info_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace test {
namespace mb = modelbuilder;

constexpr bool TensorElementTypeConversionIsConstexpr() {
constexpr std::array<ONNXTensorElementDataType, ONNX_NAMESPACE::TensorProto_DataType_DataType_ARRAYSIZE>
constexpr std::array<ONNXTensorElementDataType, static_cast<size_t>(ONNX_NAMESPACE::TensorProto_DataType_INT2) + 1>
expected_types{
ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED,
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT,
Expand Down Expand Up @@ -60,6 +60,14 @@ constexpr bool TensorElementTypeConversionIsConstexpr() {
}
}

for (size_t index = expected_types.size(); index < ONNX_NAMESPACE::TensorProto_DataType_DataType_ARRAYSIZE; ++index) {
if (type_info_internal::ToONNXTensorElementDataType(
static_cast<ONNX_NAMESPACE::TensorProto_DataType>(index)) != ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED ||
utils::ToOrtTensorElementDataType(static_cast<int>(index)) != ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED) {
return false;
}
}

return ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT2 == 24 &&
ONNX_TENSOR_ELEMENT_DATA_TYPE_INT2 == 25 &&
ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT8E8M0 == 26 &&
Expand Down Expand Up @@ -106,14 +114,17 @@ TEST(TypeInfoTests, TensorElementTypeConversions) {
{ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT2, ONNX_NAMESPACE::TensorProto_DataType_UINT2},
{ONNX_TENSOR_ELEMENT_DATA_TYPE_INT2, ONNX_NAMESPACE::TensorProto_DataType_INT2},
};
static_assert(std::size(types) == ONNX_NAMESPACE::TensorProto_DataType_DataType_ARRAYSIZE);
static_assert(std::size(types) == static_cast<size_t>(ONNX_NAMESPACE::TensorProto_DataType_INT2) + 1);
for (const auto& [api_type, proto_type] : types) {
SCOPED_TRACE(static_cast<int>(api_type));
EXPECT_EQ(utils::ToTensorProtoElementType(api_type), proto_type);
EXPECT_EQ(utils::ToOrtTensorElementDataType(proto_type), api_type);
EXPECT_EQ(utils::ToOrtTensorElementDataType(utils::ToTensorProtoElementType(api_type)), api_type);
}
EXPECT_EQ(utils::ToOrtTensorElementDataType(-1), ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED);
for (size_t index = std::size(types); index < ONNX_NAMESPACE::TensorProto_DataType_DataType_ARRAYSIZE; ++index) {
EXPECT_EQ(utils::ToOrtTensorElementDataType(static_cast<int>(index)), ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED);
}
EXPECT_EQ(utils::ToOrtTensorElementDataType(ONNX_NAMESPACE::TensorProto_DataType_DataType_ARRAYSIZE),
ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED);
}
Expand Down