Skip to content

Conversation

@Lunderberg
Copy link
Contributor

Frequently, several related rewrite rules will have identical output patterns and preconditions, differing only by the form of the input pattern. For example, both (x + y) - y and (y + x) - y can be simplified to x. Previously, these would be written as separate rewrite rules, which required duplicating the output and preconditions.

This commit introduces PMatchesOneOf, which attempts a series of pattern matches, stopping after the first successful match. The PMatchesOneOf instance can be used as the input pattern for rewrite rules. A helper function matches_one_of is also added, which returns an instance of PMatchesOneOf.

// Before
TVM_TRY_REWRITE( (x + y) - y, x);
TVM_TRY_REWRITE( (y + x) - y, x);

// After
TVM_TRY_REWRITE( matches_one_of( (x + y) - y,
                                 (y + x) - y),
                 x);

Frequently, several related rewrite rules will have identical output
patterns and preconditions, differing only by the form of the input
pattern.  For example, both `(x + y) - y` and `(y + x) - y` can be
simplified to `x`.  Previously, these would be written as separate
rewrite rules, which required duplicating the output and
preconditions.

This commit introduces `PMatchesOneOf`, which attempts a series of
pattern matches, stopping after the first successful match.  The
`PMatchesOneOf` instance can be used as the input pattern for rewrite
rules.  A helper function `matches_one_of` is also added, which
returns an instance of `PMatchesOneOf`.

```c++
// Before
TVM_TRY_REWRITE( (x + y) - y, x);
TVM_TRY_REWRITE( (y + x) - y, x);

// After
TVM_TRY_REWRITE( matches_one_of( (x + y) - y,
                                 (y + x) - y),
                 x);
```
@tvm-bot
Copy link
Collaborator

tvm-bot commented Feb 8, 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

@Lunderberg
Copy link
Contributor Author

This arose from some experiments regarding RewriteSimplifier that didn't end up materializing. During development, it was error prone to make identical updates across several related rules, so I put this utility together.

The performance when using PMatchesOneOf is matched to the performance when specifying rules independently. The call to analyzer.rewrite_simplify(test_case.before) was benchmarked for each expression in tests/python/unittest/test_arith_rewrite_simplify.py, with no deviations noticed. Each data point in the plots below is the benchmark results of one of the expressions.

image
image

@Lunderberg Lunderberg force-pushed the pattern_match_one_of branch from f286f87 to ac22e3e Compare February 9, 2023 15:48
Copy link
Contributor

@wrongtest-intellif wrongtest-intellif left a comment

Choose a reason for hiding this comment

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

LGTM.
All rules are checked compatible with previous. Many thanks!

@Lunderberg Lunderberg merged commit a543f6b into apache:main Feb 17, 2023
@Lunderberg Lunderberg deleted the pattern_match_one_of branch February 17, 2023 21:23
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Apr 7, 2023
This simplification was introduced in
apache#13217, and was erroneously removed
in apache#13933.  This commit re-enables
this simplification, and adds unit tests to prevent any future
regression.
areusch pushed a commit that referenced this pull request Apr 7, 2023
This simplification was introduced in
#13217, and was erroneously removed
in #13933.  This commit re-enables
this simplification, and adds unit tests to prevent any future
regression.
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Jun 21, 2023
Follow-up to apache#13933, where a comment
pointed out a few rewrite rules that were nearly redundant.  These
were rules that needed to distinguish between output of `max(x-y, 0)`
and `max(0, x-y)`, to match previously expected output.

This commit adds explicit collection of constants to the RHS of
add/mul/min/max.
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.

3 participants