Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

[torch_tvm] Support Lowering to TVM even if node cannot be fused #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

serhaty
Copy link

@serhaty serhaty commented Sep 23, 2019

We take PT JIT IR graph and fuse those nodes of the graph that can be lowered to Relay graph in TVM. However the current fusion logic does not create a subgraph out of a single node. You need at least two nodes that can be lowered and only then it will fuse them and create a subgraph.

In this PR, we want to enable subgraph creation for single nodes that cannot be fused with any other node. We achieve this by checking for the case where consumer can be lowered but producer cannot be.

@serhaty serhaty marked this pull request as ready for review September 23, 2019 23:18
REQ((canHandle(consumer, aliasDb) || consumer->kind() == getTVMSymbol()));

// if producer cannot be converted, check if consumer can be lowered to TVM
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should do this outside of this function. tryMerge is supposed to merge two nodes into a lowerable node. Iideally we start with a seed outside. Maybe

if (canLowerSeed(consumer

If seed cannnot be lowered, we never come to tryMerge?

Also @bwasti, maybe you should chime in here.

Copy link

Choose a reason for hiding this comment

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

Yeah, I think we should put this kind of logic in parallel to tryMerge instead of inside. I actually think we should have some kind of flag to control minimal number of ops in order to create a fusion group. But this might not be the scope of the PR.

Copy link
Author

Choose a reason for hiding this comment

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

I initially implemented as two passes:

  1. Lower all nodes that can be lowered
  2. Do Fusion among nodes that are already lowered

1 works fine but fusing two tvm nodes does not quire work as is.

ref_out, tvm_out = self.runBoth(linear, input, weight, bias)
assert torch.allclose(ref_out, tvm_out, rtol=0.01, atol=0.01)

# check to verify fustion still works
Copy link
Contributor

Choose a reason for hiding this comment

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

spell check on 'fusion'.


# check single node graph
def linear(a, b, c):
return F.linear(a, b, c)
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you check if the test fails without your changes?

Copy link
Author

Choose a reason for hiding this comment

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

yeap, it does indeed crash for single op version. For fusion one, I added to make sure future changes do not break it.

Copy link
Contributor

@kimishpatel kimishpatel left a comment

Choose a reason for hiding this comment

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

Left a couple of comments. Also asked @bwasti to chime in.

@kimishpatel
Copy link
Contributor

Also maybe we can add an flag to enable this feature or not? Or maybe just check if it affects performance?

REQ((canHandle(consumer, aliasDb) || consumer->kind() == getTVMSymbol()));

// if producer cannot be converted, check if consumer can be lowered to TVM
Copy link

Choose a reason for hiding this comment

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

Yeah, I think we should put this kind of logic in parallel to tryMerge instead of inside. I actually think we should have some kind of flag to control minimal number of ops in order to create a fusion group. But this might not be the scope of the PR.

// Already converted so return no change
return c10::nullopt;
}
// prooceed to convert current node to TVM
Copy link

Choose a reason for hiding this comment

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

Typo

@serhaty serhaty force-pushed the lower branch 4 times, most recently from 4bfc690 to b8eaeec Compare September 26, 2019 17:11
@serhaty serhaty requested a review from kimishpatel September 26, 2019 21:54
if(!aliasDb.isMutable(consumer)){
REQ(!aliasDb.hasOutputWriters(consumer));
}
consumer = SubgraphUtils::createSingletonSubgraph(consumer, getTVMSymbol());
Copy link
Contributor

Choose a reason for hiding this comment

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

I also meant to suggest that we not only check if it can be lowered, but if it can be we create the singleton graph outside. Thus tryMerge always has consumer that is already lowered and it only does merging but never creation.
I think it will be cleaner this way. Sorry if it feels like I am dragging much want to drag it much.

@lly-zero-one
Copy link
Contributor

Can you try the flow on the quantized model to see how many subgraph we could get now?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants