-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][sparse]Make isBlockSparsity more robust #75113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
yinying-lisa-li
commented
Dec 11, 2023
- A single dimension can either be blocked (with floordiv and mod pair) or non-blocked. Mixing them would be invalid.
- Block size should be non-zero value.
@llvm/pr-subscribers-mlir-sparse @llvm/pr-subscribers-mlir Author: Yinying Li (yinying-lisa-li) Changes
Full diff: https://github.com/llvm/llvm-project/pull/75113.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
index 686180c09da724..70f508827e26b4 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
+++ b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
@@ -855,16 +855,21 @@ SmallVector<unsigned> mlir::sparse_tensor::getBlockSize(AffineMap dimToLvl) {
bool mlir::sparse_tensor::isBlockSparsity(AffineMap dimToLvl) {
if (!dimToLvl)
return false;
+ SmallVector<unsigned> isDimSet;
+ isDimSet.resize(dimToLvl.getNumDims());
std::map<unsigned, int64_t> coeffientMap;
for (auto result : dimToLvl.getResults()) {
if (auto binOp = dyn_cast<AffineBinaryOpExpr>(result)) {
// Check for "dim op const".
auto dimOp = dyn_cast<AffineDimExpr>(binOp.getLHS());
auto conOp = dyn_cast<AffineConstantExpr>(binOp.getRHS());
- if (!dimOp || !conOp)
+ if (!dimOp || !conOp || conOp.getValue() <= 0)
return false;
- // Inspect "dim / const" or "dim % const".
auto pos = dimOp.getPosition();
+ // Check current dim has not been set before.
+ if (isDimSet[pos] == 1)
+ return false;
+ // Inspect "dim / const" or "dim % const".
if (binOp.getKind() == AffineExprKind::FloorDiv) {
// Expect only one floordiv for each dimension.
if (coeffientMap.find(pos) != coeffientMap.end())
@@ -881,6 +886,14 @@ bool mlir::sparse_tensor::isBlockSparsity(AffineMap dimToLvl) {
} else {
return false;
}
+ } else if (auto dimOp = dyn_cast<AffineDimExpr>(result)) {
+ auto pos = dimOp.getPosition();
+ isDimSet[pos] = 1;
+ // Expect dim to be non-blocked (without floordiv/mod pair).
+ if (coeffientMap.find(pos) != coeffientMap.end())
+ return false;
+ } else {
+ return false;
}
}
return !coeffientMap.empty();
diff --git a/mlir/test/Dialect/SparseTensor/invalid_encoding.mlir b/mlir/test/Dialect/SparseTensor/invalid_encoding.mlir
index 6514391bae92d9..2d189cc94c15e2 100644
--- a/mlir/test/Dialect/SparseTensor/invalid_encoding.mlir
+++ b/mlir/test/Dialect/SparseTensor/invalid_encoding.mlir
@@ -254,6 +254,51 @@ func.func private @wrong_order_lvl_decl(%arg0: tensor<?x?xf64, #WrongOrderLvlDec
// -----
+// expected-error@+1 {{failed to infer lvlToDim from dimToLvl}}
+#BSR = #sparse_tensor.encoding<{
+ map = ( i, j ) ->
+ ( i floordiv 2 : dense,
+ j floordiv 3 : compressed,
+ i : dense,
+ j mod 3 : dense
+ )
+}>
+func.func private @BSR(%arg0: tensor<?x?xf64, #BSR>) {
+ return
+}
+
+// -----
+
+// expected-error@+1 {{failed to infer lvlToDim from dimToLvl}}
+#BSR = #sparse_tensor.encoding<{
+ map = ( i, j ) ->
+ ( i : dense,
+ j floordiv 3 : compressed,
+ i floordiv 3 : dense,
+ j mod 3 : dense
+ )
+}>
+func.func private @BSR(%arg0: tensor<?x?xf64, #BSR>) {
+ return
+}
+
+// -----
+
+// expected-error@+1 {{failed to infer lvlToDim from dimToLvl}}
+#BSR = #sparse_tensor.encoding<{
+ map = ( i, j ) ->
+ ( i floordiv -3 : dense,
+ j floordiv -3 : compressed,
+ i mod 3 : dense,
+ j mod 3 : dense
+ )
+}>
+func.func private @BSR(%arg0: tensor<?x?xf64, #BSR>) {
+ return
+}
+
+// -----
+
// expected-error@+1 {{expected lvlToDim to be an inverse of dimToLvl}}
#BSR_explicit = #sparse_tensor.encoding<{
map =
|
aartbik
reviewed
Dec 11, 2023
aartbik
approved these changes
Dec 12, 2023
5933b10
to
4692a8a
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.