Skip to content

Conversation

@StrongerXi
Copy link
Contributor

Refactor duplicated and low-level logic for applying constant-folding over an Expr into a utility function.

@tvm-bot
Copy link
Collaborator

tvm-bot commented Nov 10, 2022

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.

  • No users to tag found in teams: relay See #10317 for details
  • Built docs for commit bb67e29 can be found here.

Generated by tvm-bot

const_mod = transform::FoldConstant()(const_mod);
GlobalVar const_main = const_mod->GetGlobalVar("main");
Expr const_val = Downcast<Function>(const_mod->functions[const_main])->body;
Expr const_val = FoldConstantExpr(const_expr);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMHO this keeps the transformation logic a lot cleaner, and allows future reuse.

@StrongerXi
Copy link
Contributor Author

Since it's just a refactor, CI should suffice for testing.

Copy link
Contributor

@echuraev echuraev left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks

const_mod = transform::FoldConstant()(const_mod);
GlobalVar const_main = const_mod->GetGlobalVar("main");
return Downcast<Function>(const_mod->functions[const_main])->body;
}
Copy link
Member

Choose a reason for hiding this comment

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

There is already

Expr FoldConstantExpr(const Expr& expr, const IRModule& mod, bool fold_qnn) {
. You can probably just use that.

And it's odd to add this function in pattern_utils.h.

Copy link
Contributor Author

@StrongerXi StrongerXi Nov 10, 2022

Choose a reason for hiding this comment

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

You can probably just use that.

Thanks, I did see that one, but (1). currently it doesn't seem to be declared in any header, and (2). the extra mod argument is not ergonomic for our use case here.

And it's odd to add this function in pattern_utils.h.

I agree, I did this for consistency because we already have InferType right above from #13275.

I think they should probably both be moved to either pass_utils.h or a new transform_utils.h.

Regardless, I also just found this. It's in contrib, but if we are okay with using it I'll go ahead.

Let me know what you think, I'll update the PR accordingly:).

Copy link
Member

@masahi masahi Nov 10, 2022

Choose a reason for hiding this comment

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

Okay how about this:

  • Introduce fold_constant.h and declare FoldConstantExpr(const Expr& expr) and FoldConstantExpr(const Expr& expr, const IRModule& mod, bool fold_qnn).
  • Implement the former one in terms of the latter.
  • Remove all duplicated definitions of FoldConstantExpr and use the new ones above.

Copy link
Contributor Author

@StrongerXi StrongerXi Nov 11, 2022

Choose a reason for hiding this comment

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

I like that, done!

Also caught another duplication in relay/quantize/realize.cc. Let me know if this PR needs more tweaking.

@StrongerXi StrongerXi force-pushed the refactor-relay-constant-fold-expr-logic branch from 0a58854 to fb22a27 Compare November 11, 2022 00:16
Comment on lines 435 to 438
[](const Expr& expr, const IRModule& mod, bool fold_qnn) {
return FoldConstantExpr(expr, mod, fold_qnn);
}
);
Copy link
Contributor Author

@StrongerXi StrongerXi Nov 11, 2022

Choose a reason for hiding this comment

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

This is needed to resolve the name overloading -- I decided to keep the FoldConstantExpr names.

@StrongerXi StrongerXi force-pushed the refactor-relay-constant-fold-expr-logic branch from fb22a27 to 479a78a Compare November 11, 2022 01:47
@StrongerXi
Copy link
Contributor Author

Make linter happy.

@StrongerXi StrongerXi force-pushed the refactor-relay-constant-fold-expr-logic branch from 479a78a to bb67e29 Compare November 11, 2022 02:37
@StrongerXi
Copy link
Contributor Author

Fix build failure in contrib, which wasn't triggered locally from default build.

@masahi masahi merged commit f9ed60a into apache:main Nov 11, 2022
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.

4 participants