xref: /llvm-project/mlir/lib/Bindings/Python/DialectSparseTensor.cpp (revision a0615d020a02e252196383439e2c8143c6525e05)
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 "mlir-c/Dialect/SparseTensor.h"
10 #include "mlir-c/IR.h"
11 #include "mlir/Bindings/Python/PybindAdaptors.h"
12 #include <optional>
13 
14 namespace py = pybind11;
15 using namespace llvm;
16 using namespace mlir;
17 using namespace mlir::python::adaptors;
18 
19 static void populateDialectSparseTensorSubmodule(const py::module &m) {
20   py::enum_<MlirSparseTensorDimLevelType>(m, "DimLevelType", py::module_local())
21       .value("dense", MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE)
22       .value("compressed", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED)
23       .value("compressed-nu", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU)
24       .value("compressed-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NO)
25       .value("compressed-nu-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU_NO)
26       .value("singleton", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON)
27       .value("singleton-nu", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU)
28       .value("singleton-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NO)
29       .value("singleton-nu-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU_NO)
30       .value("compressed-hi", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_WITH_HI)
31       .value("compressed-hi-nu",
32              MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_WITH_HI_NU)
33       .value("compressed-hi-no",
34              MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_WITH_HI_NO)
35       .value("compressed-hi-nu-no",
36              MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_WITH_HI_NU_NO);
37 
38   mlir_attribute_subclass(m, "EncodingAttr",
39                           mlirAttributeIsASparseTensorEncodingAttr)
40       .def_classmethod(
41           "get",
42           [](py::object cls, std::vector<MlirSparseTensorDimLevelType> lvlTypes,
43              std::optional<MlirAffineMap> dimOrdering,
44              std::optional<MlirAffineMap> higherOrdering, int posWidth,
45              int crdWidth, MlirContext context) {
46             return cls(mlirSparseTensorEncodingAttrGet(
47                 context, lvlTypes.size(), lvlTypes.data(),
48                 dimOrdering ? *dimOrdering : MlirAffineMap{nullptr},
49                 higherOrdering ? *higherOrdering : MlirAffineMap{nullptr},
50                 posWidth, crdWidth));
51           },
52           py::arg("cls"), py::arg("lvl_types"), py::arg("dim_ordering"),
53           py::arg("higher_ordering"), py::arg("pos_width"),
54           py::arg("crd_width"), py::arg("context") = py::none(),
55           "Gets a sparse_tensor.encoding from parameters.")
56       .def_property_readonly(
57           "lvl_types",
58           [](MlirAttribute self) {
59             const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
60             std::vector<MlirSparseTensorDimLevelType> ret;
61             ret.reserve(lvlRank);
62             for (int l = 0; l < lvlRank; ++l)
63               ret.push_back(mlirSparseTensorEncodingAttrGetLvlType(self, l));
64             return ret;
65           })
66       .def_property_readonly(
67           "dim_ordering",
68           [](MlirAttribute self) -> std::optional<MlirAffineMap> {
69             MlirAffineMap ret =
70                 mlirSparseTensorEncodingAttrGetDimOrdering(self);
71             if (mlirAffineMapIsNull(ret))
72               return {};
73             return ret;
74           })
75       .def_property_readonly(
76           "higher_ordering",
77           [](MlirAttribute self) -> std::optional<MlirAffineMap> {
78             MlirAffineMap ret =
79                 mlirSparseTensorEncodingAttrGetHigherOrdering(self);
80             if (mlirAffineMapIsNull(ret))
81               return {};
82             return ret;
83           })
84       .def_property_readonly("pos_width",
85                              mlirSparseTensorEncodingAttrGetPosWidth)
86       .def_property_readonly("crd_width",
87                              mlirSparseTensorEncodingAttrGetCrdWidth);
88 }
89 
90 PYBIND11_MODULE(_mlirDialectsSparseTensor, m) {
91   m.doc() = "MLIR SparseTensor dialect.";
92   populateDialectSparseTensorSubmodule(m);
93 }
94