You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/05/12 12:18:43 UTC
[GitHub] [tvm] wrongtest opened a new pull request, #11287: [Arith][Simplify] Extend simplify rule for floormod(x * c1 + y, c2)
wrongtest opened a new pull request, #11287:
URL: https://github.com/apache/tvm/pull/11287
Update a simplify rule when c2 is nonzero, original rule is covered with constant folding.
`floormod(x * c1, c2)` =>
`floormod(x * (floordiv(c1, c2) * c2 + floormod(c1, c2)), c2)` =>
`floormod(x * floormod(c1, c2)), c2)`
This is useful for certain non-perfect tiling case, where there are dynamic loop ranges which is actually constant wrt outer loop domain.
For example, `floordiv(floormod(x * 360, 16) + 359, 16), 3)` with x in [0, 2) can finally reduce to constant `22`, since the rule could eliminate the multiply factor `360` to `360 % 16`, activating more available rules.
Unfortunately the working example on tiling encounter a region_cover related problems again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880011806
##########
python/tvm/arith/iter_affine_map.py:
##########
@@ -117,14 +117,14 @@ def detect_iter_map(
Returns
-------
- results : List[IterSumExpr]
+ results : IterMapResult
The iter map matching result.
- Empty array if no match can be found.
+ The result's .indices is empty array if no match can be found.
"""
return _ffi_api.DetectIterMap(
indices, input_iters, predicate, require_bijective, simplify_trivial_iterators
- )
+ ).indices
Review Comment:
I agree, I am trying to rewrite cases in test_arith_iter_affine_map.py
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880006145
##########
include/tvm/arith/iter_affine_map.h:
##########
@@ -259,53 +259,29 @@ class IterSumExpr : public IterMapExpr {
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode);
};
+/*! \brief Mapping level for iterators. */
+enum IterMapLevel {
Review Comment:
Yeah, we also have some case like `A[i // 8] = B[i % 16]` which could lower to some hw-specified data movement intrin. If we consider the mapping `i // 8` -> `i % 16` it could be neither surjective nor injective. But now the `DetectIterMap` try to solve the mapping from iter variables of non-strided domain to iter expression, thus I think it may not be able to handle such things directly. Maybe we can consider extent the input argument to two group of indices in the future?
Currently we handle it via bi-directional analysis from/to variable `i`:
i -> i // 8 : surjective
i -> i % 16: surjective, and injective reversely
Thus i // 8 -> i % 16 is something can be composed of surjective mapping and injective mapping.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Lunderberg commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r879804939
##########
include/tvm/arith/iter_affine_map.h:
##########
@@ -259,53 +259,29 @@ class IterSumExpr : public IterMapExpr {
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode);
};
+/*! \brief Mapping level for iterators. */
+enum IterMapLevel {
+ // Require the mapping to be bijective.
+ Bijective = 0,
+ // Require the mapping to be subjective.
Review Comment:
Nit: subjective -> surjective
##########
src/arith/iter_affine_map.cc:
##########
@@ -1260,140 +1300,132 @@ IterSumExpr IterMapRewriter::PreprocessDividend(IterMapExpr dividend) {
}
}
+PrimExpr NearLeastCommonMultiple(const PrimExpr& a, const PrimExpr& b, Analyzer* analyzer) {
+ auto fsplit = [](const PrimExpr& e) -> std::pair<PrimExpr, int64_t> {
+ if (const IntImmNode* imm = e.as<IntImmNode>()) {
+ return {1, imm->value};
+ }
+ PVar<PrimExpr> pv;
+ PVar<IntImm> pc;
+ if ((pv * pc).Match(e) || (pc * pv).Match(e)) {
+ return {pv.Eval(), pc.Eval()->value};
+ } else {
+ return {e, 1};
+ }
+ };
+
+ auto p1 = fsplit(a);
+ auto p2 = fsplit(b);
+ auto const_lcm = Integer(LeastCommonMultiple(p1.second, p2.second));
+ if (analyzer->CanProveEqual(p1.first, p2.first)) {
+ return p1.first * const_lcm;
+ } else {
+ return (p1.first * p2.first) * const_lcm;
+ }
+}
+
std::pair<IterSplitExpr, PrimExpr> IterMapRewriter::PadDividendToDivisor(IterSplitExpr split,
PrimExpr base,
PrimExpr divisor) {
// If FloorDiv: (((source//lower_factor) % extent) + base) // divisor
// If FloorMod: (((source//lower_factor) % extent) + base) % divisor
- PrimExpr lookup_key = split;
-
- auto modified_divisor = [&]() {
- if (update_iterator_padding_) {
- return divisor;
- }
-
- auto it = padded_iter_map_.find(lookup_key);
- if (it == padded_iter_map_.end()) {
- return divisor;
- }
-
- const std::vector<PrimExpr>& divisors = it->second.divisors;
- PrimExpr largest_divisor = divisor;
- for (const auto& other : divisors) {
- if (CanProveDivisible(other, largest_divisor)) {
- // New one is bigger, use it
- largest_divisor = other;
- } else if (CanProveDivisible(largest_divisor, other)) {
- // Current is bigger, keep it
- } else {
- ErrorLogger(this) << "Iterator appears in multiple terms with incompatible divisors "
- << tvm::PrettyPrint(largest_divisor) << " and "
- << tvm::PrettyPrint(other);
- }
- }
- return largest_divisor;
- }();
-
- divisor = modified_divisor;
-
+ // Update current iteration split's padding.
// First, adding any padding that is on the lower side of a
// FloorDiv/FloorMod, such that floormod(iter-left_pad,divisor) == 0
// when iter==0.
-
- PrimExpr left_pad;
-
- if (is_zero(base)) {
- // Padding on the left is unnecessary if base is known to be zero.
- left_pad = make_zero(base->dtype);
- } else {
- left_pad = analyzer_->Simplify(floormod(base, divisor));
- }
+ PrimExpr left_pad = analyzer_->Simplify(floormod(base, divisor));
// Next, adding any padding that is on the upper side of a
// FloorDiv/FloorMod, such that floormod(left_pad + iter + right_pad, divisor) == 0
// when iter==extent.
-
PrimExpr right_edge = left_pad + split->extent;
PrimExpr right_pad;
-
if (CanProveDivisible(right_edge, divisor)) {
- // Padding on the right is unnecessary if the extent is a multiple of
- // the divisor.
right_pad = 0;
} else {
- right_pad = analyzer_->Simplify(floormod(-right_edge, divisor));
- }
-
- if (is_zero(left_pad) && is_zero(right_pad)) {
- return {split, left_pad};
+ right_pad = analyzer_->Simplify(floormod(-right_edge, divisor), 9);
}
if (update_iterator_padding_) {
+ IterMark mark = split->source;
+ auto& info = padded_iter_map_[mark];
+ info.padding_factor =
+ NearLeastCommonMultiple(info.padding_factor, divisor * split->lower_factor, analyzer_);
+
+ if (is_zero(left_pad) && is_zero(right_pad)) {
+ return {split, 0};
+ }
+
// In the first pass, the primary goal is to collect all the divisors
// that may be used for padding. These will impact the divisor used
// to determine padding in the second pass.
- IterPaddingInfo& info = padded_iter_map_[lookup_key];
-
- info.divisors.push_back(divisor);
+ PrimExpr padded_extent = analyzer_->Simplify(left_pad + split->extent + right_pad);
- PrimExpr padded_extent = left_pad + split->extent + right_pad;
-
- IterSumExpr as_sum({split}, left_pad);
- IterMark mark(as_sum, padded_extent);
- IterSplitExpr new_split(mark);
-
- return {new_split, left_pad};
+ PrimExpr mark_left_pad = left_pad * split->lower_factor;
+ if (!is_zero(left_pad)) {
+ if (info.left_pad.defined()) {
+ info.left_pad = max(info.left_pad, mark_left_pad);
+ } else {
+ info.left_pad = mark_left_pad;
+ }
+ }
+ split.CopyOnWrite()->extent = padded_extent;
+ return {split, left_pad};
}
- // Any padding that is required during parsing should have been found
- // during the first pass that determines the GCD.
- auto it = padded_iter_map_.find(lookup_key);
+ // In the second pass, update iteration mark's to padded
+ const IterMark& mark = split->source;
+ auto it = padded_iter_map_.find(mark);
if (it == padded_iter_map_.end()) {
- ErrorLogger(this) << "Dividend has extent " << tvm::PrettyPrint(split->extent) << " and offset "
- << tvm::PrettyPrint(base) << ", which requires padding for divisor "
- << tvm::PrettyPrint(divisor) << ".";
- return {IterSplitExpr(), left_pad};
+ return {split, left_pad};
}
- IterPaddingInfo& info = it->second;
-
- if (info.padded.defined()) {
- // A previous visit already applied padding to this iterator.
- // (e.g. Visiting `(i+1)//4`, then visiting `(i+1)%4`).
- ICHECK(analyzer_->CanProveEqual(info.left_pad, left_pad));
- ICHECK(analyzer_->CanProveEqual(info.right_pad, right_pad));
-
- return {info.padded, left_pad};
+ auto& info = it->second;
+ if (is_zero(info.left_pad.defined() ? info.left_pad : 0) &&
+ CanProveDivisible(mark->extent, info.padding_factor)) {
+ return {split, left_pad};
}
- // This is the first encounter with the iterator during the second pass.
- IterSumExpr as_sum({split}, left_pad);
- IterMark mark(as_sum, left_pad + split->extent + right_pad);
- info.padded = IterSplitExpr(mark);
- info.left_pad = left_pad;
- info.right_pad = right_pad;
-
- auto left_padding_introduced = (left_pad != 0);
- // Equivalent to (0 <= split < left_pad), but easier to simplify in
- // terms of the transformed variables.
- auto left_padding_predicate =
- left_padding_introduced && (floordiv(info.padded, divisor) == floordiv(base, divisor) &&
- floormod(info.padded, divisor) < left_pad);
-
- PrimExpr nparts = ceildiv(right_edge, divisor);
-
- auto right_padding_introduced = (right_pad != 0);
-
- // Equivalent to (right_edge <= split < right_edge+right_pad), but
- // easier to simplify in terms of the transformed variables.
- auto right_padding_predicate = right_padding_introduced &&
- (floordiv(info.padded, divisor) == floordiv(right_edge, divisor) &&
- floormod(info.padded, divisor) >= floormod(right_edge, divisor));
-
- requires_padding_ = requires_padding_ || (left_padding_introduced || right_padding_introduced);
- padding_predicate_ = padding_predicate_ || (left_padding_predicate || right_padding_predicate);
-
- return {info.padded, left_pad};
+ if (!info.padded.defined()) {
+ PrimExpr mark_left_pad = info.left_pad.defined() ? info.left_pad : 0;
+ PrimExpr mark_right_pad;
+ if (CanProveDivisible(mark->extent + mark_left_pad, info.padding_factor)) {
+ mark_right_pad = 0;
+ } else {
+ mark_right_pad = floormod(-(mark->extent + mark_left_pad), info.padding_factor);
+ }
+ PrimExpr padded_extent = analyzer_->Simplify(mark_left_pad + mark->extent + mark_right_pad);
+ info.right_pad = mark_right_pad;
+ info.padded = IterMark(IterSumExpr({IterSplitExpr(mark)}, mark_left_pad), padded_extent);
+
+ auto left_padding_introduced = (mark_left_pad != 0);
+ PrimExpr divisor = info.padding_factor;
+ PrimExpr right_edge = mark_left_pad + mark->extent;
+
+ // Equivalent to (0 <= split < left_pad), but easier to simplify in
+ // terms of the transformed variables.
+ auto left_padding_predicate =
+ left_padding_introduced && (floordiv(info.padded->source, divisor) == 0 &&
+ floormod(info.padded->source, divisor) < mark_left_pad);
+
+ auto right_padding_introduced = (mark_right_pad != 0);
+
+ // Equivalent to (right_edge <= split < right_edge+right_pad), but
+ // easier to simplify in terms of the transformed variables.
+ auto right_padding_predicate =
+ right_padding_introduced &&
+ (floordiv(info.padded->source, divisor) == floordiv(right_edge, divisor) &&
+ floormod(info.padded->source, divisor) >= floormod(right_edge, divisor));
+
+ requires_padding_ = requires_padding_ || (left_padding_introduced || right_padding_introduced);
+ padding_predicate_ = padding_predicate_ || (left_padding_predicate || right_padding_predicate);
+ }
+ // ICHECK(CanProveDivisible(info.padded->extent, split->lower_factor));
Review Comment:
Should these `// ICHECK` lines be either uncommented or removed?
##########
src/arith/iter_affine_map.cc:
##########
@@ -1062,58 +1099,59 @@ PaddedIterMapResult DetectPaddedIterMap(const Array<PrimExpr>& indices,
[](const IterConstraint& a, const IterConstraint& b) { return a.expr_size < b.expr_size; });
IterMapRewriter rewriter(analyzer, constrained_input_iters, simplify_trivial_iterators,
- &result.errors);
+ &result->errors);
// Step0.0: rewrite constraints in the order from size-small ones to size-big ones
for (const IterConstraint& constraint : constraints) {
auto res = rewriter.RewriteIterConstraint(constraint.iter, constraint.lower_bound,
constraint.upper_bound);
- if (result.errors.size()) {
- return result;
+ if (result->errors.size()) {
+ return result_obj;
}
}
if (!rewriter.CheckConstraints()) {
- result.errors.push_back("Invalid constraints.");
- return result;
+ result->errors.push_back("Invalid constraints.");
+ return result_obj;
}
// Step0.1: Check each index to determine required padding
- bool allow_padding = !require_bijective;
+ bool allow_padding = check_level != IterMapLevel::Bijective;
Review Comment:
This would enable padding for `IterMapLevel::Surjective`, which I don't think is correct. Since padding is any output value for which no input value exists, any introduction of padding wouldn't be surjective.
##########
include/tvm/arith/iter_affine_map.h:
##########
@@ -259,53 +259,29 @@ class IterSumExpr : public IterMapExpr {
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode);
};
+/*! \brief Mapping level for iterators. */
+enum IterMapLevel {
Review Comment:
Do we want to allow the case where the map is neither surjective nor injective? I don't have any existing use cases, but since non-surjective mappings are useful to express padded layouts and non-injective mappings may be useful to express circular buffering, I could imagine wanting to disable all of the validation checks in order to have a padded circular buffer.
##########
python/tvm/arith/iter_affine_map.py:
##########
@@ -117,14 +117,14 @@ def detect_iter_map(
Returns
-------
- results : List[IterSumExpr]
+ results : IterMapResult
The iter map matching result.
- Empty array if no match can be found.
+ The result's .indices is empty array if no match can be found.
"""
return _ffi_api.DetectIterMap(
indices, input_iters, predicate, require_bijective, simplify_trivial_iterators
- )
+ ).indices
Review Comment:
I think this line disagrees with the return type in the docstring. We should either remove the `.indices` to return a `IterMapResult`, or revert the docstring changes to describe returning a `IterMapResult`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r884082835
##########
src/arith/iter_affine_map.cc:
##########
@@ -1659,7 +1676,7 @@ bool IterMapRewriter::CanProveDivisible(const PrimExpr& lhs, const PrimExpr& rhs
PrimExpr divisor = normalizer.Convert(rhs);
return analyzer_->CanProveEqual(dividend, divisor) ||
- analyzer_->CanProve(floormod(dividend, divisor) == 0);
+ analyzer_->CanProve(analyzer_->Simplify(floormod(dividend, divisor), 8) == 0);
Review Comment:
Sorry, that is something forget to revert. There is some cases the division could not be proved like
`floormod(0 + -x * 8, x) == 0`, `floormod(8*c1*c2, c1) == 0`, even we increate iteration num. They get work-around here and there, for example,
```c++
if (CanProveDivisible(right_edge, divisor)) {
right_pad = 0;
} else {
right_pad = analyzer_->Simplify(floormod(-right_edge, divisor));
}
```
@Lunderberg suggest `Simplify` could be optimized to iterate until reaching fix point. But now it is suffice to work on existing tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r881451562
##########
src/arith/iter_affine_map.cc:
##########
@@ -1062,58 +1099,59 @@ PaddedIterMapResult DetectPaddedIterMap(const Array<PrimExpr>& indices,
[](const IterConstraint& a, const IterConstraint& b) { return a.expr_size < b.expr_size; });
IterMapRewriter rewriter(analyzer, constrained_input_iters, simplify_trivial_iterators,
- &result.errors);
+ &result->errors);
// Step0.0: rewrite constraints in the order from size-small ones to size-big ones
for (const IterConstraint& constraint : constraints) {
auto res = rewriter.RewriteIterConstraint(constraint.iter, constraint.lower_bound,
constraint.upper_bound);
- if (result.errors.size()) {
- return result;
+ if (result->errors.size()) {
+ return result_obj;
}
}
if (!rewriter.CheckConstraints()) {
- result.errors.push_back("Invalid constraints.");
- return result;
+ result->errors.push_back("Invalid constraints.");
+ return result_obj;
}
// Step0.1: Check each index to determine required padding
- bool allow_padding = !require_bijective;
+ bool allow_padding = check_level != IterMapLevel::Bijective;
Review Comment:
I agree! This is where we should be careful. In `CheckMapping` with surjective mode when padding exists, we check `padded // LCM` and `padded % LCM`(or it's sub-splits) must not both exists. The case below depict this check:
```python
sum = 80 + y
dom_map = var_dom([(y, 176)])
# (80 + y) // 32 itself could be surjective
assert_iter_sum_pattern(
{fld(sum, 32): (6, 2, 1)},
dom_map,
)
# (80 + y) % 2, ((80 + y) // 2) % 16) could be surjective,
# since they can be seen as sub-splits of (80 + y) % 32
assert_iter_sum_pattern(
{flm(fld(sum, 2), 16): (16, 0, 1), flm(sum, 2): (2, 0, 1)},
dom_map,
)
# but (80 + y) // 32, (80 + y) % 32 are not surjective
assert_iter_sum_failure({fld(sum, 32), flm(sum, 32)}, dom_map)
```
Other kinds of negatives like `(80 + y) // 32, (80 + y) // 4` would be banned by existing checking rule.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] vinx13 merged pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
vinx13 merged PR #11287:
URL: https://github.com/apache/tvm/pull/11287
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880006145
##########
include/tvm/arith/iter_affine_map.h:
##########
@@ -259,53 +259,29 @@ class IterSumExpr : public IterMapExpr {
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode);
};
+/*! \brief Mapping level for iterators. */
+enum IterMapLevel {
Review Comment:
Yeah, we also have some case like `A[i // 8] = B[i % 16]` which could lower to some hw-specified data movement intrin. If we consider the mapping `i // 8` -> `i % 16` it could be neither surjective nor injective. But now the `DetectIterMap` try to solve the mapping from iter variables of non-strided domain to iter expression, thus I think it may not be able to handle such things directly. Maybe we can consider extent the input argument to two group of indices in the future?
Currently we could possibly handle it via bi-directional analysis from/to variable `i`:
i -> i // 8 : surjective
i -> i % 16: surjective, and injective reversely
Thus i // 8 -> i % 16 is something can be composed of surjective mapping and injective mapping.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] vinx13 commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
vinx13 commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r884007873
##########
src/arith/iter_affine_map.cc:
##########
@@ -1659,7 +1676,7 @@ bool IterMapRewriter::CanProveDivisible(const PrimExpr& lhs, const PrimExpr& rhs
PrimExpr divisor = normalizer.Convert(rhs);
return analyzer_->CanProveEqual(dividend, divisor) ||
- analyzer_->CanProve(floormod(dividend, divisor) == 0);
+ analyzer_->CanProve(analyzer_->Simplify(floormod(dividend, divisor), 8) == 0);
Review Comment:
it would be great to have some explanations here that it need more simplification steps
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] vinx13 commented on pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
vinx13 commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1141635183
Could you also update this line https://github.com/apache/tvm/blob/main/src/tir/schedule/primitive/layout_transformation.cc#L395? There are some conflict that CI didn't catch because of concurrent merge
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] tqchen commented on pull request #11287: [Arith][Simplify] Extend simplify rule for floormod(x * c1 + y, c2)
Posted by GitBox <gi...@apache.org>.
tqchen commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1125031761
@wrongtest yes we should cover simplifier's behavior, but the rewrite_simplifier testcase should be sufficient for now
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] junrushao1994 commented on pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1135507092
Quick note: #11235 is merged
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Lunderberg commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880594835
##########
src/arith/iter_affine_map.cc:
##########
@@ -1062,58 +1099,59 @@ PaddedIterMapResult DetectPaddedIterMap(const Array<PrimExpr>& indices,
[](const IterConstraint& a, const IterConstraint& b) { return a.expr_size < b.expr_size; });
IterMapRewriter rewriter(analyzer, constrained_input_iters, simplify_trivial_iterators,
- &result.errors);
+ &result->errors);
// Step0.0: rewrite constraints in the order from size-small ones to size-big ones
for (const IterConstraint& constraint : constraints) {
auto res = rewriter.RewriteIterConstraint(constraint.iter, constraint.lower_bound,
constraint.upper_bound);
- if (result.errors.size()) {
- return result;
+ if (result->errors.size()) {
+ return result_obj;
}
}
if (!rewriter.CheckConstraints()) {
- result.errors.push_back("Invalid constraints.");
- return result;
+ result->errors.push_back("Invalid constraints.");
+ return result_obj;
}
// Step0.1: Check each index to determine required padding
- bool allow_padding = !require_bijective;
+ bool allow_padding = check_level != IterMapLevel::Bijective;
Review Comment:
Good point, and that does maintain surjectivity for a single index. I'm not entirely sure for the case of two indices, though. For the same `x ∈ [0,8)`, the indices `[(x+7)//8, (x+7)%8]` would have the same padding `left_pad=7` and `right_pad=1`. Even though each individual index can take any value in the output (`(x+7)//8 ∈[0,2)` and `(x+7)%8 ∈ [0,8)`), there are some coordinate pairs that cannot be generated for any value of `x` (e.g. `[0,0]` and `[1,7]`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r881811402
##########
src/arith/iter_affine_map.cc:
##########
@@ -1263,140 +1285,135 @@ IterSumExpr IterMapRewriter::PreprocessDividend(IterMapExpr dividend, PrimExpr o
}
}
+/*! \brief Find approximate least common multiplier. */
+PrimExpr ApproxLeastCommonMultiple(const PrimExpr& a, const PrimExpr& b, Analyzer* analyzer) {
+ auto fsplit = [](const PrimExpr& e) -> std::pair<PrimExpr, int64_t> {
+ if (const IntImmNode* imm = e.as<IntImmNode>()) {
+ return {1, imm->value};
+ }
+ PVar<PrimExpr> pv;
+ PVar<IntImm> pc;
+ if ((pv * pc).Match(e) || (pc * pv).Match(e)) {
+ return {pv.Eval(), pc.Eval()->value};
+ } else {
+ return {e, 1};
+ }
+ };
+ auto p1 = fsplit(a);
+ auto p2 = fsplit(b);
+ auto const_lcm = Integer(LeastCommonMultiple(p1.second, p2.second));
+ if (analyzer->CanProveEqual(p1.first, p2.first)) {
+ return p1.first * const_lcm;
+ } else {
+ return (p1.first * p2.first) * const_lcm;
+ }
+}
+
std::pair<IterSplitExpr, PrimExpr> IterMapRewriter::PadDividendToDivisor(IterSplitExpr split,
PrimExpr base,
PrimExpr divisor) {
// If FloorDiv: (((source//lower_factor) % extent) + base) // divisor
// If FloorMod: (((source//lower_factor) % extent) + base) % divisor
- PrimExpr lookup_key = split;
-
- auto modified_divisor = [&]() {
- if (update_iterator_padding_) {
- return divisor;
- }
-
- auto it = padded_iter_map_.find(lookup_key);
- if (it == padded_iter_map_.end()) {
- return divisor;
- }
-
- const std::vector<PrimExpr>& divisors = it->second.divisors;
- PrimExpr largest_divisor = divisor;
- for (const auto& other : divisors) {
- if (CanProveDivisible(other, largest_divisor)) {
- // New one is bigger, use it
- largest_divisor = other;
- } else if (CanProveDivisible(largest_divisor, other)) {
- // Current is bigger, keep it
- } else {
- ErrorLogger(this) << "Iterator appears in multiple terms with incompatible divisors "
- << tvm::PrettyPrint(largest_divisor) << " and "
- << tvm::PrettyPrint(other);
- }
- }
- return largest_divisor;
- }();
-
- divisor = modified_divisor;
-
// First, adding any padding that is on the lower side of a
- // FloorDiv/FloorMod, such that floormod(iter-left_pad,divisor) == 0
- // when iter==0.
-
- PrimExpr left_pad;
-
- if (is_zero(base)) {
- // Padding on the left is unnecessary if base is known to be zero.
- left_pad = make_zero(base->dtype);
- } else {
- left_pad = analyzer_->Simplify(floormod(base, divisor));
- }
+ // FloorDiv/FloorMod, such that floormod(split - left_pad, divisor) == 0
+ // when iter == 0.
+ PrimExpr left_pad = analyzer_->Simplify(floormod(base, divisor));
// Next, adding any padding that is on the upper side of a
- // FloorDiv/FloorMod, such that floormod(left_pad + iter + right_pad, divisor) == 0
- // when iter==extent.
-
+ // FloorDiv/FloorMod, such that floormod(left_pad + split + right_pad, divisor) == 0
+ // when iter == extent.
PrimExpr right_edge = left_pad + split->extent;
PrimExpr right_pad;
-
if (CanProveDivisible(right_edge, divisor)) {
- // Padding on the right is unnecessary if the extent is a multiple of
- // the divisor.
right_pad = 0;
} else {
right_pad = analyzer_->Simplify(floormod(-right_edge, divisor));
}
- if (is_zero(left_pad) && is_zero(right_pad)) {
- return {split, left_pad};
- }
-
+ const IterMark& mark = split->source;
if (update_iterator_padding_) {
// In the first pass, the primary goal is to collect all the divisors
- // that may be used for padding. These will impact the divisor used
- // to determine padding in the second pass.
- IterPaddingInfo& info = padded_iter_map_[lookup_key];
-
- info.divisors.push_back(divisor);
-
- PrimExpr padded_extent = left_pad + split->extent + right_pad;
-
- IterSumExpr as_sum({split}, left_pad);
- IterMark mark(as_sum, padded_extent);
- IterSplitExpr new_split(mark);
-
- return {new_split, left_pad};
+ // that may be used for padding. These will impact the divisor used
+ // to determine padding in the second pass. We try add padding to
+ // split's source iteraton mark thus all splits under the same mark will
+ // share the same padded source iteration.
+ auto& info = padded_iter_map_[mark];
+ info.padding_factor =
+ ApproxLeastCommonMultiple(info.padding_factor, divisor * split->lower_factor, analyzer_);
+
+ // If the split itself require no padding, return directly.
+ if (is_zero(left_pad) && is_zero(right_pad)) {
+ return {split, 0};
+ }
+
+ // Update padding requirement on the lower side of the source iter mark.
+ PrimExpr mark_left_pad = left_pad * split->lower_factor;
+ info.left_pad = max(info.left_pad, mark_left_pad);
Review Comment:
need check what happened if different splits take incompatible left pads.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] junrushao1994 commented on pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
junrushao1994 commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1142780392
One bug from my side is magically fixed by this PR!!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] vinx13 commented on pull request #11287: [Arith][Simplify] Extend simplify rule for floormod(x * c1 + y, c2)
Posted by GitBox <gi...@apache.org>.
vinx13 commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1126310109
LGTM, let's have #11235 merged first
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on pull request #11287: [Arith][Simplify] Extend simplify rule for floormod(x * c1 + y, c2)
Posted by GitBox <gi...@apache.org>.
wrongtest commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1126255284
The failed compute_at's region cover check possibly could get fixed by https://github.com/apache/tvm/pull/11235's improvement on iteration analysis.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880006145
##########
include/tvm/arith/iter_affine_map.h:
##########
@@ -259,53 +259,29 @@ class IterSumExpr : public IterMapExpr {
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode);
};
+/*! \brief Mapping level for iterators. */
+enum IterMapLevel {
Review Comment:
Yeah, we also have some case like `A[i // 8] = B[i % 16]` which could lower to some hw-specified data movement intrin. If we consider the mapping `i // 8` -> `i % 16` it could be neither surjective nor injective. But now the `DetectIterMap` try to solve the mapping from iter variables of non-strided domain to iter expression, thus I think it may not be able to handle such things directly. Maybe we can consider extent the input argument to two group of indices in the future?
Currently we could possibly handle it via bi-directional analysis from/to variable `i`:
i -> i // 8 : surjective
i -> i % 16: surjective, and injective reversely
Thus i // 8 -> i % 16 is something can be composed of surjective mapping and injective mapping.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Hzfengsy commented on pull request #11287: [Arith][Simplify] Extend simplify rule for floormod(x * c1 + y, c2)
Posted by GitBox <gi...@apache.org>.
Hzfengsy commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1134337378
A gentle ping for @vinx13
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] vinx13 commented on pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
vinx13 commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1135017779
@wrongtest Can you elaborate the usage of `DetectIterMapPadded` in our analysis? Do we need the padding information?
also cc @Lunderberg for `DetectIterMap` changes
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880008164
##########
src/arith/iter_affine_map.cc:
##########
@@ -1260,140 +1300,132 @@ IterSumExpr IterMapRewriter::PreprocessDividend(IterMapExpr dividend) {
}
}
+PrimExpr NearLeastCommonMultiple(const PrimExpr& a, const PrimExpr& b, Analyzer* analyzer) {
+ auto fsplit = [](const PrimExpr& e) -> std::pair<PrimExpr, int64_t> {
+ if (const IntImmNode* imm = e.as<IntImmNode>()) {
+ return {1, imm->value};
+ }
+ PVar<PrimExpr> pv;
+ PVar<IntImm> pc;
+ if ((pv * pc).Match(e) || (pc * pv).Match(e)) {
+ return {pv.Eval(), pc.Eval()->value};
+ } else {
+ return {e, 1};
+ }
+ };
+
+ auto p1 = fsplit(a);
+ auto p2 = fsplit(b);
+ auto const_lcm = Integer(LeastCommonMultiple(p1.second, p2.second));
+ if (analyzer->CanProveEqual(p1.first, p2.first)) {
+ return p1.first * const_lcm;
+ } else {
+ return (p1.first * p2.first) * const_lcm;
+ }
+}
+
std::pair<IterSplitExpr, PrimExpr> IterMapRewriter::PadDividendToDivisor(IterSplitExpr split,
PrimExpr base,
PrimExpr divisor) {
// If FloorDiv: (((source//lower_factor) % extent) + base) // divisor
// If FloorMod: (((source//lower_factor) % extent) + base) % divisor
- PrimExpr lookup_key = split;
-
- auto modified_divisor = [&]() {
- if (update_iterator_padding_) {
- return divisor;
- }
-
- auto it = padded_iter_map_.find(lookup_key);
- if (it == padded_iter_map_.end()) {
- return divisor;
- }
-
- const std::vector<PrimExpr>& divisors = it->second.divisors;
- PrimExpr largest_divisor = divisor;
- for (const auto& other : divisors) {
- if (CanProveDivisible(other, largest_divisor)) {
- // New one is bigger, use it
- largest_divisor = other;
- } else if (CanProveDivisible(largest_divisor, other)) {
- // Current is bigger, keep it
- } else {
- ErrorLogger(this) << "Iterator appears in multiple terms with incompatible divisors "
- << tvm::PrettyPrint(largest_divisor) << " and "
- << tvm::PrettyPrint(other);
- }
- }
- return largest_divisor;
- }();
-
- divisor = modified_divisor;
-
+ // Update current iteration split's padding.
// First, adding any padding that is on the lower side of a
// FloorDiv/FloorMod, such that floormod(iter-left_pad,divisor) == 0
// when iter==0.
-
- PrimExpr left_pad;
-
- if (is_zero(base)) {
- // Padding on the left is unnecessary if base is known to be zero.
- left_pad = make_zero(base->dtype);
- } else {
- left_pad = analyzer_->Simplify(floormod(base, divisor));
- }
+ PrimExpr left_pad = analyzer_->Simplify(floormod(base, divisor));
// Next, adding any padding that is on the upper side of a
// FloorDiv/FloorMod, such that floormod(left_pad + iter + right_pad, divisor) == 0
// when iter==extent.
-
PrimExpr right_edge = left_pad + split->extent;
PrimExpr right_pad;
-
if (CanProveDivisible(right_edge, divisor)) {
- // Padding on the right is unnecessary if the extent is a multiple of
- // the divisor.
right_pad = 0;
} else {
- right_pad = analyzer_->Simplify(floormod(-right_edge, divisor));
- }
-
- if (is_zero(left_pad) && is_zero(right_pad)) {
- return {split, left_pad};
+ right_pad = analyzer_->Simplify(floormod(-right_edge, divisor), 9);
}
if (update_iterator_padding_) {
+ IterMark mark = split->source;
+ auto& info = padded_iter_map_[mark];
+ info.padding_factor =
+ NearLeastCommonMultiple(info.padding_factor, divisor * split->lower_factor, analyzer_);
+
+ if (is_zero(left_pad) && is_zero(right_pad)) {
+ return {split, 0};
+ }
+
// In the first pass, the primary goal is to collect all the divisors
// that may be used for padding. These will impact the divisor used
// to determine padding in the second pass.
- IterPaddingInfo& info = padded_iter_map_[lookup_key];
-
- info.divisors.push_back(divisor);
+ PrimExpr padded_extent = analyzer_->Simplify(left_pad + split->extent + right_pad);
- PrimExpr padded_extent = left_pad + split->extent + right_pad;
-
- IterSumExpr as_sum({split}, left_pad);
- IterMark mark(as_sum, padded_extent);
- IterSplitExpr new_split(mark);
-
- return {new_split, left_pad};
+ PrimExpr mark_left_pad = left_pad * split->lower_factor;
+ if (!is_zero(left_pad)) {
+ if (info.left_pad.defined()) {
+ info.left_pad = max(info.left_pad, mark_left_pad);
+ } else {
+ info.left_pad = mark_left_pad;
+ }
+ }
+ split.CopyOnWrite()->extent = padded_extent;
+ return {split, left_pad};
}
- // Any padding that is required during parsing should have been found
- // during the first pass that determines the GCD.
- auto it = padded_iter_map_.find(lookup_key);
+ // In the second pass, update iteration mark's to padded
+ const IterMark& mark = split->source;
+ auto it = padded_iter_map_.find(mark);
if (it == padded_iter_map_.end()) {
- ErrorLogger(this) << "Dividend has extent " << tvm::PrettyPrint(split->extent) << " and offset "
- << tvm::PrettyPrint(base) << ", which requires padding for divisor "
- << tvm::PrettyPrint(divisor) << ".";
- return {IterSplitExpr(), left_pad};
+ return {split, left_pad};
}
- IterPaddingInfo& info = it->second;
-
- if (info.padded.defined()) {
- // A previous visit already applied padding to this iterator.
- // (e.g. Visiting `(i+1)//4`, then visiting `(i+1)%4`).
- ICHECK(analyzer_->CanProveEqual(info.left_pad, left_pad));
- ICHECK(analyzer_->CanProveEqual(info.right_pad, right_pad));
-
- return {info.padded, left_pad};
+ auto& info = it->second;
+ if (is_zero(info.left_pad.defined() ? info.left_pad : 0) &&
+ CanProveDivisible(mark->extent, info.padding_factor)) {
+ return {split, left_pad};
}
- // This is the first encounter with the iterator during the second pass.
- IterSumExpr as_sum({split}, left_pad);
- IterMark mark(as_sum, left_pad + split->extent + right_pad);
- info.padded = IterSplitExpr(mark);
- info.left_pad = left_pad;
- info.right_pad = right_pad;
-
- auto left_padding_introduced = (left_pad != 0);
- // Equivalent to (0 <= split < left_pad), but easier to simplify in
- // terms of the transformed variables.
- auto left_padding_predicate =
- left_padding_introduced && (floordiv(info.padded, divisor) == floordiv(base, divisor) &&
- floormod(info.padded, divisor) < left_pad);
-
- PrimExpr nparts = ceildiv(right_edge, divisor);
-
- auto right_padding_introduced = (right_pad != 0);
-
- // Equivalent to (right_edge <= split < right_edge+right_pad), but
- // easier to simplify in terms of the transformed variables.
- auto right_padding_predicate = right_padding_introduced &&
- (floordiv(info.padded, divisor) == floordiv(right_edge, divisor) &&
- floormod(info.padded, divisor) >= floormod(right_edge, divisor));
-
- requires_padding_ = requires_padding_ || (left_padding_introduced || right_padding_introduced);
- padding_predicate_ = padding_predicate_ || (left_padding_predicate || right_padding_predicate);
-
- return {info.padded, left_pad};
+ if (!info.padded.defined()) {
+ PrimExpr mark_left_pad = info.left_pad.defined() ? info.left_pad : 0;
+ PrimExpr mark_right_pad;
+ if (CanProveDivisible(mark->extent + mark_left_pad, info.padding_factor)) {
+ mark_right_pad = 0;
+ } else {
+ mark_right_pad = floormod(-(mark->extent + mark_left_pad), info.padding_factor);
+ }
+ PrimExpr padded_extent = analyzer_->Simplify(mark_left_pad + mark->extent + mark_right_pad);
+ info.right_pad = mark_right_pad;
+ info.padded = IterMark(IterSumExpr({IterSplitExpr(mark)}, mark_left_pad), padded_extent);
+
+ auto left_padding_introduced = (mark_left_pad != 0);
+ PrimExpr divisor = info.padding_factor;
+ PrimExpr right_edge = mark_left_pad + mark->extent;
+
+ // Equivalent to (0 <= split < left_pad), but easier to simplify in
+ // terms of the transformed variables.
+ auto left_padding_predicate =
+ left_padding_introduced && (floordiv(info.padded->source, divisor) == 0 &&
+ floormod(info.padded->source, divisor) < mark_left_pad);
+
+ auto right_padding_introduced = (mark_right_pad != 0);
+
+ // Equivalent to (right_edge <= split < right_edge+right_pad), but
+ // easier to simplify in terms of the transformed variables.
+ auto right_padding_predicate =
+ right_padding_introduced &&
+ (floordiv(info.padded->source, divisor) == floordiv(right_edge, divisor) &&
+ floormod(info.padded->source, divisor) >= floormod(right_edge, divisor));
+
+ requires_padding_ = requires_padding_ || (left_padding_introduced || right_padding_introduced);
+ padding_predicate_ = padding_predicate_ || (left_padding_predicate || right_padding_predicate);
+ }
+ // ICHECK(CanProveDivisible(info.padded->extent, split->lower_factor));
Review Comment:
Would like to check the padding factor is divisible by `split->lower_factor`, then the commented check can be ensured from context. I found it may fail unfortunetely due to simplifier's ability limitation when the padded extent contain complex flm/fld expressions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] Lunderberg commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
Lunderberg commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880579967
##########
src/arith/iter_affine_map.cc:
##########
@@ -1260,140 +1300,132 @@ IterSumExpr IterMapRewriter::PreprocessDividend(IterMapExpr dividend) {
}
}
+PrimExpr NearLeastCommonMultiple(const PrimExpr& a, const PrimExpr& b, Analyzer* analyzer) {
+ auto fsplit = [](const PrimExpr& e) -> std::pair<PrimExpr, int64_t> {
+ if (const IntImmNode* imm = e.as<IntImmNode>()) {
+ return {1, imm->value};
+ }
+ PVar<PrimExpr> pv;
+ PVar<IntImm> pc;
+ if ((pv * pc).Match(e) || (pc * pv).Match(e)) {
+ return {pv.Eval(), pc.Eval()->value};
+ } else {
+ return {e, 1};
+ }
+ };
+
+ auto p1 = fsplit(a);
+ auto p2 = fsplit(b);
+ auto const_lcm = Integer(LeastCommonMultiple(p1.second, p2.second));
+ if (analyzer->CanProveEqual(p1.first, p2.first)) {
+ return p1.first * const_lcm;
+ } else {
+ return (p1.first * p2.first) * const_lcm;
+ }
+}
+
std::pair<IterSplitExpr, PrimExpr> IterMapRewriter::PadDividendToDivisor(IterSplitExpr split,
PrimExpr base,
PrimExpr divisor) {
// If FloorDiv: (((source//lower_factor) % extent) + base) // divisor
// If FloorMod: (((source//lower_factor) % extent) + base) % divisor
- PrimExpr lookup_key = split;
-
- auto modified_divisor = [&]() {
- if (update_iterator_padding_) {
- return divisor;
- }
-
- auto it = padded_iter_map_.find(lookup_key);
- if (it == padded_iter_map_.end()) {
- return divisor;
- }
-
- const std::vector<PrimExpr>& divisors = it->second.divisors;
- PrimExpr largest_divisor = divisor;
- for (const auto& other : divisors) {
- if (CanProveDivisible(other, largest_divisor)) {
- // New one is bigger, use it
- largest_divisor = other;
- } else if (CanProveDivisible(largest_divisor, other)) {
- // Current is bigger, keep it
- } else {
- ErrorLogger(this) << "Iterator appears in multiple terms with incompatible divisors "
- << tvm::PrettyPrint(largest_divisor) << " and "
- << tvm::PrettyPrint(other);
- }
- }
- return largest_divisor;
- }();
-
- divisor = modified_divisor;
-
+ // Update current iteration split's padding.
// First, adding any padding that is on the lower side of a
// FloorDiv/FloorMod, such that floormod(iter-left_pad,divisor) == 0
// when iter==0.
-
- PrimExpr left_pad;
-
- if (is_zero(base)) {
- // Padding on the left is unnecessary if base is known to be zero.
- left_pad = make_zero(base->dtype);
- } else {
- left_pad = analyzer_->Simplify(floormod(base, divisor));
- }
+ PrimExpr left_pad = analyzer_->Simplify(floormod(base, divisor));
// Next, adding any padding that is on the upper side of a
// FloorDiv/FloorMod, such that floormod(left_pad + iter + right_pad, divisor) == 0
// when iter==extent.
-
PrimExpr right_edge = left_pad + split->extent;
PrimExpr right_pad;
-
if (CanProveDivisible(right_edge, divisor)) {
- // Padding on the right is unnecessary if the extent is a multiple of
- // the divisor.
right_pad = 0;
} else {
- right_pad = analyzer_->Simplify(floormod(-right_edge, divisor));
- }
-
- if (is_zero(left_pad) && is_zero(right_pad)) {
- return {split, left_pad};
+ right_pad = analyzer_->Simplify(floormod(-right_edge, divisor), 9);
}
if (update_iterator_padding_) {
+ IterMark mark = split->source;
+ auto& info = padded_iter_map_[mark];
+ info.padding_factor =
+ NearLeastCommonMultiple(info.padding_factor, divisor * split->lower_factor, analyzer_);
+
+ if (is_zero(left_pad) && is_zero(right_pad)) {
+ return {split, 0};
+ }
+
// In the first pass, the primary goal is to collect all the divisors
// that may be used for padding. These will impact the divisor used
// to determine padding in the second pass.
- IterPaddingInfo& info = padded_iter_map_[lookup_key];
-
- info.divisors.push_back(divisor);
+ PrimExpr padded_extent = analyzer_->Simplify(left_pad + split->extent + right_pad);
- PrimExpr padded_extent = left_pad + split->extent + right_pad;
-
- IterSumExpr as_sum({split}, left_pad);
- IterMark mark(as_sum, padded_extent);
- IterSplitExpr new_split(mark);
-
- return {new_split, left_pad};
+ PrimExpr mark_left_pad = left_pad * split->lower_factor;
+ if (!is_zero(left_pad)) {
+ if (info.left_pad.defined()) {
+ info.left_pad = max(info.left_pad, mark_left_pad);
+ } else {
+ info.left_pad = mark_left_pad;
+ }
+ }
+ split.CopyOnWrite()->extent = padded_extent;
+ return {split, left_pad};
}
- // Any padding that is required during parsing should have been found
- // during the first pass that determines the GCD.
- auto it = padded_iter_map_.find(lookup_key);
+ // In the second pass, update iteration mark's to padded
+ const IterMark& mark = split->source;
+ auto it = padded_iter_map_.find(mark);
if (it == padded_iter_map_.end()) {
- ErrorLogger(this) << "Dividend has extent " << tvm::PrettyPrint(split->extent) << " and offset "
- << tvm::PrettyPrint(base) << ", which requires padding for divisor "
- << tvm::PrettyPrint(divisor) << ".";
- return {IterSplitExpr(), left_pad};
+ return {split, left_pad};
}
- IterPaddingInfo& info = it->second;
-
- if (info.padded.defined()) {
- // A previous visit already applied padding to this iterator.
- // (e.g. Visiting `(i+1)//4`, then visiting `(i+1)%4`).
- ICHECK(analyzer_->CanProveEqual(info.left_pad, left_pad));
- ICHECK(analyzer_->CanProveEqual(info.right_pad, right_pad));
-
- return {info.padded, left_pad};
+ auto& info = it->second;
+ if (is_zero(info.left_pad.defined() ? info.left_pad : 0) &&
+ CanProveDivisible(mark->extent, info.padding_factor)) {
+ return {split, left_pad};
}
- // This is the first encounter with the iterator during the second pass.
- IterSumExpr as_sum({split}, left_pad);
- IterMark mark(as_sum, left_pad + split->extent + right_pad);
- info.padded = IterSplitExpr(mark);
- info.left_pad = left_pad;
- info.right_pad = right_pad;
-
- auto left_padding_introduced = (left_pad != 0);
- // Equivalent to (0 <= split < left_pad), but easier to simplify in
- // terms of the transformed variables.
- auto left_padding_predicate =
- left_padding_introduced && (floordiv(info.padded, divisor) == floordiv(base, divisor) &&
- floormod(info.padded, divisor) < left_pad);
-
- PrimExpr nparts = ceildiv(right_edge, divisor);
-
- auto right_padding_introduced = (right_pad != 0);
-
- // Equivalent to (right_edge <= split < right_edge+right_pad), but
- // easier to simplify in terms of the transformed variables.
- auto right_padding_predicate = right_padding_introduced &&
- (floordiv(info.padded, divisor) == floordiv(right_edge, divisor) &&
- floormod(info.padded, divisor) >= floormod(right_edge, divisor));
-
- requires_padding_ = requires_padding_ || (left_padding_introduced || right_padding_introduced);
- padding_predicate_ = padding_predicate_ || (left_padding_predicate || right_padding_predicate);
-
- return {info.padded, left_pad};
+ if (!info.padded.defined()) {
+ PrimExpr mark_left_pad = info.left_pad.defined() ? info.left_pad : 0;
+ PrimExpr mark_right_pad;
+ if (CanProveDivisible(mark->extent + mark_left_pad, info.padding_factor)) {
+ mark_right_pad = 0;
+ } else {
+ mark_right_pad = floormod(-(mark->extent + mark_left_pad), info.padding_factor);
+ }
+ PrimExpr padded_extent = analyzer_->Simplify(mark_left_pad + mark->extent + mark_right_pad);
+ info.right_pad = mark_right_pad;
+ info.padded = IterMark(IterSumExpr({IterSplitExpr(mark)}, mark_left_pad), padded_extent);
+
+ auto left_padding_introduced = (mark_left_pad != 0);
+ PrimExpr divisor = info.padding_factor;
+ PrimExpr right_edge = mark_left_pad + mark->extent;
+
+ // Equivalent to (0 <= split < left_pad), but easier to simplify in
+ // terms of the transformed variables.
+ auto left_padding_predicate =
+ left_padding_introduced && (floordiv(info.padded->source, divisor) == 0 &&
+ floormod(info.padded->source, divisor) < mark_left_pad);
+
+ auto right_padding_introduced = (mark_right_pad != 0);
+
+ // Equivalent to (right_edge <= split < right_edge+right_pad), but
+ // easier to simplify in terms of the transformed variables.
+ auto right_padding_predicate =
+ right_padding_introduced &&
+ (floordiv(info.padded->source, divisor) == floordiv(right_edge, divisor) &&
+ floormod(info.padded->source, divisor) >= floormod(right_edge, divisor));
+
+ requires_padding_ = requires_padding_ || (left_padding_introduced || right_padding_introduced);
+ padding_predicate_ = padding_predicate_ || (left_padding_predicate || right_padding_predicate);
+ }
+ // ICHECK(CanProveDivisible(info.padded->extent, split->lower_factor));
Review Comment:
Got it. I noticed that there were also some simplification steps that needed to increase the number of iterations performed. Is the failure to prove divisibility related, since `CanProveDivisible` only uses the default of 2 steps?
(I'm also wondering if the default for `Analyzer::Simplify` should be to iterate until it the simplification converges, rather than using a fixed number of steps.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880010949
##########
src/arith/iter_affine_map.cc:
##########
@@ -1062,58 +1099,59 @@ PaddedIterMapResult DetectPaddedIterMap(const Array<PrimExpr>& indices,
[](const IterConstraint& a, const IterConstraint& b) { return a.expr_size < b.expr_size; });
IterMapRewriter rewriter(analyzer, constrained_input_iters, simplify_trivial_iterators,
- &result.errors);
+ &result->errors);
// Step0.0: rewrite constraints in the order from size-small ones to size-big ones
for (const IterConstraint& constraint : constraints) {
auto res = rewriter.RewriteIterConstraint(constraint.iter, constraint.lower_bound,
constraint.upper_bound);
- if (result.errors.size()) {
- return result;
+ if (result->errors.size()) {
+ return result_obj;
}
}
if (!rewriter.CheckConstraints()) {
- result.errors.push_back("Invalid constraints.");
- return result;
+ result->errors.push_back("Invalid constraints.");
+ return result_obj;
}
// Step0.1: Check each index to determine required padding
- bool allow_padding = !require_bijective;
+ bool allow_padding = check_level != IterMapLevel::Bijective;
Review Comment:
That is the claim~ I try to change padding to iter mark itself.
For example,`(x + 7)` `x` in [0, 8) => `IterMark(IterSplit(IterSum({x}, 7), lower_factor=1, extent=16, scale=1), extent=16` with left_pad=7, right_pad=1
Then `(x + 7) // 8` is mapped to range [0, extent//2) == [0, 2), though we have padding into iter mark, the IterSplit's range can be achieved when we only iterate `x` in it's original domain: (0 + 7) // 8 = 0, (7 + 7) // 8 = 1
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r880030467
##########
include/tvm/arith/iter_affine_map.h:
##########
@@ -259,53 +259,29 @@ class IterSumExpr : public IterMapExpr {
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode);
};
+/*! \brief Mapping level for iterators. */
+enum IterMapLevel {
Review Comment:
Oh, "Injective" should be renamed to "NoCheck" now, there is no checking implementation on injective-ness...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on pull request #11287: [Arith][Simplify] Extend simplify rule for floormod(x * c1 + y, c2)
Posted by GitBox <gi...@apache.org>.
wrongtest commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1131306560
To enable region cover proof on such cases, we need to lift `DetectIterMapPadded` to standard implementation for `DetectIterMap`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1135062872
> usage of `DetectIterMapPadded` in our analysis
Try merge `DetectIterMapPadded` and `DetectIterMap` into the same interface, and replace option `require_bijective` with a new enum `IterMapLevel` with three alternatives:
- Bijective
for original behavior on `require_bijective=true`
- Surjective
for original behavior on `require_bijective=false`
- Injective
for behavior of `DetectIterMapPadded`
The https://github.com/apache/tvm/pull/11235 brings great way to analyze iteration form like `(x + 7) // 16` with padding. The surjective checking of `DetectIterMap` is used many where (like region cover check after schedule step), however, it can not leverage this analysis now, it is checked to take no padding predicate.
I think actually, as an example, though `(x + 7) // 16` is rewritten into a "padded" iteration form, we could still prove the mapping is surjective, since the left and right padding is no more than the largest divisor by how we pad it. If we extent `CheckMapping` rules carefully, we may be able to distinguish that
- `(x + 7) // 16` -> surjective
- this is the access index form in my original failed case
- `(x + 7) % 16` -> surjective [0, 16) if x's extent is larger than 16
- (`(x + 7) // 16`, `(x + 7) % 16`) -> non-surjective
So from my perspective it would be great if we have a uniform interface and share same padding based analysis. Ideally `padding_predicate` is not affected for IndexMap functionalities, and it should not introduce false positives in bijective/surjective checking.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on a diff in pull request #11287: [Arith] Merge surjective/non-surjective iter mapping detections
Posted by GitBox <gi...@apache.org>.
wrongtest commented on code in PR #11287:
URL: https://github.com/apache/tvm/pull/11287#discussion_r881451562
##########
src/arith/iter_affine_map.cc:
##########
@@ -1062,58 +1099,59 @@ PaddedIterMapResult DetectPaddedIterMap(const Array<PrimExpr>& indices,
[](const IterConstraint& a, const IterConstraint& b) { return a.expr_size < b.expr_size; });
IterMapRewriter rewriter(analyzer, constrained_input_iters, simplify_trivial_iterators,
- &result.errors);
+ &result->errors);
// Step0.0: rewrite constraints in the order from size-small ones to size-big ones
for (const IterConstraint& constraint : constraints) {
auto res = rewriter.RewriteIterConstraint(constraint.iter, constraint.lower_bound,
constraint.upper_bound);
- if (result.errors.size()) {
- return result;
+ if (result->errors.size()) {
+ return result_obj;
}
}
if (!rewriter.CheckConstraints()) {
- result.errors.push_back("Invalid constraints.");
- return result;
+ result->errors.push_back("Invalid constraints.");
+ return result_obj;
}
// Step0.1: Check each index to determine required padding
- bool allow_padding = !require_bijective;
+ bool allow_padding = check_level != IterMapLevel::Bijective;
Review Comment:
I agree! This is where we should be careful. In `CheckMapping` with surjective mode when padding exists, we check `padded // LCM` and `padded % LCM`(or it's sub-splits) must not both exists. The case below depict this check:
```python
sum = 80 + y
dom_map = var_dom([(y, 176)])
# (80 + y) // 32 itself could be surjective
assert_iter_sum_pattern(
{fld(sum, 32): (6, 2, 1)},
dom_map,
)
# (80 + y) % 2, ((80 + y) // 2) % 16) could be surjective, since they can be seen as sub-splits of (80 + y) % 32
assert_iter_sum_pattern(
{flm(fld(sum, 2), 16): (16, 0, 1), flm(sum, 2): (2, 0, 1)},
dom_map,
)
# but (80 + y) // 32, (80 + y) % 32 are not surjective
assert_iter_sum_failure({fld(sum, 32), flm(sum, 32)}, dom_map)
```
Other kinds of negatives like `(80 + y) // 32, (80 + y) // 4` would be banned by existing checking rule.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] wrongtest commented on pull request #11287: [Arith][Simplify] Extend simplify rule for floormod(x * c1 + y, c2)
Posted by GitBox <gi...@apache.org>.
wrongtest commented on PR #11287:
URL: https://github.com/apache/tvm/pull/11287#issuecomment-1124928776
where (or is it neccesary) to write testcase on `analyzer.simplify()`'s behavior ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org