Skip to content
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

[ARITH][BUGFIX] Fix a bug of iter map floormod(x,2) simplify #14571

Merged
merged 1 commit into from
Apr 11, 2023

Conversation

tqchen
Copy link
Member

@tqchen tqchen commented Apr 10, 2023

This PR fixes a previous bug introduced in itermap detection.

Specifically, y - (x % 2) were simplified to y + (x % 2) - 1. Which is wrong. The working rule is y + ((x + 1) % 2) - 1, but that rule will change the base iterator which is not desirable here.

@tvm-bot
Copy link
Collaborator

tvm-bot commented Apr 10, 2023

Thanks for contributing to TVM! Please refer to the contributing guidelines https://tvm.apache.org/docs/contribute/ for useful information and tips. Please request code reviews from Reviewers by @-ing them in a comment.

Generated by tvm-bot

@tqchen
Copy link
Member Author

tqchen commented Apr 10, 2023

cc @Lunderberg @junrushao Please revisit some of the previous floormod(x,2) rules to see f there are similar kinds of problems. cc @wrongtest-intellif

@tqchen tqchen mentioned this pull request Apr 10, 2023
7 tasks
Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! This really took me a while to understand why this rewriting is not correct :-)

@wrongtest-intellif
Copy link
Contributor

wrongtest-intellif commented Apr 11, 2023

The case test_postproc_disallow_async_strided_mem_copy_allows wants to check below index is contiguous but unfortunately failed, I guess it is because rewrite rules in #13936 mutate the sign?

ax0 + 1024 - j % 2 * 1024

{i: T.Range(0, 1024), ax0: T.Range(0, 1024), j: T.Range(0, 1023), ax0: T.Range(0, 1024)}

Currently in iter map we could check 1*a + 1*b could be surjective but could not handle 1*a + -1*b

@tqchen
Copy link
Member Author

tqchen commented Apr 11, 2023

That is my guess as well. Would be great to confirm if that is the case. We should prioritize the iter map rewrite and avoid mutate sign of iter map expressions.

@Lunderberg
Copy link
Contributor

Thank you for finding the error there. The rewrite were initially introduced to allow simplification of cases of (x+const)%2 introduced by the InjectSoftwarePipeline pass, with equivalent changes made for DetectIterMap so that it could handle the simplified expressions.

I agree that the DetectIterMap changes are definitely incorrect, and am going through the rewrite rules introduced in that PR to check whether they had a similar error, or whether the error was solely in the IterMapSimplify changes.

@tqchen
Copy link
Member Author

tqchen commented Apr 11, 2023

I think the gist is that we should avoid to rewrite (x+1)%2 to 1-x%2 in general. Although this is correct, change positive sign to a negative coefficient will cause issues for iter map detection in general, and is not desirable.

@tqchen
Copy link
Member Author

tqchen commented Apr 11, 2023

Update: We also removed the rule that simplifies (x + 1) % 2 => 1 - x % 2
as benefit is minimal and it introduces extra negative coefficients on variables
that hurts analysis in general (as negative coefficients are
harder in many cases).

This hopefully can recover the failed case in test_postproc_disallow_async_strided_mem_copy_allow

The particular rewrites is not that useful in software pipeline because all the is are going to be unrolled

Some take aways

  • T0: We should avoid rewriting that generate extra negative coefficients on variables, even it might bring some benefit, negative coefficient makes iterator analysis harder
  • T1: We should avoid putting too much burdens on rewrite simplify, some of the examples(that covers long range dependencies) can be better covered by canonical as a result do not need to goto rewrite. Let us check the cases under broader simplifier before adding a new rule for long range dep

@Lunderberg
Copy link
Contributor

Thank you, and that makes sense for now. I'm seeing if I can find the expression that required this simplification step in order to enable a cancellation, but however it gets re-implemented should avoid the breakage in iter map simplification.

@tqchen
Copy link
Member Author

tqchen commented Apr 11, 2023

Would also be good to put up such expressions in TVMScript so we have context for new rules

For example, some of the (i +1) %2 expressions do not need simplification mainly because they are in an unrolled loop and the expression will become constant later

3rdparty/cutlass Outdated Show resolved Hide resolved
This PR fixes a previous bug introduced in itermap detection.

Specifically, y - (x % 2) were simplified to y + (x % 2) - 1.
Which is wrong. The working rule is  y + ((x + 1) % 2) - 1,
but that rule will change the base iterator which is not desirable here.

We also removed the rule that simplifies  (x + 1) % 2 => 1 - x % 2
as benefit is minimal and it introduces extra negative co-efficients
that hurts analysis in general (as negative co-efficients are
harder in many cases).
@junrushao
Copy link
Member

This is merged. Thanks for digging into the analysis!

ysh329 pushed a commit to ysh329/tvm that referenced this pull request Apr 23, 2023
…lify (apache#14571)

This PR fixes a previous bug introduced in itermap detection.

Specifically, y - (x % 2) were simplified to y + (x % 2) - 1.
Which is wrong. The working rule is  y + ((x + 1) % 2) - 1,
but that rule will change the base iterator which is not desirable here.

We also removed the rule that simplifies  (x + 1) % 2 => 1 - x % 2
as benefit is minimal and it introduces extra negative co-efficients
that hurts analysis in general (as negative co-efficients are
harder in many cases).
ysh329 added a commit to ysh329/tvm that referenced this pull request Apr 23, 2023
junrushao pushed a commit that referenced this pull request Apr 23, 2023
junrushao pushed a commit that referenced this pull request Apr 24, 2023
elvin-n pushed a commit to Deelvin/tvm that referenced this pull request May 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants