From fa1d69776ffdbe03699463fa21080c1aefb241f6 Mon Sep 17 00:00:00 2001 From: Li Baoming <1508269885@qq.com> Date: Tue, 22 Sep 2026 16:18:06 +0800 Subject: [PATCH] feat: add distinct bool data type to InfiniRT --- src/data_type.h | 13 +++++++++---- tests/test_core.cc | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/data_type.h b/src/data_type.h index c71d540..9762508 100644 --- a/src/data_type.h +++ b/src/data_type.h @@ -23,10 +23,11 @@ enum class DataType : std::int8_t { kFloat16, kBFloat16, kFloat32, - kFloat64 + kFloat64, + kBool }; -constexpr ConstexprMap kDataTypeToSize{{{ +constexpr ConstexprMap kDataTypeToSize{{{ {DataType::kInt8, 1}, {DataType::kInt16, 2}, {DataType::kInt32, 4}, @@ -39,9 +40,10 @@ constexpr ConstexprMap kDataTypeToSize{{{ {DataType::kBFloat16, 2}, {DataType::kFloat32, 4}, {DataType::kFloat64, 8}, + {DataType::kBool, 1}, }}}; -constexpr ConstexprMap kDataTypeToDesc{{{ +constexpr ConstexprMap kDataTypeToDesc{{{ {DataType::kInt8, "int8"}, {DataType::kInt16, "int16"}, {DataType::kInt32, "int32"}, @@ -54,9 +56,10 @@ constexpr ConstexprMap kDataTypeToDesc{{{ {DataType::kBFloat16, "bfloat16"}, {DataType::kFloat32, "float32"}, {DataType::kFloat64, "float64"}, + {DataType::kBool, "bool"}, }}}; -constexpr ConstexprMap kStringToDataType{{{ +constexpr ConstexprMap kStringToDataType{{{ {"int8", DataType::kInt8}, {"int16", DataType::kInt16}, {"int32", DataType::kInt32}, @@ -69,6 +72,7 @@ constexpr ConstexprMap kStringToDataType{{{ {"bfloat16", DataType::kBFloat16}, {"float32", DataType::kFloat32}, {"float64", DataType::kFloat64}, + {"bool", DataType::kBool}, }}}; struct Float16 { @@ -164,6 +168,7 @@ using TypeMapType = typename TypeMap::type; }; DEFINE_DATA_TYPE_MAPPING(kUInt8, std::uint8_t) +DEFINE_DATA_TYPE_MAPPING(kBool, bool) DEFINE_DATA_TYPE_MAPPING(kInt8, std::int8_t) DEFINE_DATA_TYPE_MAPPING(kUInt16, std::uint16_t) DEFINE_DATA_TYPE_MAPPING(kInt16, std::int16_t) diff --git a/tests/test_core.cc b/tests/test_core.cc index 4582ffd..d3f080c 100644 --- a/tests/test_core.cc +++ b/tests/test_core.cc @@ -159,6 +159,20 @@ void TestDataType(infini::rt::test::TestContext* context) { "float64", "float64 should have a stable name."); context->ExpectEqual(infini::rt::kStringToDataType.at("uint16"), DataType::kUInt16, "uint16 should parse by name."); + context->Expect(DataType::kBool != DataType::kUInt8, + "bool must be distinct from uint8."); + context->ExpectEqual(infini::rt::kDataTypeToSize.at(DataType::kBool), + std::size_t{1}, "bool should use one byte."); + context->ExpectEqual(infini::rt::kDataTypeToDesc.at(DataType::kBool), "bool", + "bool should have a stable name."); + context->ExpectEqual(infini::rt::kStringToDataType.at("bool"), + DataType::kBool, "bool should parse by name."); + context->ExpectEqual(infini::rt::kStringToDataType.at("uint8"), + DataType::kUInt8, "uint8 should remain distinct."); + context->Expect( + std::is_same_v< + infini::rt::TypeMapType, bool>, + "CPU bool should map to C++ bool."); } void TestTensorViewRanks(infini::rt::test::TestContext* context) {