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 13 namespace py = pybind11; 14 using namespace llvm; 15 using namespace mlir; 16 using namespace mlir::python::adaptors; 17 18 static void populateDialectSparseTensorSubmodule(const py::module &m) { 19 py::enum_<MlirSparseTensorDimLevelType>(m, "DimLevelType", py::module_local()) 20 .value("dense", MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE) 21 .value("compressed", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED) 22 .value("compressed-nu", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU) 23 .value("compressed-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NO) 24 .value("compressed-nu-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED_NU_NO) 25 .value("singleton", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON) 26 .value("singleton-nu", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU) 27 .value("singleton-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NO) 28 .value("singleton-nu-no", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON_NU_NO); 29 30 mlir_attribute_subclass(m, "EncodingAttr", 31 mlirAttributeIsASparseTensorEncodingAttr) 32 .def_classmethod( 33 "get", 34 [](py::object cls, 35 std::vector<MlirSparseTensorDimLevelType> dimLevelTypes, 36 llvm::Optional<MlirAffineMap> dimOrdering, int pointerBitWidth, 37 int indexBitWidth, MlirContext context) { 38 return cls(mlirSparseTensorEncodingAttrGet( 39 context, dimLevelTypes.size(), dimLevelTypes.data(), 40 dimOrdering ? *dimOrdering : MlirAffineMap{nullptr}, 41 pointerBitWidth, indexBitWidth)); 42 }, 43 py::arg("cls"), py::arg("dim_level_types"), py::arg("dim_ordering"), 44 py::arg("pointer_bit_width"), py::arg("index_bit_width"), 45 py::arg("context") = py::none(), 46 "Gets a sparse_tensor.encoding from parameters.") 47 .def_property_readonly( 48 "dim_level_types", 49 [](MlirAttribute self) { 50 std::vector<MlirSparseTensorDimLevelType> ret; 51 for (int i = 0, 52 e = mlirSparseTensorEncodingGetNumDimLevelTypes(self); 53 i < e; ++i) 54 ret.push_back( 55 mlirSparseTensorEncodingAttrGetDimLevelType(self, i)); 56 return ret; 57 }) 58 .def_property_readonly( 59 "dim_ordering", 60 [](MlirAttribute self) -> llvm::Optional<MlirAffineMap> { 61 MlirAffineMap ret = 62 mlirSparseTensorEncodingAttrGetDimOrdering(self); 63 if (mlirAffineMapIsNull(ret)) 64 return {}; 65 return ret; 66 }) 67 .def_property_readonly( 68 "pointer_bit_width", 69 [](MlirAttribute self) { 70 return mlirSparseTensorEncodingAttrGetPointerBitWidth(self); 71 }) 72 .def_property_readonly("index_bit_width", [](MlirAttribute self) { 73 return mlirSparseTensorEncodingAttrGetIndexBitWidth(self); 74 }); 75 } 76 77 PYBIND11_MODULE(_mlirDialectsSparseTensor, m) { 78 m.doc() = "MLIR SparseTensor dialect."; 79 populateDialectSparseTensorSubmodule(m); 80 } 81