You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/12/03 01:35:02 UTC

[GitHub] [tvm] saurabh-shandilya opened a new issue, #13543: Partition vs MergeComposite

saurabh-shandilya opened a new issue, #13543:
URL: https://github.com/apache/tvm/issues/13543

   I am running a pattern-matching test as described in the code below but it keeps failing with an error message. I tried to run match and replace the same pattern with partition and it works fine. I am curious about the reason for failure for mergeComposite when partition can work fine. 
   
   ```
   def test_pattern() -> tvm.relay.dataflow_pattern.DFPattern:
       score = wildcard()
       greater_p = is_op("greater")(score, is_constant())
       where_p = is_op("argwhere")(greater_p)
       sqz_p = is_op("squeeze")(where_p)
       return sqz_p
   
   from tvm.relay.op.contrib.register import  register_pattern_table
   @register_pattern_table("test_table1")
   def pattern_table():
       return [
           ("test_pattern", test_pattern()),
       ]
   def run_mergecomposite_pass(mod, params=None, **opts):
       seq = tvm.ir.transform.Sequential(
           [
               tvm.relay.transform.MergeComposite(pattern_table()),
           ]
       )
       return seq(mod)
   
   def test_partition_merge_composite():
       score = relay.var("score", shape=[7], dtype="float32")
       box = relay.var("box", shape=[7, 4], dtype="float32")
       g = relay.greater(score, relay.const(0.01))
       w = relay.argwhere(g)
       s = relay.squeeze(w, axis=[1])
       tb = (box, s)
       boxout = relay.adv_index(tb)
       scoreout = relay.adv_index((score, s))
       sourceexpand = relay.expand_dims(scoreout, axis=-1)
       finalout = relay.concatenate([boxout, sourceexpand], axis=-1)
   
       ## Following works fine
       mod_p = test_pattern().partition(finalout,{'Composite': 'test_pattern'})
       print(mod_p)
   
       ## But this does not
       mod1 = tvm.IRModule.from_expr(finalout)
       print(mod1)
       mod = run_mergecomposite_pass(mod1)
       print("after mergecomposite...")
       print(mod)
   
   ```
   
   ### Expected behavior
   
   I was expecting mergeComposite to work fine and since parition for the same pattern works fine. 
   
   ### Actual behavior
   
   I observed following error -
    _Check failed: (ref->template IsInstance<typename SubRef::ContainerType>()) is false: Downcast from IncompleteType to relay.TensorType failed._
   
   
   ### Environment
   
   tvm 0.9.dev0
   Windows
   torch 1.10.2
   torchvision 0.11.3
   
   ### Steps to reproduce
   
   Script is shared above.
   
   ### Triage
   
   
   * needs-triage
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] masahi closed issue #13543: Partition vs MergeComposite

Posted by GitBox <gi...@apache.org>.
masahi closed issue #13543: Partition vs MergeComposite
URL: https://github.com/apache/tvm/issues/13543


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] masahi commented on issue #13543: Partition vs MergeComposite

Posted by GitBox <gi...@apache.org>.
masahi commented on issue #13543:
URL: https://github.com/apache/tvm/issues/13543#issuecomment-1340313703

   If you look at the backtrace, you can see that the issue is coming from `AdvIndexRel`. And then you can look for the place where the downcast to `TensorType` occurs. It's here https://github.com/apache/tvm/blob/5d15428994fee408ed14807fad4362ef2d783d25/src/relay/op/tensor/transform.cc#L4074.
   
   `Partition` and `MergeComposite` serve different purposes, and they are intended to be used together like this https://github.com/apache/tvm/blob/d2f9f254d275df256dbcbc5a9f8b3a07cee1d81f/tests/python/relay/test_pass_partition_graph.py#L999-L1001. In particular, partition doesn't do pattern matching.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] masahi commented on issue #13543: Partition vs MergeComposite

Posted by GitBox <gi...@apache.org>.
masahi commented on issue #13543:
URL: https://github.com/apache/tvm/issues/13543#issuecomment-1343312874

   @saurabh-shandilya Would be great if you can send a PR with this fix and your test case.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] saurabh-shandilya commented on issue #13543: Partition vs MergeComposite

Posted by GitBox <gi...@apache.org>.
saurabh-shandilya commented on issue #13543:
URL: https://github.com/apache/tvm/issues/13543#issuecomment-1340280107

   @masahi Thanks for a quick fix, it works now. What was the process in this case to find the root cause. The error message - 
   "Check failed: (ref->template IsInstance()) is false: Downcast from IncompleteType to relay.TensorType failed." didn't really indicate the source. 
   
   Another question - Is a graph created using partitioning going to be "structure_equal" to graph created using mergecomposite? It looked same to me when I generated two, but "structure_equal" check failed between the 2 graphs. 
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] masahi commented on issue #13543: Partition vs MergeComposite

Posted by GitBox <gi...@apache.org>.
masahi commented on issue #13543:
URL: https://github.com/apache/tvm/issues/13543#issuecomment-1338262757

   Good catch, the following fix should work. Feel free to send a PR with your test case.
   
   https://github.com/apache/tvm/commit/133a995c2739bd2a180170e4bc89f5769e120ca2


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [tvm] saurabh-shandilya commented on issue #13543: Partition vs MergeComposite

Posted by GitBox <gi...@apache.org>.
saurabh-shandilya commented on issue #13543:
URL: https://github.com/apache/tvm/issues/13543#issuecomment-1347088613

   @masahi Sure I will submit a PR. By the way my question about partition code that I used in my example above. 
   ` mod_p = test_pattern().partition(finalout,{'Composite': 'test_pattern'})`
   
   I wrote following test for this but it fails. 
   
   ```
   def test_partition_merge_composite():
       score = relay.var("score", shape=[7], dtype="float32")
       box = relay.var("box", shape=[7, 4], dtype="float32")
       g = relay.greater(score, relay.const(0.01))
       w = relay.argwhere(g)
       s = relay.squeeze(w, axis=[1])
       tb = (box, s)
       boxout = relay.adv_index(tb)
       scoreout = relay.adv_index((score, s))
       sourceexpand = relay.expand_dims(scoreout, axis=-1)
       finalout = relay.concatenate([boxout, sourceexpand], axis=-1)
   
       # Use partition
       partitioned_expr = test_pattern().partition(finalout,{'Composite': 'test_pattern'})
       print(partitioned_expr)
   
       # Use merge composite
       mod = tvm.IRModule.from_expr(finalout)
       mod_m = run_mergecomposite_pass(mod)
       merge_composite_expr=mod_m["main"].body
       print(merge_composite_expr)    
       
       assert tvm.ir.structural_equal(merge_composite_expr,partitioned_expr)
   ```
   
   I printed the 2 expressions and they look almost same except that the first one has shape information but the check failed. 
   
   partitioned_expr is as below
   > free_var %score: Tensor[(7), float32];
   > %2 = fn (%FunctionVar_0_0, PartitionedFromPattern="greater_argwhere_squeeze_", Composite="test_pattern") {
   >   %0 = greater(%FunctionVar_0_0, 0.01f);
   >   %1 = argwhere(%0);
   >   squeeze(%1, axis=[1])
   > };
   > free_var %box: Tensor[(7, 4), float32];
   > %3 = %2(%score);
   > %4 = (%box, %3);
   > %5 = (%score, %3);
   > %6 = adv_index(%5);
   > %7 = adv_index(%4);
   > %8 = expand_dims(%6, axis=-1);
   > %9 = (%7, %8);
   > concatenate(%9, axis=-1)
   
   merge_composite_expr is as below
   > free_var %score: Tensor[(7), float32] /* ty=Tensor[(7), float32] */;
   > %2 = fn (%FunctionVar_0_0: Tensor[(7), float32] /* ty=Tensor[(7), float32] */, PartitionedFromPattern="greater_argwhere_squeeze_", Composite="test_pattern") -> Tensor[(?), int32] {
   >   %0 = greater(%FunctionVar_0_0, 0.01f /* ty=float32 */) /* ty=Tensor[(7), bool] */;
   >   %1 = argwhere(%0) /* ty=Tensor[(?, 1), int32] */;
   >   squeeze(%1, axis=[1]) /* ty=Tensor[(?), int32] */
   > } /* ty=fn (Tensor[(7), float32]) -> Tensor[(?), int32] */;
   > free_var %box: Tensor[(7, 4), float32] /* ty=Tensor[(7, 4), float32] */;
   > %3 = %2(%score) /* ty=Tensor[(?), int32] */;
   > %4 = (%box, %3) /* ty=(Tensor[(7, 4), float32], Tensor[(?), int32]) */;
   > %5 = (%score, %3) /* ty=(Tensor[(7), float32], Tensor[(?), int32]) */;
   > %6 = adv_index(%5) /* ty=Tensor[(?), float32] */;
   > %7 = adv_index(%4) /* ty=Tensor[(?, 4), float32] */;
   > %8 = expand_dims(%6, axis=-1) /* ty=Tensor[(?, 1), float32] */;
   > %9 = (%7, %8) /* ty=(Tensor[(?, 4), float32], Tensor[(?, 1), float32]) */;
   > concatenate(%9, axis=-1) /* ty=Tensor[(?, 5), float32] */


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org