[Mlir-commits] [mlir] [mlir][pybind] Support loading DenseElementsAttr of bools (PR #82868)

Rob Suderman llvmlistbot at llvm.org
Mon Feb 26 12:04:22 PST 2024


https://github.com/rsuderman updated https://github.com/llvm/llvm-project/pull/82868

>From dba584884881de2ab88af479621367a9d7ef4a8a Mon Sep 17 00:00:00 2001
From: Rob Suderman <rob.suderman at gmail.com>
Date: Fri, 23 Feb 2024 23:01:53 -0800
Subject: [PATCH 1/5] [mlir][pybind] Support loading DenseElementsAttr of bools

We were missing the python binding support for loading tensors of
booleans from numpy arrays.
---
 mlir/lib/Bindings/Python/IRAttributes.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index dda2003ba0375a..3bf305c1bbb3e9 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -694,6 +694,8 @@ class PyDenseElementsAttribute
         // f16
         assert(view.itemsize == 2 && "mismatched array itemsize");
         bulkLoadElementType = mlirF16TypeGet(context);
+      // } else if (format == "?") {
+      //     bulkLoadElementType = mlirIntegerTypeGet(context, 1);
       } else if (isSignedIntegerFormat(format)) {
         if (view.itemsize == 4) {
           // i32

>From c4d730653f7343a6971a75b524bf1b5c6ea87f4c Mon Sep 17 00:00:00 2001
From: Rob Suderman <rob.suderman at gmail.com>
Date: Mon, 26 Feb 2024 10:27:40 -0800
Subject: [PATCH 2/5] add i1 test

---
 mlir/lib/Bindings/Python/IRAttributes.cpp | 4 ++--
 mlir/test/python/ir/array_attributes.py   | 8 ++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 3bf305c1bbb3e9..1cc7f8b63bd356 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -694,8 +694,8 @@ class PyDenseElementsAttribute
         // f16
         assert(view.itemsize == 2 && "mismatched array itemsize");
         bulkLoadElementType = mlirF16TypeGet(context);
-      // } else if (format == "?") {
-      //     bulkLoadElementType = mlirIntegerTypeGet(context, 1);
+      } else if (format == "?") {
+          bulkLoadElementType = mlirIntegerTypeGet(context, 1);
       } else if (isSignedIntegerFormat(format)) {
         if (view.itemsize == 4) {
           // i32
diff --git a/mlir/test/python/ir/array_attributes.py b/mlir/test/python/ir/array_attributes.py
index 9251588a4c48a6..d47c26ba8132ca 100644
--- a/mlir/test/python/ir/array_attributes.py
+++ b/mlir/test/python/ir/array_attributes.py
@@ -148,6 +148,14 @@ def testNonSplat():
 
 ### explicitly provided types
 
+ at run
+def testGetDenseElementsI1():
+    with Context():
+        array = np.array([[True, False, True], [False, False, True]], dtype=np._bool)
+        attr = DenseElementsAttr.get(array, type=IntegerType.get_signless(1))
+        # CHECK: dense<{{\[}}[true, false, true], [false, false, true]]> : tensor<2x3xi1>
+        print(attr)
+
 
 @run
 def testGetDenseElementsBF16():

>From 6a5a923ec1a046d3002ada500299f800758016c4 Mon Sep 17 00:00:00 2001
From: Rob Suderman <rob.suderman at gmail.com>
Date: Mon, 26 Feb 2024 11:09:29 -0800
Subject: [PATCH 3/5] git-clang-format

---
 mlir/lib/Bindings/Python/IRAttributes.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Bindings/Python/IRAttributes.cpp b/mlir/lib/Bindings/Python/IRAttributes.cpp
index 1cc7f8b63bd356..0f843b78bfb71d 100644
--- a/mlir/lib/Bindings/Python/IRAttributes.cpp
+++ b/mlir/lib/Bindings/Python/IRAttributes.cpp
@@ -695,7 +695,7 @@ class PyDenseElementsAttribute
         assert(view.itemsize == 2 && "mismatched array itemsize");
         bulkLoadElementType = mlirF16TypeGet(context);
       } else if (format == "?") {
-          bulkLoadElementType = mlirIntegerTypeGet(context, 1);
+        bulkLoadElementType = mlirIntegerTypeGet(context, 1);
       } else if (isSignedIntegerFormat(format)) {
         if (view.itemsize == 4) {
           // i32

>From 6413db26933a6d2e4ff8706ffd40b02402a45841 Mon Sep 17 00:00:00 2001
From: Rob Suderman <rob.suderman at gmail.com>
Date: Mon, 26 Feb 2024 11:42:04 -0800
Subject: [PATCH 4/5] array attribute formatting fix

---
 mlir/test/python/ir/array_attributes.py | 1 +
 1 file changed, 1 insertion(+)

diff --git a/mlir/test/python/ir/array_attributes.py b/mlir/test/python/ir/array_attributes.py
index d47c26ba8132ca..fc751d859f2ab7 100644
--- a/mlir/test/python/ir/array_attributes.py
+++ b/mlir/test/python/ir/array_attributes.py
@@ -148,6 +148,7 @@ def testNonSplat():
 
 ### explicitly provided types
 
+
 @run
 def testGetDenseElementsI1():
     with Context():

>From 2051caead6ca34c8e1d63fa16ce6c1da2d6b8d1b Mon Sep 17 00:00:00 2001
From: Rob Suderman <rob.suderman at gmail.com>
Date: Mon, 26 Feb 2024 12:04:01 -0800
Subject: [PATCH 5/5] fixed typename

---
 mlir/test/python/ir/array_attributes.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/test/python/ir/array_attributes.py b/mlir/test/python/ir/array_attributes.py
index fc751d859f2ab7..360e823fc010c4 100644
--- a/mlir/test/python/ir/array_attributes.py
+++ b/mlir/test/python/ir/array_attributes.py
@@ -152,7 +152,7 @@ def testNonSplat():
 @run
 def testGetDenseElementsI1():
     with Context():
-        array = np.array([[True, False, True], [False, False, True]], dtype=np._bool)
+        array = np.array([[True, False, True], [False, False, True]], dtype=np.bool_)
         attr = DenseElementsAttr.get(array, type=IntegerType.get_signless(1))
         # CHECK: dense<{{\[}}[true, false, true], [false, false, true]]> : tensor<2x3xi1>
         print(attr)



More information about the Mlir-commits mailing list