<feed xmlns='http://www.w3.org/2005/Atom'>
<title>llvm-project.git/mlir/test/python/ir/array_attributes.py, branch main</title>
<subtitle>Unnamed repository; edit this file 'description' to name the repository.
</subtitle>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/'/>
<entry>
<title>[mlir][python] fix PyDenseResourceElementsAttribute finalizer (#150561)</title>
<updated>2025-07-25T12:05:30+00:00</updated>
<author>
<name>Maksim Levental</name>
<email>maksim.levental@gmail.com</email>
</author>
<published>2025-07-25T12:05:30+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=21774489f0a812254c110bebfff3aa9b6c4ad960'/>
<id>21774489f0a812254c110bebfff3aa9b6c4ad960</id>
<content type='text'>
This PR melds https://github.com/llvm/llvm-project/pull/150137 and
https://github.com/llvm/llvm-project/pull/149414 *and* partially reverts
https://github.com/llvm/llvm-project/pull/124832.

The summary is the `PyDenseResourceElementsAttribute` finalizer/deleter
has/had two problems

1. wasn't threadsafe (can be called from a different thread than that
which currently holds the GIL)
2. can be called while the interpreter is "not initialized"

https://github.com/llvm/llvm-project/pull/124832 for some reason decides
to re-initialize the interpreter to avoid case 2 and runs afoul of the
fact that `Py_IsInitialized` can be false during the finalization of the
interpreter itself (e.g., at the end of a script).

I don't know why this decision was made (I missed the PR) but I believe
we should never be calling
[Py_Initialize](https://docs.python.org/3/c-api/init.html#c.Py_Initialize):

&gt; In an application \*\*\*\***embedding Python**\*\*\*\*, this should be
called before using any other Python/C API functions

**but we aren't embedding Python**!

So therefore we will only be in case 2 when the interpreter is being
finalized and in that case we should just leak the buffer.

Note,
[lldb](https://github.com/llvm/llvm-project/blob/548ca9e97673a168023a616d311d901ca04b29a3/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp#L81-L93)
does a similar sort of thing for its finalizers.

Co-authored-by: Anton Korobeynikov &lt;anton@korobeynikov.info&gt;
Co-authored-by: Max Manainen &lt;maximmanainen@gmail.com&gt;

Co-authored-by: Anton Korobeynikov &lt;anton@korobeynikov.info&gt;
Co-authored-by: Max Manainen &lt;maximmanainen@gmail.com&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This PR melds https://github.com/llvm/llvm-project/pull/150137 and
https://github.com/llvm/llvm-project/pull/149414 *and* partially reverts
https://github.com/llvm/llvm-project/pull/124832.

The summary is the `PyDenseResourceElementsAttribute` finalizer/deleter
has/had two problems

1. wasn't threadsafe (can be called from a different thread than that
which currently holds the GIL)
2. can be called while the interpreter is "not initialized"

https://github.com/llvm/llvm-project/pull/124832 for some reason decides
to re-initialize the interpreter to avoid case 2 and runs afoul of the
fact that `Py_IsInitialized` can be false during the finalization of the
interpreter itself (e.g., at the end of a script).

I don't know why this decision was made (I missed the PR) but I believe
we should never be calling
[Py_Initialize](https://docs.python.org/3/c-api/init.html#c.Py_Initialize):

&gt; In an application \*\*\*\***embedding Python**\*\*\*\*, this should be
called before using any other Python/C API functions

**but we aren't embedding Python**!

So therefore we will only be in case 2 when the interpreter is being
finalized and in that case we should just leak the buffer.

Note,
[lldb](https://github.com/llvm/llvm-project/blob/548ca9e97673a168023a616d311d901ca04b29a3/lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp#L81-L93)
does a similar sort of thing for its finalizers.

Co-authored-by: Anton Korobeynikov &lt;anton@korobeynikov.info&gt;
Co-authored-by: Max Manainen &lt;maximmanainen@gmail.com&gt;

Co-authored-by: Anton Korobeynikov &lt;anton@korobeynikov.info&gt;
Co-authored-by: Max Manainen &lt;maximmanainen@gmail.com&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>Reapply "[mlir][python] allow DenseIntElementsAttr for index type (#118947)" (#124804)</title>
<updated>2025-01-29T08:14:37+00:00</updated>
<author>
<name>Matthias Gehre</name>
<email>matthias.gehre@amd.com</email>
</author>
<published>2025-01-29T08:14:37+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=5d3ae5161210c068d01ffba36c8e0761e9971179'/>
<id>5d3ae5161210c068d01ffba36c8e0761e9971179</id>
<content type='text'>
This reapplies #118947 and adapts to nanobind.</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This reapplies #118947 and adapts to nanobind.</pre>
</div>
</content>
</entry>
<entry>
<title>Revert "[mlir][python] allow DenseIntElementsAttr for index type (#118947)"</title>
<updated>2025-01-28T17:35:50+00:00</updated>
<author>
<name>Matthias Gehre</name>
<email>matthias.gehre@amd.com</email>
</author>
<published>2025-01-28T17:35:50+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=1b729c3d70cecd89504927fed56498f851f0d74d'/>
<id>1b729c3d70cecd89504927fed56498f851f0d74d</id>
<content type='text'>
This reverts commit 9dd762e8b10586e749b0ddf3542e5dccf8392395.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This reverts commit 9dd762e8b10586e749b0ddf3542e5dccf8392395.
</pre>
</div>
</content>
</entry>
<entry>
<title>[mlir][python] allow DenseIntElementsAttr for index type (#118947)</title>
<updated>2025-01-28T17:31:58+00:00</updated>
<author>
<name>Matthias Gehre</name>
<email>matthias.gehre@amd.com</email>
</author>
<published>2025-01-28T17:31:58+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=9dd762e8b10586e749b0ddf3542e5dccf8392395'/>
<id>9dd762e8b10586e749b0ddf3542e5dccf8392395</id>
<content type='text'>
Model the `IndexType` as `uint64_t` when converting to a python integer. 

With the python bindings, 
```python
DenseIntElementsAttr(op.attributes["attr"])
```
used to `assert` when `attr` had `index` type like `dense&lt;[1, 2, 3, 4]&gt;
: vector&lt;4xindex&gt;`.

---------

Co-authored-by: Christopher McGirr &lt;christopher.mcgirr@amd.com&gt;
Co-authored-by: Tiago Trevisan Jost &lt;tiago.trevisanjost@amd.com&gt;</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Model the `IndexType` as `uint64_t` when converting to a python integer. 

With the python bindings, 
```python
DenseIntElementsAttr(op.attributes["attr"])
```
used to `assert` when `attr` had `index` type like `dense&lt;[1, 2, 3, 4]&gt;
: vector&lt;4xindex&gt;`.

---------

Co-authored-by: Christopher McGirr &lt;christopher.mcgirr@amd.com&gt;
Co-authored-by: Tiago Trevisan Jost &lt;tiago.trevisanjost@amd.com&gt;</pre>
</div>
</content>
</entry>
<entry>
<title>[MLIR,Python] Support converting boolean numpy arrays to and from mlir attributes (unrevert) (#115481)</title>
<updated>2024-11-13T06:23:10+00:00</updated>
<author>
<name>Kasper Nielsen</name>
<email>kasper0406@gmail.com</email>
</author>
<published>2024-11-13T06:23:10+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=1824e45cd799a19fb9b5f9a84f9a0197157af8c8'/>
<id>1824e45cd799a19fb9b5f9a84f9a0197157af8c8</id>
<content type='text'>
This PR re-introduces the functionality of
https://github.com/llvm/llvm-project/pull/113064, which was reverted in
https://github.com/llvm/llvm-project/commit/0a68171b3c67503f7143856580f1b22a93ef566e
due to memory lifetime issues.

Notice that I was not able to re-produce the ASan results myself, so I
have not been able to verify that this PR really fixes the issue.

---

Currently it is unsupported to:
1. Convert a MlirAttribute with type i1 to a numpy array
2. Convert a boolean numpy array to a MlirAttribute

Currently the entire Python application violently crashes with a quite
poor error message https://github.com/pybind/pybind11/issues/3336

The complication handling these conversions, is that MlirAttribute
represent booleans as a bit-packed i1 type, whereas numpy represents
booleans as a byte array with 8 bit used per boolean.

This PR proposes the following approach:
1. When converting a i1 typed MlirAttribute to a numpy array, we can not
directly use the underlying raw data backing the MlirAttribute as a
buffer to Python, as done for other types. Instead, a copy of the data
is generated using numpy's unpackbits function, and the result is send
back to Python.
2. When constructing a MlirAttribute from a numpy array, first the
python data is read as a uint8_t to get it converted to the endianess
used internally in mlir. Then the booleans are bitpacked using numpy's
bitpack function, and the bitpacked array is saved as the MlirAttribute
representation.</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This PR re-introduces the functionality of
https://github.com/llvm/llvm-project/pull/113064, which was reverted in
https://github.com/llvm/llvm-project/commit/0a68171b3c67503f7143856580f1b22a93ef566e
due to memory lifetime issues.

Notice that I was not able to re-produce the ASan results myself, so I
have not been able to verify that this PR really fixes the issue.

---

Currently it is unsupported to:
1. Convert a MlirAttribute with type i1 to a numpy array
2. Convert a boolean numpy array to a MlirAttribute

Currently the entire Python application violently crashes with a quite
poor error message https://github.com/pybind/pybind11/issues/3336

The complication handling these conversions, is that MlirAttribute
represent booleans as a bit-packed i1 type, whereas numpy represents
booleans as a byte array with 8 bit used per boolean.

This PR proposes the following approach:
1. When converting a i1 typed MlirAttribute to a numpy array, we can not
directly use the underlying raw data backing the MlirAttribute as a
buffer to Python, as done for other types. Instead, a copy of the data
is generated using numpy's unpackbits function, and the result is send
back to Python.
2. When constructing a MlirAttribute from a numpy array, first the
python data is read as a uint8_t to get it converted to the endianess
used internally in mlir. Then the booleans are bitpacked using numpy's
bitpack function, and the bitpacked array is saved as the MlirAttribute
representation.</pre>
</div>
</content>
</entry>
<entry>
<title>Revert "[MLIR,Python] Support converting boolean numpy arrays to and from mlir attributes (#113064)"</title>
<updated>2024-11-05T15:08:51+00:00</updated>
<author>
<name>Dmitri Gribenko</name>
<email>gribozavr@gmail.com</email>
</author>
<published>2024-11-05T14:48:13+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=0a68171b3c67503f7143856580f1b22a93ef566e'/>
<id>0a68171b3c67503f7143856580f1b22a93ef566e</id>
<content type='text'>
This reverts commit fb7bf7a5acc65be44fc546f282942b91472553b3. There is
an ASan issue here, see the discussion on
https://github.com/llvm/llvm-project/pull/113064.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This reverts commit fb7bf7a5acc65be44fc546f282942b91472553b3. There is
an ASan issue here, see the discussion on
https://github.com/llvm/llvm-project/pull/113064.
</pre>
</div>
</content>
</entry>
<entry>
<title>[MLIR,Python] Support converting boolean numpy arrays to and from mlir attributes (#113064)</title>
<updated>2024-11-02T06:39:48+00:00</updated>
<author>
<name>Kasper Nielsen</name>
<email>kasper0406@gmail.com</email>
</author>
<published>2024-11-02T06:39:48+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=fb7bf7a5acc65be44fc546f282942b91472553b3'/>
<id>fb7bf7a5acc65be44fc546f282942b91472553b3</id>
<content type='text'>
Currently it is unsupported to:
1. Convert a `MlirAttribute` with type `i1` to a numpy array
2. Convert a boolean numpy array to a `MlirAttribute`

Currently the entire Python application violently crashes with a quite
poor error message https://github.com/pybind/pybind11/issues/3336

The complication handling these conversions, is that `MlirAttribute`
represent booleans as a bit-packed `i1` type, whereas numpy represents
booleans as a byte array with 8 bit used per boolean.

This PR proposes the following approach:
1. When converting a `i1` typed `MlirAttribute` to a numpy array, we can
not directly use the underlying raw data backing the `MlirAttribute` as
a buffer to Python, as done for other types. Instead, a copy of the data
is generated using numpy's unpackbits function, and the result is send
back to Python.
2. When constructing a `MlirAttribute` from a numpy array, first the
python data is read as a `uint8_t` to get it converted to the endianess
used internally in mlir. Then the booleans are bitpacked using numpy's
bitpack function, and the bitpacked array is saved as the
`MlirAttribute` representation.

Please note that I am not sure if this approach is the desired solution.
I'd appreciate any feedback.</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Currently it is unsupported to:
1. Convert a `MlirAttribute` with type `i1` to a numpy array
2. Convert a boolean numpy array to a `MlirAttribute`

Currently the entire Python application violently crashes with a quite
poor error message https://github.com/pybind/pybind11/issues/3336

The complication handling these conversions, is that `MlirAttribute`
represent booleans as a bit-packed `i1` type, whereas numpy represents
booleans as a byte array with 8 bit used per boolean.

This PR proposes the following approach:
1. When converting a `i1` typed `MlirAttribute` to a numpy array, we can
not directly use the underlying raw data backing the `MlirAttribute` as
a buffer to Python, as done for other types. Instead, a copy of the data
is generated using numpy's unpackbits function, and the result is send
back to Python.
2. When constructing a `MlirAttribute` from a numpy array, first the
python data is read as a `uint8_t` to get it converted to the endianess
used internally in mlir. Then the booleans are bitpacked using numpy's
bitpack function, and the bitpacked array is saved as the
`MlirAttribute` representation.

Please note that I am not sure if this approach is the desired solution.
I'd appreciate any feedback.</pre>
</div>
</content>
</entry>
<entry>
<title>[mlir][python] Add bindings for mlirDenseElementsAttrGet (#91389)</title>
<updated>2024-05-22T10:44:22+00:00</updated>
<author>
<name>pranavm-nvidia</name>
<email>49246958+pranavm-nvidia@users.noreply.github.com</email>
</author>
<published>2024-05-22T10:44:22+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=c912f0e773386cc309155b78e2441ee5f1052c13'/>
<id>c912f0e773386cc309155b78e2441ee5f1052c13</id>
<content type='text'>
This change adds bindings for `mlirDenseElementsAttrGet` which accepts a
list of MLIR attributes and constructs a DenseElementsAttr. This allows
for creating `DenseElementsAttr`s of types not natively supported by
Python (e.g. BF16) without requiring other dependencies (e.g. `numpy` +
`ml-dtypes`).</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This change adds bindings for `mlirDenseElementsAttrGet` which accepts a
list of MLIR attributes and constructs a DenseElementsAttr. This allows
for creating `DenseElementsAttr`s of types not natively supported by
Python (e.g. BF16) without requiring other dependencies (e.g. `numpy` +
`ml-dtypes`).</pre>
</div>
</content>
</entry>
<entry>
<title>[mlir] Add Python bindings for DenseResourceElementsAttr. (#66319)</title>
<updated>2023-09-15T01:45:29+00:00</updated>
<author>
<name>Stella Laurenzo</name>
<email>stellaraccident@gmail.com</email>
</author>
<published>2023-09-15T01:45:29+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=f66cd9e9556a53142a26a5c21a72e21f1579217c'/>
<id>f66cd9e9556a53142a26a5c21a72e21f1579217c</id>
<content type='text'>
Only construction and type casting are implemented. The method to create
is explicitly named "unsafe" and the documentation calls out what the
caller is responsible for. There really isn't a better way to do this
and retain the power-user feature this represents.</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Only construction and type casting are implemented. The method to create
is explicitly named "unsafe" and the documentation calls out what the
caller is responsible for. There really isn't a better way to do this
and retain the power-user feature this represents.</pre>
</div>
</content>
</entry>
<entry>
<title>[MLIR:Python] Make DenseElementsAttr.get() only request a buffer format if no explicit type was provided.</title>
<updated>2023-07-14T23:08:15+00:00</updated>
<author>
<name>Peter Hawkins</name>
<email>phawkins@google.com</email>
</author>
<published>2023-07-14T23:08:15+00:00</published>
<link rel='alternate' type='text/html' href='https://git.belthelziquor.com/llvm-project.git/commit/?id=71a254543d44a943dfe8790abc60795b87173f0b'/>
<id>71a254543d44a943dfe8790abc60795b87173f0b</id>
<content type='text'>
Not every NumPy type (e.g., the `ml_dtypes.bfloat16` NumPy extension
type) has a type in the Python buffer protocol, so exporting such a
buffer with `PyBUF_FORMAT` may fail.

However, we don't care about the self-reported type of a buffer if the
user provides an explicit type. In the case that an explicit type is
provided, don't request the format from the buffer protocol, which
allows arrays whose element types are unknown to the buffer protocol to
be passed.

Reviewed By: jpienaar, ftynse

Differential Revision: https://reviews.llvm.org/D155209
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Not every NumPy type (e.g., the `ml_dtypes.bfloat16` NumPy extension
type) has a type in the Python buffer protocol, so exporting such a
buffer with `PyBUF_FORMAT` may fail.

However, we don't care about the self-reported type of a buffer if the
user provides an explicit type. In the case that an explicit type is
provided, don't request the format from the buffer protocol, which
allows arrays whose element types are unknown to the buffer protocol to
be passed.

Reviewed By: jpienaar, ftynse

Differential Revision: https://reviews.llvm.org/D155209
</pre>
</div>
</content>
</entry>
</feed>
