xref: /llvm-project/mlir/lib/Bindings/Python/DialectSparseTensor.cpp (revision 269c82d389f1f174b46002ec641dc358d73bc88c)
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("compressed24", MLIR_SPARSE_TENSOR_DIM_LEVEL_TWO_OUT_OF_FOUR)
23       .value("compressed", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED)
24       .value("compressed-nu", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU)
25       .value("compressed-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NO)
26       .value("compressed-nu-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU_NO)
27       .value("singleton", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON)
28       .value("singleton-nu", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU)
29       .value("singleton-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NO)
30       .value("singleton-nu-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU_NO)
31       .value("compressed-hi", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_WITH_HI)
32       .value("compressed-hi-nu",
33              MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_WITH_HI_NU)
34       .value("compressed-hi-no",
35              MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_WITH_HI_NO)
36       .value("compressed-hi-nu-no",
37              MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_WITH_HI_NU_NO);
38 
39   mlir_attribute_subclass(m, "EncodingAttr",
40                           mlirAttributeIsASparseTensorEncodingAttr)
41       .def_classmethod(
42           "get",
43           [](py::object cls, std::vector<MlirSparseTensorDimLevelType> lvlTypes,
44              std::optional<MlirAffineMap> dimToLvl, int posWidth, int crdWidth,
45              MlirContext context) {
46             return cls(mlirSparseTensorEncodingAttrGet(
47                 context, lvlTypes.size(), lvlTypes.data(),
48                 dimToLvl ? *dimToLvl : MlirAffineMap{nullptr}, posWidth,
49                 crdWidth));
50           },
51           py::arg("cls"), py::arg("lvl_types"), py::arg("dim_to_lvl"),
52           py::arg("pos_width"), py::arg("crd_width"),
53           py::arg("context") = py::none(),
54           "Gets a sparse_tensor.encoding from parameters.")
55       .def_property_readonly(
56           "lvl_types",
57           [](MlirAttribute self) {
58             const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self);
59             std::vector<MlirSparseTensorDimLevelType> ret;
60             ret.reserve(lvlRank);
61             for (int l = 0; l < lvlRank; ++l)
62               ret.push_back(mlirSparseTensorEncodingAttrGetLvlType(self, l));
63             return ret;
64           })
65       .def_property_readonly(
66           "dim_to_lvl",
67           [](MlirAttribute self) -> std::optional<MlirAffineMap> {
68             MlirAffineMap ret = mlirSparseTensorEncodingAttrGetDimToLvl(self);
69             if (mlirAffineMapIsNull(ret))
70               return {};
71             return ret;
72           })
73       .def_property_readonly("pos_width",
74                              mlirSparseTensorEncodingAttrGetPosWidth)
75       .def_property_readonly("crd_width",
76                              mlirSparseTensorEncodingAttrGetCrdWidth);
77 }
78 
79 PYBIND11_MODULE(_mlirDialectsSparseTensor, m) {
80   m.doc() = "MLIR SparseTensor dialect.";
81   populateDialectSparseTensorSubmodule(m);
82 }
83