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

[Relax] Refactor PatternRewriter into separate Block/Expr mutators #16730

Merged
merged 1 commit into from
Mar 26, 2024

Conversation

Lunderberg
Copy link
Contributor

Prior to this commit, the PatternRewriter mutator handled pattern rewriting at either the expression level (rewrite_call) or the dataflow block level (rewrite_bindings). These two functionalities had different external APIs, defined diffierent member variables, and visited different IR nodes. In effect, it had two entirely independent implementations, which just happened to be implemented within the same class.

This commit refactors the single PatternRewriter mutator into separate BlockPatternRewriter and ExprPatternRewriter mutators.

Prior to this commit, the `PatternRewriter` mutator handled pattern
rewriting at either the expression level (`rewrite_call`) or the
dataflow block level (`rewrite_bindings`).  These two functionalities
had different external APIs, defined diffierent member variables, and
visited different IR nodes.  In effect, it had two entirely
independent implementations, which just happened to be implemented
within the same class.

This commit refactors the single `PatternRewriter` mutator into
separate `BlockPatternRewriter` and `ExprPatternRewriter` mutators.
@tqchen
Copy link
Member

tqchen commented Mar 16, 2024

related to the class but not this PR necessarily would be good to think about complexity reduction for rewrite_bindings, which is source of slowdown in our overall compilation

@Lunderberg
Copy link
Contributor Author

Agreed, and I think this PR helps move in that direction. By separating out implementation of rewrite_bindings (BlockPatternRewriter) from the implementation of rewrite_call (ExprPatternRewriter), the improvements needed for rewrite_bindings should be easier to make.

I wanted to separate the two implementations before making an improvement specific to rewrite_call, to avoid introducing complexity in rewrite_bindings.

Copy link
Contributor

@slyubomirsky slyubomirsky left a comment

Choose a reason for hiding this comment

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

This seems like a clean refactor, thank you for separating out the functionality.

@Lunderberg Lunderberg merged commit 016b512 into apache:main Mar 26, 2024
19 checks passed
@Lunderberg Lunderberg deleted the relax_refactor_dataflow_matcher branch March 26, 2024 13:43
thaisacs pushed a commit to thaisacs/tvm that referenced this pull request Apr 3, 2024
…pache#16730)

Prior to this commit, the `PatternRewriter` mutator handled pattern
rewriting at either the expression level (`rewrite_call`) or the
dataflow block level (`rewrite_bindings`).  These two functionalities
had different external APIs, defined diffierent member variables, and
visited different IR nodes.  In effect, it had two entirely
independent implementations, which just happened to be implemented
within the same class.

This commit refactors the single `PatternRewriter` mutator into
separate `BlockPatternRewriter` and `ExprPatternRewriter` mutators.
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Jul 8, 2024
A follow-up to apache#16730.  Now that the
implementations for `rewrite_call` and `rewrite_bindings` are in
separate classes, they can be further split out into separate files.
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Jul 10, 2024
A follow-up to apache#16730.  Now that the
implementations for `rewrite_call` and `rewrite_bindings` are in
separate classes, they can be further split out into separate files.
Lunderberg added a commit to Lunderberg/tvm that referenced this pull request Jul 10, 2024
A follow-up to apache#16730.  Now that the
implementations for `rewrite_call` and `rewrite_bindings` are in
separate classes, they can be further split out into separate files.
sunggg pushed a commit that referenced this pull request Jul 24, 2024
* [TVMScript][Bugfix] Normalize relax::If with function's TIR var

Prior to this commit, the branches of `relax::If` were normalized
using `EraseToWellDefinedInScope`, using a fresh variable scope.
While this had the intended behavior of preventing variables defined
in a single branch from being usable outside of the conditional, it
also caused the conditional's branches to treat function-scope
symbolic variables as if they were undefined.

This commit updates the `tvm::relax::Normalizer` so that `relax::If`
is normalized within an inherited scope.  This preserves the previous
behavior for symbolic variables defined within a branch, but allows
shapes within a branch to use symbolic variables defined outside of
the branch.

* [Relax] Canonicalize known symbolic shapes in Relax expressions

Prior to this commit, known constants in Relax functions would be
inlined by the `CanonicalizeBindings` pass, but only if they appeared as Relax
expressions (e.g. `R.const` or `R.prim_value`).  Known constants that
appeared as TIR variables (e.g. symbolic shapes) would be kept as
dynamic parameters, even if they were known at compile time.

This commit updates the `CanonicalizeBindings` pass to identify known
values of symbolic shapes, and to use these known values in shape
expressions.

* [Relax][Refactor] Reorganize pattern-matching

A follow-up to #16730.  Now that the
implementations for `rewrite_call` and `rewrite_bindings` are in
separate classes, they can be further split out into separate files.

* [Relax][Refactor] Implement Rewriter class for pattern-rewrite

Prior to this commit, the pattern to be matched and the rewrite to be
performed were provided as separate arguments.  This commit introduces
a new class `ExprRewriter`, which contains both parts.

This abstraction will make it easier to combine multiple different
rewrite rules, applying them in a single pass.

* lint fixes

* Remove unnecessary change which broke a unit test

* lint fix for import order

* Add docstrings

* lint fix

* Lint fix

* lint fixes

* lint fix

* Update based on review comments

* Add test case for matching against arbitrary dtype

* Fix breakage in unit tests

One unit test that had been relying on invalid shape propagation.
Another unit test that required constructed an ill-formed output to
test against.

* Updated base class name from ExprRewriter to PatternMatchingRewriter

* lint fix
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