-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][sparse] Return actual identity map instead of null map #70365
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
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
Changes: 1. For both dimToLvl and lvlToDim, always returns the actual map instead of AffineMap() for identity map. 2. Updated custom builder for encoding to have default values. 3. Non-inferable lvlToDim will still return AffineMap() during inference, so it will be caught by verifier.
✅ With the latest revision this PR passed the C/C++ code formatter. |
@llvm/pr-subscribers-mlir-sparse @llvm/pr-subscribers-mlir Author: Yinying Li (yinying-lisa-li) ChangesChanges:
Full diff: https://github.com/llvm/llvm-project/pull/70365.diff 4 Files Affected:
diff --git a/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td b/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td
index 2dd7f8e961929cf..5348cc230ffc0d2 100644
--- a/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td
+++ b/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td
@@ -303,10 +303,13 @@ def SparseTensorEncodingAttr : SparseTensor_Attr<"SparseTensorEncoding",
let builders = [
AttrBuilder<(ins "ArrayRef<::mlir::sparse_tensor::DimLevelType>":$lvlTypes,
- "AffineMap":$dimToLvl,
- "AffineMap":$lvlToDim,
- "unsigned":$posWidth,
- "unsigned":$crdWidth), [{
+ CArg<"AffineMap", "{}">:$dimToLvl,
+ CArg<"AffineMap", "{}">:$lvlToDim,
+ CArg<"unsigned", "0">:$posWidth,
+ CArg<"unsigned", "0">:$crdWidth), [{
+ if (!dimToLvl) {
+ dimToLvl = ::mlir::AffineMap::getMultiDimIdentityMap(lvlTypes.size(), $_ctxt);
+ }
if (!lvlToDim) {
lvlToDim = ::mlir::sparse_tensor::inferLvlToDim(dimToLvl, $_ctxt);
}
diff --git a/mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.cpp b/mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.cpp
index 851867926fe679e..6a81a11a932f94a 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.cpp
+++ b/mlir/lib/Dialect/SparseTensor/IR/Detail/DimLvlMap.cpp
@@ -313,8 +313,7 @@ AffineMap DimLvlMap::getDimToLvlMap(MLIRContext *context) const {
lvlAffines.reserve(getLvlRank());
for (const auto &lvlSpec : lvlSpecs)
lvlAffines.push_back(lvlSpec.getExpr().getAffineExpr());
- auto map = AffineMap::get(getDimRank(), getSymRank(), lvlAffines, context);
- if (map.isIdentity()) return AffineMap();
+ auto map = AffineMap::get(getDimRank(), getSymRank(), lvlAffines, context);
return map;
}
@@ -328,7 +327,9 @@ AffineMap DimLvlMap::getLvlToDimMap(MLIRContext *context) const {
}
}
auto map = AffineMap::get(getLvlRank(), getSymRank(), dimAffines, context);
- if (dimAffines.empty() || map.isIdentity())
+ // If no lvlToDim map was passed in, returns a null AffineMap and infers it
+ // in SparseTensorEncodingAttr::parse.
+ if (dimAffines.empty())
return AffineMap();
return map;
}
diff --git a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
index 359c0a696858329..778a177502cbc3a 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
+++ b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
@@ -291,7 +291,7 @@ SparseTensorEncodingAttr
SparseTensorEncodingAttr::withDimToLvl(AffineMap dimToLvl) const {
assert(getImpl() && "Uninitialized SparseTensorEncodingAttr");
return SparseTensorEncodingAttr::get(getContext(), getLvlTypes(), dimToLvl,
- getLvlToDim(), getPosWidth(),
+ AffineMap(), getPosWidth(),
getCrdWidth());
}
diff --git a/mlir/test/python/dialects/sparse_tensor/dialect.py b/mlir/test/python/dialects/sparse_tensor/dialect.py
index 240db6ebd1d1eb3..fe7b41e536e2763 100644
--- a/mlir/test/python/dialects/sparse_tensor/dialect.py
+++ b/mlir/test/python/dialects/sparse_tensor/dialect.py
@@ -30,9 +30,9 @@ def testEncodingAttr1D():
# CHECK: lvl_types: [<DimLevelType.compressed: 8>]
print(f"lvl_types: {casted.lvl_types}")
- # CHECK: dim_to_lvl: None
+ # CHECK: dim_to_lvl: (d0) -> (d0)
print(f"dim_to_lvl: {casted.dim_to_lvl}")
- # CHECK: lvl_to_dim: None
+ # CHECK: lvl_to_dim: (d0) -> (d0)
print(f"lvl_to_dim: {casted.lvl_to_dim}")
# CHECK: pos_width: 16
print(f"pos_width: {casted.pos_width}")
|
aartbik
approved these changes
Oct 26, 2023
PeimingLiu
approved these changes
Oct 26, 2023
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.
Changes: