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;
21
22namespace mlir {
23namespace python {
25namespace sparse_tensor {
26
35
42
43struct EncodingAttr : PyConcreteAttribute<EncodingAttr> {
44 static constexpr IsAFunctionTy isaFunction =
46 static constexpr const char *pyClassName = "EncodingAttr";
47 static inline const MlirStringRef name =
49 using Base::Base;
50
51 static void bindDerived(ClassTy &c) {
52 c.def_static(
53 "get",
54 [](std::vector<MlirSparseTensorLevelType> lvlTypes,
55 std::optional<PyAffineMap> dimToLvl,
56 std::optional<PyAffineMap> lvlToDim, int posWidth, int crdWidth,
57 std::optional<PyAttribute> explicitVal,
58 std::optional<PyAttribute> implicitVal,
60 return EncodingAttr(
61 context->getRef(),
63 context.get()->get(), lvlTypes.size(), lvlTypes.data(),
64 dimToLvl ? *dimToLvl : MlirAffineMap{nullptr},
65 lvlToDim ? *lvlToDim : MlirAffineMap{nullptr}, posWidth,
66 crdWidth, explicitVal ? *explicitVal : MlirAttribute{nullptr},
67 implicitVal ? *implicitVal : MlirAttribute{nullptr}));
68 },
69 nb::arg("lvl_types"), nb::arg("dim_to_lvl").none(),
70 nb::arg("lvl_to_dim").none(), nb::arg("pos_width"),
71 nb::arg("crd_width"), nb::arg("explicit_val") = nb::none(),
72 nb::arg("implicit_val") = nb::none(), nb::arg("context") = nb::none(),
73 "Gets a sparse_tensor.encoding from parameters.");
74
75 c.def_static(
76 "build_level_type",
78 const std::vector<PySparseTensorLevelPropertyNondefault> &properties,
79 unsigned n, unsigned m) {
80 std::vector<MlirSparseTensorLevelPropertyNondefault> props;
81 props.reserve(properties.size());
82 for (auto prop : properties) {
83 props.push_back(
85 }
87 static_cast<MlirSparseTensorLevelFormat>(lvlFmt), props.data(),
88 props.size(), n, m);
89 },
90 nb::arg("lvl_fmt"),
91 nb::arg("properties") =
92 std::vector<PySparseTensorLevelPropertyNondefault>(),
93 nb::arg("n") = 0, nb::arg("m") = 0,
94 "Builds a sparse_tensor.encoding.level_type from parameters.");
95
96 c.def_prop_ro("lvl_types", [](const EncodingAttr &self) {
97 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
98 std::vector<MlirSparseTensorLevelType> ret;
99 ret.reserve(lvlRank);
100 for (int l = 0; l < lvlRank; ++l)
101 ret.push_back(mlirSparseTensorEncodingAttrGetLvlType(self, l));
102 return ret;
103 });
104
105 c.def_prop_ro(
106 "dim_to_lvl", [](EncodingAttr &self) -> std::optional<PyAffineMap> {
107 MlirAffineMap ret = mlirSparseTensorEncodingAttrGetDimToLvl(self);
108 if (mlirAffineMapIsNull(ret))
109 return {};
110 return PyAffineMap(self.getContext(), ret);
111 });
112
113 c.def_prop_ro(
114 "lvl_to_dim", [](EncodingAttr &self) -> std::optional<PyAffineMap> {
115 MlirAffineMap ret = mlirSparseTensorEncodingAttrGetLvlToDim(self);
116 if (mlirAffineMapIsNull(ret))
117 return {};
118 return PyAffineMap(self.getContext(), ret);
119 });
120
121 c.def_prop_ro("pos_width", mlirSparseTensorEncodingAttrGetPosWidth);
122 c.def_prop_ro("crd_width", mlirSparseTensorEncodingAttrGetCrdWidth);
123
124 c.def_prop_ro("explicit_val",
125 [](EncodingAttr &self)
126 -> std::optional<nb::typed<nb::object, PyAttribute>> {
127 MlirAttribute ret =
129 if (mlirAttributeIsNull(ret))
130 return {};
131 return PyAttribute(self.getContext(), ret).maybeDownCast();
132 });
133
134 c.def_prop_ro("implicit_val",
135 [](EncodingAttr &self)
136 -> std::optional<nb::typed<nb::object, PyAttribute>> {
137 MlirAttribute ret =
139 if (mlirAttributeIsNull(ret))
140 return {};
141 return PyAttribute(self.getContext(), ret).maybeDownCast();
142 });
143
144 c.def_prop_ro("structured_n", [](const EncodingAttr &self) -> unsigned {
145 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
147 mlirSparseTensorEncodingAttrGetLvlType(self, lvlRank - 1));
148 });
149
150 c.def_prop_ro("structured_m", [](const EncodingAttr &self) -> unsigned {
151 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
153 mlirSparseTensorEncodingAttrGetLvlType(self, lvlRank - 1));
154 });
155
156 c.def_prop_ro("lvl_formats_enum", [](const EncodingAttr &self) {
157 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
158 std::vector<PySparseTensorLevelFormat> ret;
159 ret.reserve(lvlRank);
160
161 for (int l = 0; l < lvlRank; l++)
162 ret.push_back(static_cast<PySparseTensorLevelFormat>(
164 return ret;
165 });
166 }
167};
168
169static void populateDialectSparseTensorSubmodule(nb::module_ &m) {
170 nb::enum_<PySparseTensorLevelFormat>(m, "LevelFormat", nb::is_arithmetic(),
171 nb::is_flag())
172 .value("dense", PySparseTensorLevelFormat::DENSE)
173 .value("n_out_of_m", PySparseTensorLevelFormat::N_OUT_OF_M)
174 .value("compressed", PySparseTensorLevelFormat::COMPRESSED)
175 .value("singleton", PySparseTensorLevelFormat::SINGLETON)
176 .value("loose_compressed", PySparseTensorLevelFormat::LOOSE_COMPRESSED);
177 nb::enum_<PySparseTensorLevelPropertyNondefault>(m, "LevelProperty")
181
183}
184} // namespace sparse_tensor
185} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
186} // namespace python
187} // namespace mlir
188
189NB_MODULE(_mlirDialectsSparseTensor, m) {
190 m.doc() = "MLIR SparseTensor dialect.";
193}
NB_MODULE(_mlirDialectsSparseTensor, m)
ReferrentTy * get() const
PyMlirContextRef & getContext()
Accesses the context reference.
Definition IRCore.h:297
Used in function arguments when None should resolve to the current context manager set instance.
Definition IRCore.h:278
PyAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
Definition IRCore.h:1007
static void bind(nanobind::module_ &m, PyType_Slot *slots=nullptr)
Definition IRCore.h:1087
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
Include the generated interface declarations.
A pointer to a sized fragment of a string, not necessarily null-terminated.
Definition Support.h:78