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> dimToLvl, int posWidth, int crdWidth, 44 MlirContext context) { 45 return cls(mlirSparseTensorEncodingAttrGet( 46 context, lvlTypes.size(), lvlTypes.data(), 47 dimToLvl ? *dimToLvl : MlirAffineMap{nullptr}, posWidth, 48 crdWidth)); 49 }, 50 py::arg("cls"), py::arg("lvl_types"), py::arg("dim_to_lvl"), 51 py::arg("pos_width"), py::arg("crd_width"), 52 py::arg("context") = py::none(), 53 "Gets a sparse_tensor.encoding from parameters.") 54 .def_property_readonly( 55 "lvl_types", 56 [](MlirAttribute self) { 57 const int lvlRank = mlirSparseTensorEncodingGetLvlRank(self); 58 std::vector<MlirSparseTensorDimLevelType> ret; 59 ret.reserve(lvlRank); 60 for (int l = 0; l < lvlRank; ++l) 61 ret.push_back(mlirSparseTensorEncodingAttrGetLvlType(self, l)); 62 return ret; 63 }) 64 .def_property_readonly( 65 "dim_to_lvl", 66 [](MlirAttribute self) -> std::optional<MlirAffineMap> { 67 MlirAffineMap ret = mlirSparseTensorEncodingAttrGetDimToLvl(self); 68 if (mlirAffineMapIsNull(ret)) 69 return {}; 70 return ret; 71 }) 72 .def_property_readonly("pos_width", 73 mlirSparseTensorEncodingAttrGetPosWidth) 74 .def_property_readonly("crd_width", 75 mlirSparseTensorEncodingAttrGetCrdWidth); 76 } 77 78 PYBIND11_MODULE(_mlirDialectsSparseTensor, m) { 79 m.doc() = "MLIR SparseTensor dialect."; 80 populateDialectSparseTensorSubmodule(m); 81 } 82