Although we have plans to support this, and many other, dimension level type(s), currently the tag is not supported. It will be easy to add this back once support is added.
NOTE: based on discussion in https://discourse.llvm.org/t/overcoming-sparsification-limitation-on-level-types/62585
https://github.com/llvm/llvm-project/issues/51658
Reviewed By: Peiming
Differential Revision: https://reviews.llvm.org/D131002
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(SparseTensor, sparse_tensor);
/// Dimension level types that define sparse tensors:
-/// - MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE - dimension is dense, every
+/// - MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE - dimension is dense, every
/// entry is stored
/// - MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED - dimension is sparse,
-/// only nonzeros are stored.
-/// - MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON - dimension contains single
-/// coordinate, no siblings.
+/// only nonzeros are stored (no duplicates).
///
/// These correspond to SparseTensorEncodingAttr::DimLevelType in the C++ API.
/// If updating, keep them in sync and update the static_assert in the impl
enum MlirSparseTensorDimLevelType {
MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE,
MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED,
- MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON,
};
//===----------------------------------------------------------------------===//
let extraClassDeclaration = [{
// Dimension level types that define sparse tensors:
- // Dense - dimension is dense, every entry is stored
- // Compressed - dimension is sparse, only nonzeros are stored
- // Singleton - dimension contains single coordinate, no siblings
+ // Dense - dimension is dense, every entry is stored
+ // Compressed (unique) - dimension is sparse, only nonzeros are stored (no duplicates)
enum class DimLevelType {
- Dense, Compressed, Singleton
+ Dense, Compressed
};
}];
}
namespace sparse_tensor {
/// Dimension level type for a tensor (undef means index does not appear).
-enum Dim { kSparse, kDense, kSingle, kUndef };
+enum Dim { kSparse, kDense, kUndef };
/// Tensor expression kind.
enum Kind {
static void populateDialectSparseTensorSubmodule(const py::module &m) {
py::enum_<MlirSparseTensorDimLevelType>(m, "DimLevelType", py::module_local())
.value("dense", MLIR_SPARSE_TENSOR_DIM_LEVEL_DENSE)
- .value("compressed", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED)
- .value("singleton", MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON);
+ .value("compressed", MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED);
mlir_attribute_subclass(m, "EncodingAttr",
mlirAttributeIsASparseTensorEncodingAttr)
static_cast<int>(SparseTensorEncodingAttr::DimLevelType::Dense) &&
static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_COMPRESSED) ==
static_cast<int>(
- SparseTensorEncodingAttr::DimLevelType::Compressed) &&
- static_cast<int>(MLIR_SPARSE_TENSOR_DIM_LEVEL_SINGLETON) ==
- static_cast<int>(SparseTensorEncodingAttr::DimLevelType::Singleton),
+ SparseTensorEncodingAttr::DimLevelType::Compressed),
"MlirSparseTensorDimLevelType (C-API) and DimLevelType (C++) mismatch");
bool mlirAttributeIsASparseTensorEncodingAttr(MlirAttribute attr) {
dlt.push_back(SparseTensorEncodingAttr::DimLevelType::Dense);
} else if (strVal == "compressed") {
dlt.push_back(SparseTensorEncodingAttr::DimLevelType::Compressed);
- } else if (strVal == "singleton") {
- dlt.push_back(SparseTensorEncodingAttr::DimLevelType::Singleton);
} else {
parser.emitError(parser.getNameLoc(),
"unexpected dimension level type: ")
case DimLevelType::Compressed:
printer << "\"compressed\"";
break;
- case DimLevelType::Singleton:
- printer << "\"singleton\"";
- break;
}
if (i != e - 1)
printer << ", ";
return DimLevelType::kDense;
case SparseTensorEncodingAttr::DimLevelType::Compressed:
return DimLevelType::kCompressed;
- case SparseTensorEncodingAttr::DimLevelType::Singleton:
- return DimLevelType::kSingleton;
}
llvm_unreachable("Unknown SparseTensorEncodingAttr::DimLevelType");
}
if (alreadyCompressed)
return false; // Dense after Compressed not yet supported.
break;
- case SparseTensorEncodingAttr::DimLevelType::Singleton:
- // Although Singleton isn't generally supported yet, the direct
- // conversion method doesn't have any particular problems with
- // singleton after compressed.
- break;
}
}
return true;
SparseTensorEncodingAttr::DimLevelType tp = enc.getDimLevelType()[d];
if (tp == SparseTensorEncodingAttr::DimLevelType::Compressed)
return Dim::kSparse;
- if (tp == SparseTensorEncodingAttr::DimLevelType::Singleton)
- return Dim::kSingle;
}
return Dim::kDense;
}
case kDense:
llvm::dbgs() << "D";
break;
- case kSingle:
- llvm::dbgs() << "T";
- break;
case kUndef:
llvm::dbgs() << "U";
break;
// clang-format off
const char *originalAsm =
"#sparse_tensor.encoding<{ "
- "dimLevelType = [ \"dense\", \"compressed\", \"singleton\"], "
+ "dimLevelType = [ \"dense\", \"compressed\", \"compressed\"], "
"dimOrdering = affine_map<(d0, d1, d2) -> (d0, d1, d2)>, "
"pointerBitWidth = 32, indexBitWidth = 64 }>";
// clang-format on
mlirAffineMapDump(dimOrdering);
// CHECK: level_type: 0
// CHECK: level_type: 1
- // CHECK: level_type: 2
+ // CHECK: level_type: 1
int numLevelTypes = mlirSparseTensorEncodingGetNumDimLevelTypes(originalAttr);
enum MlirSparseTensorDimLevelType *levelTypes =
malloc(sizeof(enum MlirSparseTensorDimLevelType) * numLevelTypes);