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

[Relay] Legalize pass #3672

Merged
merged 6 commits into from
Aug 6, 2019
Merged

[Relay] Legalize pass #3672

merged 6 commits into from
Aug 6, 2019

Conversation

anijain2305
Copy link
Contributor

Relevant Issue - #3670

This pass transforms an expression to other expression.

This pass has many usecases

  • Replace a expr to another expr, if the other expr has faster performance.
  • For ASICs, we might want to modify the inputs to adapt to the HW support.
  • Alter op layout can work in conjunction with this pass.

The supporting usecase is the Intel i8 x i8 conv. Intel HW supports u8 x i8 conv
in HW. Using this pass, we can replace an i8 x i8 conv to a sequence of
operators where one of the operators is now u8 x i8 conv.

// Returns the altered expression
Expr OpRewriter(const Call &ref_call,
const Array<Expr> &new_args,
const NodeRef& ctx) {
Copy link
Member

Choose a reason for hiding this comment

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

align indent

if (fop_rewrite.count(op)) {
tvm::Array<tvm::Tensor> tinfos;
for (auto expr : ref_call->args) {
auto ttype = expr->type_as<TensorTypeNode>();
Copy link
Member

Choose a reason for hiding this comment

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

it could be TupleTypeNode

bool modified = false;
if (fop_rewrite.count(op)) {
tvm::Array<tvm::Tensor> tinfos;
for (auto expr : ref_call->args) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
for (auto expr : ref_call->args) {
for (auto& expr : ref_call->args) {

@anijain2305 anijain2305 force-pushed the intel_int8 branch 2 times, most recently from 793e109 to 214a333 Compare July 31, 2019 21:52
This pass transforms an expression to other expression.

This pass has many usecases
 * Replace a expr to another expr, if the other expr has faster performance.
 * For ASICs, we might want to modify the inputs to adapt to the HW support.
 * Alter op layout can work in conjunction with this pass.

The supporting usecase is the Intel i8 x i8 conv. Intel HW supports u8 x i8 conv
in HW. Using this pass, we can replace an i8 x i8 conv to a sequence of
operators where one of the operators is now u8 x i8 conv. This will also help
automatic quantizaion performance.
@tqchen
Copy link
Member

tqchen commented Aug 1, 2019

Let us debate on the name of the pass. Rewrite is a too generic. @FrozenGene @yzhliu @merrymercy @jroesch any thoughts?

@anijain2305
Copy link
Contributor Author

How about Strength Reduction? Not sure. But the idea is to replace one expr with other expr with the goal that it will make it faster. On the other hand, Strength Reduction might sound target agnostic, but this one is not.

@FrozenGene
Copy link
Member

yeah, I also think Rewrite is too generic. Especially we have another api named as RewriteAnnotatedOps. Rewrite make us confuse these two apis at the first glance. Maybe we could name it ReweiteXXXOps like RewriteAnnotatedOps?

@tqchen
Copy link
Member

tqchen commented Aug 2, 2019

Let us call it Legalize, as per LLVM terminology. https://wiki.aalto.fi/display/t1065450/LLVM+Legalization+and+Lowering

The first step when to designing API is to check conventional APIs and try to make things as consistent as possible.

@tqchen tqchen mentioned this pull request Aug 2, 2019
@anijain2305
Copy link
Contributor Author

Thanks for the pointers for considering APIs. I have changed the name to Legalize.

Please review again.

@anijain2305 anijain2305 changed the title [Relay] Rewrite pass. [Relay] Legalize pass Aug 2, 2019
@FrozenGene
Copy link
Member

I will review the code tomorrow. Sorry for this. Because Monday is always very busy. :-(

Copy link
Member

@FrozenGene FrozenGene left a comment

Choose a reason for hiding this comment

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

LGTM. Some comments of code should be handled.

@@ -435,6 +435,21 @@ def AlterOpLayout():
return _transform.AlterOpLayout()


def Legalize():
"""Rewrites a expression with another expression.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should change Rewrites to Legalize

# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Test alter op layout pass"""
Copy link
Member

Choose a reason for hiding this comment

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

Test legalize pass

Copy link
Member

@yzhliu yzhliu left a comment

Choose a reason for hiding this comment

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

We can merge once @FrozenGene 's comments have been addressed.

@icemelon
Copy link
Member

icemelon commented Aug 6, 2019

Should we move the expansion of batch_norm op from simplify inference to legalize pass?

@anijain2305
Copy link
Contributor Author

move the expansion of batch_norm op from simplify inference to legalize pass?

I like it. This is a target-independent pass though (which is fine, if everybody agrees to it).

Simplify_inference is anyways a vague name. From the name, it seems like something really big is happening inside :)

But, I would suggest doing it in a separate PR to avoid complicating this PR, as it will likely involve the discussion of when we should call this pass.

@yzhliu yzhliu merged commit 79922bd into apache:master Aug 6, 2019
@yzhliu
Copy link
Member

yzhliu commented Aug 6, 2019

Thanks @anijain2305 @tqchen @FrozenGene @icemelon9

wweic pushed a commit to wweic/tvm that referenced this pull request Aug 9, 2019
* [Relay] Rewrite pass.

This pass transforms an expression to other expression.

This pass has many usecases
 * Replace a expr to another expr, if the other expr has faster performance.
 * For ASICs, we might want to modify the inputs to adapt to the HW support.
 * Alter op layout can work in conjunction with this pass.

The supporting usecase is the Intel i8 x i8 conv. Intel HW supports u8 x i8 conv
in HW. Using this pass, we can replace an i8 x i8 conv to a sequence of
operators where one of the operators is now u8 x i8 conv. This will also help
automatic quantizaion performance.

* Better API name.

* Removing the conv2d legalization for x86. Will send a separate PR.

* Test name changes.

* Registering one funtion to register FTVMLegalize.

* Better comments.
wweic pushed a commit to neo-ai/tvm that referenced this pull request Sep 6, 2019
* [Relay] Rewrite pass.

This pass transforms an expression to other expression.

This pass has many usecases
 * Replace a expr to another expr, if the other expr has faster performance.
 * For ASICs, we might want to modify the inputs to adapt to the HW support.
 * Alter op layout can work in conjunction with this pass.

The supporting usecase is the Intel i8 x i8 conv. Intel HW supports u8 x i8 conv
in HW. Using this pass, we can replace an i8 x i8 conv to a sequence of
operators where one of the operators is now u8 x i8 conv. This will also help
automatic quantizaion performance.

* Better API name.

* Removing the conv2d legalization for x86. Will send a separate PR.

* Test name changes.

* Registering one funtion to register FTVMLegalize.

* Better comments.
@anijain2305 anijain2305 deleted the intel_int8 branch November 13, 2019 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants