Hi all,
I am using python binding of my IR to parse the prebuilt MLIR file, however, I don’t know how to obtain the type encoding arguments?
It is like
"
tensor<1x5888x256xsi8, #xml .XExt<bufferLoc = local, fmt = K32M32, addr = 4784128 : si64, bufferId = 1 : si64, FIFONum = 4 : si64, originShape = [1, 5888, 256]>
"
I want to obtain the encoding of type, say, XExt in python. Now I know I can obtain the type by
x = op.results[0].type
but then what? Where to find the API of python bindings?
Thank you in advance
So you can cast the type to a RankedTensorType
and that has an encoding
property which will return you the encoding as an Attribute
. So in your example it would look like RankedTensorType(x).encoding
. Note that this returns a generic Attribute
and it’s on you to provide bindings to parse that attribute.
Here’s the stub file for the python bindings but that can sometimes be stale so I recommend just looking at the bindings themselves .
1 Like
As of
committed 04:02PM - 26 May 23 UTC
depends on D150839
This diff uses `MlirTypeID` to register `TypeCaster`s (i.e.,… `[](PyType pyType) -> DerivedTy { return pyType; }`) for all concrete types (i.e., `PyConcrete<...>`) that are then queried for (by `MlirTypeID`) and called in `struct type_caster<MlirType>::cast`. The result is that anywhere an `MlirType mlirType` is returned from a python binding, that `mlirType` is automatically cast to the correct concrete type. For example:
```
c0 = arith.ConstantOp(f32, 0.0)
# CHECK: F32Type(f32)
print(repr(c0.result.type))
unranked_tensor_type = UnrankedTensorType.get(f32)
unranked_tensor = tensor.FromElementsOp(unranked_tensor_type, [c0]).result
# CHECK: UnrankedTensorType
print(type(unranked_tensor.type).__name__)
# CHECK: UnrankedTensorType(tensor<*xf32>)
print(repr(unranked_tensor.type))
```
This functionality immediately extends to typed attributes (i.e., `attr.type`).
The diff also implements similar functionality for `mlir_type_subclass`es but in a slightly different way - for such types (which have no cpp corresponding `class` or `struct`) the user must provide a type caster in python (similar to how `AttrBuilder` works) or in cpp as a `py::cpp_function`.
Reviewed By: ftynse
Differential Revision: https://reviews.llvm.org/D150927
you can skip the cast (op.results[0].type
will now return a RankedTensorType
).
3 Likes
Thanks! that’s clear, I will give it a try
OK! thanks for the advise
Could you please kindly show how to bind the custom defined attribute, and how to use it?
Thanks a lot!
Here’s an example in SparseTensor and here’s the usage .