MLIR 23.0.0git
DialectSparseTensor.cpp
Go to the documentation of this file.
1//===- DialectSparseTensor.cpp - 'sparse_tensor' dialect submodule --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include <optional>
10#include <vector>
11
12#include "mlir-c/AffineMap.h"
14#include "mlir-c/IR.h"
18
19namespace nb = nanobind;
20using namespace llvm;
22
23namespace mlir {
24namespace python {
26namespace sparse_tensor {
27
36
43
44struct EncodingAttr : PyConcreteAttribute<EncodingAttr> {
45 static constexpr IsAFunctionTy isaFunction =
47 static constexpr const char *pyClassName = "EncodingAttr";
48 static inline const MlirStringRef name =
50 using Base::Base;
51
52 static void bindDerived(ClassTy &c) {
53 c.def_static(
54 "get",
55 [](std::vector<MlirSparseTensorLevelType> lvlTypes,
56 std::optional<PyAffineMap> dimToLvl,
57 std::optional<PyAffineMap> lvlToDim, int posWidth, int crdWidth,
58 std::optional<PyAttribute> explicitVal,
59 std::optional<PyAttribute> implicitVal,
61 return EncodingAttr(
62 context->getRef(),
64 context.get()->get(), lvlTypes.size(), lvlTypes.data(),
65 dimToLvl ? *dimToLvl : MlirAffineMap{nullptr},
66 lvlToDim ? *lvlToDim : MlirAffineMap{nullptr}, posWidth,
67 crdWidth, explicitVal ? *explicitVal : MlirAttribute{nullptr},
68 implicitVal ? *implicitVal : MlirAttribute{nullptr}));
69 },
70 nb::arg("lvl_types"), nb::arg("dim_to_lvl").none(),
71 nb::arg("lvl_to_dim").none(), nb::arg("pos_width"),
72 nb::arg("crd_width"), nb::arg("explicit_val") = nb::none(),
73 nb::arg("implicit_val") = nb::none(), nb::arg("context") = nb::none(),
74 "Gets a sparse_tensor.encoding from parameters.");
75
76 c.def_static(
77 "build_level_type",
79 const std::vector<PySparseTensorLevelPropertyNondefault> &properties,
80 unsigned n, unsigned m) {
81 std::vector<MlirSparseTensorLevelPropertyNondefault> props;
82 props.reserve(properties.size());
83 for (auto prop : properties) {
84 props.push_back(
86 }
88 static_cast<MlirSparseTensorLevelFormat>(lvlFmt), props.data(),
89 props.size(), n, m);
90 },
91 nb::arg("lvl_fmt"),
92 nb::arg("properties") =
93 std::vector<PySparseTensorLevelPropertyNondefault>(),
94 nb::arg("n") = 0, nb::arg("m") = 0,
95 "Builds a sparse_tensor.encoding.level_type from parameters.");
96
97 c.def_prop_ro("lvl_types", [](const EncodingAttr &self) {
98 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
99 std::vector<MlirSparseTensorLevelType> ret;
100 ret.reserve(lvlRank);
101 for (int l = 0; l < lvlRank; ++l)
102 ret.push_back(mlirSparseTensorEncodingAttrGetLvlType(self, l));
103 return ret;
104 });
105
106 c.def_prop_ro(
107 "dim_to_lvl", [](EncodingAttr &self) -> std::optional<PyAffineMap> {
108 MlirAffineMap ret = mlirSparseTensorEncodingAttrGetDimToLvl(self);
109 if (mlirAffineMapIsNull(ret))
110 return {};
111 return PyAffineMap(self.getContext(), ret);
112 });
113
114 c.def_prop_ro(
115 "lvl_to_dim", [](EncodingAttr &self) -> std::optional<PyAffineMap> {
116 MlirAffineMap ret = mlirSparseTensorEncodingAttrGetLvlToDim(self);
117 if (mlirAffineMapIsNull(ret))
118 return {};
119 return PyAffineMap(self.getContext(), ret);
120 });
121
122 c.def_prop_ro("pos_width", mlirSparseTensorEncodingAttrGetPosWidth);
123 c.def_prop_ro("crd_width", mlirSparseTensorEncodingAttrGetCrdWidth);
124
125 c.def_prop_ro("explicit_val",
126 [](EncodingAttr &self)
127 -> std::optional<nb::typed<nb::object, PyAttribute>> {
128 MlirAttribute ret =
130 if (mlirAttributeIsNull(ret))
131 return {};
132 return PyAttribute(self.getContext(), ret).maybeDownCast();
133 });
134
135 c.def_prop_ro("implicit_val",
136 [](EncodingAttr &self)
137 -> std::optional<nb::typed<nb::object, PyAttribute>> {
138 MlirAttribute ret =
140 if (mlirAttributeIsNull(ret))
141 return {};
142 return PyAttribute(self.getContext(), ret).maybeDownCast();
143 });
144
145 c.def_prop_ro("structured_n", [](const EncodingAttr &self) -> unsigned {
146 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
148 mlirSparseTensorEncodingAttrGetLvlType(self, lvlRank - 1));
149 });
150
151 c.def_prop_ro("structured_m", [](const EncodingAttr &self) -> unsigned {
152 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
154 mlirSparseTensorEncodingAttrGetLvlType(self, lvlRank - 1));
155 });
156
157 c.def_prop_ro("lvl_formats_enum", [](const EncodingAttr &self) {
158 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
159 std::vector<PySparseTensorLevelFormat> ret;
160 ret.reserve(lvlRank);
161
162 for (int l = 0; l < lvlRank; l++)
163 ret.push_back(static_cast<PySparseTensorLevelFormat>(
165 return ret;
166 });
167 }
168};
169
170static void populateDialectSparseTensorSubmodule(nb::module_ &m) {
171 nb::enum_<PySparseTensorLevelFormat>(m, "LevelFormat", nb::is_arithmetic(),
172 nb::is_flag())
173 .value("dense", PySparseTensorLevelFormat::DENSE)
174 .value("n_out_of_m", PySparseTensorLevelFormat::N_OUT_OF_M)
175 .value("compressed", PySparseTensorLevelFormat::COMPRESSED)
176 .value("singleton", PySparseTensorLevelFormat::SINGLETON)
177 .value("loose_compressed", PySparseTensorLevelFormat::LOOSE_COMPRESSED);
178 nb::enum_<PySparseTensorLevelPropertyNondefault>(m, "LevelProperty")
182
184}
185} // namespace sparse_tensor
186} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
187} // namespace python
188} // namespace mlir
189
190NB_MODULE(_mlirDialectsSparseTensor, m) {
191 m.doc() = "MLIR SparseTensor dialect.";
194}
NB_MODULE(_mlirDialectsSparseTensor, m)
ReferrentTy * get() const
PyMlirContextRef & getContext()
Accesses the context reference.
Definition IRCore.h:299
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:280
PyAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
Definition IRCore.h:1011
static void bind(nanobind::module_ &m, PyType_Slot *slots=nullptr)
Definition IRCore.h:1092
static bool mlirAffineMapIsNull(MlirAffineMap affineMap)
Checks whether an affine map is null.
Definition AffineMap.h:47
MLIR_CAPI_EXPORTED MlirAffineMap mlirSparseTensorEncodingAttrGetDimToLvl(MlirAttribute attr)
Returns the dimension-to-level mapping of the sparse_tensor.encoding attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGetImplicitVal(MlirAttribute attr)
Returns the implicit value of the sparse_tensor.encoding attribute.
MLIR_CAPI_EXPORTED intptr_t mlirSparseTensorEncodingGetLvlRank(MlirAttribute attr)
Returns the level-rank of the sparse_tensor.encoding attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGet(MlirContext ctx, intptr_t lvlRank, MlirSparseTensorLevelType const *lvlTypes, MlirAffineMap dimToLvl, MlirAffineMap lvlTodim, int posWidth, int crdWidth, MlirAttribute explicitVal, MlirAttribute implicitVal)
Creates a sparse_tensor.encoding attribute with the given parameters.
MLIR_CAPI_EXPORTED enum MlirSparseTensorLevelFormat mlirSparseTensorEncodingAttrGetLvlFmt(MlirAttribute attr, intptr_t lvl)
Returns a specified level-format of the sparse_tensor.encoding attribute.
MLIR_CAPI_EXPORTED int mlirSparseTensorEncodingAttrGetCrdWidth(MlirAttribute attr)
Returns the coordinate bitwidth of the sparse_tensor.encoding attribute.
MLIR_CAPI_EXPORTED MlirStringRef mlirSparseTensorEncodingAttrGetName(void)
MLIR_CAPI_EXPORTED MlirAffineMap mlirSparseTensorEncodingAttrGetLvlToDim(MlirAttribute attr)
Returns the level-to-dimension mapping of the sparse_tensor.encoding attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirSparseTensorEncodingAttrGetExplicitVal(MlirAttribute attr)
Returns the explicit value of the sparse_tensor.encoding attribute.
MlirSparseTensorLevelFormat
@ MLIR_SPARSE_TENSOR_LEVEL_N_OUT_OF_M
@ MLIR_SPARSE_TENSOR_LEVEL_COMPRESSED
@ MLIR_SPARSE_TENSOR_LEVEL_DENSE
@ MLIR_SPARSE_TENSOR_LEVEL_LOOSE_COMPRESSED
@ MLIR_SPARSE_TENSOR_LEVEL_SINGLETON
MLIR_CAPI_EXPORTED unsigned mlirSparseTensorEncodingAttrGetStructuredN(MlirSparseTensorLevelType lvlType)
MLIR_CAPI_EXPORTED MlirSparseTensorLevelType mlirSparseTensorEncodingAttrBuildLvlType(enum MlirSparseTensorLevelFormat lvlFmt, const enum MlirSparseTensorLevelPropertyNondefault *properties, unsigned propSize, unsigned n, unsigned m)
MLIR_CAPI_EXPORTED unsigned mlirSparseTensorEncodingAttrGetStructuredM(MlirSparseTensorLevelType lvlType)
MLIR_CAPI_EXPORTED bool mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr)
Checks whether the given attribute is a sparse_tensor.encoding attribute.
MLIR_CAPI_EXPORTED MlirSparseTensorLevelType mlirSparseTensorEncodingAttrGetLvlType(MlirAttribute attr, intptr_t lvl)
Returns a specified level-type of the sparse_tensor.encoding attribute.
MLIR_CAPI_EXPORTED int mlirSparseTensorEncodingAttrGetPosWidth(MlirAttribute attr)
Returns the position bitwidth of the sparse_tensor.encoding attribute.
MlirSparseTensorLevelPropertyNondefault
@ MLIR_SPARSE_PROPERTY_NON_UNIQUE
@ MLIR_SPARSE_PROPERTY_NON_ORDERED
@ MLIR_SPARSE_PROPERTY_SOA
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:75