summaryrefslogtreecommitdiff
path: root/mlir/test/python/ir/array_attributes.py
AgeCommit message (Collapse)Author
2025-07-25[mlir][python] fix PyDenseResourceElementsAttribute finalizer (#150561)Maksim Levental
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): > 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 <anton@korobeynikov.info> Co-authored-by: Max Manainen <maximmanainen@gmail.com> Co-authored-by: Anton Korobeynikov <anton@korobeynikov.info> Co-authored-by: Max Manainen <maximmanainen@gmail.com>
2025-01-29Reapply "[mlir][python] allow DenseIntElementsAttr for index type (#118947)" ↵Matthias Gehre
(#124804) This reapplies #118947 and adapts to nanobind.
2025-01-28Revert "[mlir][python] allow DenseIntElementsAttr for index type (#118947)"Matthias Gehre
This reverts commit 9dd762e8b10586e749b0ddf3542e5dccf8392395.
2025-01-28[mlir][python] allow DenseIntElementsAttr for index type (#118947)Matthias Gehre
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<[1, 2, 3, 4]> : vector<4xindex>`. --------- Co-authored-by: Christopher McGirr <christopher.mcgirr@amd.com> Co-authored-by: Tiago Trevisan Jost <tiago.trevisanjost@amd.com>
2024-11-13[MLIR,Python] Support converting boolean numpy arrays to and from mlir ↵Kasper Nielsen
attributes (unrevert) (#115481) 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.
2024-11-05Revert "[MLIR,Python] Support converting boolean numpy arrays to and from ↵Dmitri Gribenko
mlir attributes (#113064)" This reverts commit fb7bf7a5acc65be44fc546f282942b91472553b3. There is an ASan issue here, see the discussion on https://github.com/llvm/llvm-project/pull/113064.
2024-11-02[MLIR,Python] Support converting boolean numpy arrays to and from mlir ↵Kasper Nielsen
attributes (#113064) 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.
2024-05-22[mlir][python] Add bindings for mlirDenseElementsAttrGet (#91389)pranavm-nvidia
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`).
2023-09-14[mlir] Add Python bindings for DenseResourceElementsAttr. (#66319)Stella Laurenzo
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.
2023-07-14[MLIR:Python] Make DenseElementsAttr.get() only request a buffer format if ↵Peter Hawkins
no explicit type was provided. 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
2023-07-10[mlir][python] Downcast attributes in more placesRahul Kayaith
Update remaining `PyAttribute`-returning APIs to return `MlirAttribute` instead, so that they go through the downcasting mechanism. Reviewed By: makslevental Differential Revision: https://reviews.llvm.org/D154462
2023-05-26[NFC][Py Reformat] Reformat python files in mlir subdirTobias Hieta
This is an ongoing series of commits that are reformatting our Python code. Reformatting is done with `black`. If you end up having problems merging this commit because you have made changes to a python file, the best way to handle that is to run git checkout --ours <yourfile> and then reformat it with black. If you run into any problems, post to discourse about it and we will try to help. RFC Thread below: https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style Differential Revision: https://reviews.llvm.org/D150782
2023-05-03[MLIR][python bindings] Add support for DenseElementsAttr of IndexTypemax
Differential Revision: https://reviews.llvm.org/D149690
2023-03-30[mlir][python] Support buffer protocol for splat dense attributesRahul Kayaith
These can be made to work by setting the buffer strides to 0. Reviewed By: stellaraccident Differential Revision: https://reviews.llvm.org/D147187
2023-03-21Support retrieving the splat value from DenseElementsAttrs in PythonAdam Paszke
This is especially convenient when trying to resize the splat. Reviewed By: jpienaar Differential Revision: https://reviews.llvm.org/D146510
2021-10-07[mlir][python] Temporarily disable test for converting unsupported ↵Stella Laurenzo
DenseElementsAttr types to a buffer. * Need to investigate the proper solution to https://github.com/pybind/pybind11/issues/3336 or engineer something different. * The attempt to produce an empty buffer_info as a workaround triggers asan/ubsan. * Usage of this API does not arise naturally in practice yet, and it is more important to be asan/crash clean than have a solution right now. * Switching back to raising an exception, even though that triggers terminate().
2021-10-07[mlir] Extend C and Python API to support bulk loading of DenseElementsAttr.Stella Laurenzo
* This already half existed in terms of reading the raw buffer backing a DenseElementsAttr. * Documented the precise expectations of the buffer layout. * Extended the Python API to support construction from bitcasted buffers, allowing construction of all primitive element types (even those that lack a compatible representation in Python). * Specifically, the Python API can now load all integer types at all bit widths and all floating point types (f16, f32, f64, bf16). Differential Revision: https://reviews.llvm.org/D111284
2021-05-03Move MLIR python sources to mlir/python.Stella Laurenzo
* NFC but has some fixes for CMake glitches discovered along the way (things not cleaning properly, co-mingled depends). * Includes previously unsubmitted fix in D98681 and a TODO to fix it more appropriately in a smaller followup. Differential Revision: https://reviews.llvm.org/D101493