6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
- #include < iostream>
10
-
11
9
#include < optional>
12
10
#include < string_view>
13
11
#include < utility>
14
- #include < memory>
15
12
16
13
#include " IRModule.h"
17
14
@@ -995,7 +992,7 @@ class PyDenseElementsAttribute
995
992
} else if (format == " ?" ) {
996
993
// i1
997
994
// The i1 type needs to be bit-packed, so we will handle it seperately
998
- return getAttributeFromBufferBoolean (view, explicitShape, context);
995
+ return getAttributeFromBufferBoolBitpack (view, explicitShape, context);
999
996
} else if (isSignedIntegerFormat (format)) {
1000
997
if (view.itemsize == 4 ) {
1001
998
// i32
@@ -1047,17 +1044,21 @@ class PyDenseElementsAttribute
1047
1044
}
1048
1045
}
1049
1046
1050
- return mlirDenseElementsAttrRawBufferGet (getShapedType (bulkLoadElementType, explicitShape, view), view.len , view.buf );
1047
+ MlirType type = getShapedType (bulkLoadElementType, explicitShape, view);
1048
+ return mlirDenseElementsAttrRawBufferGet (type, view.len , view.buf );
1051
1049
}
1052
1050
1053
- static MlirAttribute getAttributeFromBufferBoolean (Py_buffer& view,
1054
- std::optional<std::vector<int64_t >> explicitShape,
1055
- MlirContext& context) {
1051
+ // There is a complication for boolean numpy arrays, as numpy represent them as
1052
+ // 8 bits per boolean, whereas MLIR bitpacks them into 8 booleans per byte.
1053
+ // This function does the bit-packing respecting endianess.
1054
+ static MlirAttribute getAttributeFromBufferBoolBitpack (Py_buffer& view,
1055
+ std::optional<std::vector<int64_t >> explicitShape,
1056
+ MlirContext& context) {
1056
1057
// First read the content of the python buffer as u8's, to correct for endianess
1057
- MlirAttribute intermediateAttr = mlirDenseElementsAttrRawBufferGet (
1058
- getShapedType ( mlirIntegerTypeUnsignedGet (context, 8 ), explicitShape, view) , view.len , view.buf );
1058
+ MlirType byteType = getShapedType ( mlirIntegerTypeUnsignedGet (context, 8 ), explicitShape, view);
1059
+ MlirAttribute intermediateAttr = mlirDenseElementsAttrRawBufferGet (byteType , view.len , view.buf );
1059
1060
1060
- // Pack the boolean array according to the i8 bitpacking layout
1061
+ // Pack the boolean array according to the i1 bitpacking layout
1061
1062
const int numPackedBytes = (view.len + 7 ) / 8 ;
1062
1063
SmallVector<uint8_t , 8 > bitpacked (numPackedBytes);
1063
1064
for (int byteNum = 0 ; byteNum < numPackedBytes; byteNum++) {
@@ -1070,8 +1071,8 @@ class PyDenseElementsAttribute
1070
1071
bitpacked[byteNum] = byte;
1071
1072
}
1072
1073
1073
- return mlirDenseElementsAttrRawBufferGet ( getShapedType (
1074
- mlirIntegerTypeGet (context, 1 ), explicitShape, view) , numPackedBytes, bitpacked.data ());
1074
+ MlirType bitpackedType = getShapedType (mlirIntegerTypeGet (context, 1 ), explicitShape, view);
1075
+ return mlirDenseElementsAttrRawBufferGet (bitpackedType , numPackedBytes, bitpacked.data ());
1075
1076
}
1076
1077
1077
1078
template <typename Type>
@@ -1145,7 +1146,6 @@ class PyDenseIntElementsAttribute
1145
1146
bool isUnsigned = mlirIntegerTypeIsUnsigned (type);
1146
1147
if (isUnsigned) {
1147
1148
if (width == 1 ) {
1148
- std::cerr << " Loading unsigned i1 values at position: " << pos << std::endl;
1149
1149
return mlirDenseElementsAttrGetBoolValue (*this , pos);
1150
1150
}
1151
1151
if (width == 8 ) {
@@ -1162,7 +1162,6 @@ class PyDenseIntElementsAttribute
1162
1162
}
1163
1163
} else {
1164
1164
if (width == 1 ) {
1165
- std::cerr << " Loading signed i1 values at position: " << pos << std::endl;
1166
1165
return mlirDenseElementsAttrGetBoolValue (*this , pos);
1167
1166
}
1168
1167
if (width == 8 ) {
0 commit comments