[all-commits] [llvm/llvm-project] fb7bf7: [MLIR, Python] Support converting boolean numpy arr...

Kasper Nielsen via All-commits all-commits at lists.llvm.org
Fri Nov 1 23:40:11 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: fb7bf7a5acc65be44fc546f282942b91472553b3
      https://github.com/llvm/llvm-project/commit/fb7bf7a5acc65be44fc546f282942b91472553b3
  Author: Kasper Nielsen <kasper0406 at gmail.com>
  Date:   2024-11-02 (Sat, 02 Nov 2024)

  Changed paths:
    M mlir/lib/Bindings/Python/IRAttributes.cpp
    M mlir/test/python/ir/array_attributes.py

  Log Message:
  -----------
  [MLIR,Python] Support converting boolean numpy arrays to and from mlir 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.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list